]> git.sur5r.net Git - minitube/blob - src/downloadmodel.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / downloadmodel.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "downloadmodel.h"
22 #include "downloadmanager.h"
23 #include "downloaditem.h"
24 #include "video.h"
25 #include "playlistmodel.h"
26
27 DownloadModel::DownloadModel(DownloadManager *downloadManager, QObject *parent) :
28         QAbstractListModel(parent),
29         downloadManager(downloadManager) {
30     hoveredRow = -1;
31     playIconHovered = false;
32     playIconPressed = false;
33 }
34
35 int DownloadModel::rowCount(const QModelIndex &/*parent*/) const {
36     return downloadManager->getItems().size();
37 }
38
39 QVariant DownloadModel::data(const QModelIndex &index, int role) const {
40
41     int row = index.row();
42     if (row < 0 || row >= rowCount()) return QVariant();
43
44     QVector<DownloadItem*> items = downloadManager->getItems();
45     if (items.isEmpty()) return QVariant();
46
47     switch (role) {
48     case ItemTypeRole:
49         return ItemTypeVideo;
50     case VideoRole:
51         return QVariant::fromValue(QPointer<Video>(items.at(row)->getVideo()));
52     case DownloadItemRole:
53         return QVariant::fromValue(QPointer<DownloadItem>(items.at(row)));
54     case ActiveTrackRole:
55         return false;
56     case HoveredItemRole:
57         return hoveredRow == index.row();
58     case DownloadButtonHoveredRole:
59         return playIconHovered;
60     case DownloadButtonPressedRole:
61         return playIconPressed;
62     }
63
64     return QVariant();
65 }
66
67 void DownloadModel::sendReset() {
68     beginResetModel();
69     endResetModel();
70 }
71
72 void DownloadModel::setHoveredRow(int row) {
73     int oldRow = hoveredRow;
74     hoveredRow = row;
75     emit dataChanged( createIndex( oldRow, 0 ), createIndex( oldRow, columnCount() - 1 ) );
76     emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, columnCount() - 1 ) );
77 }
78
79 void DownloadModel::clearHover() {
80     emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, columnCount() - 1 ) );
81     hoveredRow = -1;
82 }
83
84 void DownloadModel::enterPlayIconHover() {
85     if (playIconHovered) return;
86     playIconHovered = true;
87     updatePlayIcon();
88 }
89
90 void DownloadModel::exitPlayIconHover() {
91     if (!playIconHovered) return;
92     playIconHovered = false;
93     updatePlayIcon();
94     setHoveredRow(hoveredRow);
95 }
96
97 void DownloadModel::enterPlayIconPressed() {
98     if (playIconPressed) return;
99     playIconPressed = true;
100     updatePlayIcon();
101 }
102
103 void DownloadModel::exitPlayIconPressed() {
104     if (!playIconPressed) return;
105     playIconPressed = false;
106     updatePlayIcon();
107 }
108
109 void DownloadModel::updatePlayIcon() {
110     emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, columnCount() - 1 ) );
111 }