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