23 #include "wadseekerwadstable.h"
25 #include "speedcalculator.h"
26 #include "strings.hpp"
27 #include "wadseeker/entities/modfile.h"
30 #include <QHeaderView>
31 #include <QProgressBar>
34 WadseekerWadsTable::WadseekerWadsTable(QWidget *pParent)
37 d.bAlreadyShownOnce =
false;
38 d.updateClock.start();
41 WadseekerWadsTable::~WadseekerWadsTable()
43 QMap<QString, SpeedCalculator *>::iterator it;
44 for (it = d.speedCalculators.begin(); it != d.speedCalculators.end(); ++it)
48 void WadseekerWadsTable::addFile(
const QString &filename)
51 if (findFileRow(filename) < 0)
53 insertRow(rowCount());
54 int rowIndex = rowCount() - 1;
57 setSortingEnabled(
false);
59 auto pBar =
new QProgressBar();
60 pBar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
61 pBar->setFixedWidth(PROGRESS_COLUMN_WIDTH);
62 pBar->setAlignment(Qt::AlignCenter);
64 setItem(rowIndex, IDX_NAME_COLUMN,
new QTableWidgetItem(filename));
65 setItem(rowIndex, IDX_URL_COLUMN,
new QTableWidgetItem());
66 setCellWidget(rowIndex, IDX_PROGRESS_COLUMN, pBar);
67 setItem(rowIndex, IDX_SPEED_COLUMN,
new QTableWidgetItem());
68 setItem(rowIndex, IDX_ETA_COLUMN,
new QTableWidgetItem(tr(
"N/A")));
69 setItem(rowIndex, IDX_SIZE_COLUMN,
new QTableWidgetItem(tr(
"N/A")));
74 d.speedCalculators.insert(filename, pCalculator);
76 setSortingEnabled(
true);
82 auto menu =
new ContextMenu(
this);
83 QPoint displayPoint = this->viewport()->mapToGlobal(cursorPosition);
84 menu->move(displayPoint);
87 menu->actionSkipCurrentSite->setEnabled(
false);
94 if (row < 0 || row >= this->rowCount())
97 QString fileName = fileNameAtRow(row);
98 return d.speedCalculators[fileName]->expectedDataSize();
101 QString WadseekerWadsTable::fileNameAtRow(
int row)
const
103 if (row < 0 || row >= this->rowCount())
106 return item(row, IDX_NAME_COLUMN)->text();
109 int WadseekerWadsTable::findFileRow(
const QString &filename)
111 if (filename.isEmpty())
114 QList<QTableWidgetItem *> list = findItems(filename, Qt::MatchFixedString);
116 return list.first()->row();
121 void WadseekerWadsTable::setFileDownloadFinished(
const ModFile &filename)
123 int row = findFileRow(filename.fileName());
129 item(row, IDX_ETA_COLUMN)->setText(tr(
"N/A"));
130 item(row, IDX_SPEED_COLUMN)->setText(tr(
"N/A"));
132 item(row, IDX_URL_COLUMN)->setText(tr(
"Awaiting URLs"));
133 item(row, IDX_URL_COLUMN)->setToolTip(tr(
"Awaiting URLs"));
135 const bool FORCE =
true;
136 updateDataInfoValues(FORCE);
140 void WadseekerWadsTable::setFileFailed(
const ModFile &filename)
142 int row = findFileRow(filename.fileName());
146 item(row, IDX_NAME_COLUMN)->setIcon(QIcon(
":/icons/x.png"));
148 item(row, IDX_URL_COLUMN)->setText(
"");
152 void WadseekerWadsTable::setFileProgress(
const ModFile &filename, qint64 current, qint64 total)
154 int row = findFileRow(filename.fileName());
158 auto pBar = (QProgressBar *) this->cellWidget(row, IDX_PROGRESS_COLUMN);
159 pBar->setMaximum(total);
160 pBar->setValue(current);
163 SpeedCalculator *pCalculator = d.speedCalculators.value(filename.fileName());
167 const bool FORCE =
true;
168 updateDataInfoValues(!FORCE);
172 void WadseekerWadsTable::setFileSuccessful(
const ModFile &filename)
174 int row = findFileRow(filename.fileName());
179 auto pBar = (QProgressBar *) this->cellWidget(row, IDX_PROGRESS_COLUMN);
180 SpeedCalculator *pCalculator = d.speedCalculators[filename.fileName()];
181 if (pCalculator->expectedDataSize() == 0)
186 pBar->setMaximum(pCalculator->expectedDataSize());
187 pBar->setValue(pCalculator->expectedDataSize());
189 item(row, IDX_NAME_COLUMN)->setIcon(QIcon(
":/icons/ok.png"));
190 item(row, IDX_URL_COLUMN)->setText(
"");
192 item(row, IDX_ETA_COLUMN)->setText(tr(
"Done"));
193 item(row, IDX_SPEED_COLUMN)->setText(
"");
195 const bool FORCE =
true;
196 updateDataInfoValues(FORCE);
204 int row = findFileRow(filename.fileName());
208 QTableWidgetItem *pItem = this->item(row, IDX_URL_COLUMN);
209 pItem->setText(url.toString());
210 pItem->setToolTip(url.toString());
212 auto pBar = (QProgressBar *) this->cellWidget(row, IDX_PROGRESS_COLUMN);
216 SpeedCalculator *pCalculator = d.speedCalculators[filename.fileName()];
217 pCalculator->
start();
221 void WadseekerWadsTable::showEvent(QShowEvent *pEvent)
224 if (!d.bAlreadyShownOnce)
228 QHeaderView *pHeader = horizontalHeader();
231 pHeader->setSectionResizeMode(IDX_URL_COLUMN, QHeaderView::Stretch);
232 pHeader->setSectionResizeMode(IDX_PROGRESS_COLUMN, QHeaderView::Fixed);
234 pHeader->resizeSection(IDX_NAME_COLUMN, 140);
235 pHeader->resizeSection(IDX_PROGRESS_COLUMN, PROGRESS_COLUMN_WIDTH);
236 pHeader->resizeSection(IDX_ETA_COLUMN, 85);
237 pHeader->resizeSection(IDX_SIZE_COLUMN, 150);
238 pHeader->resizeSection(IDX_SPEED_COLUMN, 85);
244 double sumDownloadPercentages = 0.0;
246 if (this->rowCount() == 0)
249 for (
int i = 0; i < this->rowCount(); ++i)
251 const auto pBar = (
const QProgressBar *) this->cellWidget(i, IDX_PROGRESS_COLUMN);
254 int val = pBar->value();
255 int max = pBar->maximum();
259 double curPercentage = (double) val / (
double) max;
260 sumDownloadPercentages += curPercentage;
266 sumDownloadPercentages *= 100.0;
267 double averageDownloadPercentages = sumDownloadPercentages / (double) this->rowCount();
269 return averageDownloadPercentages;
272 void WadseekerWadsTable::updateDataInfoValues(
bool bForce)
275 if (d.updateClock.elapsed() > UPDATE_INTERVAL_MS || bForce)
277 d.updateClock.start();
279 for (
int i = 0; i < this->rowCount(); ++i)
282 QString filename = this->item(i, IDX_NAME_COLUMN)->text();
289 QString size = QString(
"%1 / %2").arg(strCurrent, strTotal);
290 item(i, IDX_SIZE_COLUMN)->setText(size);
299 long double ldSpeed = pCalculator->
getSpeed();
304 item(i, IDX_ETA_COLUMN)->setText(strEta);
307 item(i, IDX_ETA_COLUMN)->setText(tr(
"N/A"));
312 item(i, IDX_SPEED_COLUMN)->setText(strSpeed);
315 item(i, IDX_SPEED_COLUMN)->setText(tr(
"N/A"));
321 WadseekerWadsTable::ContextMenu::ContextMenu(QWidget *pParent)
324 this->actionSkipCurrentSite =
new QAction(tr(
"Skip current URL"),
this);
326 this->addAction(this->actionSkipCurrentSite);