Seperate qml and c++ code more.

This commit is contained in:
Hannah von Reth 2015-09-29 12:51:19 +02:00
parent cdcb09829a
commit 5d9c31f3ef
3 changed files with 184 additions and 170 deletions

View File

@ -1,45 +1,23 @@
import QtQuick 2.3 import QtQuick 2.3
import QtQuick.Window 2.2 import QtQuick.Window 2.2
Rectangle { Window {
id: root id: window
property int snoreBaseSize: body.font.pixelSize property int snoreBaseSize: body.font.pixelSize
Drag.active: mouseAreaAccept.drag.active
width: snoreBaseSize * 30 width: snoreBaseSize * 30
height: snoreBaseSize * 9 height: snoreBaseSize * 9
color: window.color color: notifyWidget.color
Connections{
target: window
onVisibleChanged: { onVisibleChanged: {
if(visible){ if(visible){
x = 0 x = 0
animation.from = window.animationFrom animation.from = notifyWidget.animationFrom
animation.start() animation.start()
} }
} }
}
onXChanged: {
// There is a Qt bug which will not stop the mouse tracking if the
// item is hid during a drag event.
if(Drag.active){
window.x += x
if(Math.abs((window.isOrientatedLeft?
window.x - window.animationTo:
window.x - window.animationFrom) + mouseAreaAccept.mouseX) <= width * 0.05){
Drag.cancel()
window.dismissed()
}
} else {
x = 0
}
}
NumberAnimation{ NumberAnimation{
@ -47,21 +25,49 @@ Rectangle {
properties: "x" properties: "x"
duration: 500 duration: 500
target: window target: window
from: window.animationFrom from: notifyWidget.animationFrom
to: window.animationTo to: notifyWidget.animationTo
} }
Rectangle{
id: root
height: window.height
width: window.width
color: window.color
Drag.active: mouseAreaAccept.drag.active
onXChanged: {
// There is a Qt bug which will not stop the mouse tracking if the
// item is hid during a drag event.
if(Drag.active){
window.x += x
if(Math.abs(window.x - (notifyWidget.isOrientatedLeft?
notifyWidget.animationTo:
notifyWidget.animationFrom) + mouseAreaAccept.mouseX) <= width * 0.05){
Drag.cancel()
notifyWidget.dismissed()
}
} else {
x = 0
}
}
MouseArea { MouseArea {
id: mouseAreaAccept id: mouseAreaAccept
anchors.fill: parent anchors.fill: parent
z: 90 z: 90
onClicked: { onClicked: {
window.invoked() notifyWidget.invoked()
} }
drag.target: parent drag.target: root
drag.axis: Drag.XAxis drag.axis: Drag.XAxis
drag.maximumX: window.dragMaxX drag.maximumX: notifyWidget.dragMaxX
drag.minimumX: window.dragMinX drag.minimumX: notifyWidget.dragMinX
drag.smoothed: true drag.smoothed: true
onPressed: { onPressed: {
animation.stop() animation.stop()
@ -75,8 +81,8 @@ Rectangle {
Text { Text {
id: title id: title
color: window.textColor color: notifyWidget.textColor
text: window.title text: notifyWidget.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
@ -85,14 +91,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: window.fontFamily font.family: notifyWidget.fontFamily
elide: Text.ElideRight elide: Text.ElideRight
} }
Text { Text {
id: body id: body
color: window.textColor color: notifyWidget.textColor
text: window.body text: notifyWidget.body
font.pointSize: 10 font.pointSize: 10
anchors.right: appIcon.left anchors.right: appIcon.left
anchors.top: title.bottom anchors.top: title.bottom
@ -103,7 +109,7 @@ Rectangle {
wrapMode: Text.WrapAtWordBoundaryOrAnywhere wrapMode: Text.WrapAtWordBoundaryOrAnywhere
onLinkActivated: Qt.openUrlExternally(link) onLinkActivated: Qt.openUrlExternally(link)
textFormat: Text.StyledText textFormat: Text.StyledText
font.family: window.fontFamily font.family: notifyWidget.fontFamily
elide: Text.ElideRight elide: Text.ElideRight
} }
@ -117,8 +123,8 @@ 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 onWidthChanged: notifyWidget.imageSize = width
source: window.image source: notifyWidget.image
} }
Image { Image {
@ -130,8 +136,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 onWidthChanged: notifyWidget.appIconSize = width
source: window.appIcon source: notifyWidget.appIcon
} }
Canvas{ Canvas{
@ -177,10 +183,11 @@ Rectangle {
MouseArea { MouseArea {
id: mouseAreaCloseButton id: mouseAreaCloseButton
anchors.fill: parent anchors.fill: parent
onClicked: window.dismissed() onClicked: notifyWidget.dismissed()
hoverEnabled: true hoverEnabled: true
onEntered: parent.requestPaint() onEntered: parent.requestPaint()
onExited: parent.requestPaint() onExited: parent.requestPaint()
} }
} }
}
} }

View File

@ -34,7 +34,6 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
m_ready(true), m_ready(true),
m_fontFamily(qApp->font().family()) m_fontFamily(qApp->font().family())
{ {
rootContext()->setContextProperty(QLatin1String("window"), this);
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
if (QSysInfo::windowsVersion() >= QSysInfo::WV_WINDOWS8) { if (QSysInfo::windowsVersion() >= QSysInfo::WV_WINDOWS8) {
@ -48,9 +47,12 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
} }
#endif #endif
#endif #endif
setSource(QUrl::fromEncoded("qrc:/notification.qml")); QQmlApplicationEngine *engine = new QQmlApplicationEngine(this);
engine->rootContext()->setContextProperty(QLatin1String("notifyWidget"), this);
engine->load(QUrl::fromEncoded("qrc:/notification.qml"));
m_window = qobject_cast<QQuickWindow *>(engine->rootObjects().value(0));
setFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowDoesNotAcceptFocus | Qt::BypassWindowManagerHint m_window->setFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowDoesNotAcceptFocus | Qt::BypassWindowManagerHint
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
| Qt::SubWindow | Qt::SubWindow
#else #else
@ -58,9 +60,6 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
#endif #endif
); );
// setFocusPolicy(Qt::NoFocus);
// setAttribute(Qt::WA_ShowWithoutActivating, true);
if (m_mem.create(sizeof(SHARED_MEM_TYPE))) { if (m_mem.create(sizeof(SHARED_MEM_TYPE))) {
m_mem.lock(); m_mem.lock();
SHARED_MEM_TYPE *data = (SHARED_MEM_TYPE *)m_mem.data(); SHARED_MEM_TYPE *data = (SHARED_MEM_TYPE *)m_mem.data();
@ -78,8 +77,6 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
snoreDebug(SNORE_DEBUG) << m_id << "State:" << data->free << "Time:" << elapsed << "Timeout:" << data->timeout; snoreDebug(SNORE_DEBUG) << m_id << "State:" << data->free << "Time:" << elapsed << "Timeout:" << data->timeout;
} }
} }
setResizeMode(QQuickView::SizeViewToRootObject);
} }
NotifyWidget::~NotifyWidget() NotifyWidget::~NotifyWidget()
@ -112,12 +109,12 @@ void NotifyWidget::display(const Notification &notification)
if (!notification.isUpdate()) { if (!notification.isUpdate()) {
syncSettings(); syncSettings();
m_color = color;
m_textColor = compueTextColor(color); m_textColor = compueTextColor(color);
setColor(color);
emit colorChanged(); emit colorChanged();
emit textColorChanged(); emit textColorChanged();
setVisible(true); m_window->show();
Utils::raiseWindowToFront(winId()); Utils::raiseWindowToFront(m_window->winId());
} }
} }
@ -165,7 +162,7 @@ bool NotifyWidget::release()
out = true; out = true;
} }
m_mem.unlock(); m_mem.unlock();
hide(); m_window->hide();
} }
return out; return out;
} }
@ -180,6 +177,11 @@ int NotifyWidget::id() const
return m_id; return m_id;
} }
bool NotifyWidget::isVisible() const
{
return m_window->isVisible();
}
void NotifyWidget::syncSettings() void NotifyWidget::syncSettings()
{ {
Qt::Corner c = static_cast<Qt::Corner>(m_parent->settingsValue(QLatin1String("Position")).toInt()); Qt::Corner c = static_cast<Qt::Corner>(m_parent->settingsValue(QLatin1String("Position")).toInt());
@ -190,21 +192,21 @@ void NotifyWidget::syncSettings()
m_cornerOld = c; m_cornerOld = c;
m_isOrientatedLeft = c == Qt::TopLeftCorner || c == Qt::BottomLeftCorner; m_isOrientatedLeft = c == Qt::TopLeftCorner || c == Qt::BottomLeftCorner;
if (m_isOrientatedLeft) { if (m_isOrientatedLeft) {
m_dragMinX = m_animationFrom = -width(); m_dragMinX = m_animationFrom = -m_window->width();
m_dragMaxX = m_animationTo = 0; m_dragMaxX = m_animationTo = 0;
} else { } else {
m_animationFrom = desktop.availableGeometry().width(); m_animationFrom = desktop.availableGeometry().width();
m_animationTo = desktop.availableGeometry().width() - width(); m_animationTo = desktop.availableGeometry().width() - m_window->width();
m_dragMinX = 0; m_dragMinX = 0;
m_dragMaxX = width(); m_dragMaxX = m_window->width();
} }
double space = (id() + 1) * height() * 0.025; double space = (id() + 1) * m_window->height() * 0.025;
if (c == Qt::TopRightCorner || c == Qt::TopLeftCorner) { if (c == Qt::TopRightCorner || c == Qt::TopLeftCorner) {
setY(space + (space + height()) * id()); m_window->setY(space + (space + m_window->height()) * id());
} else { } else {
setY(desktop.availableGeometry().height() - (space + (space + height()) * (id() + 1))); m_window->setY(desktop.availableGeometry().height() - (space + (space + m_window->height()) * (id() + 1)));
} }
emit isOrientatedLeftChanged(); emit isOrientatedLeftChanged();
emit animationFromChanged(); emit animationFromChanged();

View File

@ -38,7 +38,7 @@ inline int SHARED_MEM_TYPE_REV()
return 2; return 2;
} }
class NotifyWidget : public QQuickView class NotifyWidget : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool isOrientatedLeft MEMBER m_isOrientatedLeft NOTIFY isOrientatedLeftChanged) Q_PROPERTY(bool isOrientatedLeft MEMBER m_isOrientatedLeft NOTIFY isOrientatedLeftChanged)
@ -46,6 +46,7 @@ class NotifyWidget : public QQuickView
Q_PROPERTY(int animationTo MEMBER m_animationTo NOTIFY animationtoChanged) Q_PROPERTY(int animationTo MEMBER m_animationTo NOTIFY animationtoChanged)
Q_PROPERTY(int dragMinX MEMBER m_dragMinX NOTIFY dragMinXChanged) Q_PROPERTY(int dragMinX MEMBER m_dragMinX NOTIFY dragMinXChanged)
Q_PROPERTY(int dragMaxX MEMBER m_dragMaxX NOTIFY dragMaxXChanged) Q_PROPERTY(int dragMaxX MEMBER m_dragMaxX NOTIFY dragMaxXChanged)
Q_PROPERTY(QColor color MEMBER m_color NOTIFY colorChanged)
Q_PROPERTY(QColor textColor MEMBER m_textColor NOTIFY textColorChanged) Q_PROPERTY(QColor textColor MEMBER m_textColor NOTIFY textColorChanged)
Q_PROPERTY(QString title MEMBER m_title NOTIFY titleChanged) Q_PROPERTY(QString title MEMBER m_title NOTIFY titleChanged)
Q_PROPERTY(QString body MEMBER m_body NOTIFY bodyChanged) Q_PROPERTY(QString body MEMBER m_body NOTIFY bodyChanged)
@ -67,6 +68,7 @@ public:
Snore::Notification &notification(); Snore::Notification &notification();
int id() const; int id() const;
bool isVisible() const;
Q_SIGNALS: Q_SIGNALS:
void invoked(); void invoked();
@ -112,6 +114,7 @@ private:
int m_imageSize; int m_imageSize;
int m_appIconSize; int m_appIconSize;
QColor m_color;
QColor m_textColor; QColor m_textColor;
QString m_title; QString m_title;
@ -124,6 +127,8 @@ private:
QUrl m_appIcon; QUrl m_appIcon;
QUrl m_image; QUrl m_image;
QQuickWindow *m_window;
}; };
#endif // NOTIFYWIDGET_H #endif // NOTIFYWIDGET_H