Skip to content

Commit

Permalink
feat: add DToast
Browse files Browse the repository at this point in the history
Change-Id: Ib214763e35d801aba194cbd8a99b1ee9c1f47c80
  • Loading branch information
Iceyer committed Jan 23, 2018
1 parent ddddf0e commit c4d3f48
Show file tree
Hide file tree
Showing 10 changed files with 387 additions and 16 deletions.
25 changes: 18 additions & 7 deletions examples/dwidget-examples/collections/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
#include <QFontDatabase>
#include <QTextCodec>
#include <QDebug>
#include <QTemporaryFile>

#include <DSettingsOption>
#include <DSettings>

#include "qsettingbackend.h"
#include "dsettingsdialog.h"
#include "dsettingsoption.h"
#include "dsettings.h"

#include "dslider.h"
#include "dthememanager.h"
Expand All @@ -43,6 +44,7 @@
#include "cameraform.h"
#include "graphicseffecttab.h"
#include "simplelistviewtab.h"
#include "dtoast.h"

DTK_USE_NAMESPACE
DWIDGET_USE_NAMESPACE
Expand Down Expand Up @@ -114,10 +116,19 @@ MainWindow::MainWindow(QWidget *parent)
| Qt::WindowMaximizeButtonHint
| Qt::WindowSystemMenuHint);
}
}

#include <QTemporaryFile>
#include <qsettingbackend.h>
auto toast = new DToast(this);
toast->setText("Successfully close window");
toast->setIcon(":/images/light/images/window/close_press.svg");
QTimer::singleShot(1000, [ = ]() {
toast->pop();
toast->move((width() - toast->width()) / 2,
(height() - toast->height()) / 2);
});
QTimer::singleShot(4000, [ = ]() {
toast->pop();
});
}

void MainWindow::menuItemInvoked(QAction *action)
{
Expand Down Expand Up @@ -196,7 +207,7 @@ void MainWindow::initTabWidget()
GraphicsEffectTab *effectTab = new GraphicsEffectTab(this);

SimpleListViewTab *simplelistviewTab = new SimpleListViewTab(this);

m_mainTab->addTab(widgetsTab, "Widgets");
m_mainTab->addTab(effectTab, "GraphicsEffect");
m_mainTab->addTab(comboBoxTab, "ComboBox");
Expand Down
13 changes: 9 additions & 4 deletions src/widgets/dgraphicsgloweffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ DGraphicsGlowEffect::DGraphicsGlowEffect(QObject *parent) :
}

QT_BEGIN_NAMESPACE
extern Q_WIDGETS_EXPORT void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bool alphaOnly, int transposed = 0 );
extern Q_WIDGETS_EXPORT void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bool alphaOnly, int transposed = 0);
QT_END_NAMESPACE

void DGraphicsGlowEffect::draw(QPainter* painter)
void DGraphicsGlowEffect::draw(QPainter *painter)
{
// if nothing to show outside the item, just draw source
if ((blurRadius() + distance()) <= 0) {
Expand All @@ -46,8 +46,12 @@ void DGraphicsGlowEffect::draw(QPainter* painter)
const QPixmap sourcePx = sourcePixmap(Qt::DeviceCoordinates, &offset, mode);

// return if no source
if (sourcePx.isNull())
if (sourcePx.isNull()) {
return;
}

qreal restoreOpacity = painter->opacity();
painter->setOpacity(m_opacity);

// save world transform
QTransform restoreTransform = painter->worldTransform();
Expand Down Expand Up @@ -87,9 +91,10 @@ void DGraphicsGlowEffect::draw(QPainter* painter)

// restore world transform
painter->setWorldTransform(restoreTransform);
painter->setOpacity(restoreOpacity);
}

QRectF DGraphicsGlowEffect::boundingRectFor(const QRectF& rect) const
QRectF DGraphicsGlowEffect::boundingRectFor(const QRectF &rect) const
{
qreal delta = blurRadius() + distance();
return rect.united(rect.adjusted(-delta - xOffset(), -delta - yOffset(), delta - xOffset(), delta - yOffset()));
Expand Down
11 changes: 8 additions & 3 deletions src/widgets/dgraphicsgloweffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class LIBDTKWIDGETSHARED_EXPORT DGraphicsGlowEffect : public QGraphicsEffect
public:
explicit DGraphicsGlowEffect(QObject *parent = 0);

void draw(QPainter* painter);
QRectF boundingRectFor(const QRectF& rect) const;
void draw(QPainter *painter);
QRectF boundingRectFor(const QRectF &rect) const;

inline void setOffset(qreal dx, qreal dy) {m_xOffset = dx; m_yOffset = dy;}

Expand All @@ -49,10 +49,15 @@ class LIBDTKWIDGETSHARED_EXPORT DGraphicsGlowEffect : public QGraphicsEffect
inline void setBlurRadius(qreal blurRadius) { m_blurRadius = blurRadius; updateBoundingRect(); }
inline qreal blurRadius() const { return m_blurRadius; }

inline void setColor(const QColor& color) { m_color = color; }
inline void setColor(const QColor &color) { m_color = color; }
inline QColor color() const { return m_color; }

// TODO: refactor with d-pointer;
inline qreal opacity() const { return m_opacity; }
inline void setOpacity(qreal opacity) { m_opacity = opacity; }

private:
qreal m_opacity = 1.0;
qreal m_xOffset;
qreal m_yOffset;
qreal m_distance;
Expand Down
260 changes: 260 additions & 0 deletions src/widgets/dtoast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
/*
* Copyright (C) 2016 ~ 2017 Deepin Technology Co., Ltd.
*
* Author: Iceyer <me@iceyer.net>
*
* Maintainer: Iceyer <me@iceyer.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dtoast.h"

#include <DObjectPrivate>


#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
#include <QHBoxLayout>
#include <QLabel>
#include <QIcon>

#include "dthememanager.h"
#include "dgraphicsgloweffect.h"
#include "dhidpihelper.h"

DWIDGET_BEGIN_NAMESPACE

class DToastPrivate: public DTK_CORE_NAMESPACE::DObjectPrivate
{
public:
DToastPrivate(DToast *qq);

QBrush background = Qt::white;
int radius = 4;
QColor borderColor = QColor(0, 0, 0, 255 / 10);
QIcon icon;
QLabel *iconLabel = Q_NULLPTR;
QLabel *textLabel = Q_NULLPTR;

QPropertyAnimation *animation = Q_NULLPTR;
DGraphicsGlowEffect *effect = Q_NULLPTR;

void initUI();
private:
D_DECLARE_PUBLIC(DToast)
};

DToast::DToast(QWidget *parent) :
QFrame(parent), DObject(*new DToastPrivate(this))
{
D_D(DToast);
D_THEME_INIT_WIDGET(DToast);
d->initUI();
}

DToast::~DToast()
{

}

QString DToast::text() const
{
D_DC(DToast);
return d->textLabel->text();
}

QIcon DToast::icon() const
{
D_DC(DToast);
return d->icon;
}

qreal DToast::opacity() const
{
D_DC(DToast);
return d->effect->opacity();
}

QColor Dtk::Widget::DToast::borderColor() const
{
D_DC(DToast);
return d->borderColor;
}

QBrush DToast::background() const
{
D_DC(DToast);
return d->background;
}

int DToast::radius() const
{
D_DC(DToast);
return d->radius;

}

void DToast::setText(QString text)
{
D_D(DToast);
d->textLabel->setText(text);
}

void DToast::setIcon(QString iconfile)
{
D_D(DToast);
d->icon = QIcon(iconfile);
d->iconLabel->setPixmap(DHiDPIHelper::loadNxPixmap(iconfile));
}

void DToast::setIcon(QIcon icon, QSize defaultSize)
{
D_D(DToast);
d->icon = icon;
d->iconLabel->setPixmap(d->icon.pixmap(icon.actualSize(defaultSize)));
}

void DToast::paintEvent(QPaintEvent *)
{
D_D(DToast);

QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing);

// draw border outer
auto outer = true;
auto radius = d->radius;
auto penWidthf = 1.0;
auto background = d->background;
auto borderColor = d->borderColor;
auto margin = 2.0;
auto shadowMargins = QMarginsF(margin, margin, margin, margin);

// draw background
auto backgroundRect = QRectF(rect()).marginsRemoved(shadowMargins);
QPainterPath backgroundPath;
backgroundPath.addRoundedRect(backgroundRect, radius, radius);
painter.fillPath(backgroundPath, background);

// draw border
QPainterPath borderPath;
QRectF borderRect = QRectF(rect());
auto borderRadius = d->radius;//radius;
QMarginsF borderMargin(penWidthf / 2, penWidthf / 2, penWidthf / 2, penWidthf / 2);
if (outer) {
borderRadius += penWidthf / 2;
borderRect = borderRect.marginsAdded(borderMargin).marginsRemoved(shadowMargins);
} else {
borderRadius -= penWidthf / 2;
borderRect = borderRect.marginsRemoved(borderMargin).marginsRemoved(shadowMargins);
}
borderPath.addRoundedRect(borderRect, borderRadius, borderRadius);
QPen borderPen(borderColor);
borderPen.setWidthF(penWidthf);
painter.strokePath(borderPath, borderPen);
}

void DToast::setBorderColor(QColor borderColor)
{
D_D(DToast);
d->borderColor = borderColor;
}

void DToast::setOpacity(qreal opacity)
{
D_D(DToast);
d->effect->setOpacity(opacity);
update();
}

void DToast::setBackground(QBrush background)
{
D_D(DToast);
d->background = background;
}

void DToast::setRadius(int radius)
{
D_D(DToast);
d->radius = radius;
}

void DToast::pop()
{
Q_D(DToast);

adjustSize();
show();

if (d->animation) {
return;
}

d->animation = new QPropertyAnimation(this, "opacity");
d->animation->setDuration(2000);
d->animation->setStartValue(0);
d->animation->setKeyValueAt(0.4, 1.0);
d->animation->setKeyValueAt(0.8, 1.0);
d->animation->setEndValue(0);
d->animation->start();
d->animation->connect(d->animation, &QPropertyAnimation::finished,
this, [ = ]() {
hide();
d->animation->deleteLater();
d->animation = Q_NULLPTR;
});
}

void DToast::pack()
{
Q_D(DToast);
hide();
if (d->animation) {
d->animation->stop();
d->animation->deleteLater();
d->animation = Q_NULLPTR;
}
}

DToastPrivate::DToastPrivate(DToast *qq)
: DObjectPrivate(qq)
{

}

void DToastPrivate::initUI()
{
D_Q(DToast);
q->setWindowFlags(q->windowFlags() | Qt::WindowStaysOnTopHint);

auto layout = new QHBoxLayout(q);
layout->setContentsMargins(10, 10, 10, 10);
layout->setSpacing(12);

iconLabel = new QLabel;
textLabel = new QLabel;

layout->addWidget(iconLabel);
layout->addWidget(textLabel);

effect = new DGraphicsGlowEffect(q);
effect->setBlurRadius(20.0);
effect->setColor(QColor(0, 0, 0, 255 / 10));
effect->setOffset(0, 0);
q->setGraphicsEffect(effect);
q->hide();
}


DWIDGET_END_NAMESPACE
Loading

0 comments on commit c4d3f48

Please sign in to comment.