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->setAlignment(Qt::AlignCenter);
62 setItem(rowIndex, IDX_NAME_COLUMN,
new QTableWidgetItem(filename));
63 setItem(rowIndex, IDX_URL_COLUMN,
new QTableWidgetItem());
64 setCellWidget(rowIndex, IDX_PROGRESS_COLUMN, pBar);
65 setItem(rowIndex, IDX_SPEED_COLUMN,
new QTableWidgetItem());
66 setItem(rowIndex, IDX_ETA_COLUMN,
new QTableWidgetItem(tr(
"N/A")));
67 setItem(rowIndex, IDX_SIZE_COLUMN,
new QTableWidgetItem(tr(
"N/A")));
72 d.speedCalculators.insert(filename, pCalculator);
74 setSortingEnabled(
true);
80 auto menu =
new ContextMenu(
this);
81 QPoint displayPoint = this->viewport()->mapToGlobal(cursorPosition);
82 menu->move(displayPoint);
85 menu->actionSkipCurrentSite->setEnabled(
false);
92 if (row < 0 || row >= this->rowCount())
95 QString fileName = fileNameAtRow(row);
96 return d.speedCalculators[fileName]->expectedDataSize();
99 QString WadseekerWadsTable::fileNameAtRow(
int row)
const 101 if (row < 0 || row >= this->rowCount())
104 return item(row, IDX_NAME_COLUMN)->text();
107 int WadseekerWadsTable::findFileRow(
const QString &filename)
109 QList<QTableWidgetItem *> list = findItems(filename, Qt::MatchFixedString);
111 return list.first()->row();
116 void WadseekerWadsTable::setFileDownloadFinished(
const ModFile &filename)
118 int row = findFileRow(filename.fileName());
124 item(row, IDX_ETA_COLUMN)->setText(tr(
"N/A"));
125 item(row, IDX_SPEED_COLUMN)->setText(tr(
"N/A"));
127 item(row, IDX_URL_COLUMN)->setText(tr(
"Awaiting URLs"));
128 item(row, IDX_URL_COLUMN)->setToolTip(tr(
"Awaiting URLs"));
130 const bool FORCE =
true;
131 updateDataInfoValues(FORCE);
135 void WadseekerWadsTable::setFileFailed(
const ModFile &filename)
137 int row = findFileRow(filename.fileName());
141 item(row, IDX_NAME_COLUMN)->setIcon(QIcon(
":/icons/x.png"));
143 item(row, IDX_URL_COLUMN)->setText(
"");
147 void WadseekerWadsTable::setFileProgress(
const ModFile &filename, qint64 current, qint64 total)
149 int row = findFileRow(filename.fileName());
153 auto pBar = (QProgressBar *) this->cellWidget(row, IDX_PROGRESS_COLUMN);
154 pBar->setMaximum(total);
155 pBar->setValue(current);
158 SpeedCalculator *pCalculator = d.speedCalculators.value(filename.fileName());
162 const bool FORCE =
true;
163 updateDataInfoValues(!FORCE);
167 void WadseekerWadsTable::setFileSuccessful(
const ModFile &filename)
169 int row = findFileRow(filename.fileName());
174 auto pBar = (QProgressBar *) this->cellWidget(row, IDX_PROGRESS_COLUMN);
175 SpeedCalculator *pCalculator = d.speedCalculators[filename.fileName()];
176 if (pCalculator->expectedDataSize() == 0)
181 pBar->setMaximum(pCalculator->expectedDataSize());
182 pBar->setValue(pCalculator->expectedDataSize());
184 item(row, IDX_NAME_COLUMN)->setIcon(QIcon(
":/icons/ok.png"));
185 item(row, IDX_URL_COLUMN)->setText(
"");
187 item(row, IDX_ETA_COLUMN)->setText(tr(
"Done"));
188 item(row, IDX_SPEED_COLUMN)->setText(
"");
190 const bool FORCE =
true;
191 updateDataInfoValues(FORCE);
199 int row = findFileRow(filename.fileName());
203 QTableWidgetItem *pItem = this->item(row, IDX_URL_COLUMN);
204 pItem->setText(url.toString());
205 pItem->setToolTip(url.toString());
207 auto pBar = (QProgressBar *) this->cellWidget(row, IDX_PROGRESS_COLUMN);
211 SpeedCalculator *pCalculator = d.speedCalculators[filename.fileName()];
212 pCalculator->
start();
216 void WadseekerWadsTable::showEvent(QShowEvent *pEvent)
219 if (!d.bAlreadyShownOnce)
223 QHeaderView *pHeader = horizontalHeader();
226 pHeader->setSectionResizeMode(IDX_URL_COLUMN, QHeaderView::Stretch);
228 pHeader->resizeSection(IDX_NAME_COLUMN, 140);
229 pHeader->resizeSection(IDX_PROGRESS_COLUMN, 85);
230 pHeader->resizeSection(IDX_ETA_COLUMN, 85);
231 pHeader->resizeSection(IDX_SIZE_COLUMN, 150);
232 pHeader->resizeSection(IDX_SPEED_COLUMN, 85);
238 double sumDownloadPercentages = 0.0;
240 if (this->rowCount() == 0)
243 for (
int i = 0; i < this->rowCount(); ++i)
245 const auto pBar = (
const QProgressBar *) this->cellWidget(i, IDX_PROGRESS_COLUMN);
248 int val = pBar->value();
249 int max = pBar->maximum();
253 double curPercentage = (double) val / (
double) max;
254 sumDownloadPercentages += curPercentage;
260 sumDownloadPercentages *= 100.0;
261 double averageDownloadPercentages = sumDownloadPercentages / (double) this->rowCount();
263 return averageDownloadPercentages;
266 void WadseekerWadsTable::updateDataInfoValues(
bool bForce)
269 if (d.updateClock.elapsed() > UPDATE_INTERVAL_MS || bForce)
271 d.updateClock.start();
273 for (
int i = 0; i < this->rowCount(); ++i)
276 QString filename = this->item(i, IDX_NAME_COLUMN)->text();
283 QString size = QString(
"%1 / %2").arg(strCurrent, strTotal);
284 item(i, IDX_SIZE_COLUMN)->setText(size);
293 long double ldSpeed = pCalculator->
getSpeed();
298 item(i, IDX_ETA_COLUMN)->setText(strEta);
301 item(i, IDX_ETA_COLUMN)->setText(tr(
"N/A"));
306 item(i, IDX_SPEED_COLUMN)->setText(strSpeed);
309 item(i, IDX_SPEED_COLUMN)->setText(tr(
"N/A"));
315 WadseekerWadsTable::ContextMenu::ContextMenu(QWidget *pParent)
318 this->actionSkipCurrentSite =
new QAction(tr(
"Skip current URL"),
this);
320 this->addAction(this->actionSkipCurrentSite);
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.
void setFileUrl(const ModFile &filename, const QUrl &url)
Fired when new URL starts for specified file.
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.