File indexing completed on 2024-04-28 15:09:10

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