File indexing completed on 2024-04-28 15:09:13
0001 /* Common Table View Delegates 0002 0003 Collection of delegates assigned to each individual column 0004 in the table view. 0005 0006 SPDX-FileCopyrightText: 2022 Jasem Mutlaq <mutlaqja@ikarustech.com> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #include "tabledelegate.h" 0012 #include <QCheckBox> 0013 #include <QSpinBox> 0014 #include <QDoubleSpinBox> 0015 #include <QComboBox> 0016 #include <QApplication> 0017 0018 QWidget * ToggleDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const 0019 { 0020 QCheckBox *editor = new QCheckBox(parent); 0021 return editor; 0022 } 0023 0024 0025 void ToggleDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const 0026 { 0027 int value = index.model()->data(index, Qt::EditRole).toInt(); 0028 dynamic_cast<QCheckBox*>(editor)->setChecked(value == 1); 0029 } 0030 0031 void ToggleDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const 0032 { 0033 QStyleOptionButton checkboxstyle; 0034 QRect checkbox_rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxstyle); 0035 0036 //center 0037 checkboxstyle.rect = option.rect; 0038 checkboxstyle.rect.setLeft(option.rect.x() + option.rect.width() / 2 - checkbox_rect.width() / 2); 0039 0040 editor->setGeometry(checkboxstyle.rect); 0041 } 0042 0043 void ToggleDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 0044 { 0045 int value = dynamic_cast<QCheckBox*>(editor)->isChecked() ? 1 : 0; 0046 model->setData(index, value, Qt::EditRole); 0047 } 0048 0049 void ToggleDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0050 { 0051 QStyleOptionButton checkboxstyle; 0052 QRect checkbox_rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxstyle); 0053 0054 QRect rect(option.rect.x() + option.rect.width() / 2 - checkbox_rect.width() / 2, 0055 option.rect.y() + option.rect.height() / 2 - checkbox_rect.height() / 2, 0056 checkbox_rect.width(), checkbox_rect.height()); 0057 0058 drawCheck(painter, option, rect, index.data().toBool() ? Qt::Checked : Qt::Unchecked); 0059 } 0060 0061 bool ToggleDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &, 0062 const QModelIndex &index) 0063 { 0064 if (event->type() == QEvent::MouseButtonRelease) 0065 { 0066 bool checked = index.model()->data(index, Qt::DisplayRole).toBool(); 0067 // Toggle value 0068 int value = checked ? 0 : 1; 0069 return model->setData(index, value, Qt::EditRole); 0070 } 0071 0072 return false; 0073 } 0074 0075 ///////////////////////////////////////////////////////// 0076 // Double Delegate 0077 ///////////////////////////////////////////////////////// 0078 0079 QWidget * DoubleDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const 0080 { 0081 QDoubleSpinBox *editor = new QDoubleSpinBox(parent); 0082 editor->setFrame(false); 0083 editor->setDecimals(3); 0084 0085 if (step > 0) 0086 { 0087 editor->setMinimum(min); 0088 editor->setMaximum(max); 0089 editor->setSingleStep(step); 0090 } 0091 else 0092 { 0093 editor->setMinimum(0.001); 0094 editor->setMaximum(120); 0095 editor->setSingleStep(1); 0096 } 0097 return editor; 0098 } 0099 0100 void DoubleDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const 0101 { 0102 double value = index.model()->data(index, Qt::EditRole).toDouble(); 0103 dynamic_cast<QDoubleSpinBox*>(editor)->setValue(value); 0104 } 0105 0106 void DoubleDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 0107 { 0108 double value = dynamic_cast<QDoubleSpinBox*>(editor)->value(); 0109 model->setData(index, value, Qt::EditRole); 0110 } 0111 0112 void DoubleDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const 0113 { 0114 editor->setGeometry(option.rect); 0115 } 0116 0117 ///////////////////////////////////////////////////////// 0118 // Integer Delegate 0119 ///////////////////////////////////////////////////////// 0120 0121 QWidget * IntegerDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const 0122 { 0123 QSpinBox *editor = new QSpinBox(parent); 0124 editor->setFrame(false); 0125 if (step > 0) 0126 { 0127 editor->setMinimum(min); 0128 editor->setMaximum(max); 0129 editor->setSingleStep(step); 0130 } 0131 else 0132 { 0133 editor->setMinimum(-10000); 0134 editor->setMaximum(10000); 0135 editor->setSingleStep(100); 0136 } 0137 return editor; 0138 } 0139 0140 0141 void IntegerDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const 0142 { 0143 int value = index.model()->data(index, Qt::EditRole).toInt(); 0144 dynamic_cast<QSpinBox*>(editor)->setValue(value); 0145 } 0146 0147 void IntegerDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 0148 { 0149 int value = dynamic_cast<QSpinBox*>(editor)->value(); 0150 model->setData(index, value, Qt::EditRole); 0151 } 0152 0153 void IntegerDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const 0154 { 0155 editor->setGeometry(option.rect); 0156 } 0157 0158 ///////////////////////////////////////////////////////// 0159 // Combo Delegate 0160 ///////////////////////////////////////////////////////// 0161 0162 ComboDelegate::ComboDelegate(QObject *parent) : QStyledItemDelegate(parent) 0163 { 0164 m_Values.insert(0, "--"); 0165 } 0166 0167 QWidget * ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const 0168 { 0169 QComboBox *editor = new QComboBox(parent); 0170 editor->addItems(m_Values); 0171 return editor; 0172 } 0173 0174 0175 void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const 0176 { 0177 QString currentCombo = index.model()->data(index, Qt::EditRole).toString(); 0178 dynamic_cast<QComboBox*>(editor)->setCurrentText(currentCombo); 0179 } 0180 0181 void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 0182 { 0183 QString value = dynamic_cast<QComboBox*>(editor)->currentText(); 0184 model->setData(index, value, Qt::EditRole); 0185 } 0186 0187 void ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const 0188 { 0189 editor->setGeometry(option.rect); 0190 } 0191 0192 void ComboDelegate::setValues(const QStringList &list) 0193 { 0194 m_Values = list; 0195 m_Values.insert(0, "--"); 0196 }