28 #include <Plasma/RunnerManager>
33 m_startQueryTimer(new QTimer(this)),
34 m_runningChangedTimeout(new QTimer(this)),
37 QHash<int, QByteArray> roles;
38 roles.insert(Qt::DisplayRole,
"display");
39 roles.insert(Qt::DecorationRole,
"decoration");
40 roles.insert(
Label,
"label");
41 roles.insert(
Icon,
"icon");
42 roles.insert(
Type,
"type");
44 roles.insert(
Data,
"data");
45 roles.insert(
Id,
"id");
46 roles.insert(
SubText,
"description");
47 roles.insert(
Enabled,
"enabled");
50 roles.insert(
Actions,
"actions");
53 m_startQueryTimer->setSingleShot(
true);
54 m_startQueryTimer->setInterval(10);
55 connect(m_startQueryTimer, SIGNAL(timeout()),
this, SLOT(startQuery()));
58 m_runningChangedTimeout->setSingleShot(
true);
59 connect(m_runningChangedTimeout, SIGNAL(timeout()),
this, SLOT(queryHasFinished()));
64 return index.isValid() ? 0 : m_matches.count();
69 return m_matches.count();
74 return m_manager ? m_manager->allowedRunners() : m_pendingRunnersList;
80 if (allowedRunners.toSet() ==
runners().toSet()) {
84 m_manager->setAllowedRunners(allowedRunners);
87 m_manager->setSingleMode(allowedRunners.count() == 1);
89 m_pendingRunnersList = allowedRunners;
90 kDebug() <<
"runners set" << m_pendingRunnersList.count();
94 if (allowedRunners.count() == 1) {
95 m_singleRunnerId = allowedRunners.first();
98 m_singleRunnerId.clear();
105 if (index >= 0 && index < m_matches.count()) {
106 m_manager->run(m_matches.at(index));
117 if (!index.isValid() || index.parent().isValid() ||
118 index.column() > 0 || index.row() < 0 || index.row() >= m_matches.count()) {
124 if (role == Qt::DisplayRole || role ==
Label) {
125 return m_matches.at(index.row()).text();
126 }
else if (role == Qt::DecorationRole || role ==
Icon) {
127 return m_matches.at(index.row()).icon();
128 }
else if (role ==
Type) {
129 return m_matches.at(index.row()).type();
131 return m_matches.at(index.row()).relevance();
132 }
else if (role ==
Data) {
133 return m_matches.at(index.row()).
data();
134 }
else if (role ==
Id) {
135 return m_matches.at(index.row()).
id();
137 return m_matches.at(index.row()).subtext();
139 return m_matches.at(index.row()).isEnabled();
141 return m_matches.at(index.row()).runner()->id();
143 return m_matches.at(index.row()).runner()->name();
145 QVariantList actions;
146 Plasma::QueryMatch amatch = m_matches.at(index.row());
147 QList<QAction*> theactions = m_manager->actionsForMatch(amatch);
148 foreach(QAction* action, theactions) {
149 actions += qVariantFromValue<QObject*>(action);
159 return m_manager ? m_manager->query() : QString();
164 m_pendingQuery =
query;
165 m_startQueryTimer->start();
168 void RunnerModel::startQuery()
173 if (!m_manager && m_pendingRunnersList.count() != 1 && m_pendingQuery.isEmpty()) {
179 if (createManager() || m_pendingQuery != m_manager->query()) {
181 m_manager->launchQuery(m_pendingQuery, m_singleRunnerId);
188 bool RunnerModel::createManager()
191 m_manager =
new Plasma::RunnerManager(
this);
192 connect(m_manager, SIGNAL(matchesChanged(QList<Plasma::QueryMatch>)),
193 this, SLOT(matchesChanged(QList<Plasma::QueryMatch>)));
194 connect(m_manager, SIGNAL(queryFinished()),
195 this, SLOT(queryHasFinished()));
197 if (!m_pendingRunnersList.isEmpty()) {
199 m_pendingRunnersList.clear();
208 void RunnerModel::matchesChanged(
const QList<Plasma::QueryMatch> &matches)
211 bool fullReset =
false;
212 int oldCount = m_matches.count();
213 int newCount = matches.count();
214 if (newCount > oldCount) {
218 for (
int row = 0; row < oldCount; ++row) {
219 if (!(m_matches.at(row) == matches.at(row))) {
226 beginInsertRows(QModelIndex(), oldCount, newCount - 1);
241 m_runningChangedTimeout->start(3000);
244 void RunnerModel::queryHasFinished()
250 #include "runnermodel.moc"
RunnerModel(QObject *parent=0)
int rowCount(const QModelIndex &) const
QString currentQuery() const
QVariant data(const QModelIndex &, int) const
void scheduleQuery(const QString &query)
QStringList runners() const
Q_SCRIPTABLE void run(int row)
void setRunners(const QStringList &allowedRunners)
void runningChanged(bool running)