Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / plugins / notify / notifyitemdelegate.cpp
blobb2440762b52f5576fd2ada0d40e1017b54e2a158
1 /**
2 ******************************************************************************
4 * @file notifyitemdelegate.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @brief
7 * @see The GNU Public License (GPL) Version 3
8 * @defgroup notifyplugin
9 * @{
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <QtGui>
29 #include <QtWidgets/QWidget>
30 #include <QSpinBox>
31 #include <QCheckBox>
32 #include <QLineEdit>
33 #include <QTableWidget>
34 #include "notifyitemdelegate.h"
35 #include "notifytablemodel.h"
36 #include "notifylogging.h"
37 #include "notificationitem.h"
39 NotifyItemDelegate::NotifyItemDelegate(QObject *parent)
40 : QItemDelegate(parent)
41 , _parent(parent)
44 QWidget *NotifyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /*none*/,
45 const QModelIndex & index) const
47 if (eRepeatValue == index.column()) {
48 QComboBox *editor = new QComboBox(parent);
49 editor->clear();
50 editor->addItems(NotificationItem::retryValues);
51 return editor;
52 } else {
53 if (eExpireTimer == index.column()) {
54 QSpinBox *editor = new QSpinBox(parent);
55 connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
56 return editor;
57 } else {
58 if (eTurnOn == index.column()) {
59 QCheckBox *editor = new QCheckBox(parent);
60 connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
61 return editor;
65 QLineEdit *editor = new QLineEdit(parent);
66 return editor;
69 void NotifyItemDelegate::commitAndCloseEditor()
71 QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
73 if (editor) {
74 emit commitData(editor);
75 emit closeEditor(editor);
76 } else {
77 QComboBox *editor = qobject_cast<QComboBox *>(sender());
78 if (editor) {
79 emit commitData(editor);
80 emit closeEditor(editor);
81 } else {
82 QSpinBox *editor = qobject_cast<QSpinBox *>(sender());
83 if (editor) {
84 emit commitData(editor);
85 emit closeEditor(editor);
86 } else {
87 QCheckBox *editor = qobject_cast<QCheckBox *>(sender());
88 if (editor) {
89 emit commitData(editor);
90 emit closeEditor(editor);
97 void NotifyItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
99 QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
101 if (edit) {
102 edit->setText(index.model()->data(index, Qt::EditRole).toString());
103 } else {
104 QComboBox *repeatEditor = qobject_cast<QComboBox *>(editor);
105 if (repeatEditor) {
106 repeatEditor->setCurrentIndex(repeatEditor->findText(index.model()->data(index, Qt::EditRole).toString()));
107 } else {
108 QSpinBox *expireEditor = qobject_cast<QSpinBox *>(editor);
109 if (expireEditor) {
110 expireEditor->setValue(index.model()->data(index, Qt::EditRole).toInt());
111 } else {
112 QCheckBox *enablePlayEditor = qobject_cast<QCheckBox *>(editor);
113 if (enablePlayEditor) {
114 enablePlayEditor->setChecked(index.model()->data(index, Qt::EditRole).toBool());
121 void NotifyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
123 QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
125 if (edit) {
126 model->setData(index, edit->text());
127 } else {
128 QComboBox *repeatEditor = qobject_cast<QComboBox *>(editor);
129 if (repeatEditor) {
130 model->setData(index, repeatEditor->currentText());
131 } else {
132 QSpinBox *expireEditor = qobject_cast<QSpinBox *>(editor);
133 if (expireEditor) {
134 model->setData(index, expireEditor->value(), Qt::EditRole);
135 } else {
136 QCheckBox *enablePlayEditor = qobject_cast<QCheckBox *>(editor);
137 if (enablePlayEditor) {
138 model->setData(index, enablePlayEditor->isChecked(), Qt::EditRole);
145 void NotifyItemDelegate::selectRow(const QString & text)
147 Q_UNUSED(text);
148 QComboBox *combo = qobject_cast<QComboBox *>(sender());
149 QTableWidget *table = new QTableWidget;
151 table = (QTableWidget *)(combo->parent());
153 qNotifyDebug() << table->columnCount();
154 qNotifyDebug() << table->rowCount();
155 qNotifyDebug() << table->currentRow();
158 QSize NotifyItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
160 QSize s = QItemDelegate::sizeHint(option, index);
162 s.setHeight(10);
163 return s;