| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************** |
| 3 |
* Copyright (c) 2015 Thomas Anderson <blobfish[at]gmx.com> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#include <QAbstractEventDispatcher> |
| 25 |
#include <QVBoxLayout> |
| 26 |
#include <memory> |
| 27 |
|
| 28 |
#include <Gui/Application.h> |
| 29 |
#include <Gui/Document.h> |
| 30 |
|
| 31 |
#include "DAGView.h" |
| 32 |
#include "DAGModel.h" |
| 33 |
|
| 34 |
|
| 35 |
using namespace Gui; |
| 36 |
using namespace DAG; |
| 37 |
namespace sp = std::placeholders; |
| 38 |
|
| 39 |
DAG::DockWindow::DockWindow(Gui::Document* gDocumentIn, QWidget* parent) |
| 40 |
: Gui::DockWindow(gDocumentIn, parent) |
| 41 |
{ |
| 42 |
dagView = new View(this); |
| 43 |
auto layout = new QVBoxLayout(); |
| 44 |
layout->addWidget(dagView); |
| 45 |
this->setLayout(layout); |
| 46 |
} |
| 47 |
|
| 48 |
View::View(QWidget* parentIn) |
| 49 |
: QGraphicsView(parentIn) |
| 50 |
{ |
| 51 |
this->setRenderHint(QPainter::Antialiasing, true); |
| 52 |
this->setRenderHint(QPainter::TextAntialiasing, true); |
| 53 |
// NOLINTBEGIN |
| 54 |
conActive = Application::Instance->signalActiveDocument.connect( |
| 55 |
std::bind(&View::slotActiveDocument, this, sp::_1) |
| 56 |
); |
| 57 |
conDelete = Application::Instance->signalDeleteDocument.connect( |
| 58 |
std::bind(&View::slotDeleteDocument, this, sp::_1) |
| 59 |
); |
| 60 |
// NOLINTEND |
| 61 |
|
| 62 |
// just update the dagview when the gui process is idle. |
| 63 |
connect(QAbstractEventDispatcher::instance(), &QAbstractEventDispatcher::awake, this, &View::awakeSlot); |
| 64 |
} |
| 65 |
|
| 66 |
View::~View() = default; |
| 67 |
|
| 68 |
void View::slotActiveDocument(const Document& documentIn) |
| 69 |
{ |
| 70 |
if (Gui::Selection().hasSelection()) { |
| 71 |
return; |
| 72 |
} |
| 73 |
ModelMap::const_iterator it = modelMap.find(&documentIn); |
| 74 |
if (it == modelMap.end()) { |
| 75 |
ModelMap::value_type entry( |
| 76 |
std::make_pair(&documentIn, std::make_shared<Model>(this, documentIn)) |
| 77 |
); |
| 78 |
modelMap.insert(entry); |
| 79 |
this->setScene(entry.second.get()); |
| 80 |
} |
| 81 |
else { |
| 82 |
this->setScene(it->second.get()); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
void View::slotDeleteDocument(const Document& documentIn) |
| 87 |
{ |
| 88 |
ModelMap::const_iterator it = modelMap.find(&documentIn); |
| 89 |
if (it != modelMap.end()) { |
| 90 |
modelMap.erase(it); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void View::awakeSlot() |
| 95 |
{ |
| 96 |
Model* model = dynamic_cast<Model*>(this->scene()); |
| 97 |
if (model) { |
| 98 |
model->awake(); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void View::onSelectionChanged(const SelectionChanges& msg) |
| 103 |
{ |
| 104 |
switch (msg.Type) { |
| 105 |
case SelectionChanges::AddSelection: |
| 106 |
case SelectionChanges::RmvSelection: |
| 107 |
case SelectionChanges::SetSelection: |
| 108 |
if (!msg.pDocName || !msg.pDocName[0]) { |
| 109 |
return; |
| 110 |
} |
| 111 |
break; |
| 112 |
case SelectionChanges::ClrSelection: |
| 113 |
if (!msg.pDocName || !msg.pDocName[0]) { |
| 114 |
for (auto& v : modelMap) { |
| 115 |
v.second->selectionChanged(msg); |
| 116 |
} |
| 117 |
return; |
| 118 |
} |
| 119 |
break; |
| 120 |
default: |
| 121 |
return; |
| 122 |
} |
| 123 |
auto doc = Gui::Application::Instance->getDocument(msg.pDocName); |
| 124 |
if (!doc) { |
| 125 |
return; |
| 126 |
} |
| 127 |
auto& model = modelMap[doc]; |
| 128 |
if (!model) { |
| 129 |
model = std::make_shared<Model>(this, *doc); |
| 130 |
} |
| 131 |
this->setScene(model.get()); |
| 132 |
model->selectionChanged(msg); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
#include "moc_DAGView.cpp" |