From b75eccd4e8a42fb78093cd5cd1b68f45fe026c08 Mon Sep 17 00:00:00 2001 From: Diego Iastrubni Date: Thu, 18 Dec 2025 22:14:24 +0200 Subject: [PATCH] Meta: open a new document on first run (#25) 1) New API: the text editor plugin can open a new text editor with a pre-defined text. This can be used later on by the git plugin to display diffs for example. 2) When no editor has been defined, open a new markdown document with minimal documentation about this application. refs #25 --- src/GlobalCommands.hpp | 4 +++ src/main.cpp | 43 +++++++++++++++++++++-- src/plugins/texteditor/texteditor_plg.cpp | 38 ++++++++++++++++++++ src/plugins/texteditor/texteditor_plg.h | 4 +++ src/widgets/qmdieditor.cpp | 9 ++++- src/widgets/qmdieditor.h | 1 + 6 files changed, 96 insertions(+), 3 deletions(-) diff --git a/src/GlobalCommands.hpp b/src/GlobalCommands.hpp index f15daca..41321ca 100644 --- a/src/GlobalCommands.hpp +++ b/src/GlobalCommands.hpp @@ -36,6 +36,9 @@ inline constexpr const char *VariableInfo = "VariableInfo"; // Editor requests information about variable below mouse inline constexpr const char *KeywordTooltip = "KeywordTooltip"; +// Open a new editor, with the attached document +inline constexpr const char *DisplayText = "DisplayText"; + } // namespace GlobalCommands namespace GlobalArguments { @@ -56,4 +59,5 @@ inline constexpr const char *Raw = "Raw"; inline constexpr const char *Tags = "Tags"; inline constexpr const char *Tooltip = "Tooltip"; inline constexpr const char *Symbol = "Symbol"; +inline constexpr const char *Content = "Content"; } // namespace GlobalArguments diff --git a/src/main.cpp b/src/main.cpp index 31173d3..07d1a0a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,7 @@ #include #include +#include "GlobalCommands.hpp" #include "pluginmanager.h" #include "plugins/CTags/CTagsPlugin.hpp" #include "plugins/ProjectManager/ProjectManagerPlg.h" @@ -23,7 +24,41 @@ #include "plugins/imageviewer/imageviewer_plg.h" #include "plugins/texteditor/texteditor_plg.h" -#define USE_SPLIT +const QString WelcomContent = R"( +# Welcome to CodePointer + +CodePointer - an IDE for Rust, Go, C++ and more. The application +will look like a normal text editor, but can + +![CodePointer](https://raw.githubusercontent.com/diegoiast/qtedit4/337cd10aab4b123b15dee53c446fd1b7e343dc9a/qtedit4.png) + +Some hints for starter: + + * Normal keyboard shortcuts you are used + to should work (`control+f`, `control+o`, `control+s` and more). + * To select tabs, press `alt+1` etc. + * To select/hide/show hide sidebar, press `control+1` (this will open the file + manager). + * On the top left, you will find the application menu (shortcut is `alt+m`). + * You can access the command palette which has all the available commands + using `control+shift+p`. + * You can press `alt+control+m` to get a conservative menus+toolbars UI. + * You can split the editor horizontally by pressing + +## Project management + +You can also load projects, build and execute them: + * If you edit a `CMakeLists.txt` or `meson.build` or `cargo.toml` you will be + prompted to open this file as a project. A new sidebar will be opened with + the project files. + * You can also add an "existing project", by choosing a directory. + * You can choose commands to execute for building, or other tasks relevant + to this project (configure, build), and you can choose which target + to run (`control+b` and `control-r`). + * When building, errors are shown at the bottom. + * You can execute script files (python, Perl, Bash, PowerShell, etc.), by + pressing `control+shift+r` +)"; int main(int argc, char *argv[]) { Q_INIT_RESOURCE(qutepart_syntax_files); @@ -123,7 +158,11 @@ int main(int argc, char *argv[]) { pluginManager.updateGUI(); if (pluginManager.visibleTabs() == 0) { - textEditorPlugin->fileNew(); + CommandArgs args = { + {GlobalArguments::FileName, "welcome.md"}, + {GlobalArguments::Content, WelcomContent}, + }; + pluginManager.handleCommandAsync(GlobalCommands::DisplayText, args); pluginManager.saveSettings(); } diff --git a/src/plugins/texteditor/texteditor_plg.cpp b/src/plugins/texteditor/texteditor_plg.cpp index 5460501..8639863 100644 --- a/src/plugins/texteditor/texteditor_plg.cpp +++ b/src/plugins/texteditor/texteditor_plg.cpp @@ -1,3 +1,4 @@ + #include #include #include @@ -17,6 +18,7 @@ #include #include "AnsiToHTML.hpp" +#include "GlobalCommands.hpp" #include "texteditor_plg.h" #include "thememanager.h" #include "widgets/HistoryLineEdit.h" @@ -296,6 +298,42 @@ void TextEditorPlugin::saveConfig(QSettings &settings) { IPlugin::saveConfig(settings); } +int TextEditorPlugin::canHandleAsyncCommand(const QString &command, const CommandArgs &) const { + if (command == GlobalCommands::DisplayText) { + return true; + } + return false; +} + +QFuture TextEditorPlugin::handleCommandAsync(const QString &command, + const CommandArgs &args) { + if (command != GlobalCommands::DisplayText) { + return {}; + } + + auto fileName = args[GlobalArguments::FileName].toString(); + auto content = args[GlobalArguments::Content].toString(); + auto editor = new qmdiEditor(dynamic_cast(mdiServer), themeManager); + auto langInfo = ::Qutepart::chooseLanguage({}, {}, fileName); + if (langInfo.isValid()) { + editor->setEditorHighlighter(langInfo.id); + } + // auto shouldAutoPreview = editor->autoPreview; + // auto canOpenPreview = editor->hasPreview(); + editor->loadFile(fileName); + editor->setPreviewEnabled(true); + editor->setPreviewVisible(true); + editor->setHistoryModel(historyModel); + editor->setPlainText(content); + applySettings(editor); + mdiServer->addClient(editor); + + auto promise = new QPromise(); + auto future = promise->future(); + promise->finish(); + return promise->future(); +} + QStringList TextEditorPlugin::myExtensions() { auto s = QStringList(); s << tr("Sources C++", "EditorPlugin::myExtensions") + diff --git a/src/plugins/texteditor/texteditor_plg.h b/src/plugins/texteditor/texteditor_plg.h index ae28a8a..6a58417 100644 --- a/src/plugins/texteditor/texteditor_plg.h +++ b/src/plugins/texteditor/texteditor_plg.h @@ -57,6 +57,10 @@ class TextEditorPlugin : public IPlugin { virtual void showAbout() override; virtual void loadConfig(QSettings &settings) override; virtual void saveConfig(QSettings &settings) override; + virtual int canHandleAsyncCommand(const QString &command, + const CommandArgs &args) const override; + virtual QFuture handleCommandAsync(const QString &command, + const CommandArgs &args) override; QStringList myExtensions() override; int canOpenFile(const QString &fileName) override; diff --git a/src/widgets/qmdieditor.cpp b/src/widgets/qmdieditor.cpp index 155b96a..c94f6ee 100644 --- a/src/widgets/qmdieditor.cpp +++ b/src/widgets/qmdieditor.cpp @@ -888,7 +888,9 @@ void qmdiEditor::updateClientName() { if (mdiClientName != newName) { mdiClientName = newName; - mdiServer->updateClientName(this); + if (mdiServer) { + mdiServer->updateClientName(this); + } } } @@ -1072,6 +1074,11 @@ void qmdiEditor::hideBannerMessage() { void qmdiEditor::newDocument() { loadFile(""); } +void qmdiEditor::setPlainText(const QString &plainText) { + textEditor->setPlainText(plainText); + textEditor->document()->setModified(true); +} + bool qmdiEditor::doSave() { if (fileName.isEmpty()) { return doSaveAs(); diff --git a/src/widgets/qmdieditor.h b/src/widgets/qmdieditor.h index e82a4a9..aa05c8d 100644 --- a/src/widgets/qmdieditor.h +++ b/src/widgets/qmdieditor.h @@ -96,6 +96,7 @@ class qmdiEditor : public QWidget, public qmdiClient { void hideBannerMessage(); void newDocument(); + void setPlainText(const QString &plainText); bool doSave(); bool doSaveAs(); bool loadFile(const QString &fileName);