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 QList<QTableWidgetItem *> list = findItems(filename, Qt::MatchFixedString);
113 return list.first()->row();
118 void WadseekerWadsTable::setFileDownloadFinished(
const ModFile &filename)
120 int row = findFileRow(filename.fileName());
126 item(row, IDX_ETA_COLUMN)->setText(tr(
"N/A"));
127 item(row, IDX_SPEED_COLUMN)->setText(tr(
"N/A"));
129 item(row, IDX_URL_COLUMN)->setText(tr(
"Awaiting URLs"));
130 item(row, IDX_URL_COLUMN)->setToolTip(tr(
"Awaiting URLs"));
132 const bool FORCE =
true;
133 updateDataInfoValues(FORCE);
137 void WadseekerWadsTable::setFileFailed(
const ModFile &filename)
139 int row = findFileRow(filename.fileName());
143 item(row, IDX_NAME_COLUMN)->setIcon(QIcon(
":/icons/x.png"));
145 item(row, IDX_URL_COLUMN)->setText(
"");
149 void WadseekerWadsTable::setFileProgress(
const ModFile &filename, qint64 current, qint64 total)
151 int row = findFileRow(filename.fileName());
155 auto pBar = (QProgressBar *) this->cellWidget(row, IDX_PROGRESS_COLUMN);
156 pBar->setMaximum(total);
157 pBar->setValue(current);
160 SpeedCalculator *pCalculator = d.speedCalculators.value(filename.fileName());
164 const bool FORCE =
true;
165 updateDataInfoValues(!FORCE);
169 void WadseekerWadsTable::setFileSuccessful(
const ModFile &filename)
171 int row = findFileRow(filename.fileName());
176 auto pBar = (QProgressBar *) this->cellWidget(row, IDX_PROGRESS_COLUMN);
177 SpeedCalculator *pCalculator = d.speedCalculators[filename.fileName()];
178 if (pCalculator->expectedDataSize() == 0)
183 pBar->setMaximum(pCalculator->expectedDataSize());
184 pBar->setValue(pCalculator->expectedDataSize());
186 item(row, IDX_NAME_COLUMN)->setIcon(QIcon(
":/icons/ok.png"));
187 item(row, IDX_URL_COLUMN)->setText(
"");
189 item(row, IDX_ETA_COLUMN)->setText(tr(
"Done"));
190 item(row, IDX_SPEED_COLUMN)->setText(
"");
192 const bool FORCE =
true;
193 updateDataInfoValues(FORCE);
201 int row = findFileRow(filename.fileName());
205 QTableWidgetItem *pItem = this->item(row, IDX_URL_COLUMN);
206 pItem->setText(url.toString());
207 pItem->setToolTip(url.toString());
209 auto pBar = (QProgressBar *) this->cellWidget(row, IDX_PROGRESS_COLUMN);
213 SpeedCalculator *pCalculator = d.speedCalculators[filename.fileName()];
214 pCalculator->
start();
218 void WadseekerWadsTable::showEvent(QShowEvent *pEvent)
221 if (!d.bAlreadyShownOnce)
225 QHeaderView *pHeader = horizontalHeader();
228 pHeader->setSectionResizeMode(IDX_URL_COLUMN, QHeaderView::Stretch);
229 pHeader->setSectionResizeMode(IDX_PROGRESS_COLUMN, QHeaderView::Fixed);
231 pHeader->resizeSection(IDX_NAME_COLUMN, 140);
232 pHeader->resizeSection(IDX_PROGRESS_COLUMN, PROGRESS_COLUMN_WIDTH);
233 pHeader->resizeSection(IDX_ETA_COLUMN, 85);
234 pHeader->resizeSection(IDX_SIZE_COLUMN, 150);
235 pHeader->resizeSection(IDX_SPEED_COLUMN, 85);
241 double sumDownloadPercentages = 0.0;
243 if (this->rowCount() == 0)
246 for (
int i = 0; i < this->rowCount(); ++i)
248 const auto pBar = (
const QProgressBar *) this->cellWidget(i, IDX_PROGRESS_COLUMN);
251 int val = pBar->value();
252 int max = pBar->maximum();
256 double curPercentage = (double) val / (
double) max;
257 sumDownloadPercentages += curPercentage;
263 sumDownloadPercentages *= 100.0;
264 double averageDownloadPercentages = sumDownloadPercentages / (double) this->rowCount();
266 return averageDownloadPercentages;
269 void WadseekerWadsTable::updateDataInfoValues(
bool bForce)
272 if (d.updateClock.elapsed() > UPDATE_INTERVAL_MS || bForce)
274 d.updateClock.start();
276 for (
int i = 0; i < this->rowCount(); ++i)
279 QString filename = this->item(i, IDX_NAME_COLUMN)->text();
286 QString size = QString(
"%1 / %2").arg(strCurrent, strTotal);
287 item(i, IDX_SIZE_COLUMN)->setText(size);
296 long double ldSpeed = pCalculator->
getSpeed();
301 item(i, IDX_ETA_COLUMN)->setText(strEta);
304 item(i, IDX_ETA_COLUMN)->setText(tr(
"N/A"));
309 item(i, IDX_SPEED_COLUMN)->setText(strSpeed);
312 item(i, IDX_SPEED_COLUMN)->setText(tr(
"N/A"));
318 WadseekerWadsTable::ContextMenu::ContextMenu(QWidget *pParent)
321 this->actionSkipCurrentSite =
new QAction(tr(
"Skip current URL"),
this);
323 this->addAction(this->actionSkipCurrentSite);