| 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 <App/DocumentObjectGroup.h> |
| 25 |
#include <App/Document.h> |
| 26 |
#include <Gui/Selection/Selection.h> |
| 27 |
#include <Gui/BitmapFactory.h> |
| 28 |
#include <QMenu> |
| 29 |
#include <QAction> |
| 30 |
#include <QObject> |
| 31 |
|
| 32 |
#include "ViewProviderDocumentObjectGroup.h" |
| 33 |
#include "Application.h" |
| 34 |
#include "BitmapFactory.h" |
| 35 |
#include "Document.h" |
| 36 |
|
| 37 |
|
| 38 |
using namespace Gui; |
| 39 |
|
| 40 |
|
| 41 |
PROPERTY_SOURCE_WITH_EXTENSIONS(Gui::ViewProviderDocumentObjectGroup, Gui::ViewProviderDocumentObject) |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Creates the view provider for an object group. |
| 46 |
*/ |
| 47 |
ViewProviderDocumentObjectGroup::ViewProviderDocumentObjectGroup() |
| 48 |
{ |
| 49 |
ViewProviderGroupExtension::initExtension(this); |
| 50 |
|
| 51 |
sPixmap = "folder"; |
| 52 |
} |
| 53 |
|
| 54 |
ViewProviderDocumentObjectGroup::~ViewProviderDocumentObjectGroup() = default; |
| 55 |
|
| 56 |
std::vector<std::string> ViewProviderDocumentObjectGroup::getDisplayModes() const |
| 57 |
{ |
| 58 |
// empty |
| 59 |
return {}; |
| 60 |
} |
| 61 |
|
| 62 |
bool ViewProviderDocumentObjectGroup::isShow() const |
| 63 |
{ |
| 64 |
return Visibility.getValue(); |
| 65 |
} |
| 66 |
|
| 67 |
QIcon ViewProviderDocumentObjectGroup::getIcon() const |
| 68 |
{ |
| 69 |
return mergeGreyableOverlayIcons(Gui::BitmapFactory().iconFromTheme(sPixmap)); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Extracts the associated view providers of the objects of the associated object group group. |
| 74 |
*/ |
| 75 |
void ViewProviderDocumentObjectGroup::getViewProviders(std::vector<ViewProviderDocumentObject*>& vp) const |
| 76 |
{ |
| 77 |
App::DocumentObject* doc = getObject(); |
| 78 |
if (doc->isDerivedFrom<App::DocumentObjectGroup>()) { |
| 79 |
Gui::Document* gd = Application::Instance->getDocument(doc->getDocument()); |
| 80 |
auto grp = static_cast<App::DocumentObjectGroup*>(doc); |
| 81 |
std::vector<App::DocumentObject*> obj = grp->getObjects(); |
| 82 |
for (const auto& it : obj) { |
| 83 |
ViewProvider* v = gd->getViewProvider(it); |
| 84 |
if (v && v->isDerivedFrom<ViewProviderDocumentObject>()) { |
| 85 |
vp.push_back(static_cast<ViewProviderDocumentObject*>(v)); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
void ViewProviderDocumentObjectGroup::setupContextMenu(QMenu* menu, QObject* receiver, const char* member) |
| 92 |
{ |
| 93 |
// First, call the base class implementation to get all the standard menu items. |
| 94 |
ViewProviderDocumentObject::setupContextMenu(menu, receiver, member); |
| 95 |
|
| 96 |
App::DocumentObject* obj = this->getObject(); |
| 97 |
|
| 98 |
// This action is only for plain "Std::Group" objects, not for derived types like "Draft::Layer". |
| 99 |
// We check the TypeId to ensure we don't add this menu to other group-like objects. |
| 100 |
if (obj->getTypeId() == App::DocumentObjectGroup::getClassTypeId()) { |
| 101 |
auto* group = static_cast<App::DocumentObjectGroup*>(obj); |
| 102 |
|
| 103 |
// Only add the action if the group actually contains objects. |
| 104 |
if (group && !group->getObjects().empty()) { |
| 105 |
// Add the custom action. |
| 106 |
QIcon icon = BitmapFactory().iconFromTheme("Std_SelectGroupContents"); |
| 107 |
QAction* selectAction = new QAction(icon, QObject::tr("Select Group Contents"), menu); |
| 108 |
selectAction->setToolTip( |
| 109 |
QObject::tr("Selects all objects that are children of this group") |
| 110 |
); |
| 111 |
|
| 112 |
// Connect the action's triggered signal to a lambda function that performs the selection. |
| 113 |
QObject::connect(selectAction, &QAction::triggered, [group]() { |
| 114 |
if (!group) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
// Use getAllChildren() to recursively select contents of subgroups. |
| 119 |
const auto& children = group->getAllChildren(); |
| 120 |
|
| 121 |
if (!children.empty()) { |
| 122 |
const char* docName = group->getDocument()->getName(); |
| 123 |
|
| 124 |
Gui::Selection().clearSelection(docName); |
| 125 |
|
| 126 |
// Add each child object to the selection individually. |
| 127 |
for (App::DocumentObject* child : children) { |
| 128 |
if (child && child->isAttachedToDocument()) { |
| 129 |
Gui::Selection().addSelection(docName, child->getNameInDocument()); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
}); |
| 134 |
|
| 135 |
// Insert the action at the top of the menu for better visibility. |
| 136 |
menu->insertAction( |
| 137 |
menu->actions().isEmpty() ? nullptr : menu->actions().first(), |
| 138 |
selectAction |
| 139 |
); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
// Python feature ----------------------------------------------------------------------- |
| 145 |
|
| 146 |
namespace Gui |
| 147 |
{ |
| 148 |
/// @cond DOXERR |
| 149 |
PROPERTY_SOURCE_TEMPLATE(Gui::ViewProviderDocumentObjectGroupPython, Gui::ViewProviderDocumentObjectGroup) |
| 150 |
/// @endcond |
| 151 |
|
| 152 |
// explicit template instantiation |
| 153 |
template class GuiExport ViewProviderFeaturePythonT<ViewProviderDocumentObjectGroup>; |
| 154 |
} // namespace Gui |