#include "DownloadBar.h" #include #include #include #include #include #include #include "DownloadWidget.h" DownloadBar::DownloadBar(QWidget *parent) : QWidget(parent) { auto *outer = new QVBoxLayout(this); outer->setContentsMargins(0, 0, 0, 0); outer->setSpacing(0); header = new QWidget(this); auto *headerLayout = new QHBoxLayout(header); headerLayout->setContentsMargins(6, 2, 6, 2); auto *title = new QLabel("Downloads", header); title->setStyleSheet("font-weight: bold;"); clearButton = new QPushButton("Clear All", header); clearButton->setFlat(true); clearButton->setVisible(false); headerLayout->addWidget(title); headerLayout->addStretch(); headerLayout->addWidget(clearButton); outer->addWidget(header); downloadsLayout = new QVBoxLayout; downloadsLayout->setContentsMargins(0, 0, 0, 0); downloadsLayout->setSpacing(1); outer->addLayout(downloadsLayout); connect(clearButton, &QPushButton::clicked, this, &DownloadBar::clearCompleted); setVisible(false); } void DownloadBar::addDownload(QWebEngineDownloadRequest *download) { if (downloads.isEmpty()) { download->setDownloadDirectory(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)); } auto *widget = new DownloadWidget(download); downloads.append(widget); downloadsLayout->addWidget(widget); connect(widget, &DownloadWidget::removeRequested, this, &DownloadBar::onRemoveDownload); updateVisibility(); } void DownloadBar::onRemoveDownload(DownloadWidget *widget) { downloadsLayout->removeWidget(widget); downloads.removeOne(widget); widget->deleteLater(); updateVisibility(); } void DownloadBar::clearCompleted() { for (auto *w : QList(downloads)) { if (w->isCompleted()) { downloadsLayout->removeWidget(w); downloads.removeOne(w); w->deleteLater(); } } updateVisibility(); } void DownloadBar::updateVisibility() { bool hasAny = !downloads.isEmpty(); setVisible(hasAny); bool hasCompleted = false; for (auto *w : downloads) { if (w->isCompleted()) { hasCompleted = true; break; } } clearButton->setVisible(hasCompleted); }