add more spacing
[personal-kdebase.git] / workspace / krunner / interfaces / quicksand / qs_dialog.cpp
blob8a6d5b904ddb5d30806efea02daa27597661bdb7
1 /*
2 * Copyright (C) 2006 by Aaron Seigo <aseigo@kde.org>
3 * Copyright (C) 2007-2009 Ryan P. Bitanga <ryan.bitanga@gmail.com>
4 * Copyright (C) 2008 by Davide Bettio <davide.bettio@kdemail.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the
18 * Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA .
22 #include <QBoxLayout>
23 #include <QLabel>
24 #include <QToolButton>
26 #include <KAction>
27 #include <KStandardGuiItem>
28 #include <KWindowSystem>
30 //#include <Plasma/QueryAction>
31 #include <Plasma/QueryMatch>
32 #include <Plasma/RunnerManager>
33 #include <Plasma/Svg>
34 #include <Plasma/Theme>
36 #include "qs_dialog.h"
37 #include "qs_matchview.h"
38 #include "qs_querymatchitem.h"
39 #include "qs_queryactionitem.h"
41 QsDialog::QsDialog(Plasma::RunnerManager *runnerManager, QWidget *parent)
42 : KRunnerDialog(runnerManager, parent)
44 QWidget *w = mainWidget();
45 QVBoxLayout *layout = new QVBoxLayout(w);
47 QHBoxLayout *hLayout = new QHBoxLayout();
49 QToolButton *m_configButton = new QToolButton(w);
50 m_configButton->setText(i18n("Settings"));
51 m_configButton->setToolTip(i18n("Settings"));
52 m_configButton->setIcon(m_iconSvg->pixmap("configure"));
53 connect(m_configButton, SIGNAL(clicked()), SLOT(showConfigDialog()));
55 QLabel *label = new QLabel(w);
56 label->setText("<b>QuickSand</b>");
58 QToolButton *m_closeButton = new QToolButton(w);
59 KGuiItem guiItem = KStandardGuiItem::close();
60 m_closeButton->setText(guiItem.text());
61 m_closeButton->setToolTip(guiItem.text().remove('&'));
62 m_closeButton->setIcon(m_iconSvg->pixmap("close"));
63 connect(m_closeButton, SIGNAL(clicked(bool)), this, SLOT(close()));
65 hLayout->addWidget(m_configButton);
66 hLayout->addStretch();
67 hLayout->addWidget(label);
68 hLayout->addStretch();
69 hLayout->addWidget(m_closeButton);
70 layout->addLayout(hLayout);
72 m_matchView = new QuickSand::QsMatchView(w);
73 layout->addWidget(m_matchView);
74 connect(m_matchView, SIGNAL(textChanged(const QString&)), m_matchView, SLOT(setTitle(const QString&)));
76 m_currentMatch = 0;
78 m_actionView = new QuickSand::QsMatchView(w);
79 layout->addWidget(m_actionView);
80 m_actionView->setTitle(i18n("Actions"));
81 m_actionView->setItemCountSuffix(i18n("actions"));
82 m_actionView->hide();
84 Plasma::Theme *theme = Plasma::Theme::defaultTheme();
85 QString stylesheet = QString("* {color: %1}").arg(theme->color(Plasma::Theme::TextColor).name());
86 setStyleSheet(stylesheet);
87 QColor buttonBgColor = theme->color(Plasma::Theme::BackgroundColor);
88 QString buttonStyleSheet = QString("QToolButton { border: 1px solid %4; border-radius: 4px; padding: 2px;"
89 " background-color: rgba(%1, %2, %3, %5); }")
90 .arg(buttonBgColor.red())
91 .arg(buttonBgColor.green())
92 .arg(buttonBgColor.blue())
93 .arg(theme->color(Plasma::Theme::HighlightColor).name(), "50%");
94 buttonBgColor = theme->color(Plasma::Theme::TextColor);
95 buttonStyleSheet += QString("QToolButton:hover { border: 2px solid %1; }")
96 .arg(theme->color(Plasma::Theme::HighlightColor).name());
97 buttonStyleSheet += QString("QToolButton:focus { border: 2px solid %1; }")
98 .arg(theme->color(Plasma::Theme::HighlightColor).name());
99 m_configButton->setStyleSheet(buttonStyleSheet);
100 m_closeButton->setStyleSheet(buttonStyleSheet);
102 connect(m_runnerManager, SIGNAL(matchesChanged(const QList<Plasma::QueryMatch>&)),
103 this, SLOT(setMatches(const QList<Plasma::QueryMatch>&)));
104 connect(m_matchView, SIGNAL(textChanged(const QString&)), this, SLOT(launchQuery(const QString&)));
105 connect(m_matchView, SIGNAL(selectionChanged(MatchItem*)), this, SLOT(loadActions(MatchItem*)));
106 connect(m_matchView, SIGNAL(itemActivated(MatchItem*)), this, SLOT(run(MatchItem*)));
107 connect(m_actionView, SIGNAL(selectionChanged(MatchItem*)), this, SLOT(setAction(MatchItem*)));
108 connect(m_actionView, SIGNAL(itemActivated(MatchItem*)), this, SLOT(run(MatchItem*)));
110 m_matchView->setFocus();
112 m_newQuery = true;
115 QsDialog::~QsDialog()
118 // FIXME: We still have no notion of history... Actually adaptive search should partly take care of this
119 void QsDialog::clearHistory()
122 void QsDialog::display(const QString &term)
124 KWindowSystem::setOnDesktop(winId(), KWindowSystem::currentDesktop());
126 m_matchView->reset();
127 m_actionView->reset();
128 m_actionView->hide();
129 adjustSize();
130 show();
131 m_matchView->setFocus();
133 KWindowSystem::forceActiveWindow(winId());
134 if (term.isEmpty()) {
135 m_matchView->setTitle(QString());
136 } else {
137 m_matchView->setTitle(term);
138 launchQuery(term);
142 void QsDialog::launchQuery(const QString &query)
144 if (query.isEmpty()) {
145 m_matchView->reset();
146 } else {
147 m_matchView->showLoading();
149 m_runnerManager->launchQuery(query);
150 m_newQuery = true;
153 void QsDialog::run(MatchItem *item)
155 if (QuickSand::QueryMatchItem *match = qobject_cast<QuickSand::QueryMatchItem*>(item)) {
156 m_runnerManager->run(match->match());
157 close();
158 } else if (qobject_cast<QuickSand::QueryActionItem*>(item)) {
159 m_runnerManager->run(m_currentMatch->match());
160 close();
164 void QsDialog::loadActions(MatchItem *item)
166 if (item == m_currentMatch) {
167 return;
169 m_currentMatch = qobject_cast<QuickSand::QueryMatchItem*>(item);
170 QList<MatchItem*> actions;
171 if (m_currentMatch) {
172 QList<QAction*> queryActions = m_runnerManager->actionsForMatch(m_currentMatch->match());
173 foreach(QAction *action, queryActions) {
174 MatchItem *m = new QuickSand::QueryActionItem(action);
175 actions.append(m);
178 if (actions.size()) {
179 m_actionView->show();
180 } else if (m_actionView->isVisible()) {
181 m_actionView->hide();
183 adjustSize();
184 m_actionView->setItems(actions, false);
187 void QsDialog::setMatches(const QList<Plasma::QueryMatch> &matches)
189 QList<MatchItem*> items;
190 QMultiMap<QString, Plasma::QueryMatch> temp;
191 QMultiMap<QString, Plasma::QueryMatch>::iterator end = m_matches.end();
192 foreach (Plasma::QueryMatch match, matches) {
193 temp.insert(match.id(), match);
194 // Do not create new MatchItems for existing matches when the query hasn't changed
195 if (!m_newQuery && m_matches.find(match.id()) != end) {
196 // kDebug() << "A match with id " << match.id() << " already exists." << endl;
197 QList<Plasma::QueryMatch> duplicates = m_matches.values(match.id());
198 bool exists = false;
199 foreach (Plasma::QueryMatch m, duplicates) {
200 // FIXME: Matching the displayed text isn't always reliable
201 // maybe adding an operator== to QueryMatch would help
202 if (m.text() == match.text()) {
203 exists = true;
204 break;
208 if (exists) {
209 continue;
212 MatchItem *m = new QuickSand::QueryMatchItem(match);
213 switch(match.type())
215 case Plasma::QueryMatch::ExactMatch:
216 m->setBackgroundColor(QColor(Qt::yellow));
217 break;
218 case Plasma::QueryMatch::InformationalMatch:
219 case Plasma::QueryMatch::HelperMatch:
220 m->setBackgroundColor(QColor(Qt::blue));
221 break;
222 default:
223 m->setBackgroundColor(QColor(Qt::white));
224 break;
226 items.append(m);
228 // kDebug() << "Add " << items.size() << " matches. Append?" << !m_newQuery << endl;
229 m_matchView->setItems(items, true, !m_newQuery);
230 m_matches = temp;
231 // If new matches are obtained for the same query, append them to the list
232 m_newQuery = false;
235 void QsDialog::setAction(MatchItem *item)
237 if (QuickSand::QueryActionItem *action = qobject_cast<QuickSand::QueryActionItem*>(item)) {
238 m_currentMatch->match().setSelectedAction(action->action());
242 #include "qs_dialog.moc"