• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kde-runtime API Reference
  • KDE Home
  • Contact Us
 

PlasmaCore

  • sources
  • kde-4.14
  • kde-runtime
  • plasma
  • declarativeimports
  • core
runnermodel.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2011 Aaron Seigo <aseigo@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "runnermodel.h"
21 
22 #include <QIcon>
23 #include <QAction>
24 #include <QTimer>
25 
26 #include <KDebug>
27 
28 #include <Plasma/RunnerManager>
29 
30 RunnerModel::RunnerModel(QObject *parent)
31  : QAbstractListModel(parent),
32  m_manager(0),
33  m_startQueryTimer(new QTimer(this)),
34  m_runningChangedTimeout(new QTimer(this)),
35  m_running(false)
36 {
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");
43  roles.insert(Relevance, "relevance");
44  roles.insert(Data, "data");
45  roles.insert(Id, "id");
46  roles.insert(SubText, "description");
47  roles.insert(Enabled, "enabled");
48  roles.insert(RunnerId, "runnerid");
49  roles.insert(RunnerName, "runnerName");
50  roles.insert(Actions, "actions");
51  setRoleNames(roles);
52 
53  m_startQueryTimer->setSingleShot(true);
54  m_startQueryTimer->setInterval(10);
55  connect(m_startQueryTimer, SIGNAL(timeout()), this, SLOT(startQuery()));
56 
57  //FIXME: HACK: some runners stay in a running but finished state, not possible to say if it's actually over
58  m_runningChangedTimeout->setSingleShot(true);
59  connect(m_runningChangedTimeout, SIGNAL(timeout()), this, SLOT(queryHasFinished()));
60 }
61 
62 int RunnerModel::rowCount(const QModelIndex& index) const
63 {
64  return index.isValid() ? 0 : m_matches.count();
65 }
66 
67 int RunnerModel::count() const
68 {
69  return m_matches.count();
70 }
71 
72 QStringList RunnerModel::runners() const
73 {
74  return m_manager ? m_manager->allowedRunners() : m_pendingRunnersList;
75 }
76 
77 void RunnerModel::setRunners(const QStringList &allowedRunners)
78 {
79  //use sets to ensure comparison is order-independent
80  if (allowedRunners.toSet() == runners().toSet()) {
81  return;
82  }
83  if (m_manager) {
84  m_manager->setAllowedRunners(allowedRunners);
85 
86  //automagically enter single runner mode if there's only 1 allowed runner
87  m_manager->setSingleMode(allowedRunners.count() == 1);
88  } else {
89  m_pendingRunnersList = allowedRunners;
90  kDebug() << "runners set" << m_pendingRunnersList.count();
91  }
92 
93  // to trigger single runner fun!
94  if (allowedRunners.count() == 1) {
95  m_singleRunnerId = allowedRunners.first();
96  scheduleQuery(QString());
97  } else {
98  m_singleRunnerId.clear();
99  }
100  emit runnersChanged();
101 }
102 
103 void RunnerModel::run(int index)
104 {
105  if (index >= 0 && index < m_matches.count()) {
106  m_manager->run(m_matches.at(index));
107  }
108 }
109 
110 bool RunnerModel::isRunning() const
111 {
112  return m_running;
113 }
114 
115 QVariant RunnerModel::data(const QModelIndex &index, int role) const
116 {
117  if (!index.isValid() || index.parent().isValid() ||
118  index.column() > 0 || index.row() < 0 || index.row() >= m_matches.count()) {
119  // index requested must be valid, but we have no child items!
120  //kDebug() << "invalid index requested";
121  return QVariant();
122  }
123 
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();
130  } else if (role == Relevance) {
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();
136  } else if (role == SubText) {
137  return m_matches.at(index.row()).subtext();
138  } else if (role == Enabled) {
139  return m_matches.at(index.row()).isEnabled();
140  } else if (role == RunnerId) {
141  return m_matches.at(index.row()).runner()->id();
142  } else if (role == RunnerName) {
143  return m_matches.at(index.row()).runner()->name();
144  } else if (role == Actions) {
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);
150  }
151  return actions;
152  }
153 
154  return QVariant();
155 }
156 
157 QString RunnerModel::currentQuery() const
158 {
159  return m_manager ? m_manager->query() : QString();
160 }
161 
162 void RunnerModel::scheduleQuery(const QString &query)
163 {
164  m_pendingQuery = query;
165  m_startQueryTimer->start();
166 }
167 
168 void RunnerModel::startQuery()
169 {
170  // avoid creating a manager just so we can run nothing
171  // however, if we have one pending runner, then we'll be in single query mode
172  // and a null query is a valid query
173  if (!m_manager && m_pendingRunnersList.count() != 1 && m_pendingQuery.isEmpty()) {
174  return;
175  }
176 
177  //kDebug() << "!!!!!!!!!!!!!" << m_pendingQuery << m_manager;
178 
179  if (createManager() || m_pendingQuery != m_manager->query()) {
180  //kDebug() << "running query" << m_pendingQuery << m_manager;
181  m_manager->launchQuery(m_pendingQuery, m_singleRunnerId);
182  emit queryChanged();
183  m_running = true;
184  emit runningChanged(true);
185  }
186 }
187 
188 bool RunnerModel::createManager()
189 {
190  if (!m_manager) {
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()));
196 
197  if (!m_pendingRunnersList.isEmpty()) {
198  setRunners(m_pendingRunnersList);
199  m_pendingRunnersList.clear();
200  }
201  //connect(m_manager, SIGNAL(queryFinished()), this, SLOT(queryFinished()));
202  return true;
203  }
204 
205  return false;
206 }
207 
208 void RunnerModel::matchesChanged(const QList<Plasma::QueryMatch> &matches)
209 {
210  //kDebug() << "got matches:" << matches.count();
211  bool fullReset = false;
212  int oldCount = m_matches.count();
213  int newCount = matches.count();
214  if (newCount > oldCount) {
215  // We received more matches than we had. If all common matches are the
216  // same, we can just append new matches instead of resetting the whole
217  // model
218  for (int row = 0; row < oldCount; ++row) {
219  if (!(m_matches.at(row) == matches.at(row))) {
220  fullReset = true;
221  break;
222  }
223  }
224  if (!fullReset) {
225  // Not a full reset, inserting rows
226  beginInsertRows(QModelIndex(), oldCount, newCount - 1);
227  m_matches = matches;
228  endInsertRows();
229  emit countChanged();
230  }
231  } else {
232  fullReset = true;
233  }
234 
235  if (fullReset) {
236  beginResetModel();
237  m_matches = matches;
238  endResetModel();
239  emit countChanged();
240  }
241  m_runningChangedTimeout->start(3000);
242 }
243 
244 void RunnerModel::queryHasFinished()
245 {
246  m_running = false;
247  emit runningChanged(false);
248 }
249 
250 #include "runnermodel.moc"
251 
QTimer::setInterval
void setInterval(int msec)
QList::clear
void clear()
QModelIndex
QHash::insert
iterator insert(const Key &key, const T &value)
QAbstractItemModel::setRoleNames
void setRoleNames(const QHash< int, QByteArray > &roleNames)
runnermodel.h
RunnerModel::runnersChanged
void runnersChanged()
RunnerModel::SubText
Definition: runnermodel.h:74
RunnerModel::Actions
Definition: runnermodel.h:78
RunnerModel::Label
Definition: runnermodel.h:69
RunnerModel::RunnerModel
RunnerModel(QObject *parent=0)
Definition: runnermodel.cpp:30
QList::at
const T & at(int i) const
QList::toSet
QSet< T > toSet() const
RunnerModel::Type
Definition: runnermodel.h:68
RunnerModel::Relevance
Definition: runnermodel.h:71
RunnerModel::Enabled
Definition: runnermodel.h:75
RunnerModel::count
int count() const
QString::clear
void clear()
QAbstractItemModel::beginResetModel
void beginResetModel()
RunnerModel::rowCount
int rowCount(const QModelIndex &) const
Definition: runnermodel.cpp:62
QModelIndex::isValid
bool isValid() const
QList::count
int count(const T &value) const
QTimer
QHash< int, QByteArray >
QAbstractItemModel::endInsertRows
void endInsertRows()
QObject
QAbstractListModel
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
QModelIndex::row
int row() const
RunnerModel::Id
Definition: runnermodel.h:73
QList::first
T & first()
RunnerModel::currentQuery
QString currentQuery() const
Definition: runnermodel.cpp:157
QString
QList
QModelIndex::parent
QModelIndex parent() const
QStringList
RunnerModel::Icon
Definition: runnermodel.h:70
RunnerModel::data
QVariant data(const QModelIndex &, int) const
Definition: runnermodel.cpp:115
QAbstractItemModel::beginInsertRows
void beginInsertRows(const QModelIndex &parent, int first, int last)
RunnerModel::queryChanged
void queryChanged()
RunnerModel::scheduleQuery
void scheduleQuery(const QString &query)
Definition: runnermodel.cpp:162
RunnerModel::RunnerId
Definition: runnermodel.h:76
RunnerModel::Data
Definition: runnermodel.h:72
RunnerModel::isRunning
bool isRunning() const
Definition: runnermodel.cpp:110
RunnerModel::RunnerName
Definition: runnermodel.h:77
QAction
RunnerModel::runners
QStringList runners() const
QModelIndex::column
int column() const
QTimer::start
void start(int msec)
RunnerModel::query
QString query
Definition: runnermodel.h:46
RunnerModel::run
Q_SCRIPTABLE void run(int row)
Definition: runnermodel.cpp:103
QAbstractItemModel::endResetModel
void endResetModel()
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
RunnerModel::countChanged
void countChanged()
RunnerModel::setRunners
void setRunners(const QStringList &allowedRunners)
Definition: runnermodel.cpp:77
RunnerModel::runningChanged
void runningChanged(bool running)
QTimer::setSingleShot
void setSingleShot(bool singleShot)
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:08:28 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

PlasmaCore

Skip menu "PlasmaCore"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kde-runtime API Reference

Skip menu "kde-runtime API Reference"
  • KCMShell
  • KNotify
  • Plasma Runtime
  •     PlasmaCore
  •     DragAndDrop
  •     PlasmaComponents
  •     PlasmaExtraComponents
  •     QtExtraComponents

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal