|
| 1 | +#ifndef PRESETRULETABLEDELEGATE_H |
| 2 | +#define PRESETRULETABLEDELEGATE_H |
| 3 | + |
| 4 | +#include <QComboBox> |
| 5 | +#include <QMessageBox> |
| 6 | +#include <QStyledItemDelegate> |
| 7 | + |
| 8 | +#include "model/DeviceListModel.h" |
| 9 | +#include "model/PresetListModel.h" |
| 10 | +#include "model/PresetRuleTableModel.h" |
| 11 | +#include "utils/Log.h" |
| 12 | + |
| 13 | +class PresetRuleTableDelegate : public QStyledItemDelegate { |
| 14 | + Q_OBJECT |
| 15 | +public: |
| 16 | + PresetRuleTableDelegate(QObject* parent = nullptr) |
| 17 | + : QStyledItemDelegate(parent) {} |
| 18 | + |
| 19 | + void attachModels(DeviceListModel* devModel, |
| 20 | + PresetListModel* presetModel, |
| 21 | + PresetRuleTableModel* ruleModel) |
| 22 | + { |
| 23 | + _deviceModel = devModel; |
| 24 | + _presetModel = presetModel; |
| 25 | + _ruleModel = ruleModel; |
| 26 | + }; |
| 27 | + |
| 28 | + QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, |
| 29 | + const QModelIndex &index) const Q_DECL_OVERRIDE |
| 30 | + { |
| 31 | + Q_UNUSED(option) |
| 32 | + |
| 33 | + QComboBox *cb = new QComboBox(parent); |
| 34 | + switch(index.column()) |
| 35 | + { |
| 36 | + case 1: |
| 37 | + cb->setModel(_presetModel); |
| 38 | + break; |
| 39 | + } |
| 40 | + return cb; |
| 41 | + } |
| 42 | + |
| 43 | + void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE |
| 44 | + { |
| 45 | + if(!_ruleModel) |
| 46 | + { |
| 47 | + Log::error("PresetRuleTableDelegate::setEditorData: Rule model not attached"); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + auto rule = _ruleModel->at(index); |
| 52 | + const QString currentText = index.data(Qt::EditRole).toString(); |
| 53 | + |
| 54 | + QComboBox *cb = qobject_cast<QComboBox *>(editor); |
| 55 | + if(cb) |
| 56 | + { |
| 57 | + const int cbIndex = cb->findText(currentText); |
| 58 | + if (cbIndex >= 0) |
| 59 | + cb->setCurrentIndex(cbIndex); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE |
| 64 | + { |
| 65 | + QComboBox *cb = qobject_cast<QComboBox *>(editor); |
| 66 | + if(cb) |
| 67 | + { |
| 68 | + model->setData(index, cb->currentText(), Qt::EditRole); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | +private: |
| 73 | + DeviceListModel* _deviceModel = nullptr; |
| 74 | + PresetRuleTableModel* _ruleModel = nullptr; |
| 75 | + PresetListModel* _presetModel = nullptr; |
| 76 | + |
| 77 | +}; |
| 78 | + |
| 79 | +#endif // PRESETRULETABLEDELEGATE_H |
0 commit comments