Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/NotepadNext/ActionUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This file is part of Notepad Next.
* Copyright 2025 Justin Dailey
*
* Notepad Next is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Notepad Next is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Notepad Next. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include <QObject>
#include <QStringList>
#include <QAction>
#include <QDebug>

namespace ActionUtils {

template <typename Container>
void populateActionContainer(Container* container, QObject* context, const QStringList& actionNames) {
for (const QString& actionName : actionNames) {
if (actionName.isEmpty()) {
container->addSeparator();
} else {
const QString fullObjectName = QStringLiteral("action") + actionName;
QAction* action = context->findChild<QAction*>(fullObjectName, Qt::FindDirectChildrenOnly);

if (action) {
container->addAction(action);
} else {
qWarning() << "Cannot locate action named" << fullObjectName;
}
}
}
}

}
1 change: 1 addition & 0 deletions src/NotepadNext/NotepadNext.pro
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ SOURCES += \
widgets/StatusLabel.cpp

HEADERS += \
ActionUtils.h \
ApplicationSettings.h \
ColorPickerDelegate.h \
ComboBoxDelegate.h \
Expand Down
38 changes: 17 additions & 21 deletions src/NotepadNext/dialogs/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@

#include "FadingIndicator.h"

#include "ActionUtils.h"


MainWindow::MainWindow(NotepadNextApplication *app) :
ui(new Ui::MainWindow),
Expand Down Expand Up @@ -300,8 +302,8 @@ MainWindow::MainWindow(NotepadNextApplication *app) :
copyAsFormat(&rtf, "Rich Text Format");
});

connect(ui->actionIncrease_Indent, &QAction::triggered, this, [=]() { currentEditor()->tab(); });
connect(ui->actionDecrease_Indent, &QAction::triggered, this, [=]() { currentEditor()->backTab(); });
connect(ui->actionIncreaseIndent, &QAction::triggered, this, [=]() { currentEditor()->tab(); });
connect(ui->actionDecreaseIndent, &QAction::triggered, this, [=]() { currentEditor()->backTab(); });

addAction(ui->actionToggleOverType);
connect(ui->actionToggleOverType, &QAction::triggered, this, [=]() {
Expand Down Expand Up @@ -1821,6 +1823,15 @@ void MainWindow::restoreSettings()
ApplicationSettings *settings = app->getSettings();

zoomLevel = settings->value("Editor/ZoomLevel", 0).toInt();

if (settings->contains("Gui/ToolBar")) {
QStringList actionNames;
actionNames = settings->value("Gui/ToolBar").toStringList();

ui->mainToolBar->clear();

ActionUtils::populateActionContainer(ui->mainToolBar, this, actionNames);
}
}

ISearchResultsHandler *MainWindow::determineSearchResultsHandler()
Expand Down Expand Up @@ -1915,7 +1926,7 @@ void MainWindow::addEditor(ScintillaNext *editor)
actionNames.prepend("CopyURL");
}

buildDynamicMenu(actionNames)->popup(QCursor::pos());
buildMenu(actionNames)->popup(QCursor::pos());
});

// The editor has been entirely configured at this point, so add it to the docked editor
Expand Down Expand Up @@ -2083,27 +2094,12 @@ void MainWindow::dropEvent(QDropEvent *event)
}
}

QMenu *MainWindow::buildDynamicMenu(QStringList actionNames)
QMenu *MainWindow::buildMenu(QStringList actionNames)
{
QMenu *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);

// Populate the menu
for (const QString &actionName : actionNames) {
if (actionName.isEmpty()) {
menu->addSeparator();
}
else {
QAction *a = findChild<QAction *>(QStringLiteral("action") + actionName, Qt::FindDirectChildrenOnly);

if (a != Q_NULLPTR) {
menu->addAction(a);
}
else {
qWarning() << "Cannot locate menu named" << actionName;
}
}
}
ActionUtils::populateActionContainer(menu, this, actionNames);

return menu;
}
Expand Down Expand Up @@ -2144,7 +2140,7 @@ void MainWindow::tabBarRightClicked(ScintillaNext *editor)
actionNames = settings->value("Gui/TabBarContextMenu").toStringList();
}

buildDynamicMenu(actionNames)->popup(QCursor::pos());
buildMenu(actionNames)->popup(QCursor::pos());
}

void MainWindow::languageMenuTriggered()
Expand Down
2 changes: 1 addition & 1 deletion src/NotepadNext/dialogs/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private slots:
ZoomEventWatcher *zoomEventWatcher;
int zoomLevel = 0;
int contextMenuPos = 0;
QMenu *buildDynamicMenu(QStringList actionNames);
QMenu *buildMenu(QStringList actionNames);
};

#endif // MAINWINDOW_H
6 changes: 2 additions & 4 deletions src/NotepadNext/dialogs/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@
<property name="title">
<string>Indent</string>
</property>
<addaction name="actionIncrease_Indent"/>
<addaction name="actionDecrease_Indent"/>
</widget>
<widget class="QMenu" name="menuEOLConversion">
<property name="title">
Expand Down Expand Up @@ -575,7 +573,7 @@
<string>Ctrl+A</string>
</property>
</action>
<action name="actionIncrease_Indent">
<action name="actionIncreaseIndent">
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/icons/text_indent.png</normaloff>:/icons/text_indent.png</iconset>
Expand All @@ -584,7 +582,7 @@
<string>Increase Indent</string>
</property>
</action>
<action name="actionDecrease_Indent">
<action name="actionDecreaseIndent">
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/icons/text_indent_remove.png</normaloff>:/icons/text_indent_remove.png</iconset>
Expand Down