d7424b01d5c2613015494970a8afeae0d3fdb775
[qtrapids] / src / gui / DownloadView.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 by Lassi Väätämöinen   *
3  *   lassi.vaatamoinen@ixonos.com   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #include <QDebug>
21 #include <QVariant>
22 #include <QColor>
23 #include "DownloadView.h"
24
25
26 DownloadView::DownloadView(QWidget* parent) :
27         QTreeWidget(parent),
28         items_()
29 {
30     setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
31     setHeaderItem(DownloadViewItem::getHeaderItem());
32
33     connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
34             this, SLOT(on_itemClicked(QTreeWidgetItem*, int)));
35
36 }
37
38
39 DownloadView::~DownloadView()
40 {
41 }
42
43
44 void DownloadView::newItem(QTorrentHandle handle)
45 {
46         qDebug() << "DownloadView::newItem() " << items_.count(handle);
47
48         DownloadViewItem *item = new DownloadViewItem(QStringList()
49                         << handle.name()
50                         << QString::number(handle.getTotalSize())
51                         << GetStatusString(handle.state())
52                         << QString::number(handle.progress())
53                         << QString::number(handle.downloadRate()) 
54                         << QString::number(handle.uploadRate()) 
55                         << QString::number(handle.numSeeds()) + "/"
56                                         + QString::number(handle.numLeeches())
57                         << QString::number(handle.ratio())
58                         << "ETA" );
59         
60         QBrush brushTmp(GetStatusColor(handle.state()));
61         item->setForeground(2, brushTmp);
62                         
63         addTopLevelItem(item);
64         items_[handle] = item;
65 }
66
67
68 void DownloadView::updateItem(QTorrentHandle handle)
69 {
70         qDebug() << "DownloadView::updateItem() "  << items_.count(handle);
71         
72         if (items_.count(handle) > 0) {
73                 DownloadViewItem *item = items_[handle];
74                 item->setData(2, Qt::DisplayRole,
75                                                                         QVariant(GetStatusString(handle.state())));
76                 item->setData(3, Qt::DisplayRole,
77                                                                         QVariant(QString::number(handle.progress())));
78                 item->setData(4, Qt::DisplayRole,
79                                                                         QVariant(QString::number(handle.downloadRate())));
80                 item->setData(5, Qt::DisplayRole,
81                                                                         QVariant(QString::number(handle.uploadRate())));
82                 item->setData(6, Qt::DisplayRole, 
83                                                                         QString::number(handle.numSeeds()) + "/" 
84                                                                                 + QString::number(handle.numLeeches()));
85                                                                                 
86                 QBrush brushTmp(GetStatusColor(handle.state()));
87                 item->setForeground(2, brushTmp);
88         }
89
90 }
91
92
93 QTorrentHandle DownloadView::removeSelected()
94 {
95         qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
96
97         DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
98         
99         std::map<QTorrentHandle, DownloadViewItem*>::iterator listIter
100                         = items_.begin();
101         std::map<QTorrentHandle, DownloadViewItem*>::iterator listEnd
102                         = items_.end();
103         
104         while (listIter != listEnd) {
105                 if (listIter->second == item) {
106                         break;
107                 }
108                 ++listIter;
109         }
110         
111         QTorrentHandle handle = listIter->first;
112         items_.erase(listIter);
113
114         
115         int index = indexOfTopLevelItem(currentItem());
116         if (index >= 0) {
117                 takeTopLevelItem(index);
118         }
119         
120         qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
121         
122         return handle;
123 }
124
125
126 void DownloadView::removeItem(QTorrentHandle handle)
127 {
128 }
129
130
131 void DownloadView::on_itemClicked(QTreeWidgetItem * item, int column)
132 {/*
133         qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
134         qDebug() << "current item" << currentItem();
135         
136         if (item == currentItem() && item->isSelected()) {
137                 item->setSelected(false);
138         }
139         */
140 }
141
142
143 QString DownloadView::GetStatusString(QTorrentHandle::State const& status) const
144 {
145         switch (status) {
146                 case QTorrentHandle::QUEUED_FOR_CHECKING :
147                         return "Queued";
148                 case QTorrentHandle::CHECKING_FILES :
149                         return "Checking";
150                 case QTorrentHandle::DOWNLOADING_METADATA :
151                         return "DL meta";
152                 case QTorrentHandle::DOWNLOADING :
153                         return "Downloading";
154                 case QTorrentHandle::FINISHED :
155                         return "Finished";
156                 case QTorrentHandle::SEEDING :
157                         return "Seeding"; 
158                 case QTorrentHandle::ALLOCATING :
159                         return "Allocating";
160                 default:
161                         return "N/A";
162         }
163 }
164
165
166 QColor DownloadView::GetStatusColor(QTorrentHandle::State const& status) const
167 {
168         QColor green(40,205,40);
169         QColor yellow(255,174,0);
170
171         switch (status) {
172                 case QTorrentHandle::QUEUED_FOR_CHECKING :
173                 case QTorrentHandle::CHECKING_FILES :
174                 case QTorrentHandle::DOWNLOADING_METADATA :
175                 case QTorrentHandle::ALLOCATING :
176                         return yellow;
177                 case QTorrentHandle::DOWNLOADING :
178                 case QTorrentHandle::FINISHED :
179                 case QTorrentHandle::SEEDING :
180                                 return green;
181                 default:
182                         return QColor();
183         }
184                 
185 }