Commit 783da573 authored by Aaron J. Seigo's avatar Aaron J. Seigo

regexp matching in the search widget.

svn path=/trunk/kdemultimedia/juk/; revision=224871
parent 2a0c82c9
......@@ -29,10 +29,10 @@ PlaylistSearch::PlaylistSearch(const PlaylistSearch &search) :
}
PlaylistSearch::PlaylistSearch(const PlaylistList &playlists,
const ComponentList &components,
PlaylistSearch::PlaylistSearch(const PlaylistList &playlists,
const ComponentList &components,
SearchMode mode) :
m_playlists(playlists),
m_playlists(playlists),
m_components(components),
m_mode(mode)
{
......@@ -47,7 +47,7 @@ void PlaylistSearch::search()
{
// This really isn't as bad as it looks. Despite the four nexted loops
// most of the time this will be searching one playlist for one search
// most of the time this will be searching one playlist for one search
// component -- possibly for one column.
// Also there should be some caching of previous searches in here and
......@@ -99,30 +99,42 @@ PlaylistSearch::Component::Component() :
m_searchAllVisible(true),
m_caseSensitive(false)
{
}
PlaylistSearch::Component::Component(const Component &component) :
m_query(component.m_query),
m_queryRe(component.m_queryRe),
m_columns(component.m_columns),
m_searchAllVisible(component.m_searchAllVisible),
m_caseSensitive(component.m_caseSensitive)
m_caseSensitive(component.m_caseSensitive),
m_re(component.m_re)
{
}
PlaylistSearch::Component::Component(const QString &query, bool caseSensitive, const ColumnList &columns) :
m_query(query),
m_columns(columns),
m_query(query),
m_columns(columns),
m_searchAllVisible(columns.isEmpty()),
m_caseSensitive(caseSensitive)
m_caseSensitive(caseSensitive),
m_re(false)
{
}
PlaylistSearch::Component::Component(const QRegExp &query, const ColumnList& columns) :
m_queryRe(query),
m_columns(columns),
m_searchAllVisible(columns.isEmpty()),
m_caseSensitive(false),
m_re(true)
{
}
bool PlaylistSearch::Component::matches(PlaylistItem *item)
{
if(m_query.isEmpty())
if((m_re && m_queryRe.isEmpty()) || (!m_re && m_query.isEmpty()))
return false;
if(m_columns.isEmpty()) {
......@@ -135,9 +147,11 @@ bool PlaylistSearch::Component::matches(PlaylistItem *item)
for(ColumnList::Iterator it = m_columns.begin(); it != m_columns.end(); ++it) {
if(item->text(*it).contains(m_query, m_caseSensitive))
return true;
int matches = m_re ? item->text(*it).contains(m_queryRe)
: item->text(*it).contains(m_query, m_caseSensitive);
if(matches > 0)
return true;
}
return false;
}
......@@ -19,6 +19,7 @@
#define PLAYLISTSEARCH_H
#include <qvaluelist.h>
#include <qregexp.h>
#include "playlist.h"
#include "playlistitem.h"
......@@ -86,6 +87,11 @@ public:
*/
Component(const QString &query, bool caseSensitive = false, const ColumnList &columns = ColumnList());
/**
* Create a query component. This defaults to searching all visible coulumns.
*/
Component(const QRegExp &query, const ColumnList &columns = ColumnList());
QString query() const { return m_query; }
ColumnList columns() const { return m_columns; }
......@@ -95,9 +101,11 @@ protected:
private:
QString m_query;
QRegExp m_queryRe;
ColumnList m_columns;
bool m_searchAllVisible;
bool m_caseSensitive;
bool m_re;
};
......
......@@ -416,8 +416,8 @@ void PlaylistSplitter::setupLayout()
// Create the search widget -- this must be done after the CollectionList is created.
m_searchWidget = new SearchWidget(editorSplitter, CollectionList::instance(), "searchWidget");
editorSplitter->moveToFirst(m_searchWidget);
connect(m_searchWidget, SIGNAL(signalQueryChanged(const QString &, bool)),
this, SLOT(slotShowSearchResults(const QString &, bool)));
connect(m_searchWidget, SIGNAL(signalQueryChanged(const QString &, bool, bool)),
this, SLOT(slotShowSearchResults(const QString &, bool, bool)));
connect(CollectionList::instance(), SIGNAL(signalVisibleColumnsChanged()),
this, SLOT(slotVisibleColumnsChanged()));
......@@ -649,7 +649,7 @@ void PlaylistSplitter::slotCreatePlaylist(const PlaylistItemList &items)
playlist->createItems(items);
}
void PlaylistSplitter::slotShowSearchResults(const QString &query, bool caseSensitive)
void PlaylistSplitter::slotShowSearchResults(const QString &query, bool caseSensitive, bool regExp)
{
if(query.isEmpty()) {
visiblePlaylist()->setItemsVisible(visiblePlaylist()->items(), true);
......@@ -659,14 +659,25 @@ void PlaylistSplitter::slotShowSearchResults(const QString &query, bool caseSens
PlaylistList playlists;
playlists.append(visiblePlaylist());
PlaylistSearch::Component component(query, caseSensitive, m_searchWidget->searchedColumns(0));
PlaylistSearch::Component *component;
if (regExp)
{
component = new PlaylistSearch::Component(QRegExp(query, caseSensitive), m_searchWidget->searchedColumns(0));
}
else
{
component = new PlaylistSearch::Component(query, caseSensitive, m_searchWidget->searchedColumns(0));
}
PlaylistSearch::ComponentList components;
components.append(component);
components.append(*component);
PlaylistSearch search(playlists, components);
Playlist::setItemsVisible(search.matchedItems(), true);
Playlist::setItemsVisible(search.unmatchedItems(), false);
delete component;
}
void PlaylistSplitter::slotVisibleColumnsChanged()
......@@ -674,7 +685,7 @@ void PlaylistSplitter::slotVisibleColumnsChanged()
m_searchWidget->slotUpdateColumns();
m_searchWidget->slotQueryChanged();
if(m_searchWidget->searchedColumns(0).count() > 1)
slotShowSearchResults(m_searchWidget->query(), m_searchWidget->caseSensitive());
slotShowSearchResults(m_searchWidget->query(), m_searchWidget->caseSensitive(), m_searchWidget->regExp());
}
#include "playlistsplitter.moc"
......
......@@ -291,7 +291,7 @@ private slots:
void slotSetNextItem(PlaylistItem *item = 0) { m_nextPlaylistItem = item; }
void slotDirChanged(const QString &dir) { slotAddToPlaylist(dir, m_collection); }
void slotCreatePlaylist(const PlaylistItemList &items);
void slotShowSearchResults(const QString &query, bool caseSensitive = false);
void slotShowSearchResults(const QString &query, bool caseSensitive = false, bool regExp = false);
void slotVisibleColumnsChanged();
private:
......
......@@ -52,7 +52,12 @@ QString SearchWidget::query() const
bool SearchWidget::caseSensitive() const
{
return m_caseSensitive->isChecked();
return m_caseSensitive->currentItem() == 1;
}
bool SearchWidget::regExp() const
{
return m_caseSensitive->currentItem() == 2;
}
////////////////////////////////////////////////////////////////////////////////
......@@ -79,7 +84,7 @@ void SearchWidget::slotQueryChanged(int)
else
m_searchedColumns[0].append(m_searchFieldsBox->currentItem() - 1);
emit signalQueryChanged(m_lineEdit->text(), m_caseSensitive->isChecked());
emit signalQueryChanged(m_lineEdit->text(), caseSensitive(), regExp());
}
void SearchWidget::slotUpdateColumns()
......@@ -119,14 +124,17 @@ void SearchWidget::setupLayout()
connect(m_searchFieldsBox, SIGNAL(activated(int)), this, SLOT(slotQueryChanged()));
m_lineEdit = new KLineEdit(this, "searchLineEdit");
m_caseSensitive = new QCheckBox(i18n("Case sensitive"), this);
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, SLOT(slotQueryChanged()));
QPushButton *button = new QPushButton(i18n("Clear"), this);
connect(button, SIGNAL(clicked()), this, SLOT(clear()));
connect(m_lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(slotQueryChanged()));
connect(m_caseSensitive, SIGNAL(toggled(bool)), this, SLOT(slotQueryChanged()));
setFixedHeight(minimumSizeHint().height());
}
......
......@@ -46,6 +46,7 @@ public:
QString query() const;
bool caseSensitive() const;
bool regExp() const;
public slots:
void clear();
......@@ -53,7 +54,7 @@ public slots:
void slotQueryChanged(int = 0);
signals:
void signalQueryChanged(const QString &query, bool caseSensitive);
void signalQueryChanged(const QString &query, bool caseSensitive, bool regExp);
private:
void setupLayout();
......@@ -62,7 +63,7 @@ private:
const Playlist *m_playlist;
KLineEdit *m_lineEdit;
KComboBox *m_searchFieldsBox;
QCheckBox *m_caseSensitive;
KComboBox *m_caseSensitive;
QStringList m_columnHeaders;
QValueList< QValueList<int> > m_searchedColumns;
} ;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment