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)
87 DownloadViewItem *item = items_[handle];
89 // Only the changing fields are being updated:
90 item->setData(2, Qt::DisplayRole,
91 QVariant(GetStatusString(handle.state())));
92 item->setData(4, Qt::DisplayRole,
93 QVariant(QString::number(handle.downloadRate(), 'f', 2)));
94 item->setData(5, Qt::DisplayRole,
95 QVariant(QString::number(handle.uploadRate(), 'f', 2)));
96 item->setData(6, Qt::DisplayRole,
97 QString::number(handle.numSeeds()) + "/"
98 + QString::number(handle.numLeeches()));
100 // Set progress if increment is 1 percent.
101 float prog = handle.progress();
102 if ((prog-lastProg) >= 0.01 || prog >= 1.0)
104 item->setData(3, Qt::DisplayRole,
105 QVariant(QString::number(100*prog) + '%'));
109 /// @TODO: ETA-counter adjusting,if ETA is to be implemented
112 QBrush brushTmp(GetStatusColor(handle.state()));
113 item->setForeground(2, brushTmp);
119 qtrapids::QTorrentHandle DownloadView::removeSelected()
121 #ifdef QTRAPIDS_DEBUG
122 qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ;
125 DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
127 std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listIter
129 std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listEnd
132 while (listIter != listEnd)
134 if (listIter->second == item)
141 qtrapids::QTorrentHandle handle = listIter->first;
142 items_.erase(listIter);
145 int index = indexOfTopLevelItem(currentItem());
148 takeTopLevelItem(index);
151 #ifdef QTRAPIDS_DEBUG
152 qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ;
159 void DownloadView::removeItem(qtrapids::QTorrentHandle handle)
164 void DownloadView::setRefreshInterval(int msec)
166 timer_->setInterval(msec);
170 void DownloadView::on_itemClicked(QTreeWidgetItem * item, int column)
173 qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
174 qDebug() << "current item" << currentItem();
176 if (item == currentItem() && item->isSelected()) {
177 item->setSelected(false);
182 void DownloadView::on_timeout()
184 #ifdef QTRAPIDS_DEBUG
185 qDebug() << "DownloadView::on_timeout()";
192 QString DownloadView::GetStatusString(qtrapids::QTorrentHandle::State const& status) const
196 case qtrapids::QTorrentHandle::QUEUED_FOR_CHECKING :
198 case qtrapids::QTorrentHandle::CHECKING_FILES :
199 return tr("Checking");
200 case qtrapids::QTorrentHandle::DOWNLOADING_METADATA :
201 return tr("DL meta");
202 case qtrapids::QTorrentHandle::DOWNLOADING :
203 return tr("Downloading");
204 case qtrapids::QTorrentHandle::FINISHED :
205 return tr("Finished");
206 case qtrapids::QTorrentHandle::SEEDING :
207 return tr("Seeding");
208 case qtrapids::QTorrentHandle::ALLOCATING :
209 return tr("Allocating");
216 QColor DownloadView::GetStatusColor(qtrapids::QTorrentHandle::State const& status) const
218 QColor green(40,205,40);
219 QColor yellow(255,174,0);
223 case qtrapids::QTorrentHandle::QUEUED_FOR_CHECKING :
224 case qtrapids::QTorrentHandle::CHECKING_FILES :
225 case qtrapids::QTorrentHandle::DOWNLOADING_METADATA :
226 case qtrapids::QTorrentHandle::ALLOCATING :
228 case qtrapids::QTorrentHandle::DOWNLOADING :
229 case qtrapids::QTorrentHandle::FINISHED :
230 case qtrapids::QTorrentHandle::SEEDING :
237 void DownloadView::UpdateView()
239 DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
241 std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listIter
243 std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listEnd
246 while (listIter != listEnd)
248 updateItem(listIter->first);