* refactor of the editor class for better C++ 11 support
This commit is contained in:
parent
3fe5d81caa
commit
74b9b59bbd
|
@ -9,6 +9,9 @@
|
|||
#include "settings.h"
|
||||
#include "mainwindow.h"
|
||||
#include <Qsci/qscilexercpp.h>
|
||||
#include <QFileDialog>
|
||||
#include <QMessageBox>
|
||||
#include <QDebug>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -28,8 +31,10 @@ Editor::Editor(QWidget *parent, const QString& filename,
|
|||
mFilename = tr("untitled") + "1";
|
||||
}
|
||||
QFileInfo fileInfo(mFilename);
|
||||
if (mParentPageControl!=NULL)
|
||||
mParentPageControl->addTab(this,fileInfo.fileName());
|
||||
if (mParentPageControl!=NULL) {
|
||||
mParentPageControl->addTab(this,QString());
|
||||
updateCaption();
|
||||
}
|
||||
if (!isNew) {
|
||||
loadFile();
|
||||
} else {
|
||||
|
@ -45,6 +50,9 @@ Editor::Editor(QWidget *parent, const QString& filename,
|
|||
this->setLexer(lexer);
|
||||
this->setAutoIndent(pSettings->value(EDITOR_AUTO_INDENT).toBool());
|
||||
|
||||
// connect will fail if use new function pointer syntax
|
||||
connect(this,SIGNAL(modificationChanged(bool)),
|
||||
this,SLOT(onModificationChanged(bool)));
|
||||
}
|
||||
|
||||
Editor::~Editor() {
|
||||
|
@ -59,23 +67,22 @@ Editor::~Editor() {
|
|||
}
|
||||
|
||||
void Editor::loadFile() {
|
||||
QStringList strs;
|
||||
QFile file(mFilename);
|
||||
QByteArray ba=file.read(file.bytesAvailable());
|
||||
QByteArray content=file.read(file.bytesAvailable());
|
||||
if (mEncodingOption == ENCODING_AUTO_DETECT) {
|
||||
mFileEncoding = GetFileEncodingType(ba);
|
||||
mFileEncoding = GuessTextEncoding(content);
|
||||
} else {
|
||||
mFileEncoding = mEncodingOption;
|
||||
}
|
||||
if (mFileEncoding == ENCODING_UTF8) {
|
||||
this->setText(QString::fromUtf8(ba));
|
||||
this->setText(QString::fromUtf8(content));
|
||||
} else if (mFileEncoding == ENCODING_UTF8_BOM) {
|
||||
this->setText(QString::fromUtf8(ba.mid(3)));
|
||||
this->setText(QString::fromUtf8(content.mid(3)));
|
||||
} else if (mFileEncoding == ENCODING_ASCII) {
|
||||
this->setText(QString::fromLatin1(ba));
|
||||
this->setText(QString::fromLatin1(content));
|
||||
}else {
|
||||
QTextCodec*codec = QTextCodec::codecForName(mFileEncoding);
|
||||
this->setText(codec->toUnicode(ba));
|
||||
this->setText(codec->toUnicode(content));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -106,12 +113,50 @@ void Editor::saveFile(const QString &filename) {
|
|||
QTextCodec* codec = QTextCodec::codecForName(mFileEncoding);
|
||||
ba = codec->fromUnicode(this->text());
|
||||
}
|
||||
file.open(QFile::WriteOnly);
|
||||
file.write(ba);
|
||||
file.close();
|
||||
}
|
||||
|
||||
bool Editor::save(bool force, bool reparse) {
|
||||
if (this->mIsNew) {
|
||||
return saveAs();
|
||||
}
|
||||
QFile file(mFilename);
|
||||
QFileInfo info(mFilename);
|
||||
if (!force && !info.isWritable()) {
|
||||
QMessageBox::information(pMainWindow,tr("Fail"),
|
||||
QString(QObject::tr("File %s is not writable!")));
|
||||
return false;
|
||||
}
|
||||
//is this file read-only?
|
||||
if (this->isModified() || force) {
|
||||
saveFile(mFilename);
|
||||
setModified(false);
|
||||
}
|
||||
|
||||
if (reparse) {
|
||||
//todo: reparse the file
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Editor::saveAs(){
|
||||
QString newName = QFileDialog::getSaveFileName(pMainWindow,
|
||||
tr("Save As"));
|
||||
if (newName.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
saveFile(newName);
|
||||
mFilename = newName;
|
||||
mIsNew = false;
|
||||
setModified(false);
|
||||
|
||||
//todo: update (reassign highlighter)
|
||||
//todo: remove old file from parser and reparse file
|
||||
//todo: unmoniter/ monitor file
|
||||
//todo: update windows caption
|
||||
//todo: update class browser;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -147,3 +192,27 @@ void Editor::wheelEvent(QWheelEvent *event) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Editor::onModificationChanged(bool status) {
|
||||
qDebug()<<"???";
|
||||
updateCaption();
|
||||
}
|
||||
|
||||
void Editor::updateCaption(const QString& newCaption) {
|
||||
if (mParentPageControl==NULL) {
|
||||
return;
|
||||
}
|
||||
int index = mParentPageControl->indexOf(this);
|
||||
if (index==-1)
|
||||
return;
|
||||
if (newCaption.isEmpty()) {
|
||||
QString caption = QFileInfo(mFilename).fileName();
|
||||
if (this->isModified()) {
|
||||
caption.append("[*]");
|
||||
}
|
||||
mParentPageControl->setTabText(index,caption);
|
||||
} else {
|
||||
mParentPageControl->setTabText(index,newCaption);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,6 +16,12 @@ public:
|
|||
|
||||
~Editor();
|
||||
|
||||
//tell the compiler to prohibit copy/moving editor objects ( we should only use pointers to the editor object)
|
||||
Editor(const Editor&) = delete;
|
||||
Editor(const Editor&&) = delete;
|
||||
Editor& operator=(const Editor&) = delete;
|
||||
Editor& operator=(const Editor&&) = delete;
|
||||
|
||||
const QByteArray& encodingOption() const;
|
||||
void setEncodingOption(const QByteArray& encoding);
|
||||
const QByteArray& fileEncoding() const;
|
||||
|
@ -26,10 +32,18 @@ public:
|
|||
void loadFile();
|
||||
void saveFile(const QString& filename);
|
||||
bool save(bool force=false, bool reparse=true);
|
||||
bool saveAs();
|
||||
|
||||
QTabWidget* pageControl();
|
||||
|
||||
void updateCaption(const QString& newCaption=QString());
|
||||
|
||||
signals:
|
||||
|
||||
|
||||
protected slots:
|
||||
void onModificationChanged(bool status);
|
||||
|
||||
private:
|
||||
QByteArray mEncodingOption; // the encoding type set by the user
|
||||
QByteArray mFileEncoding; // the real encoding of the file (auto detected)
|
||||
|
|
|
@ -73,7 +73,8 @@ bool EditorList::closeEditor(Editor* editor, bool transferFocus, bool force) {
|
|||
} else if ( (editor->isModified()) && (!editor->text().isEmpty())) {
|
||||
// ask user if he wants to save
|
||||
QMessageBox::StandardButton reply;
|
||||
reply = QMessageBox::question(pMainWindow,QObject::tr("Save"),QObject::tr("Save changes to %s?"),
|
||||
reply = QMessageBox::question(pMainWindow,QObject::tr("Save"),
|
||||
QString(QObject::tr("Save changes to %1?")).arg(editor->filename()),
|
||||
QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
|
||||
if (reply == QMessageBox::Cancel) {
|
||||
return false;
|
||||
|
|
|
@ -10,7 +10,7 @@ class Editor;
|
|||
class EditorList
|
||||
{
|
||||
public:
|
||||
enum ShowType{
|
||||
enum class ShowType{
|
||||
lstNone,
|
||||
lstLeft,
|
||||
lstRight,
|
||||
|
|
|
@ -61,7 +61,7 @@ void MainWindow::on_actionOpen_triggered()
|
|||
{
|
||||
Editor * editor = mEditorList->getEditor();
|
||||
if (editor != NULL) {
|
||||
editor->save();
|
||||
//editor->save();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -75,3 +75,19 @@ void MainWindow::closeEvent(QCloseEvent *event) {
|
|||
event->accept();
|
||||
return;
|
||||
}
|
||||
|
||||
void MainWindow::on_actionSave_triggered()
|
||||
{
|
||||
Editor * editor = mEditorList->getEditor();
|
||||
if (editor != NULL) {
|
||||
editor->save();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_actionSaveAs_triggered()
|
||||
{
|
||||
Editor * editor = mEditorList->getEditor();
|
||||
if (editor != NULL) {
|
||||
editor->saveAs();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,10 @@ private slots:
|
|||
|
||||
void on_actionOpen_triggered();
|
||||
|
||||
void on_actionSave_triggered();
|
||||
|
||||
void on_actionSaveAs_triggered();
|
||||
|
||||
private:
|
||||
void setupActions();
|
||||
|
||||
|
|
|
@ -3,11 +3,11 @@
|
|||
#include <QString>
|
||||
#include <QTextCodec>
|
||||
|
||||
const QByteArray GetFileEncodingType(const QByteArray& content){
|
||||
const QByteArray GuessTextEncoding(const QByteArray& text){
|
||||
bool allAscii;
|
||||
int ii;
|
||||
int size;
|
||||
const QByteArray& s=content;
|
||||
const QByteArray& s=text;
|
||||
size = s.length();
|
||||
if ( (size >= 3) && ((unsigned char)s[0]==0xEF) && ((unsigned char)s[1]==0xBB) && ((unsigned char)s[2]==0xBF)) {
|
||||
return ENCODING_UTF8_BOM;
|
||||
|
|
|
@ -9,9 +9,7 @@ class QString;
|
|||
#define ENCODING_UTF8_BOM "UTF-8 BOM"
|
||||
#define ENCODING_SYSTEM_DEFAULT "SYSTEM"
|
||||
#define ENCODING_ASCII "ASCII"
|
||||
|
||||
|
||||
const QByteArray GetFileEncodingType(const QByteArray& content);
|
||||
const QByteArray GuessTextEncoding(const QByteArray& text);
|
||||
|
||||
bool isTextAllAscii(const QString& text);
|
||||
|
||||
|
|
Loading…
Reference in New Issue