- Added buildconf.pri to hold the master-level build options.
[qtrapids] / src / gui / DownloadView.cpp
index 5d616d8..ede3b66 100644 (file)
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 #include <QDebug>
+#include <QTimer>
 #include <QVariant>
 #include <QColor>
 #include "DownloadView.h"
 
 
 DownloadView::DownloadView(QWidget* parent) :
-        QTreeWidget(parent),
-        items_()
+               QTreeWidget(parent),
+               items_(),
+               timer_(NULL)
 {
-    setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
-    setHeaderItem(DownloadViewItem::getHeaderItem());
+  setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
+  setHeaderItem(DownloadViewItem::getHeaderItem());
 
-    connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
-            this, SLOT(on_itemClicked(QTreeWidgetItem*, int)));
+  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);
 
 }
 
@@ -43,20 +49,23 @@ DownloadView::~DownloadView()
 
 void DownloadView::newItem(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(handle.progress())
-                       << QString::number(handle.downloadRate()) 
-                       << QString::number(handle.uploadRate()) 
+                       << 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);
                        
@@ -67,22 +76,37 @@ void DownloadView::newItem(QTorrentHandle handle)
 
 void DownloadView::updateItem(QTorrentHandle handle)
 {
-       qDebug() << "DownloadView::updateItem() "  << items_.count(handle);
+       //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(3, Qt::DisplayRole,
-                                                                       QVariant(QString::number(handle.progress())));
                item->setData(4, Qt::DisplayRole,
-                                                                       QVariant(QString::number(handle.downloadRate())));
+                                                                       QVariant(QString::number(handle.downloadRate(), 'f', 2)));
                item->setData(5, Qt::DisplayRole,
-                                                                       QVariant(QString::number(handle.uploadRate())));
+                                                                       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);
        }
@@ -92,7 +116,9 @@ void DownloadView::updateItem(QTorrentHandle handle)
 
 QTorrentHandle DownloadView::removeSelected()
 {
+#ifdef QTRAPIDS_DEBUG
        qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
+#endif
 
        DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
        
@@ -116,9 +142,11 @@ QTorrentHandle DownloadView::removeSelected()
        if (index >= 0) {
                takeTopLevelItem(index);
        }
-       
+
+#ifdef QTRAPIDS_DEBUG
        qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
-       
+#endif
+
        return handle;
 }
 
@@ -128,6 +156,12 @@ void DownloadView::removeItem(QTorrentHandle handle)
 }
 
 
+void DownloadView::setRefreshInterval(int msec)
+{
+       timer_->setInterval(msec);
+}
+
+
 void DownloadView::on_itemClicked(QTreeWidgetItem * item, int column)
 {
        /*
@@ -140,6 +174,14 @@ void DownloadView::on_itemClicked(QTreeWidgetItem * item, int column)
        */
 }
 
+void DownloadView::on_timeout() {
+#ifdef QTRAPIDS_DEBUG
+       qDebug() << "DownloadView::on_timeout()";
+#endif
+       UpdateView();
+}
+
+
 
 QString DownloadView::GetStatusString(QTorrentHandle::State const& status) const
 {
@@ -182,4 +224,19 @@ QColor DownloadView::GetStatusColor(QTorrentHandle::State const& status) const
                default:
                        return QColor();
        }
-}
\ No newline at end of file
+}
+
+void DownloadView::UpdateView()
+{
+       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) {
+               updateItem(listIter->first);
+               ++listIter;
+       }
+}