searchwidget.cpp 8.88 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 19 20
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

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

#include <qlayout.h>
#include <qlabel.h>
#include <qcheckbox.h>
#include <qpushbutton.h>
30
#include <qtoolbutton.h>
Laurent Montel's avatar
Laurent Montel committed
31 32 33 34
//Added by qt3to4:
#include <Q3ValueList>
#include <QKeyEvent>
#include <QEvent>
35 36

#include "searchwidget.h"
37
#include "collectionlist.h"
38 39 40
#include "actioncollection.h"

using namespace ActionCollection;
41 42

////////////////////////////////////////////////////////////////////////////////
43
// SearchLine public methods
44 45
////////////////////////////////////////////////////////////////////////////////

46
SearchLine::SearchLine(QWidget *parent, bool simple, const char *name) :
Laurent Montel's avatar
Laurent Montel committed
47
    Q3HBox(parent, name),
Scott Wheeler's avatar
Scott Wheeler committed
48 49
    m_simple(simple),
    m_searchFieldsBox(0)
50
{
51
    setSpacing(5);
52

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

60 61
    m_lineEdit = new KLineEdit(this);
    m_lineEdit->setObjectName( "searchLineEdit" );
62
    m_lineEdit->installEventFilter(this);
63 64
    connect(m_lineEdit, SIGNAL(textChanged(const QString &)),
            this, SIGNAL(signalQueryChanged()));
65
    connect(m_lineEdit, SIGNAL(returnPressed()),
66
            this, SLOT(slotActivate()));
67

68
    if(!m_simple) {
69 70 71 72 73 74
        m_caseSensitive = new KComboBox(this);
        m_caseSensitive->insertItem(i18n("Normal Matching"), 0);
        m_caseSensitive->insertItem(i18n("Case Sensitive"), 1);
        m_caseSensitive->insertItem(i18n("Pattern Matching"), 2);
        connect(m_caseSensitive, SIGNAL(activated(int)),
                this, SIGNAL(signalQueryChanged()));
75 76
    }
    else
77
        m_caseSensitive = 0;
78 79

    updateColumns();
80 81
}

82
PlaylistSearch::Component SearchLine::searchComponent() const
Scott Wheeler's avatar
Scott Wheeler committed
83
{
84
    QString query = m_lineEdit->text();
85
    bool caseSensitive = m_caseSensitive && m_caseSensitive->currentItem() == CaseSensitive;
Scott Wheeler's avatar
Scott Wheeler committed
86

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

Laurent Montel's avatar
Laurent Montel committed
89
    Q3ValueList<int> searchedColumns;
Scott Wheeler's avatar
Scott Wheeler committed
90

91
    if(!m_searchFieldsBox || m_searchFieldsBox->currentItem() == 0) {
92 93 94 95 96
        Q3ValueListConstIterator<int> it = m_columnList.begin();
        for(; it != m_columnList.end(); ++it) {
            if(playlist->isColumnVisible(*it))
                searchedColumns.append(*it);
        }
97
    }
98
    else
99
        searchedColumns.append(m_columnList[m_searchFieldsBox->currentItem() - 1]);
Scott Wheeler's avatar
Scott Wheeler committed
100

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

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

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

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

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

138 139 140 141 142
void SearchLine::setFocus()
{
    m_lineEdit->setFocus();
}

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

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

Laurent Montel's avatar
Laurent Montel committed
152
    return Q3HBox::eventFilter(watched, e);
153 154
}

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

161 162
void SearchLine::updateColumns()
{
163 164 165
    QString currentText;

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

    QStringList columnHeaders;

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

174 175 176
    Playlist *playlist = CollectionList::instance();

    int selection = -1;
177
    m_columnList.clear();
178 179

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

187
    if(m_searchFieldsBox) {
188 189
        m_searchFieldsBox->insertStringList(columnHeaders);
        m_searchFieldsBox->setCurrentItem(selection + 1);
190
    }
191 192
}

193
////////////////////////////////////////////////////////////////////////////////
194
// SearchWidget public methods
195 196
////////////////////////////////////////////////////////////////////////////////

197
SearchWidget::SearchWidget(QWidget *parent, const char *name) : KToolBar(parent, name)
198
{
199 200
    setupLayout();
    updateColumns();
201 202
}

203
SearchWidget::~SearchWidget()
204 205
{

206 207 208 209 210 211 212
}

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

    if(components.isEmpty()) {
213 214
        clear();
        return;
215
    }
216 217

    m_searchLine->setSearchComponent(*components.begin());
218 219
}

220 221 222 223 224 225 226 227 228 229
QString SearchWidget::searchText() const
{
    return m_searchLine->searchComponent().query();
}

void SearchWidget::setSearchText(const QString &text)
{
    m_searchLine->setSearchComponent(PlaylistSearch::Component(text));
}

230
PlaylistSearch SearchWidget::search(const PlaylistList &playlists) const
231
{
232 233 234 235
    PlaylistSearch::ComponentList components;
    components.append(m_searchLine->searchComponent());
    return PlaylistSearch(playlists, components);
}
236

237 238


239 240 241
////////////////////////////////////////////////////////////////////////////////
// SearchWidget public slots
////////////////////////////////////////////////////////////////////////////////
242

243 244 245
void SearchWidget::clear()
{
    m_searchLine->clear();
246 247
}

248
void SearchWidget::setEnabled(bool enable)
249
{
250 251
    emit signalShown(enable);
    setShown(enable);
252 253
}

254 255 256 257 258
void SearchWidget::setFocus()
{
    m_searchLine->setFocus();
}

259
////////////////////////////////////////////////////////////////////////////////
260
// SearchWidget private methods
261 262
////////////////////////////////////////////////////////////////////////////////

263 264 265 266 267
void SearchWidget::updateColumns()
{
    m_searchLine->updateColumns();
}

268
void SearchWidget::setupLayout()
269
{
David Faure's avatar
David Faure committed
270
    /// Qt4 porting: disabled this: boxLayout()->setSpacing(5);
271

Scott Wheeler's avatar
Scott Wheeler committed
272 273 274 275
    QToolButton *clearSearchButton = new QToolButton(this);
    clearSearchButton->setTextLabel(i18n("Clear Search"), true);
    clearSearchButton->setIconSet(SmallIconSet("locationbar_erase"));

Stephan Kulow's avatar
Stephan Kulow committed
276 277
    QLabel *label = new QLabel(i18n("Search:"), this );
    label->setObjectName( "kde toolbar widget" );
278

Stephan Kulow's avatar
Stephan Kulow committed
279 280
    m_searchLine = new SearchLine(this, true );
    m_searchLine->setObjectName( "kde toolbar widget" );
281 282 283

    label->setBuddy(m_searchLine);

284
    connect(m_searchLine, SIGNAL(signalQueryChanged()), this, SIGNAL(signalQueryChanged()));
285
    connect(m_searchLine, SIGNAL(signalDownPressed()), this, SIGNAL(signalDownPressed()));
Scott Wheeler's avatar
Scott Wheeler committed
286
    connect(clearSearchButton, SIGNAL(pressed()), m_searchLine, SLOT(clear()));
David Faure's avatar
David Faure committed
287 288 289 290 291

#ifdef __GNUC__
#warning TODO Find replacement for KToolBar::setStretchableWidget
#endif
    //setStretchableWidget(m_searchLine);
292

293
    // I've decided that I think this is ugly, for now.
294

295 296 297 298 299 300 301
    /*
      QToolButton *b = new QToolButton(this);
      b->setTextLabel(i18n("Advanced Search"), true);
      b->setIconSet(SmallIconSet("wizard"));

      connect(b, SIGNAL(clicked()), this, SIGNAL(signalAdvancedSearchClicked()));
    */
302 303 304
}

#include "searchwidget.moc"
305 306

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