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();
44 if (isWindow()) { // In this case, the tool is a floating windows
45 // this->grabKeyboard(); // for the ESC key to close the dialog
46 mPreventToUseTwoToolsOnSameInput = false;
48 else { // In this case it is inserted into a tab
49 DD("Not implemented yet TODO !!");
51 // Prevent to load two tools at the same time
52 DD(mIsAnotherToolWaitInput);
53 if (mIsAnotherToolWaitInput) {
59 else mIsAnotherToolWaitInput = true;
60 mPreventToUseTwoToolsOnSameInput = true;
61 // Setup the UI in a new widget
63 mWidgetForTab = new QWidget(this);
64 // <-- try to set mToolWidget instead of this ? NO (loop)
65 // <-- try to set parent instead of this ? NO, change nothing
67 QVBoxLayout * verticalLayout = new QVBoxLayout;//(mWidgetForTab);
68 verticalLayout->addWidget(mToolInputSelectionWidget);
69 verticalLayout->addWidget(mToolWidget);
70 verticalLayout->setContentsMargins(1, 1, 1, 1);
71 mWidgetForTab->setLayout(verticalLayout);
72 DD(mWidgetForTab->isVisible());
73 mWidgetForTab->setVisible(true);
74 DD(mWidgetForTab->isVisible());
76 // Is this the first time we add a tab ?
77 if (parent->GetTab()->widget(mTabNumber) == NULL) { // Yes, create main widget
78 DD("Create main widget");
79 mStaticWidgetForTab = new QWidget(parent->GetTab());//parent, f);
80 // <-- try to set mToolWidget instead of nothing ? NO loop
81 // <-- try to set parent->GetTab() instead of nothing ?
82 mStaticVerticalLayout = new QVBoxLayout;//(mStaticWidgetForTab);
83 mStaticWidgetForTab->setLayout(mStaticVerticalLayout);
85 /*mWidgetForTab = new QWidget(mStaticWidgetForTab);
86 QVBoxLayout * verticalLayout = new QVBoxLayout;//(mWidgetForTab);
87 verticalLayout->addWidget(mToolInputSelectionWidget);
88 verticalLayout->addWidget(mToolWidget);
89 verticalLayout->setContentsMargins(1, 1, 1, 1);
90 mWidgetForTab->setLayout(verticalLayout);
91 DD(mWidgetForTab->isVisible());
92 mWidgetForTab->setVisible(true);
93 DD(mWidgetForTab->isVisible());*/
95 //<---------- mStaticVerticalLayout->addWidget(mWidgetForTab);
97 mTabNumber = parent->GetTab()->addTab(mStaticWidgetForTab, "");
98 DD(mStaticWidgetForTab->isVisible());
99 mStaticWidgetForTab->setVisible(true);
100 DD(mStaticWidgetForTab->isVisible());
101 // mWidgetForTab->setParent(mStaticWidgetForTab);
104 DD("insert into widget");
105 mStaticVerticalLayout->addWidget(mWidgetForTab);
107 mToolWidget->setEnabled(true);
109 parent->GetTab()->setCurrentIndex(mTabNumber);
110 mMainButtonBox->hide(); // No OK/Cancel by default in this case
113 //------------------------------------------------------------------------------
116 //------------------------------------------------------------------------------
117 void vvToolWidgetBase::Initialization()
119 mCurrentSlicerManager = 0;
120 mIsInitialized = false;
122 setWindowModality(Qt::NonModal);
123 // GUI Initialization
124 setupUi(this); ///////////////////////////// TRIAL
125 // Connect signals & slots
126 connect(mMainWindow, SIGNAL(AnImageIsBeingClosed(vvSlicerManager*)), this, SLOT(AnImageIsBeingClosed(vvSlicerManager*)));
127 connect(mMainWindow, SIGNAL(SelectedImageHasChanged(vvSlicerManager*)), this, SLOT(SelectedImageHasChanged(vvSlicerManager*)));
128 connect(mToolInputSelectionWidget, SIGNAL(accepted()), this, SLOT(InputIsSelected()));
129 connect(mToolInputSelectionWidget, SIGNAL(rejected()), this, SLOT(close()));
130 connect(mMainButtonBox, SIGNAL(accepted()), this, SLOT(apply()));
131 connect(mMainButtonBox, SIGNAL(rejected()), this, SLOT(close()));
133 // Disable main widget while input image is not selected
134 mToolWidget->setEnabled(false);
136 //------------------------------------------------------------------------------
139 //------------------------------------------------------------------------------
140 vvToolWidgetBase::~vvToolWidgetBase()
143 //------------------------------------------------------------------------------
146 //------------------------------------------------------------------------------
147 void vvToolWidgetBase::keyPressEvent(QKeyEvent *event)
149 if (event->key() == Qt::Key_Escape) {
155 QWidget::keyPressEvent(event);
158 //mMainWindow->keyPressEvent(event);
159 // QWidget::keyPressEvent(event);
161 //------------------------------------------------------------------------------
164 //------------------------------------------------------------------------------
165 void vvToolWidgetBase::accept()
169 //------------------------------------------------------------------------------
172 //------------------------------------------------------------------------------
173 void vvToolWidgetBase::reject()
177 //------------------------------------------------------------------------------
180 //------------------------------------------------------------------------------
181 void vvToolWidgetBase::AddInputSelector(QString s, clitk::ImageToImageGenericFilterBase * f, bool allowSkip)
185 mSlicerManagersCompatible.clear();
186 mToolInputSelectionWidget->setToolTip(QString("%1").arg(mFilter->GetAvailableImageTypes().c_str()));
187 mCurrentCompatibleIndex = 0;
188 for(unsigned int i=0; i<mMainWindow->GetSlicerManagers().size(); i++) {
189 vvImage * s = mMainWindow->GetSlicerManagers()[i]->GetImage();
190 if (mFilter->CheckImageType(s->GetNumberOfDimensions(),
191 s->GetNumberOfScalarComponents(),
192 s->GetScalarTypeAsITKString())) {
193 mSlicerManagersCompatible.push_back(mMainWindow->GetSlicerManagers()[i]);
194 if ((int)i == mMainWindow->GetSlicerManagerCurrentIndex()) mCurrentCompatibleIndex = j;
198 if (mSlicerManagersCompatible.size() == 0) {
199 std::ostringstream osstream;
200 osstream << "Sorry, could not perform operation. No (compatible) image. "
201 << mFilter->GetAvailableImageTypes();
202 QMessageBox::information(this, "No image", osstream.str().c_str());
206 if (mPreventToUseTwoToolsOnSameInput) {
207 CheckInputList(mSlicerManagersCompatible, mCurrentCompatibleIndex);
208 if (mSlicerManagersCompatible.size() == 0) {
209 QMessageBox::information(mMainWindow, tr("Error"), "Sorry, no other loaded images can use this tool. Abort");
214 mToolInputSelectionWidget->AddInputSelector(s, mSlicerManagersCompatible, mCurrentCompatibleIndex, allowSkip);
216 //------------------------------------------------------------------------------
219 //------------------------------------------------------------------------------
220 void vvToolWidgetBase::AddInputSelector(QString s, bool allowSkip)
222 mSlicerManagersCompatible.clear();
223 for(unsigned int i=0; i<mMainWindow->GetSlicerManagers().size(); i++) {
224 mSlicerManagersCompatible.push_back(mMainWindow->GetSlicerManagers()[i]);
226 if (mMainWindow->GetSlicerManagers().size() == 0) {
227 QMessageBox::information(this, "No image","Sorry, could not perform operation. No (compatible) image.");
231 mCurrentCompatibleIndex = mMainWindow->GetSlicerManagerCurrentIndex();
232 if (mPreventToUseTwoToolsOnSameInput) {
233 CheckInputList(mSlicerManagersCompatible, mCurrentCompatibleIndex);
234 if (mSlicerManagersCompatible.size() == 0) {
235 QMessageBox::information(mMainWindow, tr("Error"), "Sorry, no other loaded images can use this tool. Abort");
240 mToolInputSelectionWidget->AddInputSelector(s, mSlicerManagersCompatible, mCurrentCompatibleIndex, allowSkip);
242 //------------------------------------------------------------------------------
245 //------------------------------------------------------------------------------
246 void vvToolWidgetBase::HideInputSelector()
251 splitter->setSizes(s);
253 //------------------------------------------------------------------------------
256 //------------------------------------------------------------------------------
257 void vvToolWidgetBase::show()
259 if (!mIsInitialized) {
260 mToolInputSelectionWidget->Initialize();
261 mIsInitialized = true;
265 //------------------------------------------------------------------------------
268 //------------------------------------------------------------------------------
269 void vvToolWidgetBase::closeEvent(QCloseEvent *event)
271 mIsAnotherToolWaitInput = false;
273 event->accept();//return QWidget::close();
277 if (!mStaticWidgetForTab) {
278 event->accept();//return QWidget::close();
281 mStaticVerticalLayout->removeWidget(mWidgetForTab);
282 mWidgetForTab->close();
283 delete mWidgetForTab;
284 QList<QObject*> l =mStaticWidgetForTab->children();
286 QWidget * c = dynamic_cast<QWidget*>(l[1]);
292 //------------------------------------------------------------------------------
295 //------------------------------------------------------------------------------
296 bool vvToolWidgetBase::close()
298 QApplication::restoreOverrideCursor();
299 return QWidget::close();
301 //------------------------------------------------------------------------------
304 //------------------------------------------------------------------------------
305 void vvToolWidgetBase::AnImageIsBeingClosed(vvSlicerManager * m)
307 mToolInputSelectionWidget->AnImageIsBeingClosed(m);
308 if (m == mCurrentSlicerManager) {
312 //------------------------------------------------------------------------------
315 //------------------------------------------------------------------------------
316 void vvToolWidgetBase::SwapCurrentWidget()
318 mStaticWidgetForTab->setUpdatesEnabled(false);
319 QList<QObject*> l =mStaticWidgetForTab->children();
320 for(int i=1; i<l.size(); i++) {
321 QWidget * c = dynamic_cast<QWidget*>(l[i]);
322 if (l[i] == mWidgetForTab) {
326 c->setVisible(false);
329 mStaticWidgetForTab->setUpdatesEnabled(true);
331 //------------------------------------------------------------------------------
334 //------------------------------------------------------------------------------
335 void vvToolWidgetBase::SelectedImageHasChanged(vvSlicerManager * m)
337 if (!isWindow()) { // When the tool is not in a window, it is in a tab : we only display if needed
338 if (mCurrentSlicerManager == NULL) return;
339 if (mToolWidget == NULL) return;
340 if (m != mCurrentSlicerManager) { // current tool is not selected
341 mToolWidget->setEnabled(false);
343 else { // The current tool is selected
345 mToolWidget->setEnabled(true);
349 //------------------------------------------------------------------------------
352 //------------------------------------------------------------------------------
353 void vvToolWidgetBase::InitializeInputs()
356 //------------------------------------------------------------------------------
359 //------------------------------------------------------------------------------
360 void vvToolWidgetBase::InputIsSelected()
362 mMainButtonBox->setEnabled(true);
363 std::vector<vvSlicerManager*> & l = mToolInputSelectionWidget->GetSelectedInputs();
364 mCurrentSlicerManager = l[0];
365 mCurrentImage = mCurrentSlicerManager->GetImage();
366 mToolWidget->setEnabled(true);
367 if (!mCurrentSlicerManager) close();
368 if (l.size() == 1) InputIsSelected(mCurrentSlicerManager);
369 else InputIsSelected(l);
370 mIsAnotherToolWaitInput = false;
372 //------------------------------------------------------------------------------
375 //------------------------------------------------------------------------------
376 void vvToolWidgetBase::InputIsSelected(vvSlicerManager * m)
378 std::cerr << "You MUST overwrite this method vvToolWidgetBase::InputIsSelected(vvSlicerManager * m) if you use one single input" << std::endl;
381 //------------------------------------------------------------------------------
384 //------------------------------------------------------------------------------
385 void vvToolWidgetBase::InputIsSelected(std::vector<vvSlicerManager*> & l)
387 mMainButtonBox->setEnabled(true);
388 if (l.size() == 1) InputIsSelected(l[0]);
390 std::cerr << "You MUST overwrite this method vvToolWidgetBase::InputIsSelected(vector<vvSlicerManager *> m) if you use several input" << std::endl;
394 //------------------------------------------------------------------------------