more efficient desktop icons
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include "desktopstatemanager.hpp"
|
||||
#include <QDir>
|
||||
#include <QFileInfoList>
|
||||
#include <QPoint>
|
||||
|
||||
namespace ZShell::services {
|
||||
|
||||
@@ -37,7 +38,29 @@ QHash<int, QByteArray> DesktopModel::roleNames() const {
|
||||
return roles;
|
||||
}
|
||||
|
||||
QPoint DesktopModel::getEmptySpot(const QSet<QString> &occupied) const {
|
||||
for (int x = 0; ; ++x) {
|
||||
for (int y = 0; y < m_rows; ++y) {
|
||||
QString key = QString::number(x) + "," + QString::number(y);
|
||||
if (!occupied.contains(key)) {
|
||||
return QPoint(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DesktopModel::loadDirectory(const QString &path) {
|
||||
m_watchedPath = path;
|
||||
|
||||
if (!m_watcher.directories().isEmpty())
|
||||
m_watcher.removePaths(m_watcher.directories());
|
||||
|
||||
m_watcher.addPath(path);
|
||||
|
||||
connect(&m_watcher, &QFileSystemWatcher::directoryChanged,
|
||||
this, &DesktopModel::onDirectoryChanged,
|
||||
Qt::UniqueConnection);
|
||||
|
||||
beginResetModel();
|
||||
m_items.clear();
|
||||
|
||||
@@ -48,6 +71,14 @@ void DesktopModel::loadDirectory(const QString &path) {
|
||||
DesktopStateManager sm;
|
||||
QVariantMap savedLayout = sm.getLayout();
|
||||
|
||||
QSet<QString> occupied;
|
||||
for (const QFileInfo &fileInfo : list) {
|
||||
if (savedLayout.contains(fileInfo.fileName())) {
|
||||
QVariantMap pos = savedLayout[fileInfo.fileName()].toMap();
|
||||
occupied.insert(QString::number(pos["x"].toInt()) + "," + QString::number(pos["y"].toInt()));
|
||||
}
|
||||
}
|
||||
|
||||
for (const QFileInfo &fileInfo : list) {
|
||||
DesktopItem item;
|
||||
item.fileName = fileInfo.fileName();
|
||||
@@ -59,15 +90,20 @@ void DesktopModel::loadDirectory(const QString &path) {
|
||||
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;
|
||||
QPoint spot = getEmptySpot(occupied);
|
||||
item.gridX = spot.x();
|
||||
item.gridY = spot.y();
|
||||
occupied.insert(QString::number(item.gridX) + "," + QString::number(item.gridY));
|
||||
}
|
||||
m_items.append(item);
|
||||
}
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void DesktopModel::onDirectoryChanged() {
|
||||
loadDirectory(m_watchedPath);
|
||||
}
|
||||
|
||||
void DesktopModel::moveIcon(int index, int newX, int newY) {
|
||||
if (index < 0 || index >= m_items.size()) return;
|
||||
|
||||
@@ -183,4 +219,4 @@ void DesktopModel::massMove(const QVariantList& selectedPathsList, const QString
|
||||
saveCurrentLayout();
|
||||
}
|
||||
|
||||
} // namespace ZShell::services
|
||||
};
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
#include <QQmlEngine>
|
||||
#include <cstdint>
|
||||
#include <QFileSystemWatcher>
|
||||
|
||||
namespace ZShell::services {
|
||||
|
||||
@@ -39,9 +39,27 @@ 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);
|
||||
|
||||
Q_PROPERTY(int rows READ rows WRITE setRows NOTIFY rowsChanged)
|
||||
|
||||
public:
|
||||
[[nodiscard]] int rows() const {
|
||||
return m_rows;
|
||||
}
|
||||
void setRows(int r) {
|
||||
if (m_rows != r) { m_rows = r; emit rowsChanged(); }
|
||||
}
|
||||
|
||||
signals:
|
||||
void rowsChanged();
|
||||
|
||||
private:
|
||||
int m_rows = 1;
|
||||
QList<DesktopItem> m_items;
|
||||
QString m_watchedPath;
|
||||
QFileSystemWatcher m_watcher;
|
||||
void saveCurrentLayout();
|
||||
[[nodiscard]] QPoint getEmptySpot(const QSet<QString> &occupied) const;
|
||||
void onDirectoryChanged();
|
||||
};
|
||||
|
||||
} // namespace ZShell::Services
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QDebug>
|
||||
#include <QJsonArray>
|
||||
|
||||
namespace ZShell::services {
|
||||
|
||||
@@ -12,7 +13,7 @@ DesktopStateManager::DesktopStateManager(QObject *parent) : QObject(parent) {
|
||||
}
|
||||
|
||||
QString DesktopStateManager::getConfigFilePath() const {
|
||||
QString configDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/sleex";
|
||||
QString configDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/zshell";
|
||||
QDir dir(configDir);
|
||||
if (!dir.exists()) {
|
||||
dir.mkpath(".");
|
||||
@@ -29,7 +30,7 @@ void DesktopStateManager::saveLayout(const QVariantMap& layout) {
|
||||
file.write(doc.toJson(QJsonDocument::Indented));
|
||||
file.close();
|
||||
} else {
|
||||
qWarning() << "Sleex: Impossible de sauvegarder le layout du bureau dans" << getConfigFilePath();
|
||||
qWarning() << "zshell: Cannot save desktop layout to" << getConfigFilePath();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user