This commit is contained in:
Zacharias-Brohn
2026-03-12 10:04:27 +01:00
parent 401ccef90c
commit 851b78f0ff
17 changed files with 1347 additions and 64 deletions
+4
View File
@@ -7,7 +7,11 @@ qml_module(ZShell-services
audiocollector.hpp audiocollector.cpp
audioprovider.hpp audioprovider.cpp
cavaprovider.hpp cavaprovider.cpp
desktopmodel.hpp desktopmodel.cpp
desktopstatemanager.hpp desktopstatemanager.cpp
LIBRARIES
Qt6::Core
Qt6::Qml
PkgConfig::Pipewire
PkgConfig::Aubio
PkgConfig::Cava
+186
View File
@@ -0,0 +1,186 @@
#include "desktopmodel.hpp"
#include "desktopstatemanager.hpp"
#include <QDir>
#include <QFileInfoList>
namespace ZShell::services {
DesktopModel::DesktopModel(QObject *parent) : QAbstractListModel(parent) {
}
int DesktopModel::rowCount(const QModelIndex &parent) const {
if (parent.isValid()) return 0;
return m_items.count();
}
QVariant DesktopModel::data(const QModelIndex &index, int role) const {
if (!index.isValid() || index.row() >= m_items.size()) return QVariant();
const DesktopItem &item = m_items[index.row()];
switch (role) {
case FileNameRole: return item.fileName;
case FilePathRole: return item.filePath;
case IsDirRole: return item.isDir;
case GridXRole: return item.gridX;
case GridYRole: return item.gridY;
default: return QVariant();
}
}
QHash<int, QByteArray> DesktopModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[FileNameRole] = "fileName";
roles[FilePathRole] = "filePath";
roles[IsDirRole] = "isDir";
roles[GridXRole] = "gridX";
roles[GridYRole] = "gridY";
return roles;
}
void DesktopModel::loadDirectory(const QString &path) {
beginResetModel();
m_items.clear();
QDir dir(path);
dir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList list = dir.entryInfoList();
DesktopStateManager sm;
QVariantMap savedLayout = sm.getLayout();
for (const QFileInfo &fileInfo : list) {
DesktopItem item;
item.fileName = fileInfo.fileName();
item.filePath = fileInfo.absoluteFilePath();
item.isDir = fileInfo.isDir();
if (savedLayout.contains(item.fileName)) {
QVariantMap pos = savedLayout[item.fileName].toMap();
item.gridX = pos["x"].toInt();
item.gridY = pos["y"].toInt();
} else {
// TODO: make getEmptySpot in C++ and call it here to get the initial position for new icons
item.gridX = 0;
item.gridY = 0;
}
m_items.append(item);
}
endResetModel();
}
void DesktopModel::moveIcon(int index, int newX, int newY) {
if (index < 0 || index >= m_items.size()) return;
m_items[index].gridX = newX;
m_items[index].gridY = newY;
QModelIndex modelIndex = createIndex(index, 0);
emit dataChanged(modelIndex, modelIndex, {GridXRole, GridYRole});
saveCurrentLayout();
}
void DesktopModel::saveCurrentLayout() {
QVariantMap layout;
for (const auto& item : m_items) {
QVariantMap pos;
pos["x"] = item.gridX;
pos["y"] = item.gridY;
layout[item.fileName] = pos;
}
DesktopStateManager sm;
sm.saveLayout(layout);
}
void DesktopModel::massMove(const QVariantList& selectedPathsList, const QString& leaderPath, int targetX, int targetY, int maxCol, int maxRow) {
QStringList selectedPaths;
for (const QVariant& v : selectedPathsList) {
selectedPaths << v.toString();
}
if (selectedPaths.isEmpty()) return;
int oldX = 0, oldY = 0;
for (const auto& item : m_items) {
if (item.filePath == leaderPath) {
oldX = item.gridX;
oldY = item.gridY;
break;
}
}
int deltaX = targetX - oldX;
int deltaY = targetY - oldY;
if (deltaX == 0 && deltaY == 0) return;
if (selectedPaths.size() == 1 && targetX >= 0 && targetX <= maxCol && targetY >= 0 && targetY <= maxRow) {
QString movingPath = selectedPaths.first();
int movingIndex = -1;
int targetIndex = -1;
for (int i = 0; i < m_items.size(); ++i) {
if (m_items[i].filePath == movingPath) {
movingIndex = i;
} else if (m_items[i].gridX == targetX && m_items[i].gridY == targetY) {
targetIndex = i;
}
}
if (targetIndex != -1 && movingIndex != -1) {
m_items[targetIndex].gridX = oldX;
m_items[targetIndex].gridY = oldY;
m_items[movingIndex].gridX = targetX;
m_items[movingIndex].gridY = targetY;
emit dataChanged(index(0, 0), index(m_items.size() - 1, 0), {GridXRole, GridYRole});
saveCurrentLayout();
return;
}
}
QList<DesktopItem*> movingItems;
QSet<QString> occupied;
for (int i = 0; i < m_items.size(); ++i) {
if (selectedPaths.contains(m_items[i].filePath)) {
movingItems.append(&m_items[i]);
} else {
occupied.insert(QString::number(m_items[i].gridX) + "," + QString::number(m_items[i].gridY));
}
}
for (auto* item : movingItems) {
int newX = item->gridX + deltaX;
int newY = item->gridY + deltaY;
bool outOfBounds = newX < 0 || newX > maxCol || newY < 0 || newY > maxRow;
bool collision = occupied.contains(QString::number(newX) + "," + QString::number(newY));
if (outOfBounds || collision) {
bool found = false;
for (int x = 0; x <= maxCol && !found; ++x) {
for (int y = 0; y <= maxRow && !found; ++y) {
QString key = QString::number(x) + "," + QString::number(y);
if (!occupied.contains(key)) {
newX = x;
newY = y;
occupied.insert(key);
found = true;
}
}
}
} else {
occupied.insert(QString::number(newX) + "," + QString::number(newY));
}
item->gridX = newX;
item->gridY = newY;
}
emit dataChanged(index(0, 0), index(m_items.size() - 1, 0), {GridXRole, GridYRole});
saveCurrentLayout();
}
} // namespace ZShell::services
+46
View File
@@ -0,0 +1,46 @@
#pragma once
#include <QAbstractListModel>
#include <QList>
#include <QString>
#include <QQmlEngine>
namespace ZShell::services {
struct DesktopItem {
QString fileName;
QString filePath;
bool isDir;
int gridX;
int gridY;
};
class DesktopModel : public QAbstractListModel {
Q_OBJECT
QML_ELEMENT
public:
enum DesktopRoles {
FileNameRole = Qt::UserRole + 1,
FilePathRole,
IsDirRole,
GridXRole,
GridYRole
};
explicit DesktopModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override;
Q_INVOKABLE void loadDirectory(const QString &path);
Q_INVOKABLE void moveIcon(int index, int newX, int newY);
Q_INVOKABLE void massMove(const QVariantList &selectedPathsList, const QString &leaderPath, int targetX, int targetY, int maxCol, int maxRow);
private:
QList<DesktopItem> m_items;
void saveCurrentLayout();
};
} // namespace ZShell::services
@@ -0,0 +1,54 @@
#include "desktopstatemanager.hpp"
#include <QStandardPaths>
#include <QDir>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDebug>
namespace ZShell::services {
DesktopStateManager::DesktopStateManager(QObject *parent) : QObject(parent) {
}
QString DesktopStateManager::getConfigFilePath() const {
QString configDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/sleex";
QDir dir(configDir);
if (!dir.exists()) {
dir.mkpath(".");
}
return configDir + "/desktop_layout.json";
}
void DesktopStateManager::saveLayout(const QVariantMap& layout) {
QJsonObject jsonObj = QJsonObject::fromVariantMap(layout);
QJsonDocument doc(jsonObj);
QFile file(getConfigFilePath());
if (file.open(QIODevice::WriteOnly)) {
file.write(doc.toJson(QJsonDocument::Indented));
file.close();
} else {
qWarning() << "Sleex: Impossible de sauvegarder le layout du bureau dans" << getConfigFilePath();
}
}
QVariantMap DesktopStateManager::getLayout() {
QFile file(getConfigFilePath());
if (!file.open(QIODevice::ReadOnly)) {
return QVariantMap();
}
QByteArray data = file.readAll();
file.close();
QJsonDocument doc = QJsonDocument::fromJson(data);
if (doc.isObject()) {
return doc.object().toVariantMap();
}
return QVariantMap();
}
} // namespace ZShell::services
@@ -0,0 +1,24 @@
#pragma once
#include <QObject>
#include <QVariantMap>
#include <QQmlEngine>
namespace ZShell::services {
class DesktopStateManager : public QObject {
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
public:
explicit DesktopStateManager(QObject *parent = nullptr);
Q_INVOKABLE void saveLayout(const QVariantMap& layout);
Q_INVOKABLE QVariantMap getLayout();
private:
QString getConfigFilePath() const;
};
} // namespace ZShell::services