pipelinetreewidget.cpp 13.7 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2013, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
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
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.
22
23
24
// 
// ================================================================================================

25
26
27
28
#include "pipelinetreewidget.h"

#include "tgt/assert.h"
#include <QHeaderView>
29
#include <QScrollBar>
30
31
#include <QStringList>

schultezub's avatar
schultezub committed
32
namespace campvis {
33

34
35
36
    namespace {
        const int COLUMN_NAME = 0;
        const int COLUMN_ENABLED_STATE = 1;
37
38
        const int COLUMN_CLOCK_STATE = 2;
        const int COLUMN_DESCRIPTION = 3;
39
40
    }

41
42
// = TreeModel items ==============================================================================

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    DataContainerTreeItem::DataContainerTreeItem(DataContainer* dc, TreeItem* parent)
        : TreeItem(parent)
        , _dataContainer(dc)
    {
        tgtAssert(_dataContainer != 0, "Pipeline must not be 0.");
    }

    QVariant DataContainerTreeItem::getData(int column, int role) const {
        switch (role) {
        case Qt::DisplayRole:
            if (column == COLUMN_NAME)
                return QVariant(QString::fromStdString(_dataContainer->getName()));
            else
                return QVariant();
        case Qt::CheckStateRole:
            return QVariant();
        case Qt::UserRole:
            return QVariant();
        default:
            return QVariant();
        }
    }

    DataContainerTreeItem::~DataContainerTreeItem() {
    }

    bool DataContainerTreeItem::setData(int column, int role, const QVariant& value) const {
        return false;
    }

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

75
76
77
78
79
80
81
    PipelineTreeItem::PipelineTreeItem(AbstractPipeline* pipeline, TreeItem* parent)
        : TreeItem(parent)
        , _pipeline(pipeline)
    {
        tgtAssert(_pipeline != 0, "Pipeline must not be 0.");
    }

82
83
84
    QVariant PipelineTreeItem::getData(int column, int role) const {
        switch (role) {
            case Qt::DisplayRole:
85
                if (column == COLUMN_NAME)
86
                    return QVariant(QString::fromStdString(_pipeline->getName()));
87
88
89
90
91
92
93
                else
                    return QVariant();
            case Qt::CheckStateRole:
                if (column == COLUMN_ENABLED_STATE)
                    return _pipeline->getEnabled() ? QVariant(Qt::Checked) : QVariant(Qt::Unchecked);
                else
                    return QVariant();
94
95
96
97
98
            case Qt::UserRole:
                return qVariantFromValue(static_cast<void*>(_pipeline));
            default:
                return QVariant();
        }
99
100
101
102
103
    }

    PipelineTreeItem::~PipelineTreeItem() {
    }

104
105
106
107
108
109
110
111
112
113
    bool PipelineTreeItem::setData(int column, int role, const QVariant& value) const {
        if (column == COLUMN_ENABLED_STATE) {
            if (role == Qt::CheckStateRole) {
                _pipeline->setEnabled(value == Qt::Checked ? true : false);
                return true;
            }
        }
        return false;
    }

114
// ================================================================================================
115
116
117
118
119
120
121
122

    ProcessorTreeItem::ProcessorTreeItem(AbstractProcessor* processor, TreeItem* parent)
        : TreeItem(parent)
        , _processor(processor)
    {
        tgtAssert(_processor != 0, "Processor must not be 0.");
    }

123
124
125
    QVariant ProcessorTreeItem::getData(int column, int role) const {
        switch (role) {
            case Qt::DisplayRole:
126
                if (column == COLUMN_NAME)
127
                    return QVariant(QString::fromStdString(_processor->getName()));
128
                else if (column == COLUMN_DESCRIPTION)
129
                    return QVariant(QString::fromStdString(_processor->getDescription()));
130
131
132
133
134
                else
                    return QVariant();
            case Qt::CheckStateRole:
                if (column == COLUMN_ENABLED_STATE)
                    return _processor->getEnabled() ? QVariant(Qt::Checked) : QVariant(Qt::Unchecked);
135
136
                else if (column == COLUMN_CLOCK_STATE)
                    return _processor->getClockExecutionTime() ? QVariant(Qt::Checked) : QVariant(Qt::Unchecked);
137
138
                else
                    return QVariant();
139
140
141
142
143
            case Qt::UserRole:
                return qVariantFromValue(static_cast<void*>(_processor));
            default:
                return QVariant();
        }
144
145
146
147
148
    }

    ProcessorTreeItem::~ProcessorTreeItem() {
    }

149
150
151
152
153
154
155
    bool ProcessorTreeItem::setData(int column, int role, const QVariant& value) const {
        if (column == COLUMN_ENABLED_STATE) {
            if (role == Qt::CheckStateRole) {
                _processor->setEnabled(value == Qt::Checked ? true : false);
                return true;
            }
        }
156
157
158
159
160
161
        else if (column == COLUMN_CLOCK_STATE) {
            if (role == Qt::CheckStateRole) {
                _processor->setClockExecutionTime(value == Qt::Checked ? true : false);
                return true;
            }
        }
162
163
164
        return false;
    }

165
    PipelineTreeRootItem::PipelineTreeRootItem(TreeItem* parent /*= 0*/)
166
167
168
        : TreeItem(parent)
    {}

169
    QVariant PipelineTreeRootItem::getData(int column, int role) const {
170
        if (role == Qt::DisplayRole) {
171
            if (column == COLUMN_NAME)
172
                return QVariant(QString("Pipeline/Processor"));
173
            else if (column == COLUMN_DESCRIPTION)
174
                return QVariant(QString("Description"));
175
176
            else if (column == COLUMN_ENABLED_STATE)
                return QVariant(QString("Enabled"));
177
178
            else if (column == COLUMN_CLOCK_STATE)
                return QVariant(QString("Clock"));
179
        }
180
181
182
183

        return QVariant();
    }

184
    PipelineTreeRootItem::~PipelineTreeRootItem() {
185
186
187
188
189
190
191

    }

// = PipelineTreeModel ============================================================================

    PipelineTreeModel::PipelineTreeModel(QObject *parent /*= 0*/)
        : QAbstractItemModel(parent)
192
        , _rootItem(new PipelineTreeRootItem(0))
193
194
195
196
197
198
199
200
201
202
203
204
    {
    }

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

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

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

208
209
210
211
212
213
214
215
    bool PipelineTreeModel::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);
    }

216
217
218
219
    Qt::ItemFlags PipelineTreeModel::flags(const QModelIndex &index) const {
        if (!index.isValid())
            return 0;

220
221
222
223
224
225
226
        switch (index.column()) {
            case COLUMN_DESCRIPTION:
                return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
            case COLUMN_NAME:
                return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
            case COLUMN_ENABLED_STATE:
                return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
227
228
            case COLUMN_CLOCK_STATE:
                return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
229
230
231
        }

        return 0;
232
233
234
235
    }

    QVariant PipelineTreeModel::headerData(int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/) const {
        if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
236
            return _rootItem->getData(section, role);
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
278
279
280
281
282
283
284
285

        return QVariant();
    }

    QModelIndex PipelineTreeModel::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 PipelineTreeModel::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 PipelineTreeModel::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 PipelineTreeModel::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const {
286
        return 4;
287
288
    }

289
    void PipelineTreeModel::setData(const std::vector<DataContainer*>& dataContainers, const std::vector<AbstractPipeline*>& pipelines) {
290
        delete _rootItem;
291
        _rootItem = new PipelineTreeRootItem();
292

293
294
295
296
297
298
        std::map<DataContainer*, DataContainerTreeItem*> dcItemMap;
        for (size_t i = 0; i < dataContainers.size(); ++i) {
            DataContainerTreeItem* dcti = new DataContainerTreeItem(dataContainers[i], _rootItem);
            dcItemMap[dataContainers[i]] = dcti;
        }

299
        for (std::vector<AbstractPipeline*>::const_iterator pipe = pipelines.begin(); pipe != pipelines.end(); ++pipe) {
300
301
302
303
            std::map<DataContainer*, DataContainerTreeItem*>::iterator it = dcItemMap.find(&(*pipe)->getDataContainer());

            if (it != dcItemMap.end()) {
                PipelineTreeItem* pipeti = new PipelineTreeItem(*pipe, it->second);
304

305
                for (std::vector<AbstractProcessor*>::const_iterator proc = (*pipe)->getProcessors().begin(); proc != (*pipe)->getProcessors().end(); ++proc) {
306
                    new ProcessorTreeItem(*proc, pipeti);
307
308
309
310
                }
            }
            else {
                tgtAssert(false, "The DataContainer of this pipeline is not in the list of DataContainers, cannot add it to GUI!");
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
            }
        }
    }

// = PipelineTreeWidget ===========================================================================

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

    PipelineTreeWidget::~PipelineTreeWidget() {

    }

327
328
329
330
331
332
333
334
335
    QSize PipelineTreeWidget::sizeHint() const {
        int height = 0;

        // First of all, sum up the heights of top-level items and their children
        for (int i = 0; i < model()->rowCount(); i++) {
            QModelIndex index = model()->index(i, 0);
            height += sizeHintForRow(i);

            if (model()->hasChildren(index)) {
336
337
338
339
340
341
342
343
                for (int j = 0; j < model()->rowCount(index); ++j) {
                    QModelIndex cIndex = index.child(j, 0);
                    height += sizeHintForIndex(cIndex).height();

                    if (model()->hasChildren(cIndex)) {
                        height += model()->rowCount(cIndex) * sizeHintForIndex(cIndex.child(0, 0)).height();
                    }
                }                
344
345
346
347
348
            }
        }

        // Next, add the heights of the horizontal scrollbar, header, and frame
        height += horizontalScrollBar()->sizeHint().height();
349
        height += 2 * header()->sizeHint().height();
350
351
352
353
354
        height += 2 * frameWidth();

        return QSize(QTreeView::sizeHint().width(), height);
    }

355
    void PipelineTreeWidget::update(const std::vector<DataContainer*>& dataContainers, const std::vector<AbstractPipeline*>& pipelines) {
schultezub's avatar
schultezub committed
356
357
358
        // clear selection before setting the new data or we will encounter random crashes...
        selectionModel()->clear();

359
        _treeModel->setData(dataContainers, pipelines);
360
        expandAll();
361
362
        resizeColumnToContents(0);
        resizeColumnToContents(1);
363
364
365

        // The widget's size hint might have changed, notify the layout
        updateGeometry();
366
367
368
369
370
371
372
    }

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

        setModel(_treeModel);
373
    }
374

375
}