diff --git a/src/application.cpp b/src/application.cpp index fbbd891..6058e47 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -51,9 +51,6 @@ Application::Application(int &argc, char **argv) // 获取版本特性信息 m_featuresJsonObj = Utils::parseFeatureJsonFile(); m_version = m_featuresJsonObj.value("version").toString(); // 获取版本号 -#if (DTK_VERSION >= DTK_VERSION_CHECK(5, 6, 4, 0)) - initFeatureDisplayDialog(); // 初始化版本特性对话框 -#endif } void Application::handleAboutAction() @@ -64,9 +61,21 @@ void Application::handleAboutAction() } initAboutDialog(); +#if (DTK_VERSION >= DTK_VERSION_CHECK(5, 6, 4, 0)) + initFeatureDisplayDialog(); // 初始化版本特性对话框 +#endif DApplication::handleAboutAction(); } +bool Application::notify(QObject *receiver, QEvent *event) +{ + if (m_mainWindow) { + m_mainWindow->notify(receiver, event); + } + + return DApplication::notify(receiver, event); +} + void Application::checkAppConfigLocation() { QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)); @@ -99,6 +108,12 @@ void Application::setMainWindow(MainWindow *window) { initAboutDialog(); } +#if (DTK_VERSION >= DTK_VERSION_CHECK(5, 6, 4, 0)) + if (featureDisplayDialog() == nullptr || featureDisplayDialog()->parent() != m_mainWindow) + { + initFeatureDisplayDialog(); // 初始化版本特性对话框 + } +#endif } void Application::initAboutDialog() @@ -134,11 +149,6 @@ void Application::initAboutDialog() connect(aboutDialog(), &DAboutDialog::destroyed, this, [=]() { setAboutDialog(nullptr); }); -#if (DTK_VERSION >= DTK_VERSION_CHECK(5, 6, 4, 0)) - connect(aboutDialog(), &DAboutDialog::featureActivated, this, [=]() { - featureDisplayDialog()->show(); - }); -#endif dialog->hide(); } @@ -194,6 +204,11 @@ void Application::initFeatureDisplayDialog() connect(featureDisplayDialog(), &DFeatureDisplayDialog::destroyed, this, [=]() { setFeatureDisplayDialog(nullptr); }); +#if (DTK_VERSION >= DTK_VERSION_CHECK(5, 6, 4, 0)) + connect(aboutDialog(), &DAboutDialog::featureActivated, this, [=]() { + featureDisplayDialog()->show(); + }); +#endif dialog->hide(); } diff --git a/src/application.h b/src/application.h index c58998e..8e58bce 100644 --- a/src/application.h +++ b/src/application.h @@ -15,6 +15,7 @@ class Application : public DApplication public: Application(int &argc, char **argv); void handleAboutAction() override; + bool notify(QObject *receiver, QEvent *event) override; static void checkAppConfigLocation(); diff --git a/src/mainwindow-dtk.cpp b/src/mainwindow-dtk.cpp index 899617a..ca7bb13 100644 --- a/src/mainwindow-dtk.cpp +++ b/src/mainwindow-dtk.cpp @@ -132,6 +132,9 @@ void MainWindow::initTitleBar() searchEdit->setPlaceholderText(tr("Search or enter spk://")); downloadButton = new ProgressButton(ui->titlebar); + downloadButton->setDownloadListWidget(downloadlistwidget); + downloadButton->setFocusPolicy(Qt::FocusPolicy::ClickFocus); + downloadlistwidget->setFocusProxy(downloadButton); QWidget *w_titlebar = new QWidget(ui->titlebar); QHBoxLayout *ly_titlebar = new QHBoxLayout(w_titlebar); @@ -297,14 +300,6 @@ void MainWindow::initConnections() ui->appintopage->setTheme(themeType == DGuiApplicationHelper::DarkType); ui->settingspage->setTheme(themeType == DGuiApplicationHelper::DarkType); }); - connect(downloadButton, &ProgressButton::clicked, [=]() - { - QPoint pos; - pos.setX(downloadButton->mapToGlobal(QPoint(0, 0)).x() + downloadButton->width() / 2 - downloadlistwidget->width() / 2); - pos.setY(downloadButton->mapToGlobal(QPoint(0, 0)).y() + downloadButton->height() + 5); - downloadlistwidget->m_move(pos.x(), pos.y()); - downloadlistwidget->show(); }); - // appintopage按下下载按钮时标题栏下载列表按钮抖动 connect(ui->appintopage, &AppIntoPage::clickedDownloadBtn, [=]() { @@ -421,6 +416,34 @@ void MainWindow::updateUi(int now) switchPage(AppPageApplist); } +void MainWindow::notify(QObject *receiver, QEvent *event) +{ + if (!receiver) { + return; + } + + Dtk::Widget::DStyle *o_ptr = qobject_cast<Dtk::Widget::DStyle *>(receiver); + if (o_ptr) { + return; + } + + if (receiver->inherits("QWidgetWindow")) { + return; + } + + if (event->type() == QEvent::FocusIn) { + QList<QObject *> list = downloadButton->findChildren<QObject *>(QString(), Qt::FindChildrenRecursively); + list << downloadlistwidget->findChildren<QObject *>(QString(), Qt::FindChildrenRecursively); + if (receiver != downloadButton && receiver != downloadlistwidget && !list.contains(receiver)) { + downloadlistwidget->hide(); + } + } else if (event->type() == QEvent::FocusOut) { + if (!downloadlistwidget->isActiveWindow() && !isActiveWindow()) { + downloadlistwidget->hide(); + } + } +} + void MainWindow::on_pushButton_14_clicked() { // Check UOS diff --git a/src/mainwindow-dtk.h b/src/mainwindow-dtk.h index 8b1ca60..251df1f 100644 --- a/src/mainwindow-dtk.h +++ b/src/mainwindow-dtk.h @@ -25,7 +25,7 @@ class MainWindow : public BaseWidgetOpacity public: explicit MainWindow(QWidget *parent = nullptr); - ~MainWindow(); + ~MainWindow() override; void openUrl(const QString &url); @@ -41,6 +41,9 @@ private: void switchPage(int now); void updateUi(int now); +public slots: + void notify(QObject *receiver, QEvent *event); + private slots: //接受来自dbus的url void onGetUrl(const QString &url); diff --git a/src/widgets/common/progressbutton.cpp b/src/widgets/common/progressbutton.cpp index 81ed80d..41a140a 100644 --- a/src/widgets/common/progressbutton.cpp +++ b/src/widgets/common/progressbutton.cpp @@ -1,20 +1,20 @@ #include "progressbutton.h" +#include "widgets/downloadlistwidget.h" + #include <QPainter> #include <QPainterPath> -#include <QRect> -#include <QConicalGradient> -#include <QVariantAnimation> +#include <QMoveEvent> #include <QSvgRenderer> + ProgressButton::ProgressButton(QWidget *parent) : QWidget{parent} { // this->setWindowFlags(Qt::FramelessWindowHint); // this->setAttribute(Qt::WA_TranslucentBackground, true); - setMinimumWidth(36); - setMinimumHeight(36); + setMinimumSize(36, 36); + svgPath = ""; backColor = Qt::transparent; - setMouseTracking(true); connect(this, &ProgressButton::startProcessing, this, &ProgressButton::operationProcessing, Qt::QueuedConnection); } @@ -23,69 +23,6 @@ ProgressButton::~ProgressButton() { } -void ProgressButton::paintEvent(QPaintEvent *event) -{ - QPainter painter(this); - painter.setRenderHint(QPainter::Antialiasing, true); - auto rect = event->rect(); - - if (buttonState == state::normal || buttonState == state::hover) - { - auto radiu = (rect.height() - 6) / 2; - painter.translate(rect.center()); - painter.setPen(Qt::transparent); - painter.setBrush(QColor(buttonState == state::normal ? color : color.darker())); - // painter.drawEllipse(QPoint(0,0),radiu,radiu); - - // radiu -= 3; - painter.setBrush(backColor); - painter.drawEllipse(QPoint(0, 0), radiu, radiu); - - QSvgRenderer m_svgRender; - m_svgRender.load(svgPath); - m_svgRender.render(&painter, QRectF(-radiu / 2, -radiu / 2, radiu, radiu)); - } - else if (buttonState == state::openProgress) - { - painter.translate(rect.center()); - auto radiu = (rect.height() - 6) / 2 - 3; - painter.setBrush(backColor); - painter.setPen(QPen(backColor, 3)); - painter.drawEllipse(QPoint(0, 0), radiu, radiu); - - painter.setPen(QPen(backColor, 3)); - - QSvgRenderer m_svgRender; - m_svgRender.load(svgPath); - m_svgRender.render(&painter, QRectF(-radiu / 2, -radiu / 2, radiu, radiu)); - - QRect rect = QRect(-radiu, -radiu, - radiu * 2, radiu * 2); - - painter.setPen(QPen(color.darker(100), 3)); - auto angle = progress * 360 / 100; - painter.drawArc(rect.adjusted(-3, -3, 3, 3), 90 * 16, -static_cast<int>(angle * 16)); - } - else if (buttonState == state::closeProgress) - { - auto radiu = (rect.height() - 6) / 2; - painter.translate(rect.center()); - painter.setPen(Qt::transparent); - painter.setBrush(QColor(0, 0, 0, 63)); - painter.drawEllipse(QPoint(0, 0), radiu, radiu); - - radiu -= 3; - painter.setBrush(backColor); - painter.drawEllipse(QPoint(0, 0), radiu, radiu); - - painter.setPen(QPen(color, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); - painter.drawLine(QPoint(-radiu / 3, 0), - QPoint(-radiu / 5, radiu / 3)); - painter.drawLine(QPoint(-radiu / 5, radiu / 3), - QPoint(radiu / 4, -radiu / 4)); - } -} - void ProgressButton::setProgress(int progress) { buttonState = state::openProgress; @@ -107,6 +44,11 @@ void ProgressButton::setProgress(int progress) update(); } +void ProgressButton::setDownloadListWidget(DownloadListWidget *widget) +{ + m_downloadListWidget = widget; +} + void ProgressButton::setIcon(QString svgPATH) { svgPath = svgPATH; @@ -125,17 +67,15 @@ void ProgressButton::setColor(QColor color) update(); } -void ProgressButton::leaveEvent(QEvent *event) +void ProgressButton::mousePressEvent(QMouseEvent *event) { - if (buttonState == state::hover) - { - buttonState = state::normal; - update(); - } - QWidget::leaveEvent(event); + m_mouseMoved = false; + m_isDownloadListWidgetVisible = m_downloadListWidget->isVisible(); + m_downloadListWidget->hide(); + QWidget::mousePressEvent(event); } -void ProgressButton::mousePressEvent(QMouseEvent *event) +void ProgressButton::mouseReleaseEvent(QMouseEvent *event) { if (buttonState == state::hover || buttonState == state::normal) { @@ -146,8 +86,109 @@ void ProgressButton::mousePressEvent(QMouseEvent *event) { update(); } - emit clicked(); - QWidget::mousePressEvent(event); + + if (m_mouseMoved) { + return QWidget::mouseReleaseEvent(event); + } + + if (m_isDownloadListWidgetVisible) { + m_downloadListWidget->hide(); + } else { + QPoint pos(this->mapToGlobal(QPoint(0, 0))); + pos += QPoint(width() / 2 - m_downloadListWidget->width() / 2, height() + 5); + m_downloadListWidget->m_move(pos.x(), pos.y()); + m_downloadListWidget->setWindowState(windowState() & Qt::WindowState::WindowActive); + m_downloadListWidget->activateWindow(); + m_downloadListWidget->show(); + m_downloadListWidget->raise(); + } + m_isDownloadListWidgetVisible = m_downloadListWidget->isVisible(); + QWidget::mouseReleaseEvent(event); +} + +void ProgressButton::mouseDoubleClickEvent(QMouseEvent *event) +{ + event->accept(); +} + +void ProgressButton::mouseMoveEvent(QMouseEvent *event) +{ + m_mouseMoved = true; + QWidget::mouseMoveEvent(event); +} + +void ProgressButton::leaveEvent(QEvent *event) +{ + if (buttonState == state::hover) + { + buttonState = state::normal; + update(); + } + QWidget::leaveEvent(event); +} + +void ProgressButton::paintEvent(QPaintEvent *event) +{ + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing, true); + QRect rect = event->rect(); + + if (buttonState == state::normal || buttonState == state::hover) + { + int radius = (rect.height() - 6) / 2; + painter.translate(rect.center()); + painter.setPen(Qt::transparent); + painter.setBrush(QColor(buttonState == state::normal ? color : color.darker())); + // painter.drawEllipse(QPoint(0, 0), radius, radius); + + // radiu -= 3; + painter.setBrush(backColor); + painter.drawEllipse(QPoint(0, 0), radius, radius); + + QSvgRenderer m_svgRender; + m_svgRender.load(svgPath); + m_svgRender.render(&painter, QRectF(-radius / 2, -radius / 2, radius, radius)); + } + else if (buttonState == state::openProgress) + { + painter.translate(rect.center()); + int radius = (rect.height() - 6) / 2 - 3; + painter.setBrush(backColor); + painter.setPen(QPen(backColor, 3)); + painter.drawEllipse(QPoint(0, 0), radius, radius); + + painter.setPen(QPen(backColor, 3)); + + QSvgRenderer m_svgRender; + m_svgRender.load(svgPath); + m_svgRender.render(&painter, QRectF(-radius / 2, -radius / 2, radius, radius)); + + QRect rect = QRect(-radius, -radius, + radius * 2, radius * 2); + + painter.setPen(QPen(color.darker(100), 3)); + qreal angle = progress * 360 / 100 * 1.0; + painter.drawArc(rect.adjusted(-3, -3, 3, 3), 90 * 16, -qIntCast(angle * 16)); + } + else if (buttonState == state::closeProgress) + { + auto radius = (rect.height() - 6) / 2; + painter.translate(rect.center()); + painter.setPen(Qt::transparent); + painter.setBrush(QColor(0, 0, 0, 63)); + painter.drawEllipse(QPoint(0, 0), radius, radius); + + radius -= 3; + painter.setBrush(backColor); + painter.drawEllipse(QPoint(0, 0), radius, radius); + + painter.setPen(QPen(color, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); + painter.drawLine(QPoint(-radius / 3, 0), + QPoint(-radius / 5, radius / 3)); + painter.drawLine(QPoint(-radius / 5, radius / 3), + QPoint(radius / 4, -radius / 4)); + } + QWidget::paintEvent(event); } void ProgressButton::operationProcessing() @@ -156,16 +197,16 @@ void ProgressButton::operationProcessing() const int RADIUS = 60; WaterDrop::WaterDrop(QWidget *parent) - : QWidget(parent), m_waterDropAnimation(nullptr), m_animationRadius(0) + : QWidget(parent) + , m_waterDropAnimation(new QVariantAnimation(this)) + , m_animationRadius(0) { this->setFixedSize(QSize(RADIUS * 2, RADIUS * 2)); // this->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool); // this->setAttribute(Qt::WA_TranslucentBackground); // this->setAttribute(Qt::WA_DeleteOnClose); - m_waterDropAnimation = new QVariantAnimation(this); - // m_waterDropAnimation->setEasingCurve(QEasingCurve(static_cast<QEasingCurve::Type>(QRandomGenerator::global()->bounded(40)))); - connect(m_waterDropAnimation, &QVariantAnimation::finished, this, &WaterDrop::deleteLater); + // m_waterDropAnimation->setEasingCurve(QEasingCurve(static_cast<QEasingCurve::Type>(QRandomGenerator::global()->bounded(40)))); } // 把鼠标点击的点转换为圆心点坐标 @@ -191,8 +232,7 @@ void WaterDrop::paintEvent(QPaintEvent *event) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); - QPen pen; - pen.setColor(QColor(0xffffff80)); + QPen pen(QBrush(QColor("#ffff80")), 5.0); pen.setWidth(5); painter.setPen(pen); painter.drawEllipse(event->rect().center(), m_animationRadius, m_animationRadius); @@ -200,7 +240,7 @@ void WaterDrop::paintEvent(QPaintEvent *event) QWidget::paintEvent(event); } -void WaterDrop::onRadiusChanged(QVariant value) +void WaterDrop::onRadiusChanged(const QVariant &value) { m_animationRadius = value.toInt(); update(); diff --git a/src/widgets/common/progressbutton.h b/src/widgets/common/progressbutton.h index cf18391..1762600 100644 --- a/src/widgets/common/progressbutton.h +++ b/src/widgets/common/progressbutton.h @@ -1,53 +1,62 @@ #ifndef PROGRESSBUTTON_H #define PROGRESSBUTTON_H - -#include <QtWidgets/QWidget> - -#include <QPaintEvent> - +#include <QWidget> #include <QTimer> -#include <QTimerEvent> -#include<QColor> -#include <QList> -#include <QMouseEvent> - +#include <QVariantAnimation> +class DownloadListWidget; class ProgressButton : public QWidget { Q_OBJECT + public: - ProgressButton(QWidget *parent = nullptr); + explicit ProgressButton(QWidget *parent = nullptr); + ~ProgressButton() override; + void setIcon(QString svgPATH); void setBackgroundColor(QColor color); void setColor(QColor color); void setProgress(int progress); - ~ProgressButton(); + + void setDownloadListWidget(DownloadListWidget *widget); + +protected: + void mousePressEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; + void mouseDoubleClickEvent(QMouseEvent *event) override; + void mouseMoveEvent(QMouseEvent *event) override; + void leaveEvent(QEvent *event) override; + void paintEvent(QPaintEvent *event) override; + signals: void startProcessing(); void clicked(); -protected: - void paintEvent(QPaintEvent *event)override; - void leaveEvent(QEvent *event)override; - void mousePressEvent(QMouseEvent *event)override; +private slots: + void operationProcessing(); private: - enum class state + enum state { normal, hover, openProgress, closeProgress, recovery - } - buttonState{state::normal}; + }; + + DownloadListWidget *m_downloadListWidget = nullptr; + + state buttonState{state::normal}; QColor backColor; QColor color; QString svgPath; int widthChangeValue{0}; - void operationProcessing(); int progress{0};//处理百分比 + + bool m_mouseMoved = false; + bool m_isDownloadListWidgetVisible = true; }; class WaterDrop : public QWidget @@ -55,19 +64,19 @@ class WaterDrop : public QWidget Q_OBJECT public: - WaterDrop(QWidget *parent = Q_NULLPTR); + explicit WaterDrop(QWidget *parent = nullptr); void show(); void move(const QPoint &point); -private: +protected: void paintEvent(QPaintEvent *event); - void onRadiusChanged(QVariant value); + +private slots: + void onRadiusChanged(const QVariant &value); private: - class QVariantAnimation* m_waterDropAnimation; + QVariantAnimation *m_waterDropAnimation = nullptr; int m_animationRadius; }; - - #endif // PROGRESSBUTTON_H diff --git a/src/widgets/downloadlistwidget.cpp b/src/widgets/downloadlistwidget.cpp index 9aaa2d4..135a0f9 100644 --- a/src/widgets/downloadlistwidget.cpp +++ b/src/widgets/downloadlistwidget.cpp @@ -81,6 +81,7 @@ void DownloadListWidget::clearItem() // ui->listWidget->vScrollBar->scrollTop(); ui->listWidget->clear(); } + DownloadItem* DownloadListWidget::addItem(QString name, QString fileName, QString pkgName, const QPixmap icon, QString downloadurl) { if (fileName.isEmpty()) @@ -195,21 +196,7 @@ void DownloadListWidget::m_move(int x, int y) move(x, y); return; } -bool DownloadListWidget::eventFilter(QObject *watched, QEvent *event) -{ - if (Q_NULLPTR == watched) - { - return false; - } - if (QEvent::ActivationChange == event->type()) - { - if (QApplication::activeWindow() != this) - { - this->close(); - } - } - return QWidget::eventFilter(watched, event); -} + void DownloadListWidget::mouseMoveEvent(QMouseEvent *event) { setGeometry(m_rect); diff --git a/src/widgets/downloadlistwidget.h b/src/widgets/downloadlistwidget.h index 82f7cf8..dc9c359 100644 --- a/src/widgets/downloadlistwidget.h +++ b/src/widgets/downloadlistwidget.h @@ -27,7 +27,10 @@ public: QList<QUrl> getUrlList(); void m_move(int x, int y); explicit DownloadListWidget(QWidget *parent = nullptr); - ~DownloadListWidget(); + ~DownloadListWidget() override; + +protected: + void mouseMoveEvent(QMouseEvent *event) override; private: int isdownload = false; @@ -48,13 +51,13 @@ private: void clearItem(); QRect m_rect; Ui::DownloadListWidget *ui; -private slots: - bool eventFilter(QObject *, QEvent *); - void mouseMoveEvent(QMouseEvent *event); - void on_pushButton_clicked(); + signals: void downloadFinished(); void downloadProgress(int i); + +private slots: + void on_pushButton_clicked(); }; #endif // DOWNLOADLISTWIDGET_H