1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
20 #include "vvToolWidgetBase.h"
21 #include "vvMainWindowBase.h"
22 #include "vvSlicerManager.h"
25 #include <QMessageBox>
27 #include <QDockWidget>
29 //------------------------------------------------------------------------------
30 // Static initialisation
31 int vvToolWidgetBase::mTabNumber = -1;
32 QWidget * vvToolWidgetBase::mStaticWidgetForTab = NULL;
33 QVBoxLayout * vvToolWidgetBase::mStaticVerticalLayout = NULL;
34 bool vvToolWidgetBase::mIsAnotherToolWaitInput = false;
36 //------------------------------------------------------------------------------
37 vvToolWidgetBase::vvToolWidgetBase(vvMainWindowBase * parent, Qt::WindowFlags f, bool initialize):
39 Ui::vvToolWidgetBase()
42 setAttribute(Qt::WA_DeleteOnClose);
43 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;
49 else { // In this case it is inserted into a tab
50 DD("Not implemented yet TODO !!");
52 // Prevent to load two tools at the same time
53 DD(mIsAnotherToolWaitInput);
54 if (mIsAnotherToolWaitInput) {
60 else mIsAnotherToolWaitInput = true;
61 mPreventToUseTwoToolsOnSameInput = true;
62 // Setup the UI in a new widget
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
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());
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);
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());*/
96 //<---------- mStaticVerticalLayout->addWidget(mWidgetForTab);
98 mTabNumber = parent->GetTab()->addTab(mStaticWidgetForTab, "");
99 DD(mStaticWidgetForTab->isVisible());
100 mStaticWidgetForTab->setVisible(true);
101 DD(mStaticWidgetForTab->isVisible());
102 // mWidgetForTab->setParent(mStaticWidgetForTab);
105 DD("insert into widget");
106 mStaticVerticalLayout->addWidget(mWidgetForTab);
108 mToolWidget->setEnabled(true);
110 parent->GetTab()->setCurrentIndex(mTabNumber);
111 buttonBox->hide(); // No OK/Cancel by default in this case
114 //------------------------------------------------------------------------------
117 //------------------------------------------------------------------------------
118 void vvToolWidgetBase::Initialization()
120 mCurrentSlicerManager = 0;
121 mIsInitialized = false;
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(buttonBox, SIGNAL(accepted()), this, SLOT(apply()));
132 connect(buttonBox, SIGNAL(rejected()), this, SLOT(close()));
134 // Disable main widget while input image is not selected
135 mToolWidget->setEnabled(false);
137 //------------------------------------------------------------------------------
140 //------------------------------------------------------------------------------
141 vvToolWidgetBase::~vvToolWidgetBase()
144 //------------------------------------------------------------------------------
147 //------------------------------------------------------------------------------
148 void vvToolWidgetBase::keyPressEvent(QKeyEvent *event)
150 // DD("KEYPRESSEVENT");
151 if (event->key() == Qt::Key_Escape) {
157 QWidget::keyPressEvent(event);
160 //mMainWindow->keyPressEvent(event);
161 // QWidget::keyPressEvent(event);
163 //------------------------------------------------------------------------------
166 //------------------------------------------------------------------------------
167 void vvToolWidgetBase::accept()
171 //------------------------------------------------------------------------------
174 //------------------------------------------------------------------------------
175 void vvToolWidgetBase::reject()
179 //------------------------------------------------------------------------------
182 //------------------------------------------------------------------------------
183 void vvToolWidgetBase::AddInputSelector(QString s, clitk::ImageToImageGenericFilterBase * f, bool allowSkip)
187 mSlicerManagersCompatible.clear();
188 mToolInputSelectionWidget->setToolTip(QString("%1").arg(mFilter->GetAvailableImageTypes().c_str()));
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->GetScalarTypeAsString())) {
194 mSlicerManagersCompatible.push_back(mMainWindow->GetSlicerManagers()[i]);
195 if ((int)i == mMainWindow->GetSlicerManagerCurrentIndex()) mCurrentCompatibleIndex = j;
199 if (mSlicerManagersCompatible.size() == 0) {
200 QMessageBox::information(this, "No image","Sorry, could not perform operation. No (compatible) image.");
204 if (mPreventToUseTwoToolsOnSameInput) {
205 CheckInputList(mSlicerManagersCompatible, mCurrentCompatibleIndex);
206 if (mSlicerManagersCompatible.size() == 0) {
207 QMessageBox::information(mMainWindow, tr("Error"), "Sorry, no other loaded images can use this tool. Abort");
212 mToolInputSelectionWidget->AddInputSelector(s, mSlicerManagersCompatible, mCurrentCompatibleIndex, allowSkip);
214 //------------------------------------------------------------------------------
217 //------------------------------------------------------------------------------
218 void vvToolWidgetBase::AddInputSelector(QString s, bool allowSkip)
220 mSlicerManagersCompatible.clear();
221 for(unsigned int i=0; i<mMainWindow->GetSlicerManagers().size(); i++) {
222 mSlicerManagersCompatible.push_back(mMainWindow->GetSlicerManagers()[i]);
224 if (mMainWindow->GetSlicerManagers().size() == 0) {
225 QMessageBox::information(this, "No image","Sorry, could not perform operation. No (compatible) image.");
229 mCurrentCompatibleIndex = mMainWindow->GetSlicerManagerCurrentIndex();
230 if (mPreventToUseTwoToolsOnSameInput) {
231 CheckInputList(mSlicerManagersCompatible, mCurrentCompatibleIndex);
232 if (mSlicerManagersCompatible.size() == 0) {
233 QMessageBox::information(mMainWindow, tr("Error"), "Sorry, no other loaded images can use this tool. Abort");
238 mToolInputSelectionWidget->AddInputSelector(s, mSlicerManagersCompatible, mCurrentCompatibleIndex, allowSkip);
240 //------------------------------------------------------------------------------
243 //------------------------------------------------------------------------------
244 void vvToolWidgetBase::HideInputSelector()
249 splitter->setSizes(s);
251 //------------------------------------------------------------------------------
254 //------------------------------------------------------------------------------
255 void vvToolWidgetBase::show()
257 if (!mIsInitialized) {
258 mToolInputSelectionWidget->Initialize();
259 mIsInitialized = true;
263 //------------------------------------------------------------------------------
266 //------------------------------------------------------------------------------
267 void vvToolWidgetBase::closeEvent(QCloseEvent *event)
270 mIsAnotherToolWaitInput = false;
272 event->accept();//return QWidget::close();
276 if (!mStaticWidgetForTab) {
277 event->accept();//return QWidget::close();
280 mStaticVerticalLayout->removeWidget(mWidgetForTab);
281 mWidgetForTab->close();
282 delete mWidgetForTab;
283 QList<QObject*> l =mStaticWidgetForTab->children();
285 QWidget * c = dynamic_cast<QWidget*>(l[1]);
291 //------------------------------------------------------------------------------
294 //------------------------------------------------------------------------------
295 bool vvToolWidgetBase::close()
297 // DD("vvToolWidgetBase::close()");
298 return QWidget::close();
300 //------------------------------------------------------------------------------
303 //------------------------------------------------------------------------------
304 void vvToolWidgetBase::AnImageIsBeingClosed(vvSlicerManager * m)
306 mToolInputSelectionWidget->AnImageIsBeingClosed(m);
307 if (m == mCurrentSlicerManager) {
311 //------------------------------------------------------------------------------
314 //------------------------------------------------------------------------------
315 void vvToolWidgetBase::SwapCurrentWidget()
317 mStaticWidgetForTab->setUpdatesEnabled(false);
318 QList<QObject*> l =mStaticWidgetForTab->children();
319 for(int i=1; i<l.size(); i++) {
320 QWidget * c = dynamic_cast<QWidget*>(l[i]);
321 if (l[i] == mWidgetForTab) {
325 c->setVisible(false);
328 mStaticWidgetForTab->setUpdatesEnabled(true);
330 //------------------------------------------------------------------------------
333 //------------------------------------------------------------------------------
334 void vvToolWidgetBase::SelectedImageHasChanged(vvSlicerManager * m)
336 if (!isWindow()) { // When the tool is not in a window, it is in a tab : we only display if needed
337 if (mCurrentSlicerManager == NULL) return;
338 if (mToolWidget == NULL) return;
339 if (m != mCurrentSlicerManager) { // current tool is not selected
340 mToolWidget->setEnabled(false);
342 else { // The current tool is selected
344 mToolWidget->setEnabled(true);
348 //------------------------------------------------------------------------------
351 //------------------------------------------------------------------------------
352 void vvToolWidgetBase::InitializeInputs()
355 //------------------------------------------------------------------------------
358 //------------------------------------------------------------------------------
359 void vvToolWidgetBase::InputIsSelected()
361 // DD("vvToolWidgetBase::InputIsSelected()");
362 //DD(mWidgetForTab->isVisible());
363 //DD(mToolWidget->isVisible());
364 //DD(mStaticWidgetForTab->isVisible());
366 buttonBox->setEnabled(true);
367 std::vector<vvSlicerManager*> & l = mToolInputSelectionWidget->GetSelectedInputs();
368 mCurrentSlicerManager = l[0];
369 mCurrentImage = mCurrentSlicerManager->GetImage();
370 mToolWidget->setEnabled(true);
371 if (!mCurrentSlicerManager) close();
372 if (l.size() == 1) InputIsSelected(mCurrentSlicerManager);
373 else InputIsSelected(l);
374 mIsAnotherToolWaitInput = false;
376 //------------------------------------------------------------------------------
379 //------------------------------------------------------------------------------
380 void vvToolWidgetBase::InputIsSelected(vvSlicerManager * m)
382 std::cerr << "You MUST overwrite this method vvToolWidgetBase::InputIsSelected(vvSlicerManager * m) if you use one single input" << std::endl;
385 //------------------------------------------------------------------------------
388 //------------------------------------------------------------------------------
389 void vvToolWidgetBase::InputIsSelected(std::vector<vvSlicerManager*> & l)
391 buttonBox->setEnabled(true);
392 if (l.size() == 1) InputIsSelected(l[0]);
394 std::cerr << "You MUST overwrite this method vvToolWidgetBase::InputIsSelected(vector<vvSlicerManager *> m) if you use several input" << std::endl;
398 //------------------------------------------------------------------------------