11 #include "FilterGroup.h"
13 #include <QButtonGroup>
15 #include <QPushButton>
16 #include <QHBoxLayout>
19 #include <QRadioButton>
21 #include <QVBoxLayout>
23 #include "IException.h"
27 #include "AbstractFilterSelector.h"
28 #include "ImageImageFilterSelector.h"
29 #include "PointMeasureFilterSelector.h"
30 #include "ImagePointFilterSelector.h"
37 FilterGroup::FilterGroup(QString type) {
40 m_filterType =
new QString(type);
47 FilterGroup::FilterGroup(
const FilterGroup &other) {
50 m_filterType =
new QString(*other.m_filterType);
54 foreach (AbstractFilterSelector * selector, *other.m_selectors) {
55 AbstractFilterSelector *newSelector = NULL;
56 if (*m_filterType ==
"Points and Measures") {
57 newSelector =
new PointMeasureFilterSelector(
58 *((PointMeasureFilterSelector *) selector));
61 if (*m_filterType ==
"Images and Points") {
62 newSelector =
new ImagePointFilterSelector(
63 *((ImagePointFilterSelector *) selector));
66 if (*m_filterType ==
"Connections") {
67 newSelector =
new ImageImageFilterSelector(
68 *((ImageImageFilterSelector *) selector));
73 addSelector(newSelector);
76 m_buttonGroup->button(other.m_buttonGroup->checkedId())->click();
80 FilterGroup::~FilterGroup() {
98 bool FilterGroup::hasFilter(
bool (AbstractFilter::*meth)()
const)
const {
101 for (
int i = 0; !found && i < m_selectors->size(); i++)
103 found = m_selectors->at(i)->hasFilter(meth);
105 found = m_selectors->at(i)->hasFilter();
111 void FilterGroup::nullify() {
112 m_buttonGroup = NULL;
113 m_closeButton = NULL;
114 m_groupBoxLayout = NULL;
119 void FilterGroup::init() {
122 QLabel *descriptionLabel =
new QLabel(
"Combine filters using logic "
124 QFont descriptionFont(
"SansSerif", 11);
125 descriptionLabel->setFont(descriptionFont);
126 QFont logicTypeFont(
"SansSerif", 12, QFont::Bold);
127 QRadioButton *andButton =
new QRadioButton(
"and");
128 andButton->setFont(logicTypeFont);
129 QRadioButton *orButton =
new QRadioButton(
"or");
130 orButton->setFont(logicTypeFont);
131 m_buttonGroup =
new QButtonGroup;
132 m_buttonGroup->addButton(andButton, 0);
133 m_buttonGroup->addButton(orButton, 1);
134 connect(m_buttonGroup, SIGNAL(buttonClicked(
int)),
this,
135 SLOT(changeFilterCombinationLogic(
int)));
140 QHBoxLayout *logicLayout =
new QHBoxLayout;
141 QMargins margins = logicLayout->contentsMargins();
142 margins.setBottom(4);
143 logicLayout->setContentsMargins(margins);
144 logicLayout->addStretch();
145 logicLayout->addWidget(descriptionLabel);
146 logicLayout->addWidget(andButton);
147 logicLayout->addWidget(orButton);
148 logicLayout->addStretch();
150 m_logicWidget->setLayout(logicLayout);
152 m_newSelectorButton =
new QPushButton;
153 m_newSelectorButton->setIcon(QIcon(
154 FileName(
"$ISISROOT/appdata/images/icons/add.png").expanded()));
155 QString newSelectorTooltip =
"Add new filter";
156 m_newSelectorButton->setToolTip(newSelectorTooltip);
157 m_newSelectorButton->setStatusTip(newSelectorTooltip);
159 connect(m_newSelectorButton, SIGNAL(clicked()),
this, SLOT(addSelector()));
160 QHBoxLayout *newSelectorLayout =
new QHBoxLayout;
161 newSelectorLayout->addWidget(m_newSelectorButton);
162 newSelectorLayout->addStretch();
164 m_groupBoxLayout =
new QVBoxLayout;
165 m_groupBoxLayout->addWidget(m_logicWidget);
166 m_groupBoxLayout->addLayout(newSelectorLayout);
167 QGroupBox *groupBox =
new QGroupBox;
168 groupBox->setLayout(m_groupBoxLayout);
170 QVBoxLayout *mainLayout =
new QVBoxLayout;
171 margins = mainLayout->contentsMargins();
173 margins.setBottom(2);
174 mainLayout->setContentsMargins(margins);
175 mainLayout->addWidget(groupBox);
177 setLayout(mainLayout);
181 void FilterGroup::addSelector() {
182 AbstractFilterSelector *filterSelector = NULL;
183 if (*m_filterType ==
"Points and Measures")
184 filterSelector =
new PointMeasureFilterSelector;
185 else if (*m_filterType ==
"Images and Points")
186 filterSelector =
new ImagePointFilterSelector;
187 else if (*m_filterType ==
"Connections")
188 filterSelector =
new ImageImageFilterSelector;
190 addSelector(filterSelector);
194 void FilterGroup::addSelector(AbstractFilterSelector *newSelector) {
198 connect(newSelector, SIGNAL(close(AbstractFilterSelector *)),
199 this, SLOT(deleteSelector(AbstractFilterSelector *)));
200 connect(newSelector, SIGNAL(filterChanged()),
201 this, SIGNAL(filterChanged()));
202 connect(newSelector, SIGNAL(sizeChanged()),
203 this, SLOT(sendSizeChanged()));
204 m_groupBoxLayout->insertWidget(m_groupBoxLayout->count() - 1, newSelector);
205 m_selectors->append(newSelector);
206 m_selectors->size() > 1 ? m_logicWidget->show() : m_logicWidget->hide();
213 void FilterGroup::deleteSelector(AbstractFilterSelector *filterSelector) {
214 m_groupBoxLayout->removeWidget(filterSelector);
215 delete filterSelector;
216 m_selectors->removeOne(filterSelector);
218 int m_selectorsSize = m_selectors->size();
219 m_selectorsSize > 1 ? m_logicWidget->show() : m_logicWidget->hide();
220 if (!m_selectorsSize)
223 emit filterChanged();
227 void FilterGroup::sendClose() {
232 void FilterGroup::sendSizeChanged() {
233 emit sizeChanged(
this);
237 void FilterGroup::changeFilterCombinationLogic(
int button) {
238 m_andFiltersTogether = button == 0;
239 emit filterChanged();
243 bool FilterGroup::filtersAreAndedTogether()
const {
244 return m_andFiltersTogether;
248 FilterGroup &FilterGroup::operator=(FilterGroup other) {
249 ASSERT(m_filterType);
251 ASSERT(m_buttonGroup);
255 foreach (AbstractFilterSelector * selector, *other.m_selectors) {
256 AbstractFilterSelector *newSelector = NULL;
257 if (*m_filterType ==
"Points and Measures") {
258 newSelector =
new(nothrow) PointMeasureFilterSelector(
259 *((PointMeasureFilterSelector *) selector));
262 if (*m_filterType ==
"Images and Points") {
263 newSelector =
new(nothrow) ImagePointFilterSelector(
264 *((ImagePointFilterSelector *) selector));
267 if (*m_filterType ==
"Connections") {
268 newSelector =
new(nothrow) ImageImageFilterSelector(
269 *((ImageImageFilterSelector *) selector));
275 *newSelector = *selector;
276 newSelectors.append(newSelector);
281 if (newSelectors.size() == other.m_selectors->size()) {
282 foreach (AbstractFilterSelector * selector, *m_selectors) {
283 deleteSelector(selector);
286 foreach (AbstractFilterSelector * newSelector, newSelectors) {
287 addSelector(newSelector);
290 swap(*m_filterType, *other.m_filterType);
291 m_buttonGroup->button(other.m_buttonGroup->checkedId())->click();
295 foreach (AbstractFilterSelector * newSelector, newSelectors) {
302 IString msg =
"Assignment of FilterGroup failed";
310 QString FilterGroup::getDescription(
311 bool (AbstractFilter::*hasFilterMeth)()
const,
312 QString(AbstractFilter::*descriptionMeth)()
const)
const {
318 for (
int i = 0; i < m_selectors->size(); i++)
319 if (m_selectors->at(i)->hasFilter(hasFilterMeth))
320 selectorsWithFilters.append(m_selectors->at(i));
322 int numFilters = selectorsWithFilters.size();
325 QString logic =
"<b> ";
326 if (m_andFiltersTogether)
332 for (
int i = 0; i < numFilters - 1; i++) {
333 description += selectorsWithFilters[i]->getDescription(descriptionMeth)
337 description += selectorsWithFilters[numFilters - 1]->getDescription(