FrameExplorer.cpp 5.94 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2014 UJF-Grenoble 1, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
 *
 * Visit http://camitk.imag.fr for more information
 *
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * CamiTK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $CAMITK_LICENCE_END$
 ****************************************************************************/

//-- camiTK stuff
#include "FrameExplorer.h"
#include "Component.h"
#include "Log.h"
#include "Application.h"

//-- Qt stuff
#include <QInputDialog>
#include <QMessageBox>
#include <QHeaderView>

//-- stl stuff
#include <algorithm>


namespace camitk {
//----------------------- constructor ------------------------
FrameExplorer::FrameExplorer() : Viewer("Frame Explorer") {
44
    explorerTree = NULL;
45
46
47
48
}

//----------------------- destructor ------------------------
FrameExplorer::~FrameExplorer() {
49

50
51
}

52

53
//----------------------- getInstance ------------------------
54
FrameExplorer* FrameExplorer::getInstance() {
55
    // static instanciation, static method variable
56
57
58
    static FrameExplorer *frameExplorer = NULL;
    if (!frameExplorer)
        frameExplorer = new FrameExplorer();
59

60
    return frameExplorer;
61
62
63
}

//----------------------- getWidget ------------------------
64
QWidget * FrameExplorer::getWidget(QWidget * parent) {
65
66
67
68
69
70
71
    if (explorerTree == NULL) {
        //-- create the explorer tree
        explorerTree = new QTreeWidget(parent);
        // For explorerTree to emit the customMenu.. signal
        explorerTree->setContextMenuPolicy(Qt::CustomContextMenu);
        // headers
        QStringList headerTitles;
72
        headerTitles << "Name" << "ComponentName" ;
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        explorerTree->setHeaderLabels(headerTitles);
        explorerTree->header()->setStretchLastSection(false);
        explorerTree->header()->setResizeMode(0, QHeaderView::ResizeToContents); // First column stretch to what is needed
        // Unsortable column
        explorerTree->setSortingEnabled(false);
        // Multiple selection (click + Shift or Control key)
        explorerTree->setSelectionMode(QAbstractItemView::ExtendedSelection);

        //-- connections
        connect(explorerTree, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChanged()));
    }

    return explorerTree;
}

//----------------------- getPreferenceWidget ------------------------
89
QWidget* FrameExplorer::getPreferenceWidget(QWidget * parent) {
90
91
92
93
94
    // TODO (if any)
    return NULL;
}

// ---------------- numberOfViewedComponent ----------------
95
96
unsigned int FrameExplorer::numberOfViewedComponent() {
    return 0;
97
98
99
}

//----------------------- refresh ------------------------
100
void FrameExplorer::refresh(Viewer *whoIsAsking) {
101
102
    // if it is this instance who is asking the refresh, then only the Component names need to be checked...
    if (whoIsAsking != this) {
103
        ComponentList cptList = Application::getAllComponents();
104

105
106
107
108
109
110
        //clear the explorer
        remove();

        // rebuild the explorer tree
        foreach(Component *comp, cptList) {
               add(comp);
111
112
        }

113
        explorerTree->expandAll();
114
115
116
117
    }
}

//----------------------- getNewItem ------------------------
118
QTreeWidgetItem* FrameExplorer::getNewItem(QTreeWidgetItem* parent, Component *abstractNode) {
119
120
121
    //-- create the tree widget for abstractNode
    QTreeWidgetItem *tw = new QTreeWidgetItem(parent);
    // set the first column (#0)
122
    tw->setText(0, abstractNode->getFrame()->getFrameName());
123
124

    // and the second column (#1)
125
126
127
    tw->setText(1, abstractNode->getName());

    //-- add children recursively
128
    foreach( InterfaceFrame* intFrame, (abstractNode->getFrame()->getChildrenFrame()) )
129
130
131
132
133
134
135
136
137
138
    {
        ComponentList cptlst = Application::getAllComponents();
        foreach(Component* cp, cptlst)
        {
            if( cp->getFrame()->getFrameName() == intFrame->getFrameName() ){
                getNewItem(tw, cp);
            }
        }
    }

139
140
141
142
143
    return tw;
}


//----------------------- add ------------------------
144
145
146
147
QTreeWidgetItem* FrameExplorer::add(QTreeWidgetItem* parent, Component *abstractNode) {

    //-- create the tree widget for abstractNode
    QTreeWidgetItem *tw = new QTreeWidgetItem(parent);
148

149
150
151
152
153
    // set the first column (#0)
    tw->setText(0, abstractNode->getFrame()->getFrameName());

    // and the second column (#1)
    tw->setText(1, abstractNode->getName());
154
155
156
157

    // add the explorer to the Component viewer list
    abstractNode->setVisibility(this, true);

158
    //-- add children
159
    foreach( InterfaceFrame* intFrame, abstractNode->getFrame()->getChildrenFrame() )
160
161
162
163
164
165
166
167
    {
        ComponentList cptlst = Application::getAllComponents();
        foreach(Component* cp, cptlst)
        {
            if (cp->getFrame()->getFrameName() == intFrame->getFrameName()){
                getNewItem(tw, cp);
            }
        }
168
169
170
171
172
173
    }

    return tw;
}

//----------------------- add ------------------------
174
175
176
void FrameExplorer::add(Component * comp) {
    if (!comp->getFrame()->getParentFrame())
    {
177
178
179
180
181
182
        // create the items
        QTreeWidgetItem * compItem = add(NULL, comp);
        explorerTree->addTopLevelItem(compItem);
    }
}
//----------------------- remove ------------------------
183
void FrameExplorer::remove() {
184

185
    explorerTree->clear();
186
187
188
189

}

//----------------------- selectionChanged ------------------------
190
void FrameExplorer::selectionChanged() {
191
192
193
194
195
196
    // refresh!
    refresh(this);
}

}