From a42654dea8761a6cbc0eda04865528ea43c3c6ce Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Wed, 7 Apr 2021 21:13:15 +0800 Subject: [PATCH] * can create a new file --- RedPandaIDE/RedPandaIDE.pro | 4 +- RedPandaIDE/editor.cpp | 47 ++++++++++++++++++++-- RedPandaIDE/editor.h | 1 + RedPandaIDE/editorlist.cpp | 29 +++++++++++++- RedPandaIDE/editorlist.h | 18 ++++++--- RedPandaIDE/mainwindow.cpp | 15 ++++++++ RedPandaIDE/mainwindow.h | 9 +++++ RedPandaIDE/mainwindow.ui | 77 +++++++++++++++++++++---------------- RedPandaIDE/utils.cpp | 15 +++++--- RedPandaIDE/utils.h | 2 +- 10 files changed, 165 insertions(+), 52 deletions(-) diff --git a/RedPandaIDE/RedPandaIDE.pro b/RedPandaIDE/RedPandaIDE.pro index ba16a00b..0b01ce31 100644 --- a/RedPandaIDE/RedPandaIDE.pro +++ b/RedPandaIDE/RedPandaIDE.pro @@ -2,7 +2,7 @@ QT += core gui 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. # In order to do so, uncomment the following line. @@ -31,3 +31,5 @@ TRANSLATIONS += \ qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target + +LIBS += -L$$PWD -lqscintilla2_qt5 diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index cd040bba..b95fa59f 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -1,6 +1,9 @@ #include "editor.h" #include +#include + +using namespace std; Editor::Editor(QObject *parent, const QString& filename, @@ -15,12 +18,18 @@ Editor::Editor(QObject *parent, const QString& filename, mParentPageControl(parentPageControl) { mTextEdit = new QsciScintilla(); + if (mFilename.isEmpty()) { + mFilename = tr("untitled") + "1"; + } QFileInfo fileInfo(mFilename); mParentPageControl->addTab(mTextEdit,fileInfo.fileName()); if (!isNew) { loadFile(); } else { - mFileEncoding = etAscii; + if (mEncodingType == etAuto) + mFileEncoding = etAscii; + else + mFileEncoding = mEncodingType; } } @@ -36,16 +45,46 @@ void Editor::loadFile() { } switch(mFileEncoding) { case etUTF8: - mTextEdit->setText(UTF8toQString(ba)); + mTextEdit->setText(QString::fromUtf8(ba)); break; case etUTF8Bom: - mTextEdit->setText(UTF8toQString(ba.mid(3))); + mTextEdit->setText(QString::fromUtf8(ba.mid(3))); break; 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 { return mEncodingType; } diff --git a/RedPandaIDE/editor.h b/RedPandaIDE/editor.h index a4089791..07e8baa3 100644 --- a/RedPandaIDE/editor.h +++ b/RedPandaIDE/editor.h @@ -23,6 +23,7 @@ public: bool isNew() const; void loadFile(); + void saveFile(const QString& filename); signals: diff --git a/RedPandaIDE/editorlist.cpp b/RedPandaIDE/editorlist.cpp index 52b6574e..4ba8ef78 100644 --- a/RedPandaIDE/editorlist.cpp +++ b/RedPandaIDE/editorlist.cpp @@ -1,6 +1,33 @@ #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; +} + diff --git a/RedPandaIDE/editorlist.h b/RedPandaIDE/editorlist.h index 9f016d16..361f1506 100644 --- a/RedPandaIDE/editorlist.h +++ b/RedPandaIDE/editorlist.h @@ -1,14 +1,14 @@ #ifndef EDITORLIST_H #define EDITORLIST_H -#include #include #include #include +#include "utils.h" -class EditorList : public QObject +class Editor; +class EditorList { - Q_OBJECT public: enum ShowType{ lstNone, @@ -16,11 +16,19 @@ public: lstRight, 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: ShowType mLayout; diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index 41a26bdf..dd276798 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -1,11 +1,17 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#include "editorlist.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); + mEditorList = new EditorList(ui->EditorTabsLeft, + ui->EditorTabsRight, + ui->EditorPanelSplitter, + ui->EditorPanel); + setupActions(); } MainWindow::~MainWindow() @@ -13,3 +19,12 @@ MainWindow::~MainWindow() delete ui; } +void MainWindow::setupActions() { + +} + + +void MainWindow::on_actionNew_triggered() +{ + mEditorList->NewEditor("",etAuto,false,true); +} diff --git a/RedPandaIDE/mainwindow.h b/RedPandaIDE/mainwindow.h index 4643e322..88000c78 100644 --- a/RedPandaIDE/mainwindow.h +++ b/RedPandaIDE/mainwindow.h @@ -7,6 +7,8 @@ QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE +class EditorList; + class MainWindow : public QMainWindow { Q_OBJECT @@ -15,7 +17,14 @@ public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); +private slots: + void on_actionNew_triggered(); + +private: + void setupActions(); + private: Ui::MainWindow *ui; + EditorList* mEditorList; }; #endif // MAINWINDOW_H diff --git a/RedPandaIDE/mainwindow.ui b/RedPandaIDE/mainwindow.ui index 280c8959..712e901c 100644 --- a/RedPandaIDE/mainwindow.ui +++ b/RedPandaIDE/mainwindow.ui @@ -54,7 +54,7 @@ - + 2 @@ -63,39 +63,17 @@ - - - - - 1 - - - - Tab 1 - - - - - Tab 2 - - - - - - - - - Tab 1 - - - - - Tab 2 - - - - - + + + Qt::Horizontal + + + + -1 + + + + @@ -140,8 +118,39 @@ 25 + + + File + + + + + + + + toolBar + + + TopToolBarArea + + + false + + + + + + + New + + + + + Open... + + diff --git a/RedPandaIDE/utils.cpp b/RedPandaIDE/utils.cpp index db54da92..0e273c18 100644 --- a/RedPandaIDE/utils.cpp +++ b/RedPandaIDE/utils.cpp @@ -50,10 +50,13 @@ FileEncodingType GetFileEncodingType(const QByteArray& content){ return etUTF8; } -QString UTF8toQString(const QByteArray& ba){ - QTextCodec* tc = QTextCodec::codecForName("UTF-8"); - if (tc == NULL) - return QString(); - else - return tc->toUnicode(ba); +bool isTextAllAscii(const QString& text) { + for (QChar c:text) { + if (c.unicode()>127) { + return false; + } + } + return true; } + + diff --git a/RedPandaIDE/utils.h b/RedPandaIDE/utils.h index 684ad2ec..213e03c8 100644 --- a/RedPandaIDE/utils.h +++ b/RedPandaIDE/utils.h @@ -14,6 +14,6 @@ enum FileEncodingType { FileEncodingType GetFileEncodingType(const QByteArray& content); -QString UTF8toQString(const QByteArray& ba); +bool isTextAllAscii(const QString& text); #endif // UTILS_H