Code formatting/indentation unified in trunk
[qtrapids] / src / gui / DownloadView.cpp
index d7424b0..da9642b 100644 (file)
@@ -18,6 +18,7 @@
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 #include <QDebug>
+#include <QTimer>
 #include <QVariant>
 #include <QColor>
 #include "DownloadView.h"
@@ -25,7 +26,8 @@
 
 DownloadView::DownloadView(QWidget* parent) :
         QTreeWidget(parent),
-        items_()
+        items_(),
+        timer_(NULL)
 {
     setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
     setHeaderItem(DownloadViewItem::getHeaderItem());
@@ -33,6 +35,10 @@ DownloadView::DownloadView(QWidget* parent) :
     connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
             this, SLOT(on_itemClicked(QTreeWidgetItem*, int)));
 
+    timer_ = new QTimer(this);
+    connect(timer_, SIGNAL(timeout()), this, SLOT(on_timeout()));
+    timer_->start(5000);
+
 }
 
 
@@ -41,145 +47,205 @@ DownloadView::~DownloadView()
 }
 
 
-void DownloadView::newItem(QTorrentHandle handle)
+void DownloadView::newItem(qtrapids::QTorrentHandle handle)
+{
+#ifdef QTRAPIDS_DEBUG
+    qDebug() << "DownloadView::newItem() " << items_.count(handle);
+#endif
+
+    DownloadViewItem *item = new DownloadViewItem(QStringList()
+            << handle.name()
+            << QString::number(handle.getTotalSize())
+            << GetStatusString(handle.state())
+            << QString::number(100*handle.progress() + '%')
+            << QString::number(handle.downloadRate(), 'f', 2)
+            << QString::number(handle.uploadRate(),  'f', 2)
+            << QString::number(handle.numSeeds()) + "/"
+            + QString::number(handle.numLeeches())
+            << QString::number(handle.ratio())
+            << "ETA" );
+
+    // Set text color for status:
+    QBrush brushTmp(GetStatusColor(handle.state()));
+    item->setForeground(2, brushTmp);
+
+    addTopLevelItem(item);
+    items_[handle] = item;
+}
+
+
+void DownloadView::updateItem(qtrapids::QTorrentHandle handle)
 {
-       qDebug() << "DownloadView::newItem() " << items_.count(handle);
-
-       DownloadViewItem *item = new DownloadViewItem(QStringList()
-                       << handle.name()
-                       << QString::number(handle.getTotalSize())
-                       << GetStatusString(handle.state())
-                       << QString::number(handle.progress())
-                       << QString::number(handle.downloadRate()) 
-                       << QString::number(handle.uploadRate()) 
-                       << QString::number(handle.numSeeds()) + "/"
-                                       + QString::number(handle.numLeeches())
-                       << QString::number(handle.ratio())
-                       << "ETA" );
-       
-       QBrush brushTmp(GetStatusColor(handle.state()));
-       item->setForeground(2, brushTmp);
-                       
-       addTopLevelItem(item);
-       items_[handle] = item;
+    //qDebug() << "DownloadView::updateItem() "  << items_.count(handle);
+
+    static float lastProg = 0;
+
+    // If there are items currently downloading, update:
+    if (items_.count(handle) > 0)
+    {
+
+        DownloadViewItem *item = items_[handle];
+
+        // Only the changing fields are being updated:
+        item->setData(2, Qt::DisplayRole,
+                      QVariant(GetStatusString(handle.state())));
+        item->setData(4, Qt::DisplayRole,
+                      QVariant(QString::number(handle.downloadRate(), 'f', 2)));
+        item->setData(5, Qt::DisplayRole,
+                      QVariant(QString::number(handle.uploadRate(), 'f', 2)));
+        item->setData(6, Qt::DisplayRole,
+                      QString::number(handle.numSeeds()) + "/"
+                      + QString::number(handle.numLeeches()));
+
+        // Set progress if increment is 1 percent.
+        float prog = handle.progress();
+        if ((prog-lastProg) >= 0.01 || prog >= 1.0)
+        {
+            item->setData(3, Qt::DisplayRole,
+                          QVariant(QString::number(100*prog) + '%'));
+            lastProg = prog;
+        }
+
+        /// @TODO: ETA-counter adjusting,if ETA is to be implemented
+
+        // Adjust color
+        QBrush brushTmp(GetStatusColor(handle.state()));
+        item->setForeground(2, brushTmp);
+    }
+
 }
 
 
-void DownloadView::updateItem(QTorrentHandle handle)
+qtrapids::QTorrentHandle DownloadView::removeSelected()
 {
-       qDebug() << "DownloadView::updateItem() "  << items_.count(handle);
-       
-       if (items_.count(handle) > 0) {
-               DownloadViewItem *item = items_[handle];
-               item->setData(2, Qt::DisplayRole,
-                                                                       QVariant(GetStatusString(handle.state())));
-               item->setData(3, Qt::DisplayRole,
-                                                                       QVariant(QString::number(handle.progress())));
-               item->setData(4, Qt::DisplayRole,
-                                                                       QVariant(QString::number(handle.downloadRate())));
-               item->setData(5, Qt::DisplayRole,
-                                                                       QVariant(QString::number(handle.uploadRate())));
-               item->setData(6, Qt::DisplayRole, 
-                                                                       QString::number(handle.numSeeds()) + "/" 
-                                                                               + QString::number(handle.numLeeches()));
-                                                                               
-               QBrush brushTmp(GetStatusColor(handle.state()));
-               item->setForeground(2, brushTmp);
-       }
+#ifdef QTRAPIDS_DEBUG
+    qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
+#endif
+
+    DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
+
+    std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listIter
+    = items_.begin();
+    std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listEnd
+    = items_.end();
+
+    while (listIter != listEnd)
+    {
+        if (listIter->second == item)
+        {
+            break;
+        }
+        ++listIter;
+    }
+
+    qtrapids::QTorrentHandle handle = listIter->first;
+    items_.erase(listIter);
+
 
+    int index = indexOfTopLevelItem(currentItem());
+    if (index >= 0)
+    {
+        takeTopLevelItem(index);
+    }
+
+#ifdef QTRAPIDS_DEBUG
+    qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
+#endif
+
+    return handle;
 }
 
 
-QTorrentHandle DownloadView::removeSelected()
+void DownloadView::removeItem(qtrapids::QTorrentHandle handle)
 {
-       qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
-
-       DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
-       
-       std::map<QTorrentHandle, DownloadViewItem*>::iterator listIter
-                       = items_.begin();
-       std::map<QTorrentHandle, DownloadViewItem*>::iterator listEnd
-                       = items_.end();
-       
-       while (listIter != listEnd) {
-               if (listIter->second == item) {
-                       break;
-               }
-               ++listIter;
-       }
-       
-       QTorrentHandle handle = listIter->first;
-       items_.erase(listIter);
-
-       
-       int index = indexOfTopLevelItem(currentItem());
-       if (index >= 0) {
-               takeTopLevelItem(index);
-       }
-       
-       qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
-       
-       return handle;
 }
 
 
-void DownloadView::removeItem(QTorrentHandle handle)
+void DownloadView::setRefreshInterval(int msec)
 {
+    timer_->setInterval(msec);
 }
 
 
 void DownloadView::on_itemClicked(QTreeWidgetItem * item, int column)
-{/*
-       qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
-       qDebug() << "current item" << currentItem();
-       
-       if (item == currentItem() && item->isSelected()) {
-               item->setSelected(false);
-       }
-       */
+{
+    /*
+    qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
+    qDebug() << "current item" << currentItem();
+
+    if (item == currentItem() && item->isSelected()) {
+       item->setSelected(false);
+    }
+    */
+}
+
+void DownloadView::on_timeout()
+{
+#ifdef QTRAPIDS_DEBUG
+    qDebug() << "DownloadView::on_timeout()";
+#endif
+    UpdateView();
 }
 
 
-QString DownloadView::GetStatusString(QTorrentHandle::State const& status) const
+
+QString DownloadView::GetStatusString(qtrapids::QTorrentHandle::State const& status) const
 {
-       switch (status) {
-               case QTorrentHandle::QUEUED_FOR_CHECKING :
-                       return "Queued";
-               case QTorrentHandle::CHECKING_FILES :
-                       return "Checking";
-               case QTorrentHandle::DOWNLOADING_METADATA :
-                       return "DL meta";
-               case QTorrentHandle::DOWNLOADING :
-                       return "Downloading";
-               case QTorrentHandle::FINISHED :
-                       return "Finished";
-               case QTorrentHandle::SEEDING :
-                       return "Seeding"; 
-               case QTorrentHandle::ALLOCATING :
-                       return "Allocating";
-               default:
-                       return "N/A";
-       }
+    switch (status)
+    {
+    case qtrapids::QTorrentHandle::QUEUED_FOR_CHECKING :
+        return tr("Queued");
+    case qtrapids::QTorrentHandle::CHECKING_FILES :
+        return tr("Checking");
+    case qtrapids::QTorrentHandle::DOWNLOADING_METADATA :
+        return tr("DL meta");
+    case qtrapids::QTorrentHandle::DOWNLOADING :
+        return tr("Downloading");
+    case qtrapids::QTorrentHandle::FINISHED :
+        return tr("Finished");
+    case qtrapids::QTorrentHandle::SEEDING :
+        return tr("Seeding");
+    case qtrapids::QTorrentHandle::ALLOCATING :
+        return tr("Allocating");
+    default:
+        return tr("N/A");
+    }
 }
 
 
-QColor DownloadView::GetStatusColor(QTorrentHandle::State const& status) const
+QColor DownloadView::GetStatusColor(qtrapids::QTorrentHandle::State const& status) const
 {
-       QColor green(40,205,40);
-       QColor yellow(255,174,0);
-
-       switch (status) {
-               case QTorrentHandle::QUEUED_FOR_CHECKING :
-               case QTorrentHandle::CHECKING_FILES :
-               case QTorrentHandle::DOWNLOADING_METADATA :
-               case QTorrentHandle::ALLOCATING :
-                       return yellow;
-               case QTorrentHandle::DOWNLOADING :
-               case QTorrentHandle::FINISHED :
-               case QTorrentHandle::SEEDING :
-                               return green;
-               default:
-                       return QColor();
-       }
-               
-}
\ No newline at end of file
+    QColor green(40,205,40);
+    QColor yellow(255,174,0);
+
+    switch (status)
+    {
+    case qtrapids::QTorrentHandle::QUEUED_FOR_CHECKING :
+    case qtrapids::QTorrentHandle::CHECKING_FILES :
+    case qtrapids::QTorrentHandle::DOWNLOADING_METADATA :
+    case qtrapids::QTorrentHandle::ALLOCATING :
+        return yellow;
+    case qtrapids::QTorrentHandle::DOWNLOADING :
+    case qtrapids::QTorrentHandle::FINISHED :
+    case qtrapids::QTorrentHandle::SEEDING :
+        return green;
+    default:
+        return QColor();
+    }
+}
+
+void DownloadView::UpdateView()
+{
+    DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
+
+    std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listIter
+    = items_.begin();
+    std::map<qtrapids::QTorrentHandle, DownloadViewItem*>::iterator listEnd
+    = items_.end();
+
+    while (listIter != listEnd)
+    {
+        updateItem(listIter->first);
+        ++listIter;
+    }
+}