• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

kremotecontrol

  • sources
  • kde-4.12
  • kdeutils
  • kremotecontrol
  • kcmremotecontrol
model.cpp
Go to the documentation of this file.
1 /*************************************************************************
2 * Copyright (C) 2009 by Frank Scheffold <fscheffold@googlemail.com> *
3 * Copyright (C) 2009 by Michael Zanetti <michael_zanetti@gmx.net> *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation; either version 2 of *
8 * the License or (at your option) version 3 or any later version *
9 * accepted by the membership of KDE e.V. (or its successor approved *
10 * by the membership of KDE e.V.), which shall act as a proxy *
11 * defined in Section 14 of version 3 of the license. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 *************************************************************************/
21 
28 #include "model.h"
29 
30 #include "dbusinterface.h"
31 #include "profileserver.h"
32 
33 #include <kdebug.h>
34 #include <KLocale>
35 #include <KLineEdit>
36 #include <KComboBox>
37 #include <KIcon>
38 #include <KApplication>
39 
40 #include <QtCore/QVariant>
41 #include <QtCore/QtAlgorithms>
42 #include <QtGui/QSpinBox>
43 #include <QtGui/QCheckBox>
44 #include <QtGui/QDoubleSpinBox>
45 #include <QtCore/QMimeData>
46 
47 
48 /*
49 ***********************************
50 DBusServiceModel
51 ***********************************
52 */
53 
54 DBusServiceModel::DBusServiceModel(QObject* parent): QStandardItemModel(parent) {
55  setHorizontalHeaderLabels(QStringList() << i18nc("Header in a table holding DBus functions", "Application / Node"));
56  foreach(const QString &item, DBusInterface::getInstance()->registeredPrograms()) {
57  DBusServiceItem *dbusServiceItem = new DBusServiceItem(item);
58  dbusServiceItem->setEditable(false);
59  appendRow(dbusServiceItem);
60  foreach(const QString &node, DBusInterface::getInstance()->nodes(item)) {
61  dbusServiceItem->appendRow(new QStandardItem(node));
62  }
63  }
64  sort(0, Qt::AscendingOrder);
65 }
66 
67 QString DBusServiceModel::application(const QModelIndex& index) const {
68  if(index.isValid() && index.parent().isValid()){
69  return data(index.parent(), Qt::UserRole).toString();
70  }
71  return QString();
72 }
73 
74 QString DBusServiceModel::node(const QModelIndex& index) const {
75  if(index.isValid() && index.parent().isValid()){
76  return data(index, Qt::DisplayRole).toString();
77  }
78  return QString();
79 }
80 
81 QModelIndex DBusServiceModel::findOrInsert(const DBusAction* action, bool insert) {
82 
83  for(int i = 0; i < rowCount(); i++){
84  QStandardItem *appItem = item(i);
85  if(!appItem->index().parent().isValid()){ // Only check Applications, no Nodes
86  if(appItem->data(Qt::UserRole).toString() == action->application()){
87  int j = 0;
88  QStandardItem *nodeItem;
89  while((nodeItem = appItem->child(j++)) != 0){
90  if(nodeItem->data(Qt::DisplayRole) == action->node()){
91  kDebug() << "Found item at index:" << nodeItem->index();
92  return nodeItem->index();
93  }
94  }
95  }
96  }
97  }
98  // Not found... Insert it
99  if(insert){
100  kDebug() << "inserting item because app seems not to be registered at DBus";
101  DBusServiceItem *dbusServiceItem = new DBusServiceItem(action->application());
102  dbusServiceItem->setEditable(false);
103  appendRow(dbusServiceItem);
104  QStandardItem *item = new QStandardItem(action->node());
105  dbusServiceItem->appendRow(item);
106  return item->index();
107  }
108  kDebug() << "Not found and not inserting... Returning invalid index";
109  return QModelIndex();
110 }
111 
112 /*
113 ***********************************
114 DBusServiceItem
115 ***********************************
116 */
117 
118 DBusServiceItem::DBusServiceItem(const QString &item) {
119  setData(item, Qt::UserRole);
120  setFlags(Qt::ItemIsEnabled);
121 }
122 
123 DBusServiceItem::DBusServiceItem(const QString &item, const QStringList &objects) {
124  new DBusServiceItem(item);
125  foreach(const QString &object, objects) {
126  this->appendRow(new QStandardItem(object));
127  }
128 }
129 
130 QVariant DBusServiceItem::data(int role) const {
131  if (role == Qt::DisplayRole || role == Qt::EditRole) {
132  return trimAppname(QStandardItem::data(Qt::UserRole).toString());
133  }
134  return QStandardItem::data(role);
135 }
136 
137 QString DBusServiceItem::trimAppname(const QString& appName) {
138  int lastIndex = appName .lastIndexOf(QLatin1String( "." )) + 1;
139  if (lastIndex < appName.size()) {
140  QString s = appName;
141  QString domainName = appName;
142  s.remove(0, lastIndex);
143  domainName.remove(lastIndex -1, domainName.length());
144  return s.append(QLatin1String( " (" )).append( domainName).append(QLatin1Char( ')' ));
145  }
146  return appName;
147 }
148 
149 /*
150 ***********************************
151 DBusFunctionModel
152 ***********************************
153 */
154 
155 DBusFunctionModel::DBusFunctionModel(QObject *parent):QStandardItemModel(parent) {
156  qRegisterMetaType<Prototype*>("Prototype*");
157 }
158 
159 void DBusFunctionModel::refresh(const QString &app, const QString &node) {
160  clear();
161 
162  if(app.isEmpty()){
163  return;
164  }
165 
166  QMultiMap<QString, Prototype> functionMap = DBusInterface::getInstance()->functions(app, node);
167  for (QMultiMap<QString, Prototype>::const_iterator i = functionMap.constBegin(); i != functionMap.constEnd(); ++i){
168  appendRow(i.key(), i.value());
169  }
170 
171  sort(0, Qt::AscendingOrder);
172 
173 }
174 
175 void DBusFunctionModel::appendRow(const QString &interface, Prototype prototype) {
176  QList<QStandardItem*> itemList;
177  QStandardItem *item = new QStandardItem(prototype.name());
178  item->setData(qVariantFromValue(prototype), Qt::UserRole);
179  item->setData(interface, Qt::UserRole + 1);
180  itemList.append(item);
181  QString argString;
182  foreach(const Argument &arg, prototype.args()){
183  if(!argString.isEmpty()){
184  argString += QLatin1String( ", " );
185  }
186  argString += QLatin1String(QVariant::typeToName(arg.value().type()));
187  if(!arg.description().isEmpty()){
188  argString += QLatin1Char( ' ' ) + arg.description();
189  }
190  }
191  itemList.append(new QStandardItem(argString));
192 // itemList.append(new QStandardItem(prototype.name()));
193  QStandardItemModel::appendRow(itemList);
194 }
195 
196 Prototype DBusFunctionModel::getPrototype(int index) const {
197  return QStandardItemModel::item(index)->data(Qt::UserRole).value<Prototype>();
198 }
199 
200 QString DBusFunctionModel::getInterface(int index) const
201 {
202  return QStandardItemModel::item(index)->data(Qt::UserRole+1).toString();
203 }
204 
205 QModelIndex DBusFunctionModel::findOrInsert(const DBusAction* action, bool insert) {
206 
207  for(int i = 0; i < rowCount(); i++){
208  QStandardItem *functionItem = item(i);
209  if(functionItem->data(Qt::UserRole).value<Prototype>() == action->function()){
210  return functionItem->index();
211  }
212  }
213  // Not found... Insert it
214  if(insert){
215  QList<QStandardItem*> itemList;
216  QStandardItem *item = new QStandardItem(action->function().name());
217  item->setData(qVariantFromValue(action->function()), Qt::UserRole);
218  itemList.append(item);
219  QString argString;
220  foreach(const Argument &arg, action->function().args()){
221  if(!argString.isEmpty()){
222  argString += QLatin1String( ", " );
223  }
224  argString += QLatin1String(QVariant::typeToName(arg.value().type()));
225  if(!arg.description().isEmpty()){
226  argString += QLatin1Char( ' ' ) + arg.description();
227  }
228  }
229  itemList.append(new QStandardItem(argString));
230  // itemList.append(new QStandardItem(prototype.name()));
231  QStandardItemModel::appendRow(itemList);
232  return item->index();
233  }
234  kDebug() << "Not found and not inserting... Returning invalid index";
235  return QModelIndex();
236 }
237 
238 QVariant DBusFunctionModel::headerData(int section, Qt::Orientation orientation, int role) const {
239  if (orientation == Qt::Horizontal) {
240  if (role == Qt::DisplayRole) {
241  switch (section) {
242  case 0:
243  return i18n("Function");
244  case 1:
245  return i18n("Parameters");
246  }
247  }
248  }
249  return QVariant();
250 }
251 
252 /*
253 ***********************************
254 ArgumentsModel
255 ***********************************
256 */
257 
258 ArgumentsModel::ArgumentsModel(QObject* parent): QStandardItemModel(parent) {
259 }
260 
261 void ArgumentsModel::refresh(const Prototype& prototype) {
262  clear();
263  foreach(const Argument &arg, prototype.args()){
264  QList<QStandardItem*> itemList;
265  itemList.append(new QStandardItem(QLatin1String(QVariant::typeToName(arg.value().type())) + QLatin1String( ": " ) + arg.description()));
266  itemList.last()->setEditable(false);
267  itemList.append(new ArgumentsModelItem(arg));
268  appendRow(itemList);
269  }
270 }
271 
272 QVariant ArgumentsModel::headerData(int section, Qt::Orientation orientation, int role) const {
273  if (orientation == Qt::Horizontal) {
274  if (role == Qt::DisplayRole) {
275  switch (section) {
276  case 0:
277  return i18n("Description");
278  case 1:
279  return i18n("Value");
280  }
281  }
282  }
283  return QVariant();
284 }
285 
286 QList<Argument> ArgumentsModel::arguments() const {
287  QList<Argument> argList;
288  for(int i = 0; i < rowCount(); i++){
289  argList.append(qVariantValue<Argument>(item(i, 1)->data(Qt::EditRole)));
290  }
291  return argList;
292 }
293 
294 
295 /*
296 ***********************************
297 ArgumentDelegate
298 ***********************************
299 */
300 
301 ArgumentDelegate::ArgumentDelegate(QObject *parent): QItemDelegate(parent) {
302 }
303 
304 QWidget *ArgumentDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &index) const {
305  QWidget *editor;
306  unsigned int maxInt = -1;
307  Argument arg = qVariantValue<Argument>(index.model()->data(index, Qt::EditRole));
308  kDebug() << "creaing edtor for:" << arg.description() << "value:" << arg.value();
309  switch (arg.value().type()) {
310  case QVariant::Int:
311  case QVariant::LongLong: {
312  QSpinBox *spinBox = new QSpinBox(parent);
313  spinBox->setMaximum(maxInt/2);
314  spinBox->setMinimum(maxInt/2* -1);
315  spinBox->setValue(arg.value().toInt());
316  editor = spinBox;
317  }
318  break;
319  case QVariant::UInt: {
320  QSpinBox *spinBox = new QSpinBox(parent);
321  spinBox->setMaximum(maxInt/2);
322  spinBox->setValue(arg.value().toUInt());
323  editor = spinBox;
324  }
325  break;
326  case QVariant::Double: {
327  QDoubleSpinBox *dSpinBox = new QDoubleSpinBox(parent);
328  dSpinBox->setValue(arg.value().toDouble(NULL));
329  editor = dSpinBox;
330  }
331  break;
332  case QVariant::Bool: {
333  KComboBox *comboBox = new KComboBox(parent);
334  comboBox->addItem(i18nc("Value is true", "True"));
335  comboBox->addItem(i18nc("Value is false", "False"));
336  comboBox->setCurrentIndex(arg.value().toBool() ? 0 : 1);
337  editor = comboBox;
338  }
339  break;
340  case QVariant::StringList: {
341  KLineEdit *listLineEdit = new KLineEdit(parent);
342  listLineEdit->setToolTip(i18n("A comma-separated list of Strings"));
343  QString value;
344  value.clear();
345  foreach(const QString &tmp, arg.value().toStringList()) {
346  if (!value.isEmpty()) {
347  value.append(QLatin1Char( ',' ));
348  }
349  value += tmp;
350  }
351  listLineEdit->setText(value);
352 
353  editor = listLineEdit;
354  }
355  break;
356  case QVariant::ByteArray:
357  case QVariant::String:
358  default: {
359  KLineEdit *lineEdit = new KLineEdit(parent);
360  lineEdit->setText(arg.value().toString());
361  editor = lineEdit;
362  }
363  }
364  return editor;
365 }
366 
367 void ArgumentDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
368 
369  Argument arg = qVariantValue<Argument>(index.model()->data(index, Qt::EditRole));
370  switch (arg.value().type()) {
371  case QVariant::UInt:
372  case QVariant::Int:
373  case QVariant::LongLong: {
374  QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
375  spinBox->setValue(arg.value().toInt());
376  break;
377  }
378  case QVariant::Double: {
379  QDoubleSpinBox *doubleSpinBox = static_cast<QDoubleSpinBox*>(editor);
380  doubleSpinBox->setValue(arg.value().toDouble(NULL));
381  break;
382  }
383  case QVariant::Bool: {
384  KComboBox *comboBox = static_cast<KComboBox*>(editor);
385  comboBox->setCurrentIndex(arg.value().toBool() ? 0 : 1);
386  break;
387  }
388  case QVariant::StringList: {
389  KLineEdit *listLineEdit = static_cast<KLineEdit*>(editor);
390  QString value;
391  value.clear();
392  foreach(const QString &tmp, arg.value().toStringList()) {
393  if (!value.isEmpty()) {
394  value.append(QLatin1Char( ',' ));
395  }
396  value += tmp;
397  }
398  listLineEdit->setText(value);
399  break;
400  }
401  case QVariant::ByteArray:
402  case QVariant::String:
403  default: {
404  KLineEdit *lineEdit = static_cast<KLineEdit*>(editor);
405  lineEdit->setText(arg.value().toString());
406  }
407  }
408 }
409 
410 void ArgumentDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
411  QVariant value;
412  Argument arg = qVariantValue<Argument>(index.model()->data(index, Qt::EditRole));
413  switch (arg.value().type()) {
414  case QVariant::Int:
415  case QVariant::UInt:
416  case QVariant::LongLong:
417  value = QVariant(static_cast<QSpinBox*>(editor)->value());
418  break;
419  case QVariant::Double:
420  value = QVariant(static_cast<QDoubleSpinBox*>(editor)->value());
421  break;
422  case QVariant::Bool:
423  value = QVariant(static_cast<KComboBox*>(editor)->currentIndex() == 0 ? true : false);
424  break;
425  case QVariant::StringList:
426  value = QVariant(static_cast<KLineEdit*>(editor)->text().split(QLatin1Char( ',' )));
427  break;
428  case QVariant::ByteArray:
429  case QVariant::String:
430  default: {
431  value = QVariant(static_cast<KLineEdit*>(editor)->text());
432  }
433  }
434  kDebug() << "setting value" << value;
435  arg.setValue(value);
436  model->setData(index, qVariantFromValue(arg), Qt::EditRole);
437 }
438 
439 void ArgumentDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const {
440  editor->setGeometry(option.rect);
441 }
442 
443 
444 /*
445 ***********************************
446 ArgumentsModelItem
447 ***********************************
448 */
449 
450 ArgumentsModelItem::ArgumentsModelItem ( const QString & text ):QStandardItem(text) {
451  setFlags(Qt::ItemIsEnabled);
452 }
453 
454 ArgumentsModelItem::ArgumentsModelItem ( const Argument &arg ) {
455  setData(qVariantFromValue(arg), Qt::EditRole);
456  kDebug() << "creating model item:" << arg.value() << "type:" << arg.value().type();
457  if (arg.value().type() == QVariant::StringList) {
458  setToolTip(i18n("A comma-separated list of Strings"));
459  }
460 }
461 
462 QVariant ArgumentsModelItem::data ( int role ) const {
463 
464  if(role == Qt::DisplayRole) {
465  Argument arg = qVariantValue<Argument>(QStandardItem::data(Qt::EditRole));
466  if(arg.value().type() == QVariant::StringList) {
467  QString retList;
468  retList.clear();
469  foreach(const QString &tmp, arg.value().toStringList()) {
470  if (!retList.isEmpty()) {
471  retList.append(QLatin1Char( ',' ));
472  }
473  retList += tmp;
474  }
475  return QVariant(retList);
476  } else {
477  return arg.value();
478  }
479  } else {
480  return QStandardItem::data(role);
481  }
482 }
483 
484 
485 /*
486 ***********************************
487 ProfileModel
488 ***********************************
489 */
490 
491 ProfileModel::ProfileModel(QObject *parent): QStandardItemModel(parent) {
492  setHorizontalHeaderLabels(QStringList() << i18n("Profile Name"));
493  foreach(Profile *profile, ProfileServer::allProfiles()){
494  QStandardItem *item = new QStandardItem(profile->name());
495  QString tooltip;
496  if(!profile->description().isEmpty()){
497  tooltip.append(profile->description()).append(QLatin1String( "\n" ));
498  }
499  tooltip.append(i18n("Author: %1 (Version: %2)", profile->author(), profile->version()));
500  item->setToolTip(tooltip);
501  item->setData(qVariantFromValue(profile), Qt::UserRole);
502  item->setEditable(false);
503  appendRow(item);
504  }
505  sort(0);
506 }
507 
508 Profile* ProfileModel::profile(const QModelIndex& index) const {
509  if(index.isValid()){
510  return qVariantValue<Profile*>(index.data(Qt::UserRole));
511  }
512  return 0;
513 }
514 
515 QModelIndex ProfileModel::find(const ProfileAction* action) const {
516  for(int i = 0; i < rowCount(); i++){
517  kDebug() << "checking item" << item(i)->data(Qt::UserRole).value<Profile*>()->profileId() << "for" << action->profileId();
518  QStandardItem *profileItem = item(i);
519  if(profileItem->data(Qt::UserRole).value<Profile*>()->profileId() == action->profileId()){
520  return profileItem->index();
521  }
522  }
523  // Not found...
524  return QModelIndex();
525 }
526 
527 
528 /*
529 ***********************************
530 ActionTemplateModel
531 ***********************************
532 */
533 
534 ActionTemplateModel::ActionTemplateModel(QObject* parent): QStandardItemModel(parent) {
535 }
536 
537 ActionTemplateModel::ActionTemplateModel(const Profile* profile, QObject* parent): QStandardItemModel(parent) {
538  refresh(profile);
539 }
540 
541 void ActionTemplateModel::refresh(const Profile* profile) {
542  clear();
543  foreach(const ProfileActionTemplate &profileActionTemplate, profile->actionTemplates()){
544  appendRow(profileActionTemplate);
545  }
546  sort(0);
547 }
548 
549 QVariant ActionTemplateModel::headerData(int section, Qt::Orientation orientation, int role) const {
550  if (orientation == Qt::Horizontal) {
551  if (role == Qt::DisplayRole) {
552  switch (section) {
553  case 0:
554  return i18nc("Profile name", "Name");
555  case 1:
556  return i18n("Description");
557  }
558  }
559  }
560  return QVariant();
561 }
562 
563 ProfileActionTemplate ActionTemplateModel::actionTemplate(const QModelIndex &index) const {
564  return item(index.row())->data(Qt::UserRole).value<ProfileActionTemplate>();
565 }
566 
567 QModelIndex ActionTemplateModel::find(const ProfileAction* action) const {
568  for(int i = 0; i < rowCount(); i++){
569  QStandardItem *templateItem = item(i);
570  if(templateItem->data(Qt::UserRole).value<ProfileActionTemplate>().actionTemplateId() == action->actionTemplateId()){
571  return templateItem->index();
572  }
573  }
574  // Not found...
575  return QModelIndex();
576 }
577 
578 void ActionTemplateModel::appendRow(ProfileActionTemplate actionTemplate) {
579  QList<QStandardItem*> row;
580  QStandardItem *item = new QStandardItem(actionTemplate.actionName());
581  item->setData(qVariantFromValue(actionTemplate), Qt::UserRole);
582  row.append(item);
583 
584  if (!(actionTemplate.description().isEmpty())) {
585  QStandardItem *tItem = new QStandardItem(actionTemplate.description());
586  tItem->setToolTip(actionTemplate.description());
587  row.append(tItem);
588  } else {
589  row.append(new QStandardItem(QLatin1String( "-" )));
590  }
591  row.append(new QStandardItem(QString::number(actionTemplate.function().args().size())));
592  if (!actionTemplate.buttonName().isEmpty()) {
593  row.append(new QStandardItem(actionTemplate.buttonName()));
594  } else {
595  row.append(new QStandardItem(QLatin1String( "-" )));
596  }
597  QStandardItemModel::appendRow(row);
598 }
599 
600 Qt::ItemFlags ActionTemplateModel::flags(const QModelIndex& index) const {
601  return (QStandardItemModel::flags(index) & ~Qt::ItemIsEditable);
602 }
603 
604 
605 /*
606 ***********************************
607 RemoteModel
608 ***********************************
609 */
610 
611 RemoteModel::RemoteModel(QObject* parent): QStandardItemModel(parent) {
612  qRegisterMetaType<Remote*>("Remote*");
613  qRegisterMetaType<Mode*>("Mode*");
614 }
615 
616 RemoteModel::RemoteModel(const RemoteList &remoteList, QObject *parent) : QStandardItemModel(parent) {
617  qRegisterMetaType<Remote*>("Remote*");
618  qRegisterMetaType<Mode*>("Mode*");
619  refresh(remoteList);
620 }
621 
622 void RemoteModel::refresh(const RemoteList &remoteList) {
623  clear();
624  setHorizontalHeaderLabels(QStringList() << i18n("Remotes and modes") << i18n("Button"));
625  foreach(Remote *remote, remoteList){
626  QList<QStandardItem*> itemList;
627  QStandardItem *item = new RemoteItem(remote);
628  itemList.append(item);
629  item = new QStandardItem();
630  item->setData(qVariantFromValue(remote), Qt::UserRole);
631  itemList.append(item);
632  appendRow(itemList);
633  }
634 }
635 
636 Remote *RemoteModel::remote(const QModelIndex &index) const {
637  if(index.isValid() && index.parent().isValid()){
638  return qVariantValue<Remote*>(index.parent().data(Qt::UserRole));
639  }
640  if(index.isValid()){
641  return qVariantValue<Remote*>(index.data(Qt::UserRole));
642  }
643  return 0;
644 }
645 
646 Mode *RemoteModel::mode(const QModelIndex &index) const {
647  if(index.isValid() && index.parent().isValid()){
648  return qVariantValue<Mode*>(index.data(Qt::UserRole));
649  }
650  if(index.isValid()){
651  return qVariantValue<Remote*>(index.data(Qt::UserRole))->masterMode();
652  }
653  return 0;
654 }
655 
656 QModelIndex RemoteModel::find(Mode* mode) const {
657  for(int i = 0; i < rowCount(); i++){
658  QStandardItem *remoteItem = itemFromIndex(index(i, 0));
659  if(remoteItem->data(Qt::UserRole).value<Remote*>()->masterMode() == mode){
660  return remoteItem->index();
661  }
662  for(int j = 0; j < rowCount(remoteItem->index()); j++){
663  QStandardItem *modeItem = itemFromIndex(index(j, 0, remoteItem->index()));
664  if(modeItem->data(Qt::UserRole).value<Mode*>() == mode){
665  return modeItem->index();
666  }
667  }
668  }
669  // Not found...
670  return QModelIndex();
671 }
672 
673 QVariant RemoteModel::data(const QModelIndex& index, int role) const {
674  if(index.isValid() && index.parent().isValid()){
675  if(role == Qt::DisplayRole){
676  switch(index.column()){
677  case 0:
678  return mode(index)->name();
679  case 1:
680  return RemoteControlButton(QString(), mode(index)->button()).description();
681  }
682  }
683  } else if(index.isValid()){
684  if(role == Qt::FontRole){
685  if(index.column() == 0 && !remote(index)->isAvailable()){
686  QFont font = KApplication::font();
687  font.setItalic(true);
688  return font;
689  }
690  }
691  }
692 
693  if(role == Qt::TextColorRole){
694  if(index.column() == 0 && !remote(index)->isAvailable()){
695  return KGlobalSettings::inactiveTextColor();
696  }
697  }
698 
699  return QStandardItemModel::data(index, role);
700 }
701 
702 Qt::ItemFlags RemoteModel::flags(const QModelIndex& index) const {
703  if(index.isValid()) {
704  return (QStandardItemModel::flags(index) | Qt::ItemIsDropEnabled);
705  }
706 
707  return QStandardItemModel::flags(index);
708 }
709 
710 bool RemoteModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
711  Q_UNUSED(row)
712  if (!data->hasFormat(QLatin1String( "kremotecontrol/action" )))
713  return false;
714 
715  if (action == Qt::IgnoreAction)
716  return true;
717 
718  if (column > 0)
719  return false;
720 
721  QByteArray encodedData = data->data(QLatin1String( "kremotecontrol/action" ));
722  QDataStream stream(&encodedData, QIODevice::ReadOnly);
723 
724  quint64 actionPointer;
725  stream >> actionPointer;
726  Action *droppedAction = reinterpret_cast<Action*>(actionPointer);
727  kDebug() << "action pointer is" << droppedAction << "name is" << droppedAction->name();
728 
729  mode(parent)->addAction(droppedAction->clone());
730  emit modeChanged(mode(parent));
731 
732  return true;
733 }
734 
735 QStringList RemoteModel::mimeTypes() const {
736  QStringList types;
737  types << QLatin1String( "kremotecontrol/action" );
738  return types;
739 }
740 
741 Qt::DropActions RemoteModel::supportedDropActions() const {
742  return Qt::CopyAction | Qt::MoveAction;
743 }
744 
745 RemoteItem::RemoteItem(Remote *remote) {
746  setData(qVariantFromValue(remote), Qt::UserRole);
747  foreach(Mode *mode, remote->allModes()) {
748  if(mode->name() != QLatin1String( "Master" )){ // Don't show the Master Mode separately
749  QList<QStandardItem*> itemList;
750  QStandardItem *item = new QStandardItem(mode->name());
751  item->setData(qVariantFromValue(mode), Qt::UserRole);
752  if(remote->defaultMode() == mode){
753  QFont font = KApplication::font();
754  font.setBold(true);
755  item->setFont(font);
756  }
757  item->setIcon(KIcon(mode->iconName()));
758  itemList.append(item);
759  item = new QStandardItem(mode->name());
760  item->setData(qVariantFromValue(mode), Qt::UserRole);
761  itemList.append(item);
762  appendRow(itemList);
763  }
764  }
765 }
766 
767 QVariant RemoteItem::data(int role) const {
768  Remote *remote = qVariantValue<Remote*>(QStandardItem::data(Qt::UserRole));
769  if(role == Qt::DisplayRole) {
770  return remote->name();
771  }
772  if(role == Qt::DecorationRole){
773  if(remote->isAvailable()) {
774  return KIcon(remote->masterMode()->iconName());
775  } else {
776  return KIcon(remote->masterMode()->iconName(), 0, QStringList() << QLatin1String("emblem-important"));
777  }
778  }
779  if(role == Qt::ToolTipRole) {
780  if(!remote->isAvailable()) {
781  return i18n("This remote control is currently not available.");
782  }
783  }
784  return QStandardItem::data(role);
785 }
786 
787 ActionModel::ActionModel(QObject *parent): QStandardItemModel(parent) {
788  setHorizontalHeaderLabels(QStringList() << i18n("Button") << i18n("Application") << i18n("Function"));
789 }
790 
791 void ActionModel::refresh(Mode* mode) {
792  m_mode = mode;
793  removeRows(0, rowCount());
794  foreach(Action *action, mode->actions()){
795  QStandardItem *item = new QStandardItem();
796  item->setData(qVariantFromValue(action), Qt::UserRole);
797  appendRow(item);
798  }
799 }
800 
801 QVariant ActionModel::data(const QModelIndex& index, int role) const {
802  if(role == Qt::DisplayRole){
803  Action *action = qVariantValue<Action*>(item(index.row())->data(Qt::UserRole));
804  switch(index.column()){
805  case 0:
806  return RemoteControlButton(QString(), action->button()).description();
807  case 1:
808  return action->name();
809  case 2:
810  return action->description();
811  }
812  }
813  return QStandardItemModel::data(index, role);
814 }
815 
816 Action* ActionModel::action(const QModelIndex& index) const {
817  if(index.isValid()){
818  return qVariantValue<Action*>(item(index.row())->data(Qt::UserRole));
819  }
820  return 0;
821 }
822 
823 QModelIndex ActionModel::find(Action* action) const {
824  for(int i = 0; i < rowCount(); i++){
825  QModelIndex actionIndex = index(i, 0);
826  QStandardItem *actionItem = itemFromIndex(actionIndex);
827  if(actionItem->data(Qt::UserRole).value<Action*>() == action){
828  return actionItem->index();
829  }
830  }
831  // Not found...
832  return QModelIndex();
833 }
834 
835 Qt::ItemFlags ActionModel::flags(const QModelIndex &index) const {
836  if (index.isValid()) {
837  return (QStandardItemModel::flags(index) | Qt::ItemIsDragEnabled);
838  }
839 
840  return QStandardItemModel::flags(index);
841 }
842 
843 QMimeData *ActionModel::mimeData(const QModelIndexList &indexes) const {
844  QMimeData *mimeData = new QMimeData();
845  QByteArray encodedData;
846 
847  QDataStream stream(&encodedData, QIODevice::WriteOnly);
848 
849  QModelIndex index = indexes.first(); // Only need column 0
850  if(index.isValid()) {
851  Action *dragAction = action(index);
852  kDebug() << "index:" << index << "dragging action pointer is" << dragAction << "name is" << dragAction->name();
853  quint64 actionPointer = reinterpret_cast<quint64>(dragAction);
854  stream << actionPointer;
855  }
856 
857  mimeData->setData(QLatin1String( "kremotecontrol/action" ), encodedData);
858  return mimeData;
859 }
860 
861 Qt::DropActions ActionModel::supportedDragActions() const {
862  return Qt::CopyAction | Qt::MoveAction;
863 }
864 
ActionModel::action
Action * action(const QModelIndex &index) const
Definition: model.cpp:816
RemoteModel::supportedDropActions
Qt::DropActions supportedDropActions() const
Definition: model.cpp:741
Argument
Definition: argument.h:27
ActionModel::ActionModel
ActionModel(QObject *parent=0)
Definition: model.cpp:787
DBusFunctionModel::findOrInsert
QModelIndex findOrInsert(const DBusAction *action, bool insert=false)
Definition: model.cpp:205
ProfileActionTemplate
Definition: profileactiontemplate.h:31
ProfileAction::profileId
QString profileId() const
Definition: profileaction.cpp:34
ArgumentDelegate::createEditor
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: model.cpp:304
ActionTemplateModel::refresh
void refresh(const Profile *profile)
Definition: model.cpp:541
Remote::name
QString name() const
Definition: remote.cpp:247
Mode::actions
QVector< Action * > actions() const
Definition: mode.cpp:89
Prototype::name
QString name() const
Definition: prototype.cpp:28
Action::description
virtual QString description() const =0
DBusFunctionModel::DBusFunctionModel
DBusFunctionModel(QObject *parent)
Definition: model.cpp:155
dbusinterface.h
RemoteModel::remote
Remote * remote(const QModelIndex &index) const
Definition: model.cpp:636
RemoteModel::modeChanged
void modeChanged(Mode *mode)
ActionTemplateModel::ActionTemplateModel
ActionTemplateModel(QObject *parent=0)
Definition: model.cpp:534
DBusAction::node
QString node() const
Definition: dbusaction.cpp:37
DBusAction::function
Prototype function() const
Definition: dbusaction.cpp:55
ArgumentDelegate::setEditorData
void setEditorData(QWidget *editor, const QModelIndex &index) const
Definition: model.cpp:367
DBusServiceItem
Definition: model.h:57
QWidget
Mode
Definition: mode.h:31
Argument::setValue
void setValue(const QVariant &value)
Definition: argument.cpp:36
ActionModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: model.cpp:801
Action::button
QString button() const
Definition: action.cpp:37
RemoteItem::RemoteItem
RemoteItem(Remote *remote)
Definition: model.cpp:745
RemoteModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: model.cpp:702
DBusInterface::getInstance
static DBusInterface * getInstance()
Definition: dbusinterface.cpp:50
ArgumentsModelItem::data
virtual QVariant data(int role=Qt::UserRole+1) const
Definition: model.cpp:462
model.h
QStandardItemModel
ProfileModel::find
QModelIndex find(const ProfileAction *action) const
Definition: model.cpp:515
RemoteModel::refresh
void refresh(const RemoteList &remoteList)
Definition: model.cpp:622
Action::clone
virtual Action * clone() const =0
DBusServiceModel::findOrInsert
QModelIndex findOrInsert(const DBusAction *action, bool insert=false)
Definition: model.cpp:81
ProfileServer::allProfiles
QList< Profile * > allProfiles()
Definition: profileserver.cpp:89
RemoteModel::RemoteModel
RemoteModel(const RemoteList &remoteList, QObject *parent=0)
Definition: model.cpp:616
ActionTemplateModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: model.cpp:600
profileserver.h
QObject
ProfileModel::profile
Profile * profile(const QModelIndex &index) const
Definition: model.cpp:508
RemoteModel::mimeTypes
QStringList mimeTypes() const
Definition: model.cpp:735
Remote::allModes
QVector< Mode * > allModes() const
Definition: remote.cpp:269
RemoteModel::data
virtual QVariant data(const QModelIndex &index, int role) const
Definition: model.cpp:673
ArgumentsModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role) const
Definition: model.cpp:272
Action
Definition: action.h:30
Argument::description
QString description() const
Definition: argument.cpp:40
RemoteModel::mode
Mode * mode(const QModelIndex &index) const
Definition: model.cpp:646
DBusAction
Definition: dbusaction.h:32
DBusFunctionModel::refresh
void refresh(const QString &app, const QString &node)
Definition: model.cpp:159
Prototype
Definition: prototype.h:28
Action::name
virtual QString name() const =0
DBusInterface::functions
QMultiMap< QString, Prototype > functions(const QString &program, const QString &object)
Definition: dbusinterface.cpp:172
ProfileActionTemplate::function
Prototype function() const
Definition: profileactiontemplate.cpp:87
RemoteItem::data
virtual QVariant data(int role) const
Definition: model.cpp:767
RemoteModel::dropMimeData
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Definition: model.cpp:710
ArgumentsModelItem
Definition: model.h:99
ProfileActionTemplate::buttonName
QString buttonName() const
Definition: profileactiontemplate.cpp:103
ProfileAction::actionTemplateId
QString actionTemplateId() const
Definition: profileaction.cpp:42
ActionModel::supportedDragActions
Qt::DropActions supportedDragActions() const
Definition: model.cpp:861
DBusServiceModel::DBusServiceModel
DBusServiceModel(QObject *parent=0)
Created on: 01.02.2009.
Definition: model.cpp:54
RemoteModel::find
QModelIndex find(Mode *mode) const
Definition: model.cpp:656
Profile::author
QString author() const
Definition: profile.cpp:60
Profile::actionTemplates
QList< ProfileActionTemplate > actionTemplates() const
Definition: profile.cpp:76
ProfileModel::ProfileModel
ProfileModel(QObject *parent=0)
Definition: model.cpp:491
ArgumentsModel::arguments
QList< Argument > arguments() const
Definition: model.cpp:286
DBusServiceModel::node
QString node(const QModelIndex &index) const
Definition: model.cpp:74
ActionModel::find
QModelIndex find(Action *action) const
Definition: model.cpp:823
DBusFunctionModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role) const
Definition: model.cpp:238
QItemDelegate
Profile::version
QString version() const
Definition: profile.cpp:98
Prototype::args
QList< Argument > args() const
Definition: prototype.cpp:32
DBusServiceModel::application
QString application(const QModelIndex &index) const
Definition: model.cpp:67
ArgumentsModel::ArgumentsModel
ArgumentsModel(QObject *parent=0)
Definition: model.cpp:258
ArgumentDelegate::ArgumentDelegate
ArgumentDelegate(QObject *parent=0)
Definition: model.cpp:301
Mode::addAction
void addAction(Action *action)
Add the given Action to the Mode.
Definition: mode.cpp:64
RemoteControlButton::description
QString description() const
Retrieves the description of the Button.
Definition: remotecontrolbutton.cpp:320
ActionTemplateModel::find
QModelIndex find(const ProfileAction *action) const
Definition: model.cpp:567
ArgumentDelegate::updateEditorGeometry
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: model.cpp:439
ArgumentsModelItem::ArgumentsModelItem
ArgumentsModelItem(const QString &text)
Definition: model.cpp:450
ActionModel::mimeData
QMimeData * mimeData(const QModelIndexList &indexes) const
Definition: model.cpp:843
KComboBox
Remote::masterMode
Mode * masterMode() const
Definition: remote.cpp:300
ProfileServer::profile
Profile * profile(const QString &profileId)
Definition: profileserver.cpp:93
ActionModel::refresh
void refresh(Mode *mode)
Definition: model.cpp:791
DBusAction::application
QString application() const
Definition: dbusaction.cpp:29
DBusServiceItem::DBusServiceItem
DBusServiceItem(const QString &item)
Definition: model.cpp:118
Remote::defaultMode
Mode * defaultMode() const
Definition: remote.cpp:313
ActionModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: model.cpp:835
ArgumentsModel::refresh
void refresh(const Prototype &prototype)
Definition: model.cpp:261
Profile
Definition: profile.h:26
Argument::value
QVariant value() const
Definition: argument.cpp:32
QStandardItem
Profile::name
QString name() const
Definition: profile.cpp:56
DBusServiceItem::data
virtual QVariant data(int role) const
Definition: model.cpp:130
RemoteItem
Definition: model.h:168
DBusFunctionModel::getPrototype
Prototype getPrototype(int index) const
Definition: model.cpp:196
DBusFunctionModel::getInterface
QString getInterface(int index) const
Definition: model.cpp:200
ProfileActionTemplate::actionName
QString actionName() const
Definition: profileactiontemplate.cpp:66
ActionTemplateModel::actionTemplate
ProfileActionTemplate actionTemplate(const QModelIndex &index) const
Definition: model.cpp:563
ProfileAction
Definition: profileaction.h:26
Mode::name
QString name() const
Definition: mode.cpp:36
RemoteList
Definition: remotelist.h:28
RemoteControlButton
Definition: remotecontrolbutton.h:30
ActionTemplateModel::appendRow
void appendRow(ProfileActionTemplate actionTemplate)
Definition: model.cpp:578
Remote::isAvailable
bool isAvailable() const
Definition: remote.cpp:355
Profile::description
QString description() const
Definition: profile.cpp:64
ActionTemplateModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role) const
Definition: model.cpp:549
ArgumentDelegate::setModelData
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Definition: model.cpp:410
Remote
Definition: remote.h:32
Mode::iconName
QString iconName() const
Definition: mode.cpp:40
ProfileActionTemplate::description
QString description() const
Definition: profileactiontemplate.cpp:78
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:43 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kremotecontrol

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

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

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