1 /***************************************************************************
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 /***************************************************************************
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. *
17 ***************************************************************************/
19 #include "searchwidget.h"
20 #include "collectionlist.h"
21 #include "actioncollection.h"
22 #include "searchadaptor.h"
25 #include <klineedit.h>
26 #include <kiconloader.h>
27 #include <kcombobox.h>
34 #include <QPushButton>
35 #include <QToolButton>
39 using namespace ActionCollection
;
41 ////////////////////////////////////////////////////////////////////////////////
42 // SearchLine public methods
43 ////////////////////////////////////////////////////////////////////////////////
45 SearchLine::SearchLine(QWidget
*parent
, bool simple
, const char *name
) :
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()));
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()));
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
);
97 searchedColumns
.append(m_columnList
[m_searchFieldsBox
->currentIndex() - 1]);
99 if(m_caseSensitive
&& m_caseSensitive
->currentIndex() == Pattern
)
100 return PlaylistSearch::Component(QRegExp(query
), searchedColumns
);
102 return PlaylistSearch::Component(query
, caseSensitive
, searchedColumns
);
105 void SearchLine::setSearchComponent(const PlaylistSearch::Component
&component
)
107 if(component
== searchComponent())
110 if(m_simple
|| !component
.isPatternSearch()) {
111 m_lineEdit
->setText(component
.query());
113 m_caseSensitive
->setCurrentIndex(component
.isCaseSensitive() ? CaseSensitive
: Default
);
116 m_lineEdit
->setText(component
.pattern().pattern());
118 m_caseSensitive
->setCurrentIndex(Pattern
);
122 if(component
.columns().isEmpty() || component
.columns().size() > 1)
123 m_searchFieldsBox
->setCurrentIndex(0);
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())
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()
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();
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);
203 SearchWidget::~SearchWidget()
208 void SearchWidget::setSearch(const PlaylistSearch
&search
)
210 PlaylistSearch::ComponentList components
= search
.components();
212 if(components
.isEmpty()) {
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
);
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()));
284 #warning TODO Find replacement for KToolBar::setStretchableWidget
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: