From 686f58c997dce499ab90c7ba035e2c387ab96ec4 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 13 May 2018 16:58:53 +0200 Subject: Download widget item tooltip Simplified ElidedLabel --- lib/downloads/downloadswidget.cpp | 31 +-------- lib/downloads/downloadswidget.h | 3 - lib/downloads/widgets/downloaditemform.ui | 99 ++++++++++++++-------------- lib/downloads/widgets/downloaditemwidget.cpp | 44 +++++++------ lib/downloads/widgets/downloaditemwidget.h | 6 +- lib/downloads/widgets/elidedlabel.cpp | 72 ++++---------------- lib/downloads/widgets/elidedlabel.h | 65 ++---------------- 7 files changed, 93 insertions(+), 227 deletions(-) (limited to 'lib/downloads') diff --git a/lib/downloads/downloadswidget.cpp b/lib/downloads/downloadswidget.cpp index c1ffb31..2a8fe56 100644 --- a/lib/downloads/downloadswidget.cpp +++ b/lib/downloads/downloadswidget.cpp @@ -22,22 +22,6 @@ DownloadsWidget::DownloadsWidget(const QString &downloadPath, QWidget *parent) ui->setupUi(this); m_downloadPath = downloadPath; - - connect(ui->listWidget, &QListWidget::currentItemChanged, this, [this](QListWidgetItem *current, QListWidgetItem *previous) { - DownloadItemWidget *currentWidget = qobject_cast(ui->listWidget->itemWidget(current)); - currentWidget->showDetails(); - currentWidget->setFixedWidth(ui->listWidget->viewport()->width()); - currentWidget->adjustSize(); - current->setSizeHint(currentWidget->size()); - - DownloadItemWidget *previousWidget = qobject_cast(ui->listWidget->itemWidget(previous)); - if(previousWidget != nullptr) { - previousWidget->hideDetails(); - previousWidget->setFixedWidth(ui->listWidget->viewport()->width()); - previousWidget->adjustSize(); - previous->setSizeHint(previousWidget->size()); - } - }); } DownloadsWidget::~DownloadsWidget() @@ -47,8 +31,6 @@ DownloadsWidget::~DownloadsWidget() void DownloadsWidget::addDownload(QWebEngineDownloadItem *item) { - this->show(); - QString filepath = QFileDialog::getSaveFileName(this, tr("Save"), m_downloadPath + "/" + QFileInfo(item->path()).fileName()); @@ -67,20 +49,9 @@ void DownloadsWidget::addDownload(QWebEngineDownloadItem *item) auto *form = new DownloadItemWidget(item, this); ui->listWidget->setItemWidget(listItem, form); + listItem->setSizeHint(form->sizeHint()); item->accept(); ui->listWidget->setCurrentRow(rowIndex); } - -void DownloadsWidget::resizeEvent(QResizeEvent *event) -{ - QWidget::resizeEvent(event); - - for(int i = 0; i < ui->listWidget->count(); ++i) { - QWidget *w = ui->listWidget->itemWidget(ui->listWidget->item(i)); - w->setFixedWidth(ui->listWidget->viewport()->width()); - w->adjustSize(); - ui->listWidget->item(i)->setSizeHint(w->size()); - } -} diff --git a/lib/downloads/downloadswidget.h b/lib/downloads/downloadswidget.h index f7c25bb..dcf13d1 100644 --- a/lib/downloads/downloadswidget.h +++ b/lib/downloads/downloadswidget.h @@ -28,9 +28,6 @@ public: public slots: void addDownload(QWebEngineDownloadItem *item); -protected: - void resizeEvent(QResizeEvent *event) override; - private: Ui::DownloadDialog *ui; QString m_downloadPath; diff --git a/lib/downloads/widgets/downloaditemform.ui b/lib/downloads/widgets/downloaditemform.ui index c8a4604..9b4ba12 100644 --- a/lib/downloads/widgets/downloaditemform.ui +++ b/lib/downloads/widgets/downloaditemform.ui @@ -7,20 +7,23 @@ 0 0 500 - 128 + 95 - - - 0 - 0 - - - - Form - + 2 + + + 0 + + + 0 + + + 0 + + 0 @@ -38,47 +41,41 @@ - - - - - - - - Pause - - - true - - - - - - - Abort - - - - - - - status_label - - - - - - - - - path_label - - - true - - - - + + + + + Pause + + + true + + + + + + + Abort + + + + + + + status_label + + + + + + + + + path_label + + + true + diff --git a/lib/downloads/widgets/downloaditemwidget.cpp b/lib/downloads/widgets/downloaditemwidget.cpp index 2790c5c..660d8a5 100644 --- a/lib/downloads/widgets/downloaditemwidget.cpp +++ b/lib/downloads/widgets/downloaditemwidget.cpp @@ -15,8 +15,8 @@ DownloadItemWidget::DownloadItemWidget(QWebEngineDownloadItem *item, QWidget *pa : QWidget(parent) , ui(new Ui::DownloadItemForm) { - m_item = item; - + Q_CHECK_PTR(item); + this->item = item; ui->setupUi(this); { // pause/resume icons @@ -28,9 +28,7 @@ DownloadItemWidget::DownloadItemWidget(QWebEngineDownloadItem *item, QWidget *pa ui->abort_toolButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop)); } - ui->url_label->setContent(item->url().toString()); - ui->detailsWidget->hide(); - + ui->url_label->setText(item->url().toString()); ui->path_label->setText(item->path()); connect(item, &QWebEngineDownloadItem::stateChanged, this, &DownloadItemWidget::updateState); @@ -52,21 +50,6 @@ DownloadItemWidget::~DownloadItemWidget() delete ui; } -void DownloadItemWidget::showDetails() -{ - ui->detailsWidget->show(); -} - -void DownloadItemWidget::hideDetails() -{ - ui->detailsWidget->hide(); -} - -QWebEngineDownloadItem *DownloadItemWidget::item() const -{ - return m_item; -} - QString DownloadItemWidget::sizeString(int size) const { if(size < 1024) { @@ -127,4 +110,25 @@ void DownloadItemWidget::updateProgress(qint64 value, qint64 total) void DownloadItemWidget::updateFinished() { ui->progressBar->setValue(ui->progressBar->maximum()); + QString tooltip = "" + "" + "" + "" + "" + "" + "" + "" + "" + "
PreviewInformation
%1" + "

URL: %2

" + "

Path: %3

" + "

MIME Type: %4

" + "

Size: %5

" + "
"; + if(item->mimeType().startsWith("image")) + tooltip = tooltip.arg("not available"); + else + tooltip = tooltip.arg("not available"); + tooltip = tooltip.arg(item->url().toString(), item->path(), item->mimeType(), sizeString(item->totalBytes())); + setToolTip(tooltip); } diff --git a/lib/downloads/widgets/downloaditemwidget.h b/lib/downloads/widgets/downloaditemwidget.h index a772769..29a02d6 100644 --- a/lib/downloads/widgets/downloaditemwidget.h +++ b/lib/downloads/widgets/downloaditemwidget.h @@ -25,10 +25,6 @@ public: explicit DownloadItemWidget(QWebEngineDownloadItem *item, QWidget *parent = 0); ~DownloadItemWidget() override; - void showDetails(); - void hideDetails(); - - QWebEngineDownloadItem *item() const; QString sizeString(int size) const; private slots: @@ -38,7 +34,7 @@ private slots: private: Ui::DownloadItemForm *ui; - QWebEngineDownloadItem *m_item; + QWebEngineDownloadItem *item = nullptr; }; #endif // SMOLBOTE_DOWNLOADITEMFORM_H diff --git a/lib/downloads/widgets/elidedlabel.cpp b/lib/downloads/widgets/elidedlabel.cpp index f5db921..d898da0 100644 --- a/lib/downloads/widgets/elidedlabel.cpp +++ b/lib/downloads/widgets/elidedlabel.cpp @@ -1,58 +1,9 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ /* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original * location: https://neueland.iserlohn-fortress.net/smolbote.hg * - * SPDX-License-Identifier: BSD-3-Clause + * SPDX-License-Identifier: GPL-3.0 */ #include "elidedlabel.h" @@ -62,23 +13,26 @@ ElidedLabel::ElidedLabel(QWidget *parent) : QLabel(parent) - , elided(false) - , content("elided_label") { setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); } -void ElidedLabel::setContent(const QString &newText) +void ElidedLabel::setText(const QString &text) { - content = newText; - setText(newText); + setToolTip(text); + content = text; + elideText(); } -void ElidedLabel::resizeEvent(QResizeEvent *event) +void ElidedLabel::elideText() { - QLabel::resizeEvent(event); - QFontMetrics font = this->fontMetrics(); QString elidedLine = font.elidedText(content, Qt::ElideRight, width()); - setText(elidedLine); + QLabel::setText(elidedLine); +} + +void ElidedLabel::resizeEvent(QResizeEvent *event) +{ + QLabel::resizeEvent(event); + elideText(); } diff --git a/lib/downloads/widgets/elidedlabel.h b/lib/downloads/widgets/elidedlabel.h index 96d2609..3415720 100644 --- a/lib/downloads/widgets/elidedlabel.h +++ b/lib/downloads/widgets/elidedlabel.h @@ -1,62 +1,13 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ /* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original * location: https://neueland.iserlohn-fortress.net/smolbote.hg * - * SPDX-License-Identifier: BSD-3-Clause + * SPDX-License-Identifier: GPL-3.0 */ -#ifndef ELIDEDLABEL_H -#define ELIDEDLABEL_H +#ifndef SMOLBOTE_ELIDEDLABEL_H +#define SMOLBOTE_ELIDEDLABEL_H #include @@ -66,22 +17,18 @@ class ElidedLabel : public QLabel public: explicit ElidedLabel(QWidget *parent = nullptr); - void setContent(const QString &text); const QString &text() const { return content; } - bool isElided() const - { - return elided; - } + void setText(const QString &text); protected: + void elideText(); void resizeEvent(QResizeEvent *event) override; private: - bool elided; QString content; }; -#endif // ELIDEDLABEL_H +#endif // SMOLBOTE_ELIDEDLABEL_H -- cgit v1.2.1