]> git.cworth.org Git - vogl/blob - src/vogleditor/vogleditor_qstatetreemodel.cpp
Initial vogl checkin
[vogl] / src / vogleditor / vogleditor_qstatetreemodel.cpp
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 #include "vogleditor_qstatetreemodel.h"
27 #include "vogleditor_statetreeitem.h"
28 #include "vogleditor_statetreecontextitem.h"
29
30 #include "vogl_sync_object.h"
31
32 //===============================================
33
34 vogleditor_QStateTreeModel::vogleditor_QStateTreeModel( QObject* parent)
35    : QAbstractItemModel(parent),
36      m_pSnapshot(NULL),
37      m_pBaseSnapshot(NULL),
38      m_pCurrentContextItem(NULL)
39 {
40    m_ColumnTitles << "State";
41    m_ColumnTitles << "Value";
42
43    m_rootItem = new vogleditor_stateTreeItem(m_ColumnTitles, this);
44 }
45
46 void vogleditor_QStateTreeModel::set_snapshot(vogleditor_gl_state_snapshot *pSnapshot)
47 {
48     m_pSnapshot = pSnapshot;
49     setupModelData(m_pSnapshot, m_rootItem);
50 }
51
52 vogleditor_QStateTreeModel::vogleditor_QStateTreeModel(vogleditor_gl_state_snapshot* pSnapshot, QObject* parent)
53    : QAbstractItemModel(parent),
54      m_pSnapshot(pSnapshot),
55      m_pBaseSnapshot(NULL)
56 {
57    m_ColumnTitles << "State";
58    m_ColumnTitles << "Value";
59
60    m_rootItem = new vogleditor_stateTreeItem(m_ColumnTitles, this);
61    setupModelData(m_pSnapshot, m_rootItem);
62 }
63
64 vogleditor_QStateTreeModel::~vogleditor_QStateTreeModel()
65 {
66    if (m_rootItem != NULL)
67    {
68       delete m_rootItem;
69       m_rootItem = NULL;
70    }
71 }
72
73 QModelIndex vogleditor_QStateTreeModel::index(int row, int column, const QModelIndex& parent) const
74 {
75    if (!hasIndex(row, column, parent))
76        return QModelIndex();
77
78    vogleditor_stateTreeItem *parentItem;
79
80    if (!parent.isValid())
81        parentItem = m_rootItem;
82    else
83        parentItem = static_cast<vogleditor_stateTreeItem*>(parent.internalPointer());
84
85    vogleditor_stateTreeItem *childItem = parentItem->child(row);
86    if (childItem)
87        return createIndex(row, column, childItem);
88    else
89        return QModelIndex();
90 }
91
92 QModelIndex vogleditor_QStateTreeModel::parent(const QModelIndex& child) const
93 {
94    if (!child.isValid())
95        return QModelIndex();
96
97    vogleditor_stateTreeItem* childItem = static_cast<vogleditor_stateTreeItem*>(child.internalPointer());
98    vogleditor_stateTreeItem* parentItem = childItem->parent();
99
100    if (parentItem == m_rootItem)
101        return QModelIndex();
102
103    return createIndex(parentItem->row(), 0, parentItem);
104 }
105
106 int vogleditor_QStateTreeModel::rowCount(const QModelIndex& parent) const
107 {
108    vogleditor_stateTreeItem* parentItem;
109    if (parent.column() > 0)
110        return 0;
111
112    if (!parent.isValid())
113        parentItem = m_rootItem;
114    else
115        parentItem = static_cast<vogleditor_stateTreeItem*>(parent.internalPointer());
116
117    return parentItem->childCount();
118 }
119
120 int vogleditor_QStateTreeModel::columnCount(const QModelIndex& parent) const
121 {
122    VOGL_NOTE_UNUSED(parent);
123    return m_ColumnTitles.size();
124 }
125
126 QVariant vogleditor_QStateTreeModel::data(const QModelIndex& index, int role) const
127 {
128    if (!index.isValid())
129        return QVariant();
130
131    vogleditor_stateTreeItem *item = static_cast<vogleditor_stateTreeItem*>(index.internalPointer());
132
133    return item->columnData(index.column(), role);
134 }
135
136 QVariant vogleditor_QStateTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
137 {
138    if (orientation == Qt::Horizontal)
139        return m_rootItem->columnData(section, role);
140
141    return QVariant();
142 }
143
144 vogleditor_stateTreeItem* vogleditor_QStateTreeModel::root() const
145 {
146    return m_rootItem;
147 }
148
149 vogleditor_gl_state_snapshot* vogleditor_QStateTreeModel::get_snapshot() const
150 {
151    return m_pSnapshot;
152 }
153
154 void vogleditor_QStateTreeModel::set_diff_base_snapshot(const vogleditor_gl_state_snapshot *pDiffBaseSnapshot)
155 {
156     m_pBaseSnapshot = pDiffBaseSnapshot;
157 }
158
159 void vogleditor_QStateTreeModel::setupModelData(vogleditor_gl_state_snapshot* pSnapshot, vogleditor_stateTreeItem* parent)
160 {
161     QString tmp;
162
163     vogl_context_snapshot_ptr_vec contexts = pSnapshot->get_contexts();
164     for (uint c = 0; c < contexts.size(); c++)
165     {
166         vogl_context_snapshot* pContext = contexts[c];
167
168         const vogl_context_desc& desc = pContext->get_context_desc();
169         vogleditor_stateTreeContextItem* pContextItem = new vogleditor_stateTreeContextItem(tmp.sprintf("Context %p", (void*)desc.get_trace_context()), "", parent, *pContext);
170         if (m_pBaseSnapshot != NULL && m_pBaseSnapshot->get_contexts().size() > 0 && m_pBaseSnapshot->get_contexts().at(c) != NULL)
171         {
172             // set the diff state to be the same state, so that there does not appear to be any diff's
173             const vogl_context_snapshot* pDiffContext = m_pBaseSnapshot->get_contexts()[c];
174             pContextItem->set_diff_base_state(pDiffContext);
175
176             // if this is the current context, store it
177             if (pContext->get_context_desc().get_trace_context() == pSnapshot->get_cur_trace_context())
178             {
179                 m_pCurrentContextItem = pContextItem;
180             }
181         }
182
183         parent->appendChild(pContextItem);
184     }
185 }
186
187 vogl::vector<vogleditor_stateTreeProgramItem*> vogleditor_QStateTreeModel::get_program_objects()
188 {
189     vogl::vector<vogleditor_stateTreeProgramItem*> programs;
190     if (m_pCurrentContextItem != NULL)
191     {
192         programs = m_pCurrentContextItem->get_program_objects();
193     }
194     return programs;
195 }