]> Creatis software - clitk.git/blob - vv/vvLinkPanel.cxx
Initial revision
[clitk.git] / vv / vvLinkPanel.cxx
1 #ifndef _vvLinkPanel_CXX
2 #define _vvLinkPanel_CXX
3
4 /*=========================================================================
5
6  Program:   vv
7  Module:    $RCSfile: vvLinkPanel.cxx,v $
8  Language:  C++
9  Date:      $Date: 2010/01/06 13:31:57 $
10  Version:   $Revision: 1.1 $
11  Author :   Pierre Seroul (pierre.seroul@gmail.com)
12
13 Copyright (C) 200COLUMN_IMAGE_NAME
14 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
15 CREATIS-LRMN http://www.creatis.insa-lyon.fr
16
17 This program is free software: you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation, version 3 of the License.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
29 =========================================================================*/
30
31 #include "vvLinkPanel.h"
32 #include "clitkCommon.h"
33
34 #include <QtGui>
35 #include <Qt>
36 #include "QTreePushButton.h"
37
38 //====================================================================
39 vvLinkPanel::vvLinkPanel(QWidget * parent):QWidget(parent)
40 {
41     setupUi(this);
42     imageNames.resize(0);
43     image1Ids.resize(0);
44     image2Ids.resize(0);
45
46     linkTableWidget->resizeColumnsToContents();
47     linkTableWidget->verticalHeader()->hide();
48     linkTableWidget->horizontalHeader()->hide();
49     linkTableWidget->hideColumn(4);
50     linkTableWidget->hideColumn(5);
51
52     connect(image1ComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(UpdateComboBox2(int)));
53     connect(linkButton,SIGNAL(clicked()),this,SLOT(addLink()));
54     connect(linkAllButton,SIGNAL(clicked()),this,SLOT(linkAll()));
55 }
56
57 void vvLinkPanel::addImage(std::string name, std::string id)
58 {
59     imageNames.push_back(name);
60     image1Ids.push_back(id);
61     UpdateComboBox1();
62 }
63
64 void vvLinkPanel::removeImage(int index)
65 {
66     std::string idRemoved = image1Ids[index];
67     std::vector<std::string>::iterator Nameiter = imageNames.begin();
68     std::vector<std::string>::iterator Iditer = image1Ids.begin();
69     for (int i = 0; i < index; i++)
70     {
71         Nameiter++;
72         Iditer++;
73     }
74     imageNames.erase(Nameiter);
75     image1Ids.erase(Iditer);
76     UpdateComboBox1();
77     for (int i = linkTableWidget->rowCount() - 1; i >= 0 ;i--)
78     {
79         if (linkTableWidget->item(i,4)->text().toStdString() == idRemoved ||
80                 linkTableWidget->item(i,5)->text().toStdString() == idRemoved)
81         {
82             emit removeLink(linkTableWidget->item(i,4)->text(),linkTableWidget->item(i,5)->text());
83             linkTableWidget->removeRow(i);
84             UpdateComboBox2(image1ComboBox->currentIndex());
85         }
86     }
87 }
88
89 void vvLinkPanel::UpdateComboBox1()
90 {
91     image1ComboBox->clear();
92     for (unsigned int i = 0; i < imageNames.size();i++)
93     {
94         image1ComboBox->addItem(imageNames[i].c_str());
95     }
96 }
97
98 void vvLinkPanel::UpdateComboBox2(int index)
99 {
100     image2ComboBox->clear();
101     image2Ids.resize(0);
102     if (imageNames.size() > 1 && index >= 0)
103     {
104         for (unsigned int i = 0; i < imageNames.size();i++)
105         {
106             if ((int)i != index)
107             {
108                 bool AlreadyLinked = false;
109                 for (int row = 0; row < linkTableWidget->rowCount();row++)
110                 {
111                     if ((linkTableWidget->item(row,1)->text().toStdString() == imageNames[index] &&
112                             linkTableWidget->item(row,3)->text().toStdString() == imageNames[i]) ||
113                             (linkTableWidget->item(row,3)->text().toStdString() == imageNames[index] &&
114                              linkTableWidget->item(row,1)->text().toStdString() == imageNames[i]))
115                     {
116                         AlreadyLinked = true;
117                         break;
118                     }
119                 }
120                 if (!AlreadyLinked)
121                 {
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 void vvLinkPanel::linkAll()
135 {
136     //First remove all links
137     while (linkTableWidget->rowCount())
138         removeLink(1,1);
139     //Now create all possible links
140     int count=image2ComboBox->count();
141     for (int j=0;j<count;j++)
142     {
143         image1ComboBox->setCurrentIndex(j);
144         image2ComboBox->setCurrentIndex(0);
145         for (int i=0;i< count-j;i++)
146             addLink();
147     }
148 }
149
150 void vvLinkPanel::addLink()
151 {
152     if (!image1ComboBox->currentText().isEmpty()
153             && !image2ComboBox->currentText().isEmpty())
154     {
155         int row = linkTableWidget->rowCount();
156         linkTableWidget->insertRow(row);
157
158         linkTableWidget->setItem(row,1,new QTableWidgetItem(image1ComboBox->currentText()));
159         linkTableWidget->setItem(row,2,new QTableWidgetItem("&"));
160         linkTableWidget->setItem(row,3,new QTableWidgetItem(image2ComboBox->currentText()));
161         linkTableWidget->setItem(row,4,new QTableWidgetItem(image1Ids[image1ComboBox->currentIndex()].c_str()));
162         linkTableWidget->setItem(row,5,new QTableWidgetItem(image2Ids[image2ComboBox->currentIndex()].c_str()));
163         QTreePushButton* cButton = new QTreePushButton;
164         cButton->setIndex(linkTableWidget->rowCount());
165         cButton->setColumn(0);
166         cButton->setIcon(QIcon(QString::fromUtf8(":/new/prefix1/icons/exit.png")));
167         connect(cButton,SIGNAL(clickedInto(int, int)),
168                 this,SLOT(removeLink(int, int)));
169         cButton->setToolTip(tr("remove link"));
170         linkTableWidget->setCellWidget(row,0,cButton);
171
172         linkTableWidget->resizeColumnToContents(0);
173         linkTableWidget->resizeColumnToContents(1);
174         linkTableWidget->resizeColumnToContents(2);
175         linkTableWidget->resizeColumnToContents(3);
176         linkTableWidget->setRowHeight(row,17);
177
178         emit addLink(image1Ids[image1ComboBox->currentIndex()].c_str(),
179                      image2Ids[image2ComboBox->currentIndex()].c_str());
180         UpdateComboBox2(image1ComboBox->currentIndex());
181     }
182
183 }
184
185 void vvLinkPanel::removeLink(int row, int column)
186 {
187     emit removeLink(linkTableWidget->item(row-1,4)->text(),linkTableWidget->item(row-1,5)->text());
188     linkTableWidget->removeRow(row-1);
189     UpdateComboBox2(image1ComboBox->currentIndex());
190 }
191
192 #endif /* end #define _vvLinkPanel_CXX */
193