Improve qml code and alow dismiss with a touch gesture.

This commit is contained in:
Patrick von Reth 2015-09-25 16:06:10 +02:00
parent a54d03e3b0
commit 5bea4b509d
3 changed files with 165 additions and 87 deletions

View File

@ -4,35 +4,73 @@ import QtQuick.Window 2.2
Rectangle { Rectangle {
id: root id: root
signal dismissed()
signal invoked()
property int snoreBaseSize: body.font.pixelSize property int snoreBaseSize: body.font.pixelSize
width: snoreBaseSize * 30 width: snoreBaseSize * 30
height: snoreBaseSize * 9 height: snoreBaseSize * 9
color: window.color
Drag.active: mouseAreaAccept.drag.active
visible: window.visible
x: 0
onVisibleChanged: {
if(visible){
x = 0
animation.from = window.animationFrom
animation.start()
}
}
onXChanged: {
window.x += x
var threashHold = 0
if(window.isOrientatedLeft) {
threashHold = Math.abs(window.x - window.animationTo + width)
}else{
threashHold = Math.abs(window.x - window.animationFrom)
}
if(window.visible && threashHold <= width * 0.3){
window.visible = false
Drag.cancel()
window.dismissed()
}
}
NumberAnimation{ NumberAnimation{
id: animation id: animation
objectName: "animation"
properties: "x" properties: "x"
duration: 500 duration: 500
target: window target: window
from: window.animationFrom
to: window.animationTo
} }
MouseArea { MouseArea {
id: mouseArea2 id: mouseAreaAccept
anchors.fill: parent anchors.fill: parent
z: 90 z: 90
onClicked: root.invoked() onClicked: {
window.invoked()
} }
drag.target: parent
drag.axis: Drag.XAxis
drag.maximumX: window.dragMaxX
drag.minimumX: window.dragMinX
onPressed: {
animation.stop()
}
onReleased: {
animation.from = window.x
animation.start()
}
}
Text { Text {
id: title id: title
objectName: "title" color: window.textColor
color: "#000000" text: window.title
text: "Title"
font.pointSize: body.font.pointSize * 1.5 font.pointSize: body.font.pointSize * 1.5
font.bold: true font.bold: true
anchors.top: parent.top anchors.top: parent.top
@ -41,15 +79,14 @@ Rectangle {
anchors.left: image.right anchors.left: image.right
anchors.right: closeButton.left anchors.right: closeButton.left
textFormat: Text.StyledText textFormat: Text.StyledText
font.family: snoreFont font.family: window.fontFamily
elide: Text.ElideRight elide: Text.ElideRight
} }
Text { Text {
id: body id: body
objectName: "body" color: window.textColor
color: "#000000" text: window.body
text: "Body"
font.pointSize: 10 font.pointSize: 10
anchors.right: appIcon.left anchors.right: appIcon.left
anchors.top: title.bottom anchors.top: title.bottom
@ -60,13 +97,12 @@ Rectangle {
wrapMode: Text.WrapAtWordBoundaryOrAnywhere wrapMode: Text.WrapAtWordBoundaryOrAnywhere
onLinkActivated: Qt.openUrlExternally(link) onLinkActivated: Qt.openUrlExternally(link)
textFormat: Text.StyledText textFormat: Text.StyledText
font.family: snoreFont font.family: window.fontFamily
elide: Text.ElideRight elide: Text.ElideRight
} }
Image { Image {
id: image id: image
objectName: "image"
fillMode: Image.PreserveAspectFit fillMode: Image.PreserveAspectFit
width: height width: height
smooth: true smooth: true
@ -75,11 +111,12 @@ Rectangle {
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
anchors.top: parent.top anchors.top: parent.top
z: 4 z: 4
onWidthChanged: window.imageSize = width
source: window.image
} }
Image { Image {
id: appIcon id: appIcon
objectName: "appIcon"
fillMode: Image.PreserveAspectFit fillMode: Image.PreserveAspectFit
height: root.height * 0.30 height: root.height * 0.30
width: root.height * 0.30 width: root.height * 0.30
@ -87,6 +124,8 @@ Rectangle {
anchors.right: parent.right anchors.right: parent.right
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
anchors.margins: snoreBaseSize anchors.margins: snoreBaseSize
onWidthChanged: window.appIconSize = width
source: window.appIcon
} }
Canvas{ Canvas{
@ -132,7 +171,7 @@ Rectangle {
MouseArea { MouseArea {
id: mouseAreaCloseButton id: mouseAreaCloseButton
anchors.fill: parent anchors.fill: parent
onClicked: root.dismissed() onClicked: window.dismissed()
hoverEnabled: true hoverEnabled: true
onEntered: parent.requestPaint() onEntered: parent.requestPaint()
onExited: parent.requestPaint() onExited: parent.requestPaint()

View File

@ -21,6 +21,7 @@
#include "libsnore/log.h" #include "libsnore/log.h"
#include "libsnore/utils.h" #include "libsnore/utils.h"
#include <QApplication>
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QQmlProperty> #include <QQmlProperty>
@ -30,28 +31,24 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
m_id(pos), m_id(pos),
m_parent(parent), m_parent(parent),
m_mem(QLatin1String("SnoreNotifyWidget_rev") + QString::number(SHARED_MEM_TYPE_REV()) + QLatin1String("_id") + QString::number(m_id)), m_mem(QLatin1String("SnoreNotifyWidget_rev") + QString::number(SHARED_MEM_TYPE_REV()) + QLatin1String("_id") + QString::number(m_id)),
m_ready(true) m_ready(true),
m_fontFamily(qApp->font().family())
{ {
rootContext()->setContextProperty(QLatin1String("window"), this); rootContext()->setContextProperty(QLatin1String("window"), this);
QString font = qApp->font().family();
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
if (QSysInfo::windowsVersion() >= QSysInfo::WV_WINDOWS8) { if (QSysInfo::windowsVersion() >= QSysInfo::WV_WINDOWS8) {
font = QLatin1String("Segoe UI Symbol"); m_fontFamily = QLatin1String("Segoe UI Symbol");
emit fontFamilyChanged();
} }
#if QT_VERSION >= QT_VERSION_CHECK(5,5,0) #if QT_VERSION >= QT_VERSION_CHECK(5,5,0)
if (QSysInfo::windowsVersion() >= QSysInfo::WV_WINDOWS10) { if (QSysInfo::windowsVersion() >= QSysInfo::WV_WINDOWS10) {
font = QLatin1String("Segoe UI Emoji"); m_fontFamily = QLatin1String("Segoe UI Emoji");
emit fontFamilyChanged();
} }
#endif #endif
#endif #endif
rootContext()->setContextProperty(QLatin1String("snoreFont"), font);
setSource(QUrl::fromEncoded("qrc:/notification.qml")); setSource(QUrl::fromEncoded("qrc:/notification.qml"));
m_appIcon = rootObject()->findChild<QObject*>(QLatin1String("appIcon"));
m_image = rootObject()->findChild<QObject*>(QLatin1String("image"));
m_title = rootObject()->findChild<QObject*>(QLatin1String("title"));
m_body = rootObject()->findChild<QObject*>(QLatin1String("body"));
m_animation = rootObject()->findChild<QObject*>(QLatin1String("animation"));
setFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowDoesNotAcceptFocus setFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowDoesNotAcceptFocus
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
@ -82,9 +79,6 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
} }
setResizeMode(QQuickView::SizeViewToRootObject); setResizeMode(QQuickView::SizeViewToRootObject);
connect(rootObject(), SIGNAL(invoked()), this, SLOT(slotInvoked()));
connect(rootObject(), SIGNAL(dismissed()), this, SLOT(slotDismissed()));
} }
NotifyWidget::~NotifyWidget() NotifyWidget::~NotifyWidget()
@ -104,39 +98,23 @@ void NotifyWidget::display(const Notification &notification)
color = computeBackgrondColor(notification.application().icon().pixmap(QSize(20, 20)).toImage()); color = computeBackgrondColor(notification.application().icon().pixmap(QSize(20, 20)).toImage());
notification.application().hints().setPrivateValue(parent(), "backgroundColor", color); notification.application().hints().setPrivateValue(parent(), "backgroundColor", color);
} }
QColor textColor = compueTextColor(color); m_appIcon = QUrl::fromLocalFile(notification.application().icon().localUrl(QSize(m_appIconSize, m_appIconSize)));
int appIconWidht = m_appIcon->property("width").toInt(); emit appIconChanged();
m_appIcon->setProperty("source", QUrl::fromLocalFile(notification.application().icon().localUrl(QSize(appIconWidht,appIconWidht))));
int imageWidth = m_image->property("width").toInt();
m_image->setProperty("source", QUrl::fromLocalFile(notification.icon().localUrl(QSize(imageWidth,imageWidth))));
m_image = QUrl::fromLocalFile(notification.icon().localUrl(QSize(m_imageSize, m_imageSize)));
emit imageChanged();
m_title->setProperty("text", notification.title(Utils::ALL_MARKUP)); m_title = notification.title(Utils::ALL_MARKUP);
m_title->setProperty("color", textColor); emit titleChanged();
m_body = notification.text(Utils::ALL_MARKUP);
m_body->setProperty("text", notification.text(Utils::ALL_MARKUP)); emit bodyChanged();
m_body->setProperty("color", textColor);
rootObject()->setProperty("color", color);
if (!notification.isUpdate()) { if (!notification.isUpdate()) {
QDesktopWidget desktop; syncSettings();
double space = (id() + 1) * height() * 0.025; m_textColor = compueTextColor(color);
setY(space + (space + height()) * id()); setColor(color);
if (corner() == Qt::TopRightCorner || corner() == Qt::BottomRightCorner) { emit colorChanged();
m_animation->setProperty("from", desktop.availableGeometry().width()); emit textColorChanged();
m_animation->setProperty("to", desktop.availableGeometry().width() - width());
} else {
m_animation->setProperty("from", -width());
m_animation->setProperty("to", 0);
}
if (corner() == Qt::TopRightCorner || corner() == Qt::TopLeftCorner) {
setY(space + (space + height()) * id());
} else {
setY(desktop.availableGeometry().height() - (space + (space + height()) * (id() + 1)));
}
QMetaObject::invokeMethod(m_animation, "start");
setVisible(true); setVisible(true);
Utils::raiseWindowToFront(winId()); Utils::raiseWindowToFront(winId());
} }
@ -193,24 +171,43 @@ Notification &NotifyWidget::notification()
return m_notification; return m_notification;
} }
int NotifyWidget::id() int NotifyWidget::id() const
{ {
return m_id; return m_id;
} }
Qt::Corner NotifyWidget::corner() void NotifyWidget::syncSettings()
{ {
return static_cast<Qt::Corner>(m_parent->settingsValue(QLatin1String("Position")).toInt()); Qt::Corner c = static_cast<Qt::Corner>(m_parent->settingsValue(QLatin1String("Position")).toInt());
} if (c != m_cornerOld || !m_initialized) {
m_initialized = true;
QDesktopWidget desktop;
void NotifyWidget::slotDismissed() m_cornerOld = c;
{ m_isOrientatedLeft = c == Qt::TopLeftCorner || c == Qt::BottomLeftCorner;
emit dismissed(); if (m_isOrientatedLeft) {
} m_dragMinX = m_animationFrom = -width();
m_dragMaxX = m_animationTo = 0;
} else {
m_animationFrom = desktop.availableGeometry().width();
m_animationTo = desktop.availableGeometry().width() - width();
m_dragMinX = 0;
m_dragMaxX = width();
void NotifyWidget::slotInvoked() }
{ double space = (id() + 1) * height() * 0.025;
emit invoked();
if (c == Qt::TopRightCorner || c == Qt::TopLeftCorner) {
setY(space + (space + height()) * id());
} else {
setY(desktop.availableGeometry().height() - (space + (space + height()) * (id() + 1)));
}
emit isOrientatedLeftChanged();
emit animationFromChanged();
emit animationtoChanged();
emit dragMaxXChanged();
emit dragMinXChanged();
}
} }
QColor NotifyWidget::computeBackgrondColor(const QImage &img) QColor NotifyWidget::computeBackgrondColor(const QImage &img)
@ -236,3 +233,4 @@ QColor NotifyWidget::compueTextColor(const QColor &backgroundColor)
QRgb compColor = qGray(backgroundColor.rgb()) > 186 ? 0 : 255; QRgb compColor = qGray(backgroundColor.rgb()) > 186 ? 0 : 255;
return QColor(compColor, compColor, compColor); return QColor(compColor, compColor, compColor);
} }

View File

@ -40,6 +40,19 @@ inline int SHARED_MEM_TYPE_REV()
class NotifyWidget : public QQuickView class NotifyWidget : public QQuickView
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool isOrientatedLeft MEMBER m_isOrientatedLeft NOTIFY isOrientatedLeftChanged)
Q_PROPERTY(int animationFrom MEMBER m_animationFrom NOTIFY animationFromChanged)
Q_PROPERTY(int animationTo MEMBER m_animationTo NOTIFY animationtoChanged)
Q_PROPERTY(int dragMinX MEMBER m_dragMinX NOTIFY dragMinXChanged)
Q_PROPERTY(int dragMaxX MEMBER m_dragMaxX NOTIFY dragMaxXChanged)
Q_PROPERTY(QColor textColor MEMBER m_textColor NOTIFY textColorChanged)
Q_PROPERTY(QString title MEMBER m_title NOTIFY titleChanged)
Q_PROPERTY(QString body MEMBER m_body NOTIFY bodyChanged)
Q_PROPERTY(int imageSize MEMBER m_imageSize)
Q_PROPERTY(int appIconSize MEMBER m_appIconSize)
Q_PROPERTY(QUrl image MEMBER m_image NOTIFY imageChanged)
Q_PROPERTY(QUrl appIcon MEMBER m_appIcon NOTIFY appIconChanged)
Q_PROPERTY(QString fontFamily MEMBER m_fontFamily NOTIFY fontFamilyChanged)
public: public:
explicit NotifyWidget(int pos, const SnoreNotifier *parent); explicit NotifyWidget(int pos, const SnoreNotifier *parent);
@ -52,35 +65,63 @@ public:
Snore::Notification &notification(); Snore::Notification &notification();
int id(); int id() const;
Qt::Corner corner();
Q_SIGNALS: Q_SIGNALS:
void invoked(); void invoked();
void dismissed(); void dismissed();
private Q_SLOTS: void isOrientatedLeftChanged();
void slotDismissed(); void animationFromChanged();
void animationtoChanged();
void dragMinXChanged();
void dragMaxXChanged();
void slotInvoked(); void textColorChanged();
void colorChanged();
void titleChanged();
void bodyChanged();
void imageChanged();
void appIconChanged();
void fontFamilyChanged();
private: private:
void syncSettings();
QColor computeBackgrondColor(const QImage &img); QColor computeBackgrondColor(const QImage &img);
QColor compueTextColor(const QColor &backgroundColor); QColor compueTextColor(const QColor &backgroundColor);
void updateCornerValues(Qt::Corner c);
Snore::Notification m_notification; Snore::Notification m_notification;
int m_id; int m_id;
const SnoreNotifier *m_parent; const SnoreNotifier *m_parent;
QSharedMemory m_mem; QSharedMemory m_mem;
bool m_ready; bool m_ready;
Qt::Corner m_cornerOld = Qt::TopLeftCorner;
bool m_isOrientatedLeft;
QObject *m_appIcon; int m_animationFrom;
QObject *m_image; int m_animationTo;
QObject *m_title; int m_dragMinX;
int m_dragMaxX;
QObject *m_body; int m_imageSize;
QObject *m_animation; int m_appIconSize;
QColor m_textColor;
QString m_title;
QString m_body;
QString m_fontFamily;
bool m_initialized = false;
QUrl m_appIcon;
QUrl m_image;
}; };