1
/***************************************************************************
2
* Copyright (c) 2016 WandererFan <wandererfan@gmail.com> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
26
# include <QTreeWidget>
27
#endif // #ifndef _PreComp_
29
#include <App/Document.h>
30
#include <App/DocumentObject.h>
31
#include <Base/Console.h>
33
#include <Gui/Application.h>
34
#include <Gui/BitmapFactory.h>
35
#include <Gui/Command.h>
36
#include <Gui/Document.h>
37
#include <Gui/Selection.h>
38
#include <Gui/ViewProvider.h>
39
#include <Mod/TechDraw/App/DrawPage.h>
40
#include <Mod/TechDraw/App/DrawViewDimension.h>
42
#include "TaskLinkDim.h"
43
#include "ui_TaskLinkDim.h"
47
using namespace TechDraw;
48
using namespace TechDrawGui;
51
TaskLinkDim::TaskLinkDim(std::vector<App::DocumentObject*> parts, std::vector<std::string>& subs, TechDraw::DrawPage* page) :
52
ui(new Ui_TaskLinkDim),
58
ui->selector->setAvailableLabel(tr("Available"));
59
ui->selector->setSelectedLabel(tr("Selected"));
61
connect(ui->selector->availableTreeWidget(), &QTreeWidget::currentItemChanged,
62
this, &TaskLinkDim::onCurrentItemChanged);
63
connect(ui->selector->selectedTreeWidget(), &QTreeWidget::currentItemChanged,
64
this, &TaskLinkDim::onCurrentItemChanged);
68
ui->leFeature1->setText(QString::fromStdString(parts.at(0)->getNameInDocument()));
69
ui->leGeometry1->setText(QString::fromStdString(subs.at(0)));
71
if (subs.size() > 1) {
72
ui->leGeometry2->setText(QString::fromStdString(subs.at(1)));
73
if (parts.at(0)->getNameInDocument() != parts.at(1)->getNameInDocument()) {
74
ui->leFeature2->setText(QString::fromStdString(parts.at(1)->getNameInDocument()));
76
ui->leFeature2->clear();
81
TaskLinkDim::~TaskLinkDim()
85
void TaskLinkDim::loadAvailDims()
87
App::Document* doc = m_page->getDocument();
88
Gui::Document* guiDoc = Gui::Application::Instance->getDocument(doc);
92
std::vector<App::DocumentObject*> pageViews = m_page->Views.getValues();
93
std::vector<App::DocumentObject*>::iterator itView = pageViews.begin();
95
int selRefType = TechDraw::DrawViewDimension::getRefTypeSubElements(m_subs);
97
for (; itView != pageViews.end(); itView++) {
98
if ((*itView)->isDerivedFrom(TechDraw::DrawViewDimension::getClassTypeId())) {
99
TechDraw::DrawViewDimension* dim = static_cast<TechDraw::DrawViewDimension*>((*itView));
100
int dimRefType = dim->getRefType();
101
if (dimRefType == selRefType) { //potential matches
103
if (dim->has3DReferences()) {
104
if (dimReferencesSelection(dim)) {
105
loadToTree(dim, true, guiDoc);
107
continue; //already linked to something else
110
loadToTree(dim, false, guiDoc);
115
//if (found == 0) { "No matching Dimensions found in %s", m_page->getNameInDocument())
118
void TaskLinkDim::loadToTree(const TechDraw::DrawViewDimension* dim, const bool selected, Gui::Document* guiDoc)
120
QString label = QString::fromUtf8(dim->Label.getValue());
121
QString name = QString::fromUtf8(dim->getNameInDocument());
122
QString tooltip = label + QString::fromUtf8(" / ") + name;
124
QTreeWidgetItem* child = new QTreeWidgetItem();
125
child->setText(0, label);
126
child->setToolTip(0, tooltip);
127
child->setData(0, Qt::UserRole, name);
128
Gui::ViewProvider* vp = guiDoc->getViewProvider(dim);
129
if (vp) child->setIcon(0, vp->getIcon());
131
ui->selector->selectedTreeWidget()->addTopLevelItem(child);
133
ui->selector->availableTreeWidget()->addTopLevelItem(child);
137
//! does this dim already have a reference to the selection?
138
bool TaskLinkDim::dimReferencesSelection(const TechDraw::DrawViewDimension* dim) const
140
if (!dim->has3DReferences()) {
144
std::vector<App::DocumentObject*> refParts = dim->References3D.getValues();
145
std::vector<std::string> refSubs = dim->References3D.getSubValues();
146
if (refParts.size() != m_parts.size()) {
150
if(refParts.empty()) {
152
} else if (refParts.size() == 1) {
153
if ((refParts[0] == m_parts[0]) &&
154
(refSubs[0] == m_subs[0]) ) { //everything matches
157
} else if (refParts.size() == 2) {
158
if (( (refParts[0] == m_parts[0]) &&
159
(refParts[1] == m_parts[1]) ) &&
160
( (refSubs[0] == m_subs[0]) &&
161
(refSubs[1] == m_subs[1]) ) ) {
163
} else if (( (refParts[0] == m_parts[1]) &&
164
(refParts[1] == m_parts[0]) ) &&
165
( (refSubs[0] == m_subs[1]) &&
166
(refSubs[1] == m_subs[0]) ) ) {
174
void TaskLinkDim::updateDims()
177
int count = ui->selector->selectedTreeWidget()->topLevelItemCount();
178
for (iDim=0; iDim<count; iDim++) {
179
QTreeWidgetItem* child = ui->selector->selectedTreeWidget()->topLevelItem(iDim);
180
QString name = child->data(0, Qt::UserRole).toString();
181
App::DocumentObject* obj = m_page->getDocument()->getObject(name.toStdString().c_str());
182
TechDraw::DrawViewDimension* dim = dynamic_cast<TechDraw::DrawViewDimension*>(obj);
185
// std::vector<App::DocumentObject*> parts;
186
// for (unsigned int iPart = 0; iPart < m_subs.size(); iPart++) {
187
// parts.push_back(m_part);
189
dim->References3D.setValues(m_parts, m_subs);
190
std::string DimName = dim->getNameInDocument();
191
std::string measureType = "True";
192
Gui::Command::doCommand(Gui::Command::Gui, "App.activeDocument().%s.MeasureType = \'%s\'",
193
DimName.c_str(), measureType.c_str());
194
//dim->MeasureType.setValue("True");
196
count = ui->selector->availableTreeWidget()->topLevelItemCount();
197
for (iDim=0; iDim < count; iDim++) {
198
QTreeWidgetItem* child = ui->selector->availableTreeWidget()->topLevelItem(iDim);
199
QString name = child->data(0, Qt::UserRole).toString();
200
App::DocumentObject* obj = m_page->getDocument()->getObject(name.toStdString().c_str());
201
TechDraw::DrawViewDimension* dim = dynamic_cast<TechDraw::DrawViewDimension*>(obj);
202
if (dim && dimReferencesSelection(dim)) {
203
std::string measureType = "Projected";
204
std::string DimName = dim->getNameInDocument();
205
Gui::Command::doCommand(Gui::Command::Gui, "App.activeDocument().%s.MeasureType = \'%s\'",
206
DimName.c_str(), measureType.c_str());
207
dim->References3D.setValue(nullptr, ""); //DVD.References3D
208
dim->clear3DMeasurements(); //DVD.measurement.References3D
213
void TaskLinkDim::onCurrentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous)
218
// Base::Console().Message("TRACE - TLD::onCurrent - text: %s data: %s is previous\n",
219
// qPrintable(previous->text(0)), qPrintable(previous->data(0, Qt::UserRole).toString()));
220
// if (previous->treeWidget() == ui->selector->selectedTreeWidget()) {
221
// Base::Console().Message("TRACE - TLD::onCurrent - previous belongs to selected\n");
223
// if (previous->treeWidget() == ui->selector->availableTreeWidget()) {
224
// Base::Console().Message("TRACE - TLD::onCurrent - previous belongs to available\n");
228
// Base::Console().Message("TRACE - TLD::onCurrent - text: %s data: %s is current\n",
229
// qPrintable(current->text(0)), qPrintable(current->data(0, Qt::UserRole).toString()));
230
// if (current->treeWidget() == ui->selector->selectedTreeWidget()) {
231
// Base::Console().Message("TRACE - TLD::onCurrent - current belongs to selected\n");
233
// if (current->treeWidget() == ui->selector->availableTreeWidget()) {
234
// Base::Console().Message("TRACE - TLD::onCurrent - current belongs to available\n");
239
bool TaskLinkDim::accept()
245
bool TaskLinkDim::reject()
250
void TaskLinkDim::changeEvent(QEvent *event)
252
if (event->type() == QEvent::LanguageChange) {
253
ui->retranslateUi(this);
258
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
259
TaskDlgLinkDim::TaskDlgLinkDim(std::vector<App::DocumentObject*> parts, std::vector<std::string>& subs, TechDraw::DrawPage* page) :
262
widget = new TaskLinkDim(parts, subs, page);
263
taskbox = new Gui::TaskView::TaskBox(Gui::BitmapFactory().pixmap("TechDraw_LinkDimension"),
264
widget->windowTitle(), true, nullptr);
265
taskbox->groupLayout()->addWidget(widget);
266
Content.push_back(taskbox);
269
TaskDlgLinkDim::~TaskDlgLinkDim()
273
void TaskDlgLinkDim::update()
275
//widget->updateTask();
278
//==== calls from the TaskView ===============================================================
279
void TaskDlgLinkDim::open()
283
void TaskDlgLinkDim::clicked(int i)
288
bool TaskDlgLinkDim::accept()
294
bool TaskDlgLinkDim::reject()
300
#include <Mod/TechDraw/Gui/moc_TaskLinkDim.cpp>