]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinsearchbox.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19 #include "dolphinsearchbox.h"
21 #include <config-nepomuk.h>
24 #include <kglobalsettings.h>
25 #include <klineedit.h>
27 #include <kiconloader.h>
31 #include <QHBoxLayout>
32 #include <QStandardItemModel>
33 #include <QtGui/QCompleter>
34 #include <QtGui/QTreeView>
35 #include <QToolButton>
38 #include <Nepomuk/ResourceManager>
39 #include <Nepomuk/Tag>
43 DolphinSearchCompleter::DolphinSearchCompleter(KLineEdit
* linedit
) :
53 void DolphinSearchCompleter::init()
55 m_completionModel
= new QStandardItemModel(this);
58 if (!Nepomuk::ResourceManager::instance()->init()) {
59 //read all currently set tags
60 //NOTE if the user changes tags elsewhere they won't get updated here
61 QList
<Nepomuk::Tag
> tags
= Nepomuk::Tag::allTags();
62 foreach (const Nepomuk::Tag
& tag
, tags
) {
63 const QString tagText
= tag
.label();
64 addCompletionItem(tagText
,
65 "tag:\"" + tagText
+ '\"',
66 i18nc("Tag as in Nepomuk::Tag", "Tag"),
67 KIcon("mail-tagged"));
72 //add "and", "or" and "-" (not) logic operators
73 addCompletionItem(i18nc("and ss in a logic operator to connect search terms", "and"),
75 "logic operator and");
76 addCompletionItem(i18nc("or as in a logic operator to connect search terms", "or"),
79 addCompletionItem(i18nc("not as in a logic operator to connect search terms", "not"),
81 "logic operator not");
83 m_completionModel
->sort(0, Qt::AscendingOrder
);
85 m_completer
= new QCompleter(m_completionModel
, this);
86 m_completer
->setWidget(q
);
87 m_completer
->setCaseSensitivity(Qt::CaseInsensitive
);
88 QTreeView
*view
= new QTreeView
;
89 m_completer
->setPopup(view
);
90 view
->setRootIsDecorated(false);
91 view
->setHeaderHidden(true);
93 connect(q
, SIGNAL(textEdited(QString
)), this, SLOT(slotTextEdited(QString
)));
94 connect(m_completer
, SIGNAL(activated(QModelIndex
)), this, SLOT(completionActivated(QModelIndex
)));
95 connect(m_completer
, SIGNAL(highlighted(QModelIndex
)), this, SLOT(highlighted(QModelIndex
)));
98 void DolphinSearchCompleter::addCompletionItem(const QString
& displayed
, const QString
& usedForCompletition
, const QString
& description
, const KIcon
& icon
)
100 QList
<QStandardItem
*> items
;
101 QStandardItem
*item
= new QStandardItem();
102 item
->setData(QVariant(displayed
), Qt::DisplayRole
);
103 item
->setData(QVariant(usedForCompletition
), Qt::UserRole
);
106 item
= new QStandardItem(description
);
110 m_completionModel
->insertRow(m_completionModel
->rowCount(), items
);
113 void DolphinSearchCompleter::findText(int* wordStart
, int* wordEnd
, QString
* newWord
, int cursorPos
, const QString
&input
)
115 --cursorPos
;//decrease to get a useful position (not the end of the word e.g.)
117 if (!wordStart
|| !wordEnd
) {
124 //the word might contain "" and thus maybe spaces
125 if (input
.contains('\"')) {
130 tempStart
= input
.indexOf('\"', tempEnd
+ 1);
131 tempEnd
= input
.indexOf('\"', tempStart
+ 1);
132 if ((cursorPos
>= tempStart
) && (cursorPos
<= tempEnd
)) {
133 *wordStart
= tempStart
;
136 } else if ((tempEnd
== -1) && (cursorPos
>= tempStart
)) {
137 //one " found, so probably the beginning of the new word
138 *wordStart
= tempStart
;
141 } while ((tempStart
!= -1) && (tempEnd
!= -1));
145 *wordEnd
= input
.indexOf(' ', *wordEnd
) - 1;
147 *wordEnd
= input
.indexOf(' ', cursorPos
) - 1;
150 *wordEnd
= input
.length() - 1;
153 if (*wordStart
> -1) {
154 *wordStart
= input
.lastIndexOf(' ', *wordStart
+ 1) + 1;
156 *wordStart
= input
.lastIndexOf(' ', cursorPos
) + 1;
158 if (*wordStart
< 0) {
163 QString word
= input
.mid(*wordStart
, *wordEnd
- *wordStart
+ 1);
165 //remove opening braces or negations ('-' = not) at the beginning
166 while (word
.count() && ((word
[0] == '(') || (word
[0] == '-'))) {
171 //remove ending braces at the end
172 while (word
.count() && (word
[word
.count() - 1] == ')')) {
173 word
.remove(word
.count() - 1, 1);
182 void DolphinSearchCompleter::slotTextEdited(const QString
& text
)
184 findText(&m_wordStart
, &m_wordEnd
, &m_userText
, q
->cursorPosition(), text
);
186 if (!m_userText
.isEmpty()) {
187 const int role
= m_completer
->completionRole();
189 //change the role used for comparison depending on what the user entered
190 if (m_userText
.contains(':') || m_userText
.contains('\"')) {
191 //assume that m_userText contains searchinformation like 'tag:"..."'
192 if (role
!= Qt::UserRole
) {
193 m_completer
->setCompletionRole(Qt::UserRole
);
195 } else if (role
!= Qt::EditRole
) {
196 m_completer
->setCompletionRole(Qt::EditRole
);
199 m_completer
->setCompletionPrefix(m_userText
);
200 m_completer
->complete();
204 void DolphinSearchCompleter::highlighted(const QModelIndex
& index
)
206 QString text
= q
->text();
210 findText(&wordStart
, &wordEnd
, 0, q
->cursorPosition(), text
);
212 QString replace
= index
.sibling(index
.row(), 0).data(Qt::UserRole
).toString();
213 //show the originally entered text
214 if (replace
.isEmpty()) {
215 replace
= m_userText
;
218 text
.replace(wordStart
, wordEnd
- wordStart
+ 1, replace
);
220 q
->setCursorPosition(wordStart
+ replace
.length());
223 void DolphinSearchCompleter::activated(const QModelIndex
& index
)
225 if ((m_wordStart
== -1) || (m_wordStart
== -1)) {
229 const QString replace
= index
.sibling(index
.row(), 0).data(Qt::UserRole
).toString();
230 QString newText
= q
->text();
231 newText
.replace(m_wordStart
, m_wordEnd
- m_wordStart
+ 1, replace
);
235 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
241 QHBoxLayout
* hLayout
= new QHBoxLayout(this);
242 hLayout
->setMargin(0);
243 hLayout
->setSpacing(0);
245 m_searchInput
= new KLineEdit(this);
246 m_searchInput
->setClearButtonShown(true);
247 m_searchInput
->setMinimumWidth(150);
248 m_searchInput
->setClickMessage(i18nc("@label:textbox", "Search..."));
249 hLayout
->addWidget(m_searchInput
);
250 connect(m_searchInput
, SIGNAL(returnPressed()),
251 this, SLOT(emitSearchSignal()));
253 m_completer
= new DolphinSearchCompleter(m_searchInput
);
256 m_searchButton
= new QToolButton(this);
257 m_searchButton
->setAutoRaise(true);
258 m_searchButton
->setIcon(KIcon("edit-find"));
259 m_searchButton
->setToolTip(i18nc("@info:tooltip", "Click to begin the search"));
260 hLayout
->addWidget(m_searchButton
);
261 connect(m_searchButton
, SIGNAL(clicked()),
262 this, SLOT(emitSearchSignal()));
265 DolphinSearchBox::~DolphinSearchBox()
270 bool DolphinSearchBox::event(QEvent
* event
)
272 if (event
->type() == QEvent::Polish
) {
273 m_searchInput
->setFont(KGlobalSettings::generalFont());
274 } else if (event
->type() == QEvent::KeyPress
) {
275 if (static_cast<QKeyEvent
*>(event
)->key() == Qt::Key_Escape
) {
276 m_searchInput
->clear();
279 return QWidget::event(event
);
282 void DolphinSearchBox::emitSearchSignal()
284 emit
search(KUrl("nepomuksearch:/" + m_searchInput
->text()));
287 #include "dolphinsearchbox.moc"