File indexing completed on 2024-05-05 04:59:13

0001 /***************************************************************************
0002  *   Copyright (C) 2008 by Joris Guisson and Ivan Vasic                    *
0003  *   joris.guisson@gmail.com                                               *
0004  *   ivasic@gmail.com                                                      *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, write to the                         *
0018  *   Free Software Foundation, Inc.,                                       *
0019  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0020  ***************************************************************************/
0021 #ifndef KTPEERVIEWMODEL_H
0022 #define KTPEERVIEWMODEL_H
0023 
0024 #include <QAbstractTableModel>
0025 #include <QIcon>
0026 #include <QList>
0027 
0028 #include <interfaces/peerinterface.h>
0029 
0030 namespace kt
0031 {
0032 
0033 /**
0034     @author Joris Guisson
0035     Model for the PeerView
0036 */
0037 class PeerViewModel : public QAbstractTableModel
0038 {
0039     Q_OBJECT
0040 public:
0041     PeerViewModel(QObject *parent);
0042     ~PeerViewModel() override;
0043 
0044     /// A peer has been added
0045     void peerAdded(bt::PeerInterface *peer);
0046 
0047     /// A peer has been removed
0048     void peerRemoved(bt::PeerInterface *peer);
0049 
0050     /**
0051      * Update the model
0052      */
0053     void update();
0054 
0055     void clear();
0056 
0057     int rowCount(const QModelIndex &parent) const override;
0058     int columnCount(const QModelIndex &parent) const override;
0059     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0060     QVariant data(const QModelIndex &index, int role) const override;
0061     bool removeRows(int row, int count, const QModelIndex &parent) override;
0062     bool insertRows(int row, int count, const QModelIndex &parent) override;
0063     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0064 
0065     bt::PeerInterface *indexToPeer(const QModelIndex &idx);
0066 
0067 public Q_SLOTS:
0068     void sort(int col, Qt::SortOrder order) override;
0069 
0070 public:
0071     struct Item {
0072         bt::PeerInterface *peer;
0073         mutable bt::PeerInterface::Stats stats;
0074         QString country;
0075         QIcon flag;
0076 
0077         Item(bt::PeerInterface *peer);
0078 
0079         bool changed(int col, bool &modified) const;
0080         QVariant data(int col) const;
0081         QVariant decoration(int col) const;
0082         bool lessThan(int col, const Item *other) const;
0083     };
0084 
0085 private:
0086     QList<Item *> items;
0087     int sort_column;
0088     Qt::SortOrder sort_order;
0089 };
0090 }
0091 
0092 #endif