| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/toolbars/basetoolbar.h" |
| 4 | |
| 5 | #include "3rd-party/boolinq/boolinq.h" |
| 6 | #include "definitions/definitions.h" |
| 7 | #include "miscellaneous/settings.h" |
| 8 | |
| 9 | #include <QFont> |
| 10 | #include <QPainter> |
| 11 | #include <QWidgetAction> |
| 12 | |
| 13 | BaseToolBar::BaseToolBar(const QString& title, QWidget* parent) : QToolBar(title, parent) { |
| 14 | // Update right margin of filter textbox. |
| 15 | QMargins margins = contentsMargins(); |
| 16 | |
| 17 | margins.setRight(margins.right() + FILTER_RIGHT_MARGIN); |
| 18 | setContentsMargins(margins); |
| 19 | } |
| 20 | |
| 21 | BaseToolBar::~BaseToolBar() { |
| 22 | qDebugNN << LOGSEC_GUI << "Destroying BaseToolBar instance."; |
| 23 | } |
| 24 | |
| 25 | QList<QAction*> BaseBar::extraActions() const { |
| 26 | return {}; |
| 27 | } |
| 28 | |
| 29 | void BaseBar::loadSavedActions() { |
| 30 | loadSpecificActions(convertActions(savedActions()), true); |
| 31 | } |
| 32 | |
| 33 | QAction* BaseBar::findMatchingAction(const QString& action, const QList<QAction*>& actions) const { |
| 34 | for (QAction* act : actions) { |
| 35 | auto act_obj_name = act->objectName(); |
| 36 | |
| 37 | if (!act_obj_name.isEmpty() && action.startsWith(act_obj_name)) { |
| 38 | return act; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return nullptr; |
| 43 | } |
| 44 | |
| 45 | void BaseToolBar::addActionToMenu(QMenu* menu, |
| 46 | const QIcon& icon, |
| 47 | const QString& title, |
| 48 | const QString& tooltip_suffix, |
| 49 | const QVariant& value, |
| 50 | const QString& object_name) { |
| 51 | QAction* action = menu->addAction(icon, title); |
| 52 | |
| 53 | action->setToolTip(title + tooltip_suffix); |
| 54 | action->setCheckable(true); |
| 55 | action->setData(value); |
| 56 | action->setObjectName(object_name); |
| 57 | } |
| 58 | |
| 59 | void BaseToolBar::activateAction(const QString& action_name, QWidgetAction* widget_action) { |
| 60 | const int start = action_name.indexOf('['); |
| 61 | const int end = action_name.indexOf(']'); |
| 62 | |
| 63 | if (start != -1 && end != -1 && end == action_name.length() - 1) { |
| 64 | const QStringList menu_action_names = action_name.chopped(1).right(end - start - 1).split(QL1C(';')); |
| 65 | auto tool_btn = qobject_cast<QToolButton*>(widget_action->defaultWidget()); |
| 66 | |
| 67 | for (QAction* action : tool_btn->menu()->actions()) { |
| 68 | if (menu_action_names.contains(action->objectName()) && !action->isChecked()) { |
| 69 | action->trigger(); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void BaseToolBar::saveToolButtonSelection(const QString& button_name, |
| 76 | const QString& setting_name, |
| 77 | const QList<QAction*>& actions) const { |
| 78 | QStringList action_names = savedActions(); |
| 79 | auto opts_list = boolinq::from(actions) |
| 80 | .select([](const QAction* act) { |
| 81 | return act->objectName(); |
| 82 | }) |
| 83 | .toStdList(); |
| 84 | QStringList opts = FROM_STD_LIST(QStringList, opts_list); |
| 85 | |
| 86 | for (QString& action_name : action_names) { |
| 87 | if (action_name.startsWith(button_name)) { |
| 88 | action_name = button_name + QSL("[%1]").arg(opts.join(QL1C(';'))); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | qApp->settings()->setValue(GROUP(GUI), setting_name, action_names.join(QSL(","))); |
| 93 | } |
| 94 | |
| 95 | void BaseToolBar::drawNumberOfCriterias(QToolButton* btn, int count) { |
| 96 | QPixmap px(128, 128); |
| 97 | px.fill(Qt::GlobalColor::transparent); |
| 98 | |
| 99 | QPainter p(&px); |
| 100 | |
| 101 | auto fon = p.font(); |
| 102 | |
| 103 | fon.setPixelSize(40); |
| 104 | p.setFont(fon); |
| 105 | |
| 106 | p.drawPixmap(0, 0, 80, 80, btn->defaultAction()->icon().pixmap(128, 128)); |
| 107 | p.drawText(65, 65, 50, 50, Qt::AlignmentFlag::AlignCenter, QString::number(count)); |
| 108 | |
| 109 | btn->setIcon(px); |
| 110 | } |