libplasma
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 <QPointer> 00023 #include <QVariant> 00024 #include <QSharedData> 00025 #include <QStringList> 00026 #include <QIcon> 00027 00028 #include <KDebug> 00029 00030 #include "abstractrunner.h" 00031 00032 namespace Plasma 00033 { 00034 00035 class QueryMatchPrivate : public QSharedData 00036 { 00037 public: 00038 QueryMatchPrivate(AbstractRunner *r) 00039 : QSharedData(), 00040 runner(r), 00041 type(QueryMatch::ExactMatch), 00042 enabled(true), 00043 relevance(.7) 00044 { 00045 } 00046 00047 QPointer<AbstractRunner> runner; 00048 QueryMatch::Type type; 00049 QString id; 00050 QString text; 00051 QString subtext; 00052 QIcon icon; 00053 QVariant data; 00054 bool enabled; 00055 qreal relevance; 00056 }; 00057 00058 00059 QueryMatch::QueryMatch(AbstractRunner *runner) 00060 : d(new QueryMatchPrivate(runner)) 00061 { 00062 if (runner) { 00063 d->id = runner->id(); 00064 } 00065 // kDebug() << "new match created"; 00066 } 00067 00068 QueryMatch::QueryMatch(const QueryMatch &other) 00069 : d(other.d) 00070 { 00071 } 00072 00073 QueryMatch::~QueryMatch() 00074 { 00075 } 00076 00077 bool QueryMatch::isValid() const 00078 { 00079 return d->runner != 0; 00080 } 00081 00082 QString QueryMatch::id() const 00083 { 00084 return d->id; 00085 } 00086 00087 void QueryMatch::setType(Type type) 00088 { 00089 d->type = type; 00090 } 00091 00092 QueryMatch::Type QueryMatch::type() const 00093 { 00094 return d->type; 00095 } 00096 00097 void QueryMatch::setRelevance(qreal relevance) 00098 { 00099 d->relevance = qMax(qreal(0.0), qMin(qreal(1.0), relevance)); 00100 } 00101 00102 qreal QueryMatch::relevance() const 00103 { 00104 return d->relevance; 00105 } 00106 00107 AbstractRunner* QueryMatch::runner() const 00108 { 00109 return d->runner; 00110 } 00111 00112 void QueryMatch::setText(const QString& text) 00113 { 00114 d->text = text; 00115 } 00116 00117 void QueryMatch::setSubtext(const QString& subtext) 00118 { 00119 d->subtext = subtext; 00120 } 00121 00122 void QueryMatch::setData(const QVariant& data) 00123 { 00124 d->data = data; 00125 setId(data.toString()); 00126 } 00127 00128 void QueryMatch::setId(const QString &id) 00129 { 00130 if (d->runner) { 00131 d->id = d->runner->id(); 00132 } 00133 00134 00135 if (!id.isEmpty()) { 00136 d->id.append('_').append(id); 00137 } 00138 } 00139 00140 void QueryMatch::setIcon(const QIcon& icon) 00141 { 00142 d->icon = icon; 00143 } 00144 00145 QVariant QueryMatch::data() const 00146 { 00147 return d->data; 00148 } 00149 00150 QString QueryMatch::text() const 00151 { 00152 return d->text; 00153 } 00154 00155 QString QueryMatch::subtext() const 00156 { 00157 return d->subtext; 00158 } 00159 00160 QIcon QueryMatch::icon() const 00161 { 00162 return d->icon; 00163 } 00164 00165 void QueryMatch::setEnabled( bool enabled ) 00166 { 00167 d->enabled = enabled; 00168 } 00169 00170 bool QueryMatch::isEnabled() const 00171 { 00172 return d->enabled && d->runner; 00173 } 00174 00175 bool QueryMatch::operator<(const QueryMatch& other) const 00176 { 00177 if (d->type == other.d->type) { 00178 if (isEnabled() != other.isEnabled()) { 00179 return other.isEnabled(); 00180 } 00181 00182 if (d->relevance != other.d->relevance) { 00183 return d->relevance < other.d->relevance; 00184 } 00185 00186 // when resorting to sort by alpha, we want the 00187 // reverse sort order! 00188 return d->text > other.d->text; 00189 } 00190 00191 return d->type < other.d->type; 00192 } 00193 00194 QueryMatch& QueryMatch::operator=(const QueryMatch &other) 00195 { 00196 if (d != other.d) { 00197 d = other.d; 00198 } 00199 00200 return *this; 00201 } 00202 00203 void QueryMatch::run(const RunnerContext &context) const 00204 { 00205 //kDebug() << "we run the term" << context->query() << "whose type is" << context->mimetype(); 00206 if (d->runner) { 00207 d->runner->run(context, *this); 00208 } 00209 } 00210 00211 } // Plasma namespace 00212