File indexing completed on 2024-05-12 11:54:36

0001 /*
0002     SPDX-FileCopyrightText: 2009 Nick Shaforostoff <shaforostoff@kde.ru>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef IKWSOPTS_P_H
0008 #define IKWSOPTS_P_H
0009 
0010 #include <QAbstractTableModel>
0011 #include <QSet>
0012 
0013 class SearchProvider;
0014 
0015 class ProvidersModel : public QAbstractTableModel
0016 {
0017     Q_OBJECT
0018 public:
0019     enum { Name, Shortcuts, Preferred, ColumnCount };
0020     explicit ProvidersModel(QObject *parent = nullptr)
0021         : QAbstractTableModel(parent)
0022     {
0023     }
0024 
0025     ~ProvidersModel() override;
0026 
0027     Qt::ItemFlags flags(const QModelIndex &index) const override;
0028     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0029     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0030     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0031     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0032     int columnCount(const QModelIndex &parent = QModelIndex()) const override
0033     {
0034         Q_UNUSED(parent);
0035         return ColumnCount;
0036     }
0037 
0038     void setProviders(const QList<SearchProvider *> &, const QStringList &);
0039     void setFavoriteProviders(const QStringList &);
0040     void addProvider(SearchProvider *p);
0041     void deleteProvider(SearchProvider *p);
0042     void changeProvider(SearchProvider *p);
0043     QStringList favoriteEngines() const;
0044     QList<SearchProvider *> providers() const
0045     {
0046         return m_providers;
0047     }
0048 
0049     /// Creates new ProvidersListModel which directly uses data of this model.
0050     QAbstractListModel *createListModel();
0051 
0052 Q_SIGNALS:
0053     void dataModified();
0054 
0055 private:
0056     QSet<QString> m_favoriteEngines;
0057     QList<SearchProvider *> m_providers;
0058 };
0059 
0060 /**
0061  * A model for kcombobox of default search engine.
0062  * It is created via ProvidersModel::createListModel() and uses createListModel's data directly,
0063  * just forwarding all the signals
0064  */
0065 class ProvidersListModel : public QAbstractListModel
0066 {
0067     Q_OBJECT
0068 public:
0069     enum { ShortNameRole = Qt::UserRole };
0070 
0071 private:
0072     explicit ProvidersListModel(QList<SearchProvider *> &providers, QObject *parent = nullptr);
0073 
0074 public:
0075     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0076     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0077 
0078 public Q_SLOTS:
0079     void emitDataChanged(const QModelIndex &start, const QModelIndex &end)
0080     {
0081         Q_EMIT dataChanged(index(start.row(), 0), index(end.row(), 0));
0082     }
0083 
0084     void emitRowsAboutToBeInserted(const QModelIndex &, int start, int end)
0085     {
0086         beginInsertRows(QModelIndex(), start, end);
0087     }
0088 
0089     void emitRowsAboutToBeRemoved(const QModelIndex &, int start, int end)
0090     {
0091         beginRemoveRows(QModelIndex(), start, end);
0092     }
0093 
0094     void emitRowsInserted(const QModelIndex &, int, int)
0095     {
0096         endInsertRows();
0097     }
0098 
0099     void emitRowsRemoved(const QModelIndex &, int, int)
0100     {
0101         endRemoveRows();
0102     }
0103 
0104 private:
0105     QList<SearchProvider *> &m_providers;
0106 
0107     friend class ProvidersModel;
0108 };
0109 
0110 #endif