Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
PropertyDelegate.cpp
1/*
2Copyright (C) 2019,2022,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#include "PropertyDelegate.h"
18#include "WorldEditor.h"
19#include <iostream>
20#include <limits>
21#include <algorithm>
22#include <QSpinBox>
23#include <QDoubleSpinBox>
24#include <QLineEdit>
25
26namespace AeonGames
27{
28 PropertyDelegate::PropertyDelegate ( QObject *parent ) : QStyledItemDelegate ( parent ) {}
29
34 template<class T> QWidget *BuildSpinboxEditor ( QWidget* parent )
35 {
36 QSpinBox *editor = new QSpinBox ( parent );
37 editor->setFrame ( false );
38 editor->setMinimum (
39 static_cast<int> (
40 std::max (
41 static_cast<long long> ( std::numeric_limits<T>::min() ),
42 static_cast<long long> ( std::numeric_limits<int>::min() ) ) ) );
43 editor->setMaximum (
44 static_cast<int> (
45 std::min (
46 static_cast<unsigned long long> ( std::numeric_limits<T>::max() ),
47 static_cast<unsigned long long> ( std::numeric_limits<int>::max() ) ) ) );
48 return editor;
49 }
50
55 template<class T> void SetSpinboxEditorValue ( QWidget *editor, const QVariant& value )
56 {
57 QSpinBox *spinBox = static_cast<QSpinBox*> ( editor );
58 spinBox->setValue ( static_cast<int> ( value.value<T>() ) );
59 }
60
66 template<class T> void SetSpinboxModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index )
67 {
68 QSpinBox *spinBox = static_cast<QSpinBox*> ( editor );
69 spinBox->interpretText();
70 model->setData ( index, QVariant::fromValue ( static_cast<T> ( spinBox->value() ) ), Qt::EditRole );
71 }
72
73 QWidget *PropertyDelegate::createEditor ( QWidget *parent,
74 const QStyleOptionViewItem &option,
75 const QModelIndex &index ) const
76 {
77 QVariant value = index.model()->data ( index, Qt::EditRole );
78 int user_type{value.userType() };
79 switch ( user_type )
80 {
81 case QMetaType::Int:
82 return BuildSpinboxEditor<int> ( parent );
83 case QMetaType::Long:
84 return BuildSpinboxEditor<long> ( parent );
85 case QMetaType::LongLong:
86 return BuildSpinboxEditor<long long> ( parent );
87 case QMetaType::UInt:
88 return BuildSpinboxEditor<unsigned int> ( parent );
89 case QMetaType::ULong:
90 return BuildSpinboxEditor<unsigned long> ( parent );
91 case QMetaType::ULongLong:
93 case QMetaType::Float:
94 {
95 auto float_spinbox = new QDoubleSpinBox ( parent );
96 float_spinbox->setMinimum (
97 std::numeric_limits<float>::min() );
98 float_spinbox->setMaximum (
99 std::numeric_limits<float>::max() );
100 return float_spinbox;
101 }
102 }
103 return QStyledItemDelegate::createEditor ( parent, option, index );
104 }
105
106 void PropertyDelegate::setEditorData ( QWidget *editor,
107 const QModelIndex &index ) const
108 {
109 QVariant value = index.model()->data ( index, Qt::EditRole );
110 int user_type{value.userType() };
111 switch ( user_type )
112 {
113 case QMetaType::Int:
114 SetSpinboxEditorValue<int> ( editor, value );
115 return;
116 case QMetaType::Long:
117 SetSpinboxEditorValue<long> ( editor, value );
118 return;
119 case QMetaType::LongLong:
120 SetSpinboxEditorValue<long long> ( editor, value );
121 return;
122 case QMetaType::UInt:
123 SetSpinboxEditorValue<unsigned int> ( editor, value );
124 return;
125 case QMetaType::ULong:
126 SetSpinboxEditorValue<unsigned long> ( editor, value );
127 return;
128 case QMetaType::ULongLong:
130 return;
131 case QMetaType::Float:
132 {
133 QDoubleSpinBox* spinBox = static_cast<QDoubleSpinBox*> ( editor );
134 spinBox->setValue ( value.value<float>() );
135 }
136 return;
137 default:
138 if ( user_type == qWorldEditorApp->GetPathMetaType() )
139 {
140 QLineEdit* line_edit{static_cast<QLineEdit*> ( editor ) };
141 line_edit->setText ( QString::fromStdString ( value.value<std::filesystem::path>().string() ) );
142 return;
143 }
144 }
145 QStyledItemDelegate::setEditorData ( editor, index );
146 }
147
148 void PropertyDelegate::setModelData ( QWidget *editor, QAbstractItemModel *model,
149 const QModelIndex &index ) const
150 {
151 QVariant value = index.model()->data ( index, Qt::EditRole );
152 int user_type{value.userType() };
153 switch ( user_type )
154 {
155 case QMetaType::Int:
156 SetSpinboxModelData<int> ( editor, model, index );
157 return;
158 case QMetaType::Long:
159 SetSpinboxModelData<long> ( editor, model, index );
160 return;
161 case QMetaType::LongLong:
162 SetSpinboxModelData<long long> ( editor, model, index );
163 return;
164 case QMetaType::UInt:
165 SetSpinboxModelData<unsigned int> ( editor, model, index );
166 return;
167 case QMetaType::ULong:
168 SetSpinboxModelData<unsigned long> ( editor, model, index );
169 return;
170 case QMetaType::ULongLong:
171 SetSpinboxModelData<unsigned long long> ( editor, model, index );
172 return;
173 case QMetaType::Float:
174 {
175 QDoubleSpinBox* spinBox = static_cast<QDoubleSpinBox*> ( editor );
176 spinBox->interpretText();
177 model->setData ( index, QVariant::fromValue ( static_cast<float> ( spinBox->value() ) ), Qt::EditRole );
178 }
179 return;
180 default:
181 if ( user_type == qWorldEditorApp->GetPathMetaType() )
182 {
183 QLineEdit* line_edit{static_cast<QLineEdit*> ( editor ) };
184 model->setData ( index, line_edit->text(), Qt::EditRole );
185 return;
186 }
187 }
188 QStyledItemDelegate::setModelData ( editor, model, index );
189 }
190
192 const QStyleOptionViewItem &option, const QModelIndex &index ) const
193 {
194 QVariant value = index.model()->data ( index, Qt::EditRole );
195 int user_type{value.userType() };
196 switch ( user_type )
197 {
198 case QMetaType::ULongLong:
199 {
200 editor->setGeometry ( option.rect );
201 }
202 return;
203 }
204 QStyledItemDelegate::updateEditorGeometry ( editor, option, index );
205 }
206}
PropertyDelegate(QObject *parent=nullptr)
Construct the property delegate.
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Create an editor widget for the given model index.
void setEditorData(QWidget *editor, const QModelIndex &index) const override
Set the editor widget data from the model.
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
Write the editor widget data back to the model.
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Update the editor geometry to match the view item area.
<- This is here just for the literals
Definition AABB.hpp:31
void SetSpinboxEditorValue(QWidget *editor, const QVariant &value)
Set the value of a QSpinBox editor from a QVariant.
QWidget * BuildSpinboxEditor(QWidget *parent)
Build a QSpinBox editor widget configured for type T.
void SetSpinboxModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)
Commit a QSpinBox editor value to the item model.