2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

datacontainertreewidget.cpp 13.7 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
schultezub's avatar
schultezub committed
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
schultezub's avatar
schultezub committed
11
12
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
schultezub's avatar
schultezub committed
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
schultezub's avatar
schultezub committed
22
23
24
// 
// ================================================================================================

25
26
27
28
29
30


#include "datacontainertreewidget.h"

#include "tgt/assert.h"

31
#include "application/gui/qtdatahandle.h"
32
33
#include "core/datastructures/datacontainer.h"
#include "core/datastructures/abstractdata.h"
34
#include "core/datastructures/lightsourcedata.h"
35
36
#include "core/datastructures/facegeometry.h"
#include "core/datastructures/meshgeometry.h"
37
38
#include "core/datastructures/indexedmeshgeometry.h"
#include "core/datastructures/multiindexedgeometry.h"
39
#include "core/datastructures/imageseries.h"
40
41
42
#include "core/datastructures/imagerepresentationdisk.h"
#include "core/datastructures/imagerepresentationlocal.h"
#include "core/datastructures/imagerepresentationgl.h"
43
#include "core/datastructures/renderdata.h"
44

45
46
#include "core/tools/stringutils.h"

47
48
49
50
#ifdef CAMPVIS_HAS_MODULE_COLUMBIA
#include "modules/columbia/datastructures/fiberdata.h"
#endif

51
52
53
#include <QHeaderView>
#include <QStringList>

schultezub's avatar
schultezub committed
54
namespace campvis {
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

    namespace {
        const int COLUMN_NAME = 0;
        const int COLUMN_TYPE = 1;
    }

// = TreeModel items ==============================================================================


    DataContainerTreeRootItem::DataContainerTreeRootItem(TreeItem* parent /*= 0*/)
        : TreeItem(parent)
    {}

    QVariant DataContainerTreeRootItem::getData(int column, int role) const {
        if (role == Qt::DisplayRole) {
            if (column == COLUMN_NAME)
                return QVariant(QString("Name"));
            else if (column == COLUMN_TYPE)
                return QVariant(QString("Data Type"));
        }

        return QVariant();
    }

    DataContainerTreeRootItem::~DataContainerTreeRootItem() {

    }

// ================================================================================================

85
    DataHandleTreeItem::DataHandleTreeItem(QtDataHandle dataHandle, const std::string& name, TreeItem* parent)
86
87
88
89
        : TreeItem(parent)
        , _dataHandle(dataHandle)
        , _name(name)
    {
90
        tgtAssert(_dataHandle.getData() != 0, "WTF - QtDataHandle with empty data?");
91
        updateChildren();
92
93
94
95
96
97
98
99
    }

    QVariant DataHandleTreeItem::getData(int column, int role) const {
        switch (role) {
        case Qt::DisplayRole:
            if (column == COLUMN_NAME)
                return QVariant(QString::fromStdString(_name));
            else if (column == COLUMN_TYPE) {
100
                const AbstractData* data = _dataHandle.getData();
101
                tgtAssert(data != 0, "WTF - QtDataHandle with empty data?");
102

103
104
                if (const ImageData* tester = dynamic_cast<const ImageData*>(data)) {
                	return QVariant(QString("Image Data"));
105
                }
106
107
108
109
110
111
112
113
114
115
116
117
118

#ifdef CAMPVIS_HAS_MODULE_COLUMBIA
                else if (const FiberData* tester = dynamic_cast<const FiberData*>(data)) {
                    return QVariant(QString("Fiber Geometry"));
                }
#endif

                else if (const IndexedMeshGeometry* tester = dynamic_cast<const IndexedMeshGeometry*>(data)) {
                    return QVariant(QString("Indexed Geometry"));
                }
                else if (const MultiIndexedGeometry* tester = dynamic_cast<const MultiIndexedGeometry*>(data)) {
                	return QVariant(QString("Multi Indexed Geometry"));
                }
119
120
                else if (const FaceGeometry* tester = dynamic_cast<const FaceGeometry*>(data)) {
                	return QVariant(QString("Face Geometry"));
121
                }
122
123
                else if (const MeshGeometry* tester = dynamic_cast<const MeshGeometry*>(data)) {
                    return QVariant(QString("Mesh Geometry"));
124
                }
125
126
127
128
                else if (const GeometryData* tester = dynamic_cast<const GeometryData*>(data)) {
                    return QVariant(QString("Geometry"));
                }

129
130
131
                else if (const RenderData* tester = dynamic_cast<const RenderData*>(data)) {
                    return QVariant(QString("Render Data"));
                }
132
133
134
135

                else if (const ImageSeries* tester = dynamic_cast<const ImageSeries*>(data)) {
                    return QVariant(QString("Image Series"));
                }
136
137
138
139

                else if (const LightSourceData* tester = dynamic_cast<const LightSourceData*>(data)) {
                    return QVariant(QString("Light Source"));
                }
140
141
142
143
            }
            else
                return QVariant();
        case Qt::UserRole:
144
            return qVariantFromValue(_dataHandle);
145
146
147
148
149
150
151
152
        default:
            return QVariant();
        }
    }

    DataHandleTreeItem::~DataHandleTreeItem() {
    }

153
    void DataHandleTreeItem::setDataHandle(QtDataHandle dataHandle) {
154
        _dataHandle = dataHandle;
155
156
157
158
159
160
161
162
163
164
        updateChildren();
    }

    void DataHandleTreeItem::updateChildren() {
        qDeleteAll(_children);
        _children.clear();

        if (const AbstractData* data = _dataHandle.getData()) {
            if (const RenderData* tester = dynamic_cast<const RenderData*>(data)) {
                for (size_t i = 0; i < tester->getNumColorTextures(); ++i) {
165
                    new DataHandleTreeItem(QtDataHandle(tester->getColorDataHandle(i)), _name + "::ColorTexture" + StringUtils::toString(i), this);
166
167
                }
                if (tester->hasDepthTexture()) {
168
                    new DataHandleTreeItem(QtDataHandle(tester->getDepthDataHandle()), _name + "::DepthTexture", this);
169
170
171
172
                }
            }
            else if (const ImageSeries* tester = dynamic_cast<const ImageSeries*>(data)) {
                for (size_t i = 0; i < tester->getNumImages(); ++i) {
173
                    new DataHandleTreeItem(QtDataHandle(tester->getImage(i)), _name + "::Image" + StringUtils::toString(i), this);
174
175
176
177
                }
            }

        }
178
179
    }

180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// = DataContainerTreeModel ============================================================================

    DataContainerTreeModel::DataContainerTreeModel(QObject *parent /*= 0*/)
        : QAbstractItemModel(parent)
        , _rootItem(new DataContainerTreeRootItem(0))
    {
    }

    DataContainerTreeModel::~DataContainerTreeModel() {
        delete _rootItem;
    }

    QVariant DataContainerTreeModel::data(const QModelIndex &index, int role) const {
        if (!index.isValid())
            return QVariant();

        TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
        return item->getData(index.column(), role);
    }

    bool DataContainerTreeModel::setData(const QModelIndex& index, const QVariant& value, int role) {
        if (!index.isValid())
            return false;

        TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
        return item->setData(index.column(), role, value);
    }

    Qt::ItemFlags DataContainerTreeModel::flags(const QModelIndex &index) const {
        if (!index.isValid())
            return 0;

        switch (index.column()) {
            case COLUMN_TYPE:
                return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
            case COLUMN_NAME:
                return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
        }

        return 0;
    }

    QVariant DataContainerTreeModel::headerData(int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/) const {
        if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
            return _rootItem->getData(section, role);

        return QVariant();
    }

    QModelIndex DataContainerTreeModel::index(int row, int column, const QModelIndex &parent /*= QModelIndex()*/) const {
        if (!hasIndex(row, column, parent))
            return QModelIndex();

        TreeItem* parentItem;

        if (!parent.isValid())
            parentItem = _rootItem;
        else
            parentItem = static_cast<TreeItem*>(parent.internalPointer());

        TreeItem* childItem = parentItem->getChild(row);
        if (childItem)
            return createIndex(row, column, childItem);
        else
            return QModelIndex();
    }

    QModelIndex DataContainerTreeModel::parent(const QModelIndex &index) const {
        if (!index.isValid())
            return QModelIndex();

        TreeItem* childItem = static_cast<TreeItem*>(index.internalPointer());
        TreeItem* parentItem = childItem->getParent();

        if (parentItem == _rootItem)
            return QModelIndex();

        return createIndex(parentItem->getRow(), 0, parentItem);
    }

    int DataContainerTreeModel::rowCount(const QModelIndex &parent /*= QModelIndex()*/) const {
        TreeItem* parentItem;
        if (parent.column() > 0)
            return 0;

        if (!parent.isValid())
            parentItem = _rootItem;
        else
            parentItem = static_cast<TreeItem*>(parent.internalPointer());

        return parentItem->getChildCount();
    }

    int DataContainerTreeModel::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const {
        return 2;
    }

    void DataContainerTreeModel::setData(const DataContainer* dataContainer) {
278
279
        beginResetModel();

280
        _itemMap.clear();
281
282
283
        delete _rootItem;
        _rootItem = new DataContainerTreeRootItem();

284
285
        if (dataContainer != 0) {
            std::vector< std::pair< std::string, DataHandle> > handlesCopy = dataContainer->getDataHandlesCopy();
286
287
            std::sort(handlesCopy.begin(), handlesCopy.end(), [&] (const std::pair<std::string, DataHandle>& a, const std::pair<std::string, DataHandle>& b) -> bool { return a.first < b.first; });

288
289
290
291
            for (std::vector< std::pair< std::string, DataHandle> >::iterator it = handlesCopy.begin(); it != handlesCopy.end(); ++it) {
                DataHandleTreeItem* dhti = new DataHandleTreeItem(QtDataHandle(it->second), it->first, _rootItem);
                _itemMap.insert(std::make_pair(QString::fromStdString(it->first), dhti));
            }
292
        }
293
294

        endResetModel();
295
296
    }

297
298
    void DataContainerTreeModel::onDataContainerChanged(const QString& key, QtDataHandle dh) {
        tgtAssert(dh.getData() != 0, "WTF - QtDataHandle with empty data?");
299

300
        std::map<QString, DataHandleTreeItem*>::iterator it = _itemMap.lower_bound(key);
301
        if (it == _itemMap.end() || it->first != key) {
302
            // there is no tree item with this key yet => create a new one
303
            DataHandleTreeItem* dhti = new DataHandleTreeItem(dh, key.toStdString(), _rootItem);
304
            _itemMap.insert(std::make_pair(key, dhti));
305
306
307

            // notify views, that there is new data
            emit dataChanged(createIndex(dhti->getRow(), 0, dhti), createIndex(dhti->getRow() + dhti->getChildCount(), columnCount(), dhti));
308
309
        }
        else {
310
311
312
313
314
315
            // there is alredy a tree item with this key => update the item and its children
            QModelIndex keyIndex = createIndex(it->second->getRow(), 0, it->second);

            // by replacing the DataHandle in the tree item, we also replace all of the TreeItem's children
            // this is effectively a remove followed by an insert
            beginRemoveRows(keyIndex, 0, it->second->getChildCount());
316
            it->second->setDataHandle(dh);
317
318
319
320
321
322
323
            endRemoveRows();

            beginInsertRows(keyIndex, 0, it->second->getChildCount());
            endInsertRows();

            // notify views, that there is new data
            emit dataChanged(keyIndex, createIndex(it->second->getRow(), columnCount(), it->second));
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
        }
    }

// = DataContainerTreeWidget ===========================================================================

    DataContainerTreeWidget::DataContainerTreeWidget(QWidget* parent /*= 0*/)
        : QTreeView(parent)
    {
        setupWidget();
    }

    DataContainerTreeWidget::~DataContainerTreeWidget() {

    }

    void DataContainerTreeWidget::update(const DataContainer* dataContainer) {
schultezub's avatar
schultezub committed
340
341
342
343
        // clear selection before setting the new data or we will encounter random crashes...
        selectionModel()->clear();

        // set new data
344
        _treeModel->setData(dataContainer);
schultezub's avatar
schultezub committed
345
346

        // adjust view
347
348
349
350
351
352
353
354
355
        resizeColumnToContents(0);
        resizeColumnToContents(1);
    }

    void DataContainerTreeWidget::setupWidget() {
        _treeModel = new DataContainerTreeModel(this);
        tgtAssert(_treeModel != 0, "Failed creating TreeViewWidget model.");

        setModel(_treeModel);
356
357
358
359
360
361
    }

    DataContainerTreeModel* DataContainerTreeWidget::getTreeModel() {
        return _treeModel;
    }

362
363

}