3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "channelmodel.h"
24 static const int channelOffset = 2;
26 ChannelModel::ChannelModel(QObject *parent) :
27 QAbstractListModel(parent),
31 int ChannelModel::rowCount(const QModelIndex &) const {
32 return channels.isEmpty() ? 0 : channelOffset + channels.size();
35 QVariant ChannelModel::data(const QModelIndex &index, int role) const {
38 case ChannelModel::ItemTypeRole:
39 return typeForIndex(index);
41 case ChannelModel::DataObjectRole:
42 if (typeForIndex(index) == ChannelModel::ItemChannel)
43 return QVariant::fromValue(QPointer<YTUser>(userForIndex(index)));
46 case ChannelModel::HoveredItemRole:
47 return hoveredRow == index.row();
49 case Qt::StatusTipRole:
50 if (typeForIndex(index) == ChannelModel::ItemChannel)
51 return userForIndex(index)->getDescription();
58 YTUser* ChannelModel::userForIndex(const QModelIndex &index) const {
59 const int row = index.row();
60 if (row < channelOffset) return 0;
61 return channels.at(index.row() - channelOffset);
64 ChannelModel::ItemTypes ChannelModel::typeForIndex(const QModelIndex &index) const {
65 switch (index.row()) {
67 return ChannelModel::ItemAggregate;
69 return ChannelModel::ItemUnwatched;
71 return ChannelModel::ItemChannel;
75 void ChannelModel::setQuery(const QString &query, const QSqlDatabase &db) {
77 sqlError = QSqlError();
81 bool success = q.exec();
83 qWarning() << q.lastQuery() << q.lastError().text();
84 sqlError = q.lastError();
87 YTUser *user = YTUser::forId(q.value(0).toString());
88 connect(user, SIGNAL(thumbnailLoaded()), SLOT(updateSender()), Qt::UniqueConnection);
89 connect(user, SIGNAL(notifyCountChanged()), SLOT(updateSender()), Qt::UniqueConnection);
90 connect(user, SIGNAL(destroyed(QObject *)), SLOT(removeChannel(QObject *)), Qt::UniqueConnection);
97 QSqlError ChannelModel::lastError() const {
101 void ChannelModel::updateSender() {
102 YTUser *user = static_cast<YTUser*>(sender());
104 qWarning() << "Cannot get sender" << __PRETTY_FUNCTION__;
110 void ChannelModel::updateChannel(YTUser *user) {
111 int row = channels.indexOf(user);
112 if (row == -1) return;
113 row += channelOffset;
114 QModelIndex i = createIndex(row, 0);
115 emit dataChanged(i, i);
118 void ChannelModel::updateUnwatched() {
119 QModelIndex i = createIndex(1, 0);
120 emit dataChanged(i, i);
123 void ChannelModel::removeChannel(QObject *obj) {
124 YTUser *user = static_cast<YTUser*>(obj);
125 qWarning() << "user is" << user << obj << obj->metaObject()->className();
128 int row = channels.indexOf(user);
129 if (row == -1) return;
131 int position = row + channelOffset;
132 beginRemoveRows(QModelIndex(), position, position+1);
133 channels.removeAt(row);
137 void ChannelModel::setHoveredRow(int row) {
138 int oldRow = hoveredRow;
140 emit dataChanged( createIndex( oldRow, 0 ), createIndex( oldRow, 0 ) );
141 emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, 0 ) );
144 void ChannelModel::clearHover() {
145 emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, 0 ) );