Code formatting/indentation unified in trunk
[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
87         DownloadViewItem *item = items_[handle];
88
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()));
99
100         // Set progress if increment is 1 percent.
101         float prog = handle.progress();
102         if ((prog-lastProg) >= 0.01 || prog >= 1.0)
103         {
104             item->setData(3, Qt::DisplayRole,
105                           QVariant(QString::number(100*prog) + '%'));
106             lastProg = prog;
107         }
108
109         /// @TODO: ETA-counter adjusting,if ETA is to be implemented
110
111         // Adjust color
112         QBrush brushTmp(GetStatusColor(handle.state()));
113         item->setForeground(2, brushTmp);
114     }
115
116 }
117
118
119 qtrapids::QTorrentHandle DownloadView::removeSelected()
120 {
121 #ifdef QTRAPIDS_DEBUG
122     qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
123 #endif
124
125     DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
126
127     std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listIter
128     = items_.begin();
129     std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listEnd
130     = items_.end();
131
132     while (listIter != listEnd)
133     {
134         if (listIter->second == item)
135         {
136             break;
137         }
138         ++listIter;
139     }
140
141     qtrapids::QTorrentHandle handle = listIter->first;
142     items_.erase(listIter);
143
144
145     int index = indexOfTopLevelItem(currentItem());
146     if (index >= 0)
147     {
148         takeTopLevelItem(index);
149     }
150
151 #ifdef QTRAPIDS_DEBUG
152     qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
153 #endif
154
155     return handle;
156 }
157
158
159 void DownloadView::removeItem(qtrapids::QTorrentHandle handle)
160 {
161 }
162
163
164 void DownloadView::setRefreshInterval(int msec)
165 {
166     timer_->setInterval(msec);
167 }
168
169
170 void DownloadView::on_itemClicked(QTreeWidgetItem * item, int column)
171 {
172     /*
173     qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
174     qDebug() << "current item" << currentItem();
175
176     if (item == currentItem() && item->isSelected()) {
177         item->setSelected(false);
178     }
179     */
180 }
181
182 void DownloadView::on_timeout()
183 {
184 #ifdef QTRAPIDS_DEBUG
185     qDebug() << "DownloadView::on_timeout()";
186 #endif
187     UpdateView();
188 }
189
190
191
192 QString DownloadView::GetStatusString(qtrapids::QTorrentHandle::State const& status) const
193 {
194     switch (status)
195     {
196     case qtrapids::QTorrentHandle::QUEUED_FOR_CHECKING :
197         return tr("Queued");
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");
210     default:
211         return tr("N/A");
212     }
213 }
214
215
216 QColor DownloadView::GetStatusColor(qtrapids::QTorrentHandle::State const& status) const
217 {
218     QColor green(40,205,40);
219     QColor yellow(255,174,0);
220
221     switch (status)
222     {
223     case qtrapids::QTorrentHandle::QUEUED_FOR_CHECKING :
224     case qtrapids::QTorrentHandle::CHECKING_FILES :
225     case qtrapids::QTorrentHandle::DOWNLOADING_METADATA :
226     case qtrapids::QTorrentHandle::ALLOCATING :
227         return yellow;
228     case qtrapids::QTorrentHandle::DOWNLOADING :
229     case qtrapids::QTorrentHandle::FINISHED :
230     case qtrapids::QTorrentHandle::SEEDING :
231         return green;
232     default:
233         return QColor();
234     }
235 }
236
237 void DownloadView::UpdateView()
238 {
239     DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
240
241     std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listIter
242     = items_.begin();
243     std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listEnd
244     = items_.end();
245
246     while (listIter != listEnd)
247     {
248         updateItem(listIter->first);
249         ++listIter;
250     }
251 }