| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 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 |
|
| 24 |
#include <QMenu> |
| 25 |
|
| 26 |
|
| 27 |
#include <App/Document.h> |
| 28 |
#include <App/DocumentObject.h> |
| 29 |
#include <App/Part.h> |
| 30 |
|
| 31 |
#include "ViewProviderPart.h" |
| 32 |
#include "ActionFunction.h" |
| 33 |
#include "Application.h" |
| 34 |
#include "BitmapFactory.h" |
| 35 |
#include "Command.h" |
| 36 |
#include "MDIView.h" |
| 37 |
|
| 38 |
|
| 39 |
using namespace Gui; |
| 40 |
|
| 41 |
|
| 42 |
PROPERTY_SOURCE_WITH_EXTENSIONS(Gui::ViewProviderPart, Gui::ViewProviderDragger) |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Creates the view provider for an object group. |
| 47 |
*/ |
| 48 |
ViewProviderPart::ViewProviderPart() |
| 49 |
{ |
| 50 |
initExtension(this); |
| 51 |
|
| 52 |
sPixmap = "Geofeaturegroup.svg"; |
| 53 |
aPixmap = "Geoassembly.svg"; |
| 54 |
} |
| 55 |
|
| 56 |
ViewProviderPart::~ViewProviderPart() = default; |
| 57 |
|
| 58 |
/** |
| 59 |
* TODO |
| 60 |
* Whenever a property of the group gets changed then the same property of all |
| 61 |
* associated view providers of the objects of the object group get changed as well. |
| 62 |
*/ |
| 63 |
void ViewProviderPart::onChanged(const App::Property* prop) |
| 64 |
{ |
| 65 |
ViewProviderDragger::onChanged(prop); |
| 66 |
} |
| 67 |
|
| 68 |
void ViewProviderPart::setupContextMenu(QMenu* menu, QObject* receiver, const char* member) |
| 69 |
{ |
| 70 |
auto func = new Gui::ActionFunction(menu); |
| 71 |
|
| 72 |
QAction* act = menu->addAction(QObject::tr("Active Object")); |
| 73 |
act->setCheckable(true); |
| 74 |
act->setChecked(isActivePart()); |
| 75 |
func->trigger(act, [this]() { this->toggleActivePart(); }); |
| 76 |
|
| 77 |
ViewProviderDragger::setupContextMenu(menu, receiver, member); |
| 78 |
} |
| 79 |
|
| 80 |
bool ViewProviderPart::isActivePart(const char* key) |
| 81 |
{ |
| 82 |
App::DocumentObject* activePart = nullptr; |
| 83 |
auto activeDoc = Gui::Application::Instance->activeDocument(); |
| 84 |
if (!activeDoc) { |
| 85 |
activeDoc = getDocument(); |
| 86 |
} |
| 87 |
auto activeView = activeDoc->setActiveView(this); |
| 88 |
if (!activeView) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
activePart = activeView->getActiveObject<App::DocumentObject*>(key); |
| 93 |
|
| 94 |
if (activePart == this->getObject()) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
else { |
| 98 |
return false; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void ViewProviderPart::toggleActivePart() |
| 103 |
{ |
| 104 |
// make the part the active one |
| 105 |
if (isActivePart()) { |
| 106 |
// active part double-clicked. Deactivate. |
| 107 |
Gui::Command::doCommand( |
| 108 |
Gui::Command::Gui, |
| 109 |
"Gui.ActiveDocument.ActiveView.setActiveObject('%s', None)", |
| 110 |
PARTKEY |
| 111 |
); |
| 112 |
} |
| 113 |
else { |
| 114 |
// set new active part |
| 115 |
Gui::Command::doCommand( |
| 116 |
Gui::Command::Gui, |
| 117 |
"Gui.ActiveDocument.ActiveView.setActiveObject('%s', " |
| 118 |
"App.getDocument('%s').getObject('%s'))", |
| 119 |
PARTKEY, |
| 120 |
this->getObject()->getDocument()->getName(), |
| 121 |
this->getObject()->getNameInDocument() |
| 122 |
); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
bool ViewProviderPart::doubleClicked() |
| 127 |
{ |
| 128 |
toggleActivePart(); |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
QIcon ViewProviderPart::getIcon() const |
| 133 |
{ |
| 134 |
// the original Part object for this ViewProviderPart |
| 135 |
auto part = this->getObject<App::Part>(); |
| 136 |
// the normal case for Std_Part |
| 137 |
const char* pixmap = sPixmap; |
| 138 |
// if it's flagged as an Assembly in its Type, it gets another icon |
| 139 |
if (part->Type.getStrValue() == "Assembly") { |
| 140 |
pixmap = aPixmap; |
| 141 |
} |
| 142 |
|
| 143 |
return mergeGreyableOverlayIcons(Gui::BitmapFactory().pixmap(pixmap)); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
// Python feature ----------------------------------------------------------------------- |
| 148 |
|
| 149 |
namespace Gui |
| 150 |
{ |
| 151 |
/// @cond DOXERR |
| 152 |
PROPERTY_SOURCE_TEMPLATE(Gui::ViewProviderPartPython, Gui::ViewProviderPart) |
| 153 |
/// @endcond |
| 154 |
|
| 155 |
// explicit template instantiation |
| 156 |
template class GuiExport ViewProviderFeaturePythonT<ViewProviderPart>; |
| 157 |
} // namespace Gui |