FreeCAD

Форк
0
/
TaskLinkDim.cpp 
300 строк · 11.3 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2016 WandererFan <wandererfan@gmail.com>                *
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 "PreCompiled.h"
24
#ifndef _PreComp_
25
# include <cmath>
26
# include <QTreeWidget>
27
#endif // #ifndef _PreComp_
28

29
#include <App/Document.h>
30
#include <App/DocumentObject.h>
31
#include <Base/Console.h>
32

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>
41

42
#include "TaskLinkDim.h"
43
#include "ui_TaskLinkDim.h"
44

45

46
using namespace Gui;
47
using namespace TechDraw;
48
using namespace TechDrawGui;
49

50

51
TaskLinkDim::TaskLinkDim(std::vector<App::DocumentObject*> parts, std::vector<std::string>& subs, TechDraw::DrawPage* page) :
52
    ui(new Ui_TaskLinkDim),
53
    m_parts(parts),
54
    m_subs(subs),
55
    m_page(page)
56
{
57
    ui->setupUi(this);
58
    ui->selector->setAvailableLabel(tr("Available"));
59
    ui->selector->setSelectedLabel(tr("Selected"));
60

61
    connect(ui->selector->availableTreeWidget(), &QTreeWidget::currentItemChanged,
62
            this, &TaskLinkDim::onCurrentItemChanged);
63
    connect(ui->selector->selectedTreeWidget(), &QTreeWidget::currentItemChanged,
64
            this, &TaskLinkDim::onCurrentItemChanged);
65

66
    loadAvailDims();
67

68
    ui->leFeature1->setText(QString::fromStdString(parts.at(0)->getNameInDocument()));
69
    ui->leGeometry1->setText(QString::fromStdString(subs.at(0)));
70

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()));
75
        } else {
76
            ui->leFeature2->clear();
77
        }
78
    }
79
}
80

81
TaskLinkDim::~TaskLinkDim()
82
{
83
}
84

85
void TaskLinkDim::loadAvailDims()
86
{
87
    App::Document* doc = m_page->getDocument();
88
    Gui::Document* guiDoc = Gui::Application::Instance->getDocument(doc);
89
    if (!guiDoc)
90
        return;
91

92
    std::vector<App::DocumentObject*> pageViews = m_page->Views.getValues();
93
    std::vector<App::DocumentObject*>::iterator itView = pageViews.begin();
94
    std::string result;
95
    int selRefType = TechDraw::DrawViewDimension::getRefTypeSubElements(m_subs);
96
    //int found = 0;
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
102
    //            found++;
103
                if (dim->has3DReferences()) {
104
                    if (dimReferencesSelection(dim))  {
105
                        loadToTree(dim, true, guiDoc);
106
                    } else {
107
                        continue;                                               //already linked to something else
108
                    }
109
                } else {
110
                    loadToTree(dim, false, guiDoc);
111
                }
112
            }
113
        }
114
    }
115
    //if (found == 0) { "No matching Dimensions found in %s", m_page->getNameInDocument())
116
}
117

118
void TaskLinkDim::loadToTree(const TechDraw::DrawViewDimension* dim, const bool selected, Gui::Document* guiDoc)
119
{
120
    QString label = QString::fromUtf8(dim->Label.getValue());
121
    QString name = QString::fromUtf8(dim->getNameInDocument());
122
    QString tooltip = label + QString::fromUtf8(" / ") + name;
123

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());
130
    if (selected) {
131
        ui->selector->selectedTreeWidget()->addTopLevelItem(child);
132
    } else {
133
        ui->selector->availableTreeWidget()->addTopLevelItem(child);
134
    }
135
}
136

137
//! does this dim already have a reference to the selection?
138
bool TaskLinkDim::dimReferencesSelection(const TechDraw::DrawViewDimension* dim) const
139
{
140
    if (!dim->has3DReferences()) {
141
        return false;
142
    }
143

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()) {
147
        return false;
148
    }
149

150
    if(refParts.empty()) {
151
        //shouldn't happen!
152
    } else if (refParts.size() == 1) {
153
        if ((refParts[0] == m_parts[0]) &&
154
                (refSubs[0] == m_subs[0]) ) {         //everything matches
155
            return true;
156
        }
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]) ) ) {
162
            return true;
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]) ) ) {
167
            return true;
168
        }
169
    }
170

171
    return false;
172
}
173

174
void TaskLinkDim::updateDims()
175
{
176
    int iDim;
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);
183
        if (!dim)
184
            continue;
185
//        std::vector<App::DocumentObject*> parts;
186
//        for (unsigned int iPart = 0; iPart < m_subs.size(); iPart++) {
187
//            parts.push_back(m_part);
188
//        }
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");
195
    }
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
209
        }
210
    }
211
}
212

213
void TaskLinkDim::onCurrentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous)
214
{
215
    Q_UNUSED(current);
216
    Q_UNUSED(previous);
217
//    if (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");
222
//        }
223
//        if (previous->treeWidget() == ui->selector->availableTreeWidget()) {
224
//            Base::Console().Message("TRACE - TLD::onCurrent - previous belongs to available\n");
225
//        }
226
//    }
227
//    if (current) {
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");
232
//        }
233
//        if (current->treeWidget() == ui->selector->availableTreeWidget()) {
234
//            Base::Console().Message("TRACE - TLD::onCurrent - current belongs to available\n");
235
//        }
236
//    }
237
}
238

239
bool TaskLinkDim::accept()
240
{
241
    updateDims();
242
    return true;
243
}
244

245
bool TaskLinkDim::reject()
246
{
247
    return true;
248
}
249

250
void TaskLinkDim::changeEvent(QEvent *event)
251
{
252
    if (event->type() == QEvent::LanguageChange) {
253
        ui->retranslateUi(this);
254
    }
255
}
256

257

258
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
259
TaskDlgLinkDim::TaskDlgLinkDim(std::vector<App::DocumentObject*> parts, std::vector<std::string>& subs, TechDraw::DrawPage* page) :
260
    TaskDialog()
261
{
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);
267
}
268

269
TaskDlgLinkDim::~TaskDlgLinkDim()
270
{
271
}
272

273
void TaskDlgLinkDim::update()
274
{
275
    //widget->updateTask();
276
}
277

278
//==== calls from the TaskView ===============================================================
279
void TaskDlgLinkDim::open()
280
{
281
}
282

283
void TaskDlgLinkDim::clicked(int i)
284
{
285
    Q_UNUSED(i);
286
}
287

288
bool TaskDlgLinkDim::accept()
289
{
290
    widget->accept();
291
    return true;
292
}
293

294
bool TaskDlgLinkDim::reject()
295
{
296
    widget->reject();
297
    return true;
298
}
299

300
#include <Mod/TechDraw/Gui/moc_TaskLinkDim.cpp>
301

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.