Communi  3.7.0
A cross-platform IRC framework written with Qt
ircusermodel.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008-2020 The Communi Project
3 
4  You may use this file under the terms of BSD license as follows:
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the copyright holder nor the names of its
14  contributors may be used to endorse or promote products derived
15  from this software without specific prior written permission.
16 
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
21  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef IRCUSERMODEL_H
30 #define IRCUSERMODEL_H
31 
32 #include <Irc>
33 #include <IrcGlobal>
34 #include <QtCore/qmetatype.h>
35 #include <QtCore/qstringlist.h>
36 #include <QtCore/qabstractitemmodel.h>
37 
38 IRC_BEGIN_NAMESPACE
39 
40 class IrcUser;
41 class IrcChannel;
42 class IrcMessage;
43 class IrcUserModelPrivate;
44 
45 class IRC_MODEL_EXPORT IrcUserModel : public QAbstractListModel
46 {
47  Q_OBJECT
48  Q_PROPERTY(int count READ count NOTIFY countChanged)
49  Q_PROPERTY(bool empty READ isEmpty NOTIFY emptyChanged)
50  Q_PROPERTY(QStringList names READ names NOTIFY namesChanged)
51  Q_PROPERTY(QStringList titles READ titles NOTIFY titlesChanged)
52  Q_PROPERTY(QList<IrcUser*> users READ users NOTIFY usersChanged)
53  Q_PROPERTY(Irc::DataRole displayRole READ displayRole WRITE setDisplayRole)
54  Q_PROPERTY(IrcChannel* channel READ channel WRITE setChannel NOTIFY channelChanged)
55  Q_PROPERTY(Irc::SortMethod sortMethod READ sortMethod WRITE setSortMethod)
56  Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder)
57 
58 public:
59  explicit IrcUserModel(QObject* parent = nullptr);
60  ~IrcUserModel() override;
61 
62  IrcChannel* channel() const;
63  void setChannel(IrcChannel* channel);
64 
65  int count() const;
66  bool isEmpty() const;
67  QStringList names() const;
68  QStringList titles() const;
69  QList<IrcUser*> users() const;
70  Q_INVOKABLE IrcUser* get(int index) const;
71  Q_INVOKABLE IrcUser* find(const QString& name) const;
72  Q_INVOKABLE bool contains(const QString& name) const;
73  Q_INVOKABLE int indexOf(IrcUser* user) const;
74 
75  Irc::DataRole displayRole() const;
76  void setDisplayRole(Irc::DataRole role);
77 
78  Irc::SortMethod sortMethod() const;
79  void setSortMethod(Irc::SortMethod method);
80 
81  Qt::SortOrder sortOrder() const;
82  void setSortOrder(Qt::SortOrder order);
83 
84  QModelIndex index(IrcUser* user) const;
85  IrcUser* user(const QModelIndex& index) const;
86 
87 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
88  QHash<int, QByteArray> roleNames() const override;
89 #endif
90  int rowCount(const QModelIndex& parent = QModelIndex()) const override;
91  QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
92  QModelIndex index(int row, int column = 0, const QModelIndex& parent = QModelIndex()) const override;
93 
94 public Q_SLOTS:
95  void clear();
96  void sort(int column = 0, Qt::SortOrder order = Qt::AscendingOrder) override;
97  void sort(Irc::SortMethod method, Qt::SortOrder order = Qt::AscendingOrder);
98 
99 Q_SIGNALS:
100  void added(IrcUser* user);
101  void removed(IrcUser* user);
102  void aboutToBeAdded(IrcUser* user);
104  void countChanged(int count);
105  void emptyChanged(bool empty);
106  void namesChanged(const QStringList& names);
107  void titlesChanged(const QStringList& titles);
108  void usersChanged(const QList<IrcUser*>& users);
109  void channelChanged(IrcChannel* channel);
110 
111 protected:
112  virtual bool lessThan(IrcUser* one, IrcUser* another, Irc::SortMethod method) const;
113 
114 private:
115  friend class IrcUserLessThan;
116  friend class IrcChannelPrivate;
117  friend class IrcUserGreaterThan;
118  QScopedPointer<IrcUserModelPrivate> d_ptr;
119  Q_DECLARE_PRIVATE(IrcUserModel)
120  Q_DISABLE_COPY(IrcUserModel)
121 };
122 
123 IRC_END_NAMESPACE
124 
125 Q_DECLARE_METATYPE(IRC_PREPEND_NAMESPACE(IrcUserModel*))
126 
127 #endif // IRCUSERMODEL_H
Keeps track of channel status.
Definition: ircchannel.h:41
The base class of all messages.
Definition: ircmessage.h:48
Keeps track of channel users.
Definition: ircusermodel.h:46
void added(IrcUser *user)
void aboutToBeRemoved(IrcUser *user)
void aboutToBeAdded(IrcUser *user)
void removed(IrcUser *user)
Keeps track of user status on a channel.
Definition: ircuser.h:43
SortMethod
Definition: irc.h:88
DataRole
Definition: irc.h:78