-#include <QDebug>
-
-NickModel::NickModel(IrcChannel *channel, QObject *parent) : QAbstractItemModel(parent) {
- // we support 6 categories: q, a, o, h, v and standard
- users = QVector<QList<IrcUser *> >(6);
-
- if(channel) setIrcChannel(channel);
- else _ircChannel = 0;
-}
-
-NickModel::~NickModel() {
-
-
-}
-
-IrcChannel *NickModel::ircChannel() const {
- return _ircChannel;
-}
-
-void NickModel::setIrcChannel(IrcChannel *channel) {
- if(_ircChannel) {
- disconnect(_ircChannel, 0, this, 0);
- }
- foreach(QList<IrcUser *> l, users) l.clear();
- _ircChannel = channel;
- reset();
- if(_ircChannel) {
- connect(channel, SIGNAL(ircUserJoined(IrcUser *)), this, SLOT(addUser(IrcUser *)));
- connect(channel, SIGNAL(ircUserParted(IrcUser *)), this, SLOT(removeUser(IrcUser *)));
- connect(channel, SIGNAL(ircUserNickSet(IrcUser *, QString)), this, SLOT(renameUser(IrcUser *)));
- connect(channel, SIGNAL(ircUserModesSet(IrcUser *, QString)), this, SLOT(changeUserModes(IrcUser *)));
-
- foreach(IrcUser *ircuser, channel->ircUsers()) {
- // TODO: make this efficient by sorting after everything is appended instead!
- addUser(ircuser);
- }
- }
-
-}
-
-QVariant NickModel::headerData(int section, Qt::Orientation orientation, int role) const {
- if(section == 0 && role == Qt::DisplayRole) {
- if(ircChannel()) return ircChannel()->name();
- else return "No channel";
- }
- return QAbstractItemModel::headerData(section, orientation, role);
-}
-
-QModelIndex NickModel::index(int row, int column, const QModelIndex &parent) const {
- if(!parent.isValid()) { // Top-level item, i.e. a nick category
- if(column > 0) return QModelIndex();
- //int r = 0;
- //for(int i = 0; i < row; i++) { // we need to skip empty categories
- if(row > users.count()) {
- qDebug() << "invalid model index!";
- return QModelIndex();
- }
- return createIndex(row, column, 0);
- }
- // Second-level item, i.e. a nick. internalId() contains the parent category (starting at 1).
- int cat = parent.row() + 1;
- if(row > users[cat-1].count()) {
- qDebug() << "invalid model index!";
- return QModelIndex();
- }
- return createIndex(row, column, cat);
-}
-
-QModelIndex NickModel::indexOfUser(IrcUser *user) const {
- int idx = -1; int cat;
- for(cat = users.count()-1; cat >= 0; cat--) {
- // we count backwards, since most users will usually be in the last category
- idx = users[cat].indexOf(user);
- if(idx >=0) break;
- }
- if(idx < 0) {
- qWarning("NickModel: Index of unknown user requested!");
- return QModelIndex();
- }
- return createIndex(idx, 0, cat+1);
-}
-
-QModelIndex NickModel::parent(const QModelIndex &index) const {
- if(!index.isValid()) return QModelIndex();
- int cat = index.internalId();
- if(cat) return createIndex(cat-1, 0, 0);
- else return QModelIndex();
-}
-
-int NickModel::rowCount(const QModelIndex &parent) const {
- if(!parent.isValid()) {
- if(!ircChannel()) return 1; // informative text
- return users.count();
- }
- int cat = parent.internalId();
- if(!cat) { // top-level item (category)
- return users[parent.row()].count();
- }
- return 0; // second-level items don't have children