Fairly large overhaul of the JuK codebase to beat out a lot of the Qt 3 stuff.
[kdemultimedia.git] / juk / searchwidget.cpp
blob5298ce10207344d1fdc6788bd85f77550a073d93
1 /***************************************************************************
2 begin : Sun Mar 6 2003
3 copyright : (C) 2003 by Richard Lärkäng
4 email : nouseforaname@home.se
6 copyright : (C) 2003 - 2004 by Scott Wheeler
7 email : wheeler@kde.org
8 ***************************************************************************/
10 /***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
19 #include "searchwidget.h"
20 #include "collectionlist.h"
21 #include "actioncollection.h"
22 #include "searchadaptor.h"
24 #include <klocale.h>
25 #include <klineedit.h>
26 #include <kiconloader.h>
27 #include <kcombobox.h>
28 #include <kdebug.h>
29 #include <kaction.h>
31 #include <QLayout>
32 #include <QLabel>
33 #include <QCheckBox>
34 #include <QPushButton>
35 #include <QToolButton>
36 #include <QKeyEvent>
37 #include <QtDBus>
39 using namespace ActionCollection;
41 ////////////////////////////////////////////////////////////////////////////////
42 // SearchLine public methods
43 ////////////////////////////////////////////////////////////////////////////////
45 SearchLine::SearchLine(QWidget *parent, bool simple, const char *name) :
46 Q3HBox(parent, name),
47 m_simple(simple),
48 m_searchFieldsBox(0)
50 setSpacing(5);
52 if(!m_simple) {
53 m_searchFieldsBox = new KComboBox(this);
54 m_searchFieldsBox->setObjectName( "searchFields" );
55 connect(m_searchFieldsBox, SIGNAL(activated(int)),
56 this, SIGNAL(signalQueryChanged()));
59 m_lineEdit = new KLineEdit(this);
60 m_lineEdit->setObjectName( "searchLineEdit" );
61 m_lineEdit->installEventFilter(this);
62 connect(m_lineEdit, SIGNAL(textChanged(const QString &)),
63 this, SIGNAL(signalQueryChanged()));
64 connect(m_lineEdit, SIGNAL(returnPressed()),
65 this, SLOT(slotActivate()));
67 if(!m_simple) {
68 m_caseSensitive = new KComboBox(this);
69 m_caseSensitive->addItem(i18n("Normal Matching"));
70 m_caseSensitive->addItem(i18n("Case Sensitive"));
71 m_caseSensitive->addItem(i18n("Pattern Matching"));
72 connect(m_caseSensitive, SIGNAL(activated(int)),
73 this, SIGNAL(signalQueryChanged()));
75 else
76 m_caseSensitive = 0;
78 updateColumns();
81 PlaylistSearch::Component SearchLine::searchComponent() const
83 QString query = m_lineEdit->text();
84 bool caseSensitive = m_caseSensitive && m_caseSensitive->currentIndex() == CaseSensitive;
86 Playlist *playlist = CollectionList::instance();
88 QList<int> searchedColumns;
90 if(!m_searchFieldsBox || m_searchFieldsBox->currentIndex() == 0) {
91 foreach(int column, m_columnList) {
92 if(playlist->isColumnVisible(column))
93 searchedColumns.append(column);
96 else
97 searchedColumns.append(m_columnList[m_searchFieldsBox->currentIndex() - 1]);
99 if(m_caseSensitive && m_caseSensitive->currentIndex() == Pattern)
100 return PlaylistSearch::Component(QRegExp(query), searchedColumns);
101 else
102 return PlaylistSearch::Component(query, caseSensitive, searchedColumns);
105 void SearchLine::setSearchComponent(const PlaylistSearch::Component &component)
107 if(component == searchComponent())
108 return;
110 if(m_simple || !component.isPatternSearch()) {
111 m_lineEdit->setText(component.query());
112 if(m_caseSensitive)
113 m_caseSensitive->setCurrentIndex(component.isCaseSensitive() ? CaseSensitive : Default);
115 else {
116 m_lineEdit->setText(component.pattern().pattern());
117 if(m_caseSensitive)
118 m_caseSensitive->setCurrentIndex(Pattern);
121 if(!m_simple) {
122 if(component.columns().isEmpty() || component.columns().size() > 1)
123 m_searchFieldsBox->setCurrentIndex(0);
124 else
125 m_searchFieldsBox->setCurrentIndex(component.columns().front() + 1);
129 void SearchLine::clear()
131 // We don't want to emit the signal if it's already empty.
132 if(!m_lineEdit->text().isEmpty())
133 m_lineEdit->clear();
136 void SearchLine::setFocus()
138 m_lineEdit->setFocus();
141 bool SearchLine::eventFilter(QObject *watched, QEvent *e)
143 if(watched != m_lineEdit || e->type() != QEvent::KeyPress)
144 return Q3HBox::eventFilter(watched, e);
146 QKeyEvent *key = static_cast<QKeyEvent *>(e);
147 if(key->key() == Qt::Key_Down)
148 emit signalDownPressed();
150 return Q3HBox::eventFilter(watched, e);
153 void SearchLine::slotActivate()
155 action("stop")->trigger();
156 action("playFirst")->trigger();
159 void SearchLine::updateColumns()
161 QString currentText;
163 if(m_searchFieldsBox) {
164 currentText = m_searchFieldsBox->currentText();
165 m_searchFieldsBox->clear();
168 QStringList columnHeaders;
170 columnHeaders.append(QString("<%1>").arg(i18n("All Visible")));
172 Playlist *playlist = CollectionList::instance();
174 int selection = -1;
175 m_columnList.clear();
177 for(int i = 0; i < playlist->columns(); i++) {
178 m_columnList.append(i);
179 QString text = playlist->columnText(i);
180 columnHeaders.append(text);
181 if(currentText == text)
182 selection = m_columnList.size() - 1;
185 if(m_searchFieldsBox) {
186 m_searchFieldsBox->addItems(columnHeaders);
187 m_searchFieldsBox->setCurrentIndex(selection + 1);
191 ////////////////////////////////////////////////////////////////////////////////
192 // SearchWidget public methods
193 ////////////////////////////////////////////////////////////////////////////////
195 SearchWidget::SearchWidget(QWidget *parent, const char *name) : KToolBar(parent, name)
197 new SearchAdaptor(this);
198 QDBusConnection::sessionBus().registerObject("/Search", this);
199 setupLayout();
200 updateColumns();
203 SearchWidget::~SearchWidget()
208 void SearchWidget::setSearch(const PlaylistSearch &search)
210 PlaylistSearch::ComponentList components = search.components();
212 if(components.isEmpty()) {
213 clear();
214 return;
217 m_searchLine->setSearchComponent(*components.begin());
220 QString SearchWidget::searchText() const
222 return m_searchLine->searchComponent().query();
225 void SearchWidget::setSearchText(const QString &text)
227 m_searchLine->setSearchComponent(PlaylistSearch::Component(text));
230 PlaylistSearch SearchWidget::search(const PlaylistList &playlists) const
232 PlaylistSearch::ComponentList components;
233 components.append(m_searchLine->searchComponent());
234 return PlaylistSearch(playlists, components);
239 ////////////////////////////////////////////////////////////////////////////////
240 // SearchWidget public slots
241 ////////////////////////////////////////////////////////////////////////////////
243 void SearchWidget::clear()
245 m_searchLine->clear();
248 void SearchWidget::setEnabled(bool enable)
250 emit signalShown(enable);
251 setVisible(enable);
254 void SearchWidget::setFocus()
256 m_searchLine->setFocus();
259 ////////////////////////////////////////////////////////////////////////////////
260 // SearchWidget private methods
261 ////////////////////////////////////////////////////////////////////////////////
263 void SearchWidget::updateColumns()
265 m_searchLine->updateColumns();
268 void SearchWidget::setupLayout()
270 /// Qt4 porting: disabled this: boxLayout()->setSpacing(5);
272 QLabel *label = new QLabel(i18n("Search:"), this );
273 label->setObjectName( "kde toolbar widget" );
275 m_searchLine = new SearchLine(this, true );
276 m_searchLine->setObjectName( "kde toolbar widget" );
278 label->setBuddy(m_searchLine);
280 connect(m_searchLine, SIGNAL(signalQueryChanged()), this, SIGNAL(signalQueryChanged()));
281 connect(m_searchLine, SIGNAL(signalDownPressed()), this, SIGNAL(signalDownPressed()));
283 #ifdef __GNUC__
284 #warning TODO Find replacement for KToolBar::setStretchableWidget
285 #endif
286 //setStretchableWidget(m_searchLine);
288 // I've decided that I think this is ugly, for now.
291 QToolButton *b = new QToolButton(this);
292 b->setTextLabel(i18n("Advanced Search"), true);
293 b->setIconSet(SmallIconSet("wizard"));
295 connect(b, SIGNAL(clicked()), this, SIGNAL(signalAdvancedSearchClicked()));
299 #include "searchwidget.moc"
301 // vim: set et sw=4 tw=0 sta: