23 #include "wadseekerwadstable.h"
25 #include "speedcalculator.h"
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 pHeader->setResizeMode(IDX_URL_COLUMN, QHeaderView::Stretch);
238 pHeader->resizeSection(IDX_NAME_COLUMN, 140);
239 pHeader->resizeSection(IDX_PROGRESS_COLUMN, 85);
240 pHeader->resizeSection(IDX_ETA_COLUMN, 85);
241 pHeader->resizeSection(IDX_SIZE_COLUMN, 150);
242 pHeader->resizeSection(IDX_SPEED_COLUMN, 85);
249 double sumDownloadPercentages = 0.0;
251 if (this->rowCount() == 0)
256 for (
int i = 0; i < this->rowCount(); ++i)
258 const QProgressBar* pBar = (
const QProgressBar*) this->cellWidget(i, IDX_PROGRESS_COLUMN);
261 int val = pBar->value();
262 int max = pBar->maximum();
266 double curPercentage = (double) val / (
double) max;
267 sumDownloadPercentages += curPercentage;
273 sumDownloadPercentages *= 100.0;
274 double averageDownloadPercentages = sumDownloadPercentages / (double) this->rowCount();
276 return averageDownloadPercentages;
279 void WadseekerWadsTable::updateDataInfoValues(
bool bForce)
282 if (d.updateClock.elapsed() > UPDATE_INTERVAL_MS || bForce)
284 d.updateClock.start();
286 for (
int i = 0; i < this->rowCount(); ++i)
289 QString filename = this->item(i, IDX_NAME_COLUMN)->text();
296 QString size = QString(
"%1 / %2").arg(strCurrent, strTotal);
297 item(i, IDX_SIZE_COLUMN)->setText(size);
306 long double ldSpeed = pCalculator->
getSpeed();
311 item(i, IDX_ETA_COLUMN)->setText(strEta);
315 item(i, IDX_ETA_COLUMN)->setText(tr(
"N/A"));
321 item(i, IDX_SPEED_COLUMN)->setText(strSpeed);
325 item(i, IDX_SPEED_COLUMN)->setText(tr(
"N/A"));
332 WadseekerWadsTable::ContextMenu::ContextMenu(QWidget* pParent)
335 this->actionSkipCurrentSite =
new QAction(tr(
"Skip current URL"),
this);
337 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.