Ran am2cmake.
[basket4.git] / src / filter.cpp
blob42426409f8b5e7466594ab274303b60d7f973cfd
1 /***************************************************************************
2 * Copyright (C) 2003 by S�astien Laot *
3 * slaout@linux62.org *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <qlayout.h>
22 //#include <ktoolbarbutton.h>
23 #include <qtoolbutton.h>
24 #include <qlabel.h>
25 //Added by qt3to4:
26 #include <Q3HBoxLayout>
27 //#include <qcombobox.h>
28 //#include <klineedit.h>
29 #include <kiconloader.h>
30 #include <klocale.h>
31 #include <kglobalsettings.h>
32 #include <kapplication.h>
33 #include <kiconloader.h>
34 #include <qpixmap.h>
35 #include <qimage.h>
36 #include <qpainter.h>
37 #include <qbitmap.h>
38 #include <kdialogbase.h>
40 #include "filter.h"
41 //#include "settings.h"
42 #include "global.h"
43 #include "bnpview.h"
44 #include "tools.h"
45 #include "tag.h"
46 #include "focusedwidgets.h"
48 /** FilterBar */
50 FilterBar::FilterBar(QWidget *parent, const char *name)
51 : QWidget(parent, name)/*, m_blinkTimer(this), m_blinkedTimes(0)*/
53 Q3HBoxLayout *hBox = new Q3HBoxLayout(this, /*margin*/0, /*spacing*/0);
55 // Create every widgets:
56 QIcon resetIconSet = kapp->iconLoader()->loadIconSet("locationbar_erase", KIcon::Toolbar);
57 QIcon inAllIconSet = kapp->iconLoader()->loadIconSet("find", KIcon::Toolbar);
60 m_resetButton = new QToolButton(this);
61 m_resetButton->setIconSet(resetIconSet);
62 m_resetButton->setTextLabel(i18n("Reset Filter"));//, /*groupText=*/"", this, SLOT(reset()), 0);
63 m_resetButton->setAutoRaise(true);
64 //new KToolBarButton("locationbar_erase", /*id=*/1230, this, /*name=*/0, i18n("Reset Filter"));
65 m_lineEdit = new FocusedLineEdit(this);
66 QLabel *label = new QLabel(m_lineEdit, i18n("&Filter: "), this);
67 m_tagsBox = new FocusedComboBox(this);
68 QLabel *label2 = new QLabel(m_tagsBox, i18n("T&ag: "), this);
69 m_inAllBasketsButton = new QToolButton(this);
70 m_inAllBasketsButton->setIconSet(inAllIconSet);
71 m_inAllBasketsButton->setTextLabel(i18n("Filter all Baskets"));//, /*groupText=*/"", this, SLOT(inAllBaskets()), 0);
72 m_inAllBasketsButton->setAutoRaise(true);
74 // Configure the Reset button:
75 m_resetButton->setEnabled(false);
77 // Configure the Tags combobox:
78 repopulateTagsComnbo();
80 // Configure the Serach in all Baskets button:
81 m_inAllBasketsButton->setToggleButton(true);
82 // m_inAllBasketsButton->setOn(true);
83 // Global::bnpView->toggleFilterAllBaskets(true);
85 // m_lineEdit->setMaximumWidth(150);
87 // Layout all those widgets:
88 // hBox->addStretch();
89 hBox->addWidget(m_resetButton);
90 hBox->addSpacing(KDialogBase::spacingHint());
91 hBox->addWidget(label);
92 hBox->addWidget(m_lineEdit);
93 hBox->addSpacing(KDialogBase::spacingHint());
94 hBox->addWidget(label2);
95 hBox->addWidget(m_tagsBox);
96 hBox->addSpacing(KDialogBase::spacingHint());
97 hBox->addWidget(m_inAllBasketsButton);
99 m_data = new FilterData(); // TODO: Not a pointer! and return a const & !!
101 // connect( &m_blinkTimer, SIGNAL(timeout()), this, SLOT(blinkBar()) );
102 connect( m_resetButton, SIGNAL(clicked()), this, SLOT(reset()) );
103 connect( m_lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged(const QString&)) );
104 connect( m_tagsBox, SIGNAL(activated(int)), this, SLOT(tagChanged(int)) );
106 // connect( m_inAllBasketsButton, SIGNAL(clicked()), this, SLOT(inAllBaskets()) );
107 connect( m_inAllBasketsButton, SIGNAL(toggled(bool)), Global::bnpView, SLOT(toggleFilterAllBaskets(bool)) );
109 connect( m_lineEdit, SIGNAL(escapePressed()), this, SIGNAL(escapePressed()) );
110 connect( m_lineEdit, SIGNAL(returnPressed()), this, SIGNAL(returnPressed()) );
111 connect( m_tagsBox, SIGNAL(escapePressed()), this, SIGNAL(escapePressed()) );
112 connect( m_tagsBox, SIGNAL(returnPressed2()), this, SIGNAL(returnPressed()) );
115 FilterBar::~FilterBar()
119 void FilterBar::setFilterAll(bool filterAll)
121 m_inAllBasketsButton->setOn(filterAll);
124 void FilterBar::setFilterData(const FilterData &data)
126 m_lineEdit->setText(data.string);
128 int index = 0;
129 switch (data.tagFilterType) {
130 default:
131 case FilterData::DontCareTagsFilter: index = 0; break;
132 case FilterData::NotTaggedFilter: index = 1; break;
133 case FilterData::TaggedFilter: index = 2; break;
134 case FilterData::TagFilter: filterTag(data.tag); return;
135 case FilterData::StateFilter: filterState(data.state); return;
138 if (m_tagsBox->currentItem() != index) {
139 m_tagsBox->setCurrentItem(index);
140 tagChanged(index);
144 void FilterBar::repopulateTagsComnbo()
146 static const int ICON_SIZE = 16;
148 m_tagsBox->clear();
149 m_tagsMap.clear();
150 m_statesMap.clear();
152 m_tagsBox->insertItem("", 0);
153 m_tagsBox->insertItem(i18n("(Not tagged)"), 1);
154 m_tagsBox->insertItem(i18n("(Tagged)"), 2);
156 int index = 3;
157 Tag *tag;
158 State *state;
159 QString icon;
160 QString text;
161 QPixmap emblem;
162 for (Tag::List::iterator it = Tag::all.begin(); it != Tag::all.end(); ++it) {
163 tag = *it;
164 state = tag->states().first();
165 // Insert the tag in the combo-box:
166 if (tag->countStates() > 1) {
167 text = tag->name();
168 icon = "";
169 } else {
170 text = state->name();
171 icon = state->emblem();
173 emblem = kapp->iconLoader()->loadIcon(icon, KIcon::Desktop, ICON_SIZE, KIcon::DefaultState, 0L, /*canReturnNull=*/true);
174 m_tagsBox->insertItem(emblem, text, index);
175 // Update the mapping:
176 m_tagsMap.insert(index, tag);
177 ++index;
178 // Insert sub-states, if needed:
179 if (tag->countStates() > 1) {
180 for (State::List::iterator it2 = tag->states().begin(); it2 != tag->states().end(); ++it2) {
181 state = *it2;
182 // Insert the state:
183 text = state->name();
184 icon = state->emblem();
185 emblem = kapp->iconLoader()->loadIcon(icon, KIcon::Desktop, ICON_SIZE, KIcon::DefaultState, 0L, /*canReturnNull=*/true);
186 // Indent the emblem to show the hierarchy relation:
187 if (!emblem.isNull())
188 emblem = Tools::indentPixmap(emblem, /*depth=*/1, /*deltaX=*/2 * ICON_SIZE / 3);
189 m_tagsBox->insertItem(emblem, text, index);
190 // Update the mapping:
191 m_statesMap.insert(index, state);
192 ++index;
198 void FilterBar::reset()
200 m_lineEdit->setText(""); // m_data->isFiltering will be set to false;
201 if (m_tagsBox->currentItem() != 0) {
202 m_tagsBox->setCurrentItem(0);
203 tagChanged(0);
207 void FilterBar::filterTag(Tag *tag)
209 int index = 0;
211 for (QMap<int, Tag*>::Iterator it = m_tagsMap.begin(); it != m_tagsMap.end(); ++it)
212 if (it.data() == tag) {
213 index = it.key();
214 break;
216 if (index <= 0)
217 return;
219 if (m_tagsBox->currentItem() != index) {
220 m_tagsBox->setCurrentItem(index);
221 tagChanged(index);
225 void FilterBar::filterState(State *state)
227 int index = 0;
229 for (QMap<int, State*>::Iterator it = m_statesMap.begin(); it != m_statesMap.end(); ++it)
230 if (it.data() == state) {
231 index = it.key();
232 break;
234 if (index <= 0)
235 return;
237 if (m_tagsBox->currentItem() != index) {
238 m_tagsBox->setCurrentItem(index);
239 tagChanged(index);
243 void FilterBar::inAllBaskets()
245 // TODO!
248 void FilterBar::setEditFocus()
250 m_lineEdit->setFocus();
253 bool FilterBar::hasEditFocus()
255 return m_lineEdit->hasFocus();
258 const FilterData& FilterBar::filterData()
260 return *m_data;
263 void FilterBar::textChanged(const QString &text)
265 m_data->string = text;
266 m_data->isFiltering = (!m_data->string.isEmpty() || m_data->tagFilterType != FilterData::DontCareTagsFilter);
267 m_resetButton->setEnabled(m_data->isFiltering);
268 emit newFilter(*m_data);
271 void FilterBar::tagChanged(int index)
273 m_data->tag = 0;
274 m_data->state = 0;
275 switch (index) {
276 case 0:
277 m_data->tagFilterType = FilterData::DontCareTagsFilter;
278 break;
279 case 1:
280 m_data->tagFilterType = FilterData::NotTaggedFilter;
281 break;
282 case 2:
283 m_data->tagFilterType = FilterData::TaggedFilter;
284 break;
285 default:
286 // Try to find if we are filtering a tag:
287 QMapIterator<int, Tag*> it = m_tagsMap.find(index);
288 if (it != m_tagsMap.end()) {
289 m_data->tagFilterType = FilterData::TagFilter;
290 m_data->tag = *it;
291 } else {
292 // If not, try to find if we are filtering a state:
293 QMapIterator<int, State*> it2 = m_statesMap.find(index);
294 if (it2 != m_statesMap.end()) {
295 m_data->tagFilterType = FilterData::StateFilter;
296 m_data->state = *it2;
297 } else {
298 // If not (should never happens), do as if the tags filter was reseted:
299 m_data->tagFilterType = FilterData::DontCareTagsFilter;
302 break;
304 m_data->isFiltering = (!m_data->string.isEmpty() || m_data->tagFilterType != FilterData::DontCareTagsFilter);
305 m_resetButton->setEnabled(m_data->isFiltering);
306 emit newFilter(*m_data);
309 #include "filter.moc"