From a9d0443489bdb33ddb7250b33cff554067f50201 Mon Sep 17 00:00:00 2001 From: Michail Vourlakos Date: Wed, 16 Jun 2021 13:12:47 +0300 Subject: [PATCH] detailsdialog:provide color schemes model --- app/settings/detailsdialog/CMakeLists.txt | 1 + app/settings/detailsdialog/schemesmodel.cpp | 126 ++++++++++++++++++++ app/settings/detailsdialog/schemesmodel.h | 54 +++++++++ app/tools/commontools.cpp | 10 +- 4 files changed, 187 insertions(+), 4 deletions(-) create mode 100644 app/settings/detailsdialog/schemesmodel.cpp create mode 100644 app/settings/detailsdialog/schemesmodel.h diff --git a/app/settings/detailsdialog/CMakeLists.txt b/app/settings/detailsdialog/CMakeLists.txt index bec36607d..37f88fa07 100644 --- a/app/settings/detailsdialog/CMakeLists.txt +++ b/app/settings/detailsdialog/CMakeLists.txt @@ -4,6 +4,7 @@ set(lattedock-app_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/detailsdialog.cpp ${CMAKE_CURRENT_SOURCE_DIR}/detailshandler.cpp ${CMAKE_CURRENT_SOURCE_DIR}/patternwidget.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/schemesmodel.cpp PARENT_SCOPE ) diff --git a/app/settings/detailsdialog/schemesmodel.cpp b/app/settings/detailsdialog/schemesmodel.cpp new file mode 100644 index 000000000..a490643aa --- /dev/null +++ b/app/settings/detailsdialog/schemesmodel.cpp @@ -0,0 +1,126 @@ +/* + SPDX-FileCopyrightText: 2021 Michail Vourlakos + SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "schemesmodel.h" + +// local +#include "../../data/layoutdata.h" +#include "../../layouts/importer.h" +#include "../../wm/schemecolors.h" + +// Qt +#include +#include + +// KDE +#include + +namespace Latte { +namespace Settings { +namespace Model { + +Schemes::Schemes(QObject *parent) + : QAbstractListModel(parent) +{ + initSchemes(); +} + +Schemes::~Schemes() +{ + qDeleteAll(m_schemes); +} + +int Schemes::rowCount(const QModelIndex &parent) const +{ + if (parent.isValid()) { + return 0; + } + + return m_schemes.count(); +} + +void Schemes::initSchemes() +{ + qDeleteAll(m_schemes); + m_schemes.clear(); + + QString currentSchemePath = WindowSystem::SchemeColors::possibleSchemeFile(Data::Layout::DEFAULTSCHEMEFILE); + insertSchemeInList(currentSchemePath); + + QStringList standardPaths = Latte::Layouts::Importer::standardPathsFor("color-schemes"); + + QStringList registeredSchemes; + + for(auto path : standardPaths) { + QDir directory(path); + QStringList tempSchemes = directory.entryList(QStringList() << "*.colors" << "*.COLORS", QDir::Files); + + foreach (QString filename, tempSchemes) { + if (!registeredSchemes.contains(filename)) { + QString fullPath = path + "/" + filename; + insertSchemeInList(fullPath); + registeredSchemes << filename; + } + } + } +} + +void Schemes::insertSchemeInList(QString file) +{ + WindowSystem::SchemeColors *tempScheme = new WindowSystem::SchemeColors(this, file); + + int atPos{0}; + + for (int i = 0; i < m_schemes.count(); i++) { + WindowSystem::SchemeColors *s = m_schemes[i]; + + int result = QString::compare(tempScheme->schemeName(), s->schemeName(), Qt::CaseInsensitive); + + if (result < 0) { + atPos = i; + break; + } else { + atPos = i + 1; + } + + } + + m_schemes.insert(atPos, tempScheme); +} + +QVariant Schemes::data(const QModelIndex &index, int role) const +{ + if (!index.isValid() || index.column() != 0 || index.row() < 0 || index.row() >= m_schemes.count()) { + return QVariant(); + } + + const WindowSystem::SchemeColors *d = m_schemes[index.row()]; + + switch (role) { + case IDROLE: + return index.row() == 0 ? Data::Layout::DEFAULTSCHEMEFILE : d->schemeFile(); + break; + + case Qt::DisplayRole: + case NAMEROLE: + return index.row() == 0 ? i18n("System Colors") : d->schemeName(); + break; + + case TEXTCOLORROLE: + return d->textColor(); + break; + + case BACKGROUNDCOLORROLE: + return d->backgroundColor(); + break; + } + + return QVariant(); +} + +} +} +} + diff --git a/app/settings/detailsdialog/schemesmodel.h b/app/settings/detailsdialog/schemesmodel.h new file mode 100644 index 000000000..243b62863 --- /dev/null +++ b/app/settings/detailsdialog/schemesmodel.h @@ -0,0 +1,54 @@ +/* + SPDX-FileCopyrightText: 2021 Michail Vourlakos + SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef SETTINGSSCHEMESMODEL_H +#define SETTINGSSCHEMESMODEL_H + +#include + +namespace Latte { +namespace WindowSystem { +class SchemeColors; +} +} + +namespace Latte { +namespace Settings { +namespace Model { + +class Schemes : public QAbstractListModel +{ + Q_OBJECT + +public: + enum SchemesRoles + { + IDROLE = Qt::UserRole + 1, + NAMEROLE, + TEXTCOLORROLE, + BACKGROUNDCOLORROLE + }; + + explicit Schemes(QObject *parent = nullptr); + virtual ~Schemes(); + + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + +private slots: + void initSchemes(); + +private: + void insertSchemeInList(QString file); + +private: + QList m_schemes; +}; + +} +} +} + +#endif diff --git a/app/tools/commontools.cpp b/app/tools/commontools.cpp index 6b9aa36ee..94004fdf1 100644 --- a/app/tools/commontools.cpp +++ b/app/tools/commontools.cpp @@ -83,16 +83,18 @@ QString standardPath(QString subPath, bool localfirst) { QStringList paths = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation); + QString separator = subPath.startsWith("/") ? "" : "/"; + if (localfirst) { for (const auto &pt : paths) { - QString ptF = pt + "/" +subPath; + QString ptF = pt + separator +subPath; if (QFileInfo(ptF).exists()) { return ptF; } } } else { for (int i=paths.count()-1; i>=0; i--) { - QString ptF = paths[i] + "/" +subPath; + QString ptF = paths[i] + separator +subPath; if (QFileInfo(ptF).exists()) { return ptF; } @@ -100,8 +102,8 @@ QString standardPath(QString subPath, bool localfirst) } //! in any case that above fails - if (QFileInfo("/usr/share/"+subPath).exists()) { - return "/usr/share/"+subPath; + if (QFileInfo("/usr/share" + separator + subPath).exists()) { + return "/usr/share" + separator + subPath; } return "";