|
| 1 | +#include "ActionsWidget.h" |
| 2 | + |
| 3 | +ActionsWidget::ActionsWidget(QWidget* parent): |
| 4 | + QWidget(parent), |
| 5 | + _model(this), |
| 6 | + _proxy(this), |
| 7 | + _splitter(Qt::Horizontal, this) |
| 8 | +{ |
| 9 | + _proxy.setSourceModel(&_model); |
| 10 | + _proxy.setFilterCaseSensitivity(Qt::CaseInsensitive); |
| 11 | + _proxy.setSearchRoles({Qt::ToolTipRole, Roles::ActionId}); |
| 12 | + |
| 13 | + _mainLayout.addWidget(&_splitter); |
| 14 | + |
| 15 | + setLayout(&_mainLayout); |
| 16 | + |
| 17 | + _leftPanelWidget.setLayout(&_leftPanelLayout); |
| 18 | + _rightPanelWidget.setLayout(&_rightPanelLayout); |
| 19 | + |
| 20 | + _splitter.addWidget(&_leftPanelWidget); |
| 21 | + _splitter.addWidget(&_rightPanelWidget); |
| 22 | + |
| 23 | + _splitter.setStretchFactor(0, 0); |
| 24 | + _splitter.setStretchFactor(1, 1); |
| 25 | + |
| 26 | + _splitter.setSizes({ 320, 680 }); |
| 27 | + |
| 28 | + _leftPanelLayout.addWidget(&_search); |
| 29 | + _leftPanelLayout.addWidget(&_tree); |
| 30 | + |
| 31 | + _rightPanelLayout.addWidget(&_detailsArea); |
| 32 | + |
| 33 | + _search.setPlaceholderText("Search actions"); |
| 34 | + |
| 35 | + _tree.setModel(&_proxy); |
| 36 | + _tree.setHeaderHidden(true); |
| 37 | + _tree.setUniformRowHeights(true); |
| 38 | + _tree.setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 39 | + _tree.setSelectionMode(QAbstractItemView::SingleSelection); |
| 40 | + _tree.setSelectionBehavior(QAbstractItemView::SelectRows); |
| 41 | + _tree.header()->setStretchLastSection(true); |
| 42 | + |
| 43 | + _detailsLayout.setContentsMargins(0, 0, 0, 0); |
| 44 | + |
| 45 | + _detailsArea.setWidgetResizable(true); |
| 46 | + _detailsArea.setWidget(&_detailsHost); |
| 47 | + |
| 48 | + connect(&_search, &QLineEdit::textChanged, this, [this](const QString& text) { |
| 49 | + _proxy.setFilterRegularExpression(QRegularExpression(QRegularExpression::escape(text), QRegularExpression::CaseInsensitiveOption)); |
| 50 | + |
| 51 | + // UX: when searching, expand all; when empty, collapse to top-level |
| 52 | + if (!text.isEmpty()) |
| 53 | + _tree.expandAll(); |
| 54 | + else |
| 55 | + _tree.collapseAll(); |
| 56 | + }); |
| 57 | + |
| 58 | + connect(_tree.selectionModel(), &QItemSelectionModel::currentChanged, this, &ActionsWidget::onCurrentChanged); |
| 59 | +} |
| 60 | + |
| 61 | +void ActionsWidget::addAction(const QString& category, const QString& text, const QString& actionId, Factory factory, const QIcon& icon, const QString& toolTip) |
| 62 | +{ |
| 63 | + const auto cat = category.trimmed().isEmpty() ? QStringLiteral("Uncategorized") : category.trimmed(); |
| 64 | + |
| 65 | + auto categoryItem = ensureCategory(cat); |
| 66 | + |
| 67 | + auto actionItem = new QStandardItem(icon, text); |
| 68 | + |
| 69 | + actionItem->setToolTip(toolTip); |
| 70 | + actionItem->setData(ActionNode, Roles::NodeType); |
| 71 | + actionItem->setData(actionId, Roles::ActionId); |
| 72 | + |
| 73 | + categoryItem->appendRow(actionItem); |
| 74 | + |
| 75 | + _factories.insert(actionId, std::move(factory)); |
| 76 | +} |
| 77 | + |
| 78 | +void ActionsWidget::onCurrentChanged(const QModelIndex& current, const QModelIndex& previous) |
| 79 | +{ |
| 80 | + clearDetails(); |
| 81 | + |
| 82 | + if (!current.isValid()) |
| 83 | + return; |
| 84 | + |
| 85 | + const auto sourceIndex = _proxy.mapToSource(current); |
| 86 | + |
| 87 | + auto item = _model.itemFromIndex(sourceIndex); |
| 88 | + |
| 89 | + if (!item) |
| 90 | + return; |
| 91 | + |
| 92 | + const auto type = item->data(Roles::NodeType).toInt(); |
| 93 | + |
| 94 | + if (type != ActionNode) |
| 95 | + return; // category selected: leave details empty (or show placeholder) |
| 96 | + |
| 97 | + const auto actionId = item->data(Roles::ActionId).toString(); |
| 98 | + |
| 99 | + auto it = _factories.find(actionId); |
| 100 | + |
| 101 | + if (it == _factories.end()) |
| 102 | + return; |
| 103 | + |
| 104 | + auto actionWidget = it.value()(&_detailsHost); |
| 105 | + |
| 106 | + if (!actionWidget) |
| 107 | + return; |
| 108 | + |
| 109 | + _detailsLayout.addWidget(actionWidget); |
| 110 | + _detailsLayout.addStretch(1); |
| 111 | +} |
| 112 | + |
| 113 | +QStandardItem* ActionsWidget::ensureCategory(const QString& category) |
| 114 | +{ |
| 115 | + if (auto it = _categories.find(category); it != _categories.end()) |
| 116 | + return it.value(); |
| 117 | + |
| 118 | + auto catItem = new QStandardItem(category); |
| 119 | + |
| 120 | + catItem->setData(CategoryNode, Roles::NodeType); |
| 121 | + catItem->setSelectable(true); |
| 122 | + |
| 123 | + _model.appendRow(catItem); |
| 124 | + _categories.insert(category, catItem); |
| 125 | + |
| 126 | + return catItem; |
| 127 | +} |
| 128 | + |
| 129 | +void ActionsWidget::clearDetails() |
| 130 | +{ |
| 131 | + while (auto child = _detailsLayout.takeAt(0)) |
| 132 | + { |
| 133 | + if (auto actionWidget = child->widget()) |
| 134 | + actionWidget->deleteLater(); |
| 135 | + |
| 136 | + delete child; |
| 137 | + } |
| 138 | +} |
0 commit comments