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://www.centreleonberard.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>
30 //------------------------------------------------------------------------------
31 // Static initialisation
32 int vvToolWidgetBase::mTabNumber = -1;
33 QWidget * vvToolWidgetBase::mStaticWidgetForTab = NULL;
34 QVBoxLayout * vvToolWidgetBase::mStaticVerticalLayout = NULL;
35 bool vvToolWidgetBase::mIsAnotherToolWaitInput = false;
37 //------------------------------------------------------------------------------
38 vvToolWidgetBase::vvToolWidgetBase(vvMainWindowBase * parent, Qt::WindowFlags f, bool initialize):
40 Ui::vvToolWidgetBase()
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;
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 mMainButtonBox->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(mMainButtonBox, SIGNAL(accepted()), this, SLOT(apply()));
132 connect(mMainButtonBox, 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 if (event->key() == Qt::Key_Escape) {
156 QWidget::keyPressEvent(event);
159 //mMainWindow->keyPressEvent(event);
160 // QWidget::keyPressEvent(event);
162 //------------------------------------------------------------------------------
165 //------------------------------------------------------------------------------
166 void vvToolWidgetBase::accept()
170 //------------------------------------------------------------------------------
173 //------------------------------------------------------------------------------
174 void vvToolWidgetBase::reject()
178 //------------------------------------------------------------------------------
181 //------------------------------------------------------------------------------
182 void vvToolWidgetBase::AddInputSelector(QString s, clitk::ImageToImageGenericFilterBase * f, bool allowSkip)
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;
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());
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");
215 mToolInputSelectionWidget->AddInputSelector(s, mSlicerManagersCompatible, mCurrentCompatibleIndex, allowSkip);
217 //------------------------------------------------------------------------------
220 //------------------------------------------------------------------------------
221 void vvToolWidgetBase::AddInputSelector(QString s, bool allowSkip)
223 mSlicerManagersCompatible.clear();
224 for(unsigned int i=0; i<mMainWindow->GetSlicerManagers().size(); i++) {
225 mSlicerManagersCompatible.push_back(mMainWindow->GetSlicerManagers()[i]);
227 if (mMainWindow->GetSlicerManagers().size() == 0) {
228 QMessageBox::information(this, "No image","Sorry, could not perform operation. No (compatible) image.");
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");
241 mToolInputSelectionWidget->AddInputSelector(s, mSlicerManagersCompatible, mCurrentCompatibleIndex, allowSkip);
243 //------------------------------------------------------------------------------
246 //------------------------------------------------------------------------------
247 void vvToolWidgetBase::HideInputSelector()
249 mToolInputSelectionWidget->hide();
251 //------------------------------------------------------------------------------
254 //------------------------------------------------------------------------------
255 void vvToolWidgetBase::show()
257 if (!mIsInitialized) {
258 mToolInputSelectionWidget->Initialize();
259 mIsInitialized = true;
263 //------------------------------------------------------------------------------
266 //------------------------------------------------------------------------------
267 void vvToolWidgetBase::closeEvent(QCloseEvent *event)
269 mIsAnotherToolWaitInput = false;
271 event->accept();//return QWidget::close();
275 if (!mStaticWidgetForTab) {
276 event->accept();//return QWidget::close();
279 mStaticVerticalLayout->removeWidget(mWidgetForTab);
280 mWidgetForTab->close();
281 delete mWidgetForTab;
282 QList<QObject*> l =mStaticWidgetForTab->children();
284 QWidget * c = dynamic_cast<QWidget*>(l[1]);
290 //------------------------------------------------------------------------------
293 //------------------------------------------------------------------------------
294 bool vvToolWidgetBase::close()
296 QApplication::restoreOverrideCursor();
297 return QWidget::close();
299 //------------------------------------------------------------------------------
302 //------------------------------------------------------------------------------
303 void vvToolWidgetBase::AnImageIsBeingClosed(vvSlicerManager * m)
305 mToolInputSelectionWidget->AnImageIsBeingClosed(m);
306 if (m == mCurrentSlicerManager) {
310 //------------------------------------------------------------------------------
313 //------------------------------------------------------------------------------
314 void vvToolWidgetBase::SwapCurrentWidget()
316 mStaticWidgetForTab->setUpdatesEnabled(false);
317 QList<QObject*> l =mStaticWidgetForTab->children();
318 for(int i=1; i<l.size(); i++) {
319 QWidget * c = dynamic_cast<QWidget*>(l[i]);
320 if (l[i] == mWidgetForTab) {
324 c->setVisible(false);
327 mStaticWidgetForTab->setUpdatesEnabled(true);
329 //------------------------------------------------------------------------------
332 //------------------------------------------------------------------------------
333 void vvToolWidgetBase::SelectedImageHasChanged(vvSlicerManager * m)
335 if (!isWindow()) { // When the tool is not in a window, it is in a tab : we only display if needed
336 if (mCurrentSlicerManager == NULL) return;
337 if (mToolWidget == NULL) return;
338 if (m != mCurrentSlicerManager) { // current tool is not selected
339 mToolWidget->setEnabled(false);
341 else { // The current tool is selected
343 mToolWidget->setEnabled(true);
347 //------------------------------------------------------------------------------
350 //------------------------------------------------------------------------------
351 void vvToolWidgetBase::InitializeInputs()
354 //------------------------------------------------------------------------------
357 //------------------------------------------------------------------------------
358 void vvToolWidgetBase::InputIsSelected()
360 mMainButtonBox->setEnabled(true);
361 std::vector<vvSlicerManager*> & l = mToolInputSelectionWidget->GetSelectedInputs();
362 mCurrentSlicerManager = l[0];
363 mCurrentImage = mCurrentSlicerManager->GetImage();
364 mToolWidget->setEnabled(true);
365 if (!mCurrentSlicerManager) close();
366 if (l.size() == 1) InputIsSelected(mCurrentSlicerManager);
367 else InputIsSelected(l);
368 mIsAnotherToolWaitInput = false;
370 //------------------------------------------------------------------------------
373 //------------------------------------------------------------------------------
374 void vvToolWidgetBase::InputIsSelected(vvSlicerManager * m)
376 std::cerr << "You MUST overwrite this method vvToolWidgetBase::InputIsSelected(vvSlicerManager * m) if you use one single input" << std::endl;
379 //------------------------------------------------------------------------------
382 //------------------------------------------------------------------------------
383 void vvToolWidgetBase::InputIsSelected(std::vector<vvSlicerManager*> & l)
385 mMainButtonBox->setEnabled(true);
386 if (l.size() == 1) InputIsSelected(l[0]);
388 std::cerr << "You MUST overwrite this method vvToolWidgetBase::InputIsSelected(vector<vvSlicerManager *> m) if you use several input" << std::endl;
392 //------------------------------------------------------------------------------