CustusX  15.8
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxAllFiltersWidget.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 "cxAllFiltersWidget.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 
60  BaseWidget(parent, "algorithm_widgets_configurable_filter", "Configurable Filter")
61 {
62  XmlOptionFile options = profile()->getXmlSettings().descend("filterwidget");
63  mFilters.reset(new FilterGroup(options));
64  mFilters->append(FilterPtr(new BinaryThresholdImageFilter(services)));
65  mFilters->append(FilterPtr(new BinaryThinningImageFilter3DFilter(services)));
66  mFilters->append(FilterPtr(new ContourFilter(services)));
67  mFilters->append(FilterPtr(new SmoothingImageFilter(services)));
68  mFilters->append(FilterPtr(new ResampleImageFilter(services)));
69  mFilters->append(FilterPtr(new DilationFilter(services)));
70 
71  mServiceListener.reset(
73  LogicManager::getInstance()->getPluginContext(),
74  boost::bind(&AllFiltersWidget::onServiceAdded, this, _1),
75  boost::function<void(Filter*)>(),
76  boost::bind(&AllFiltersWidget::onServiceRemoved, this, _1)));
77  mServiceListener->open();
78 
79  QStringList availableFilters;
80  std::map<QString,QString> names;
81  for (unsigned i=0; i<mFilters->size(); ++i)
82  {
83  availableFilters << mFilters->get(i)->getUid();
84  names[mFilters->get(i)->getUid()] = mFilters->get(i)->getName();
85  }
86 
87  mFilterSelector = StringProperty::initialize("filterSelector",
88  "Filter",
89  "Select which filter to use.",
90  availableFilters[0],
91  availableFilters,
92  options.getElement());
93  mFilterSelector->setDisplayNames(names);
94  connect(mFilterSelector.get(), &StringProperty::valueWasSet,
95  this, &AllFiltersWidget::filterChangedSlot);
96 
97  QVBoxLayout* topLayout = new QVBoxLayout(this);
98 
99  QHBoxLayout* filterLayout = new QHBoxLayout;
100  topLayout->addLayout(filterLayout);
101 
102  filterLayout->addWidget(new LabeledComboBoxWidget(this, mFilterSelector));
103 
104  //Add detailed button
105  QAction* detailsAction = this->createAction(this,
106  QIcon(":/icons/open_icon_library/system-run-5.png"),
107  "Details", "Show Details",
108  SLOT(toggleDetailsSlot()),
109  NULL);
110 
111  QToolButton* detailsButton = new QToolButton();
112  detailsButton->setObjectName("DetailedButton");
113  detailsButton->setDefaultAction(detailsAction);
114 // editsLayout->addWidget(detailsButton, 0, 2);
115  filterLayout->addWidget(detailsButton);
116 
117  //Add run button
118  QAction* runAction = this->createAction(this,
119  QIcon(":/icons/open_icon_library/arrow-right-3.png"),
120  "Run Filter", "",
121  SLOT(runFilterSlot()),
122  NULL);
123 
124  CXSmallToolButton* button = new CXSmallToolButton();
125  button->setObjectName("RunFilterButton");
126  button->setDefaultAction(runAction);
127  filterLayout->addWidget(button);
128 
129  mTimedAlgorithmProgressBar = new cx::TimedAlgorithmProgressBar;
130  topLayout->addWidget(mTimedAlgorithmProgressBar);
131 
132  mSetupWidget = new FilterSetupWidget(services, this, options, false);
133  topLayout->addWidget(mSetupWidget);
134 
135  topLayout->addStretch();
136 
137  this->filterChangedSlot();
138 }
139 
141 {
142  return QString("<html>"
143  "<h3>Filter Widget.</h3>"
144  "<p>Select one type of filter.</p>"
145  "<p><i>Currently selected filter:</i></p>"
146  "<p>%1</p>"
147  "</html>").arg(mSetupWidget->generateHelpText());
148 }
149 
150 void AllFiltersWidget::filterChangedSlot()
151 {
152  for (unsigned i=0; i<mFilters->size(); ++i)
153  {
154  if (mFilters->get(i)->getUid() == mFilterSelector->getValue())
155  {
156  mCurrentFilter = mFilters->get(i);
157  }
158  }
159 
160  mSetupWidget->setFilter(mCurrentFilter);
161  mFilterSelector->setHelp(this->generateHelpText());
162 }
163 
164 void AllFiltersWidget::toggleDetailsSlot()
165 {
166  mSetupWidget->toggleDetailed();
167 
168 }
169 
170 void AllFiltersWidget::runFilterSlot()
171 {
172  if (!mCurrentFilter)
173  return;
174  if (mThread)
175  {
176  reportWarning(QString("Last operation on %1 is not finished. Could not start filtering")
177  .arg(mThread->getFilter()->getName()));
178  return;
179  }
180 
181  mThread.reset(new FilterTimedAlgorithm(mCurrentFilter));
182  connect(mThread.get(), SIGNAL(finished()), this, SLOT(finishedSlot()));
183  mTimedAlgorithmProgressBar->attach(mThread);
184 
185  mThread->execute();
186 }
187 
188 void AllFiltersWidget::finishedSlot()
189 {
190  mTimedAlgorithmProgressBar->detach(mThread);
191  disconnect(mThread.get(), SIGNAL(finished()), this, SLOT(finishedSlot()));
192  mThread.reset();
193 }
194 
195 void AllFiltersWidget::onServiceAdded(Filter* service)
196 {
197  mFilters->append(FilterPtr(service, null_deleter()));
198 }
199 
200 void AllFiltersWidget::onServiceRemoved(Filter *service)
201 {
202  mFilters->remove(service);
203 }
204 
205 
206 } /* namespace cx */
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:169
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:62
Composite widget for string selection.
Show progress for a TimedBaseAlgorithm.
QDomElement getElement()
return the current element
QAction * createAction(QObject *parent, QIcon iconName, QString text, QString tip, T slot, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:129
void detach(TimedAlgorithmPtr algorithm)
AllFiltersWidget(VisServicesPtr services, QWidget *parent)
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())
Helper class for listening to services being added, modified and removed.
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.