summaryrefslogtreecommitdiff
path: root/src/widgetinfomodel.cpp
blob: 9bb53e47e9570c1c7271c460362d285ff1a7491e (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <QtCore/QDebug>
#include <QtCore/QBitArray>
#include <QtCore/QStringList>
#include "widgetinfomodel.h"

namespace
{

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

WidgetInfo::WidgetPosition canonicalize_widget_pos(WidgetInfo::WidgetPosition pos, WidgetInfo::WidgetSize size)
{
	switch (size) {
	case WidgetInfo::Size1Q:
		return pos; // Any position is valid
	case WidgetInfo::Size2QHorizontal:
		switch (pos) {
		case WidgetInfo::PosNW:
		case WidgetInfo::PosNE:
			return WidgetInfo::PosNW;
		case WidgetInfo::PosSW:
		case WidgetInfo::PosSE:
			return WidgetInfo::PosSW;
		}
		break;
	case WidgetInfo::Size2QVertical:
		switch (pos) {
		case WidgetInfo::PosNW:
		case WidgetInfo::PosSW:
			return WidgetInfo::PosNW;
		case WidgetInfo::PosNE:
		case WidgetInfo::PosSE:
			return WidgetInfo::PosNE;
		}
		break;
	case WidgetInfo::Size4Q:
		return WidgetInfo::PosNW; // 4Q widgets use entire screen
	}

	return pos;
}

QBitArray used_positions(WidgetInfo::WidgetPosition pos, WidgetInfo::WidgetSize size)
{
	QBitArray used(4, false);

	switch (size) {
	case WidgetInfo::Size1Q:
		used[pos] = true;
		break;
	case WidgetInfo::Size2QHorizontal:
		switch (pos) {
		case WidgetInfo::PosNW:
		case WidgetInfo::PosNE:
			used[WidgetInfo::PosNW] = true;
			used[WidgetInfo::PosNE] = true;
			break;
		case WidgetInfo::PosSW:
		case WidgetInfo::PosSE:
			used[WidgetInfo::PosNW] = true;
			used[WidgetInfo::PosNE] = true;
			break;
		}
		break;
	case WidgetInfo::Size2QVertical:
		switch (pos) {
		case WidgetInfo::PosNW:
		case WidgetInfo::PosSW:
			used[WidgetInfo::PosNW] = true;
			used[WidgetInfo::PosSW] = true;
			break;
		case WidgetInfo::PosNE:
		case WidgetInfo::PosSE:
			used[WidgetInfo::PosNE] = true;
			used[WidgetInfo::PosSE] = true;
			break;
		}
		break;
	case WidgetInfo::Size4Q:
		used.fill(true);
		break;
	}

	return used;
}

}

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();
}

bool WidgetInfoModel::widgetOverlaps(int page, WidgetInfo::WidgetPosition pos, WidgetInfo::WidgetSize size) const
{
	QBitArray usedPos = used_positions(pos, size);

	for (int i = 0; i < _widgets.size(); i++) {
		if (_widgets[i].url().isEmpty()) continue;
		if (_widgets[i].page() != page) continue;

		QBitArray intersection = usedPos & used_positions(_widgets[i].position(), _widgets[i].size());
		if (intersection.count(true) > 0) {
			return true;
		}
	}

	return false;
}

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 -1;
	}

	pos = canonicalize_widget_pos(pos, size);

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

	if (widgetOverlaps(page, pos, size)) {
		qWarning() << "Can't add the widget: it overlaps with an existing one!";
		return -1;
	}

	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());
}

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::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;
		}
	}
}