]> Creatis software - clitk.git/blob - vv/vvLinkPanel.cxx
Debug RTStruct conversion with empty struc
[clitk.git] / vv / vvLinkPanel.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ===========================================================================**/
18
19 #ifndef _vvLinkPanel_CXX
20 #define _vvLinkPanel_CXX
21
22 #include "vvLinkPanel.h"
23 #include "clitkCommon.h"
24
25 #include <QtGui>
26 #include <Qt>
27 #include "QTreePushButton.h"
28
29 //------------------------------------------------------------------------------
30 vvLinkPanel::vvLinkPanel(QWidget * parent):QWidget(parent)
31 {
32   setupUi(this);
33   imageNames.resize(0);
34   image1Ids.resize(0);
35   image2Ids.resize(0);
36
37   linkTableWidget->resizeColumnsToContents();
38   linkTableWidget->verticalHeader()->hide();
39   linkTableWidget->horizontalHeader()->hide();
40   linkTableWidget->hideColumn(4);
41   linkTableWidget->hideColumn(5);
42
43   connect(image1ComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(UpdateComboBox2(int)));
44   connect(linkButton,SIGNAL(clicked()),this,SLOT(addLink()));
45   connect(linkAllButton,SIGNAL(clicked()),this,SLOT(linkAll()));
46 }
47 //------------------------------------------------------------------------------
48
49
50 //------------------------------------------------------------------------------
51 void vvLinkPanel::addImage(std::string name, std::string id)
52 {
53   imageNames.push_back(name);
54   image1Ids.push_back(id);
55   UpdateComboBox1();
56 }
57 //------------------------------------------------------------------------------
58
59
60 //------------------------------------------------------------------------------
61 bool vvLinkPanel::isLinkAll()
62 {
63   return mLinkAll;
64 }
65 //------------------------------------------------------------------------------
66
67 //------------------------------------------------------------------------------
68 void vvLinkPanel::removeImage(int index)
69 {
70   std::string idRemoved = image1Ids[index];
71   std::vector<std::string>::iterator Nameiter = imageNames.begin();
72   std::vector<std::string>::iterator Iditer = image1Ids.begin();
73   for (int i = 0; i < index; i++) {
74     Nameiter++;
75     Iditer++;
76   }
77   imageNames.erase(Nameiter);
78   image1Ids.erase(Iditer);
79   UpdateComboBox1();
80   for (int i = linkTableWidget->rowCount() - 1; i >= 0 ; i--) {
81     if (linkTableWidget->item(i,4)->text().toStdString() == idRemoved ||
82         linkTableWidget->item(i,5)->text().toStdString() == idRemoved) {
83       emit removeLink(linkTableWidget->item(i,4)->text(),linkTableWidget->item(i,5)->text());
84       linkTableWidget->removeRow(i);
85       UpdateComboBox2(image1ComboBox->currentIndex());
86     }
87   }
88 }
89 //------------------------------------------------------------------------------
90
91
92 //------------------------------------------------------------------------------
93 void vvLinkPanel::UpdateComboBox1()
94 {
95   image1ComboBox->clear();
96   for (unsigned int i = 0; i < imageNames.size(); i++) {
97     image1ComboBox->addItem(imageNames[i].c_str());
98   }
99 }
100 //------------------------------------------------------------------------------
101
102
103 //------------------------------------------------------------------------------
104 void vvLinkPanel::UpdateComboBox2(int index)
105 {
106   image2ComboBox->clear();
107   image2Ids.resize(0);
108   if (imageNames.size() > 1 && index >= 0) {
109     for (unsigned int i = 0; i < imageNames.size(); i++) {
110       if ((int)i != index) {
111         bool AlreadyLinked = false;
112         for (int row = 0; row < linkTableWidget->rowCount(); row++) {
113           if ((linkTableWidget->item(row,1)->text().toStdString() == imageNames[index] &&
114                linkTableWidget->item(row,3)->text().toStdString() == imageNames[i]) ||
115               (linkTableWidget->item(row,3)->text().toStdString() == imageNames[index] &&
116                linkTableWidget->item(row,1)->text().toStdString() == imageNames[i])) {
117             AlreadyLinked = true;
118             break;
119           }
120         }
121         if (!AlreadyLinked) {
122           image2ComboBox->addItem(imageNames[i].c_str());
123           image2Ids.push_back(image1Ids[i]);
124         }
125       }
126     }
127   }
128   if (image2ComboBox->count() == 0)
129     linkButton->setEnabled(0);
130   else
131     linkButton->setEnabled(1);
132 }
133 //------------------------------------------------------------------------------
134
135
136 //------------------------------------------------------------------------------
137 void vvLinkPanel::linkAll()
138 {
139   mLinkAll = true;
140   //First remove all links
141   while (linkTableWidget->rowCount())
142     removeLink(1,1);
143   //Now create all possible links
144   int count=image2ComboBox->count();
145   for (int j=0; j<count; j++) {
146     image1ComboBox->setCurrentIndex(j);
147     image2ComboBox->setCurrentIndex(0);
148     for (int i=0; i< count-j; i++)
149       addLink();
150   }
151   mLinkAll = false;
152 }
153 //------------------------------------------------------------------------------
154
155 //------------------------------------------------------------------------------
156 void vvLinkPanel::addLinkFromIds(QString id1, QString id2)
157 {
158   int index1 = -1, index2 = -1;
159   size_t s1 = image1Ids.size();
160   size_t s2 = image2Ids.size();
161   for (size_t i = 0; i < s1 && index1 == -1; i++)
162     if (image1Ids[i] == id1.toStdString())
163       index1 = i;
164
165   if (index1 >= 0)
166     image1ComboBox->setCurrentIndex(index1);
167     
168   for (size_t i = 0; i < s2 && index2 == -1; i++)
169     if (image2Ids[i] == id2.toStdString())
170       index2 = i;
171   
172   if (index1 >= 0 && index2 >= 0) {
173     image2ComboBox->setCurrentIndex(index2);
174     addLink();
175   }
176 }
177 //------------------------------------------------------------------------------
178
179 //------------------------------------------------------------------------------
180 void vvLinkPanel::addLink()
181 {
182   if (!image1ComboBox->currentText().isEmpty()
183       && !image2ComboBox->currentText().isEmpty()) {
184     int row = linkTableWidget->rowCount();
185     linkTableWidget->insertRow(row);
186
187     linkTableWidget->setItem(row,1,new QTableWidgetItem(image1ComboBox->currentText()));
188     linkTableWidget->setItem(row,2,new QTableWidgetItem("&"));
189     linkTableWidget->setItem(row,3,new QTableWidgetItem(image2ComboBox->currentText()));
190     linkTableWidget->setItem(row,4,new QTableWidgetItem(image1Ids[image1ComboBox->currentIndex()].c_str()));
191     linkTableWidget->setItem(row,5,new QTableWidgetItem(image2Ids[image2ComboBox->currentIndex()].c_str()));
192     QTreePushButton* cButton = new QTreePushButton;
193     cButton->setIndex(linkTableWidget->rowCount());
194     cButton->setColumn(0);
195     cButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/exit.png")));
196     connect(cButton,SIGNAL(clickedInto(int, int)),
197             this,SLOT(removeLink(int, int)));
198     cButton->setToolTip(tr("remove link"));
199     linkTableWidget->setCellWidget(row,0,cButton);
200
201     linkTableWidget->resizeColumnToContents(0);
202     linkTableWidget->resizeColumnToContents(1);
203     linkTableWidget->resizeColumnToContents(2);
204     linkTableWidget->resizeColumnToContents(3);
205     linkTableWidget->setRowHeight(row,17);
206
207     emit addLink(image1Ids[image1ComboBox->currentIndex()].c_str(),
208                  image2Ids[image2ComboBox->currentIndex()].c_str(),
209                  true
210                 );
211     UpdateComboBox2(image1ComboBox->currentIndex());
212   }
213
214 }
215 //------------------------------------------------------------------------------
216
217
218 //------------------------------------------------------------------------------
219 void vvLinkPanel::removeLink(int row, int column)
220 {
221 //  DD(row);
222 //   DD(column);
223   while (linkTableWidget->item(row-1,4) == NULL) {
224     --row;
225     //  DD(linkTableWidget->rowCount());
226 //     DD(row);
227     // return;
228   }
229   if (linkTableWidget->item(row-1,5) == NULL) {
230     return; // should not happend ...
231   }
232
233   emit removeLink(linkTableWidget->item(row-1,4)->text(),linkTableWidget->item(row-1,5)->text());
234   // DD("after emit");
235   linkTableWidget->removeRow(row-1);
236   // DD("after removeRow");
237   UpdateComboBox2(image1ComboBox->currentIndex());
238   // DD("end");
239 }
240 //------------------------------------------------------------------------------
241
242 #endif /* end #define _vvLinkPanel_CXX */
243