* 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
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

View File

@ -1,6 +1,9 @@
#include "editor.h"
#include <QtCore/QFileInfo>
#include <memory>
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;
}

View File

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

View File

@ -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;
}

View File

@ -1,14 +1,14 @@
#ifndef EDITORLIST_H
#define EDITORLIST_H
#include <QObject>
#include <QTabWidget>
#include <QSplitter>
#include <QWidget>
#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;

View File

@ -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);
}

View File

@ -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

View File

@ -54,7 +54,7 @@
</attribute>
</widget>
</widget>
<widget class="QWidget" name="widget_2" native="true">
<widget class="QWidget" name="EditorPanel" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>2</horstretch>
@ -63,39 +63,17 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QTabWidget" name="tabWidget_3">
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="tab_5">
<attribute name="title">
<string>Tab 1</string>
</attribute>
</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>
<widget class="QSplitter" name="EditorPanelSplitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QTabWidget" name="EditorTabsLeft">
<property name="currentIndex">
<number>-1</number>
</property>
</widget>
<widget class="QTabWidget" name="EditorTabsRight"/>
</widget>
</item>
</layout>
</widget>
@ -140,8 +118,39 @@
<height>25</height>
</rect>
</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 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>
<resources/>
<connections/>

View File

@ -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;
}

View File

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