playaround with KActionMenu

This commit is contained in:
Gary Wang 2024-06-30 12:51:22 +08:00
parent a75a4aeee8
commit d7c71f41b2
No known key found for this signature in database
GPG Key ID: 5D30A4F15EA78760
7 changed files with 270 additions and 8 deletions

View File

@ -27,6 +27,7 @@ target_sources(pineapple-notepad
PRIVATE PRIVATE
main.cpp main.cpp
mainwindow.cpp mainwindow.h mainwindow.cpp mainwindow.h
editorviewhelper.cpp editorviewhelper.h
generalsettings.ui generalsettings.ui
appsettings.kcfg appsettings.kcfg

165
editorviewhelper.cpp Normal file
View File

@ -0,0 +1,165 @@
#include "editorviewhelper.h"
#include <QActionGroup>
#include <QApplication>
#include <QMenu>
#include <KActionMenu>
#include <Qsci/qsciscintilla.h>
#include <Qsci/qscilexer.h>
#include <Qsci/qscilexerbash.h>
#include <Qsci/qscilexerbatch.h>
#include <Qsci/qscilexercpp.h>
#include <Qsci/qscilexercmake.h>
#include <Qsci/qscilexercsharp.h>
#include <Qsci/qscilexercss.h>
#include <Qsci/qscilexerd.h>
#include <Qsci/qscilexerdiff.h>
#include <Qsci/qscilexerhtml.h>
#include <Qsci/qscilexerjava.h>
#include <Qsci/qscilexerjavascript.h>
#include <Qsci/qscilexerjson.h>
#include <Qsci/qscilexerlua.h>
#include <Qsci/qscilexermakefile.h>
#include <Qsci/qscilexermarkdown.h>
#include <Qsci/qscilexerpascal.h>
#include <Qsci/qscilexerperl.h>
#include <Qsci/qscilexerpo.h>
#include <Qsci/qscilexerpython.h>
#include <Qsci/qscilexerruby.h>
#include <Qsci/qscilexerxml.h>
#include <Qsci/qscilexeryaml.h>
const QStringList languages_group[] = {
QStringList{
"Bash", "Batch"
},
QStringList{
"C", "C++", "C#", "CMake", "CSS"
},
QStringList{
"D", "Diff"
},
QStringList{
"HTML"
},
QStringList{
"Java", "JavaScript", "JSON"
},
QStringList{
"Lua"
},
QStringList{
"Makefile", "Markdown"
},
QStringList{
"Pascal", "Perl", "PO", "Python"
},
QStringList{
"Ruby"
},
QStringList{
"XML"
},
QStringList{
"YAML"
},
};
QString LexerGroupActionMenu::groupName(LanguageGroup grp)
{
switch (grp) {
case LANG_GRP_B:
return QLatin1String("B");
case LANG_GRP_C:
return QLatin1String("C");
case LANG_GRP_D:
return QLatin1String("D");
case LANG_GRP_H:
return QLatin1String("H");
case LANG_GRP_J:
return QLatin1String("J");
case LANG_GRP_L:
return QLatin1String("L");
case LANG_GRP_M:
return QLatin1String("M");
case LANG_GRP_P:
return QLatin1String("P");
case LANG_GRP_R:
return QLatin1String("R");
case LANG_GRP_X:
return QLatin1String("X");
case LANG_GRP_Y:
return QLatin1String("Y");
}
Q_UNREACHABLE_RETURN("");
}
QsciLexer * LexerGroupActionMenu::createLexerByLanguage(const QString &languageName)
{
QsciLexer * ret = nullptr;
if (languageName == "Bash") {
ret = new QsciLexerBash();
} else if (languageName == "Batch") {
ret = new QsciLexerBatch();
} else if (languageName == "C" || languageName == "C++") {
ret = new QsciLexerCPP();
} else if (languageName == "C#") {
ret = new QsciLexerCSharp();
} else if (languageName == "CMake") {
ret = new QsciLexerCMake();
} else if (languageName == "CSS") {
ret = new QsciLexerCSS();
} else if (languageName == "D") {
ret = new QsciLexerD();
} else if (languageName == "Diff") {
ret = new QsciLexerDiff();
} else if (languageName == "HTML") {
ret = new QsciLexerHTML();
} else if (languageName == "Java") {
ret = new QsciLexerJava();
} else if (languageName == "JavaScript") {
ret = new QsciLexerJavaScript();
} else if (languageName == "JSON") {
ret = new QsciLexerJSON();
} else if (languageName == "Lua") {
ret = new QsciLexerLua();
} else if (languageName == "Makefile") {
ret = new QsciLexerMakefile();
} else if (languageName == "Markdown") {
ret = new QsciLexerMarkdown();
} else if (languageName == "Pascal") {
ret = new QsciLexerPascal();
} else if (languageName == "Perl") {
ret = new QsciLexerPerl();
} else if (languageName == "PO") {
ret = new QsciLexerPO();
} else if (languageName == "Python") {
ret = new QsciLexerPython();
} else if (languageName == "Ruby") {
ret = new QsciLexerRuby();
} else if (languageName == "XML") {
ret = new QsciLexerXML();
} else if (languageName == "YAML") {
ret = new QsciLexerYAML();
}
return ret;
}
void LexerGroupActionMenu::slotAboutToShow()
{
if (!m_group) {
m_group = new QActionGroup(menu());
} else {
return;
}
for (const QString & name : languages_group[m_languageGroup]) {
QAction *a = menu()->addAction(name, this, [this, name](){
emit lexerSelected(name);
});
a->setActionGroup(m_group);
}
}

46
editorviewhelper.h Normal file
View File

@ -0,0 +1,46 @@
#pragma once
#include <KActionMenu>
#include <QMenu>
class QsciLexer;
class LexerGroupActionMenu : public KActionMenu
{
Q_OBJECT
public:
enum LanguageGroup {
LANG_GRP_B,
LANG_GRP_C,
LANG_GRP_D,
LANG_GRP_H,
LANG_GRP_J,
LANG_GRP_L,
LANG_GRP_M,
LANG_GRP_P,
LANG_GRP_R,
LANG_GRP_X,
LANG_GRP_Y,
LANG_GRP_LAST = LANG_GRP_Y
};
LexerGroupActionMenu(const QString &text, enum LanguageGroup grp, QObject *parent)
: KActionMenu(text, parent)
{
m_group = nullptr;
m_languageGroup = grp;
connect(menu(), &QMenu::aboutToShow, this, &LexerGroupActionMenu::slotAboutToShow);
setPopupMode(QToolButton::InstantPopup);
}
static QString groupName(enum LanguageGroup grp);
static QsciLexer * createLexerByLanguage(const QString & languageName);
signals:
void lexerSelected(const QString lexerName);
private:
QActionGroup *m_group;
enum LanguageGroup m_languageGroup;
public:
void slotAboutToShow();
};

View File

@ -6,6 +6,7 @@
#include <KLocalizedString> #include <KLocalizedString>
#include <KIconTheme> #include <KIconTheme>
#include <KStyleManager> #include <KStyleManager>
#include <QSci/qsciglobal.h>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -25,6 +26,8 @@ int main(int argc, char *argv[])
QString(), QString(),
QString(), QString(),
QStringLiteral("https://blumia.net")); QStringLiteral("https://blumia.net"));
aboutData.addComponent("QsciScintilla", "A port to Qt6 of Neil Hodgson's Scintilla C++ editor class",
QSCINTILLA_VERSION_STR, "https://www.riverbankcomputing.com/software/qscintilla/intro");
KAboutData::setApplicationData(aboutData); KAboutData::setApplicationData(aboutData);
a.setWindowIcon(QIcon::fromTheme(QStringLiteral("accessories-text-editor"))); a.setWindowIcon(QIcon::fromTheme(QStringLiteral("accessories-text-editor")));

View File

@ -1,9 +1,13 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "appsettings.h" #include "appsettings.h"
#include "editorviewhelper.h"
#include <QActionGroup>
#include <QApplication> #include <QApplication>
#include <QMenu> #include <QMenu>
#include <QStringBuilder>
#include <QStatusBar>
#include <KActionCollection> #include <KActionCollection>
#include <KStandardAction> #include <KStandardAction>
@ -14,24 +18,35 @@
#include <QSpinBox> #include <QSpinBox>
#include <Qsci/qsciscintilla.h> #include <Qsci/qsciscintilla.h>
#include <Qsci/qscilexer.h>
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: KXmlGuiWindow(parent) : KXmlGuiWindow(parent)
, m_editor(new QsciScintilla(this)) , m_editor(new QsciScintilla(this))
, m_cursorPosStatusLabel(new QLabel(QString("Ln: ? Col: ?")))
{ {
setCentralWidget(m_editor); setCentralWidget(m_editor);
statusBar()->addPermanentWidget(m_cursorPosStatusLabel);
QFont font(AppSettings::self()->editorFont()); QFont font(AppSettings::self()->editorFont());
m_editor->setFont(font); m_editor->setFont(font);
m_editor->setMarginsFont(font); m_editor->setMarginsFont(font);
m_editor->setMarginLineNumbers(1, true); m_editor->setMarginLineNumbers(1, true);
m_editor->setMarginWidth(1, 40); m_editor->setMarginWidth(1, 40);
m_editor->setFolding(QsciScintilla::BoxedTreeFoldStyle);
m_editor->setBraceMatching(QsciScintilla::SloppyBraceMatch); m_editor->setBraceMatching(QsciScintilla::SloppyBraceMatch);
m_editor->setTabWidth(AppSettings::self()->tabWidth()); m_editor->setTabWidth(AppSettings::self()->tabWidth());
m_editor->setEolMode(QsciScintilla::EolUnix); m_editor->setEolMode(QsciScintilla::EolUnix);
connect(m_editor, &QsciScintilla::cursorPositionChanged, this, [this](int line, int index){
// FIXME: not get called at all?
qDebug() << line << index;
m_cursorPosStatusLabel->setText(QString("Ln: %1 Col: %2").arg(line, index));
});
setupActions(); setupActions();
} }
@ -82,6 +97,14 @@ void MainWindow::setupActions()
m_editor->setLexer(nullptr); m_editor->setLexer(nullptr);
}); });
for (int i = 0; i <= LexerGroupActionMenu::LANG_GRP_LAST; i++) {
LexerGroupActionMenu::LanguageGroup group = static_cast<LexerGroupActionMenu::LanguageGroup>(i);
const QString groupName(LexerGroupActionMenu::groupName(group));
LexerGroupActionMenu *lexerGroupMenu = new LexerGroupActionMenu(groupName, group, this);
actionCollection()->addAction(QStringLiteral("lexer_group_") % groupName.toLower(), lexerGroupMenu);
connect(lexerGroupMenu, &LexerGroupActionMenu::lexerSelected, this, &MainWindow::applyLexer);
}
// Toolbar actions // Toolbar actions
KStandardAction::zoomIn(this, [this](){ KStandardAction::zoomIn(this, [this](){
m_editor->zoomIn(); m_editor->zoomIn();
@ -147,3 +170,12 @@ void MainWindow::showSettings()
dialog->show(); dialog->show();
} }
void MainWindow::applyLexer(const QString &lexer)
{
QsciLexer * ret = LexerGroupActionMenu::createLexerByLanguage(lexer);
if (ret) {
ret->setFont(AppSettings::self()->editorFont());
}
m_editor->setLexer(ret);
}

View File

@ -1,7 +1,10 @@
#pragma once
#include <KXmlGuiWindow> #include <KXmlGuiWindow>
#include "ui_GeneralSettings.h" #include "ui_GeneralSettings.h"
class QLabel;
class QsciScintilla; class QsciScintilla;
class MainWindow : public KXmlGuiWindow class MainWindow : public KXmlGuiWindow
{ {
@ -14,8 +17,10 @@ public:
private: private:
void setupActions(); void setupActions();
void showSettings(); void showSettings();
void applyLexer(const QString & lexer);
QsciScintilla * m_editor; QsciScintilla * m_editor;
QLabel * m_cursorPosStatusLabel;
}; };
// plainly for KConfigDialog // plainly for KConfigDialog

View File

@ -20,7 +20,7 @@ Template: https://github.com/KDE/kxmlgui/blob/master/src/ui_standards.rc
<Action name="edit_find_prev" /> <Action name="edit_find_prev" />
<Action name="edit_replace" /> <Action name="edit_replace" />
</Menu> </Menu>
<Menu name="view"><text>&amp;View</text> <Menu name="view">
<!-- <Menu><text>&amp;Zoom</text> <!-- <Menu><text>&amp;Zoom</text>
<Action name="view_zoom_in"/> <Action name="view_zoom_in"/>
<Action name="view_zoom_out"/> <Action name="view_zoom_out"/>
@ -31,16 +31,26 @@ Template: https://github.com/KDE/kxmlgui/blob/master/src/ui_standards.rc
</Menu> </Menu>
<Menu name="language"><text>&amp;Language</text> <Menu name="language"><text>&amp;Language</text>
<Action name="lexer_none" /> <Action name="lexer_none" />
<Separator/>
<!-- FIXME: those lexer_group_ actions should be exclude from Edit ToolBar dialog -->
<Action name="lexer_group_b" />
<Action name="lexer_group_c" />
<Action name="lexer_group_d" />
<Action name="lexer_group_h" />
<Action name="lexer_group_j" />
<Action name="lexer_group_l" />
<Action name="lexer_group_m" />
<Action name="lexer_group_p" />
<Action name="lexer_group_r" />
<Action name="lexer_group_x" />
<Action name="lexer_group_y" />
</Menu> </Menu>
<Menu name="settings"><text>&amp;Settings</text> <Menu name="settings">
<Action name="colorscheme_menu"/> <Action name="colorscheme_menu"/>
</Menu> </Menu>
<!-- this is not enough, user can still see actions like About KDE from other places <!-- FIXME: This is NOT a KDE project -->
<menu name="help" noMerge="1"><text>&amp;Help</text> <menu name="help">
<Action name="open_kcommand_bar"/> </menu>
<Separator/>
<Action name="help_about_app"/>
</menu> -->
</MenuBar> </MenuBar>
<ToolBar name="mainToolBar" > <ToolBar name="mainToolBar" >
<text>Main Toolbar</text> <text>Main Toolbar</text>