/
stylemodel.cpp
148 lines (127 loc) · 4.18 KB
/
stylemodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "stylemodel.h"
#include "styleitem.h"
#include "util.h"
#include <QPixmap>
CStyleModel::CStyleModel(QWidget& aParent, CartoType::CFramework& aFramework,const std::string& aStyleSheet,CartoType::CLegend& aLegend):
QAbstractItemModel(&aParent),
m_parent(aParent),
m_framework(aFramework),
m_legend(aLegend),
m_style_sheet_text(aStyleSheet),
m_root(nullptr)
{
if (!ParseStyleSheet())
return;
PopulateTreeNode(m_xml_document.first_node(),nullptr);
if (m_root)
m_root->CreateImages();
}
CStyleModel::~CStyleModel()
{
delete m_root;
}
std::string CStyleModel::EditedStyleSheetText() const
{
if (m_root)
return m_root->Xml(true);
return "";
}
bool CStyleModel::ParseStyleSheet()
{
try
{
m_style_sheet_text_copy = m_style_sheet_text;
m_xml_document.parse<0>(&m_style_sheet_text_copy[0]);
}
catch (rapidxml::parse_error e)
{
m_error_message = e.what();
const char* w = e.where<char>();
m_error_location.assign(w,std::min(size_t(32),strlen(w)));
return false;
}
// Check that the top-level object is a CartoTypeStyleSheet.
rapidxml::xml_node<>* top_node = m_xml_document.first_node();
if (top_node == nullptr || strcmp(top_node->name(),"CartoTypeStyleSheet"))
{
m_error_message = "not a CartoType style sheet";
return false;
}
return true;
}
QVariant CStyleModel::data(const QModelIndex& aIndex,int aRole) const
{
if (!aIndex.isValid())
return QVariant();
CStyleItem* item = static_cast<CStyleItem*>(aIndex.internalPointer());
return item->Data(aIndex.column(),aRole);
}
Qt::ItemFlags CStyleModel::flags(const QModelIndex& aIndex) const
{
if (!aIndex.isValid())
return 0;
return QAbstractItemModel::flags(aIndex);
}
QVariant CStyleModel::headerData(int aSection,Qt::Orientation aOrientation,int aRole) const
{
if (aOrientation == Qt::Horizontal && aRole == Qt::DisplayRole)
return m_root->Data(aSection,aRole);
return QVariant();
}
QModelIndex CStyleModel::index(int aRow,int aColumn,const QModelIndex& aParent) const
{
if (!hasIndex(aRow,aColumn,aParent))
return QModelIndex();
CStyleItem* parent_item = nullptr;
if (!aParent.isValid())
parent_item = m_root;
else
parent_item = static_cast<CStyleItem*>(aParent.internalPointer());
CStyleItem* child_item = parent_item->Child(aRow);
if (child_item)
return createIndex(aRow,aColumn,child_item);
else
return QModelIndex();
}
QModelIndex CStyleModel::parent(const QModelIndex& aIndex) const
{
if (!aIndex.isValid())
return QModelIndex();
CStyleItem* child_item = static_cast<CStyleItem*>(aIndex.internalPointer());
CStyleItem* parent_item = child_item->Parent();
if (parent_item == m_root)
return QModelIndex();
return createIndex(parent_item->Row(),0,parent_item);
}
int CStyleModel::rowCount(const QModelIndex& aParent) const
{
if (aParent.column() > 0)
return 0;
CStyleItem* parent_item = nullptr;
if (!aParent.isValid())
parent_item = m_root;
else
parent_item = static_cast<CStyleItem*>(aParent.internalPointer());
return parent_item->ChildCount();
}
int CStyleModel::columnCount(const QModelIndex& aParent) const
{
if (aParent.isValid())
return static_cast<CStyleItem*>(aParent.internalPointer())->ColumnCount();
else
return m_root->ColumnCount();
}
void CStyleModel::PopulateTreeNode(rapidxml::xml_node<>* aXmlNode,CStyleItem* aParent)
{
assert(aXmlNode);
CStyleItem* item = new CStyleItem(m_parent,m_framework,m_legend,m_style_sheet_text,aXmlNode,aParent);
if (!aParent)
{
assert(m_root == nullptr);
m_root = item;
}
// Make a recursive call to add the child items, unless this is an SVG node, in which case the content is added as a string.
if (strcmp(aXmlNode->name(),"svg"))
for (rapidxml::xml_node<>* node = aXmlNode->first_node(); node; node = node->next_sibling())
PopulateTreeNode(node,item);
}