CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxFiltersWidget.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 
33 #include "cxFiltersWidget.h"
34 
35 #include "cxStringProperty.h"
36 
37 
40 #include "cxLogger.h"
41 #include "cxDummyFilter.h"
44 #include "cxContourFilter.h"
45 #include "cxSmoothingImageFilter.h"
46 #include "cxResampleImageFilter.h"
47 #include "cxFilterPresetWidget.h"
48 #include "cxDilationFilter.h"
49 #include "cxPluginFramework.h"
50 #include "cxLogicManager.h"
51 #include <boost/bind.hpp>
52 #include <boost/shared_ptr.hpp>
53 #include <boost/shared_ptr.hpp>
54 #include "cxNullDeleter.h"
55 #include "cxProfile.h"
56 
57 namespace cx {
58 
59 FiltersWidget::FiltersWidget(VisServicesPtr services, QWidget* parent, QStringList wantedFilters, QString optionfileTag) :
60  BaseWidget(parent, "algorithm_widgets_configurable_filter", "All Filters"),
61  mWantedFilters(wantedFilters)
62 {
63  XmlOptionFile options = profile()->getXmlSettings().descend(optionfileTag);
64  mFilters.reset(new FilterGroup(options));
65 
66  this->appendFilters(services);
67 
68  this->setWindowTitleAndObjectNameBasedOnWantedFilters();
69 
70  this->configureFilterSelector(options);
71 
72  this->setupLayout(services, options);
73 
74  this->filterChangedSlot();
75 }
76 
78 {
79  return QString("<html>"
80  "<h3>Filter Widget.</h3>"
81  "<p>Select one type of filter.</p>"
82  "<p><i>Currently selected filter:</i></p>"
83  "<p>%1</p>"
84  "</html>").arg(mSetupWidget->generateHelpText());
85 }
86 
87 void FiltersWidget::filterChangedSlot()
88 {
89  for (unsigned i=0; i<mFilters->size(); ++i)
90  {
91  if (mFilters->get(i)->getUid() == mFilterSelector->getValue())
92  {
93  mCurrentFilter = mFilters->get(i);
94  }
95  }
96 
97  mSetupWidget->setFilter(mCurrentFilter);
98  mFilterSelector->setHelp(this->generateHelpText());
99 }
100 
101 void FiltersWidget::toggleDetailsSlot()
102 {
103  mSetupWidget->toggleDetailed();
104 
105 }
106 
107 void FiltersWidget::runFilterSlot()
108 {
109  if (!mCurrentFilter)
110  return;
111  if (mThread)
112  {
113  reportWarning(QString("Last operation on %1 is not finished. Could not start filtering").arg(mThread->getFilter()->getName()));
114  return;
115  }
116 
117  mThread.reset(new FilterTimedAlgorithm(mCurrentFilter));
118  connect(mThread.get(), SIGNAL(finished()), this, SLOT(finishedSlot()));
119  mTimedAlgorithmProgressBar->attach(mThread);
120 
121  mThread->execute();
122 }
123 
124 void FiltersWidget::finishedSlot()
125 {
126  mTimedAlgorithmProgressBar->detach(mThread);
127  disconnect(mThread.get(), SIGNAL(finished()), this, SLOT(finishedSlot()));
128  mThread.reset();
129 }
130 
131 void FiltersWidget::onServiceAdded(Filter* service)
132 {
133  this->appendFilterIfWanted(FilterPtr(service, null_deleter()));
134 }
135 
136 void FiltersWidget::onServiceRemoved(Filter *service)
137 {
138  mFilters->remove(service);
139 }
140 
141 void FiltersWidget::appendFiltersThatAreNotServices(VisServicesPtr services)
142 {
143  this->appendFilterIfWanted(FilterPtr(new BinaryThresholdImageFilter(services)));
144  this->appendFilterIfWanted(FilterPtr(new BinaryThinningImageFilter3DFilter(services)));
145  this->appendFilterIfWanted(FilterPtr(new ContourFilter(services)));
146  this->appendFilterIfWanted(FilterPtr(new SmoothingImageFilter(services)));
147  this->appendFilterIfWanted(FilterPtr(new ResampleImageFilter(services)));
148  this->appendFilterIfWanted(FilterPtr(new DilationFilter(services)));
149 }
150 
151 void FiltersWidget::appendFilterServices()
152 {
153  mServiceListener.reset(
154  new ServiceTrackerListener<Filter>(
155  LogicManager::getInstance()->getPluginContext(),
156  boost::bind(&FiltersWidget::onServiceAdded, this, _1),
157  boost::function<void(Filter*)>(),
158  boost::bind(&FiltersWidget::onServiceRemoved, this, _1)));
159  mServiceListener->open();
160 }
161 
162 void FiltersWidget::appendFilters(VisServicesPtr services)
163 {
164  this->appendFiltersThatAreNotServices(services);
165  this->appendFilterServices();
166 }
167 
168 void FiltersWidget::appendFilterIfWanted(FilterPtr filter)
169 {
170  if(mWantedFilters.empty() || mWantedFilters.contains(filter->getName()))
171  mFilters->append(filter);
172 }
173 
174 void FiltersWidget::configureFilterSelector(XmlOptionFile options)
175 {
176  QStringList availableFilters;
177  std::map<QString,QString> names;
178  for (unsigned i=0; i<mFilters->size(); ++i)
179  {
180  availableFilters << mFilters->get(i)->getUid();
181  names[mFilters->get(i)->getUid()] = mFilters->get(i)->getName();
182  }
183  if(availableFilters.isEmpty())
184  {
185  availableFilters << "FILTER NOT FOUND";
186  }
187  mFilterSelector = StringProperty::initialize("filterSelector",
188  "Filter",
189  "Select which filter to use.",
190  availableFilters[0],
191  availableFilters,
192  options.getElement());
193  mFilterSelector->setDisplayNames(names);
194  connect(mFilterSelector.get(), &StringProperty::valueWasSet, this, &FiltersWidget::filterChangedSlot);
195 }
196 
197 void FiltersWidget::addDetailedButton(QHBoxLayout* filterLayout)
198 {
199  QAction* detailsAction = this->createAction(this,
200  QIcon(":/icons/open_icon_library/system-run-5.png"),
201  "Details", "Toggle Details",
202  SLOT(toggleDetailsSlot()),
203  NULL);
204 
205  QToolButton* detailsButton = new QToolButton();
206  detailsButton->setObjectName("DetailedButton");
207  detailsButton->setDefaultAction(detailsAction);
208  filterLayout->addWidget(detailsButton);
209 }
210 
211 void FiltersWidget::addRunButton(QHBoxLayout* filterLayout)
212 {
213  QAction* runAction = this->createAction(this,
214  QIcon(":/icons/open_icon_library/arrow-right-3.png"),
215  "Run Filter", "",
216  SLOT(runFilterSlot()),
217  NULL);
218 
219  CXSmallToolButton* button = new CXSmallToolButton();
220  button->setObjectName("RunFilterButton");
221  button->setDefaultAction(runAction);
222  filterLayout->addWidget(button);
223 }
224 
225 QHBoxLayout * FiltersWidget::addFilterSelector(QVBoxLayout* topLayout)
226 {
227  QHBoxLayout* filterLayout = new QHBoxLayout;
228  topLayout->addLayout(filterLayout);
229  LabeledComboBoxWidget* filterSelectorWidget = new LabeledComboBoxWidget(this, mFilterSelector);
230  filterSelectorWidget->showLabel(false);
231  filterLayout->addWidget(filterSelectorWidget);
232 
233  return filterLayout;
234 }
235 
236 void FiltersWidget::addProgressBar(QVBoxLayout* topLayout)
237 {
238  mTimedAlgorithmProgressBar = new cx::TimedAlgorithmProgressBar;
239  topLayout->addWidget(mTimedAlgorithmProgressBar);
240 }
241 
242 void FiltersWidget::addFilterWidget(XmlOptionFile options, VisServicesPtr services, QVBoxLayout* topLayout)
243 {
244  mSetupWidget = new FilterSetupWidget(services, this, options, false);
245  topLayout->addWidget(mSetupWidget);
246 }
247 
248 void FiltersWidget::setWindowTitleAndObjectNameBasedOnWantedFilters()
249 {
250  if(mWantedFilters.size() == 1)
251  {
252  this->setWindowTitle(mWantedFilters.first());
253  this->setObjectName(mWantedFilters.first()+" Widget");
254  }
255  else if(mWantedFilters.empty())
256  {
257  this->setWindowTitle("All Filters");
258  this->setObjectName("algorithm_widgets_configurable_filter");
259  }
260 }
261 
262 void FiltersWidget::setupLayout(VisServicesPtr services, XmlOptionFile options)
263 {
264  QVBoxLayout* topLayout = new QVBoxLayout(this);
265  QHBoxLayout* filterLayout = addFilterSelector(topLayout);
266  this->addDetailedButton(filterLayout);
267  this->addRunButton(filterLayout);
268  this->addProgressBar(topLayout);
269  this->addFilterWidget(options, services, topLayout);
270  topLayout->addStretch();
271 }
272 
273 } /* namespace cx */
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:176
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:62
FiltersWidget(VisServicesPtr services, QWidget *parent, QStringList wantedFilters=QStringList(), QString optionfileTag="filterwidget")
FiltersWidget Widget for displaying N image filters.
Composite widget for string selection.
Show progress for a TimedBaseAlgorithm.
void addRunButton(QHBoxLayout *filterLayout)
QAction * createAction(QObject *parent, QIcon iconName, QString text, QString tip, T slot, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:149
void detach(TimedAlgorithmPtr algorithm)
boost::shared_ptr< class Filter > FilterPtr
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
static LogicManager * getInstance()
QString generateHelpText() const
void setFilter(FilterPtr filter)
void attach(TimedAlgorithmPtr algorithm)
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:108
QString generateHelpText() const
Helper class for xml files used to store ssc/cx data.