* can create a new file

This commit is contained in:
Roy Qu 2021-04-07 21:13:15 +08:00 committed by royqh1979@gmail.com
parent 69644b18b5
commit a42654dea8
10 changed files with 165 additions and 52 deletions

View File

@ -2,7 +2,7 @@ QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11 CONFIG += c++14
# You can make your code fail to compile if it uses deprecated APIs. # You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line. # In order to do so, uncomment the following line.
@ -31,3 +31,5 @@ TRANSLATIONS += \
qnx: target.path = /tmp/$${TARGET}/bin qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target !isEmpty(target.path): INSTALLS += target
LIBS += -L$$PWD -lqscintilla2_qt5

View File

@ -1,6 +1,9 @@
#include "editor.h" #include "editor.h"
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <memory>
using namespace std;
Editor::Editor(QObject *parent, const QString& filename, Editor::Editor(QObject *parent, const QString& filename,
@ -15,12 +18,18 @@ Editor::Editor(QObject *parent, const QString& filename,
mParentPageControl(parentPageControl) mParentPageControl(parentPageControl)
{ {
mTextEdit = new QsciScintilla(); mTextEdit = new QsciScintilla();
if (mFilename.isEmpty()) {
mFilename = tr("untitled") + "1";
}
QFileInfo fileInfo(mFilename); QFileInfo fileInfo(mFilename);
mParentPageControl->addTab(mTextEdit,fileInfo.fileName()); mParentPageControl->addTab(mTextEdit,fileInfo.fileName());
if (!isNew) { if (!isNew) {
loadFile(); loadFile();
} else { } else {
mFileEncoding = etAscii; if (mEncodingType == etAuto)
mFileEncoding = etAscii;
else
mFileEncoding = mEncodingType;
} }
} }
@ -36,16 +45,46 @@ void Editor::loadFile() {
} }
switch(mFileEncoding) { switch(mFileEncoding) {
case etUTF8: case etUTF8:
mTextEdit->setText(UTF8toQString(ba)); mTextEdit->setText(QString::fromUtf8(ba));
break; break;
case etUTF8Bom: case etUTF8Bom:
mTextEdit->setText(UTF8toQString(ba.mid(3))); mTextEdit->setText(QString::fromUtf8(ba.mid(3)));
break; break;
default: default:
mTextEdit->setText(QString(ba)); mTextEdit->setText(QString::fromLocal8Bit(ba));
} }
} }
void Editor::saveFile(const QString &filename) {
if (mEncodingType!=etAuto && mEncodingType!=mFileEncoding) {
mFileEncoding = mEncodingType;
}
if (mEncodingType ==etAuto && mFileEncoding == etAscii) {
if (!isTextAllAscii(mTextEdit->text())) {
mFileEncoding = etAnsi;
}
//todo: update status bar, and set fileencoding using configurations
}
QFile file(filename);
QByteArray ba;
switch(mFileEncoding) {
case etUTF8:
ba = mTextEdit->text().toUtf8();
break;
case etUTF8Bom:
ba.resize(3);
ba[0]=0xEF;
ba[1]=0xBB;
ba[2]=0xBF;
ba.append(mTextEdit->text().toUtf8());
break;
default:
ba = mTextEdit->text().toLocal8Bit();
}
file.write(ba);
file.close();
}
FileEncodingType Editor::encodingType() const { FileEncodingType Editor::encodingType() const {
return mEncodingType; return mEncodingType;
} }

View File

@ -23,6 +23,7 @@ public:
bool isNew() const; bool isNew() const;
void loadFile(); void loadFile();
void saveFile(const QString& filename);
signals: signals:

View File

@ -1,6 +1,33 @@
#include "editorlist.h" #include "editorlist.h"
#include "editor.h"
EditorList::EditorList(QObject *parent) : QObject(parent) EditorList::EditorList(QTabWidget* leftPageWidget,
QTabWidget* rightPageWidget,
QSplitter* splitter,
QWidget* panel):
mLeftPageWidget(leftPageWidget),
mRightPageWidget(rightPageWidget),
mSplitter(splitter),
mPanel(panel),
mUpdateCount(0)
{ {
} }
Editor* EditorList::NewEditor(const QString& filename, FileEncodingType encoding,
bool inProject, bool newFile,
QTabWidget* page) {
QTabWidget * parentPageControl = NULL;
if (page == NULL)
parentPageControl = GetNewEditorPageControl();
else
parentPageControl = page;
return new Editor(parentPageControl,filename,encoding,inProject,newFile,parentPageControl);
//UpdateLayout;
}
QTabWidget* EditorList::GetNewEditorPageControl() {
//todo: return widget depends on layout
return mLeftPageWidget;
}

View File

@ -1,14 +1,14 @@
#ifndef EDITORLIST_H #ifndef EDITORLIST_H
#define EDITORLIST_H #define EDITORLIST_H
#include <QObject>
#include <QTabWidget> #include <QTabWidget>
#include <QSplitter> #include <QSplitter>
#include <QWidget> #include <QWidget>
#include "utils.h"
class EditorList : public QObject class Editor;
class EditorList
{ {
Q_OBJECT
public: public:
enum ShowType{ enum ShowType{
lstNone, lstNone,
@ -16,11 +16,19 @@ public:
lstRight, lstRight,
lstBoth lstBoth
}; };
explicit EditorList(QObject *parent = nullptr);
explicit EditorList(QTabWidget* leftPageWidget,
QTabWidget* rightPageWidget,
QSplitter* splitter,
QWidget* panel);
Editor* NewEditor(const QString& filename, FileEncodingType encoding,
bool inProject, bool newFile,
QTabWidget* page=NULL);
private:
QTabWidget* GetNewEditorPageControl();
signals:
private: private:
ShowType mLayout; ShowType mLayout;

View File

@ -1,11 +1,17 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "editorlist.h"
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
{ {
ui->setupUi(this); ui->setupUi(this);
mEditorList = new EditorList(ui->EditorTabsLeft,
ui->EditorTabsRight,
ui->EditorPanelSplitter,
ui->EditorPanel);
setupActions();
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -13,3 +19,12 @@ MainWindow::~MainWindow()
delete ui; delete ui;
} }
void MainWindow::setupActions() {
}
void MainWindow::on_actionNew_triggered()
{
mEditorList->NewEditor("",etAuto,false,true);
}

View File

@ -7,6 +7,8 @@ QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } namespace Ui { class MainWindow; }
QT_END_NAMESPACE QT_END_NAMESPACE
class EditorList;
class MainWindow : public QMainWindow class MainWindow : public QMainWindow
{ {
Q_OBJECT Q_OBJECT
@ -15,7 +17,14 @@ public:
MainWindow(QWidget *parent = nullptr); MainWindow(QWidget *parent = nullptr);
~MainWindow(); ~MainWindow();
private slots:
void on_actionNew_triggered();
private:
void setupActions();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
EditorList* mEditorList;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -54,7 +54,7 @@
</attribute> </attribute>
</widget> </widget>
</widget> </widget>
<widget class="QWidget" name="widget_2" native="true"> <widget class="QWidget" name="EditorPanel" native="true">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>2</horstretch> <horstretch>2</horstretch>
@ -63,39 +63,17 @@
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_2"> <widget class="QSplitter" name="EditorPanelSplitter">
<item> <property name="orientation">
<widget class="QTabWidget" name="tabWidget_3"> <enum>Qt::Horizontal</enum>
<property name="currentIndex"> </property>
<number>1</number> <widget class="QTabWidget" name="EditorTabsLeft">
</property> <property name="currentIndex">
<widget class="QWidget" name="tab_5"> <number>-1</number>
<attribute name="title"> </property>
<string>Tab 1</string> </widget>
</attribute> <widget class="QTabWidget" name="EditorTabsRight"/>
</widget> </widget>
<widget class="QWidget" name="tab_6">
<attribute name="title">
<string>Tab 2</string>
</attribute>
</widget>
</widget>
</item>
<item>
<widget class="QTabWidget" name="tabWidget_4">
<widget class="QWidget" name="tab_7">
<attribute name="title">
<string>Tab 1</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_8">
<attribute name="title">
<string>Tab 2</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</item> </item>
</layout> </layout>
</widget> </widget>
@ -140,8 +118,39 @@
<height>25</height> <height>25</height>
</rect> </rect>
</property> </property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
</widget>
<addaction name="menuFile"/>
</widget> </widget>
<widget class="QStatusBar" name="statusbar"/> <widget class="QStatusBar" name="statusbar"/>
<widget class="QToolBar" name="toolBar">
<property name="windowTitle">
<string>toolBar</string>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
</widget>
<action name="actionNew">
<property name="text">
<string>New</string>
</property>
</action>
<action name="actionOpen">
<property name="text">
<string>Open...</string>
</property>
</action>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>

View File

@ -50,10 +50,13 @@ FileEncodingType GetFileEncodingType(const QByteArray& content){
return etUTF8; return etUTF8;
} }
QString UTF8toQString(const QByteArray& ba){ bool isTextAllAscii(const QString& text) {
QTextCodec* tc = QTextCodec::codecForName("UTF-8"); for (QChar c:text) {
if (tc == NULL) if (c.unicode()>127) {
return QString(); return false;
else }
return tc->toUnicode(ba); }
return true;
} }

View File

@ -14,6 +14,6 @@ enum FileEncodingType {
FileEncodingType GetFileEncodingType(const QByteArray& content); FileEncodingType GetFileEncodingType(const QByteArray& content);
QString UTF8toQString(const QByteArray& ba); bool isTextAllAscii(const QString& text);
#endif // UTILS_H #endif // UTILS_H