fw4spl
t/src/fwGuiQt/dialog/ProgressDialog.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include <QPaintEvent>
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwGui/registry/macros.hpp>
12 #include <fwGui/container/fwContainer.hpp>
13 #include <fwGui/IFrameSrv.hpp>
14 
15 #include "fwGuiQt/dialog/ProgressDialog.hpp"
16 
18 
19 namespace fwGuiQt
20 {
21 namespace dialog
22 {
23 
24 //------------------------------------------------------------------------------
25 
26 ProgressDialog::ProgressDialog( ::fwGui::GuiBaseObject::Key key, const std::string &title, const std::string &message)
27  : m_title (""),
28  m_pdialog ( NULL ),
29  m_pprogressbar ( NULL ),
30  m_pcancelButton ( NULL ),
31  m_pmainWindow ( NULL )
32 {
33 
34  // Use progress widget defined by IFrameSrv
35  ::fwGui::container::fwContainer::sptr progressWidget = ::fwGui::IFrameSrv::getProgressWidget();
36  QWidget *activeWindow = ::fwGuiQt::container::QtContainer::dynamicCast( progressWidget )->getQtContainer();
37  m_pmainWindow = qobject_cast< QMainWindow * >( activeWindow );
38 
39 // QWidget *activeWindow = NULL;
40 //
41 // BOOST_FOREACH (QWidget *widget, QApplication::topLevelWidgets())
42 // {
43 // activeWindow = qobject_cast< QMainWindow * >(widget);
44 // // activeWindow must also have a layout to use statusBar()
45 // if ( activeWindow && activeWindow->layout())
46 // {
47 // m_pmainWindow = qobject_cast< QMainWindow * >(activeWindow);
48 // break;
49 // }
50 // }
51 
52  m_pcancelButton = new QPushButton("Cancel");
53  QObject::connect( m_pcancelButton, SIGNAL(clicked()), this, SLOT(cancelPressed()) );
54 
55  if ( m_pmainWindow )
56  {
57  m_pprogressbar = new QProgressBar();
58  m_pprogressbar->setRange(0,100);
59  m_pprogressbar->setValue(0);
60  m_pmainWindow->statusBar()->addPermanentWidget(m_pprogressbar,0);
61  m_pmainWindow->statusBar()->addPermanentWidget(m_pcancelButton,0);
62  m_pmainWindow->statusBar()->setMinimumHeight(25);
63  m_pmainWindow->statusBar()->setMaximumHeight(25);
64  }
65  else
66  {
67  m_pdialog = new QProgressDialog( 0, Qt::WindowStaysOnTopHint );
68  //m_pdialog = new QProgressDialog( activeWindow, Qt::WindowStaysOnTopHint );
69 
70  // FIXME modal dialog has conflict with MessageHandler
71  //m_pdialog->setWindowModality(Qt::WindowModal);
72  m_pdialog->setWindowModality(Qt::NonModal);
73  m_pdialog->setMinimum(0);
74  m_pdialog->setMaximum(100);
75  m_pdialog->setValue(0);
76  m_pdialog->setCancelButton(m_pcancelButton);
77 
78  this->setTitle(title);
79  this->setMessage(message);
80 
81  m_pdialog->show();
82  }
83 }
84 
85 //------------------------------------------------------------------------------
86 
87 ProgressDialog::~ProgressDialog()
88 {
89  QObject::disconnect( m_pcancelButton, SIGNAL(clicked()), this, SLOT(cancelPressed()) );
90 
91  this->setTitle("");
92  this->setMessage("");
93 
94  if (m_pdialog)
95  {
96  m_pdialog->hide();
97  delete m_pdialog;
98  }
99  else if ( m_pprogressbar )
100  {
101  m_pmainWindow->statusBar()->removeWidget( m_pprogressbar );
102  m_pmainWindow->statusBar()->removeWidget( m_pcancelButton );
103  m_pcancelButton->hide();
104  delete m_pcancelButton;
105  m_pprogressbar->hide();
106  delete m_pprogressbar;
107 
108  }
109 
110  m_pmainWindow = 0;
111 }
112 
113 //------------------------------------------------------------------------------
114 
115 void ProgressDialog::operator()(float percent, std::string msg)
116 {
117  SLM_ASSERT("m_pdialog or m_pprogressbar not instanced", m_pprogressbar || m_pdialog);
118  int value = (int)(percent*100);
119  if(value != this->m_value)
120  {
121  OSLM_TRACE( "ProgressDialog msg" << msg << " : " << value <<"%");
122  this->setMessage(msg);
123 
124  if ( m_pprogressbar )
125  {
126  m_pprogressbar->setValue(value);
127  }
128  else if ( m_pdialog )
129  {
130  m_pdialog->setValue(value);
131  }
132 
133  if ( m_processUserEvents )
134  {
135  QCoreApplication::processEvents(QEventLoop::AllEvents);
136  }
137  else
138  {
139  QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
140  }
141  this->m_value = value;
142  }
143 }
144 
145 //------------------------------------------------------------------------------
146 
147 
148 void ProgressDialog::setTitle(const std::string &title)
149 {
150  m_title = QString::fromStdString(title);
151  if ( m_pprogressbar )
152  {
153  m_pmainWindow->statusBar()->showMessage(m_title);
154  }
155  else if ( m_pdialog )
156  {
157  m_pdialog->setWindowTitle(m_title);
158  }
159 }
160 
161 //------------------------------------------------------------------------------
162 
163 void ProgressDialog::setMessage(const std::string &msg)
164 {
165  QString message("");
166  if (!m_title.isEmpty())
167  {
168  message += m_title;
169  message += " - ";
170  }
171 
172  message += QString::fromStdString(msg);
173  if ( m_pprogressbar )
174  {
175  m_pmainWindow->statusBar()->showMessage(message);
176  }
177  else if ( m_pdialog )
178  {
179  m_pdialog->setLabelText(message);
180  }
181 }
182 
183 //------------------------------------------------------------------------------
184 
185 void ProgressDialog::cancelPressed()
186 {
187  IProgressDialog::cancelPressed();
188 }
189 
190 //------------------------------------------------------------------------------
191 void ProgressDialog::hideCancelButton()
192 {
193  m_pcancelButton->hide();
194 }
195 
196 } // namespace dialog
197 } // namespace fwGuiQt
FWGUI_API void setTitle(const std::string &title) override
set the title for the dialog
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
This class allows us to select an acquisition in a patient data base.
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
static FWGUI_API const FactoryRegistryKeyType REGISTRY_KEY
this unique key should be used for all factory for specific LocationDialog(qt,wx,...)
The namespace fwGuiQt contains classes which provide the implementation of the Gui using Qt library...
Definition: WindowLevel.hpp:32
FWGUI_API void setMessage(const std::string &message) override
set the message for the dialog
FWGUI_API void operator()(float percent, std::string msg) override
action called by fwTools::ProgressAdviser
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
static FWGUI_API::fwGui::container::fwContainer::sptr getProgressWidget()
Get widget defined for progress bar.
Definition: IFrameSrv.cpp:292
int m_value
progress bar&#39;s current value: [0-100]