- Search plugin interface changed: enum constants to define widget types to allow...
[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 <QTimer>
22 #include <QVariant>
23 #include <QColor>
24 #include "DownloadView.h"
25
26
27 DownloadView::DownloadView(QWidget* parent) :
28                 QTreeWidget(parent),
29                 items_(),
30                 timer_(NULL)
31 {
32         setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
33         setHeaderItem(DownloadViewItem::getHeaderItem());
34
35         connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
36                 this, SLOT(on_itemClicked(QTreeWidgetItem*, int)));
37
38         timer_ = new QTimer(this);
39         connect(timer_, SIGNAL(timeout()), this, SLOT(on_timeout()));
40         timer_->start(5000);
41
42 }
43
44
45 DownloadView::~DownloadView()
46 {
47 }
48
49
50 void DownloadView::newItem(qtrapids::QTorrentHandle handle)
51 {
52 #ifdef QTRAPIDS_DEBUG
53         qDebug() << "DownloadView::newItem() " << items_.count(handle);
54 #endif
55
56         DownloadViewItem *item = new DownloadViewItem(QStringList()
57                 << handle.name()
58                 << QString::number(handle.getTotalSize())
59                 << GetStatusString(handle.state())
60                 << QString::number(100*handle.progress() + '%')
61                 << QString::number(handle.downloadRate(), 'f', 2)
62                 << QString::number(handle.uploadRate(),  'f', 2)
63                 << QString::number(handle.numSeeds()) + "/"
64                 + QString::number(handle.numLeeches())
65                 << QString::number(handle.ratio())
66                 << "ETA" );
67
68         // Set text color for status:
69         QBrush brushTmp(GetStatusColor(handle.state()));
70         item->setForeground(2, brushTmp);
71
72         addTopLevelItem(item);
73         items_[handle] = item;
74 }
75
76
77 void DownloadView::updateItem(qtrapids::QTorrentHandle handle)
78 {
79         //qDebug() << "DownloadView::updateItem() "  << items_.count(handle);
80
81         static float lastProg = 0;
82
83         // If there are items currently downloading, update:
84         if (items_.count(handle) > 0) {
85
86                 DownloadViewItem *item = items_[handle];
87
88                 // Only the changing fields are being updated:
89                 item->setData(2, Qt::DisplayRole,
90                               QVariant(GetStatusString(handle.state())));
91                 item->setData(4, Qt::DisplayRole,
92                               QVariant(QString::number(handle.downloadRate(), 'f', 2)));
93                 item->setData(5, Qt::DisplayRole,
94                               QVariant(QString::number(handle.uploadRate(), 'f', 2)));
95                 item->setData(6, Qt::DisplayRole,
96                               QString::number(handle.numSeeds()) + "/"
97                               + QString::number(handle.numLeeches()));
98
99                 // Set progress if increment is 1 percent.
100                 float prog = handle.progress();
101                 if ((prog-lastProg) >= 0.01 || prog >= 1.0) {
102                         item->setData(3, Qt::DisplayRole,
103                                       QVariant(QString::number(100*prog) + '%'));
104                         lastProg = prog;
105                 }
106
107                 /// @TODO: ETA-counter adjusting,if ETA is to be implemented
108
109                 // Adjust color
110                 QBrush brushTmp(GetStatusColor(handle.state()));
111                 item->setForeground(2, brushTmp);
112         }
113
114 }
115
116
117 qtrapids::QTorrentHandle DownloadView::removeSelected()
118 {
119 #ifdef QTRAPIDS_DEBUG
120         qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
121 #endif
122
123         DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
124
125         std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listIter
126         = items_.begin();
127         std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listEnd
128         = items_.end();
129
130         while (listIter != listEnd) {
131                 if (listIter->second == item) {
132                         break;
133                 }
134                 ++listIter;
135         }
136
137         qtrapids::QTorrentHandle handle = listIter->first;
138         items_.erase(listIter);
139
140
141         int index = indexOfTopLevelItem(currentItem());
142         if (index >= 0) {
143                 takeTopLevelItem(index);
144         }
145
146 #ifdef QTRAPIDS_DEBUG
147         qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
148 #endif
149
150         return handle;
151 }
152
153
154 void DownloadView::removeItem(qtrapids::QTorrentHandle handle)
155 {
156 }
157
158
159 void DownloadView::setRefreshInterval(int msec)
160 {
161         timer_->setInterval(msec);
162 }
163
164
165 void DownloadView::on_itemClicked(QTreeWidgetItem * item, int column)
166 {
167         /*
168         qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
169         qDebug() << "current item" << currentItem();
170
171         if (item == currentItem() && item->isSelected()) {
172                 item->setSelected(false);
173         }
174         */
175 }
176
177 void DownloadView::on_timeout()
178 {
179 #ifdef QTRAPIDS_DEBUG
180         qDebug() << "DownloadView::on_timeout()";
181 #endif
182         UpdateView();
183 }
184
185
186
187 QString DownloadView::GetStatusString(qtrapids::QTorrentHandle::State const& status) const
188 {
189         switch (status) {
190         case qtrapids::QTorrentHandle::QUEUED_FOR_CHECKING :
191                 return tr("Queued");
192         case qtrapids::QTorrentHandle::CHECKING_FILES :
193                 return tr("Checking");
194         case qtrapids::QTorrentHandle::DOWNLOADING_METADATA :
195                 return tr("DL meta");
196         case qtrapids::QTorrentHandle::DOWNLOADING :
197                 return tr("Downloading");
198         case qtrapids::QTorrentHandle::FINISHED :
199                 return tr("Finished");
200         case qtrapids::QTorrentHandle::SEEDING :
201                 return tr("Seeding");
202         case qtrapids::QTorrentHandle::ALLOCATING :
203                 return tr("Allocating");
204         default:
205                 return tr("N/A");
206         }
207 }
208
209
210 QColor DownloadView::GetStatusColor(qtrapids::QTorrentHandle::State const& status) const
211 {
212         QColor green(40,205,40);
213         QColor yellow(255,174,0);
214
215         switch (status) {
216         case qtrapids::QTorrentHandle::QUEUED_FOR_CHECKING :
217         case qtrapids::QTorrentHandle::CHECKING_FILES :
218         case qtrapids::QTorrentHandle::DOWNLOADING_METADATA :
219         case qtrapids::QTorrentHandle::ALLOCATING :
220                 return yellow;
221         case qtrapids::QTorrentHandle::DOWNLOADING :
222         case qtrapids::QTorrentHandle::FINISHED :
223         case qtrapids::QTorrentHandle::SEEDING :
224                 return green;
225         default:
226                 return QColor();
227         }
228 }
229
230 void DownloadView::UpdateView()
231 {
232         DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
233
234         std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listIter
235         = items_.begin();
236         std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listEnd
237         = items_.end();
238
239         while (listIter != listEnd) {
240                 updateItem(listIter->first);
241                 ++listIter;
242         }
243 }