Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxTimedAlgorithmProgressBar.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
34 #include <QtWidgets>
35 
36 #include "cxTimedAlgorithm.h"
37 
38 #include "cxDisplayTimerWidget.h"
39 
40 namespace cx
41 {
42 
44  mShowTextLabel(true)
45 {
46  QHBoxLayout* layout = new QHBoxLayout(this);
47  layout->setMargin(0);
48 
49  mLabel = new QLabel;
50  mLabel->hide();
51  layout->addWidget(mLabel);
52 
53  mTimerWidget = new DisplayTimerWidget(this);
54  mTimerWidget->setFontSize(3);
55  mTimerWidget->hide();
56  layout->addWidget(mTimerWidget);
57 
58  mProgressBar = new QProgressBar;
59  mProgressBar->hide();
60  layout->addWidget(mProgressBar);
61 }
62 
63 void TimedAlgorithmProgressBar::attach(std::set<cx::TimedAlgorithmPtr> threads)
64 {
65  std::set<cx::TimedAlgorithmPtr>::iterator iter;
66  for(iter=threads.begin(); iter!=threads.end(); ++iter)
67  this->attach(*iter);
68 }
69 
70 void TimedAlgorithmProgressBar::detach(std::set<cx::TimedAlgorithmPtr> threads)
71 {
72  std::set<cx::TimedAlgorithmPtr>::iterator iter;
73  for(iter=threads.begin(); iter!=threads.end(); ++iter)
74  this->detach(*iter);
75 }
76 
78 {
79  if (mAlgorithm.count(algorithm))
80  return;
81 
82  if (algorithm)
83  {
84  connect(algorithm.get(), SIGNAL(started(int)), this, SLOT(algorithmStartedSlot(int)));
85  connect(algorithm.get(), SIGNAL(finished()), this, SLOT(algorithmFinishedSlot()));
86  connect(algorithm.get(), SIGNAL(productChanged()), this, SLOT(productChangedSlot()));
87  }
88 
89  mAlgorithm.insert(algorithm);
90 }
91 
93 {
94  if (!mAlgorithm.count(algorithm))
95  return;
96 
97  if (algorithm)
98  {
99  disconnect(algorithm.get(), SIGNAL(started(int)), this, SLOT(algorithmStartedSlot(int)));
100  disconnect(algorithm.get(), SIGNAL(finished()), this, SLOT(algorithmFinishedSlot()));
101  disconnect(algorithm.get(), SIGNAL(productChanged()), this, SLOT(productChangedSlot()));
102  this->algorithmFinished(algorithm.get());
103  }
104 
105  mAlgorithm.erase(algorithm);
106 }
107 
108 void TimedAlgorithmProgressBar::productChangedSlot()
109 {
110  TimedBaseAlgorithm* algo = dynamic_cast<TimedBaseAlgorithm*>(sender());
111  QString product = "algorithm";
112  if (algo)
113  product = algo->getProduct();
114 
115  mLabel->setText(product);
116 }
117 
119 {
120  mShowTextLabel = on;
121  if (mLabel->isVisible())
122  mLabel->setVisible(on);
123 }
124 
125 void TimedAlgorithmProgressBar::algorithmStartedSlot(int maxSteps)
126 {
127  TimedBaseAlgorithm* algo = dynamic_cast<TimedBaseAlgorithm*>(sender());
128  QString product = "algorithm";
129  if (algo)
130  product = algo->getProduct();
131 
132  mLabel->setText(product);
133  mLabel->setVisible(mShowTextLabel);
134  mStartedAlgos.insert(algo);
135 
136  mTimerWidget->show();
137  mTimerWidget->start();
138 
139  mProgressBar->setRange(0, maxSteps);
140  mProgressBar->setValue(0);
141  mProgressBar->show();
142 }
143 
144 void TimedAlgorithmProgressBar::algorithmFinishedSlot()
145 {
146  TimedBaseAlgorithm* algo = dynamic_cast<TimedBaseAlgorithm*>(sender());
147  this->algorithmFinished(algo);
148 }
149 
150 void TimedAlgorithmProgressBar::algorithmFinished(TimedBaseAlgorithm* algo)
151 {
152  QString product = "algorithm";
153  if (algo)
154  product = algo->getProduct();
155 
156  mStartedAlgos.erase(algo);
157  if (!mStartedAlgos.empty())
158  return;
159 
160  mProgressBar->setValue(0);
161  mProgressBar->hide();
162  mLabel->hide();
163 
164  mTimerWidget->hide();
165  mTimerWidget->stop();
166 }
167 
168 }
Base class for algorithms that wants to time their execution.
void setFontSize(int fontSize)
void detach(TimedAlgorithmPtr algorithm)
boost::shared_ptr< class TimedBaseAlgorithm > TimedAlgorithmPtr
virtual QString getProduct() const
void attach(TimedAlgorithmPtr algorithm)
A second counter widget.