Kstars

tabledelegate.cpp
1/* Common Table View Delegates
2
3 Collection of delegates assigned to each individual column
4 in the table view.
5
6 SPDX-FileCopyrightText: 2022 Jasem Mutlaq <mutlaqja@ikarustech.com>
7
8 SPDX-License-Identifier: GPL-2.0-or-later
9*/
10
11#include "tabledelegate.h"
12#include <QCheckBox>
13#include <QSpinBox>
14#include <QDoubleSpinBox>
15#include <QComboBox>
16#include <QApplication>
17
18QWidget * ToggleDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
19{
20 QCheckBox *editor = new QCheckBox(parent);
21 return editor;
22}
23
24
25void ToggleDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
26{
27 int value = index.model()->data(index, Qt::EditRole).toInt();
28 dynamic_cast<QCheckBox*>(editor)->setChecked(value == 1);
29}
30
31void ToggleDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
32{
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
43void ToggleDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
44{
45 int value = dynamic_cast<QCheckBox*>(editor)->isChecked() ? 1 : 0;
46 model->setData(index, value, Qt::EditRole);
47}
48
49void ToggleDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
50{
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}
60
61bool ToggleDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &,
62 const QModelIndex &index)
63{
64 if (event->type() == QEvent::MouseButtonRelease)
65 {
66 bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
67 // Toggle value
68 int value = checked ? 0 : 1;
69 return model->setData(index, value, Qt::EditRole);
70 }
71
72 return false;
73}
74
75/////////////////////////////////////////////////////////
76// Double Delegate
77/////////////////////////////////////////////////////////
78
79QWidget * DoubleDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
80{
82 editor->setFrame(false);
83 editor->setDecimals(3);
84
85 if (step > 0)
86 {
87 editor->setMinimum(min);
88 editor->setMaximum(max);
89 editor->setSingleStep(step);
90 }
91 else
92 {
93 editor->setMinimum(0.001);
94 editor->setMaximum(120);
95 editor->setSingleStep(1);
96 }
97 return editor;
98}
99
100void DoubleDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
101{
102 double value = index.model()->data(index, Qt::EditRole).toDouble();
103 dynamic_cast<QDoubleSpinBox*>(editor)->setValue(value);
104}
105
106void DoubleDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
107{
108 double value = dynamic_cast<QDoubleSpinBox*>(editor)->value();
109 model->setData(index, value, Qt::EditRole);
110}
111
112void DoubleDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
113{
114 editor->setGeometry(option.rect);
115}
116
117/////////////////////////////////////////////////////////
118// Integer Delegate
119/////////////////////////////////////////////////////////
120
121QWidget * IntegerDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
122{
123 QSpinBox *editor = new QSpinBox(parent);
124 editor->setFrame(false);
125 if (step > 0)
126 {
127 editor->setMinimum(min);
128 editor->setMaximum(max);
129 editor->setSingleStep(step);
130 }
131 else
132 {
133 editor->setMinimum(-10000);
134 editor->setMaximum(10000);
135 editor->setSingleStep(100);
136 }
137 return editor;
138}
139
140
141void IntegerDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
142{
143 int value = index.model()->data(index, Qt::EditRole).toInt();
144 dynamic_cast<QSpinBox*>(editor)->setValue(value);
145}
146
147void IntegerDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
148{
149 int value = dynamic_cast<QSpinBox*>(editor)->value();
150 model->setData(index, value, Qt::EditRole);
151}
152
153void IntegerDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
154{
155 editor->setGeometry(option.rect);
156}
157
158/////////////////////////////////////////////////////////
159// Combo Delegate
160/////////////////////////////////////////////////////////
161
162ComboDelegate::ComboDelegate(QObject *parent) : QStyledItemDelegate(parent)
163{
164 m_Values.insert(0, "--");
165}
166
167QWidget * ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
168{
169 QComboBox *editor = new QComboBox(parent);
170 editor->addItems(m_Values);
171 return editor;
172}
173
174
175void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
176{
177 QString currentCombo = index.model()->data(index, Qt::EditRole).toString();
178 dynamic_cast<QComboBox*>(editor)->setCurrentText(currentCombo);
179}
180
181void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
182{
183 QString value = dynamic_cast<QComboBox*>(editor)->currentText();
184 model->setData(index, value, Qt::EditRole);
185}
186
187void ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
188{
189 editor->setGeometry(option.rect);
190}
191
192void ComboDelegate::setValues(const QStringList &list)
193{
194 m_Values = list;
195 m_Values.insert(0, "--");
196}
KIOCORE_EXPORT QStringList list(const QString &fileClass)
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
void setFrame(bool)
QStyle * style()
void addItems(const QStringList &texts)
void setDecimals(int prec)
void setMaximum(double max)
void setMinimum(double min)
void setSingleStep(double val)
MouseButtonRelease
Int toInt() const const
virtual void drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const const
iterator insert(const_iterator before, parameter_type value)
QVariant data(int role) const const
const QAbstractItemModel * model() const const
virtual bool event(QEvent *e)
QObject * parent() const const
void setMaximum(int max)
void setMinimum(int min)
void setSingleStep(int val)
SE_CheckBoxIndicator
virtual QRect subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const const=0
EditRole
bool toBool() const const
void setGeometry(const QRect &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:48:25 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.