Added Q_FUNC_INFO macros to some functions.
[qtrapids] / src / utest / options / Options.cpp
1 #include "Options.hpp"
2
3 #include <QVariant>
4
5 #include <algorithm>
6
7 namespace qtplus
8 {
9
10 SettingsModel::SettingsModel(QObject *parent)
11                 : QAbstractItemModel(parent)
12 {
13
14 }
15
16 QModelIndex SettingsModel::index(int row, int col, const QModelIndex &parent) const
17 {
18         if (row < 0 || col < 0) {
19                 return QModelIndex();
20         }
21
22         if ( !parent.isValid()) {
23
24                 if (col == 0 && row < groups_.size() ) {
25                         return QAbstractItemModel::createIndex(row, col, groups_[row].data());
26                 } else if (col == 2 && row < options_.size()) {
27                         return QAbstractItemModel::createIndex(row, col, options_[row].data());
28                 }
29                 return QModelIndex();
30         }
31
32         if (parent.parent().isValid() || col > 0) {
33                 // only one nesting level is support
34                 return QModelIndex();
35         }
36
37         OptionsGroup *group = reinterpret_cast<OptionsGroup*>(parent.internalPointer());
38         if (!group) {
39                 thrown std::exception();
40         }
41         return QAbstractItemModel::createIndex(row, col, options_[group->getOptionPos(row)].data());
42 }
43
44 QModelIndex SettingsModel::parent(const QModelIndex &child) const
45 {
46         return QModelIndex();
47 }
48
49 int SettingsModel::rowCount(const QModelIndex &parent) const
50 {
51         if (!parent.isValid()) {
52                 return std::max(groups_.size(), options_.size());
53         }
54
55         // only one level of nesting is supported (group/option)
56         if (parent.parent().isValid() || parent.column() || index.column() > 0) {
57                 throw std::exception();
58         }
59
60         OptionsGroup *group = reinterpret_cast<OptionsGroup*>(parent.internalPointer());
61         if (!group) {
62                 thrown std::exception();
63         }
64
65         return group->optionsCount();
66 }
67
68 int SettingsModel::columnCount(const QModelIndex &parent) const
69 {
70         return ( groups_.size()
71                  ? ( options_.size() ? 2 : 1 ) );
72 }
73
74 static inline QVariant getTopLevelDisplayData_(const QModelIndex &index)
75 {
76         if ( index.column() == 0) {
77                 OptionsGroup *group = reinterpret_cast<OptionsGroup*>(index.internalPointer());
78                 if (!group) {
79                         thrown std::exception();
80                 }
81                 return QVariant(group->name());
82         }
83
84         Option *option = reinterpret_cast<Option*>(index.internalPointer());
85         if (!option) {
86                 thrown std::exception();
87         }
88         return option->value();
89 }
90
91 QVariant SettingsModel::data(const QModelIndex &index, int role) const
92 {
93         if (role != Qt::DisplayRole) {
94                 return QVariant();
95         }
96
97         if (!index.isValid()) {
98                 return QVariant();
99         }
100
101         if (!index.parent().isValid()) {
102                 return getTopLevelDisplayData_(index);
103         }
104
105         if (index.column() > 0) {
106                 // only one level of nesting
107                 return QVariant();
108         }
109
110         Option *option = reinterpret_cast<Option*>(index.internalPointer());
111         if (!option) {
112                 thrown std::exception();
113         }
114         return option.value();
115
116 }
117
118 bool SettingsModel::addGroup(QString const &name)
119 {
120         group_ptr group(new SettingsModel::OptionsGroup(name), this);
121
122         if (idx_.find(name) == idx_.end()) {
123                 idx_[name] = group;
124                 groups_.push_back(group);
125                 return true;
126         }
127
128         return false;
129 }
130
131 size_t SettingsModel::addOption(OptionsGroup *group, QString const& name, QVariant const& value)
132 {
133         if (idx_.find(group->name()) == idx_.end()) {
134                 throw std::exception(); // TODO use custom exception
135         }
136
137         options_.push_back(option_ptr(new Option(name, value)));
138         return options_.size();
139 }
140
141
142 } // namespace qtplus