File indexing completed on 2024-10-27 04:34:00

0001 /***************************************************************************
0002  *   Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net>                     *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #ifndef MIRRORMODEL_H
0021 #define MIRRORMODEL_H
0022 
0023 #include <QSortFilterProxyModel>
0024 #include <QStyledItemDelegate>
0025 
0026 #include <QIcon>
0027 #include <QUrl>
0028 
0029 class QSortFilterProxyModel;
0030 
0031 class MirrorDelegate : public QStyledItemDelegate
0032 {
0033     Q_OBJECT
0034 
0035 public:
0036     MirrorDelegate(QObject *parent = nullptr);
0037     explicit MirrorDelegate(QSortFilterProxyModel *countrySort, QObject *parent = nullptr);
0038 
0039     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0040     void setEditorData(QWidget *editor, const QModelIndex &index) const override;
0041     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
0042     void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0043     QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0044 
0045 private:
0046     QSortFilterProxyModel *m_countrySort = nullptr;
0047 };
0048 
0049 class MirrorProxyModel : public QSortFilterProxyModel
0050 {
0051     Q_OBJECT
0052 
0053 public:
0054     MirrorProxyModel(QObject *parent = nullptr);
0055 
0056 protected:
0057     bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
0058 };
0059 
0060 class MirrorItem
0061 {
0062 public:
0063     MirrorItem();
0064 
0065     enum DataType { Used = 0, Url, Connections, Priority, Country };
0066 
0067     QVariant data(int column, int role = Qt::DisplayRole) const;
0068     Qt::ItemFlags flags(int column) const;
0069     bool setData(int column, const QVariant &value, int role = Qt::EditRole);
0070 
0071 private:
0072     QUrl m_url;
0073     Qt::CheckState m_checked;
0074     int m_numConnections;
0075     int m_priority;
0076     QString m_countryCode;
0077     QString m_countryName;
0078 };
0079 
0080 class MirrorModel : public QAbstractTableModel
0081 {
0082     Q_OBJECT
0083 
0084 public:
0085     MirrorModel(QObject *parent);
0086     ~MirrorModel() override;
0087 
0088     int rowCount(const QModelIndex &index = QModelIndex()) const override;
0089     int columnCount(const QModelIndex &index = QModelIndex()) const override;
0090     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0091     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0092     Qt::ItemFlags flags(const QModelIndex &index) const override;
0093     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0094     bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
0095 
0096     void addMirror(const QUrl &url, int numConnections = 0, int priority = 0, const QString &countryCode = QString());
0097     void setMirrors(const QHash<QUrl, QPair<bool, int>> &mirrors);
0098     QHash<QUrl, QPair<bool, int>> availableMirrors() const;
0099 
0100 private:
0101     QList<MirrorItem *> m_data;
0102 };
0103 
0104 #endif