1
/***************************************************************************
2
* Copyright (c) 2016 Stefan Tröger <stefantroeger@gmx.net> *
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 <QMessageBox>
29
#include <App/Document.h>
30
#include <App/DocumentObject.h>
31
#include <App/GroupExtension.h>
32
#include <Base/Console.h>
33
#include <Base/Tools.h>
35
#include "ViewProviderGroupExtension.h"
36
#include "ViewProviderDocumentObject.h"
39
#include "MainWindow.h"
44
EXTENSION_PROPERTY_SOURCE(Gui::ViewProviderGroupExtension, Gui::ViewProviderExtension)
46
ViewProviderGroupExtension::ViewProviderGroupExtension()
48
initExtensionType(ViewProviderGroupExtension::getExtensionClassTypeId());
51
ViewProviderGroupExtension::~ViewProviderGroupExtension() = default;
53
bool ViewProviderGroupExtension::extensionCanDragObjects() const {
57
bool ViewProviderGroupExtension::extensionCanDragObject(App::DocumentObject*) const {
59
//we can drag anything out
63
void ViewProviderGroupExtension::extensionDragObject(App::DocumentObject* obj) {
65
Gui::Command::doCommand(Gui::Command::Doc,"App.getDocument(\"%s\").getObject(\"%s\").removeObject("
66
"App.getDocument(\"%s\").getObject(\"%s\"))",
67
getExtendedViewProvider()->getObject()->getDocument()->getName(), getExtendedViewProvider()->getObject()->getNameInDocument(),
68
obj->getDocument()->getName(), obj->getNameInDocument() );
71
bool ViewProviderGroupExtension::extensionCanDropObjects() const {
75
bool ViewProviderGroupExtension::extensionCanDropObject(App::DocumentObject* obj) const {
78
Base::Console().Log("Check ViewProviderGroupExtension");
81
auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();
83
//we cannot drop thing of this group into it again if it does not allow reorder
84
if (group->hasObject(obj) && !getExtendedViewProvider()->acceptReorderingObjects())
87
if (group->allowObject(obj))
93
void ViewProviderGroupExtension::extensionDropObject(App::DocumentObject* obj) {
95
auto grp = static_cast<App::DocumentObject*>(getExtendedViewProvider()->getObject());
96
App::Document* doc = grp->getDocument();
98
// build Python command for execution
100
cmd = QString::fromLatin1("App.getDocument(\"%1\").getObject(\"%2\").addObject("
101
"App.getDocument(\"%1\").getObject(\"%3\"))")
102
.arg(QString::fromLatin1(doc->getName()),
103
QString::fromLatin1(grp->getNameInDocument()),
104
QString::fromLatin1(obj->getNameInDocument()));
106
Gui::Command::doCommand(Gui::Command::App, cmd.toUtf8());
109
std::vector< App::DocumentObject* > ViewProviderGroupExtension::extensionClaimChildren() const {
111
auto* obj = getExtendedViewProvider()->getObject();
116
auto* group = obj->getExtensionByType<App::GroupExtension>();
117
return group->Group.getValues();
120
void ViewProviderGroupExtension::extensionShow() {
122
// avoid possible infinite recursion
125
Base::StateLocker lock(guard);
127
// when reading the Visibility property from file then do not hide the
128
// objects of this group because they have stored their visibility status, too
130
// Property::User1 is used by ViewProviderDocumentObject to mark for
131
// temporary visibility changes. Do not propagate the change to children.
132
if (!getExtendedViewProvider()->isRestoring()
133
&& !getExtendedViewProvider()->Visibility.testStatus(App::Property::User1)) {
134
auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();
135
for(auto obj : group->Group.getValues()) {
136
if(obj && !obj->Visibility.getValue())
137
obj->Visibility.setValue(true);
141
ViewProviderExtension::extensionShow();
144
void ViewProviderGroupExtension::extensionHide() {
146
// avoid possible infinite recursion
149
Base::StateLocker lock(guard);
151
// when reading the Visibility property from file then do not hide the
152
// objects of this group because they have stored their visibility status, too
154
// Property::User1 is used by ViewProviderDocumentObject to mark for
155
// temporary visibility changes. Do not propagate the change to children.
156
if (!getExtendedViewProvider()->isRestoring()
157
&& !getExtendedViewProvider()->Visibility.testStatus(App::Property::User1))
159
auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();
160
for(auto obj : group->Group.getValues()) {
161
if(obj && obj->Visibility.getValue())
162
obj->Visibility.setValue(false);
165
ViewProviderExtension::extensionHide();
168
bool ViewProviderGroupExtension::extensionOnDelete(const std::vector< std::string >& ) {
170
auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();
171
// If the group is nonempty ask the user if they want to delete its content
172
if (group->Group.getSize() > 0) {
173
QMessageBox::StandardButton choice =
174
QMessageBox::question(getMainWindow(), QObject::tr ( "Delete group content?" ),
175
QObject::tr ( "The %1 is not empty, delete its content as well?")
176
.arg ( QString::fromUtf8 ( getExtendedViewProvider()->getObject()->Label.getValue () ) ),
177
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes );
179
if (choice == QMessageBox::Yes) {
180
Gui::Command::doCommand(Gui::Command::Doc,
181
"App.getDocument(\"%s\").getObject(\"%s\").removeObjectsFromDocument()"
182
, getExtendedViewProvider()->getObject()->getDocument()->getName()
183
, getExtendedViewProvider()->getObject()->getNameInDocument());
191
EXTENSION_PROPERTY_SOURCE_TEMPLATE(Gui::ViewProviderGroupExtensionPython, Gui::ViewProviderGroupExtension)
193
// explicit template instantiation
194
template class GuiExport ViewProviderExtensionPythonT<ViewProviderGroupExtension>;