playaround with KActionMenu

This commit is contained in:
2024-06-30 12:51:22 +08:00
parent a75a4aeee8
commit d7c71f41b2
7 changed files with 270 additions and 8 deletions

View File

@ -1,9 +1,13 @@
#include "mainwindow.h"
#include "appsettings.h"
#include "editorviewhelper.h"
#include <QActionGroup>
#include <QApplication>
#include <QMenu>
#include <QStringBuilder>
#include <QStatusBar>
#include <KActionCollection>
#include <KStandardAction>
@ -14,24 +18,35 @@
#include <QSpinBox>
#include <Qsci/qsciscintilla.h>
#include <Qsci/qscilexer.h>
MainWindow::MainWindow(QWidget *parent)
: KXmlGuiWindow(parent)
, m_editor(new QsciScintilla(this))
, m_cursorPosStatusLabel(new QLabel(QString("Ln: ? Col: ?")))
{
setCentralWidget(m_editor);
statusBar()->addPermanentWidget(m_cursorPosStatusLabel);
QFont font(AppSettings::self()->editorFont());
m_editor->setFont(font);
m_editor->setMarginsFont(font);
m_editor->setMarginLineNumbers(1, true);
m_editor->setMarginWidth(1, 40);
m_editor->setFolding(QsciScintilla::BoxedTreeFoldStyle);
m_editor->setBraceMatching(QsciScintilla::SloppyBraceMatch);
m_editor->setTabWidth(AppSettings::self()->tabWidth());
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();
}
@ -82,6 +97,14 @@ void MainWindow::setupActions()
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
KStandardAction::zoomIn(this, [this](){
m_editor->zoomIn();
@ -147,3 +170,12 @@ void MainWindow::showSettings()
dialog->show();
}
void MainWindow::applyLexer(const QString &lexer)
{
QsciLexer * ret = LexerGroupActionMenu::createLexerByLanguage(lexer);
if (ret) {
ret->setFont(AppSettings::self()->editorFont());
}
m_editor->setLexer(ret);
}