Kstars

filterdelegate.cpp
1 /* Ekos Filter Delegates
2 
3  Collection of delegates assigned to each individual column
4  in the table view.
5 
6  SPDX-FileCopyrightText: 2017 Jasem Mutlaq <[email protected]>
7 
8  SPDX-License-Identifier: GPL-2.0-or-later
9 */
10 
11 #include "filterdelegate.h"
12 #include <QCheckBox>
13 #include <QSpinBox>
14 #include <QDoubleSpinBox>
15 #include <QComboBox>
16 #include <QApplication>
17 
18 QWidget * UseAutoFocusDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
19 {
20  cb = new QCheckBox(parent);
21  return cb;
22 }
23 
24 
25 void UseAutoFocusDelegate::setEditorData(QWidget *, const QModelIndex &index) const
26 {
27  int value = index.model()->data(index, Qt::EditRole).toInt();
28  cb->setChecked(value == 1);
29 }
30 
31 void UseAutoFocusDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
32 {
33  QStyleOptionButton checkboxstyle;
34  QRect checkbox_rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxstyle);
35 
36  //center
37  checkboxstyle.rect = option.rect;
38  checkboxstyle.rect.setLeft(option.rect.x() + option.rect.width()/2 - checkbox_rect.width()/2);
39 
40  editor->setGeometry(checkboxstyle.rect);
41 }
42 
43 void UseAutoFocusDelegate::setModelData(QWidget *, QAbstractItemModel *model, const QModelIndex &index) const
44 {
45  int value = cb->isChecked() ? 1 : 0;
46  model->setData(index, value, Qt::EditRole);
47 }
48 
49 void UseAutoFocusDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
50 {
51  QStyleOptionButton checkboxstyle;
52  QRect checkbox_rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxstyle);
53 
54  QRect rect(option.rect.x() + option.rect.width()/2 - checkbox_rect.width()/2,
55  option.rect.y() + option.rect.height()/2 - checkbox_rect.height()/2,
56  checkbox_rect.width(), checkbox_rect.height());
57 
58  drawCheck(painter, option, rect, index.data().toBool() ? Qt::Checked : Qt::Unchecked);
59  //drawFocus(painter, option, checkboxstyle.rect);
60 }
61 
62 bool UseAutoFocusDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &,
63  const QModelIndex &index)
64 {
65  if (event->type() == QEvent::MouseButtonRelease)
66  {
67  bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
68  // Toggle value
69  int value = checked ? 0 : 1;
70  return model->setData(index, value, Qt::EditRole);
71  }
72 
73  return false;
74 }
75 
76 /////////////////////////////////////////////////////////
77 // Exposure Delegate
78 /////////////////////////////////////////////////////////
79 
80 QWidget * ExposureDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
81 {
82  spinbox = new QDoubleSpinBox(parent);
83  spinbox->setFrame(false);
84  spinbox->setDecimals(3);
85  spinbox->setMinimum(0.001);
86  spinbox->setMaximum(120);
87  spinbox->setSingleStep(1);
88  return spinbox;
89 }
90 
91 
92 void ExposureDelegate::setEditorData(QWidget *, const QModelIndex &index) const
93 {
94  double value = index.model()->data(index, Qt::EditRole).toDouble();
95  spinbox->setValue(value);
96 }
97 
98 void ExposureDelegate::setModelData(QWidget *, QAbstractItemModel *model, const QModelIndex &index) const
99 {
100  double value = spinbox->value();
101  model->setData(index, value, Qt::EditRole);
102 }
103 
104 void ExposureDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
105 {
106  editor->setGeometry(option.rect);
107 }
108 
109 /////////////////////////////////////////////////////////
110 // Offset Delegate
111 /////////////////////////////////////////////////////////
112 
113 QWidget * OffsetDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
114 {
115  spinbox = new QSpinBox(parent);
116  spinbox->setFrame(false);
117  spinbox->setMinimum(-10000);
118  spinbox->setMaximum(10000);
119  return spinbox;
120 }
121 
122 
123 void OffsetDelegate::setEditorData(QWidget *, const QModelIndex &index) const
124 {
125  int value = index.model()->data(index, Qt::EditRole).toInt();
126  spinbox->setValue(value);
127 }
128 
129 void OffsetDelegate::setModelData(QWidget *, QAbstractItemModel *model, const QModelIndex &index) const
130 {
131  int value = spinbox->value();
132  model->setData(index, value, Qt::EditRole);
133 }
134 
135 void OffsetDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
136 {
137  editor->setGeometry(option.rect);
138 }
139 
140 /////////////////////////////////////////////////////////
141 // Lock Delegate
142 /////////////////////////////////////////////////////////
143 
144 LockDelegate::LockDelegate(QStringList filterList, QObject *parent) : QStyledItemDelegate(parent)
145 {
146  m_FilterList = filterList;
147  m_FilterList.insert(0, "--");
148 }
149 
150 QWidget * LockDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
151 {
152  lockbox = new QComboBox(parent);
153  lockbox->addItems(m_FilterList);
154  return lockbox;
155 }
156 
157 
158 void LockDelegate::setEditorData(QWidget *, const QModelIndex &index) const
159 {
160  QString currentLockFilter = index.model()->data(index, Qt::EditRole).toString();
161  lockbox->setCurrentText(currentLockFilter);
162 }
163 
164 void LockDelegate::setModelData(QWidget *, QAbstractItemModel *model, const QModelIndex &index) const
165 {
166  QString value = lockbox->currentText();
167  model->setData(index, value, Qt::EditRole);
168 }
169 
170 void LockDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
171 {
172  editor->setGeometry(option.rect);
173 }
174 
175 void LockDelegate::setCurrentFilterList(const QStringList &list)
176 {
177  m_FilterList = list;
178  m_FilterList.insert(0, "--");
179 }
EditRole
MouseButtonRelease
SE_CheckBoxIndicator
virtual QVariant data(const QModelIndex &index, int role) const const=0
int width() const const
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
QVariant data(int role) const const
double toDouble(bool *ok) const const
int toInt(bool *ok) const const
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
void insert(int i, const T &value)
bool toBool() const const
void setGeometry(int x, int y, int w, int h)
int height() const const
virtual QRect subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget) const const=0
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
const QAbstractItemModel * model() const const
QStyle * style()
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Aug 15 2022 04:04:01 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.