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 {
id: root
signal dismissed()
signal invoked()
property int snoreBaseSize: body.font.pixelSize
width: snoreBaseSize * 30
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{
id: animation
objectName: "animation"
properties: "x"
duration: 500
target: window
from: window.animationFrom
to: window.animationTo
}
MouseArea {
id: mouseArea2
id: mouseAreaAccept
anchors.fill: parent
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 {
id: title
objectName: "title"
color: "#000000"
text: "Title"
color: window.textColor
text: window.title
font.pointSize: body.font.pointSize * 1.5
font.bold: true
anchors.top: parent.top
@ -41,15 +79,14 @@ Rectangle {
anchors.left: image.right
anchors.right: closeButton.left
textFormat: Text.StyledText
font.family: snoreFont
font.family: window.fontFamily
elide: Text.ElideRight
}
Text {
id: body
objectName: "body"
color: "#000000"
text: "Body"
color: window.textColor
text: window.body
font.pointSize: 10
anchors.right: appIcon.left
anchors.top: title.bottom
@ -60,13 +97,12 @@ Rectangle {
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
onLinkActivated: Qt.openUrlExternally(link)
textFormat: Text.StyledText
font.family: snoreFont
font.family: window.fontFamily
elide: Text.ElideRight
}
Image {
id: image
objectName: "image"
fillMode: Image.PreserveAspectFit
width: height
smooth: true
@ -75,11 +111,12 @@ Rectangle {
anchors.bottom: parent.bottom
anchors.top: parent.top
z: 4
onWidthChanged: window.imageSize = width
source: window.image
}
Image {
id: appIcon
objectName: "appIcon"
fillMode: Image.PreserveAspectFit
height: root.height * 0.30
width: root.height * 0.30
@ -87,6 +124,8 @@ Rectangle {
anchors.right: parent.right
anchors.bottom: parent.bottom
anchors.margins: snoreBaseSize
onWidthChanged: window.appIconSize = width
source: window.appIcon
}
Canvas{
@ -132,7 +171,7 @@ Rectangle {
MouseArea {
id: mouseAreaCloseButton
anchors.fill: parent
onClicked: root.dismissed()
onClicked: window.dismissed()
hoverEnabled: true
onEntered: parent.requestPaint()
onExited: parent.requestPaint()

View File

@ -21,6 +21,7 @@
#include "libsnore/log.h"
#include "libsnore/utils.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QQmlProperty>
@ -30,28 +31,24 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
m_id(pos),
m_parent(parent),
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);
QString font = qApp->font().family();
#ifdef Q_OS_WIN
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 (QSysInfo::windowsVersion() >= QSysInfo::WV_WINDOWS10) {
font = QLatin1String("Segoe UI Emoji");
m_fontFamily = QLatin1String("Segoe UI Emoji");
emit fontFamilyChanged();
}
#endif
#endif
rootContext()->setContextProperty(QLatin1String("snoreFont"), font);
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
#ifdef Q_OS_MAC
@ -61,8 +58,8 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
#endif
);
// setFocusPolicy(Qt::NoFocus);
// setAttribute(Qt::WA_ShowWithoutActivating, true);
// setFocusPolicy(Qt::NoFocus);
// setAttribute(Qt::WA_ShowWithoutActivating, true);
if (m_mem.create(sizeof(SHARED_MEM_TYPE))) {
m_mem.lock();
@ -82,9 +79,6 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
}
setResizeMode(QQuickView::SizeViewToRootObject);
connect(rootObject(), SIGNAL(invoked()), this, SLOT(slotInvoked()));
connect(rootObject(), SIGNAL(dismissed()), this, SLOT(slotDismissed()));
}
NotifyWidget::~NotifyWidget()
@ -101,42 +95,26 @@ void NotifyWidget::display(const Notification &notification)
if (vcolor.isValid()) {
color = vcolor.value<QColor>();
} else {
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);
}
QColor textColor = compueTextColor(color);
int appIconWidht = m_appIcon->property("width").toInt();
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_appIcon = QUrl::fromLocalFile(notification.application().icon().localUrl(QSize(m_appIconSize, m_appIconSize)));
emit appIconChanged();
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->setProperty("color", textColor);
m_body->setProperty("text", notification.text(Utils::ALL_MARKUP));
m_body->setProperty("color", textColor);
rootObject()->setProperty("color", color);
m_title = notification.title(Utils::ALL_MARKUP);
emit titleChanged();
m_body = notification.text(Utils::ALL_MARKUP);
emit bodyChanged();
if (!notification.isUpdate()) {
QDesktopWidget desktop;
double space = (id() + 1) * height() * 0.025;
setY(space + (space + height()) * id());
if (corner() == Qt::TopRightCorner || corner() == Qt::BottomRightCorner) {
m_animation->setProperty("from", desktop.availableGeometry().width());
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");
syncSettings();
m_textColor = compueTextColor(color);
setColor(color);
emit colorChanged();
emit textColorChanged();
setVisible(true);
Utils::raiseWindowToFront(winId());
}
@ -193,40 +171,59 @@ Notification &NotifyWidget::notification()
return m_notification;
}
int NotifyWidget::id()
int NotifyWidget::id() const
{
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()
{
emit dismissed();
}
m_cornerOld = c;
m_isOrientatedLeft = c == Qt::TopLeftCorner || c == Qt::BottomLeftCorner;
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()
{
emit invoked();
}
double space = (id() + 1) * height() * 0.025;
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)
{
int stepSize = img.depth()/8;
int stepSize = img.depth() / 8;
qulonglong r = 0;
qulonglong g = 0;
qulonglong b = 0;
const uchar *end = img.constBits() + img.byteCount();
for (const uchar* bit = img.constBits(); bit != end; bit += stepSize ) {
const QRgb c = *reinterpret_cast<const QRgb*>(bit);
for (const uchar *bit = img.constBits(); bit != end; bit += stepSize) {
const QRgb c = *reinterpret_cast<const QRgb *>(bit);
r += qRed(c);
g += qGreen(c);
b += qBlue(c);
}
int size = img.byteCount()/stepSize;
int size = img.byteCount() / stepSize;
return QColor(r / size, g / size, b / size);
}
@ -236,3 +233,4 @@ QColor NotifyWidget::compueTextColor(const QColor &backgroundColor)
QRgb compColor = qGray(backgroundColor.rgb()) > 186 ? 0 : 255;
return QColor(compColor, compColor, compColor);
}

View File

@ -40,6 +40,19 @@ inline int SHARED_MEM_TYPE_REV()
class NotifyWidget : public QQuickView
{
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:
explicit NotifyWidget(int pos, const SnoreNotifier *parent);
@ -52,35 +65,63 @@ public:
Snore::Notification &notification();
int id();
Qt::Corner corner();
int id() const;
Q_SIGNALS:
void invoked();
void dismissed();
private Q_SLOTS:
void slotDismissed();
void isOrientatedLeftChanged();
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:
void syncSettings();
QColor computeBackgrondColor(const QImage &img);
QColor compueTextColor(const QColor &backgroundColor);
void updateCornerValues(Qt::Corner c);
Snore::Notification m_notification;
int m_id;
const SnoreNotifier *m_parent;
QSharedMemory m_mem;
bool m_ready;
Qt::Corner m_cornerOld = Qt::TopLeftCorner;
bool m_isOrientatedLeft;
QObject *m_appIcon;
QObject *m_image;
int m_animationFrom;
int m_animationTo;
QObject *m_title;
int m_dragMinX;
int m_dragMaxX;
QObject *m_body;
QObject *m_animation;
int m_imageSize;
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;
};