searchwidget.cpp 8.47 KB
Newer Older
1
2
/***************************************************************************
    begin                : Sun Mar 6 2003
3
    copyright            : (C) 2003 by Richard Lärkäng
4
5
6
7
    email                : nouseforaname@home.se

    copyright            : (C) 2003 - 2004 by Scott Wheeler
    email                : wheeler@kde.org
8
9
10
11
12
13
14
15
16
17
18
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

19
20
21
22
23
#include "searchwidget.h"
#include "collectionlist.h"
#include "actioncollection.h"
#include "searchadaptor.h"

24
25
#include <klocale.h>
#include <klineedit.h>
26
#include <kiconloader.h>
27
#include <kcombobox.h>
28
#include <kdebug.h>
29
#include <kaction.h>
30

31
32
33
#include <QLabel>
#include <QCheckBox>
#include <QPushButton>
34
#include <QToolButton>
Laurent Montel's avatar
Laurent Montel committed
35
#include <QKeyEvent>
Laurent Montel's avatar
Laurent Montel committed
36
#include <QHBoxLayout>
37

38
using namespace ActionCollection;
39
40

////////////////////////////////////////////////////////////////////////////////
41
// SearchLine public methods
42
43
////////////////////////////////////////////////////////////////////////////////

44
45
SearchLine::SearchLine(QWidget *parent, bool simple)
    : QFrame(parent),
Scott Wheeler's avatar
Scott Wheeler committed
46
47
    m_simple(simple),
    m_searchFieldsBox(0)
48
{
49
    QHBoxLayout *layout = new QHBoxLayout(this);
Sebastian Sauer's avatar
Sebastian Sauer committed
50
    layout->setMargin(0);
51
    layout->setSpacing(5);
52

53
    if(!m_simple) {
54
        m_searchFieldsBox = new KComboBox(this);
55
        layout->addWidget(m_searchFieldsBox);
56
        m_searchFieldsBox->setObjectName( "searchFields" );
57
58
        connect(m_searchFieldsBox, SIGNAL(activated(int)),
                this, SIGNAL(signalQueryChanged()));
59
    }
60

61
    m_lineEdit = new KLineEdit(this);
62
    layout->addWidget(m_lineEdit);
63
    m_lineEdit->setClearButtonShown(true);
64
    m_lineEdit->installEventFilter(this);
65
66
    connect(m_lineEdit, SIGNAL(textChanged(const QString &)),
            this, SIGNAL(signalQueryChanged()));
67
    connect(m_lineEdit, SIGNAL(returnPressed()),
68
            this, SLOT(slotActivate()));
69

70
    if(!m_simple) {
71
        m_caseSensitive = new KComboBox(this);
72
        layout->addWidget(m_caseSensitive);
Stephan Kulow's avatar
Stephan Kulow committed
73
74
75
        m_caseSensitive->addItem(i18n("Normal Matching"));
        m_caseSensitive->addItem(i18n("Case Sensitive"));
        m_caseSensitive->addItem(i18n("Pattern Matching"));
76
77
        connect(m_caseSensitive, SIGNAL(activated(int)),
                this, SIGNAL(signalQueryChanged()));
78
79
    }
    else
80
        m_caseSensitive = 0;
81
82

    updateColumns();
83
84
}

85
PlaylistSearch::Component SearchLine::searchComponent() const
Scott Wheeler's avatar
Scott Wheeler committed
86
{
87
    QString query = m_lineEdit->text();
88
    bool caseSensitive = m_caseSensitive && m_caseSensitive->currentIndex() == CaseSensitive;
Scott Wheeler's avatar
Scott Wheeler committed
89

90
    Playlist *playlist = CollectionList::instance();
Scott Wheeler's avatar
Scott Wheeler committed
91

92
    QList<int> searchedColumns;
Scott Wheeler's avatar
Scott Wheeler committed
93

94
    if(!m_searchFieldsBox || m_searchFieldsBox->currentIndex() == 0) {
95
96
97
        foreach(int column, m_columnList) {
            if(playlist->isColumnVisible(column))
                searchedColumns.append(column);
98
        }
99
    }
100
    else
101
        searchedColumns.append(m_columnList[m_searchFieldsBox->currentIndex() - 1]);
Scott Wheeler's avatar
Scott Wheeler committed
102

103
    if(m_caseSensitive && m_caseSensitive->currentIndex() == Pattern)
104
        return PlaylistSearch::Component(QRegExp(query), searchedColumns);
Scott Wheeler's avatar
Scott Wheeler committed
105
    else
106
        return PlaylistSearch::Component(query, caseSensitive, searchedColumns);
107
108
109
110
}

void SearchLine::setSearchComponent(const PlaylistSearch::Component &component)
{
111
    if(component == searchComponent())
112
        return;
113

114
    if(m_simple || !component.isPatternSearch()) {
115
116
        m_lineEdit->setText(component.query());
        if(m_caseSensitive)
117
            m_caseSensitive->setCurrentIndex(component.isCaseSensitive() ? CaseSensitive : Default);
Scott Wheeler's avatar
Scott Wheeler committed
118
119
    }
    else {
120
121
        m_lineEdit->setText(component.pattern().pattern());
        if(m_caseSensitive)
122
            m_caseSensitive->setCurrentIndex(Pattern);
Scott Wheeler's avatar
Scott Wheeler committed
123
    }
124
125

    if(!m_simple) {
126
        if(component.columns().isEmpty() || component.columns().size() > 1)
127
            m_searchFieldsBox->setCurrentIndex(0);
128
        else
129
            m_searchFieldsBox->setCurrentIndex(component.columns().front() + 1);
130
    }
Scott Wheeler's avatar
Scott Wheeler committed
131
132
}

133
void SearchLine::clear()
134
{
135
136
    // We don't want to emit the signal if it's already empty.
    if(!m_lineEdit->text().isEmpty())
137
        m_lineEdit->clear();
138
}
139

140
141
142
143
144
void SearchLine::setFocus()
{
    m_lineEdit->setFocus();
}

145
146
147
bool SearchLine::eventFilter(QObject *watched, QEvent *e)
{
    if(watched != m_lineEdit || e->type() != QEvent::KeyPress)
148
        return QFrame::eventFilter(watched, e);
149
150
151

    QKeyEvent *key = static_cast<QKeyEvent *>(e);
    if(key->key() == Qt::Key_Down)
152
        emit signalDownPressed();
153

154
    return QFrame::eventFilter(watched, e);
155
156
}

157
158
void SearchLine::slotActivate()
{
David Faure's avatar
David Faure committed
159
160
    action("stop")->trigger();
    action("playFirst")->trigger();
161
162
}

163
164
void SearchLine::updateColumns()
{
165
166
167
    QString currentText;

    if(m_searchFieldsBox) {
168
169
        currentText = m_searchFieldsBox->currentText();
        m_searchFieldsBox->clear();
170
    }
171
172
173
174

    QStringList columnHeaders;

    columnHeaders.append(QString("<%1>").arg(i18n("All Visible")));
175

176
177
178
    Playlist *playlist = CollectionList::instance();

    int selection = -1;
179
    m_columnList.clear();
180
181

    for(int i = 0; i < playlist->columns(); i++) {
182
183
184
185
186
        m_columnList.append(i);
        QString text = playlist->columnText(i);
        columnHeaders.append(text);
        if(currentText == text)
            selection = m_columnList.size() - 1;
187
    }
188

189
    if(m_searchFieldsBox) {
Stephan Kulow's avatar
Stephan Kulow committed
190
        m_searchFieldsBox->addItems(columnHeaders);
191
        m_searchFieldsBox->setCurrentIndex(selection + 1);
192
    }
193
194
}

195
////////////////////////////////////////////////////////////////////////////////
196
// SearchWidget public methods
197
198
////////////////////////////////////////////////////////////////////////////////

199
200
201
SearchWidget::SearchWidget(QWidget *parent)
    : KToolBar(parent),
    m_searchLine(this, true)
202
{
Laurent Montel's avatar
Laurent Montel committed
203
    new SearchAdaptor(this);
204
    QDBusConnection::sessionBus().registerObject("/Search", this);
205

206
207
208
209
210
211
212
    QLabel *label = new QLabel(i18n("Search:"), this );
    label->setBuddy(&m_searchLine);
    addWidget(label);
    addWidget(&m_searchLine);

    connect(&m_searchLine, SIGNAL(signalQueryChanged()), this, SIGNAL(signalQueryChanged()));
    connect(&m_searchLine, SIGNAL(signalDownPressed()), this, SIGNAL(signalDownPressed()));
Michael Pyne's avatar
Michael Pyne committed
213
    connect(m_searchLine.m_lineEdit, SIGNAL(returnPressed()), this, SIGNAL(returnPressed()));
214

215
216
217
218
219
220
221
222
223
    // I've decided that I think this is ugly, for now.
    /*
      QToolButton *b = new QToolButton(this);
      b->setTextLabel(i18n("Advanced Search"), true);
      b->setIconSet(SmallIconSet("wizard"));

      connect(b, SIGNAL(clicked()), this, SIGNAL(signalAdvancedSearchClicked()));
    */
    updateColumns();
224
225
226
227
228
229
230
}

void SearchWidget::setSearch(const PlaylistSearch &search)
{
    PlaylistSearch::ComponentList components = search.components();

    if(components.isEmpty()) {
231
232
        clear();
        return;
233
    }
234

235
    m_searchLine.setSearchComponent(*components.begin());
236
237
}

238
239
QString SearchWidget::searchText() const
{
240
    return m_searchLine.searchComponent().query();
241
242
243
244
}

void SearchWidget::setSearchText(const QString &text)
{
245
    m_searchLine.setSearchComponent(PlaylistSearch::Component(text));
246
247
}

248
PlaylistSearch SearchWidget::search(const PlaylistList &playlists) const
249
{
250
    PlaylistSearch::ComponentList components;
251
    components.append(m_searchLine.searchComponent());
252
253
    return PlaylistSearch(playlists, components);
}
254

255
256


257
258
259
////////////////////////////////////////////////////////////////////////////////
// SearchWidget public slots
////////////////////////////////////////////////////////////////////////////////
260

261
262
void SearchWidget::clear()
{
263
    m_searchLine.clear();
264
265
}

266
void SearchWidget::setEnabled(bool enable)
267
{
268
    emit signalShown(enable);
269
    setVisible(enable);
270
271
}

272
273
void SearchWidget::setFocus()
{
274
    m_searchLine.setFocus();
275
276
}

277
////////////////////////////////////////////////////////////////////////////////
278
// SearchWidget private methods
279
280
////////////////////////////////////////////////////////////////////////////////

281
282
void SearchWidget::updateColumns()
{
283
    m_searchLine.updateColumns();
284
285
286
}

#include "searchwidget.moc"
287
288

// vim: set et sw=4 tw=0 sta: