kek test
This commit is contained in:
@@ -4,6 +4,7 @@ pkg_check_modules(Qalculate IMPORTED_TARGET libqalculate REQUIRED)
|
||||
pkg_check_modules(Pipewire IMPORTED_TARGET libpipewire-0.3 REQUIRED)
|
||||
pkg_check_modules(Aubio IMPORTED_TARGET aubio REQUIRED)
|
||||
pkg_check_modules(Cava IMPORTED_TARGET libcava QUIET)
|
||||
pkg_check_modules(GLIB REQUIRED glib-2.0 gobject-2.0 gio-2.0)
|
||||
if(NOT Cava_FOUND)
|
||||
pkg_check_modules(Cava IMPORTED_TARGET cava REQUIRED)
|
||||
endif()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user