1 /***************************************************************************
2 * Copyright (C) 2010 by Ixonos Plc *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; version 2 of the License. *
8 * This program is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11 * GNU General Public License for more details. *
13 * You should have received a copy of the GNU General Public License *
14 * along with this program; if not, write to the *
15 * Free Software Foundation, Inc., *
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17 ***************************************************************************/
22 #include "DownloadView.h"
25 DownloadView::DownloadView(QWidget* parent) :
30 setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
31 setHeaderItem(DownloadViewItem::getHeaderItem());
33 connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
34 this, SLOT(on_itemClicked(QTreeWidgetItem*, int)));
36 timer_ = new QTimer(this);
37 connect(timer_, SIGNAL(timeout()), this, SLOT(on_timeout()));
43 DownloadView::~DownloadView()
48 void DownloadView::newItem(qtrapids::QTorrentHandle handle)
51 qDebug() << "DownloadView::newItem() " << items_.count(handle);
54 DownloadViewItem *item = new DownloadViewItem(QStringList()
56 << QString::number(handle.getTotalSize())
57 << GetStatusString(handle.state())
58 << QString::number(100*handle.progress())
59 << QString::number(handle.downloadRate(), 'f', 2)
60 << QString::number(handle.uploadRate(), 'f', 2)
61 << QString::number(handle.numSeeds()) + "/"
62 + QString::number(handle.numLeeches())
63 << QString::number(handle.ratio())
66 // Set text color for status:
67 QBrush brushTmp(GetStatusColor(handle.state()));
68 item->setForeground(2, brushTmp);
70 addTopLevelItem(item);
71 items_[handle] = item;
75 void DownloadView::updateItem(qtrapids::QTorrentHandle handle)
77 //qDebug() << "DownloadView::updateItem() " << items_.count(handle);
79 static float lastProg = 0;
81 // If there are items currently downloading, update:
82 if (items_.count(handle) > 0) {
84 DownloadViewItem *item = items_[handle];
86 // Only the changing fields are being updated:
87 item->setData(2, Qt::DisplayRole,
88 QVariant(GetStatusString(handle.state())));
89 item->setData(4, Qt::DisplayRole,
90 QVariant(QString::number(handle.downloadRate(), 'f', 2)));
91 item->setData(5, Qt::DisplayRole,
92 QVariant(QString::number(handle.uploadRate(), 'f', 2)));
93 item->setData(6, Qt::DisplayRole,
94 QString::number(handle.numSeeds()) + "/"
95 + QString::number(handle.numLeeches()));
97 // Set progress if increment is 1 percent.
98 float prog = handle.progress();
99 if ((prog-lastProg) >= 0.01 || prog >= 1.0) {
100 item->setData(3, Qt::DisplayRole,
101 QVariant(QString::number(100*prog)));
105 /// @TODO: ETA-counter adjusting,if ETA is to be implemented
108 QBrush brushTmp(GetStatusColor(handle.state()));
109 item->setForeground(2, brushTmp);
115 qtrapids::QTorrentHandle DownloadView::removeSelected()
117 #ifdef QTRAPIDS_DEBUG
118 qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ;
121 DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
123 std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listIter
125 std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listEnd
128 while (listIter != listEnd) {
129 if (listIter->second == item) {
135 qtrapids::QTorrentHandle handle = listIter->first;
136 items_.erase(listIter);
139 int index = indexOfTopLevelItem(currentItem());
141 takeTopLevelItem(index);
144 #ifdef QTRAPIDS_DEBUG
145 qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ;
152 void DownloadView::removeItem(qtrapids::QTorrentHandle handle)
157 void DownloadView::setRefreshInterval(int msec)
159 timer_->setInterval(msec);
163 void DownloadView::on_itemClicked(QTreeWidgetItem * item, int column)
166 qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
167 qDebug() << "current item" << currentItem();
169 if (item == currentItem() && item->isSelected()) {
170 item->setSelected(false);
175 void DownloadView::on_timeout()
177 #ifdef QTRAPIDS_DEBUG
178 qDebug() << "DownloadView::on_timeout()";
185 QString DownloadView::GetStatusString(qtrapids::QTorrentHandle::State const& status) const
188 case qtrapids::QTorrentHandle::QUEUED_FOR_CHECKING :
190 case qtrapids::QTorrentHandle::CHECKING_FILES :
191 return tr("Checking");
192 case qtrapids::QTorrentHandle::DOWNLOADING_METADATA :
193 return tr("DL meta");
194 case qtrapids::QTorrentHandle::DOWNLOADING :
195 return tr("Downloading");
196 case qtrapids::QTorrentHandle::FINISHED :
197 return tr("Finished");
198 case qtrapids::QTorrentHandle::SEEDING :
199 return tr("Seeding");
200 case qtrapids::QTorrentHandle::ALLOCATING :
201 return tr("Allocating");
208 QColor DownloadView::GetStatusColor(qtrapids::QTorrentHandle::State const& status) const
210 QColor green(40,205,40);
211 QColor yellow(255,174,0);
214 case qtrapids::QTorrentHandle::QUEUED_FOR_CHECKING :
215 case qtrapids::QTorrentHandle::CHECKING_FILES :
216 case qtrapids::QTorrentHandle::DOWNLOADING_METADATA :
217 case qtrapids::QTorrentHandle::ALLOCATING :
219 case qtrapids::QTorrentHandle::DOWNLOADING :
220 case qtrapids::QTorrentHandle::FINISHED :
221 case qtrapids::QTorrentHandle::SEEDING :
228 void DownloadView::UpdateView()
230 DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
232 std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listIter
234 std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listEnd
237 while (listIter != listEnd) {
238 updateItem(listIter->first);