File indexing completed on 2024-05-19 05:42:26
0001 // ct_lvtqtw_namespacetreeview.cpp -*-C++-*- 0002 0003 /* 0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk> 0005 // SPDX-License-Identifier: Apache-2.0 0006 // 0007 // Licensed under the Apache License, Version 2.0 (the "License"); 0008 // you may not use this file except in compliance with the License. 0009 // You may obtain a copy of the License at 0010 // 0011 // http://www.apache.org/licenses/LICENSE-2.0 0012 // 0013 // Unless required by applicable law or agreed to in writing, software 0014 // distributed under the License is distributed on an "AS IS" BASIS, 0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0016 // See the License for the specific language governing permissions and 0017 // limitations under the License. 0018 */ 0019 #include <ct_lvtqtw_namespacetreeview.h> 0020 0021 #include <ct_lvtmdl_modelhelpers.h> 0022 #include <ct_lvtmdl_namespacetreemodel.h> 0023 0024 #include <QMenu> 0025 0026 namespace Codethink::lvtqtw { 0027 0028 struct NamespaceTreeView::Private { 0029 lvtmdl::NamespaceTreeModel *model; 0030 }; 0031 0032 NamespaceTreeView::NamespaceTreeView(QWidget *parent): 0033 TreeView(parent), d(std::make_unique<NamespaceTreeView::Private>()) 0034 { 0035 setHeaderHidden(false); 0036 setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu); 0037 0038 connect(this, &QTreeView::customContextMenuRequested, this, &NamespaceTreeView::createContextMenu); 0039 } 0040 0041 NamespaceTreeView::~NamespaceTreeView() noexcept = default; 0042 0043 void NamespaceTreeView::createContextMenu(const QPoint& point) 0044 { 0045 QModelIndex idx = indexAt(point); 0046 if (!idx.isValid()) { 0047 return; 0048 } 0049 0050 QMenu menu; 0051 menu.addActions(createActionsForNamespaces(idx)); 0052 menu.addActions(createActionsForNoElement(idx)); 0053 0054 if (!menu.actions().empty()) { 0055 menu.exec(viewport()->mapToGlobal(point)); 0056 } 0057 } 0058 0059 void NamespaceTreeView::setModel(QAbstractItemModel *model) 0060 { 0061 d->model = qobject_cast<lvtmdl::NamespaceTreeModel *>(model); 0062 TreeView::setModel(model); 0063 } 0064 0065 QList<QAction *> NamespaceTreeView::createActionsForNamespaces(const QModelIndex& idx) const 0066 { 0067 if (!idx.data(lvtmdl::ModelRoles::e_IsBranch).value<bool>()) { 0068 return {}; 0069 } 0070 0071 if (d->model->rootNamespace()) { 0072 return {}; 0073 } 0074 0075 auto *action = new QAction(tr("Set as Root Namespace")); 0076 action->setData(idx.row()); 0077 connect(action, &QAction::triggered, this, [this, idx] { 0078 d->model->setRootNamespace(idx.data().toString()); 0079 }); 0080 0081 return {action}; 0082 } 0083 0084 QList<QAction *> NamespaceTreeView::createActionsForNoElement(const QModelIndex& idx) const 0085 { 0086 Q_UNUSED(idx); 0087 0088 if (!d->model->rootNamespace()) { 0089 return {}; 0090 } 0091 0092 auto *action = new QAction(tr("Use Global Namespace")); 0093 connect(action, &QAction::triggered, this, [this] { 0094 d->model->setRootNamespace({}); 0095 }); 0096 0097 return {action}; 0098 } 0099 0100 } // namespace Codethink::lvtqtw