|
| 1 | +/*************************************************************************** |
| 2 | + * Copyright (c) 2026 Théo Veilleux-Trinh <theo.veilleux.trinh@proton.me>* |
| 3 | + * * |
| 4 | + * This file is part of the FreeCAD CAx development system. * |
| 5 | + * * |
| 6 | + * This library is free software; you can redistribute it and/or * |
| 7 | + * modify it under the terms of the GNU Library General Public * |
| 8 | + * License as published by the Free Software Foundation; either * |
| 9 | + * version 2 of the License, or (at your option) any later version. * |
| 10 | + * * |
| 11 | + * This library is distributed in the hope that it will be useful, * |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | + * GNU Library General Public License for more details. * |
| 15 | + * * |
| 16 | + * You should have received a copy of the GNU Library General Public * |
| 17 | + * License along with this library; see the file COPYING.LIB. If not, * |
| 18 | + * write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 | + * Suite 330, Boston, MA 02111-1307, USA * |
| 20 | + * * |
| 21 | + ***************************************************************************/ |
| 22 | + |
| 23 | +#include "TaskCommandLink.h" |
| 24 | + |
| 25 | +#include "ui_TaskCommandLink.h" |
| 26 | + |
| 27 | +#include "Application.h" |
| 28 | +#include "Document.h" |
| 29 | +#include "ViewProvider.h" |
| 30 | + |
| 31 | +#include <App/Application.h> |
| 32 | +#include <App/DocumentObject.h> |
| 33 | +#include <App/Document.h> |
| 34 | + |
| 35 | +namespace Gui |
| 36 | +{ |
| 37 | +TaskCommandLink::TaskCommandLink() |
| 38 | + : ui(new Ui_TaskCommandLinkDialog()) |
| 39 | +{ |
| 40 | + proxy = new QWidget(this); |
| 41 | + ui->setupUi(proxy); |
| 42 | + ui->objectsList->header()->hide(); |
| 43 | + ui->objectsList->setSelectionMode(QAbstractItemView::SelectionMode::ExtendedSelection); |
| 44 | + |
| 45 | + this->groupLayout()->addWidget(proxy); |
| 46 | + |
| 47 | + buildObjectsList(); |
| 48 | +} |
| 49 | +TaskCommandLink::~TaskCommandLink() |
| 50 | +{ |
| 51 | + delete proxy; |
| 52 | + delete ui; |
| 53 | +} |
| 54 | +std::vector<App::DocumentObject*> TaskCommandLink::selectedObjects() |
| 55 | +{ |
| 56 | + auto selected = ui->objectsList->selectedItems(); |
| 57 | + std::vector<App::DocumentObject*> dst; |
| 58 | + dst.reserve(selected.size()); |
| 59 | + |
| 60 | + for (auto sel : selected) { |
| 61 | + dst.push_back(sel->data(0, Qt::UserRole).value<App::DocumentObject*>()); |
| 62 | + } |
| 63 | + return dst; |
| 64 | +} |
| 65 | +void processObjectsHelper(std::vector<App::DocumentObject*> objs, QTreeWidgetItem* item) |
| 66 | +{ |
| 67 | + for (auto obj : objs) { |
| 68 | + auto objItem = new QTreeWidgetItem(item); |
| 69 | + |
| 70 | + objItem->setText(0, obj->Label.getValue()); |
| 71 | + objItem->setData(0, Qt::UserRole, QVariant::fromValue(obj)); |
| 72 | + |
| 73 | + Gui::ViewProvider* vp = nullptr; |
| 74 | + if (auto doc = Application::Instance->getDocument(obj->getDocument())) { |
| 75 | + vp = doc->getViewProvider(obj); |
| 76 | + } |
| 77 | + if (vp) { |
| 78 | + objItem->setIcon(0, vp->getIcon()); |
| 79 | + processObjectsHelper(vp->claimChildren(), objItem); |
| 80 | + } |
| 81 | + else { |
| 82 | + objItem->setIcon(0, QIcon()); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +void TaskCommandLink::buildObjectsList() |
| 87 | +{ |
| 88 | + ui->objectsList->clear(); |
| 89 | + |
| 90 | + auto allDocuments = App::GetApplication().getDocuments(); |
| 91 | + bool collapse = true; |
| 92 | + std::map<QTreeWidgetItem*, App::Document*> docItemMap; |
| 93 | + |
| 94 | + for (auto doc : allDocuments) { |
| 95 | + auto docItem = new QTreeWidgetItem(); |
| 96 | + std::string itemName = doc->Label.getValue(); |
| 97 | + |
| 98 | + docItem->setText(0, QString::fromStdString(itemName)); |
| 99 | + docItem->setIcon(0, QIcon(QStringLiteral(":/icons/Document.svg"))); |
| 100 | + docItem->setFlags(docItem->flags() & ~Qt::ItemIsSelectable); // Can't link a whole document |
| 101 | + |
| 102 | + docItemMap[docItem] = doc; |
| 103 | + |
| 104 | + ui->objectsList->addTopLevelItem(docItem); |
| 105 | + |
| 106 | + processObjectsHelper(Application::Instance->getDocument(doc)->getTreeRootObjects(), docItem); |
| 107 | + |
| 108 | + if (collapse) { |
| 109 | + ui->objectsList->collapseAll(); |
| 110 | + } |
| 111 | + else { |
| 112 | + ui->objectsList->expandToDepth(0); |
| 113 | + } |
| 114 | + } |
| 115 | + ui->objectsList->selectedItems(); |
| 116 | +} |
| 117 | + |
| 118 | +// dialog |
| 119 | + |
| 120 | +TaskCommandLinkDialog::TaskCommandLinkDialog( |
| 121 | + std::function<void(std::vector<App::DocumentObject*>)> executor_ |
| 122 | +) |
| 123 | + : executor(executor_) |
| 124 | +{ |
| 125 | + commandLink = new TaskCommandLink(); |
| 126 | + Content.push_back(commandLink); |
| 127 | +} |
| 128 | +void TaskCommandLinkDialog::open() |
| 129 | +{} |
| 130 | +bool TaskCommandLinkDialog::accept() |
| 131 | +{ |
| 132 | + executor(commandLink->selectedObjects()); |
| 133 | + return true; |
| 134 | +} |
| 135 | +} // namespace Gui |
0 commit comments