Plasma
querymatch.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright 2006-2007 Aaron Seigo <aseigo@kde.org> 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU Library General Public License as 00006 * published by the Free Software Foundation; either version 2, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details 00013 * 00014 * You should have received a copy of the GNU Library General Public 00015 * License along with this program; if not, write to the 00016 * Free Software Foundation, Inc., 00017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "querymatch.h" 00021 00022 #include <QAction> 00023 #include <QIcon> 00024 #include <QReadWriteLock> 00025 #include <QSharedData> 00026 #include <QStringList> 00027 #include <QVariant> 00028 #include <QWeakPointer> 00029 00030 #include <kdebug.h> 00031 00032 #include "abstractrunner.h" 00033 00034 namespace Plasma 00035 { 00036 00037 class QueryMatchPrivate : public QSharedData 00038 { 00039 public: 00040 QueryMatchPrivate(AbstractRunner *r) 00041 : QSharedData(), 00042 lock(new QReadWriteLock(QReadWriteLock::Recursive)), 00043 runner(r), 00044 type(QueryMatch::ExactMatch), 00045 relevance(.7), 00046 selAction(0), 00047 enabled(true), 00048 idSetByData(false) 00049 { 00050 } 00051 00052 QueryMatchPrivate(const QueryMatchPrivate &other) 00053 : QSharedData(other), 00054 lock(new QReadWriteLock(QReadWriteLock::Recursive)) 00055 { 00056 QReadLocker lock(other.lock); 00057 runner = other.runner; 00058 type = other.type; 00059 relevance = other.relevance; 00060 selAction = other.selAction; 00061 enabled = other.enabled; 00062 idSetByData = other.idSetByData; 00063 id = other.id; 00064 text = other.text; 00065 subtext = other.subtext; 00066 icon = other.icon; 00067 data = other.data; 00068 } 00069 00070 ~QueryMatchPrivate() 00071 { 00072 delete lock; 00073 } 00074 00075 QReadWriteLock *lock; 00076 QWeakPointer<AbstractRunner> runner; 00077 QueryMatch::Type type; 00078 QString id; 00079 QString text; 00080 QString subtext; 00081 QIcon icon; 00082 QVariant data; 00083 qreal relevance; 00084 QAction *selAction; 00085 bool enabled : 1; 00086 bool idSetByData : 1; 00087 }; 00088 00089 QueryMatch::QueryMatch(AbstractRunner *runner) 00090 : d(new QueryMatchPrivate(runner)) 00091 { 00092 // kDebug() << "new match created"; 00093 } 00094 00095 QueryMatch::QueryMatch(const QueryMatch &other) 00096 : d(other.d) 00097 { 00098 } 00099 00100 QueryMatch::~QueryMatch() 00101 { 00102 } 00103 00104 bool QueryMatch::isValid() const 00105 { 00106 return d->runner != 0; 00107 } 00108 00109 QString QueryMatch::id() const 00110 { 00111 if (d->id.isEmpty() && d->runner) { 00112 return d->runner.data()->id(); 00113 } 00114 00115 return d->id; 00116 } 00117 00118 void QueryMatch::setType(Type type) 00119 { 00120 d->type = type; 00121 } 00122 00123 QueryMatch::Type QueryMatch::type() const 00124 { 00125 return d->type; 00126 } 00127 00128 void QueryMatch::setRelevance(qreal relevance) 00129 { 00130 d->relevance = qMax(qreal(0.0), relevance); 00131 } 00132 00133 qreal QueryMatch::relevance() const 00134 { 00135 return d->relevance; 00136 } 00137 00138 AbstractRunner* QueryMatch::runner() const 00139 { 00140 return d->runner.data(); 00141 } 00142 00143 void QueryMatch::setText(const QString &text) 00144 { 00145 QWriteLocker locker(d->lock); 00146 d->text = text; 00147 } 00148 00149 void QueryMatch::setSubtext(const QString &subtext) 00150 { 00151 QWriteLocker locker(d->lock); 00152 d->subtext = subtext; 00153 } 00154 00155 void QueryMatch::setData(const QVariant & data) 00156 { 00157 QWriteLocker locker(d->lock); 00158 d->data = data; 00159 00160 if (d->id.isEmpty() || d->idSetByData) { 00161 const QString id = data.toString(); 00162 if (!id.isEmpty()) { 00163 setId(data.toString()); 00164 d->idSetByData = true; 00165 } 00166 } 00167 } 00168 00169 void QueryMatch::setId(const QString &id) 00170 { 00171 QWriteLocker locker(d->lock); 00172 if (d->runner) { 00173 d->id = d->runner.data()->id(); 00174 } 00175 00176 if (!id.isEmpty()) { 00177 d->id.append('_').append(id); 00178 } 00179 00180 d->idSetByData = false; 00181 } 00182 00183 void QueryMatch::setIcon(const QIcon &icon) 00184 { 00185 QWriteLocker locker(d->lock); 00186 d->icon = icon; 00187 } 00188 00189 QVariant QueryMatch::data() const 00190 { 00191 QReadLocker locker(d->lock); 00192 return d->data; 00193 } 00194 00195 QString QueryMatch::text() const 00196 { 00197 QReadLocker locker(d->lock); 00198 return d->text; 00199 } 00200 00201 QString QueryMatch::subtext() const 00202 { 00203 QReadLocker locker(d->lock); 00204 return d->subtext; 00205 } 00206 00207 QIcon QueryMatch::icon() const 00208 { 00209 QReadLocker locker(d->lock); 00210 return d->icon; 00211 } 00212 00213 void QueryMatch::setEnabled(bool enabled) 00214 { 00215 d->enabled = enabled; 00216 } 00217 00218 bool QueryMatch::isEnabled() const 00219 { 00220 return d->enabled && d->runner; 00221 } 00222 00223 QAction* QueryMatch::selectedAction() const 00224 { 00225 return d->selAction; 00226 } 00227 00228 void QueryMatch::setSelectedAction(QAction *action) 00229 { 00230 d->selAction = action; 00231 } 00232 00233 bool QueryMatch::operator<(const QueryMatch &other) const 00234 { 00235 if (d->type == other.d->type) { 00236 if (isEnabled() != other.isEnabled()) { 00237 return other.isEnabled(); 00238 } 00239 00240 if (d->relevance != other.d->relevance) { 00241 return d->relevance < other.d->relevance; 00242 } 00243 00244 QReadLocker locker(d->lock); 00245 QReadLocker otherLocker(other.d->lock); 00246 // when resorting to sort by alpha, we want the 00247 // reverse sort order! 00248 return d->text > other.d->text; 00249 } 00250 00251 return d->type < other.d->type; 00252 } 00253 00254 QueryMatch &QueryMatch::operator=(const QueryMatch &other) 00255 { 00256 if (d != other.d) { 00257 d = other.d; 00258 } 00259 00260 return *this; 00261 } 00262 00263 bool QueryMatch::operator==(const QueryMatch &other) const 00264 { 00265 return (d == other.d); 00266 } 00267 00268 void QueryMatch::run(const RunnerContext &context) const 00269 { 00270 //kDebug() << "we run the term" << context->query() << "whose type is" << context->mimetype(); 00271 if (d->runner) { 00272 d->runner.data()->run(context, *this); 00273 } 00274 } 00275 00276 bool QueryMatch::hasConfigurationInterface() const 00277 { 00278 return d->runner && d->runner.data()->hasRunOptions(); 00279 } 00280 00281 void QueryMatch::createConfigurationInterface(QWidget *parent) 00282 { 00283 if (hasConfigurationInterface()) { 00284 d->runner.data()->createRunOptions(parent); 00285 } 00286 } 00287 00288 } // Plasma namespace 00289
KDE 4.7 API Reference