Move notification code from qml back to c++

This commit is contained in:
Patrick von Reth 2015-09-22 15:26:50 +02:00
parent 90e3b40c6a
commit a54d03e3b0
4 changed files with 63 additions and 56 deletions

View File

@ -205,8 +205,12 @@ void SnoreCorePrivate::setLocalSttingsPrefix(const QString &prefix)
QString SnoreCorePrivate::tempPath()
{
#if 1
static QTemporaryDir dir;
return dir.path();
#else
return QDir::temp().path() + QLatin1String("/libsnore/");
#endif
}
// TODO: this is somehow horrible code

View File

@ -13,47 +13,12 @@ Rectangle {
height: snoreBaseSize * 9
function update(nTitle, bBody, nImage, nAppIcon, color, textColor, isUpdate)
{
title.text = nTitle
title.color = textColor
body.text = bBody
body.color = textColor
appIcon.source = nAppIcon
image.source = nImage
root.color = color
if (!isUpdate) {
var corner = window.corner
var id = window.id
var space = (id + 1) * height * 0.025
animation.target = window
window.y = space + (space + height) * id
if (corner == Qt.TopRightCorner || corner == Qt.BottomRightCorner) {
animation.from = Screen.desktopAvailableWidth
animation.to = Screen.desktopAvailableWidth - width
} else {
animation.from = -width
animation.to = 0
}
if (corner == Qt.TopRightCorner || corner == Qt.TopLeftCorner) {
window.y = space + (space + height) * id
} else {
window.y = Screen.desktopAvailableHeight - (space + (space + height) * (id + 1))
}
animation.start()
window.visible = true
utils.raiseWindowToFront(window.wid)
}
}
NumberAnimation{
id: animation
objectName: "animation"
properties: "x"
duration: 500
target: window
}
MouseArea {
@ -65,6 +30,7 @@ Rectangle {
Text {
id: title
objectName: "title"
color: "#000000"
text: "Title"
font.pointSize: body.font.pointSize * 1.5
@ -81,6 +47,7 @@ Rectangle {
Text {
id: body
objectName: "body"
color: "#000000"
text: "Body"
font.pointSize: 10
@ -99,6 +66,7 @@ Rectangle {
Image {
id: image
objectName: "image"
fillMode: Image.PreserveAspectFit
width: height
smooth: true
@ -111,6 +79,7 @@ Rectangle {
Image {
id: appIcon
objectName: "appIcon"
fillMode: Image.PreserveAspectFit
height: root.height * 0.30
width: root.height * 0.30

View File

@ -21,6 +21,9 @@
#include "libsnore/log.h"
#include "libsnore/utils.h"
#include <QDesktopWidget>
#include <QQmlProperty>
using namespace Snore;
NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
@ -30,7 +33,6 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
m_ready(true)
{
rootContext()->setContextProperty(QLatin1String("window"), this);
rootContext()->setContextProperty(QLatin1String("utils"), new Utils(this));
QString font = qApp->font().family();
#ifdef Q_OS_WIN
@ -45,6 +47,11 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
#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
@ -94,18 +101,45 @@ void NotifyWidget::display(const Notification &notification)
if (vcolor.isValid()) {
color = vcolor.value<QColor>();
} else {
color = computeBackgrondColor(notification.application().icon().image().scaled(20, 20));
color = computeBackgrondColor(notification.application().icon().pixmap(QSize(20,20)).toImage());
notification.application().hints().setPrivateValue(parent(), "backgroundColor", color);
}
QColor textColor = compueTextColor(color);
QMetaObject::invokeMethod(rootObject(), "update", Qt::QueuedConnection,
Q_ARG(QVariant, notification.title(Utils::ALL_MARKUP)),
Q_ARG(QVariant, notification.text(Utils::ALL_MARKUP)),
Q_ARG(QVariant, QUrl::fromLocalFile(notification.icon().localUrl())),
Q_ARG(QVariant, QUrl::fromLocalFile(notification.application().icon().localUrl())),
Q_ARG(QVariant, color),
Q_ARG(QVariant, textColor),
Q_ARG(QVariant, notification.isUpdate()));
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_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);
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");
setVisible(true);
Utils::raiseWindowToFront(winId());
}
}
bool NotifyWidget::acquire()
@ -169,11 +203,6 @@ Qt::Corner NotifyWidget::corner()
return static_cast<Qt::Corner>(m_parent->settingsValue(QLatin1String("Position")).toInt());
}
qlonglong NotifyWidget::wid()
{
return this->winId();
}
void NotifyWidget::slotDismissed()
{
emit dismissed();

View File

@ -42,9 +42,6 @@ class NotifyWidget : public QQuickView
Q_OBJECT
public:
Q_PROPERTY(int id READ id)
Q_PROPERTY(Qt::Corner corner READ corner)
Q_PROPERTY(qlonglong wid READ wid)
explicit NotifyWidget(int pos, const SnoreNotifier *parent);
~NotifyWidget();
@ -57,7 +54,6 @@ public:
int id();
Qt::Corner corner();
qlonglong wid();
Q_SIGNALS:
void invoked();
@ -77,6 +73,15 @@ private:
const SnoreNotifier *m_parent;
QSharedMemory m_mem;
bool m_ready;
QObject *m_appIcon;
QObject *m_image;
QObject *m_title;
QObject *m_body;
QObject *m_animation;
};
#endif // NOTIFYWIDGET_H