1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2009 by Matthias Fuchs <mat69@gmx.net> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "dolphinsearchbox.h"
23 #include <config-nepomuk.h>
25 #include <KConfigGroup>
26 #include <KDesktopFile>
27 #include <kglobalsettings.h>
28 #include <klineedit.h>
30 #include <kiconloader.h>
31 #include <KStandardDirs>
35 #include <QHBoxLayout>
36 #include <QStandardItemModel>
37 #include <QtGui/QCompleter>
38 #include <QtGui/QTreeView>
39 #include <QToolButton>
42 #include <Nepomuk/ResourceManager>
43 #include <Nepomuk/Tag>
46 DolphinSearchCompleter::DolphinSearchCompleter(KLineEdit
* linedit
) :
54 m_completionModel
= new QStandardItemModel(this);
57 if (!Nepomuk::ResourceManager::instance()->init()) {
58 //read all currently set tags
59 //NOTE if the user changes tags elsewhere they won't get updated here
60 QList
<Nepomuk::Tag
> tags
= Nepomuk::Tag::allTags();
61 foreach (const Nepomuk::Tag
& tag
, tags
) {
62 const QString tagText
= tag
.label();
63 addCompletionItem(tagText
,
64 "tag:\"" + tagText
+ '\"',
65 i18nc("Tag as in Nepomuk::Tag", "Tag"),
67 KIcon("mail-tagged"));
72 // load the completions stored in the desktop file
73 KDesktopFile
file(KStandardDirs::locate("data", "dolphin/dolphinsearchcommands.desktop"));
74 foreach (const QString
&group
, file
.groupList()) {
75 KConfigGroup
cg(&file
, group
);
76 const QString displayed
= cg
.readEntry("Name", QString());
77 const QString usedForCompletition
= cg
.readEntry("Completion", QString());
78 const QString description
= cg
.readEntry("Comment", QString());
79 const QString toolTip
= cg
.readEntry("GenericName", QString());
80 const QString icon
= cg
.readEntry("Icon", QString());
83 addCompletionItem(displayed
, usedForCompletition
, description
, toolTip
);
85 addCompletionItem(displayed
, usedForCompletition
, description
, toolTip
, KIcon(icon
));
89 m_completionModel
->sort(0, Qt::AscendingOrder
);
91 m_completer
= new QCompleter(m_completionModel
, this);
92 m_completer
->setWidget(q
);
93 m_completer
->setCaseSensitivity(Qt::CaseInsensitive
);
94 QTreeView
*view
= new QTreeView
;
95 m_completer
->setPopup(view
);
96 view
->setRootIsDecorated(false);
97 view
->setHeaderHidden(true);
99 connect(q
, SIGNAL(textEdited(QString
)), this, SLOT(slotTextEdited(QString
)));
100 connect(m_completer
, SIGNAL(highlighted(QModelIndex
)), this, SLOT(highlighted(QModelIndex
)));
103 void DolphinSearchCompleter::addCompletionItem(const QString
& displayed
, const QString
& usedForCompletition
, const QString
& description
, const QString
& toolTip
, const KIcon
& icon
)
105 if (displayed
.isEmpty() || usedForCompletition
.isEmpty()) {
109 QList
<QStandardItem
*> items
;
110 QStandardItem
*item
= new QStandardItem();
111 item
->setData(QVariant(displayed
), Qt::DisplayRole
);
112 item
->setData(QVariant(usedForCompletition
), Qt::UserRole
);
113 item
->setData(QVariant(toolTip
), Qt::ToolTipRole
);
116 item
= new QStandardItem(description
);
117 if (!icon
.isNull()) {
120 item
->setData(QVariant(toolTip
), Qt::ToolTipRole
);
123 m_completionModel
->insertRow(m_completionModel
->rowCount(), items
);
126 void DolphinSearchCompleter::findText(int* wordStart
, int* wordEnd
, QString
* newWord
, int cursorPos
, const QString
&input
)
128 --cursorPos
;//decrease to get a useful position (not the end of the word e.g.)
130 if (!wordStart
|| !wordEnd
) {
137 // the word might contain "" and thus maybe spaces
138 if (input
.contains('\"')) {
143 tempStart
= input
.indexOf('\"', tempEnd
+ 1);
144 tempEnd
= input
.indexOf('\"', tempStart
+ 1);
145 if ((cursorPos
>= tempStart
) && (cursorPos
<= tempEnd
)) {
146 *wordStart
= tempStart
;
149 } else if ((tempEnd
== -1) && (cursorPos
>= tempStart
)) {
150 //one " found, so probably the beginning of the new word
151 *wordStart
= tempStart
;
154 } while ((tempStart
!= -1) && (tempEnd
!= -1));
158 *wordEnd
= input
.indexOf(' ', *wordEnd
) - 1;
160 *wordEnd
= input
.indexOf(' ', cursorPos
) - 1;
163 *wordEnd
= input
.length() - 1;
166 if (*wordStart
> -1) {
167 *wordStart
= input
.lastIndexOf(' ', *wordStart
+ 1) + 1;
169 *wordStart
= input
.lastIndexOf(' ', cursorPos
) + 1;
171 if (*wordStart
< 0) {
176 QString word
= input
.mid(*wordStart
, *wordEnd
- *wordStart
+ 1);
178 //remove opening braces or negations ('-' = not) at the beginning
179 while (word
.count() && ((word
[0] == '(') || (word
[0] == '-'))) {
184 //remove ending braces at the end
185 while (word
.count() && (word
[word
.count() - 1] == ')')) {
186 word
.remove(word
.count() - 1, 1);
195 void DolphinSearchCompleter::slotTextEdited(const QString
& text
)
197 findText(&m_wordStart
, &m_wordEnd
, &m_userText
, q
->cursorPosition(), text
);
199 if (!m_userText
.isEmpty()) {
200 const int role
= m_completer
->completionRole();
202 //change the role used for comparison depending on what the user entered
203 if (m_userText
.contains(':') || m_userText
.contains('\"')) {
204 //assume that m_userText contains searchinformation like 'tag:"..."'
205 if (role
!= Qt::UserRole
) {
206 m_completer
->setCompletionRole(Qt::UserRole
);
208 } else if (role
!= Qt::EditRole
) {
209 m_completer
->setCompletionRole(Qt::EditRole
);
212 m_completer
->setCompletionPrefix(m_userText
);
213 m_completer
->complete();
217 void DolphinSearchCompleter::highlighted(const QModelIndex
& index
)
219 QString text
= q
->text();
223 findText(&wordStart
, &wordEnd
, 0, q
->cursorPosition(), text
);
225 QString replace
= index
.sibling(index
.row(), 0).data(Qt::UserRole
).toString();
226 //show the originally entered text
227 if (replace
.isEmpty()) {
228 replace
= m_userText
;
231 text
.replace(wordStart
, wordEnd
- wordStart
+ 1, replace
);
233 q
->setCursorPosition(wordStart
+ replace
.length());
236 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 m_searchInput
->installEventFilter(this);
250 hLayout
->addWidget(m_searchInput
);
251 connect(m_searchInput
, SIGNAL(returnPressed()),
252 this, SLOT(emitSearchSignal()));
255 DolphinSearchBox::~DolphinSearchBox()
259 QString
DolphinSearchBox::text() const
261 return m_searchInput
->text();
264 bool DolphinSearchBox::event(QEvent
* event
)
266 if (event
->type() == QEvent::Polish
) {
267 m_searchInput
->setFont(KGlobalSettings::generalFont());
268 } else if (event
->type() == QEvent::KeyPress
) {
269 if (static_cast<QKeyEvent
*>(event
)->key() == Qt::Key_Escape
) {
270 m_searchInput
->clear();
273 return QWidget::event(event
);
276 bool DolphinSearchBox::eventFilter(QObject
* watched
, QEvent
* event
)
278 if ((watched
== m_searchInput
) && (event
->type() == QEvent::FocusIn
)) {
279 // Postpone the creation of the search completer until
280 // the search box is used. This decreases the startup time
282 if (m_completer
== 0) {
283 m_completer
= new DolphinSearchCompleter(m_searchInput
);
285 emit
requestSearchOptions();
288 return QWidget::eventFilter(watched
, event
);
292 void DolphinSearchBox::emitSearchSignal()
294 emit
search(m_searchInput
->text());
297 #include "dolphinsearchbox.moc"