* basic editing functions done

This commit is contained in:
royqh1979@gmail.com 2021-04-11 21:33:08 +08:00
parent 7600035d09
commit 0d2a82e741
11 changed files with 214 additions and 47 deletions

View File

@ -14,6 +14,7 @@ SOURCES += \
main.cpp \ main.cpp \
mainwindow.cpp \ mainwindow.cpp \
settings.cpp \ settings.cpp \
systemconsts.cpp \
utils.cpp utils.cpp
HEADERS += \ HEADERS += \
@ -21,6 +22,7 @@ HEADERS += \
editorlist.h \ editorlist.h \
mainwindow.h \ mainwindow.h \
settings.h \ settings.h \
systemconsts.h \
utils.h utils.h
FORMS += \ FORMS += \

View File

@ -8,6 +8,7 @@
#include <memory> #include <memory>
#include "settings.h" #include "settings.h"
#include "mainwindow.h" #include "mainwindow.h"
#include "systemconsts.h"
#include <Qsci/qscilexercpp.h> #include <Qsci/qscilexercpp.h>
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
@ -15,6 +16,19 @@
using namespace std; using namespace std;
SaveException::SaveException(const QString& reason) {
mReason = reason;
}
SaveException::SaveException(const QString&& reason) {
mReason = reason;
}
const QString& SaveException::reason() const noexcept{
return mReason;
}
const char *SaveException::what() const noexcept {
return mReason.toLocal8Bit();
}
Editor::Editor(QWidget *parent, const QString& filename, Editor::Editor(QWidget *parent, const QString& filename,
const QByteArray& encoding, const QByteArray& encoding,
@ -92,6 +106,8 @@ Editor::Editor(QWidget *parent, const QString& filename,
this, SLOT(onCursorPositionChanged(int,int))); this, SLOT(onCursorPositionChanged(int,int)));
connect(this, SIGNAL(linesChanged()), connect(this, SIGNAL(linesChanged()),
this,SLOT(onLinesChanged())); this,SLOT(onLinesChanged()));
this->toggleComment
} }
Editor::~Editor() { Editor::~Editor() {
@ -107,7 +123,13 @@ Editor::~Editor() {
void Editor::loadFile() { void Editor::loadFile() {
QFile file(mFilename); QFile file(mFilename);
QByteArray content=file.read(file.bytesAvailable()); if (!file.open(QFile::ReadOnly)) {
QMessageBox::information(pMainWindow,
tr("Error"),
QString(tr("Can't Open File %1:%2")).arg(mFilename).arg(file.errorString()));
}
QByteArray content=file.readAll();
file.close();
if (mEncodingOption == ENCODING_AUTO_DETECT) { if (mEncodingOption == ENCODING_AUTO_DETECT) {
mFileEncoding = GuessTextEncoding(content); mFileEncoding = GuessTextEncoding(content);
} else { } else {
@ -119,6 +141,8 @@ void Editor::loadFile() {
this->setText(QString::fromUtf8(content.mid(3))); this->setText(QString::fromUtf8(content.mid(3)));
} else if (mFileEncoding == ENCODING_ASCII) { } else if (mFileEncoding == ENCODING_ASCII) {
this->setText(QString::fromLatin1(content)); this->setText(QString::fromLatin1(content));
} else if (mFileEncoding == ENCODING_SYSTEM_DEFAULT) {
this->setText(QString::fromLocal8Bit(content));
}else { }else {
QTextCodec*codec = QTextCodec::codecForName(mFileEncoding); QTextCodec*codec = QTextCodec::codecForName(mFileEncoding);
this->setText(codec->toUnicode(content)); this->setText(codec->toUnicode(content));
@ -148,30 +172,42 @@ void Editor::saveFile(const QString &filename) {
ba.append(this->text().toUtf8()); ba.append(this->text().toUtf8());
} else if (mFileEncoding == ENCODING_ASCII) { } else if (mFileEncoding == ENCODING_ASCII) {
ba = this->text().toLatin1(); ba = this->text().toLatin1();
} else if (mFileEncoding == ENCODING_SYSTEM_DEFAULT) {
ba = this->text().toLocal8Bit();
} else { } else {
QTextCodec* codec = QTextCodec::codecForName(mFileEncoding); QTextCodec* codec = QTextCodec::codecForName(mFileEncoding);
ba = codec->fromUnicode(this->text()); ba = codec->fromUnicode(this->text());
} }
file.open(QFile::WriteOnly); if (file.open(QFile::WriteOnly)) {
file.write(ba); if (file.write(ba)<0) {
file.close(); throw SaveException(QString(tr("Failed to Save file %1: %2")).arg(filename).arg(file.errorString()));
}
file.close();
} else {
throw SaveException(QString(tr("Failed to Open file %1: %2")).arg(filename).arg(file.errorString()));
}
} }
bool Editor::save(bool force, bool reparse) { bool Editor::save(bool force, bool reparse) {
if (this->mIsNew) { if (this->mIsNew) {
return saveAs(); return saveAs();
} }
QFile file(mFilename);
QFileInfo info(mFilename); QFileInfo info(mFilename);
//is this file writable;
if (!force && !info.isWritable()) { if (!force && !info.isWritable()) {
QMessageBox::information(pMainWindow,tr("Fail"), QMessageBox::information(pMainWindow,tr("Fail"),
QString(QObject::tr("File %s is not writable!"))); QString(QObject::tr("File %s is not writable!")));
return false; return false;
} }
//is this file read-only?
if (this->isModified() || force) { if (this->isModified() || force) {
saveFile(mFilename); try {
setModified(false); saveFile(mFilename);
setModified(false);
} catch (SaveException& exception) {
QMessageBox::information(pMainWindow,tr("Fail"),
exception.reason());
return false;
}
} }
if (reparse) { if (reparse) {
@ -181,15 +217,23 @@ bool Editor::save(bool force, bool reparse) {
} }
bool Editor::saveAs(){ bool Editor::saveAs(){
QString selectedFileFilter = pSystemConsts->defaultFileFilter();
QString newName = QFileDialog::getSaveFileName(pMainWindow, QString newName = QFileDialog::getSaveFileName(pMainWindow,
tr("Save As")); tr("Save As"), QString(), pSystemConsts->defaultFileFilters().join(";;"),
&selectedFileFilter);
if (newName.isEmpty()) { if (newName.isEmpty()) {
return false; return false;
} }
saveFile(newName); try {
mFilename = newName; saveFile(mFilename);
mIsNew = false; mFilename = newName;
setModified(false); mIsNew = false;
setModified(false);
} catch (SaveException& exception) {
QMessageBox::information(pMainWindow,tr("Fail"),
exception.reason());
return false;
}
//todo: update (reassign highlighter) //todo: update (reassign highlighter)
//todo: remove old file from parser and reparse file //todo: remove old file from parser and reparse file
@ -199,26 +243,26 @@ bool Editor::saveAs(){
return true; return true;
} }
const QByteArray& Editor::encodingOption() const { const QByteArray& Editor::encodingOption() const noexcept{
return mEncodingOption; return mEncodingOption;
} }
void Editor::setEncodingOption(const QByteArray& encoding) { void Editor::setEncodingOption(const QByteArray& encoding) noexcept{
mEncodingOption = encoding; mEncodingOption = encoding;
} }
const QByteArray& Editor::fileEncoding() const { const QByteArray& Editor::fileEncoding() const noexcept{
return mFileEncoding; return mFileEncoding;
} }
const QString& Editor::filename() { const QString& Editor::filename() const noexcept{
return mFilename; return mFilename;
} }
bool Editor::inProject() const { bool Editor::inProject() const noexcept{
return mInProject; return mInProject;
} }
bool Editor::isNew() const { bool Editor::isNew() const noexcept {
return mIsNew; return mIsNew;
} }
QTabWidget* Editor::pageControl() { QTabWidget* Editor::pageControl() noexcept{
return mParentPageControl; return mParentPageControl;
} }
@ -232,17 +276,15 @@ void Editor::wheelEvent(QWheelEvent *event) {
} }
} }
void Editor::onModificationChanged(bool status) { void Editor::onModificationChanged(bool) {
updateCaption(); updateCaption();
} }
void Editor::onCursorPositionChanged(int line, int index) void Editor::onCursorPositionChanged(int line, int index) {
{
pMainWindow->updateStatusBarForEditingInfo(line,index+1,lines(),text().length()); pMainWindow->updateStatusBarForEditingInfo(line,index+1,lines(),text().length());
} }
void Editor::onLinesChanged() void Editor::onLinesChanged() {
{
qDebug()<<"lala"<<lines(); qDebug()<<"lala"<<lines();
} }

View File

@ -6,6 +6,19 @@
#include <QTabWidget> #include <QTabWidget>
#include <Qsci/qsciscintilla.h> #include <Qsci/qsciscintilla.h>
class SaveException: public std::exception {
public:
explicit SaveException(const QString& reason);
explicit SaveException(const QString&& reason);
// exception interface
const QString& reason() const noexcept;
public:
const char *what() const noexcept override;
private:
QString mReason;
};
class Editor : public QsciScintilla class Editor : public QsciScintilla
{ {
Q_OBJECT Q_OBJECT
@ -22,19 +35,19 @@ public:
Editor& operator=(const Editor&) = delete; Editor& operator=(const Editor&) = delete;
Editor& operator=(const Editor&&) = delete; Editor& operator=(const Editor&&) = delete;
const QByteArray& encodingOption() const; const QByteArray& encodingOption() const noexcept;
void setEncodingOption(const QByteArray& encoding); void setEncodingOption(const QByteArray& encoding) noexcept;
const QByteArray& fileEncoding() const; const QByteArray& fileEncoding() const noexcept;
const QString& filename(); const QString& filename() const noexcept;
bool inProject() const; bool inProject() const noexcept;
bool isNew() const; bool isNew() const noexcept;
void loadFile(); void loadFile();
void saveFile(const QString& filename); void saveFile(const QString& filename);
bool save(bool force=false, bool reparse=true); bool save(bool force=false, bool reparse=true);
bool saveAs(); bool saveAs();
QTabWidget* pageControl(); QTabWidget* pageControl() noexcept;
void updateCaption(const QString& newCaption=QString()); void updateCaption(const QString& newCaption=QString());
@ -42,9 +55,9 @@ signals:
protected slots: protected slots:
void onModificationChanged(bool status); void onModificationChanged(bool status) ;
void onCursorPositionChanged(int line, int index); void onCursorPositionChanged(int line, int index) ;
void onLinesChanged(); void onLinesChanged() ;
private: private:
QByteArray mEncodingOption; // the encoding type set by the user QByteArray mEncodingOption; // the encoding type set by the user

View File

@ -123,3 +123,20 @@ bool EditorList::closeAll(bool force) {
return true; return true;
} }
Editor* EditorList::findOpenedEditor(const QString &filename)
{
for (int i=0;i<mLeftPageWidget->count();i++) {
Editor* e = static_cast<Editor*>(mLeftPageWidget->widget(i));
if (e->filename() == filename) {
return e;
}
}
for (int i=0;i<mRightPageWidget->count();i++) {
Editor* e = static_cast<Editor*>(mRightPageWidget->widget(i));
if (e->filename() == filename) {
return e;
}
}
return nullptr;
}

View File

@ -40,6 +40,8 @@ public:
bool closeAll(bool force = false); bool closeAll(bool force = false);
Editor* findOpenedEditor(const QString& filename);
void beginUpdate(); void beginUpdate();
void endUpdate(); void endUpdate();

View File

@ -1,14 +1,17 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "settings.h" #include "settings.h"
#include "systemconsts.h"
#include <QApplication> #include <QApplication>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication app(argc, argv);
Settings s; SystemConsts systemConsts;
pSettings = &s; pSystemConsts = &systemConsts;
MainWindow w; Settings settings;
pMainWindow = &w; pSettings = &settings;
w.show(); MainWindow mainWindow;
return a.exec(); pMainWindow = &mainWindow;
mainWindow.show();
return app.exec();
} }

View File

@ -2,8 +2,11 @@
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "editorlist.h" #include "editorlist.h"
#include "editor.h" #include "editor.h"
#include "systemconsts.h"
#include "settings.h"
#include <QCloseEvent> #include <QCloseEvent>
#include <QFileDialog>
#include <QLabel> #include <QLabel>
MainWindow* pMainWindow; MainWindow* pMainWindow;
@ -50,6 +53,28 @@ void MainWindow::updateStatusBarForEditingInfo(int line,int col,int lines,int ch
} }
} }
void MainWindow::openFiles(const QStringList &files)
{
mEditorList->beginUpdate();
for (QString file:files) {
openFile(file);
}
mEditorList->endUpdate();
}
void MainWindow::openFile(const QString &filename)
{
Editor* editor = mEditorList->findOpenedEditor(filename);
if (editor!=nullptr) {
editor->setFocus();
return;
}
editor = mEditorList->newEditor(filename,ENCODING_AUTO_DETECT,
false,false);
editor->setFocus();
this->updateStatusBarForEncoding();
}
void MainWindow::setupActions() { void MainWindow::setupActions() {
} }
@ -69,10 +94,11 @@ void MainWindow::on_EditorTabsLeft_tabCloseRequested(int index)
void MainWindow::on_actionOpen_triggered() void MainWindow::on_actionOpen_triggered()
{ {
Editor * editor = mEditorList->getEditor(); QString selectedFileFilter = pSystemConsts->defaultFileFilter();
if (editor != NULL) { QStringList files = QFileDialog::getOpenFileNames(pMainWindow,
//editor->save(); tr("Open"), QString(), pSystemConsts->defaultFileFilters().join(";;"),
} &selectedFileFilter);
openFiles(files);
} }
void MainWindow::closeEvent(QCloseEvent *event) { void MainWindow::closeEvent(QCloseEvent *event) {

View File

@ -21,7 +21,9 @@ public:
void updateStatusBarForEncoding(); void updateStatusBarForEncoding();
void updateStatusBarForEditingInfo(int line,int col,int lines,int charCount); void updateStatusBarForEditingInfo(int line,int col,int lines,int charCount);
protected:
void openFiles(const QStringList& files);
void openFile(const QString& filename);
private slots: private slots:
void on_actionNew_triggered(); void on_actionNew_triggered();

View File

@ -222,6 +222,9 @@
<property name="text"> <property name="text">
<string>New</string> <string>New</string>
</property> </property>
<property name="shortcut">
<string>Ctrl+N</string>
</property>
</action> </action>
<action name="actionOpen"> <action name="actionOpen">
<property name="icon"> <property name="icon">
@ -232,6 +235,9 @@
<property name="text"> <property name="text">
<string>Open...</string> <string>Open...</string>
</property> </property>
<property name="shortcut">
<string>Ctrl+O</string>
</property>
</action> </action>
<action name="actionSave"> <action name="actionSave">
<property name="icon"> <property name="icon">
@ -242,6 +248,9 @@
<property name="text"> <property name="text">
<string>Save</string> <string>Save</string>
</property> </property>
<property name="shortcut">
<string>Ctrl+S</string>
</property>
</action> </action>
<action name="actionSaveAs"> <action name="actionSaveAs">
<property name="icon"> <property name="icon">
@ -265,6 +274,9 @@
<property name="text"> <property name="text">
<string>Save All</string> <string>Save All</string>
</property> </property>
<property name="shortcut">
<string>Ctrl+Shift+S</string>
</property>
</action> </action>
</widget> </widget>
<resources/> <resources/>

View File

@ -0,0 +1,30 @@
#include "systemconsts.h"
#include <QObject>
#include <QString>
#include <QStringList>
SystemConsts* pSystemConsts;
SystemConsts::SystemConsts(): mDefaultFileFilters()
{
addDefaultFileFilter(QObject::tr("C files"),"*.c");
addDefaultFileFilter(QObject::tr("C++ files"),"*.cpp *.cc *.cxx");
addDefaultFileFilter(QObject::tr("Header files"),"*.h *.hh");
addDefaultFileFilter(QObject::tr("Text files"),"*.txt");
addDefaultFileFilter(QObject::tr("All files"),"*");
}
const QStringList &SystemConsts::defaultFileFilters() const noexcept
{
return mDefaultFileFilters;
}
const QString &SystemConsts::defaultFileFilter() const noexcept
{
return mDefaultFileFilters[1];
}
void SystemConsts::addDefaultFileFilter(const QString &name, const QString &fileExtensions)
{
mDefaultFileFilters.append(name+ " (" + fileExtensions+")");
}

View File

@ -0,0 +1,18 @@
#ifndef SYSTEMCONSTS_H
#define SYSTEMCONSTS_H
#include <QStringList>
class SystemConsts
{
public:
SystemConsts();
const QStringList& defaultFileFilters() const noexcept;
const QString& defaultFileFilter() const noexcept;
void addDefaultFileFilter(const QString& name, const QString& fileExtensions);
private:
QStringList mDefaultFileFilters;
};
extern SystemConsts* pSystemConsts;
#endif // SYSTEMCONSTS_H