Kstars

tabledelegate.h
1/* Optical Train 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#pragma once
12
13#include <QStyledItemDelegate>
14#include <QItemDelegate>
15
16class QCheckBox;
17class QSpinBox;
18class QDoubleSpinBox;
19class QComboBox;
20class QDateTime;
21
22class NotEditableDelegate : public QStyledItemDelegate
23{
25 public:
26 explicit NotEditableDelegate(QObject *parent = nullptr)
28 {}
29
30 protected:
31 bool editorEvent(QEvent *, QAbstractItemModel *, const QStyleOptionViewItem &, const QModelIndex &) override
32 {
33 return false;
34 }
35 QWidget* createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override
36 {
37 return nullptr;
38 }
39};
40
41// Not editable delegate to display numbers to 2 decimal places
42class NotEditableDelegate2dp : public QStyledItemDelegate
43{
45 public:
46 explicit NotEditableDelegate2dp(QObject *parent = nullptr)
48 {}
49
50 QString displayText(const QVariant &value, const QLocale &locale) const override
51 {
52 Q_UNUSED(locale)
53 QString str = QString::number(value.toDouble(), 'f', 2);
54 return str;
55 }
56
57 protected:
58 bool editorEvent(QEvent *, QAbstractItemModel *, const QStyleOptionViewItem &, const QModelIndex &) override
59 {
60 return false;
61 }
62 QWidget* createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override
63 {
64 return nullptr;
65 }
66};
67
68class ToggleDelegate : public QItemDelegate
69{
71 public:
72 explicit ToggleDelegate(QObject *parent = nullptr) : QItemDelegate(parent) {}
73
74 QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
75 void setEditorData(QWidget *editor, const QModelIndex &index) const override;
76 void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
77 void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
78 void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
79 bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &, const QModelIndex &index) override;
80};
81
82class DoubleDelegate : public QStyledItemDelegate
83{
85 public:
86 explicit DoubleDelegate(QObject *parent = nullptr, double min = -1, double max = -1,
87 double step = -1) : QStyledItemDelegate(parent)
88 {
89 this->min = min;
90 this->max = max;
91 this->step = step;
92 }
93
94 QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
95 void setEditorData(QWidget *editor, const QModelIndex &index) const override;
96 void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
97 void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
98 protected:
99 double min {-1}, max {-1}, step {-1};
100};
101
102class IntegerDelegate : public QStyledItemDelegate
103{
105 public:
106 explicit IntegerDelegate(QObject *parent = nullptr, int min = -1, int max = -1, int step = -1) : QStyledItemDelegate(parent)
107 {
108 this->min = min;
109 this->max = max;
110 this->step = step;
111 }
112
113 QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
114 void setEditorData(QWidget *editor, const QModelIndex &index) const override;
115 void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
116 void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
117 void setMinMaxStep(int min, int max, int step);
118 protected:
119 int min {-1}, max {-1}, step {-1};
120};
121
122class ComboDelegate : public QStyledItemDelegate
123{
125 public:
126 explicit ComboDelegate(QObject *parent = nullptr);
127
128 QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
129 void setEditorData(QWidget *editor, const QModelIndex &index) const override;
130 void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
131 void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
132 void setValues(const QStringList &values);
133 const QStringList &values() const
134 {
135 return m_Values;
136 }
137
138 private:
139 mutable QStringList m_Values;
140};
141
142class DatetimeDelegate : public QStyledItemDelegate
143{
145 public:
146 explicit DatetimeDelegate(QObject *parent = nullptr, QString format = "yyyy-MM-dd hh:mm:ss", QString minDT = "2025-01-01", QString maxDT = "2100-01-01", bool calPopup = true) : QStyledItemDelegate(parent)
147 {
148 this->format = format;
149 this->minDT = minDT;
150 this->maxDT = maxDT;
151 this->calPopup = calPopup;
152 }
153
154 QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
155 void setEditorData(QWidget *editor, const QModelIndex &index) const override;
156 void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
157 void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
158 protected:
159 QString format, maxDT, minDT;
160 bool calPopup { true };
161};
QItemDelegate(QObject *parent)
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
virtual bool event(QEvent *e)
QObject * parent() const const
QString number(double n, char format, int precision)
QStyledItemDelegate(QObject *parent)
double toDouble(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 11:55:59 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.