Skip to content

Commit 339a27a

Browse files
committed
GUI: start to simplify action creation
1 parent a519ded commit 339a27a

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/gui/Src/BasicView/AbstractTableView.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ void AbstractTableView::updateFonts()
7979

8080
void AbstractTableView::updateShortcuts()
8181
{
82+
for(const auto & actionShortcut : actionShortcutPairs)
83+
actionShortcut.action->setShortcut(ConfigShortcut(actionShortcut.shortcut));
8284
}
8385

8486
void AbstractTableView::slot_updateColors()

src/gui/Src/BasicView/AbstractTableView.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include <QApplication>
88
#include <QMouseEvent>
99
#include <QPainter>
10+
#include <QMenu>
1011
#include "StringUtil.h"
12+
#include "Configuration.h"
1113

1214
//Hacky class that fixes a really annoying cursor problem
1315
class AbstractTableScrollBar : public QScrollBar
@@ -199,6 +201,82 @@ public slots:
199201
QColor separatorColor;
200202
QColor headerTextColor;
201203
QColor selectionColor;
204+
205+
//action helpers
206+
private:
207+
struct ActionShortcut
208+
{
209+
QAction* action;
210+
const char* shortcut;
211+
212+
ActionShortcut(QAction* action, const char* shortcut)
213+
: action(action),
214+
shortcut(shortcut)
215+
{
216+
}
217+
};
218+
219+
std::vector<ActionShortcut> actionShortcutPairs;
220+
221+
inline QAction* connectAction(QAction* action, const char* slot)
222+
{
223+
connect(action, SIGNAL(triggered(bool)), this, slot);
224+
addAction(action);
225+
return action;
226+
}
227+
228+
inline QAction* connectShortcutAction(QAction* action, const char* shortcut)
229+
{
230+
actionShortcutPairs.push_back(ActionShortcut(action, shortcut));
231+
action->setShortcut(ConfigShortcut(shortcut));
232+
return action;
233+
}
234+
235+
inline QAction* connectMenuAction(QMenu* menu, QAction* action)
236+
{
237+
return connectMenuAction(menu, action);
238+
}
239+
240+
protected:
241+
inline QAction* makeAction(const QString & text, const char* slot)
242+
{
243+
return connectAction(new QAction(text, this), slot);
244+
}
245+
246+
inline QAction* makeAction(const QIcon & icon, const QString & text, const char* slot)
247+
{
248+
return connectAction(new QAction(icon, text, this), slot);
249+
}
250+
251+
inline QAction* makeShortcutAction(const QString & text, const char* slot, const char* shortcut)
252+
{
253+
return connectShortcutAction(makeAction(text, slot), shortcut);
254+
}
255+
256+
inline QAction* makeShortcutAction(const QIcon & icon, const QString & text, const char* slot, const char* shortcut)
257+
{
258+
return connectShortcutAction(makeAction(icon, text, slot), shortcut);
259+
}
260+
261+
inline QAction* makeMenuAction(QMenu* menu, const QString & text, const char* slot)
262+
{
263+
return connectMenuAction(menu, makeAction(text, slot));
264+
}
265+
266+
inline QAction* makeMenuAction(QMenu* menu, const QIcon & icon, const QString & text, const char* slot)
267+
{
268+
return connectMenuAction(menu, makeAction(icon, text, slot));
269+
}
270+
271+
inline QAction* makeMenuShortcutAction(QMenu* menu, const QString & text, const char* slot, const char* shortcut)
272+
{
273+
return connectShortcutAction(makeMenuAction(menu, text, slot), shortcut);
274+
}
275+
276+
inline QAction* makeMenuShortcutAction(QMenu* menu, const QIcon & icon, const QString & text, const char* slot, const char* shortcut)
277+
{
278+
return connectShortcutAction(makeMenuAction(menu, icon, text, slot), shortcut);
279+
}
202280
};
203281

204282
#endif // ABSTRACTTABLEVIEW_H

0 commit comments

Comments
 (0)