dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
notifyitemdelegate.cpp
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 
30 #include <QtGui>
31 #include "notifyitemdelegate.h"
32 #include "notifytablemodel.h"
33 #include "notifylogging.h"
34 #include "notificationitem.h"
35 #include <QSpinBox>
36 #include <QCheckBox>
37 #include <QLineEdit>
38 #include <QTableWidget>
39 #include <QComboBox>
40 
42  : QItemDelegate(parent)
43  , _parent(parent)
44 {
45 }
46 
47 QWidget *NotifyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /*none*/,
48  const QModelIndex &index) const
49 {
50  if (eRepeatValue == index.column()) {
51  QComboBox *editor = new QComboBox(parent);
52  editor->clear();
53  editor->addItems(NotificationItem::retryValues);
54  return editor;
55  } else {
56  if (eExpireTimer == index.column()) {
57  QSpinBox *editor = new QSpinBox(parent);
58  connect(editor, &QAbstractSpinBox::editingFinished, this,
59  &NotifyItemDelegate::commitAndCloseEditor);
60  return editor;
61  } else {
62  if (eTurnOn == index.column()) {
63  QCheckBox *editor = new QCheckBox(parent);
67  // connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
68  return editor;
69  }
70  }
71  }
72  QLineEdit *editor = new QLineEdit(parent);
73  return editor;
74 }
75 
76 void NotifyItemDelegate::commitAndCloseEditor()
77 {
78  QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
79  if (editor) {
80  emit commitData(editor);
81  emit closeEditor(editor);
82  } else {
83  QComboBox *editor = qobject_cast<QComboBox *>(sender());
84  if (editor) {
85  emit commitData(editor);
86  emit closeEditor(editor);
87  } else {
88  QSpinBox *editor = qobject_cast<QSpinBox *>(sender());
89  if (editor) {
90  emit commitData(editor);
91  emit closeEditor(editor);
92  } else {
93  QCheckBox *editor = qobject_cast<QCheckBox *>(sender());
94  if (editor) {
95  emit commitData(editor);
96  emit closeEditor(editor);
97  }
98  }
99  }
100  }
101 }
102 
103 void NotifyItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
104 {
105  QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
106  if (edit) {
107  edit->setText(index.model()->data(index, Qt::EditRole).toString());
108  } else {
109  QComboBox *repeatEditor = qobject_cast<QComboBox *>(editor);
110  if (repeatEditor)
111  repeatEditor->setCurrentIndex(
112  repeatEditor->findText(index.model()->data(index, Qt::EditRole).toString()));
113  else {
114  QSpinBox *expireEditor = qobject_cast<QSpinBox *>(editor);
115  if (expireEditor)
116  expireEditor->setValue(index.model()->data(index, Qt::EditRole).toInt());
117  else {
118  QCheckBox *enablePlayEditor = qobject_cast<QCheckBox *>(editor);
119  if (enablePlayEditor)
120  enablePlayEditor->setChecked(index.model()->data(index, Qt::EditRole).toBool());
121  }
122  }
123  }
124 }
125 
126 void NotifyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
127  const QModelIndex &index) const
128 {
129  QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
130  if (edit) {
131  model->setData(index, edit->text());
132  } else {
133  QComboBox *repeatEditor = qobject_cast<QComboBox *>(editor);
134  if (repeatEditor) {
135  model->setData(index, repeatEditor->currentText());
136  } else {
137  QSpinBox *expireEditor = qobject_cast<QSpinBox *>(editor);
138  if (expireEditor) {
139  model->setData(index, expireEditor->value(), Qt::EditRole);
140  } else {
141  QCheckBox *enablePlayEditor = qobject_cast<QCheckBox *>(editor);
142  if (enablePlayEditor) {
143  model->setData(index, enablePlayEditor->isChecked(), Qt::EditRole);
144  }
145  }
146  }
147  }
148 }
149 
150 void NotifyItemDelegate::selectRow(const QString &text)
151 {
152  Q_UNUSED(text);
153 
154  QComboBox *combo = qobject_cast<QComboBox *>(sender());
155  QTableWidget *table = new QTableWidget;
156  table = dynamic_cast<QTableWidget *>(combo->parent());
157 
158  qNotifyDebug() << table->columnCount();
159  qNotifyDebug() << table->rowCount();
160  qNotifyDebug() << table->currentRow();
161 }
162 
163 QSize NotifyItemDelegate::sizeHint(const QStyleOptionViewItem &option,
164  const QModelIndex &index) const
165 {
166  QSize s = QItemDelegate::sizeHint(option, index);
167  s.setHeight(10);
168  return s;
169 }
Uses to logging only inside notify plugin, can be convinient turned on/off.
NotifyItemDelegate(QObject *parent=nullptr)
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
QDebug qNotifyDebug()
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
static QStringList retryValues
void setEditorData(QWidget *editor, const QModelIndex &index) const
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const