- enhancement: new file template

- fix: when an editor is created, its caret will be displayed even it doesn't have focus
This commit is contained in:
royqh1979@gmail.com 2021-10-20 12:11:36 +08:00
parent 25d83d0e8d
commit ae2f3596aa
12 changed files with 268 additions and 151 deletions

View File

@ -2,6 +2,8 @@ Version 0.7.0
- fix: Backspace still works in readonly mode
- fix: save as file dialog's operation mode is not correct
- enhancement: fill indents in the editor (Turned off by default)
- enhancement: new file template
- fix: when an editor is created, its caret will be displayed even it doesn't have focus
Version 0.6.8
- enhancement: add link to cppreference in the help menu

View File

@ -19,7 +19,6 @@ SOURCES += \
caretlist.cpp \
codeformatter.cpp \
codesnippetsmanager.cpp \
codetemplate.cpp \
colorscheme.cpp \
compiler/projectcompiler.cpp \
platform.cpp \
@ -133,7 +132,6 @@ HEADERS += \
caretlist.h \
codeformatter.h \
codesnippetsmanager.h \
codetemplate.h \
colorscheme.h \
compiler/compiler.h \
compiler/compilermanager.h \

View File

@ -273,69 +273,89 @@
<context>
<name>CodeSnippetsManager</name>
<message>
<location filename="codesnippetsmanager.cpp" line="25"/>
<location filename="codesnippetsmanager.cpp" line="35"/>
<location filename="codesnippetsmanager.cpp" line="37"/>
<location filename="codesnippetsmanager.cpp" line="47"/>
<source>Load default code snippets failed</source>
<translation></translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="26"/>
<location filename="codesnippetsmanager.cpp" line="36"/>
<location filename="codesnippetsmanager.cpp" line="38"/>
<location filename="codesnippetsmanager.cpp" line="48"/>
<source>Can&apos;t copy default code snippets &apos;%1&apos; to &apos;%2&apos;.</source>
<translation>&apos;%1&apos;&apos;%2&apos;</translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="47"/>
<location filename="codesnippetsmanager.cpp" line="58"/>
<location filename="codesnippetsmanager.cpp" line="59"/>
<location filename="codesnippetsmanager.cpp" line="70"/>
<source>Read code snippets failed</source>
<translation></translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="48"/>
<location filename="codesnippetsmanager.cpp" line="60"/>
<source>Can&apos;t open code snippet file &apos;%1&apos; for read.</source>
<translation>&apos;%1&apos;</translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="59"/>
<location filename="codesnippetsmanager.cpp" line="71"/>
<source>Read code snippet file &apos;%1&apos; failed:%2</source>
<translation>&apos;%1&apos;%2</translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="84"/>
<location filename="codesnippetsmanager.cpp" line="103"/>
<location filename="codesnippetsmanager.cpp" line="96"/>
<location filename="codesnippetsmanager.cpp" line="115"/>
<source>Save code snippets failed</source>
<translation></translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="85"/>
<location filename="codesnippetsmanager.cpp" line="97"/>
<source>Can&apos;t open code snippet file &apos;%1&apos; for write.</source>
<translation></translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="104"/>
<location filename="codesnippetsmanager.cpp" line="116"/>
<source>Write to code snippet file &apos;%1&apos; failed.</source>
<translation>&apos;%1&apos;</translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="132"/>
<source>Load new file template failed</source>
<translation></translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="133"/>
<source>Can&apos;t open new file template file &apos;%1&apos; for read.</source>
<translation>&apos;%1&apos;</translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="146"/>
<source>Save new file template failed</source>
<translation></translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="147"/>
<source>Can&apos;t open new file template file &apos;%1&apos; for write.</source>
<translation>&apos;%1&apos;</translation>
</message>
</context>
<context>
<name>CodeSnippetsModel</name>
<message>
<location filename="codesnippetsmanager.cpp" line="218"/>
<location filename="codesnippetsmanager.cpp" line="272"/>
<source>Caption</source>
<translation></translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="220"/>
<location filename="codesnippetsmanager.cpp" line="274"/>
<source>Completion Prefix</source>
<translation></translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="222"/>
<location filename="codesnippetsmanager.cpp" line="276"/>
<source>Description</source>
<translation></translation>
</message>
<message>
<location filename="codesnippetsmanager.cpp" line="224"/>
<location filename="codesnippetsmanager.cpp" line="278"/>
<source>Menu Section</source>
<translation></translation>
</message>
@ -931,76 +951,76 @@ Are you really want to continue?</source>
<translation type="vanished"></translation>
</message>
<message>
<location filename="editor.cpp" line="213"/>
<location filename="editor.cpp" line="227"/>
<location filename="editor.cpp" line="303"/>
<location filename="editor.cpp" line="1130"/>
<location filename="editor.cpp" line="1135"/>
<location filename="editor.cpp" line="1153"/>
<location filename="editor.cpp" line="1158"/>
<location filename="editor.cpp" line="216"/>
<location filename="editor.cpp" line="230"/>
<location filename="editor.cpp" line="306"/>
<location filename="editor.cpp" line="1133"/>
<location filename="editor.cpp" line="1138"/>
<location filename="editor.cpp" line="1156"/>
<location filename="editor.cpp" line="1161"/>
<source>Error</source>
<translation></translation>
</message>
<message>
<location filename="editor.cpp" line="214"/>
<location filename="editor.cpp" line="217"/>
<source>File %1 is not writable!</source>
<translation>&quot;%1&quot;</translation>
</message>
<message>
<location filename="editor.cpp" line="256"/>
<location filename="editor.cpp" line="259"/>
<source>Save As</source>
<translation></translation>
</message>
<message>
<location filename="editor.cpp" line="1131"/>
<location filename="editor.cpp" line="1134"/>
<source>The text to be copied exceeds count limit!</source>
<translation></translation>
</message>
<message>
<location filename="editor.cpp" line="1136"/>
<location filename="editor.cpp" line="1139"/>
<source>The text to be copied exceeds character limit!</source>
<translation></translation>
</message>
<message>
<location filename="editor.cpp" line="1154"/>
<location filename="editor.cpp" line="1157"/>
<source>The text to be cut exceeds count limit!</source>
<translation></translation>
</message>
<message>
<location filename="editor.cpp" line="1159"/>
<location filename="editor.cpp" line="1162"/>
<source>The text to be cut exceeds character limit!</source>
<translation></translation>
</message>
<message>
<location filename="editor.cpp" line="2127"/>
<location filename="editor.cpp" line="2130"/>
<source>Print Document</source>
<translation></translation>
</message>
<message>
<location filename="editor.cpp" line="2674"/>
<location filename="editor.cpp" line="2711"/>
<location filename="editor.cpp" line="2762"/>
<location filename="editor.cpp" line="2677"/>
<location filename="editor.cpp" line="2714"/>
<location filename="editor.cpp" line="2765"/>
<source>Ctrl+click for more info</source>
<translation>Ctrl+</translation>
</message>
<message>
<location filename="editor.cpp" line="3107"/>
<location filename="editor.cpp" line="3137"/>
<location filename="editor.cpp" line="3110"/>
<location filename="editor.cpp" line="3140"/>
<source>Symbol &apos;%1&apos; not found!</source>
<translation>&apos;%1&apos;!</translation>
</message>
<message>
<location filename="editor.cpp" line="3526"/>
<location filename="editor.cpp" line="3529"/>
<source>Break point condition</source>
<translation></translation>
</message>
<message>
<location filename="editor.cpp" line="3527"/>
<location filename="editor.cpp" line="3530"/>
<source>Enter the condition of the breakpoint:</source>
<translation>:</translation>
</message>
<message>
<location filename="editor.cpp" line="3704"/>
<location filename="editor.cpp" line="3707"/>
<source>Readonly</source>
<translation></translation>
</message>
@ -1506,15 +1526,25 @@ Are you really want to continue?</source>
<translation></translation>
</message>
<message>
<location filename="settingsdialog/editorsnippetwidget.ui" line="73"/>
<location filename="settingsdialog/editorsnippetwidget.ui" line="24"/>
<source>Code Snippets</source>
<translation></translation>
</message>
<message>
<location filename="settingsdialog/editorsnippetwidget.ui" line="83"/>
<source>Add</source>
<translation></translation>
</message>
<message>
<location filename="settingsdialog/editorsnippetwidget.ui" line="84"/>
<location filename="settingsdialog/editorsnippetwidget.ui" line="94"/>
<source>Remove</source>
<translation></translation>
</message>
<message>
<location filename="settingsdialog/editorsnippetwidget.ui" line="134"/>
<source>New File Template</source>
<translation></translation>
</message>
</context>
<context>
<name>EditorSymbolCompletionWidget</name>

View File

@ -14,6 +14,18 @@ CodeSnippetsManager::CodeSnippetsManager(QObject *parent) : QObject(parent)
}
void CodeSnippetsManager::load()
{
loadSnippets();
loadNewFileTemplate();
}
void CodeSnippetsManager::save()
{
saveSnippets();
saveNewFileTemplate();
}
void CodeSnippetsManager::loadSnippets()
{
//if config file not exists, copy it from data
QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_CODESNIPPET_FILE;
@ -75,7 +87,7 @@ void CodeSnippetsManager::load()
}
}
void CodeSnippetsManager::save()
void CodeSnippetsManager::saveSnippets()
{
QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_CODESNIPPET_FILE;
QFile file(filename);
@ -107,6 +119,38 @@ void CodeSnippetsManager::save()
}
}
void CodeSnippetsManager::loadNewFileTemplate()
{
QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_NEWFILETEMPLATES_FILE;
QFile file(filename);
if (!file.exists()) {
mNewFileTemplate = "";
return;
}
if (!file.open(QFile::ReadOnly)) {
QMessageBox::critical(nullptr,
tr("Load new file template failed"),
tr("Can't open new file template file '%1' for read.")
.arg(filename));
return;
}
mNewFileTemplate=QString::fromUtf8(file.readAll());
}
void CodeSnippetsManager::saveNewFileTemplate()
{
QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_NEWFILETEMPLATES_FILE;
QFile file(filename);
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
QMessageBox::critical(nullptr,
tr("Save new file template failed"),
tr("Can't open new file template file '%1' for write.")
.arg(filename));
return;
}
file.write(mNewFileTemplate.toUtf8());
}
const QList<PCodeSnippet> &CodeSnippetsManager::snippets() const
{
return mSnippets;
@ -117,6 +161,16 @@ void CodeSnippetsManager::setSnippets(const QList<PCodeSnippet> &newSnippets)
mSnippets = newSnippets;
}
const QString &CodeSnippetsManager::newFileTemplate() const
{
return mNewFileTemplate;
}
void CodeSnippetsManager::setNewFileTemplate(const QString &newNewFileTemplate)
{
mNewFileTemplate = newNewFileTemplate;
}
void CodeSnippetsModel::addSnippet(const QString &caption, const QString &prefix, const QString &code, const QString &description, int menuSection)
{
beginInsertRows(QModelIndex(),mSnippets.count(),mSnippets.count());

View File

@ -41,14 +41,25 @@ public:
void load();
void save();
const QList<PCodeSnippet> &snippets() const;
void setSnippets(const QList<PCodeSnippet> &newSnippets);
const QString &newFileTemplate() const;
void setNewFileTemplate(const QString &newNewFileTemplate);
signals:
private:
void loadSnippets();
void saveSnippets();
void loadNewFileTemplate();
void saveNewFileTemplate();
private:
QList<PCodeSnippet> mSnippets;
QString mNewFileTemplate;
};
using PCodeSnippetManager = std::shared_ptr<CodeSnippetsManager>;

View File

@ -1,6 +0,0 @@
#include "codetemplate.h"
CodeTemplate::CodeTemplate()
{
}

View File

@ -1,11 +0,0 @@
#ifndef CODETEMPLATE_H
#define CODETEMPLATE_H
class CodeTemplate
{
public:
CodeTemplate();
};
#endif // CODETEMPLATE_H

View File

@ -97,6 +97,9 @@ Editor::Editor(QWidget *parent, const QString& filename,
else
mFileEncoding = mEncodingOption;
highlighter=highlighterManager.getCppHighlighter();
if (parentPageControl!=nullptr) {
insertCodeSnippet(pMainWindow->codeSnippetManager()->newFileTemplate());
}
}
if (highlighter) {

View File

@ -129,7 +129,7 @@ SynEdit::SynEdit(QWidget *parent) : QAbstractScrollArea(parent)
synFontChanged();
showCaret();
hideCaret();
connect(horizontalScrollBar(),&QScrollBar::valueChanged,
this, &SynEdit::onScrolled);

View File

@ -38,6 +38,8 @@ EditorSnippetWidget::EditorSnippetWidget(const QString& name, const QString& gro
mUpdatingCode = false;
}
});
connect(ui->editFileTemplate,&Editor::changed,
this, &SettingsWidget::setSettingsChanged);
}
EditorSnippetWidget::~EditorSnippetWidget()
@ -48,11 +50,13 @@ EditorSnippetWidget::~EditorSnippetWidget()
void EditorSnippetWidget::doLoad()
{
mModel.updateSnippets(pMainWindow->codeSnippetManager()->snippets());
ui->editFileTemplate->lines()->setText(pMainWindow->codeSnippetManager()->newFileTemplate());
}
void EditorSnippetWidget::doSave()
{
pMainWindow->codeSnippetManager()->setSnippets(mModel.snippets());
pMainWindow->codeSnippetManager()->setNewFileTemplate(ui->editFileTemplate->text());
pMainWindow->codeSnippetManager()->save();
}

View File

@ -14,6 +14,16 @@
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tabSnippet">
<attribute name="title">
<string>Code Snippets</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
@ -119,6 +129,27 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tabFileTemplate">
<attribute name="title">
<string>New File Template</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="Editor" name="editFileTemplate">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>Editor</class>

View File

@ -43,6 +43,7 @@
#define DEV_LASTOPENS_FILE "lastopens.ini"
#define DEV_SYMBOLUSAGE_FILE "symbolusage.json"
#define DEV_CODESNIPPET_FILE "codesnippets.json"
#define DEV_NEWFILETEMPLATES_FILE "newfiletemplate.txt"
#define DEV_AUTOLINK_FILE "autolink.json"
#define DEV_SHORTCUT_FILE "shortcuts.json"
#define DEV_TOOLS_FILE "tools.json"