FreeCAD

Форк
0
/
ViewProviderGroupExtension.cpp 
195 строк · 7.7 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2016 Stefan Tröger <stefantroeger@gmx.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
#include "PreCompiled.h"
24

25
#ifndef _PreComp_
26
# include <QMessageBox>
27
#endif
28

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

35
#include "ViewProviderGroupExtension.h"
36
#include "ViewProviderDocumentObject.h"
37
#include "Command.h"
38
#include "Document.h"
39
#include "MainWindow.h"
40

41

42
using namespace Gui;
43

44
EXTENSION_PROPERTY_SOURCE(Gui::ViewProviderGroupExtension, Gui::ViewProviderExtension)
45

46
ViewProviderGroupExtension::ViewProviderGroupExtension()
47
{
48
    initExtensionType(ViewProviderGroupExtension::getExtensionClassTypeId());
49
}
50

51
ViewProviderGroupExtension::~ViewProviderGroupExtension() = default;
52

53
bool ViewProviderGroupExtension::extensionCanDragObjects() const {
54
    return true;
55
}
56

57
bool ViewProviderGroupExtension::extensionCanDragObject(App::DocumentObject*) const {
58

59
    //we can drag anything out
60
    return true;
61
}
62

63
void ViewProviderGroupExtension::extensionDragObject(App::DocumentObject* obj) {
64

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() );
69
}
70

71
bool ViewProviderGroupExtension::extensionCanDropObjects() const {
72
    return true;
73
}
74

75
bool ViewProviderGroupExtension::extensionCanDropObject(App::DocumentObject* obj) const {
76

77
#ifdef FC_DEBUG
78
    Base::Console().Log("Check ViewProviderGroupExtension");
79
#endif
80

81
    auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();
82

83
    //we cannot drop thing of this group into it again if it does not allow reorder
84
    if (group->hasObject(obj) && !getExtendedViewProvider()->acceptReorderingObjects())
85
        return false;
86

87
    if (group->allowObject(obj))
88
        return true;
89

90
    return false;
91
}
92

93
void ViewProviderGroupExtension::extensionDropObject(App::DocumentObject* obj) {
94

95
    auto grp = static_cast<App::DocumentObject*>(getExtendedViewProvider()->getObject());
96
    App::Document* doc = grp->getDocument();
97

98
    // build Python command for execution
99
    QString cmd;
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()));
105

106
    Gui::Command::doCommand(Gui::Command::App, cmd.toUtf8());
107
}
108

109
std::vector< App::DocumentObject* > ViewProviderGroupExtension::extensionClaimChildren() const {
110

111
    auto* obj = getExtendedViewProvider()->getObject();
112
    if (!obj) {
113
        return {};
114
    }
115

116
    auto* group = obj->getExtensionByType<App::GroupExtension>();
117
    return group->Group.getValues();
118
}
119

120
void ViewProviderGroupExtension::extensionShow() {
121

122
    // avoid possible infinite recursion
123
    if (guard)
124
        return;
125
    Base::StateLocker lock(guard);
126

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
129
    //
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);
138
        }
139
    }
140

141
    ViewProviderExtension::extensionShow();
142
}
143

144
void ViewProviderGroupExtension::extensionHide() {
145

146
    // avoid possible infinite recursion
147
    if (guard)
148
        return;
149
    Base::StateLocker lock(guard);
150

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
153
    //
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))
158
    {
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);
163
        }
164
    }
165
    ViewProviderExtension::extensionHide();
166
}
167

168
bool ViewProviderGroupExtension::extensionOnDelete(const std::vector< std::string >& ) {
169

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 );
178

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());
184
        }
185
    }
186
    return true;
187
}
188

189

190
namespace Gui {
191
EXTENSION_PROPERTY_SOURCE_TEMPLATE(Gui::ViewProviderGroupExtensionPython, Gui::ViewProviderGroupExtension)
192

193
// explicit template instantiation
194
template class GuiExport ViewProviderExtensionPythonT<ViewProviderGroupExtension>;
195
}
196

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

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

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

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