File indexing completed on 2025-01-12 05:02:03

0001 #pragma once
0002 
0003 /*
0004  * SPDX-FileCopyrightText: 2003-2007 Craig Drummond <craig@kde.org>
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "FontList.h"
0009 #include <KIO/Job>
0010 #include <QAbstractItemModel>
0011 #include <QList>
0012 #include <QModelIndex>
0013 #include <QTreeView>
0014 #include <QVariant>
0015 
0016 class QDragEnterEvent;
0017 class QDragLeaveEvent;
0018 class QDropEvent;
0019 class QTextStream;
0020 class QDomElement;
0021 
0022 namespace KFI
0023 {
0024 class CGroupList;
0025 class CFontItem;
0026 
0027 class CGroupListItem
0028 {
0029 public:
0030     enum EType {
0031         ALL,
0032         PERSONAL,
0033         SYSTEM,
0034         UNCLASSIFIED,
0035         CUSTOM,
0036     };
0037 
0038     union Data {
0039         bool validated; // CUSTOM
0040         CGroupList *parent; // UNCLASSIFIED
0041     };
0042 
0043     CGroupListItem(const QString &name);
0044     CGroupListItem(EType type, CGroupList *p);
0045 
0046     const QString &name() const
0047     {
0048         return m_name;
0049     }
0050     void setName(const QString &n)
0051     {
0052         m_name = n;
0053     }
0054     QSet<QString> &families()
0055     {
0056         return m_families;
0057     }
0058     EType type() const
0059     {
0060         return m_type;
0061     }
0062     bool isCustom() const
0063     {
0064         return CUSTOM == m_type;
0065     }
0066     bool isAll() const
0067     {
0068         return ALL == m_type;
0069     }
0070     bool isUnclassified() const
0071     {
0072         return UNCLASSIFIED == m_type;
0073     }
0074     bool isPersonal() const
0075     {
0076         return PERSONAL == m_type;
0077     }
0078     bool isSystem() const
0079     {
0080         return SYSTEM == m_type;
0081     }
0082     bool validated() const
0083     {
0084         return isCustom() ? m_data.validated : true;
0085     }
0086     void setValidated()
0087     {
0088         if (isCustom())
0089             m_data.validated = true;
0090     }
0091     bool highlighted() const
0092     {
0093         return m_highlighted;
0094     }
0095     void setHighlighted(bool b)
0096     {
0097         m_highlighted = b;
0098     }
0099     bool hasFont(const CFontItem *fnt) const;
0100     CFamilyItem::EStatus status() const
0101     {
0102         return m_status;
0103     }
0104     void updateStatus(QSet<QString> &enabled, QSet<QString> &disabled, QSet<QString> &partial);
0105     bool load(QDomElement &elem);
0106     bool addFamilies(QDomElement &elem);
0107     void save(QTextStream &str);
0108     void addFamily(const QString &family)
0109     {
0110         m_families.insert(family);
0111     }
0112     void removeFamily(const QString &family)
0113     {
0114         m_families.remove(family);
0115     }
0116     bool hasFamily(const QString &family)
0117     {
0118         return m_families.contains(family);
0119     }
0120 
0121 private:
0122     QSet<QString> m_families;
0123     QString m_name;
0124     EType m_type;
0125     Data m_data;
0126     bool m_highlighted;
0127     CFamilyItem::EStatus m_status;
0128 };
0129 
0130 class CGroupList : public QAbstractItemModel
0131 {
0132     Q_OBJECT
0133 
0134 public:
0135     CGroupList(QWidget *parent = nullptr);
0136     ~CGroupList() override;
0137 
0138     QVariant data(const QModelIndex &index, int role) const override;
0139     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
0140     Qt::ItemFlags flags(const QModelIndex &index) const override;
0141     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0142     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0143     QModelIndex parent(const QModelIndex &index) const override;
0144     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0145     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0146     void update(const QModelIndex &unHighlight, const QModelIndex &highlight);
0147     void updateStatus(QSet<QString> &enabled, QSet<QString> &disabled, QSet<QString> &partial);
0148     void setSysMode(bool sys);
0149     void rescan();
0150     void load();
0151     bool load(const QString &file);
0152     bool save();
0153     bool save(const QString &fileName, CGroupListItem *grp);
0154     void merge(const QString &file);
0155     void clear();
0156     QModelIndex index(CGroupListItem::EType t);
0157     void createGroup(const QString &name);
0158     bool removeGroup(const QModelIndex &idx);
0159     void removeFamily(const QString &family);
0160     bool removeFromGroup(CGroupListItem *grp, const QString &family);
0161     QString whatsThis() const;
0162 
0163     CGroupListItem *group(CGroupListItem::EType t)
0164     {
0165         return m_specialGroups[t];
0166     }
0167     bool exists(const QString &name, bool showDialog = true);
0168 
0169 public Q_SLOTS:
0170 
0171     void addToGroup(const QModelIndex &group, const QSet<QString> &families);
0172     void removeFromGroup(const QModelIndex &group, const QSet<QString> &families);
0173 
0174 Q_SIGNALS:
0175 
0176     void refresh();
0177 
0178 private:
0179     void readGroupsFile();
0180     void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
0181     Qt::DropActions supportedDropActions() const override;
0182     QStringList mimeTypes() const override;
0183     CGroupListItem *find(const QString &name);
0184     QModelIndex createIdx(int r, int c, void *p)
0185     {
0186         return createIndex(r, c, p);
0187     }
0188 
0189 private:
0190     QString m_fileName;
0191     time_t m_timeStamp;
0192     bool m_modified;
0193     QWidget *m_parent;
0194     QList<CGroupListItem *> m_groups;
0195     QMap<CGroupListItem::EType, CGroupListItem *> m_specialGroups;
0196     Qt::SortOrder m_sortOrder;
0197 
0198     friend class CGroupListItem;
0199     friend class CGroupListView;
0200 };
0201 
0202 class CGroupListView : public QTreeView
0203 {
0204     Q_OBJECT
0205 
0206 public:
0207     CGroupListView(QWidget *parent, CGroupList *model);
0208     ~CGroupListView() override
0209     {
0210     }
0211 
0212     QSize sizeHint() const override
0213     {
0214         return QSize(32, 32);
0215     }
0216 
0217     bool isCustom()
0218     {
0219         return CGroupListItem::CUSTOM == getType();
0220     }
0221     bool isUnclassified()
0222     {
0223         return CGroupListItem::UNCLASSIFIED == getType();
0224     }
0225     bool isSystem()
0226     {
0227         return CGroupListItem::SYSTEM == getType();
0228     }
0229     bool isPersonal()
0230     {
0231         return CGroupListItem::PERSONAL == getType();
0232     }
0233     CGroupListItem::EType getType();
0234     void controlMenu(bool del, bool en, bool dis, bool p, bool exp);
0235 
0236 Q_SIGNALS:
0237 
0238     void del();
0239     void print();
0240     void enable();
0241     void disable();
0242     void zip();
0243     void moveFonts();
0244     void info(const QString &str);
0245     void addFamilies(const QModelIndex &group, const QSet<QString> &);
0246     void removeFamilies(const QModelIndex &group, const QSet<QString> &);
0247     void itemSelected(const QModelIndex &);
0248     void unclassifiedChanged();
0249 
0250 private Q_SLOTS:
0251 
0252     void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) override;
0253     void rename();
0254     void emitMoveFonts();
0255 
0256 private:
0257     void contextMenuEvent(QContextMenuEvent *ev) override;
0258     void dragEnterEvent(QDragEnterEvent *event) override;
0259     void dragMoveEvent(QDragMoveEvent *event) override;
0260     void dragLeaveEvent(QDragLeaveEvent *event) override;
0261     void dropEvent(QDropEvent *event) override;
0262     void drawHighlighter(const QModelIndex &idx);
0263     bool viewportEvent(QEvent *event) override;
0264 
0265 private:
0266     QMenu *m_menu;
0267     QAction *m_deleteAct, *m_enableAct, *m_disableAct, *m_printAct, *m_renameAct, *m_exportAct;
0268     QModelIndex m_currentDropItem;
0269 };
0270 }