RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
qpropertybrowser.cpp
Go to the documentation of this file.
1 //--------------------------------------------------------------- @License begins
2 // RioEngine: The late night Coke -without whores- debugging sessions
3 // 2012-2015 Leopoldo Lomas Flores. Torreon, Coahuila. MEXICO
4 // leopoldolomas [at] gmail
5 // www.rioengine.com
6 // www.leopoldolomas.info
7 // "You have a problem, you face it like a man."
8 //
9 // This is free and unencumbered software released into the public domain.
10 //
11 // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
12 // software, either in source code form or as a compiled binary, for any purpose,
13 // commercial or non-commercial, and by any means.
14 //
15 // In jurisdictions that recognize copyright laws, the author or authors of this
16 // software dedicate any and all copyright interest in the software to the public
17 // domain. We make this dedication for the benefit of the public at large and to
18 // the detriment of our heirs and successors. We intend this dedication to be
19 // an overt act of relinquishment in perpetuity of all present and future
20 // rights to this software under copyright law.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS
24 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //--------------------------------------------------------------- @License ends
29 
30 #include <QSplitter>
31 #include "cengine/cobject.h"
35 #include "ui_qpropertybrowser.h"
36 #include "qpropertybrowser.h"
37 
38 //-----------------------------------------------------------------------------
39 
41  QWidget(parent),
42  m_ui(new Ui::QPropertyBrowser) {
43  m_ui->setupUi(this);
44  m_listView = new QObjectInspector(this);
45  m_propertyEditor = new QPropertyEditorWidget(this);
46  m_model = new QObjectModel(this);
47  m_propertyEditor->setAlternatingRowColors(true);
48 
49  QSplitter* splitter = new QSplitter(this);
50  splitter->setOrientation(Qt::Vertical);
51  splitter->addWidget(m_listView);
52  splitter->addWidget(m_propertyEditor);
53  splitter->setStretchFactor(1, 1);
54 
55  m_listView->setModel(m_model);
56  connect(m_listView, SIGNAL(clicked(QModelIndex)),
57  this, SLOT(onSelectedObjectInInspector(QModelIndex)));
58 
59  m_ui->verticalLayout->addWidget(splitter);
60 }
61 
62 //-----------------------------------------------------------------------------
63 
65  return m_propertyEditor;
66 }
67 
68 //-----------------------------------------------------------------------------
69 
70 bool QPropertyBrowser::containsChild(QObject* child) {
71  RE_ASSERT(child);
72  int index = m_model->children()->indexOf(child);
73  return (index != -1);
74 }
75 
76 //-----------------------------------------------------------------------------
77 
78 void QPropertyBrowser::addChild(QObject* child, bool select_immediately) {
79  if (containsChild(child)) {
80  if (select_immediately) {
81  setSelectedChild(child);
82  }
83  return;
84  }
85 
86  m_model->addChild(child);
87 
88  if (select_immediately) {
89  setSelectedChild(child);
90  }
91 }
92 
93 //-----------------------------------------------------------------------------
94 
95 void QPropertyBrowser::setSelectedChild(QObject *child) {
96  if (!containsChild(child)) {
97  return;
98  }
99 
100  selectChildInInspector(child->objectName().toStdString());
101  m_propertyEditor->setObject(child);
102 }
103 
104 //-----------------------------------------------------------------------------
105 
106 void QPropertyBrowser::newObjectSelected(const QModelIndex &index) {
107  setSelectedChild(m_model->children()->at(index.row()));
108 }
109 
110 //-----------------------------------------------------------------------------
111 
113  m_propertyEditor->setObject(NULL);
114  m_listView->unselectAll();
115 }
116 
117 //-----------------------------------------------------------------------------
118 
119 void QPropertyBrowser::selectChildInInspector(std::string id) {
120  m_listView->unselectAll();
121  QObjectModel* model = (QObjectModel*)m_listView->model();
122  int index = -1;
123  for (int i = 0; i < model->children()->count(); i++) {
124  if (model->children()->at(i)->objectName().toStdString() == id) {
125  index = i;
126  break;
127  }
128  }
129  if (index != -1) {
130  QModelIndex Index = model->index(index, 0);
131  if ( Index.isValid() ) {
132  m_listView->selectionModel()->select( Index, QItemSelectionModel::Select );
133  }
134  }
135 }
136 
137 //-----------------------------------------------------------------------------
138 
140  m_listView->unselectAll();
141  QObjectModel* model = (QObjectModel*)m_listView->model();
142  for (int i = 0; i < model->children()->count(); i++) {
143  if (model->children()->at(i)->objectName().toStdString() == id) {
144  model->removeChild(QString::fromStdString(id));
145  return;
146  }
147  }
148 }
149 
150 //-----------------------------------------------------------------------------
151 
153  m_model->clear();
154 }
155 
156 //-----------------------------------------------------------------------------
157 
159  m_propertyEditor->setFocus(); // bug fix
160  emit m_model->dataChanged(QModelIndex(), QModelIndex());
161  m_propertyEditor->updateObject();
162 }
163 
164 //-----------------------------------------------------------------------------
165 
166 void QPropertyBrowser::onSelectedObjectInInspector(QModelIndex index) {
167  QVariant value = m_model->data(index, 0);
168  emit selectedObjectOnInspector(value.toString());
169 }
170 
171 //-----------------------------------------------------------------------------
172 
174  SAFE_RELEASE(m_model);
175  delete m_ui;
176 }
void removeChild(QString child_name)
Definition: about.h:6
QVariant data(const QModelIndex &index, int role) const
QList< QObject * > * children() const
void newObjectSelected(const QModelIndex &index)
The QPropertyEditorWidget offers an easy to use mechanism to visualize properties of a class inherite...
void addChild(QObject *child)
void updateObject(QObject *propertyObject)
void selectedObjectOnInspector(QString)
#define SAFE_RELEASE(x)
Definition: cobject.h:36
#define RE_ASSERT
Definition: macro.h:57
void addChild(QObject *child, bool select_immediately=true)
void setSelectedChild(QObject *child)
void setObject(QObject *propertyObject)
virtual void unselectAll()
QPropertyBrowser(QWidget *parent=0)
QPropertyEditorWidget * getQPropertyEditorWidget() const
bool containsChild(QObject *child)
void removeChildInInspector(std::string child)