FreeCAD

Форк
0
/
SceneInspector.cpp 
230 строк · 7.6 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2008 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
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
# include <Inventor/nodes/SoSeparator.h>
26
# include <QHeaderView>
27
# include <QTextStream>
28
#endif
29

30
#include "SceneInspector.h"
31
#include "ui_SceneInspector.h"
32
#include "Application.h"
33
#include "Document.h"
34
#include "View3DInventor.h"
35
#include "View3DInventorViewer.h"
36
#include "ViewProviderDocumentObject.h"
37

38

39
using namespace Gui::Dialog;
40

41
/* TRANSLATOR Gui::Dialog::SceneModel */
42

43
SceneModel::SceneModel(QObject* parent)
44
    : QStandardItemModel(parent)
45
{
46
}
47

48
SceneModel::~SceneModel() = default;
49

50
int SceneModel::columnCount (const QModelIndex & parent) const
51
{
52
    Q_UNUSED(parent);
53
    return 2;
54
}
55

56
Qt::ItemFlags SceneModel::flags (const QModelIndex & index) const
57
{
58
    return QAbstractItemModel::flags(index);
59
}
60

61
QVariant SceneModel::headerData (int section, Qt::Orientation orientation, int role) const
62
{
63
    if (orientation == Qt::Horizontal) {
64
        if (role != Qt::DisplayRole)
65
            return {};
66
        if (section == 0)
67
            return tr("Inventor Tree");
68
        else if (section == 1)
69
            return tr("Name");
70
    }
71

72
    return {};
73
}
74

75
bool SceneModel::setHeaderData (int, Qt::Orientation, const QVariant &, int)
76
{
77
    return false;
78
}
79

80
void SceneModel::setNode(SoNode* node)
81
{
82
    this->clear();
83
    this->setHeaderData(0, Qt::Horizontal, tr("Nodes"), Qt::DisplayRole);
84

85
    this->insertColumns(0,2);
86
    this->insertRows(0,1);
87
    setNode(this->index(0, 0), node);
88
}
89

90
void SceneModel::setNode(QModelIndex index, SoNode* node)
91
{
92
    this->setData(index, QVariant(QString::fromLatin1(QByteArray(node->getTypeId().getName()))));
93
    if (node->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) {
94
        auto group = static_cast<SoGroup*>(node);
95
        // insert SoGroup icon
96
        this->insertColumns(0,2,index);
97
        this->insertRows(0,group->getNumChildren(), index);
98
        for (int i=0; i<group->getNumChildren();i++) {
99
            SoNode* child = group->getChild(i);
100
            setNode(this->index(i, 0, index), child);
101

102
            QHash<SoNode*, QString>::iterator it = nodeNames.find(child);
103
            QString name;
104
            QTextStream stream(&name);
105
            stream << child << ", ";
106
            if(child->isOfType(SoSwitch::getClassTypeId())) {
107
                auto pcSwitch = static_cast<SoSwitch*>(child);
108
                stream << pcSwitch->whichChild.getValue() << ", ";
109
            } else if (child->isOfType(SoSeparator::getClassTypeId())) {
110
                auto pcSeparator = static_cast<SoSeparator*>(child);
111
                stream << pcSeparator->renderCaching.getValue() << ", ";
112
            }
113
            if (it != nodeNames.end())
114
                stream << it.value();
115
            else
116
                stream << child->getName();
117
            this->setData(this->index(i, 1, index), QVariant(name));
118
        }
119
    }
120
    // insert icon
121
}
122

123
void SceneModel::setNodeNames(const QHash<SoNode*, QString>& names)
124
{
125
    nodeNames = names;
126
}
127

128
// --------------------------------------------------------
129

130
/* TRANSLATOR Gui::Dialog::DlgInspector */
131

132
DlgInspector::DlgInspector(QWidget* parent, Qt::WindowFlags fl)
133
  : QDialog(parent, fl), ui(new Ui_SceneInspector())
134
{
135
    ui->setupUi(this);
136
    connect(ui->refreshButton, &QPushButton::clicked,
137
            this, &DlgInspector::onRefreshButtonClicked);
138
    setWindowTitle(tr("Scene Inspector"));
139

140
    auto model = new SceneModel(this);
141
    ui->treeView->setModel(model);
142
    ui->treeView->setRootIsDecorated(true);
143
}
144

145
/*
146
 *  Destroys the object and frees any allocated resources
147
 */
148
DlgInspector::~DlgInspector()
149
{
150
    // no need to delete child widgets, Qt does it all for us
151
    delete ui;
152
}
153

154
void DlgInspector::setDocument(Gui::Document* doc)
155
{
156
    setNodeNames(doc);
157

158
    auto view = qobject_cast<View3DInventor*>(doc->getActiveView());
159
    if (view) {
160
        View3DInventorViewer* viewer = view->getViewer();
161
        setNode(viewer->getSceneGraph());
162
        ui->treeView->expandToDepth(3);
163
    }
164
}
165

166
void DlgInspector::setNode(SoNode* node)
167
{
168
    auto model = static_cast<SceneModel*>(ui->treeView->model());
169
    model->setNode(node);
170

171
    QHeaderView* header = ui->treeView->header();
172
    header->setSectionResizeMode(0, QHeaderView::Stretch);
173
    header->setSectionsMovable(false);
174
}
175

176
void DlgInspector::setNodeNames(Gui::Document* doc)
177
{
178
    std::vector<Gui::ViewProvider*> vps = doc->getViewProvidersOfType
179
            (Gui::ViewProviderDocumentObject::getClassTypeId());
180
    QHash<SoNode*, QString> nodeNames;
181
    for (const auto & it : vps) {
182
        auto vp = static_cast<Gui::ViewProviderDocumentObject*>(it);
183
        App::DocumentObject* obj = vp->getObject();
184
        if (obj) {
185
            QString label = QString::fromUtf8(obj->Label.getValue());
186
            nodeNames[vp->getRoot()] = label;
187
        }
188

189
        std::vector<std::string> modes = vp->getDisplayMaskModes();
190
        for (const auto & mode : modes) {
191
            SoNode* node = vp->getDisplayMaskMode(mode.c_str());
192
            if (node) {
193
                nodeNames[node] = QString::fromStdString(mode);
194
            }
195
        }
196
    }
197

198
    auto model = static_cast<SceneModel*>(ui->treeView->model());
199
    model->setNodeNames(nodeNames);
200
}
201

202
void DlgInspector::changeEvent(QEvent *e)
203
{
204
    if (e->type() == QEvent::LanguageChange) {
205
        ui->retranslateUi(this);
206
        setWindowTitle(tr("Scene Inspector"));
207
    }
208
    QDialog::changeEvent(e);
209
}
210

211
void DlgInspector::onRefreshButtonClicked()
212
{
213
    Gui::Document* doc = Application::Instance->activeDocument();
214
    if (doc) {
215
        setNodeNames(doc);
216

217
        auto view = qobject_cast<View3DInventor*>(doc->getActiveView());
218
        if (view) {
219
            View3DInventorViewer* viewer = view->getViewer();
220
            setNode(viewer->getSceneGraph());
221
            ui->treeView->expandToDepth(3);
222
        }
223
    }
224
    else {
225
        auto model = static_cast<SceneModel*>(ui->treeView->model());
226
        model->clear();
227
    }
228
}
229

230
#include "moc_SceneInspector.cpp"
231

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

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

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

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