From eca5159e12288e1b4ee7d2d52f0475ca6433a0eb Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 24 Aug 2018 15:13:47 +0200 Subject: Downloads: prevent download widget width from exceeding download list width Downloads: add open button --- lib/downloads/downloadsform.ui | 15 +++++++++- lib/downloads/downloadswidget.cpp | 15 ++++------ lib/downloads/downloadswidget.h | 2 +- lib/downloads/widgets/downloaditemform.ui | 10 +++++-- lib/downloads/widgets/downloaditemwidget.cpp | 44 +++++++++++++++++----------- lib/downloads/widgets/downloaditemwidget.h | 2 -- lib/downloads/widgets/elidedlabel.cpp | 1 + 7 files changed, 55 insertions(+), 34 deletions(-) (limited to 'lib/downloads') diff --git a/lib/downloads/downloadsform.ui b/lib/downloads/downloadsform.ui index 43a0d49..9ca3459 100644 --- a/lib/downloads/downloadsform.ui +++ b/lib/downloads/downloadsform.ui @@ -15,7 +15,20 @@ - + + + Qt::ScrollBarAlwaysOff + + + QAbstractItemView::NoEditTriggers + + + false + + + 4 + + diff --git a/lib/downloads/downloadswidget.cpp b/lib/downloads/downloadswidget.cpp index 2a8fe56..340ef93 100644 --- a/lib/downloads/downloadswidget.cpp +++ b/lib/downloads/downloadswidget.cpp @@ -14,14 +14,13 @@ DownloadsWidget::DownloadsWidget(const QString &downloadPath, QWidget *parent) : QDialog(parent) , ui(new Ui::DownloadDialog) + , m_downloadPath(downloadPath) { // make sure this dialog does not get deleted on close setAttribute(Qt::WA_DeleteOnClose, false); setWindowTitle(tr("Downloads")); ui->setupUi(this); - - m_downloadPath = downloadPath; } DownloadsWidget::~DownloadsWidget() @@ -31,9 +30,7 @@ DownloadsWidget::~DownloadsWidget() void DownloadsWidget::addDownload(QWebEngineDownloadItem *item) { - QString filepath = QFileDialog::getSaveFileName(this, - tr("Save"), - m_downloadPath + "/" + QFileInfo(item->path()).fileName()); + const QString filepath = QFileDialog::getSaveFileName(this, tr("Save File"), m_downloadPath + "/" + QFileInfo(item->path()).fileName()); if(filepath.isEmpty()) { // user cancelled the save dialog @@ -43,15 +40,13 @@ void DownloadsWidget::addDownload(QWebEngineDownloadItem *item) item->setPath(filepath); - auto *listItem = new QListWidgetItem(); - int rowIndex = ui->listWidget->count(); - ui->listWidget->insertItem(rowIndex, listItem); + auto *listItem = new QListWidgetItem(ui->listWidget); auto *form = new DownloadItemWidget(item, this); ui->listWidget->setItemWidget(listItem, form); - listItem->setSizeHint(form->sizeHint()); + listItem->setSizeHint(QSize(listItem->sizeHint().width(), form->size().height())); item->accept(); - ui->listWidget->setCurrentRow(rowIndex); + ui->listWidget->setCurrentItem(listItem); } diff --git a/lib/downloads/downloadswidget.h b/lib/downloads/downloadswidget.h index dcf13d1..5dfde3e 100644 --- a/lib/downloads/downloadswidget.h +++ b/lib/downloads/downloadswidget.h @@ -30,7 +30,7 @@ public slots: private: Ui::DownloadDialog *ui; - QString m_downloadPath; + const QString m_downloadPath; }; #endif // SMOLBOTE_DOWNLOADDIALOG_H diff --git a/lib/downloads/widgets/downloaditemform.ui b/lib/downloads/widgets/downloaditemform.ui index 9b4ba12..7944084 100644 --- a/lib/downloads/widgets/downloaditemform.ui +++ b/lib/downloads/widgets/downloaditemform.ui @@ -66,6 +66,13 @@ + + + + Open + + + @@ -73,9 +80,6 @@ path_label - - true - diff --git a/lib/downloads/widgets/downloaditemwidget.cpp b/lib/downloads/widgets/downloaditemwidget.cpp index 3fe2ebd..927c58b 100644 --- a/lib/downloads/widgets/downloaditemwidget.cpp +++ b/lib/downloads/widgets/downloaditemwidget.cpp @@ -10,6 +10,24 @@ #include "ui_downloaditemform.h" #include #include +#include + +inline QString sizeString(int size) +{ + if(size < 1024) { + return QString("%1 bytes").arg(size); + } + // KiB + if(size < 1024 * 1024) { + return QString("%1 kB").arg(size / 1024); + } + // MiB + if(size < 1024 * 1024 * 1024) { + return QString("%1 MB").arg(size / (1024 * 1024)); + } + // GiB + return QString("%1 GB").arg(size / (1024 * 1024 * 1024)); +} DownloadItemWidget::DownloadItemWidget(QWebEngineDownloadItem *item, QWidget *parent) : QWidget(parent) @@ -31,6 +49,7 @@ DownloadItemWidget::DownloadItemWidget(QWebEngineDownloadItem *item, QWidget *pa ui->url_label->setText(item->url().toString()); ui->path_label->setText(item->path()); + this->updateState(item->state()); connect(item, &QWebEngineDownloadItem::stateChanged, this, &DownloadItemWidget::updateState); connect(item, &QWebEngineDownloadItem::downloadProgress, this, &DownloadItemWidget::updateProgress); connect(item, &QWebEngineDownloadItem::finished, this, &DownloadItemWidget::updateFinished); @@ -43,6 +62,9 @@ DownloadItemWidget::DownloadItemWidget(QWebEngineDownloadItem *item, QWidget *pa item->resume(); } }); + connect(ui->open_toolButton, &QToolButton::clicked, item, [item]() { + QDesktopServices::openUrl(QUrl::fromLocalFile(item->path())); + }); } DownloadItemWidget::~DownloadItemWidget() @@ -50,23 +72,6 @@ DownloadItemWidget::~DownloadItemWidget() delete ui; } -QString DownloadItemWidget::sizeString(int size) const -{ - if(size < 1024) { - return QString("%1 bytes").arg(size); - } - // KiB - if(size < 1024 * 1024) { - return QString("%1 kB").arg(size / 1024); - } - // MiB - if(size < 1024 * 1024 * 1024) { - return QString("%1 MB").arg(size / (1024 * 1024)); - } - // GiB - return QString("%1 GB").arg(size / (1024 * 1024 * 1024)); -} - void DownloadItemWidget::updateState(QWebEngineDownloadItem::DownloadState state) { switch(state) { @@ -74,26 +79,31 @@ void DownloadItemWidget::updateState(QWebEngineDownloadItem::DownloadState state ui->status_label->setText(tr("Requested")); ui->pause_toolButton->setEnabled(true); ui->abort_toolButton->setEnabled(true); + ui->open_toolButton->setEnabled(false); break; case QWebEngineDownloadItem::DownloadInProgress: ui->status_label->setText(tr("In progress")); ui->pause_toolButton->setEnabled(true); ui->abort_toolButton->setEnabled(true); + ui->open_toolButton->setEnabled(false); break; case QWebEngineDownloadItem::DownloadCompleted: ui->status_label->setText(tr("Completed")); ui->pause_toolButton->setEnabled(false); ui->abort_toolButton->setEnabled(false); + ui->open_toolButton->setEnabled(true); break; case QWebEngineDownloadItem::DownloadCancelled: ui->status_label->setText(tr("Cancelled")); ui->pause_toolButton->setEnabled(false); ui->abort_toolButton->setEnabled(false); + ui->open_toolButton->setEnabled(false); break; case QWebEngineDownloadItem::DownloadInterrupted: ui->status_label->setText(tr("Interrupted")); ui->pause_toolButton->setEnabled(false); ui->abort_toolButton->setEnabled(false); + ui->open_toolButton->setEnabled(false); break; default: break; diff --git a/lib/downloads/widgets/downloaditemwidget.h b/lib/downloads/widgets/downloaditemwidget.h index 29a02d6..db4dc8e 100644 --- a/lib/downloads/widgets/downloaditemwidget.h +++ b/lib/downloads/widgets/downloaditemwidget.h @@ -25,8 +25,6 @@ public: explicit DownloadItemWidget(QWebEngineDownloadItem *item, QWidget *parent = 0); ~DownloadItemWidget() override; - QString sizeString(int size) const; - private slots: void updateState(QWebEngineDownloadItem::DownloadState state); void updateProgress(qint64 value, qint64 total); diff --git a/lib/downloads/widgets/elidedlabel.cpp b/lib/downloads/widgets/elidedlabel.cpp index da81407..b6761ab 100644 --- a/lib/downloads/widgets/elidedlabel.cpp +++ b/lib/downloads/widgets/elidedlabel.cpp @@ -14,6 +14,7 @@ ElidedLabel::ElidedLabel(QWidget *parent) : QLabel(parent) { + setWordWrap(true); } void ElidedLabel::setText(const QString &text) -- cgit v1.2.1