summaryrefslogtreecommitdiff
path: root/src/widgetinfomodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgetinfomodel.cpp')
-rw-r--r--src/widgetinfomodel.cpp125
1 files changed, 113 insertions, 12 deletions
diff --git a/src/widgetinfomodel.cpp b/src/widgetinfomodel.cpp
index e2f145f..9bb53e4 100644
--- a/src/widgetinfomodel.cpp
+++ b/src/widgetinfomodel.cpp
@@ -1,4 +1,5 @@
#include <QtCore/QDebug>
+#include <QtCore/QBitArray>
#include <QtCore/QStringList>
#include "widgetinfomodel.h"
@@ -10,6 +11,82 @@ 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) :
@@ -69,23 +146,21 @@ QList<WidgetInfo> WidgetInfoModel::toList() const
return _widgets.toList();
}
-void WidgetInfoModel::reload()
+bool WidgetInfoModel::widgetOverlaps(int page, WidgetInfo::WidgetPosition pos, WidgetInfo::WidgetSize size) const
{
- beginResetModel();
- _widgets.resize(16);
+ QBitArray usedPos = used_positions(pos, size);
for (int i = 0; i < _widgets.size(); i++) {
- WidgetInfo &info = _widgets[i];
- const QString base = get_widget_dconf_base(i);
+ if (_widgets[i].url().isEmpty()) continue;
+ if (_widgets[i].page() != page) continue;
- 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());
+ QBitArray intersection = usedPos & used_positions(_widgets[i].position(), _widgets[i].size());
+ if (intersection.count(true) > 0) {
+ return true;
+ }
}
- endResetModel();
+ return false;
}
int WidgetInfoModel::addWidget(const QUrl &url, int page, WidgetInfo::WidgetPosition pos, WidgetInfo::WidgetSize size)
@@ -93,11 +168,18 @@ int WidgetInfoModel::addWidget(const QUrl &url, int page, WidgetInfo::WidgetPosi
int slot = findEmptySlot();
if (slot == -1) {
qWarning() << "No empty slots!"; // This shouldn't happen
- return slot;
+ 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));
@@ -122,6 +204,25 @@ void WidgetInfoModel::removeWidget(int widgetId)
_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++) {