]> Creatis software - clitk.git/blob - vv/vvToolWidgetBase.cxx
patches sent from Joel Schaerer
[clitk.git] / vv / vvToolWidgetBase.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 // vv
20 #include "vvToolWidgetBase.h"
21 #include "vvMainWindowBase.h"
22 #include "vvSlicerManager.h"
23
24 // Qt
25 #include <QMessageBox>
26 #include <QKeyEvent>
27 #include <QDockWidget>
28 #include <QTabWidget>
29
30 //------------------------------------------------------------------------------
31 // Static initialisation
32 int vvToolWidgetBase::mTabNumber = -1;
33 QWidget * vvToolWidgetBase::mStaticWidgetForTab = NULL;
34 QVBoxLayout * vvToolWidgetBase::mStaticVerticalLayout = NULL;
35 bool vvToolWidgetBase::mIsAnotherToolWaitInput = false;
36
37 //------------------------------------------------------------------------------
38 vvToolWidgetBase::vvToolWidgetBase(vvMainWindowBase * parent, Qt::WindowFlags f, bool initialize):
39   QWidget(parent, f),
40   Ui::vvToolWidgetBase()
41 {
42   mMainWindow = parent;
43   setAttribute(Qt::WA_DeleteOnClose);
44   if (initialize) Initialization();
45   if (isWindow()) { // In this case, the tool is a floating windows
46     // this->grabKeyboard();   // for the ESC key to close the dialog
47     mPreventToUseTwoToolsOnSameInput = false;
48   }
49   else { // In this case it is inserted into a tab
50     DD("Not implemented yet TODO !!");
51     exit(0);
52     // Prevent to load two tools at the same time
53     DD(mIsAnotherToolWaitInput);
54     if (mIsAnotherToolWaitInput) {
55       //      setVisible(false);
56       QWidget::close();
57       DD("before return");
58       return;
59     }
60     else mIsAnotherToolWaitInput = true;
61     mPreventToUseTwoToolsOnSameInput = true;
62     // Setup the UI in a new widget
63
64     mWidgetForTab = new QWidget(this); 
65                                 // <-- try to set mToolWidget instead of this ? NO  (loop)
66                                 // <-- try to set parent instead of this ? NO, change nothing
67
68     QVBoxLayout * verticalLayout = new QVBoxLayout;//(mWidgetForTab);
69     verticalLayout->addWidget(mToolInputSelectionWidget);
70     verticalLayout->addWidget(mToolWidget);
71     verticalLayout->setContentsMargins(1, 1, 1, 1);
72     mWidgetForTab->setLayout(verticalLayout);
73     DD(mWidgetForTab->isVisible());
74     mWidgetForTab->setVisible(true);
75     DD(mWidgetForTab->isVisible());
76
77     // Is this the first time we add a tab ? 
78     if (parent->GetTab()->widget(mTabNumber) == NULL) { // Yes, create main widget
79       DD("Create main widget");
80       mStaticWidgetForTab = new QWidget(parent->GetTab());//parent, f); 
81                                 // <-- try to set mToolWidget instead of nothing ? NO loop
82                                 // <-- try to set parent->GetTab() instead of nothing ? 
83       mStaticVerticalLayout = new QVBoxLayout;//(mStaticWidgetForTab);
84       mStaticWidgetForTab->setLayout(mStaticVerticalLayout);
85
86       /*mWidgetForTab = new QWidget(mStaticWidgetForTab);
87     QVBoxLayout * verticalLayout = new QVBoxLayout;//(mWidgetForTab);
88     verticalLayout->addWidget(mToolInputSelectionWidget);
89     verticalLayout->addWidget(mToolWidget);
90     verticalLayout->setContentsMargins(1, 1, 1, 1);
91     mWidgetForTab->setLayout(verticalLayout);
92     DD(mWidgetForTab->isVisible());
93     mWidgetForTab->setVisible(true);
94     DD(mWidgetForTab->isVisible());*/
95
96       //<----------      mStaticVerticalLayout->addWidget(mWidgetForTab); 
97
98       mTabNumber = parent->GetTab()->addTab(mStaticWidgetForTab, "");
99       DD(mStaticWidgetForTab->isVisible());
100       mStaticWidgetForTab->setVisible(true);
101       DD(mStaticWidgetForTab->isVisible());
102       //      mWidgetForTab->setParent(mStaticWidgetForTab);
103     }
104     else {
105       DD("insert into widget");
106       mStaticVerticalLayout->addWidget(mWidgetForTab);
107       SwapCurrentWidget();
108       mToolWidget->setEnabled(true);
109     }
110     parent->GetTab()->setCurrentIndex(mTabNumber); 
111     mMainButtonBox->hide(); // No OK/Cancel by default in this case
112   }
113 }
114 //------------------------------------------------------------------------------
115
116
117 //------------------------------------------------------------------------------
118 void vvToolWidgetBase::Initialization() 
119 {
120   mCurrentSlicerManager = 0;
121   mIsInitialized = false;
122   mFilter = 0;
123   setWindowModality(Qt::NonModal);
124   // GUI Initialization
125   setupUi(this); ///////////////////////////// TRIAL
126   // Connect signals & slots
127   connect(mMainWindow, SIGNAL(AnImageIsBeingClosed(vvSlicerManager*)), this, SLOT(AnImageIsBeingClosed(vvSlicerManager*)));
128   connect(mMainWindow, SIGNAL(SelectedImageHasChanged(vvSlicerManager*)), this, SLOT(SelectedImageHasChanged(vvSlicerManager*)));
129   connect(mToolInputSelectionWidget, SIGNAL(accepted()), this, SLOT(InputIsSelected()));
130   connect(mToolInputSelectionWidget, SIGNAL(rejected()), this, SLOT(close()));
131   connect(mMainButtonBox, SIGNAL(accepted()), this, SLOT(apply()));
132   connect(mMainButtonBox, SIGNAL(rejected()), this, SLOT(close()));
133
134   // Disable main widget while input image is not selected
135   mToolWidget->setEnabled(false);
136 }
137 //------------------------------------------------------------------------------
138
139
140 //------------------------------------------------------------------------------
141 vvToolWidgetBase::~vvToolWidgetBase()
142 {
143 }
144 //------------------------------------------------------------------------------
145
146
147 //------------------------------------------------------------------------------
148 void vvToolWidgetBase::keyPressEvent(QKeyEvent *event) 
149 {
150   if (event->key() == Qt::Key_Escape) {
151     reject();
152     event->accept();
153     return;
154   } 
155   else {
156     QWidget::keyPressEvent(event);
157   }
158   //  event->ignore();
159   //mMainWindow->keyPressEvent(event);
160   // QWidget::keyPressEvent(event);
161 }
162 //------------------------------------------------------------------------------
163
164
165 //------------------------------------------------------------------------------
166 void vvToolWidgetBase::accept()
167 {
168   apply();
169 }
170 //------------------------------------------------------------------------------
171
172
173 //------------------------------------------------------------------------------
174 void vvToolWidgetBase::reject()
175 {
176   close();
177 }
178 //------------------------------------------------------------------------------
179
180
181 //------------------------------------------------------------------------------
182 void vvToolWidgetBase::AddInputSelector(QString s, clitk::ImageToImageGenericFilterBase * f, bool allowSkip)
183 {
184   int j=0;
185   mFilter = f;
186   mSlicerManagersCompatible.clear();
187   mToolInputSelectionWidget->setToolTip(QString("%1").arg(mFilter->GetAvailableImageTypes().c_str()));
188   mCurrentCompatibleIndex = 0;
189   for(unsigned int i=0; i<mMainWindow->GetSlicerManagers().size(); i++) {
190     vvImage * s = mMainWindow->GetSlicerManagers()[i]->GetImage();
191     if (mFilter->CheckImageType(s->GetNumberOfDimensions(),
192                                 s->GetNumberOfScalarComponents(),
193                                 s->GetScalarTypeAsITKString())) {
194       mSlicerManagersCompatible.push_back(mMainWindow->GetSlicerManagers()[i]);
195       if ((int)i == mMainWindow->GetSlicerManagerCurrentIndex()) mCurrentCompatibleIndex = j;
196       j++;
197     }
198   }
199   if (mSlicerManagersCompatible.size() == 0) {
200     std::ostringstream osstream;
201     osstream << "Sorry, could not perform operation. No (compatible) image. "
202              << mFilter->GetAvailableImageTypes();
203     QMessageBox::information(this, "No image", osstream.str().c_str());
204     reject();
205     return;
206   }
207   if (mPreventToUseTwoToolsOnSameInput) {
208     CheckInputList(mSlicerManagersCompatible, mCurrentCompatibleIndex);
209     if (mSlicerManagersCompatible.size() == 0) {
210       QMessageBox::information(mMainWindow, tr("Error"), "Sorry, no other loaded images can use this tool. Abort");
211       reject();
212       return;
213     }
214   }
215   mToolInputSelectionWidget->AddInputSelector(s, mSlicerManagersCompatible, mCurrentCompatibleIndex, allowSkip);
216 }
217 //------------------------------------------------------------------------------
218
219
220 //------------------------------------------------------------------------------
221 void vvToolWidgetBase::AddInputSelector(QString s, bool allowSkip)
222 {
223   mSlicerManagersCompatible.clear();
224   for(unsigned int i=0; i<mMainWindow->GetSlicerManagers().size(); i++) {
225     mSlicerManagersCompatible.push_back(mMainWindow->GetSlicerManagers()[i]);
226   }
227   if (mMainWindow->GetSlicerManagers().size() == 0) {
228     QMessageBox::information(this, "No image","Sorry, could not perform operation. No (compatible) image.");
229     close();
230     return;
231   }
232   mCurrentCompatibleIndex = mMainWindow->GetSlicerManagerCurrentIndex();
233   if (mPreventToUseTwoToolsOnSameInput) {
234     CheckInputList(mSlicerManagersCompatible,  mCurrentCompatibleIndex);
235     if (mSlicerManagersCompatible.size() == 0) {
236       QMessageBox::information(mMainWindow, tr("Error"), "Sorry, no other loaded images can use this tool. Abort");
237       close();
238       return;
239     }
240   }
241   mToolInputSelectionWidget->AddInputSelector(s, mSlicerManagersCompatible, mCurrentCompatibleIndex, allowSkip);
242 }
243 //------------------------------------------------------------------------------
244
245
246 //------------------------------------------------------------------------------
247 void vvToolWidgetBase::HideInputSelector()
248 {
249   QList<int> s;
250   s.push_back(0);
251   s.push_back(1);
252   splitter->setSizes(s);
253 }
254 //------------------------------------------------------------------------------
255
256
257 //------------------------------------------------------------------------------
258 void vvToolWidgetBase::show()
259 {
260   if (!mIsInitialized) {
261     mToolInputSelectionWidget->Initialize();
262     mIsInitialized = true;
263   }
264   QWidget::show();
265 }
266 //------------------------------------------------------------------------------
267
268
269 //------------------------------------------------------------------------------
270 void vvToolWidgetBase::closeEvent(QCloseEvent *event) 
271 {
272   mIsAnotherToolWaitInput = false;
273   if (isWindow()) {
274     event->accept();//return QWidget::close();
275     return;
276   }
277   else {
278     if (!mStaticWidgetForTab) {
279       event->accept();//return QWidget::close();
280       return;
281     }
282     mStaticVerticalLayout->removeWidget(mWidgetForTab);
283     mWidgetForTab->close();
284     delete mWidgetForTab;
285     QList<QObject*> l =mStaticWidgetForTab->children(); 
286     if (l.size() > 1) {
287       QWidget * c = dynamic_cast<QWidget*>(l[1]);
288       c->setVisible(true);
289     }
290   }
291   event->accept();
292 }
293 //------------------------------------------------------------------------------
294
295
296 //------------------------------------------------------------------------------
297 bool vvToolWidgetBase::close()
298 {
299   QApplication::restoreOverrideCursor();
300   return QWidget::close();
301 }
302 //------------------------------------------------------------------------------
303
304
305 //------------------------------------------------------------------------------
306 void vvToolWidgetBase::AnImageIsBeingClosed(vvSlicerManager * m)
307 {
308   mToolInputSelectionWidget->AnImageIsBeingClosed(m);
309   if (m == mCurrentSlicerManager) {
310     close();
311   }
312 }
313 //------------------------------------------------------------------------------
314
315
316 //------------------------------------------------------------------------------
317 void vvToolWidgetBase::SwapCurrentWidget()
318 {
319   mStaticWidgetForTab->setUpdatesEnabled(false);
320   QList<QObject*> l =mStaticWidgetForTab->children(); 
321   for(int i=1; i<l.size(); i++) {
322     QWidget * c = dynamic_cast<QWidget*>(l[i]);
323     if (l[i] == mWidgetForTab) {
324       c->setVisible(true);
325     }
326     else {
327       c->setVisible(false);
328     }
329   } 
330   mStaticWidgetForTab->setUpdatesEnabled(true);
331 }
332 //------------------------------------------------------------------------------
333
334
335 //------------------------------------------------------------------------------
336 void vvToolWidgetBase::SelectedImageHasChanged(vvSlicerManager * m)
337 {
338   if (!isWindow()) { // When the tool is not in a window, it is in a tab : we only display if needed
339     if (mCurrentSlicerManager == NULL) return;
340     if (mToolWidget == NULL) return;
341     if (m != mCurrentSlicerManager) { // current tool is not selected
342       mToolWidget->setEnabled(false);
343     }
344     else { // The current tool is selected
345       SwapCurrentWidget();
346       mToolWidget->setEnabled(true);
347     }
348   }
349 }
350 //------------------------------------------------------------------------------
351
352
353 //------------------------------------------------------------------------------
354 void vvToolWidgetBase::InitializeInputs()
355 {
356 }
357 //------------------------------------------------------------------------------
358
359
360 //------------------------------------------------------------------------------
361 void vvToolWidgetBase::InputIsSelected()
362 {
363   mMainButtonBox->setEnabled(true);
364   std::vector<vvSlicerManager*> & l = mToolInputSelectionWidget->GetSelectedInputs();
365   mCurrentSlicerManager = l[0];
366   mCurrentImage = mCurrentSlicerManager->GetImage();
367   mToolWidget->setEnabled(true);
368   if (!mCurrentSlicerManager) close();
369   if (l.size() == 1) InputIsSelected(mCurrentSlicerManager);
370   else InputIsSelected(l);
371   mIsAnotherToolWaitInput = false;
372 }
373 //------------------------------------------------------------------------------
374
375
376 //------------------------------------------------------------------------------
377 void vvToolWidgetBase::InputIsSelected(vvSlicerManager * m)
378 {
379   std::cerr << "You MUST overwrite this method vvToolWidgetBase::InputIsSelected(vvSlicerManager * m) if you use one single input" << std::endl;
380   exit(0);
381 }
382 //------------------------------------------------------------------------------
383
384
385 //------------------------------------------------------------------------------
386 void vvToolWidgetBase::InputIsSelected(std::vector<vvSlicerManager*> & l)
387 {
388   mMainButtonBox->setEnabled(true);
389   if (l.size() == 1) InputIsSelected(l[0]);
390   else {
391     std::cerr << "You MUST overwrite this method vvToolWidgetBase::InputIsSelected(vector<vvSlicerManager *> m) if you use several input" << std::endl;
392     exit(0);
393   }
394 }
395 //------------------------------------------------------------------------------