pineapple-notepad/mainwindow.cpp

176 lines
6.6 KiB
C++
Raw Normal View History

2024-06-22 20:01:42 +08:00
#include "mainwindow.h"
2024-07-02 23:47:26 +08:00
#include "sciedit.h"
2024-06-22 20:01:42 +08:00
#include "appsettings.h"
2024-06-30 12:51:22 +08:00
#include "editorviewhelper.h"
2024-06-22 20:01:42 +08:00
2024-06-30 12:51:22 +08:00
#include <QActionGroup>
2024-06-22 20:01:42 +08:00
#include <QApplication>
#include <QMenu>
2024-06-30 12:51:22 +08:00
#include <QStringBuilder>
#include <QStatusBar>
2024-06-22 20:01:42 +08:00
#include <KActionCollection>
#include <KStandardAction>
#include <KActionMenu>
#include <KConfigDialog>
#include <KColorSchemeManager>
#include <KColorSchemeMenu>
#include <QSpinBox>
2024-07-02 23:47:26 +08:00
#include <ILexer.h>
#include <Lexilla.h>
2024-06-22 20:01:42 +08:00
MainWindow::MainWindow(QWidget *parent)
: KXmlGuiWindow(parent)
2024-07-02 23:47:26 +08:00
, m_editor(new SciEdit(this))
2024-06-30 12:51:22 +08:00
, m_cursorPosStatusLabel(new QLabel(QString("Ln: ? Col: ?")))
2024-06-22 20:01:42 +08:00
{
setCentralWidget(m_editor);
2024-06-30 12:51:22 +08:00
statusBar()->addPermanentWidget(m_cursorPosStatusLabel);
2024-06-22 20:01:42 +08:00
QFont font(AppSettings::self()->editorFont());
2024-07-02 23:47:26 +08:00
m_editor->setStyleFont(font);
m_editor->setStyleFont(font, STYLE_LINENUMBER);
2024-06-22 20:01:42 +08:00
2024-07-02 23:47:26 +08:00
m_editor->setMarginTypeN(1, SC_MARGIN_NUMBER);
m_editor->setMarginWidthN(1, m_editor->textWidth(STYLE_LINENUMBER, "_99999"));
m_editor->setFolding(SciEdit::BoxFoldType);
// m_editor->setBraceMatching(QsciScintilla::SloppyBraceMatch);
2024-06-22 20:01:42 +08:00
m_editor->setTabWidth(AppSettings::self()->tabWidth());
2024-07-02 23:47:26 +08:00
m_editor->setEOLMode(SC_EOL_LF);
2024-06-22 20:01:42 +08:00
2024-07-02 23:47:26 +08:00
connect(m_editor, &SciEdit::cursorPosChanged, this, [this](int line, int index){
m_cursorPosStatusLabel->setText(QString("Ln: %1 Col: %2").arg(line).arg(index));
2024-06-30 12:51:22 +08:00
});
2024-06-22 20:01:42 +08:00
setupActions();
}
MainWindow::~MainWindow()
{
}
void MainWindow::setupActions()
{
using namespace Qt::Literals::StringLiterals;
// "File" menu
KStandardAction::openNew(this, [](){}, actionCollection());
KStandardAction::open(this, [](){}, actionCollection());
KStandardAction::save(this, [](){}, actionCollection());
KStandardAction::close(this, [](){}, actionCollection());
KStandardAction::quit(qApp, &QApplication::quit, actionCollection());
// "Edit" menu
KStandardAction::undo(this, [this](){
m_editor->undo();
}, actionCollection());
KStandardAction::redo(this, [this](){
m_editor->redo();
}, actionCollection());
KStandardAction::cut(this, [this](){
m_editor->cut();
}, actionCollection());
KStandardAction::copy(this, [this](){
m_editor->copy();
}, actionCollection());
KStandardAction::paste(this, [this](){
m_editor->paste();
}, actionCollection());
// "Search" menu
KStandardAction::find(this, [](){}, actionCollection());
KStandardAction::findNext(this, [](){}, actionCollection());
KStandardAction::findPrev(this, [](){}, actionCollection());
KStandardAction::replace(this, [](){}, actionCollection());
qDebug() << KStandardAction::name(KStandardAction::Replace);
// "Language" menu
QAction *lexerNoneAction = new QAction(this);
lexerNoneAction->setText("None (Normal Text)");
actionCollection()->addAction(u"lexer_none"_s, lexerNoneAction);
connect(lexerNoneAction, &QAction::triggered, this, [this](){
2024-07-02 23:47:26 +08:00
// m_editor->setLexer(nullptr);
2024-06-22 20:01:42 +08:00
});
2024-07-02 23:47:26 +08:00
for (const QChar & group : LexerGroupActionMenu::groups()) {
LexerGroupActionMenu *lexerGroupMenu = new LexerGroupActionMenu(group.toUpper(), group, this);
actionCollection()->addAction(QStringLiteral("lexer_group_") % group, lexerGroupMenu);
2024-06-30 12:51:22 +08:00
connect(lexerGroupMenu, &LexerGroupActionMenu::lexerSelected, this, &MainWindow::applyLexer);
}
2024-06-22 20:01:42 +08:00
// Toolbar actions
KStandardAction::zoomIn(this, [this](){
m_editor->zoomIn();
}, actionCollection());
KStandardAction::zoomOut(this, [this](){
m_editor->zoomOut();
}, actionCollection());
KStandardAction::preferences(this, &MainWindow::showSettings, actionCollection());
QAction *toggleWrapModeAction = new QAction(this);
toggleWrapModeAction->setText("Toggle Wrap Mode");
toggleWrapModeAction->setIcon(QIcon::fromTheme(u"text-wrap"_s));
toggleWrapModeAction->setCheckable(true);
actionCollection()->addAction(u"toggle_wrap_mode"_s, toggleWrapModeAction);
connect(toggleWrapModeAction, &QAction::triggered, this, [this, toggleWrapModeAction](){
2024-07-02 23:47:26 +08:00
bool switchToWrapNone = m_editor->wrapMode() == SC_WRAP_WORD;
m_editor->setWrapMode(switchToWrapNone ? SC_WRAP_NONE : SC_WRAP_WORD);
2024-06-22 20:01:42 +08:00
toggleWrapModeAction->setChecked(!switchToWrapNone);
});
QAction *toggleWhitespaceVisibilityAction = new QAction(this);
toggleWhitespaceVisibilityAction->setText("Show All Characters");
toggleWhitespaceVisibilityAction->setCheckable(true);
// toggleWhitespaceVisibilityAction->setIcon(QIcon::fromTheme(u"text-wrap"_s));
actionCollection()->addAction(u"toggle_show_all_characters"_s, toggleWhitespaceVisibilityAction);
connect(toggleWhitespaceVisibilityAction, &QAction::triggered, this, [this, toggleWhitespaceVisibilityAction](){
2024-07-02 23:47:26 +08:00
bool switchToVisible = m_editor->viewWS() == SCWS_INVISIBLE;
m_editor->setViewWS(switchToVisible ? SCWS_VISIBLEALWAYS : SCWS_INVISIBLE);
m_editor->setViewEOL(switchToVisible);
2024-06-22 20:01:42 +08:00
toggleWhitespaceVisibilityAction->setChecked(switchToVisible);
});
QAction *toggleIndentGuideAction = new QAction(this);
toggleIndentGuideAction->setText("Toggle Indent Guide");
toggleIndentGuideAction->setIcon(QIcon::fromTheme(u"show-guides"_s));
actionCollection()->addAction(u"toggle_indent_guide"_s, toggleIndentGuideAction);
connect(toggleIndentGuideAction, &QAction::triggered, this, [this](){
2024-07-02 23:47:26 +08:00
// m_editor->setIndentationGuides(!m_editor->indentationGuides());
2024-06-22 20:01:42 +08:00
});
// Load themes
KColorSchemeManager *manager = new KColorSchemeManager(this);
auto *colorSelectionMenu = KColorSchemeMenu::createMenu(manager, this);
colorSelectionMenu->menu()->setTitle("&Window Color Scheme");
actionCollection()->addAction(QStringLiteral("colorscheme_menu"), colorSelectionMenu);
setupGUI();
}
void MainWindow::showSettings()
{
if (KConfigDialog::showDialog(QStringLiteral("settings"))) {
return;
}
KConfigDialog *dialog = new KConfigDialog(this, QStringLiteral("settings"), AppSettings::self());
dialog->setFaceType(KPageDialog::FlatList);
dialog->addPage(new SettingsPage<Ui::GeneralSettings>(dialog), "Appearance", "preferences-desktop-theme-global");
connect(dialog, &KConfigDialog::settingsChanged, this, [](const QString &dialogName){
qDebug() << dialogName << "changed";
});
dialog->show();
}
2024-06-30 12:51:22 +08:00
2024-07-02 23:47:26 +08:00
void MainWindow::applyLexer(const QString &lexerName)
2024-06-30 12:51:22 +08:00
{
2024-07-02 23:47:26 +08:00
Scintilla::ILexer5 * lexer = CreateLexer(lexerName.toStdString().c_str());
m_editor->setLexer(lexer);
2024-06-30 12:51:22 +08:00
}