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,43 +1,21 @@
import QtQuick 2.3
import QtQuick.Window 2.2
Rectangle {
id: root
Window {
id: window
property int snoreBaseSize: body.font.pixelSize
Drag.active: mouseAreaAccept.drag.active
width: snoreBaseSize * 30
height: snoreBaseSize * 9
color: window.color
Connections{
target: window
onVisibleChanged: {
if(visible){
x = 0
animation.from = window.animationFrom
animation.start()
}
}
}
color: notifyWidget.color
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 {
onVisibleChanged: {
if(visible){
x = 0
animation.from = notifyWidget.animationFrom
animation.start()
}
}
@ -47,140 +25,169 @@ Rectangle {
properties: "x"
duration: 500
target: window
from: window.animationFrom
to: window.animationTo
from: notifyWidget.animationFrom
to: notifyWidget.animationTo
}
MouseArea {
id: mouseAreaAccept
anchors.fill: parent
z: 90
onClicked: {
window.invoked()
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
}
}
drag.target: parent
drag.axis: Drag.XAxis
drag.maximumX: window.dragMaxX
drag.minimumX: window.dragMinX
drag.smoothed: true
onPressed: {
animation.stop()
MouseArea {
id: mouseAreaAccept
anchors.fill: parent
z: 90
onClicked: {
notifyWidget.invoked()
}
drag.target: root
drag.axis: Drag.XAxis
drag.maximumX: notifyWidget.dragMaxX
drag.minimumX: notifyWidget.dragMinX
drag.smoothed: true
onPressed: {
animation.stop()
}
onReleased: {
animation.from = window.x
animation.start()
}
}
onReleased: {
animation.from = window.x
animation.start()
Text {
id: title
color: notifyWidget.textColor
text: notifyWidget.title
font.pointSize: body.font.pointSize * 1.5
font.bold: true
anchors.top: parent.top
anchors.margins: snoreBaseSize
anchors.topMargin: snoreBaseSize / 2
anchors.left: image.right
anchors.right: closeButton.left
textFormat: Text.StyledText
font.family: notifyWidget.fontFamily
elide: Text.ElideRight
}
}
Text {
id: body
color: notifyWidget.textColor
text: notifyWidget.body
font.pointSize: 10
anchors.right: appIcon.left
anchors.top: title.bottom
anchors.bottom: parent.bottom
anchors.left: image.right
anchors.margins: snoreBaseSize
anchors.topMargin: snoreBaseSize / 2
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
onLinkActivated: Qt.openUrlExternally(link)
textFormat: Text.StyledText
font.family: notifyWidget.fontFamily
elide: Text.ElideRight
}
Text {
id: title
color: window.textColor
text: window.title
font.pointSize: body.font.pointSize * 1.5
font.bold: true
anchors.top: parent.top
anchors.margins: snoreBaseSize
anchors.topMargin: snoreBaseSize / 2
anchors.left: image.right
anchors.right: closeButton.left
textFormat: Text.StyledText
font.family: window.fontFamily
elide: Text.ElideRight
}
Image {
id: image
fillMode: Image.PreserveAspectFit
width: height
smooth: true
anchors.left: parent.left
anchors.margins: snoreBaseSize
anchors.bottom: parent.bottom
anchors.top: parent.top
z: 4
onWidthChanged: notifyWidget.imageSize = width
source: notifyWidget.image
}
Text {
id: body
color: window.textColor
text: window.body
font.pointSize: 10
anchors.right: appIcon.left
anchors.top: title.bottom
anchors.bottom: parent.bottom
anchors.left: image.right
anchors.margins: snoreBaseSize
anchors.topMargin: snoreBaseSize / 2
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
onLinkActivated: Qt.openUrlExternally(link)
textFormat: Text.StyledText
font.family: window.fontFamily
elide: Text.ElideRight
}
Image {
id: appIcon
fillMode: Image.PreserveAspectFit
height: root.height * 0.30
width: root.height * 0.30
smooth: true
anchors.right: parent.right
anchors.bottom: parent.bottom
anchors.margins: snoreBaseSize
onWidthChanged: notifyWidget.appIconSize = width
source: notifyWidget.appIcon
}
Image {
id: image
fillMode: Image.PreserveAspectFit
width: height
smooth: true
anchors.left: parent.left
anchors.margins: snoreBaseSize
anchors.bottom: parent.bottom
anchors.top: parent.top
z: 4
onWidthChanged: window.imageSize = width
source: window.image
}
Canvas{
id: closeButton
height: root.height * 0.25
width: root.height * 0.25
anchors.top: parent.top
anchors.margins: snoreBaseSize
anchors.right: parent.right
z: 91
Image {
id: appIcon
fillMode: Image.PreserveAspectFit
height: root.height * 0.30
width: root.height * 0.30
smooth: true
anchors.right: parent.right
anchors.bottom: parent.bottom
anchors.margins: snoreBaseSize
onWidthChanged: window.appIconSize = width
source: window.appIcon
}
onPaint: {
var context = getContext("2d");
context.lineWidth = snoreBaseSize * 0.25;
Canvas{
id: closeButton
height: root.height * 0.25
width: root.height * 0.25
anchors.top: parent.top
anchors.margins: snoreBaseSize
anchors.right: parent.right
z: 91
onPaint: {
var context = getContext("2d");
context.lineWidth = snoreBaseSize * 0.25;
context.beginPath();
context.clearRect(0, 0, width, height);
context.fill();
if(mouseAreaCloseButton.containsMouse)
{
context.beginPath();
context.strokeStyle = root.color
context.fillStyle = body.color
context.globalAlpha = 0.5
context.moveTo(width/2 + width/2 , height/2);
context.arc(width/2, height/2, width/2 , 0, 2*Math.PI, true)
context.clearRect(0, 0, width, height);
context.fill();
if(mouseAreaCloseButton.containsMouse)
{
context.beginPath();
context.strokeStyle = root.color
context.fillStyle = body.color
context.globalAlpha = 0.5
context.moveTo(width/2 + width/2 , height/2);
context.arc(width/2, height/2, width/2 , 0, 2*Math.PI, true)
context.fill();
context.stroke();
}
var margin = snoreBaseSize * 0.5
context.beginPath();
context.globalAlpha = 1
context.strokeStyle = body.color
context.moveTo(margin, margin);
context.lineTo(width - margin, height - margin);
context.moveTo(width - margin, 0 + margin);
context.lineTo(margin, height - margin);
context.stroke();
}
var margin = snoreBaseSize * 0.5
context.beginPath();
context.globalAlpha = 1
context.strokeStyle = body.color
context.moveTo(margin, margin);
context.lineTo(width - margin, height - margin);
context.moveTo(width - margin, 0 + margin);
context.lineTo(margin, height - margin);
context.stroke();
}
MouseArea {
id: mouseAreaCloseButton
anchors.fill: parent
onClicked: window.dismissed()
hoverEnabled: true
onEntered: parent.requestPaint()
onExited: parent.requestPaint()
MouseArea {
id: mouseAreaCloseButton
anchors.fill: parent
onClicked: notifyWidget.dismissed()
hoverEnabled: true
onEntered: parent.requestPaint()
onExited: parent.requestPaint()
}
}
}
}

View File

@ -34,7 +34,6 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
m_ready(true),
m_fontFamily(qApp->font().family())
{
rootContext()->setContextProperty(QLatin1String("window"), this);
#ifdef Q_OS_WIN
if (QSysInfo::windowsVersion() >= QSysInfo::WV_WINDOWS8) {
@ -48,18 +47,18 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
}
#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
| Qt::SubWindow
| Qt::SubWindow
#else
| Qt::Tool
| Qt::Tool
#endif
);
// setFocusPolicy(Qt::NoFocus);
// setAttribute(Qt::WA_ShowWithoutActivating, true);
);
if (m_mem.create(sizeof(SHARED_MEM_TYPE))) {
m_mem.lock();
@ -73,13 +72,11 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
} else {
m_mem.lock();
SHARED_MEM_TYPE *data = (SHARED_MEM_TYPE *)m_mem.data();
m_mem.unlock();
m_mem.unlock();
int elapsed = (QTime::currentTime().msecsSinceStartOfDay() - data->date) / 1000;
snoreDebug(SNORE_DEBUG) << m_id << "State:" << data->free << "Time:" << elapsed << "Timeout:" << data->timeout;
}
}
setResizeMode(QQuickView::SizeViewToRootObject);
}
NotifyWidget::~NotifyWidget()
@ -112,12 +109,12 @@ void NotifyWidget::display(const Notification &notification)
if (!notification.isUpdate()) {
syncSettings();
m_color = color;
m_textColor = compueTextColor(color);
setColor(color);
emit colorChanged();
emit textColorChanged();
setVisible(true);
Utils::raiseWindowToFront(winId());
m_window->show();
Utils::raiseWindowToFront(m_window->winId());
}
}
@ -165,7 +162,7 @@ bool NotifyWidget::release()
out = true;
}
m_mem.unlock();
hide();
m_window->hide();
}
return out;
}
@ -180,6 +177,11 @@ int NotifyWidget::id() const
return m_id;
}
bool NotifyWidget::isVisible() const
{
return m_window->isVisible();
}
void NotifyWidget::syncSettings()
{
Qt::Corner c = static_cast<Qt::Corner>(m_parent->settingsValue(QLatin1String("Position")).toInt());
@ -190,21 +192,21 @@ void NotifyWidget::syncSettings()
m_cornerOld = c;
m_isOrientatedLeft = c == Qt::TopLeftCorner || c == Qt::BottomLeftCorner;
if (m_isOrientatedLeft) {
m_dragMinX = m_animationFrom = -width();
m_dragMinX = m_animationFrom = -m_window->width();
m_dragMaxX = m_animationTo = 0;
} else {
m_animationFrom = desktop.availableGeometry().width();
m_animationTo = desktop.availableGeometry().width() - width();
m_animationTo = desktop.availableGeometry().width() - m_window->width();
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) {
setY(space + (space + height()) * id());
m_window->setY(space + (space + m_window->height()) * id());
} 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 animationFromChanged();

View File

@ -38,7 +38,7 @@ inline int SHARED_MEM_TYPE_REV()
return 2;
}
class NotifyWidget : public QQuickView
class NotifyWidget : public QObject
{
Q_OBJECT
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 dragMinX MEMBER m_dragMinX NOTIFY dragMinXChanged)
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(QString title MEMBER m_title NOTIFY titleChanged)
Q_PROPERTY(QString body MEMBER m_body NOTIFY bodyChanged)
@ -67,6 +68,7 @@ public:
Snore::Notification &notification();
int id() const;
bool isVisible() const;
Q_SIGNALS:
void invoked();
@ -112,6 +114,7 @@ private:
int m_imageSize;
int m_appIconSize;
QColor m_color;
QColor m_textColor;
QString m_title;
@ -124,6 +127,8 @@ private:
QUrl m_appIcon;
QUrl m_image;
QQuickWindow *m_window;
};
#endif // NOTIFYWIDGET_H