Skip to content

Commit a1f9020

Browse files
committed
Allow customized toolbar
See #751
1 parent 825f065 commit a1f9020

File tree

4 files changed

+63
-20
lines changed

4 files changed

+63
-20
lines changed

src/NotepadNext/ActionUtils.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of Notepad Next.
3+
* Copyright 2025 Justin Dailey
4+
*
5+
* Notepad Next is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* Notepad Next is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with Notepad Next. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#pragma once
20+
21+
#include <QObject>
22+
#include <QStringList>
23+
#include <QAction>
24+
#include <QDebug>
25+
26+
namespace ActionUtils {
27+
28+
template <typename Container>
29+
void populateActionContainer(Container* container, QObject* context, const QStringList& actionNames) {
30+
for (const QString& actionName : actionNames) {
31+
if (actionName.isEmpty()) {
32+
container->addSeparator();
33+
} else {
34+
const QString fullObjectName = QStringLiteral("action") + actionName;
35+
QAction* action = context->findChild<QAction*>(fullObjectName, Qt::FindDirectChildrenOnly);
36+
37+
if (action) {
38+
container->addAction(action);
39+
} else {
40+
qWarning() << "Cannot locate action named" << fullObjectName;
41+
}
42+
}
43+
}
44+
}
45+
46+
}

src/NotepadNext/NotepadNext.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ SOURCES += \
138138
widgets/StatusLabel.cpp
139139

140140
HEADERS += \
141+
ActionUtils.h \
141142
ApplicationSettings.h \
142143
ColorPickerDelegate.h \
143144
ComboBoxDelegate.h \

src/NotepadNext/dialogs/MainWindow.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585

8686
#include "FadingIndicator.h"
8787

88+
#include "ActionUtils.h"
89+
8890

8991
MainWindow::MainWindow(NotepadNextApplication *app) :
9092
ui(new Ui::MainWindow),
@@ -1821,6 +1823,15 @@ void MainWindow::restoreSettings()
18211823
ApplicationSettings *settings = app->getSettings();
18221824

18231825
zoomLevel = settings->value("Editor/ZoomLevel", 0).toInt();
1826+
1827+
if (settings->contains("Gui/ToolBar")) {
1828+
QStringList actionNames;
1829+
actionNames = settings->value("Gui/ToolBar").toStringList();
1830+
1831+
ui->mainToolBar->clear();
1832+
1833+
ActionUtils::populateActionContainer(ui->mainToolBar, this, actionNames);
1834+
}
18241835
}
18251836

18261837
ISearchResultsHandler *MainWindow::determineSearchResultsHandler()
@@ -1915,7 +1926,7 @@ void MainWindow::addEditor(ScintillaNext *editor)
19151926
actionNames.prepend("CopyURL");
19161927
}
19171928

1918-
buildDynamicMenu(actionNames)->popup(QCursor::pos());
1929+
buildMenu(actionNames)->popup(QCursor::pos());
19191930
});
19201931

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

2086-
QMenu *MainWindow::buildDynamicMenu(QStringList actionNames)
2097+
QMenu *MainWindow::buildMenu(QStringList actionNames)
20872098
{
20882099
QMenu *menu = new QMenu(this);
20892100
menu->setAttribute(Qt::WA_DeleteOnClose);
20902101

2091-
// Populate the menu
2092-
for (const QString &actionName : actionNames) {
2093-
if (actionName.isEmpty()) {
2094-
menu->addSeparator();
2095-
}
2096-
else {
2097-
QAction *a = findChild<QAction *>(QStringLiteral("action") + actionName, Qt::FindDirectChildrenOnly);
2098-
2099-
if (a != Q_NULLPTR) {
2100-
menu->addAction(a);
2101-
}
2102-
else {
2103-
qWarning() << "Cannot locate menu named" << actionName;
2104-
}
2105-
}
2106-
}
2102+
ActionUtils::populateActionContainer(menu, this, actionNames);
21072103

21082104
return menu;
21092105
}
@@ -2144,7 +2140,7 @@ void MainWindow::tabBarRightClicked(ScintillaNext *editor)
21442140
actionNames = settings->value("Gui/TabBarContextMenu").toStringList();
21452141
}
21462142

2147-
buildDynamicMenu(actionNames)->popup(QCursor::pos());
2143+
buildMenu(actionNames)->popup(QCursor::pos());
21482144
}
21492145

21502146
void MainWindow::languageMenuTriggered()

src/NotepadNext/dialogs/MainWindow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private slots:
171171
ZoomEventWatcher *zoomEventWatcher;
172172
int zoomLevel = 0;
173173
int contextMenuPos = 0;
174-
QMenu *buildDynamicMenu(QStringList actionNames);
174+
QMenu *buildMenu(QStringList actionNames);
175175
};
176176

177177
#endif // MAINWINDOW_H

0 commit comments

Comments
 (0)