forked from FreeCAD/FreeCAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewProvider.cpp
362 lines (297 loc) · 12.4 KB
/
ViewProvider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/***************************************************************************
* Copyright (c) 2011 Juergen Riegel <FreeCAD@juergen-riegel.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QMessageBox>
# include <QAction>
# include <QApplication>
# include <QMenu>
#include <Inventor/nodes/SoSwitch.h>
#endif
#include <Gui/ActionFunction.h>
#include <Gui/Command.h>
#include <Gui/MDIView.h>
#include <Gui/Control.h>
#include <Gui/Application.h>
#include <Gui/Document.h>
#include <Gui/BitmapFactory.h>
#include <Base/Exception.h>
#include <Mod/PartDesign/App/Body.h>
#include <Mod/PartDesign/App/Feature.h>
#include <Mod/Sketcher/App/SketchObject.h>
#include "Utils.h"
#include "TaskFeatureParameters.h"
#include "ViewProvider.h"
#include "ViewProviderPy.h"
using namespace PartDesignGui;
PROPERTY_SOURCE_WITH_EXTENSIONS(PartDesignGui::ViewProvider, PartGui::ViewProviderPart)
ViewProvider::ViewProvider()
:oldWb(""), oldTip(NULL), isSetTipIcon(false)
{
PartGui::ViewProviderAttachExtension::initExtension(this);
}
ViewProvider::~ViewProvider()
{
}
bool ViewProvider::doubleClicked(void)
{
try {
PartDesign::Body* body = PartDesign::Body::findBodyOf(getObject());
QString text = QObject::tr("Edit %1").arg(QString::fromUtf8(getObject()->Label.getValue()));
Gui::Command::openCommand(text.toUtf8());
FCMD_SET_EDIT(pcObject);
}
catch (const Base::Exception&) {
Gui::Command::abortCommand();
}
return true;
}
void ViewProvider::startDefaultEditMode()
{
QString text = QObject::tr("Edit %1").arg(QString::fromUtf8(getObject()->Label.getValue()));
Gui::Command::openCommand(text.toUtf8());
Gui::Document* document = this->getDocument();
if (document) {
document->setEdit(this, ViewProvider::Default);
}
}
void ViewProvider::addDefaultAction(QMenu* menu, const QString& text)
{
QAction* act = menu->addAction(text);
act->setData(QVariant((int)ViewProvider::Default));
Gui::ActionFunction* func = new Gui::ActionFunction(menu);
func->trigger(act, boost::bind(&ViewProvider::startDefaultEditMode, this));
}
void ViewProvider::setupContextMenu(QMenu* menu, QObject* receiver, const char* member)
{
QAction* act = menu->addAction(QObject::tr("Set colors..."), receiver, member);
act->setData(QVariant((int)ViewProvider::Color));
// Call the extensions
Gui::ViewProvider::setupContextMenu(menu, receiver, member);
}
bool ViewProvider::setEdit(int ModNum)
{
if (ModNum == ViewProvider::Default ) {
// When double-clicking on the item for this feature the
// object unsets and sets its edit mode without closing
// the task panel
Gui::TaskView::TaskDialog *dlg = Gui::Control().activeDialog();
TaskDlgFeatureParameters *featureDlg = qobject_cast<TaskDlgFeatureParameters *>(dlg);
// NOTE: if the dialog is not partDesigan dialog the featureDlg will be NULL
if (featureDlg && featureDlg->viewProvider() != this) {
featureDlg = 0; // another feature left open its task panel
}
if (dlg && !featureDlg) {
QMessageBox msgBox;
msgBox.setText(QObject::tr("A dialog is already open in the task panel"));
msgBox.setInformativeText(QObject::tr("Do you want to close this dialog?"));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
int ret = msgBox.exec();
if (ret == QMessageBox::Yes) {
Gui::Control().reject();
} else {
return false;
}
}
// clear the selection (convenience)
Gui::Selection().clearSelection();
// always change to PartDesign WB, remember where we come from
oldWb = Gui::Command::assureWorkbench("PartDesignWorkbench");
// start the edit dialog if
if (!featureDlg) {
featureDlg = this->getEditDialog();
if (!featureDlg) { // Shouldn't generally happen
throw Base::RuntimeError ("Failed to create new edit dialog.");
}
}
Gui::Control().showDialog(featureDlg);
return true;
} else {
return PartGui::ViewProviderPart::setEdit(ModNum);
}
}
TaskDlgFeatureParameters *ViewProvider::getEditDialog() {
throw Base::NotImplementedError("getEditDialog() not implemented");
}
void ViewProvider::unsetEdit(int ModNum)
{
// return to the WB we were in before editing the PartDesign feature
if (!oldWb.empty())
Gui::Command::assureWorkbench(oldWb.c_str());
if (ModNum == ViewProvider::Default) {
// when pressing ESC make sure to close the dialog
#if 0
PartDesign::Body* activeBody = Gui::Application::Instance->activeView()->getActiveObject<PartDesign::Body*>(PDBODYKEY);
#endif
Gui::Control().closeDialog();
#if 0
if ((activeBody != NULL) && (oldTip != NULL)) {
Gui::Selection().clearSelection();
Gui::Selection().addSelection(oldTip->getDocument()->getName(), oldTip->getNameInDocument());
Gui::Command::doCommand(Gui::Command::Gui,"FreeCADGui.runCommand('PartDesign_MoveTip')");
}
#endif
oldTip = NULL;
}
else {
PartGui::ViewProviderPart::unsetEdit(ModNum);
oldTip = NULL;
}
}
void ViewProvider::updateData(const App::Property* prop)
{
// TODO What's that? (2015-07-24, Fat-Zer)
if (prop->getTypeId() == Part::PropertyPartShape::getClassTypeId() &&
strcmp(prop->getName(),"AddSubShape") == 0) {
return;
}
inherited::updateData(prop);
}
void ViewProvider::onChanged(const App::Property* prop) {
//if the object is inside of a body we make sure it is the only visible one on activation
if(prop == &Visibility && Visibility.getValue()) {
Part::BodyBase* body = Part::BodyBase::findBodyOf(getObject());
if(body) {
//hide all features in the body other than this object
for(App::DocumentObject* obj : body->Group.getValues()) {
if(obj->isDerivedFrom(PartDesign::Feature::getClassTypeId()) && obj != getObject()) {
auto vpd = Base::freecad_dynamic_cast<Gui::ViewProviderDocumentObject>(
Gui::Application::Instance->getViewProvider(obj));
if(vpd && vpd->Visibility.getValue())
vpd->Visibility.setValue(false);
}
}
}
}
PartGui::ViewProviderPartExt::onChanged(prop);
}
void ViewProvider::setTipIcon(bool onoff) {
isSetTipIcon = onoff;
signalChangeIcon();
}
QIcon ViewProvider::mergeColorfulOverlayIcons (const QIcon & orig) const
{
QIcon mergedicon = orig;
if(isSetTipIcon) {
QPixmap px;
static const char * const feature_tip_xpm[]={
"9 9 3 1",
". c None",
"# c #00cc00",
"a c #ffffff",
"...###...",
".##aaa##.",
".##aaa##.",
"###aaa###",
"##aaaaa##",
"##aaaaa##",
".##aaa##.",
".##aaa##.",
"...###..."};
px = QPixmap(feature_tip_xpm);
mergedicon = Gui::BitmapFactoryInst::mergePixmap(mergedicon, px, Gui::BitmapFactoryInst::BottomRight);
}
return Gui::ViewProvider::mergeColorfulOverlayIcons (mergedicon);
}
bool ViewProvider::onDelete(const std::vector<std::string> &)
{
PartDesign::Feature* feature = static_cast<PartDesign::Feature*>(getObject());
App::DocumentObject* previousfeat = feature->BaseFeature.getValue();
// Visibility - we want:
// 1. If the visible object is not the one being deleted, we leave that one visible.
// 2. If the visible object is the one being deleted, we make the previous object visible.
if (isShow() && previousfeat && Gui::Application::Instance->getViewProvider(previousfeat)) {
Gui::Application::Instance->getViewProvider(previousfeat)->show();
}
// find surrounding features in the tree
Part::BodyBase* body = PartDesign::Body::findBodyOf(getObject());
if (body != NULL) {
// Deletion from the tree of a feature is handled by Document.removeObject, which has no clue
// about what a body is. Therefore, Bodies, although an "activable" container, know nothing
// about what happens at Document level with the features they contain.
//
// The Deletion command StdCmdDelete::activated, however does notify the viewprovider corresponding
// to the feature (not body) of the imminent deletion (before actually doing it).
//
// Consequently, the only way of notifying a body of the imminent deletion of one of its features
// so as to do the clean up required (moving basefeature references, tip management) is from the
// viewprovider, so we call it here.
//
// fixes (#3084)
FCMD_OBJ_CMD(body,"removeObject(" << Gui::Command::getObjectCmd(feature) << ')');
}
return true;
}
void ViewProvider::setBodyMode(bool bodymode) {
std::vector<App::Property*> props;
getPropertyList(props);
auto vp = getBodyViewProvider();
if(!vp)
return;
for(App::Property* prop : props) {
//we keep visibility and selectibility per object
if(prop == &Visibility ||
prop == &Selectable)
continue;
//we hide only properties which are available in the body, not special ones
if(!vp->getPropertyByName(prop->getName()))
continue;
prop->setStatus(App::Property::Hidden, bodymode);
}
}
void ViewProvider::makeTemporaryVisible(bool onoff)
{
//make sure to not use the overridden versions, as they change properties
if (onoff) {
if (VisualTouched) {
updateVisual();
}
Gui::ViewProvider::show();
}
else
Gui::ViewProvider::hide();
}
PyObject* ViewProvider::getPyObject()
{
if (!pyViewObject)
pyViewObject = new ViewProviderPy(this);
pyViewObject->IncRef();
return pyViewObject;
}
ViewProviderBody* ViewProvider::getBodyViewProvider() {
auto body = PartDesign::Body::findBodyOf(getObject());
auto doc = getDocument();
if(body && doc) {
auto vp = doc->getViewProvider(body);
if(vp && vp->isDerivedFrom(ViewProviderBody::getClassTypeId()))
return static_cast<ViewProviderBody*>(vp);
}
return nullptr;
}
namespace Gui {
/// @cond DOXERR
PROPERTY_SOURCE_TEMPLATE(PartDesignGui::ViewProviderPython, PartDesignGui::ViewProvider)
/// @endcond
// explicit template instantiation
template class PartDesignGuiExport ViewProviderPythonFeatureT<PartDesignGui::ViewProvider>;
}