@ 261105.1 · fetching
current
/***************************************************************************
                               thememodel.h
                             -------------------
    begin                : 2026
    copyright            : (C) 2009-2026 by Andre Simon
    email                : a.simon@mailbox.org
 ***************************************************************************/

/*
This file is part of Highlight.

Highlight is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Highlight is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Highlight.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef THEMEMODEL_H
#define THEMEMODEL_H

#include <QAbstractListModel>
#include <QSortFilterProxyModel>
#include <QColor>
#include <QDir>
#include <QList>
#include <QString>
#include <QStringList>

/** Metadata and preview colours of one colour theme */
struct ThemeEntry
{
    QString id;           ///< path relative to the themes dir without suffix, e.g. "base16/monokai"
    QString description;
    QStringList categories;
    QColor background;
    QColor foreground;
    QColor keyword;
    QColor type;
    QColor string;
    QColor number;
    QColor comment;
};

/** List model of all installed colour themes */
class ThemeListModel : public QAbstractListModel
{
    Q_OBJECT

public:
    enum Roles {
        ThemeIdRole = Qt::UserRole + 1,
        CategoriesRole,
        SearchTextRole,
        BackgroundRole,
        ForegroundRole,
        KeywordRole,
        TypeRole,
        StringRole,
        NumberRole,
        CommentRole
    };

    explicit ThemeListModel(QObject *parent = nullptr);

    /** Read all *.theme files of dir and append them to the model
        \param dir theme directory
        \param idPrefix prefix of the theme ids (e.g. "base16/")
        \return list of error messages of themes which could not be read */
    QStringList addThemes(const QDir &dir, const QString &idPrefix = QString());

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

    /** \return row of theme with the given id, -1 if not found */
    int rowOfTheme(const QString &id) const;

private:
    QList<ThemeEntry> themes;
};

/** Filters themes by whitespace separated search terms, all of which must
    match the theme name, description or one of its categories */
class ThemeFilterProxyModel : public QSortFilterProxyModel
{
    Q_OBJECT

public:
    explicit ThemeFilterProxyModel(QObject *parent = nullptr);

public slots:
    void setSearchText(const QString &text);

protected:
    bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;

private:
    QStringList searchTerms;
};

#endif // THEMEMODEL_H