1 /***************************************************************************
2 * Copyright (C) 2009 by Lassi Väätämöinen *
3 * lassi.vaatamoinen@ixonos.com *
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. *
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. *
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 ***************************************************************************/
24 #include "DownloadView.h"
27 DownloadView::DownloadView(QWidget* parent) :
32 setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
33 setHeaderItem(DownloadViewItem::getHeaderItem());
35 connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
36 this, SLOT(on_itemClicked(QTreeWidgetItem*, int)));
38 timer_ = new QTimer(this);
39 connect(timer_, SIGNAL(timeout()), this, SLOT(on_timeout()));
45 DownloadView::~DownloadView()
50 void DownloadView::newItem(qtrapids::QTorrentHandle handle)
53 qDebug() << "DownloadView::newItem() " << items_.count(handle);
56 DownloadViewItem *item = new DownloadViewItem(QStringList()
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())
68 // Set text color for status:
69 QBrush brushTmp(GetStatusColor(handle.state()));
70 item->setForeground(2, brushTmp);
72 addTopLevelItem(item);
73 items_[handle] = item;
77 void DownloadView::updateItem(qtrapids::QTorrentHandle handle)
79 //qDebug() << "DownloadView::updateItem() " << items_.count(handle);
81 static float lastProg = 0;
83 // If there are items currently downloading, update:
84 if (items_.count(handle) > 0) {
86 DownloadViewItem *item = items_[handle];
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()));
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) + '%'));
107 /// @TODO: ETA-counter adjusting,if ETA is to be implemented
110 QBrush brushTmp(GetStatusColor(handle.state()));
111 item->setForeground(2, brushTmp);
117 qtrapids::QTorrentHandle DownloadView::removeSelected()
119 #ifdef QTRAPIDS_DEBUG
120 qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ;
123 DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
125 std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listIter
127 std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listEnd
130 while (listIter != listEnd) {
131 if (listIter->second == item) {
137 qtrapids::QTorrentHandle handle = listIter->first;
138 items_.erase(listIter);
141 int index = indexOfTopLevelItem(currentItem());
143 takeTopLevelItem(index);
146 #ifdef QTRAPIDS_DEBUG
147 qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ;
154 void DownloadView::removeItem(qtrapids::QTorrentHandle handle)
159 void DownloadView::setRefreshInterval(int msec)
161 timer_->setInterval(msec);
165 void DownloadView::on_itemClicked(QTreeWidgetItem * item, int column)
168 qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
169 qDebug() << "current item" << currentItem();
171 if (item == currentItem() && item->isSelected()) {
172 item->setSelected(false);
177 void DownloadView::on_timeout()
179 #ifdef QTRAPIDS_DEBUG
180 qDebug() << "DownloadView::on_timeout()";
187 QString DownloadView::GetStatusString(qtrapids::QTorrentHandle::State const& status) const
190 case qtrapids::QTorrentHandle::QUEUED_FOR_CHECKING :
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");
210 QColor DownloadView::GetStatusColor(qtrapids::QTorrentHandle::State const& status) const
212 QColor green(40,205,40);
213 QColor yellow(255,174,0);
216 case qtrapids::QTorrentHandle::QUEUED_FOR_CHECKING :
217 case qtrapids::QTorrentHandle::CHECKING_FILES :
218 case qtrapids::QTorrentHandle::DOWNLOADING_METADATA :
219 case qtrapids::QTorrentHandle::ALLOCATING :
221 case qtrapids::QTorrentHandle::DOWNLOADING :
222 case qtrapids::QTorrentHandle::FINISHED :
223 case qtrapids::QTorrentHandle::SEEDING :
230 void DownloadView::UpdateView()
232 DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
234 std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listIter
236 std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listEnd
239 while (listIter != listEnd) {
240 updateItem(listIter->first);