Fraxinus  18.10
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
13 #include <QtWidgets>
14 
15 #include "cxTimedAlgorithm.h"
16 
17 #include "cxDisplayTimerWidget.h"
18 
19 namespace cx
20 {
21 
23  mShowTextLabel(true)
24 {
25  QHBoxLayout* layout = new QHBoxLayout(this);
26  layout->setMargin(0);
27 
28  mLabel = new QLabel;
29  mLabel->hide();
30  layout->addWidget(mLabel);
31 
32  mTimerWidget = new DisplayTimerWidget(this);
33  mTimerWidget->setFontSize(3);
34  mTimerWidget->hide();
35  layout->addWidget(mTimerWidget);
36 
37  mProgressBar = new QProgressBar;
38  mProgressBar->hide();
39  layout->addWidget(mProgressBar);
40 }
41 
42 void TimedAlgorithmProgressBar::attach(std::set<cx::TimedAlgorithmPtr> threads)
43 {
44  std::set<cx::TimedAlgorithmPtr>::iterator iter;
45  for(iter=threads.begin(); iter!=threads.end(); ++iter)
46  this->attach(*iter);
47 }
48 
49 void TimedAlgorithmProgressBar::detach(std::set<cx::TimedAlgorithmPtr> threads)
50 {
51  std::set<cx::TimedAlgorithmPtr>::iterator iter;
52  for(iter=threads.begin(); iter!=threads.end(); ++iter)
53  this->detach(*iter);
54 }
55 
57 {
58  if (mAlgorithm.count(algorithm))
59  return;
60 
61  if (algorithm)
62  {
63  connect(algorithm.get(), SIGNAL(started(int)), this, SLOT(algorithmStartedSlot(int)));
64  connect(algorithm.get(), SIGNAL(finished()), this, SLOT(algorithmFinishedSlot()));
65  connect(algorithm.get(), SIGNAL(productChanged()), this, SLOT(productChangedSlot()));
66  }
67 
68  mAlgorithm.insert(algorithm);
69 }
70 
72 {
73  if (!mAlgorithm.count(algorithm))
74  return;
75 
76  if (algorithm)
77  {
78  disconnect(algorithm.get(), SIGNAL(started(int)), this, SLOT(algorithmStartedSlot(int)));
79  disconnect(algorithm.get(), SIGNAL(finished()), this, SLOT(algorithmFinishedSlot()));
80  disconnect(algorithm.get(), SIGNAL(productChanged()), this, SLOT(productChangedSlot()));
81  this->algorithmFinished(algorithm.get());
82  }
83 
84  mAlgorithm.erase(algorithm);
85 }
86 
87 void TimedAlgorithmProgressBar::productChangedSlot()
88 {
89  TimedBaseAlgorithm* algo = dynamic_cast<TimedBaseAlgorithm*>(sender());
90  QString product = "algorithm";
91  if (algo)
92  product = algo->getProduct();
93 
94  mLabel->setText(product);
95 }
96 
98 {
99  mShowTextLabel = on;
100  if (mLabel->isVisible())
101  mLabel->setVisible(on);
102 }
103 
104 void TimedAlgorithmProgressBar::algorithmStartedSlot(int maxSteps)
105 {
106  TimedBaseAlgorithm* algo = dynamic_cast<TimedBaseAlgorithm*>(sender());
107  QString product = "algorithm";
108  if (algo)
109  product = algo->getProduct();
110 
111  mLabel->setText(product);
112  mLabel->setVisible(mShowTextLabel);
113  mStartedAlgos.insert(algo);
114 
115  mTimerWidget->show();
116  mTimerWidget->start();
117 
118  mProgressBar->setRange(0, maxSteps);
119  mProgressBar->setValue(0);
120  mProgressBar->show();
121 }
122 
123 void TimedAlgorithmProgressBar::algorithmFinishedSlot()
124 {
125  TimedBaseAlgorithm* algo = dynamic_cast<TimedBaseAlgorithm*>(sender());
126  this->algorithmFinished(algo);
127 }
128 
129 void TimedAlgorithmProgressBar::algorithmFinished(TimedBaseAlgorithm* algo)
130 {
131  QString product = "algorithm";
132  if (algo)
133  product = algo->getProduct();
134 
135  mStartedAlgos.erase(algo);
136  if (!mStartedAlgos.empty())
137  return;
138 
139  mProgressBar->setValue(0);
140  mProgressBar->hide();
141  mLabel->hide();
142 
143  mTimerWidget->hide();
144  mTimerWidget->stop();
145 }
146 
147 }
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.
Namespace for all CustusX production code.