- Minor edit
[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         /*
134         qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
135         qDebug() << "current item" << currentItem();
136         
137         if (item == currentItem() && item->isSelected()) {
138                 item->setSelected(false);
139         }
140         */
141 }
142
143
144 QString DownloadView::GetStatusString(QTorrentHandle::State const& status) const
145 {
146         switch (status) {
147                 case QTorrentHandle::QUEUED_FOR_CHECKING :
148                         return tr("Queued");
149                 case QTorrentHandle::CHECKING_FILES :
150                         return tr("Checking");
151                 case QTorrentHandle::DOWNLOADING_METADATA :
152                         return tr("DL meta");
153                 case QTorrentHandle::DOWNLOADING :
154                         return tr("Downloading");
155                 case QTorrentHandle::FINISHED :
156                         return tr("Finished");
157                 case QTorrentHandle::SEEDING :
158                         return tr("Seeding"); 
159                 case QTorrentHandle::ALLOCATING :
160                         return tr("Allocating");
161                 default:
162                         return tr("N/A");
163         }
164 }
165
166
167 QColor DownloadView::GetStatusColor(QTorrentHandle::State const& status) const
168 {
169         QColor green(40,205,40);
170         QColor yellow(255,174,0);
171
172         switch (status) {
173                 case QTorrentHandle::QUEUED_FOR_CHECKING :
174                 case QTorrentHandle::CHECKING_FILES :
175                 case QTorrentHandle::DOWNLOADING_METADATA :
176                 case QTorrentHandle::ALLOCATING :
177                         return yellow;
178                 case QTorrentHandle::DOWNLOADING :
179                 case QTorrentHandle::FINISHED :
180                 case QTorrentHandle::SEEDING :
181                                 return green;
182                 default:
183                         return QColor();
184         }
185 }