change font size with ctrl+mouse wheel

This commit is contained in:
royqh1979@gmail.com 2021-04-09 17:48:25 +08:00
parent 2b71ef16f4
commit 3fe5d81caa
9 changed files with 171 additions and 32 deletions

Binary file not shown.

View File

@ -1,3 +1,85 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="RedPandaIDE_zh_CN"></TS>
<TS version="2.1" language="zh_CN">
<context>
<name>Editor</name>
<message>
<location filename="editor.cpp" line="26"/>
<source>untitled</source>
<translation></translation>
</message>
</context>
<context>
<name>MainWindow</name>
<message>
<location filename="mainwindow.ui" line="14"/>
<source>Red Panda C++</source>
<translation>C++</translation>
</message>
<message>
<location filename="mainwindow.ui" line="77"/>
<location filename="mainwindow.ui" line="153"/>
<source>Tab 1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="82"/>
<location filename="mainwindow.ui" line="158"/>
<source>Tab 2</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="177"/>
<source>File</source>
<translation></translation>
</message>
<message>
<location filename="mainwindow.ui" line="191"/>
<source>toolBar</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="211"/>
<source>New</source>
<translation></translation>
</message>
<message>
<location filename="mainwindow.ui" line="221"/>
<source>Open...</source>
<translation>...</translation>
</message>
<message>
<location filename="mainwindow.ui" line="231"/>
<source>Save</source>
<translation></translation>
</message>
<message>
<location filename="mainwindow.ui" line="241"/>
<source>Save As...</source>
<translation>...</translation>
</message>
<message>
<location filename="mainwindow.ui" line="244"/>
<source>Save As</source>
<translation></translation>
</message>
<message>
<location filename="mainwindow.ui" line="254"/>
<source>Save All</source>
<translation></translation>
</message>
</context>
<context>
<name>QObject</name>
<message>
<location filename="editorlist.cpp" line="80"/>
<source>Save</source>
<translation></translation>
</message>
<message>
<location filename="editorlist.cpp" line="80"/>
<source>Save changes to %s?</source>
<translation>&quot;%s&quot;</translation>
</message>
</context>
</TS>

View File

@ -1,32 +1,35 @@
#include "editor.h"
#include <QtCore/QFileInfo>
#include <QFont>
#include <QTextCodec>
#include <QVariant>
#include <QWheelEvent>
#include <memory>
#include "settings.h"
#include "mainwindow.h"
#include <Qsci/qscilexercpp.h>
using namespace std;
Editor::Editor(QObject *parent, const QString& filename,
Editor::Editor(QWidget *parent, const QString& filename,
const QByteArray& encoding,
bool inProject, bool isNew,
QTabWidget* parentPageControl):
QObject(parent),
QsciScintilla(parent),
mFilename(filename),
mEncodingOption(encoding),
mInProject(inProject),
mIsNew(isNew),
mParentPageControl(parentPageControl)
{
mTextEdit = new QsciScintilla();
if (mFilename.isEmpty()) {
mFilename = tr("untitled") + "1";
}
QFileInfo fileInfo(mFilename);
mParentPageControl->addTab(mTextEdit,fileInfo.fileName());
if (mParentPageControl!=NULL)
mParentPageControl->addTab(this,fileInfo.fileName());
if (!isNew) {
loadFile();
} else {
@ -35,12 +38,24 @@ Editor::Editor(QObject *parent, const QString& filename,
else
mFileEncoding = mEncodingOption;
}
mTextEdit->setProperty("editor",QVariant::fromValue<intptr_t>((intptr_t)this));
//
QsciLexerCPP *lexer = new QsciLexerCPP();
lexer->setHighlightEscapeSequences(true);
this->setLexer(lexer);
this->setAutoIndent(pSettings->value(EDITOR_AUTO_INDENT).toBool());
}
Editor::~Editor() {
int index = mParentPageControl->indexOf(mTextEdit);
mParentPageControl->removeTab(index);
if (mParentPageControl!=NULL) {
int index = mParentPageControl->indexOf(this);
mParentPageControl->removeTab(index);
}
this->setParent(0);
delete this->lexer();
this->setLexer(NULL);
}
void Editor::loadFile() {
@ -53,14 +68,14 @@ void Editor::loadFile() {
mFileEncoding = mEncodingOption;
}
if (mFileEncoding == ENCODING_UTF8) {
mTextEdit->setText(QString::fromUtf8(ba));
this->setText(QString::fromUtf8(ba));
} else if (mFileEncoding == ENCODING_UTF8_BOM) {
mTextEdit->setText(QString::fromUtf8(ba.mid(3)));
this->setText(QString::fromUtf8(ba.mid(3)));
} else if (mFileEncoding == ENCODING_ASCII) {
mTextEdit->setText(QString::fromLatin1(ba));
this->setText(QString::fromLatin1(ba));
}else {
QTextCodec*codec = QTextCodec::codecForName(mFileEncoding);
mTextEdit->setText(codec->toUnicode(ba));
this->setText(codec->toUnicode(ba));
}
}
@ -69,7 +84,7 @@ void Editor::saveFile(const QString &filename) {
mFileEncoding = mEncodingOption;
}
if (mEncodingOption == ENCODING_AUTO_DETECT && mFileEncoding == ENCODING_ASCII) {
if (!isTextAllAscii(mTextEdit->text())) {
if (!isTextAllAscii(this->text())) {
mFileEncoding = pSettings->value(EDITOR_DEFAULT_ENCODING).toByteArray();
}
pMainWindow->updateStatusBarForEncoding();
@ -78,18 +93,18 @@ void Editor::saveFile(const QString &filename) {
QFile file(filename);
QByteArray ba;
if (mFileEncoding == ENCODING_UTF8) {
ba = mTextEdit->text().toUtf8();
ba = this->text().toUtf8();
} else if (mFileEncoding == ENCODING_UTF8_BOM) {
ba.resize(3);
ba[0]=0xEF;
ba[1]=0xBB;
ba[2]=0xBF;
ba.append(mTextEdit->text().toUtf8());
ba.append(this->text().toUtf8());
} else if (mFileEncoding == ENCODING_ASCII) {
ba = mTextEdit->text().toLatin1();
ba = this->text().toLatin1();
} else {
QTextCodec* codec = QTextCodec::codecForName(mFileEncoding);
ba = codec->fromUnicode(mTextEdit->text());
ba = codec->fromUnicode(this->text());
}
file.write(ba);
file.close();
@ -118,6 +133,17 @@ bool Editor::inProject() const {
bool Editor::isNew() const {
return mIsNew;
}
QsciScintilla* Editor::textEdit() {
return mTextEdit;
QTabWidget* Editor::pageControl() {
return mParentPageControl;
}
void Editor::wheelEvent(QWheelEvent *event) {
if ( (event->modifiers() & Qt::ControlModifier)!=0) {
if (event->angleDelta().y()>0) {
this->zoomIn();
} else {
this->zoomOut();
}
}
}

View File

@ -6,14 +6,13 @@
#include <QTabWidget>
#include <Qsci/qsciscintilla.h>
class Editor : public QObject
class Editor : public QsciScintilla
{
Q_OBJECT
public:
explicit Editor(QObject *parent, const QString& filename,
explicit Editor(QWidget *parent, const QString& filename,
const QByteArray& encoding,
bool inProject, bool isNew,
QTabWidget* parentPageControl);
bool inProject, bool isNew,QTabWidget* parentPageControl);
~Editor();
@ -28,7 +27,6 @@ public:
void saveFile(const QString& filename);
bool save(bool force=false, bool reparse=true);
QsciScintilla* textEdit();
QTabWidget* pageControl();
signals:
@ -39,7 +37,11 @@ private:
QTabWidget* mParentPageControl;
bool mInProject;
bool mIsNew;
QsciScintilla* mTextEdit;
// QWidget interface
protected:
void wheelEvent(QWheelEvent *event) override;
};
#endif // EDITOR_H

View File

@ -13,7 +13,6 @@ EditorList::UpdateLocker::~UpdateLocker() {
mEditorList->endUpdate();
}
}
EditorList::EditorList(QTabWidget* leftPageWidget,
QTabWidget* rightPageWidget,
QSplitter* splitter,
@ -62,10 +61,7 @@ Editor* EditorList::getEditor(int index, QTabWidget* tabsWidget) const {
if (index<0 || index >= selectedWidget->count()) {
return NULL;
}
QWidget* textEdit = selectedWidget->widget(index);
QVariant pop = textEdit->property("editor");
Editor *editor = (Editor*)pop.value<intptr_t>();
return editor;
return (Editor*)selectedWidget->widget(index);
}
bool EditorList::closeEditor(Editor* editor, bool transferFocus, bool force) {
@ -74,7 +70,7 @@ bool EditorList::closeEditor(Editor* editor, bool transferFocus, bool force) {
return false;
if (force) {
editor->save(true,false);
} else if ( (editor->textEdit()->isModified()) && (!editor->textEdit()->text().isEmpty())) {
} 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?"),
@ -88,7 +84,9 @@ bool EditorList::closeEditor(Editor* editor, bool transferFocus, bool force) {
}
}
if (transferFocus && editor-)
if (transferFocus && (editor->pageControl()->currentWidget()==editor)) {
//todo: activate & focus the previous editor
}
delete editor;
return true;
@ -111,5 +109,16 @@ void EditorList::endUpdate() {
bool EditorList::closeAll(bool force) {
UpdateLocker locker(this);
while (mLeftPageWidget->count()>0) {
if (!closeEditor(getEditor(0,mLeftPageWidget),false,force)) {
return false;
}
}
while (mRightPageWidget->count()>0) {
if (!closeEditor(getEditor(0,mRightPageWidget),false,force)) {
return false;
}
}
return true;
}

View File

@ -3,6 +3,7 @@
#include "editorlist.h"
#include "editor.h"
#include <QCloseEvent>
#include <QLabel>
MainWindow* pMainWindow;
@ -63,3 +64,14 @@ void MainWindow::on_actionOpen_triggered()
editor->save();
}
}
void MainWindow::closeEvent(QCloseEvent *event) {
if (!mEditorList->closeAll(true)) {
event->ignore();
return ;
}
delete mEditorList;
event->accept();
return;
}

View File

@ -20,6 +20,7 @@ public:
void updateStatusBarForEncoding();
private slots:
void on_actionNew_triggered();
@ -35,6 +36,10 @@ private:
EditorList* mEditorList;
QLabel* mFileInfoStatus;
QLabel* mFileEncodingStatus;
// QWidget interface
protected:
void closeEvent(QCloseEvent *event) override;
};
extern MainWindow* pMainWindow;

View File

@ -9,6 +9,7 @@ Settings::Settings():
// default values for editors
setDefault(EDITOR_DEFAULT_ENCODING, QTextCodec::codecForLocale()->name());
setDefault(EDITOR_AUTO_INDENT,true);
}

View File

@ -4,6 +4,8 @@
#include <QSettings>
#define EDITOR_DEFAULT_ENCODING "editor/default_encoding"
#define EDITOR_AUTO_INDENT "editor/default_auto_indent"
class Settings
{