1 #include "control/controlmodel.h"
2
3 #include "moc_controlmodel.cpp"
4
ControlModel(QObject * pParent)5 ControlModel::ControlModel(QObject* pParent)
6 : QAbstractTableModel(pParent) {
7
8 setHeaderData(CONTROL_COLUMN_GROUP, Qt::Horizontal, tr("Group"));
9 setHeaderData(CONTROL_COLUMN_ITEM, Qt::Horizontal, tr("Item"));
10 setHeaderData(CONTROL_COLUMN_VALUE, Qt::Horizontal, tr("Value"));
11 setHeaderData(CONTROL_COLUMN_PARAMETER, Qt::Horizontal, tr("Parameter"));
12 setHeaderData(CONTROL_COLUMN_TITLE, Qt::Horizontal, tr("Title"));
13 setHeaderData(CONTROL_COLUMN_DESCRIPTION, Qt::Horizontal, tr("Description"));
14 }
15
~ControlModel()16 ControlModel::~ControlModel() {
17 }
18
addControl(const ConfigKey & key,const QString & title,const QString & description)19 void ControlModel::addControl(const ConfigKey& key,
20 const QString& title,
21 const QString& description) {
22 ControlInfo info;
23 info.key = key;
24 info.title = title;
25 info.description = description;
26 info.pControl = new ControlProxy(info.key, this);
27
28 beginInsertRows(QModelIndex(), m_controls.size(),
29 m_controls.size());
30 m_controls.append(info);
31 endInsertRows();
32 }
33
rowCount(const QModelIndex & parent) const34 int ControlModel::rowCount(const QModelIndex& parent) const {
35 if (parent.isValid()) {
36 return 0;
37 }
38 return m_controls.size();
39 }
40
columnCount(const QModelIndex & parent) const41 int ControlModel::columnCount(const QModelIndex& parent) const {
42 if (parent.isValid()) {
43 return 0;
44 }
45 return NUM_CONTROL_COLUMNS;
46 }
47
data(const QModelIndex & index,int role) const48 QVariant ControlModel::data(const QModelIndex& index,
49 int role) const {
50 if (!index.isValid() || (role != Qt::DisplayRole &&
51 role != Qt::EditRole)) {
52 return QVariant();
53 }
54
55 int row = index.row();
56 int column = index.column();
57
58 if (row < 0 || row >= m_controls.size()) {
59 return QVariant();
60 }
61
62 const ControlInfo& control = m_controls.at(row);
63 switch (column) {
64 case CONTROL_COLUMN_GROUP:
65 return control.key.group;
66 case CONTROL_COLUMN_ITEM:
67 return control.key.item;
68 case CONTROL_COLUMN_VALUE:
69 return control.pControl->get();
70 case CONTROL_COLUMN_PARAMETER:
71 return control.pControl->getParameter();
72 case CONTROL_COLUMN_TITLE:
73 return control.title;
74 case CONTROL_COLUMN_DESCRIPTION:
75 return control.description;
76 case CONTROL_COLUMN_FILTER:
77 return QVariant(control.key.group % QStringLiteral(",") % control.key.item);
78 }
79 return QVariant();
80 }
81
setHeaderData(int section,Qt::Orientation orientation,const QVariant & value,int role)82 bool ControlModel::setHeaderData(int section,
83 Qt::Orientation orientation,
84 const QVariant& value,
85 int role) {
86 int numColumns = columnCount();
87 if (section < 0 || section >= numColumns) {
88 return false;
89 }
90
91 if (orientation != Qt::Horizontal) {
92 // We only care about horizontal headers.
93 return false;
94 }
95
96 if (m_headerInfo.size() != numColumns) {
97 m_headerInfo.resize(numColumns);
98 }
99
100 m_headerInfo[section][role] = value;
101 emit headerDataChanged(orientation, section, section);
102 return true;
103 }
104
headerData(int section,Qt::Orientation orientation,int role) const105 QVariant ControlModel::headerData(int section,
106 Qt::Orientation orientation,
107 int role) const {
108 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
109 QVariant headerValue = m_headerInfo.value(section).value(role);
110 if (!headerValue.isValid()) {
111 // Try EditRole if DisplayRole wasn't present
112 headerValue = m_headerInfo.value(section).value(Qt::EditRole);
113 }
114 if (!headerValue.isValid()) {
115 headerValue = QVariant(section).toString();
116 }
117 return headerValue;
118 }
119 return QAbstractTableModel::headerData(section, orientation, role);
120 }
121
setData(const QModelIndex & index,const QVariant & value,int role)122 bool ControlModel::setData(const QModelIndex& index,
123 const QVariant& value,
124 int role) {
125 if (!index.isValid() || role != Qt::EditRole) {
126 return false;
127 }
128
129 int row = index.row();
130 int column = index.column();
131
132 if (row < 0 || row >= m_controls.size()) {
133 return false;
134 }
135
136 ControlInfo& control = m_controls[row];
137
138 switch (column) {
139 case CONTROL_COLUMN_VALUE:
140 control.pControl->set(value.toDouble());
141 return true;
142 case CONTROL_COLUMN_PARAMETER:
143 control.pControl->setParameter(value.toDouble());
144 return true;
145 }
146
147 return false;
148 }
149
flags(const QModelIndex & index) const150 Qt::ItemFlags ControlModel::flags(const QModelIndex& index) const {
151 if (!index.isValid()) {
152 return Qt::ItemIsEnabled;
153 }
154
155 Qt::ItemFlags defaultFlags = QAbstractTableModel::flags(index);
156 if (index.column() == CONTROL_COLUMN_VALUE ||
157 index.column() == CONTROL_COLUMN_PARAMETER) {
158 defaultFlags |= Qt::ItemIsEditable;
159 }
160 return defaultFlags;
161 }
162