]> Creatis software - clitk.git/blob - vv/vvLandmarksPanel.cxx
Merge branch 'master' of git://git.creatis.insa-lyon.fr/clitk
[clitk.git] / vv / vvLandmarksPanel.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 #ifndef _vvLandmarksPanel_CXX
19 #define _vvLandmarksPanel_CXX
20 #include "vvLandmarksPanel.h"
21
22 #include <QtGui>
23 #include <Qt>
24 #include "QTreePushButton.h"
25 #include "vvLandmarks.h"
26
27 #include <vtksys/SystemTools.hxx>
28
29 //====================================================================
30 vvLandmarksPanel::vvLandmarksPanel(QWidget * parent):QWidget(parent)
31 {
32   setupUi(this);
33
34   tableWidget->verticalHeader()->hide();
35   tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
36   tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
37   loadButton->setEnabled(0);
38   saveButton->setEnabled(0);
39   removeButton->setEnabled(0);
40   connect(loadButton, SIGNAL(clicked()),this,SLOT(Load()));
41   connect(saveButton, SIGNAL(clicked()),this,SLOT(Save()));
42   connect(removeButton, SIGNAL(clicked()),this,SLOT(RemoveSelectedPoints()));
43   connect(tableWidget,SIGNAL(cellChanged(int,int)),this,SLOT(CommentsChanged(int,int)));
44 }
45
46 void vvLandmarksPanel::Load()
47 {
48   QString file = QFileDialog::getOpenFileName(this,tr("Load Landmarks"),
49                  mCurrentPath.c_str(),tr("Landmarks ( *.txt)"));
50   if (!file.isEmpty())
51     mCurrentLandmarks->LoadFile(file.toStdString());
52   SetCurrentLandmarks(mCurrentLandmarks,2);
53   emit UpdateRenderWindows();
54 }
55
56 void vvLandmarksPanel::Save()
57 {
58   QString file = QFileDialog::getSaveFileName(this,
59                  tr("Save Landmarks"),
60                  mCurrentPath.c_str(),tr("Landmarks ( *.txt)"));
61   if (!file.isEmpty()) {
62     std::string filename = vtksys::SystemTools::GetFilenamePath(file.toStdString());
63     filename += "/" + vtksys::SystemTools::GetFilenameWithoutLastExtension(file.toStdString());
64     filename += ".txt";
65     mCurrentLandmarks->SaveFile(filename.c_str());
66   }
67 }
68
69 void vvLandmarksPanel::RemoveSelectedPoints()
70 {
71   if (tableWidget->rowCount() > 0) {
72     QList<QTableWidgetItem *> items = tableWidget->selectedItems();
73     if (items.empty()) {
74       tableWidget->removeRow(tableWidget->rowCount()-1);
75       mCurrentLandmarks->RemoveLastLandmark();
76     }
77     else {
78       // we're using single-selection mode
79       int row = items[0]->row();
80       mCurrentLandmarks->RemoveLandmark(row);
81       tableWidget->removeRow(row);
82       
83       for (int i = row; i < tableWidget->rowCount(); i++) {
84         QTableWidgetItem* iItem = tableWidget->item(i, 0);
85         iItem->setText(QString::number(i));
86       }
87         
88     }
89     emit UpdateRenderWindows();
90   }
91 }
92
93 void vvLandmarksPanel::AddPoint()
94 {
95   AddPoint(mCurrentLandmarks->GetNumberOfPoints()-1);
96 }
97
98 void vvLandmarksPanel::AddPoint(int landmarksIndex)
99 {
100   int rowIndex = landmarksIndex; //tableWidget->rowCount();
101   tableWidget->setRowCount(rowIndex+1);
102   tableWidget->setRowHeight(rowIndex,20);
103   QTableWidgetItem* iItem = new QTableWidgetItem(QString::number(landmarksIndex));
104   iItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
105   tableWidget->setItem(rowIndex,0,iItem);
106
107   QTableWidgetItem* xItem = new QTableWidgetItem(
108     QString::number(mCurrentLandmarks->GetCoordinates(landmarksIndex)[0],'f',1));
109   xItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
110   tableWidget->setItem(rowIndex,1,xItem);
111
112   QTableWidgetItem* yItem = new QTableWidgetItem(
113     QString::number(mCurrentLandmarks->GetCoordinates(landmarksIndex)[1],'f',1));
114   yItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
115   tableWidget->setItem(rowIndex,2,yItem);
116
117   QTableWidgetItem* zItem = new QTableWidgetItem(
118     QString::number(mCurrentLandmarks->GetCoordinates(landmarksIndex)[2],'f',1));
119   zItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
120   tableWidget->setItem(rowIndex,3,zItem);
121
122   QTableWidgetItem* tItem = new QTableWidgetItem(
123     QString::number(mCurrentLandmarks->GetCoordinates(landmarksIndex)[3],'f',1));
124   tItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
125   tableWidget->setItem(rowIndex,4,tItem);
126
127
128   QTableWidgetItem* vItem = new QTableWidgetItem(
129     QString::number(mCurrentLandmarks->GetPixelValue(landmarksIndex),'f',1));
130   vItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
131   tableWidget->setItem(rowIndex,5,vItem);
132
133   tableWidget->setItem(rowIndex,6, new QTableWidgetItem(mCurrentLandmarks->GetComments(landmarksIndex).c_str()));
134 }
135
136 void vvLandmarksPanel::SetCurrentLandmarks(vvLandmarks* lm,int time)
137 {
138   loadButton->setEnabled(1);
139   saveButton->setEnabled(1);
140   removeButton->setEnabled(1);
141   mCurrentLandmarks = lm;
142   tableWidget->clearContents();
143   tableWidget->setRowCount(mCurrentLandmarks->GetNumberOfPoints());
144   for (unsigned int i = 0; i < mCurrentLandmarks->GetNumberOfPoints(); i++)
145     AddPoint(i);
146   //if (time > 1)
147   //tableWidget->setColumnHidden(4,1);
148   //else
149   //tableWidget->setColumnHidden(4,0);
150   tableWidget->resizeColumnsToContents();
151 }
152
153 void vvLandmarksPanel::SetCurrentImage(std::string filename)
154 {
155   QString image = "<b>CurrentImage : </b>";
156   image += vtksys::SystemTools::GetFilenameWithoutLastExtension(filename).c_str();
157   nameLabel->setText(image);
158 }
159
160 void vvLandmarksPanel::CommentsChanged(int row, int column)
161 {
162   if (column == 6) {
163     mCurrentLandmarks->ChangeComments(row,std::string(tableWidget->item(row,column)->text().toStdString()));
164     tableWidget->resizeColumnsToContents();
165   }
166 }
167
168 #endif /* end #define _vvLandmarksPanel_CXX */
169