summaryrefslogtreecommitdiff
path: root/src/widgetinfomodel.cpp
blob: e2f145f4602cefd2c44c6ce6f863f6a258324c4b (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <QtCore/QDebug>
#include <QtCore/QStringList>
#include "widgetinfomodel.h"

namespace
{

inline QString get_widget_dconf_base(int index)
{
	return QString("widget%1_").arg(index);
}

}

WidgetInfoModel::WidgetInfoModel(const QString &settingsPrefix, QObject *parent) :
	QAbstractListModel(parent),
	_settings(new MDConfGroup(this))
{
	_settings->setPath(settingsPrefix);
	connect(_settings, &MDConfGroup::valueChanged,
			this, &WidgetInfoModel::handleSettingChanged);
	reload();
}

QHash<int, QByteArray> WidgetInfoModel::roleNames() const
{
	QHash<int, QByteArray> roles;

	roles[UrlRole] = "url";
	roles[InvertRole] = "invert";
	roles[PageRole] = "page";
	roles[SizeRole] = "size";
	roles[PositionRole] = "position";

	return roles;
}

int WidgetInfoModel::rowCount(const QModelIndex &parent) const
{
	if (parent.isValid()) return 0;
	return _widgets.size();
}

QVariant WidgetInfoModel::data(const QModelIndex &index, int role) const
{
	if (!index.isValid()) return QVariant();
	const int row = index.row();
	if (row < 0 || row >= _widgets.size()) return QVariant();

	switch (role) {
	case UrlRole:
		return QVariant::fromValue<QUrl>(_widgets[row].url());
	case InvertRole:
		return QVariant::fromValue<bool>(_widgets[row].invert());
	case PageRole:
		return QVariant::fromValue<int>(_widgets[row].page());
	case SizeRole:
		return QVariant::fromValue<int>(_widgets[row].size());
	case PositionRole:
		return QVariant::fromValue<int>(_widgets[row].position());
	default:
		qWarning() << "Unknown role:" << role;
		return QVariant();
	}
}

QList<WidgetInfo> WidgetInfoModel::toList() const
{
	return _widgets.toList();
}

void WidgetInfoModel::reload()
{
	beginResetModel();
	_widgets.resize(16);

	for (int i = 0; i < _widgets.size(); i++) {
		WidgetInfo &info = _widgets[i];
		const QString base = get_widget_dconf_base(i);

		info.setInvert(_settings->value(base + "invert").toBool());
		info.setPage(_settings->value(base + "page").toInt());
		info.setSize(static_cast<WidgetInfo::WidgetSize>(_settings->value(base + "size").toInt()));
		info.setPosition(static_cast<WidgetInfo::WidgetPosition>(_settings->value(base + "position").toInt()));
		info.setUrl(_settings->value(base + "url").toUrl());
	}

	endResetModel();
}

int WidgetInfoModel::addWidget(const QUrl &url, int page, WidgetInfo::WidgetPosition pos, WidgetInfo::WidgetSize size)
{
	int slot = findEmptySlot();
	if (slot == -1) {
		qWarning() << "No empty slots!"; // This shouldn't happen
		return slot;
	}

	qDebug() << "Adding widget" << url << page << pos << size;

	const QString base = get_widget_dconf_base(slot);

	_settings->setValue(base + "invert", QVariant::fromValue<bool>(false));
	_settings->setValue(base + "page", QVariant::fromValue<int>(page));
	_settings->setValue(base + "size", QVariant::fromValue<int>(size));
	_settings->setValue(base + "position", QVariant::fromValue<int>(pos));
	_settings->setValue(base + "url", QVariant::fromValue<QUrl>(url));

	// MDConf watcher will update the model.

	return slot;
}

void WidgetInfoModel::removeWidget(int widgetId)
{
	const QString base = get_widget_dconf_base(widgetId);

	_settings->setValue(base + "url", QVariant());
	_settings->setValue(base + "invert", QVariant());
	_settings->setValue(base + "page", QVariant());
	_settings->setValue(base + "size", QVariant());
	_settings->setValue(base + "position", QVariant());
}

int WidgetInfoModel::findEmptySlot()
{
	for (int i = 0; i < _widgets.size(); i++) {
		if (_widgets[i].url().isEmpty()) {
			return i;
		}
	}

	return -1;
}

void WidgetInfoModel::handleSettingChanged(const QString &key)
{
	static const QString widgetPrefix("widget");
	if (key.startsWith(widgetPrefix)) {
		QStringList parts = key.split('_');
		if (parts.size() == 2) {
			bool ok = false;
			int slot = parts[0].mid(widgetPrefix.length()).toInt(&ok);
			if (!ok || slot < 0 || slot >= _widgets.size()) {
				qWarning() << "Invalid widget number:" << key;
			}

			WidgetInfo &info = _widgets[slot];
			const QString &role = parts[1];
			QVector<int> roles;

			if (role == "url") {
				info.setUrl(_settings->value(key).toUrl());
				roles << UrlRole;
			} else if (role == "invert") {
				info.setInvert(_settings->value(key).toBool());
				roles << InvertRole;
			} else if (role == "page") {
				info.setPage(_settings->value(key).toInt());
				roles << PageRole;
			} else if (role == "size") {
				info.setSize(static_cast<WidgetInfo::WidgetSize>(_settings->value(key).toInt()));
				roles << SizeRole;
			} else if (role == "position") {
				info.setPosition(static_cast<WidgetInfo::WidgetPosition>(_settings->value(key).toInt()));
				roles << PositionRole;
			} else {
				qWarning() << "Unknown widget key changed:" << key;
				return;
			}

			const QModelIndex index = createIndex(slot, 0);
			emit dataChanged(index, index, roles);
		} else {
			qWarning() << "Unknown widget key changed:" << key;
		}
	}
}