Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
ComponentModel.cpp
1/*
2Copyright (C) 2018,2019,2021,2022,2025 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 <QIcon>
18#include <QFile>
19#include <QMimeData>
20#include <QDataStream>
21#include <QByteArray>
22#include <QXmlStreamWriter>
23#include <QTextStream>
24#include <QAction>
25#include <QVariant>
26
27#include <typeinfo>
28#include <cassert>
29#include <iostream>
30
31#include "aeongames/ResourceId.hpp"
32#include "aeongames/Component.hpp"
33#include "aeongames/ToString.hpp"
34#include "aeongames/StringId.hpp"
36#include "ComponentModel.h"
37#include "WorldEditor.h"
38
39namespace AeonGames
40{
43
45
46
47 QModelIndex ComponentModel::index ( int row, int column, const QModelIndex & parent ) const
48 {
49 if ( mComponent && row < static_cast<int> ( mComponent->GetPropertyCount() ) )
50 {
51 return createIndex ( row, column );
52 }
53 return QModelIndex();
54 }
55
56 int ComponentModel::rowCount ( const QModelIndex & index ) const
57 {
58 if ( mComponent && !index.isValid() )
59 {
60 return static_cast<int> ( mComponent->GetPropertyCount() );
61 }
62 // Only root may have children/rows
63 return 0;
64 }
65
66 template<typename... Types>
67 static inline QVariant fromStdVariant ( const std::variant<Types...> &value )
68 {
69#if QT_VERSION >= QT_VERSION_CHECK ( 5, 11, 0 )
70 return QVariant::fromStdVariant ( value );
71#else
72 if ( value.valueless_by_exception() )
73 {
74 return QVariant();
75 }
76 return std::visit ( [] ( const auto & arg )
77 {
78 return QVariant::fromValue ( arg );
79 }, value );
80#endif
81 }
82
83 QVariant ComponentModel::data ( const QModelIndex & index, int role ) const
84 {
85 if ( mComponent && index.isValid() )
86 {
87 if ( role == Qt::EditRole || role == Qt::DisplayRole )
88 switch ( index.column() )
89 {
90 case 0:
91 if ( role == Qt::DisplayRole )
92 {
93 return QString ( mComponent->GetPropertyInfoArray() [index.row()].GetString() );
94 }
95 break;
96 case 1:
97 {
98 Property property{ mComponent->GetProperty ( mComponent->GetPropertyInfoArray() [index.row()] ) };
99 if ( std::holds_alternative<std::string> ( property ) )
100 {
101 return QString::fromStdString ( std::get<std::string> ( property ) );
102 }
103 else if ( std::holds_alternative<std::filesystem::path> ( property ) )
104 {
105 return QString::fromStdString ( std::get<std::filesystem::path> ( property ).string() );
106 }
107 else
108 {
109 return fromStdVariant ( property );
110 }
111 }
112 }
113 }
114 return QVariant();
115 }
116
117 bool ComponentModel::setData ( const QModelIndex & index, const QVariant & value, int role )
118 {
119 if ( ( role == Qt::EditRole ) && ( index.isValid() ) && ( value.isValid() ) && ( index.column() == 1 ) )
120 {
121 Property property;
122 int user_type{value.userType() };
123 switch ( user_type )
124 {
125 case QMetaType::Int: // 2 int
126 property = value.value<int>();
127 break;
128 case QMetaType::UInt: //3 unsigned int
129 property = value.value<unsigned int>();
130 break;
131 case QMetaType::Double: //6 double
132 property = value.value<double>();
133 break;
134 case QMetaType::Long: //32 long
135 property = value.value<long>();
136 break;
137 case QMetaType::LongLong: //4 LongLong
138 property = value.value<long long>();
139 break;
140 case QMetaType::ULong: //35 unsigned long
141 property = value.value<unsigned long>();
142 break;
143 case QMetaType::ULongLong: //5 ULongLong
144 property = value.value<unsigned long long>();
145 break;
146 case QMetaType::Float: //38 float
147 property = value.value<float>();
148 break;
149 case QMetaType::QString: //10 QString
150 property = value.value<QString>().toStdString();
151 break;
152 default:
153 std::cout << LogLevel::Warning << " No Change" << std::endl;
154 return false;
155 }
156 try
157 {
158 mComponent->SetProperty ( mComponent->GetPropertyInfoArray() [index.row()], property );
159 }
160 catch ( const std::runtime_error& e )
161 {
162 std::cout << LogLevel::Warning << e.what() << " No Change" << std::endl;
163 return false;
164 }
165 emit dataChanged ( index, index );
166 return true;
167 }
168 return false;
169 }
170
172 {
173 beginResetModel();
174 mComponent = aComponent;
175 endResetModel();
176 }
177}
Defines log severity levels and stream output for the AeonGames engine.
Abstract base class for node components.
Definition Component.hpp:39
virtual ~ComponentModel()
Destructor.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Return the index of the item at the given row and column under parent.
int rowCount(const QModelIndex &index=QModelIndex()) const override
Return the number of rows under the given parent.
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Set the data for the given index and role.
void SetComponent(Component *aComponent)
Set the component whose properties are represented by this model.
ComponentModel(QObject *parent=nullptr)
Construct the component model.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Return the data for the given index and role.
QModelIndex parent(const QModelIndex &index) const override
Return the parent of the given model index.
PropertyModel(QObject *parent=nullptr)
Construct the property model.
<- This is here just for the literals
Definition AABB.hpp:31
std::variant< int, long, long long, unsigned, unsigned long, unsigned long long, float, double, std::string, std::filesystem::path > Property
A variant type that can hold any commonly used property value.
Definition Property.hpp:31
@ Warning
Potential issues that may need attention.
Definition LogLevel.hpp:32