23 #include "wadseekerwadstable.h"    25 #include "speedcalculator.h"    26 #include "strings.hpp"    29 #include <QHeaderView>    30 #include <QProgressBar>    33 WadseekerWadsTable::WadseekerWadsTable(QWidget* pParent)
    36         d.bAlreadyShownOnce = 
false;
    37         d.updateClock.start();
    40 WadseekerWadsTable::~WadseekerWadsTable()
    42         QMap<QString, SpeedCalculator* >::iterator it;
    43         for (it = d.speedCalculators.begin(); it != d.speedCalculators.end(); ++it)
    49 void WadseekerWadsTable::addFile(
const QString& filename)
    52         if (findFileRow(filename) < 0)
    54                 insertRow(rowCount());
    55                 int rowIndex = rowCount() - 1;
    58                 setSortingEnabled(
false);
    60                 QProgressBar* pBar = 
new QProgressBar();
    61                 pBar->setAlignment(Qt::AlignCenter);
    63                 setItem(rowIndex, IDX_NAME_COLUMN, 
new QTableWidgetItem(filename));
    64                 setItem(rowIndex, IDX_URL_COLUMN, 
new QTableWidgetItem());
    65                 setCellWidget(rowIndex, IDX_PROGRESS_COLUMN, pBar);
    66                 setItem(rowIndex, IDX_SPEED_COLUMN, 
new QTableWidgetItem());
    67                 setItem(rowIndex, IDX_ETA_COLUMN, 
new QTableWidgetItem(tr(
"N/A")));
    68                 setItem(rowIndex, IDX_SIZE_COLUMN, 
new QTableWidgetItem(tr(
"N/A")));
    73                 d.speedCalculators.insert(filename, pCalculator);
    75                 setSortingEnabled(
true);
    82         QPoint displayPoint = this->viewport()->mapToGlobal(cursorPosition);
    83         menu->move(displayPoint);
    87                 menu->actionSkipCurrentSite->setEnabled(
false);
    95         if (row < 0 || row >= this->rowCount())
   100         QString fileName = fileNameAtRow(row);
   101         return d.speedCalculators[fileName]->expectedDataSize();
   104 QString WadseekerWadsTable::fileNameAtRow(
int row)
 const   106         if (row < 0 || row >= this->rowCount())
   111         return item(row, IDX_NAME_COLUMN)->text();
   114 int WadseekerWadsTable::findFileRow(
const QString& filename)
   116         QList<QTableWidgetItem *> list = findItems(filename, Qt::MatchFixedString);
   119                 return list.first()->row();
   125 void WadseekerWadsTable::setFileDownloadFinished(
const QString& filename)
   127         int row = findFileRow(filename);
   133                 item(row, IDX_ETA_COLUMN)->setText(tr(
"N/A"));
   134                 item(row, IDX_SPEED_COLUMN)->setText(tr(
"N/A"));
   136                 item(row, IDX_URL_COLUMN)->setText(tr(
"Awaiting URLs"));
   137                 item(row, IDX_URL_COLUMN)->setToolTip(tr(
"Awaiting URLs"));
   139                 const bool FORCE = 
true;
   140                 updateDataInfoValues(FORCE);
   144 void WadseekerWadsTable::setFileFailed(
const QString& filename)
   146         int row = findFileRow(filename);
   150                 item(row, IDX_NAME_COLUMN)->setIcon(QIcon(
":/icons/x.png"));
   152                 item(row, IDX_URL_COLUMN)->setText(
"");
   156 void WadseekerWadsTable::setFileProgress(
const QString& filename, qint64 current, qint64 total)
   158         int row = findFileRow(filename);
   162                 QProgressBar* pBar = (QProgressBar*) this->cellWidget(row, IDX_PROGRESS_COLUMN);
   163                 pBar->setMaximum(total);
   164                 pBar->setValue(current);
   171                 const bool FORCE = 
true;
   172                 updateDataInfoValues(!FORCE);
   176 void WadseekerWadsTable::setFileSuccessful(
const QString& filename)
   178         int row = findFileRow(filename);
   183                 QProgressBar* pBar = (QProgressBar*) this->cellWidget(row, IDX_PROGRESS_COLUMN);
   185                 if (pCalculator->expectedDataSize() == 0)
   192                 pBar->setMaximum(pCalculator->expectedDataSize());
   193                 pBar->setValue(pCalculator->expectedDataSize());
   195                 item(row, IDX_NAME_COLUMN)->setIcon(QIcon(
":/icons/ok.png"));
   196                 item(row, IDX_URL_COLUMN)->setText(
"");
   198                 item(row, IDX_ETA_COLUMN)->setText(tr(
"Done"));
   199                 item(row, IDX_SPEED_COLUMN)->setText(
"");
   201                 const bool FORCE = 
true;
   202                 updateDataInfoValues(FORCE);
   210         int row = findFileRow(filename);
   214                 QTableWidgetItem* pItem = this->item(row, IDX_URL_COLUMN);
   215                 pItem->setText(url.toString());
   216                 pItem->setToolTip(url.toString());
   218                 QProgressBar* pBar = (QProgressBar*) this->cellWidget(row, IDX_PROGRESS_COLUMN);
   223                 pCalculator->
start();
   227 void WadseekerWadsTable::showEvent(QShowEvent* pEvent)
   229         if (!d.bAlreadyShownOnce)
   233                 QHeaderView* pHeader = horizontalHeader();
   236 #if QT_VERSION >= 0x050000   237                 pHeader->setSectionResizeMode(IDX_URL_COLUMN, QHeaderView::Stretch);
   239                 pHeader->setResizeMode(IDX_URL_COLUMN, QHeaderView::Stretch);
   242                 pHeader->resizeSection(IDX_NAME_COLUMN, 140);
   243                 pHeader->resizeSection(IDX_PROGRESS_COLUMN, 85);
   244                 pHeader->resizeSection(IDX_ETA_COLUMN, 85);
   245                 pHeader->resizeSection(IDX_SIZE_COLUMN, 150);
   246                 pHeader->resizeSection(IDX_SPEED_COLUMN, 85);
   253         double sumDownloadPercentages = 0.0;
   255         if (this->rowCount() == 0)
   260         for (
int i = 0; i < this->rowCount(); ++i)
   262                 const QProgressBar* pBar = (
const QProgressBar*) this->cellWidget(i, IDX_PROGRESS_COLUMN);
   265                         int val = pBar->value();
   266                         int max = pBar->maximum();
   270                                 double curPercentage =  (double) val / (
double) max;
   271                                 sumDownloadPercentages += curPercentage;
   277         sumDownloadPercentages *= 100.0;
   278         double averageDownloadPercentages = sumDownloadPercentages / (double) this->rowCount();
   280         return averageDownloadPercentages;
   283 void WadseekerWadsTable::updateDataInfoValues(
bool bForce)
   286         if (d.updateClock.elapsed() > UPDATE_INTERVAL_MS || bForce)
   288                 d.updateClock.start();
   290                 for (
int i = 0; i < this->rowCount(); ++i)
   293                         QString filename = this->item(i, IDX_NAME_COLUMN)->text();
   300                         QString size = QString(
"%1 / %2").arg(strCurrent, strTotal);
   301                         item(i, IDX_SIZE_COLUMN)->setText(size);
   310                                 long double ldSpeed = pCalculator->
getSpeed();
   315                                         item(i, IDX_ETA_COLUMN)->setText(strEta);
   319                                         item(i, IDX_ETA_COLUMN)->setText(tr(
"N/A"));
   325                                         item(i, IDX_SPEED_COLUMN)->setText(strSpeed);
   329                                         item(i, IDX_SPEED_COLUMN)->setText(tr(
"N/A"));
   336 WadseekerWadsTable::ContextMenu::ContextMenu(QWidget* pParent)
   339         this->actionSkipCurrentSite = 
new QAction(tr(
"Skip current URL"), 
this);
   341         this->addAction(this->actionSkipCurrentSite);
 void setFileUrl(const QString &filename, const QUrl &url)
Fired when new URL starts for specified file. 
 
qint64 expectedDataSize(int row) const 
Total size of data in bytes for specified row. 
 
void start()
Clears all values. Prepares SpeedCalculator for new speed measure. 
 
static QString formatTime(float seconds)
Formats a numerical time value into a string. 
 
double totalDonePercentage() const 
Total done percentage calculated basing on the data set by siteFileProgress(). 
 
void registerDataAmount(qint64 totalAmountOfArrivedData)
Register new total amount of data. 
 
long double getSpeed() const 
In bytes per second. 
 
void setExpectedDataSize(qint64 size)
Maximum expected size of the data. 
 
static QString formatDataAmount(qint64 bytes)
Similar to formatDataSpeed(). 
 
static QString formatDataSpeed(float speedInBytesPerSecond)
Formats a numerical speed value into a string. 
 
qint64 lastRegisterAttemptedDataAmount() const 
Last amount of data that was passed to registerDataAmount() 
 
long double estimatedTimeUntilArrival() const 
In seconds.