Seperate qml and c++ code more.
This commit is contained in:
parent
cdcb09829a
commit
5d9c31f3ef
|
@ -1,45 +1,23 @@
|
|||
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
|
||||
color: notifyWidget.color
|
||||
|
||||
|
||||
Connections{
|
||||
target: window
|
||||
onVisibleChanged: {
|
||||
if(visible){
|
||||
x = 0
|
||||
animation.from = window.animationFrom
|
||||
animation.from = notifyWidget.animationFrom
|
||||
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{
|
||||
|
@ -47,21 +25,49 @@ Rectangle {
|
|||
properties: "x"
|
||||
duration: 500
|
||||
target: window
|
||||
from: window.animationFrom
|
||||
to: window.animationTo
|
||||
from: notifyWidget.animationFrom
|
||||
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 {
|
||||
id: mouseAreaAccept
|
||||
anchors.fill: parent
|
||||
z: 90
|
||||
onClicked: {
|
||||
window.invoked()
|
||||
notifyWidget.invoked()
|
||||
}
|
||||
drag.target: parent
|
||||
drag.target: root
|
||||
drag.axis: Drag.XAxis
|
||||
drag.maximumX: window.dragMaxX
|
||||
drag.minimumX: window.dragMinX
|
||||
drag.maximumX: notifyWidget.dragMaxX
|
||||
drag.minimumX: notifyWidget.dragMinX
|
||||
drag.smoothed: true
|
||||
onPressed: {
|
||||
animation.stop()
|
||||
|
@ -75,8 +81,8 @@ Rectangle {
|
|||
|
||||
Text {
|
||||
id: title
|
||||
color: window.textColor
|
||||
text: window.title
|
||||
color: notifyWidget.textColor
|
||||
text: notifyWidget.title
|
||||
font.pointSize: body.font.pointSize * 1.5
|
||||
font.bold: true
|
||||
anchors.top: parent.top
|
||||
|
@ -85,14 +91,14 @@ Rectangle {
|
|||
anchors.left: image.right
|
||||
anchors.right: closeButton.left
|
||||
textFormat: Text.StyledText
|
||||
font.family: window.fontFamily
|
||||
font.family: notifyWidget.fontFamily
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
|
||||
Text {
|
||||
id: body
|
||||
color: window.textColor
|
||||
text: window.body
|
||||
color: notifyWidget.textColor
|
||||
text: notifyWidget.body
|
||||
font.pointSize: 10
|
||||
anchors.right: appIcon.left
|
||||
anchors.top: title.bottom
|
||||
|
@ -103,7 +109,7 @@ Rectangle {
|
|||
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
|
||||
onLinkActivated: Qt.openUrlExternally(link)
|
||||
textFormat: Text.StyledText
|
||||
font.family: window.fontFamily
|
||||
font.family: notifyWidget.fontFamily
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
|
||||
|
@ -117,8 +123,8 @@ Rectangle {
|
|||
anchors.bottom: parent.bottom
|
||||
anchors.top: parent.top
|
||||
z: 4
|
||||
onWidthChanged: window.imageSize = width
|
||||
source: window.image
|
||||
onWidthChanged: notifyWidget.imageSize = width
|
||||
source: notifyWidget.image
|
||||
}
|
||||
|
||||
Image {
|
||||
|
@ -130,8 +136,8 @@ Rectangle {
|
|||
anchors.right: parent.right
|
||||
anchors.bottom: parent.bottom
|
||||
anchors.margins: snoreBaseSize
|
||||
onWidthChanged: window.appIconSize = width
|
||||
source: window.appIcon
|
||||
onWidthChanged: notifyWidget.appIconSize = width
|
||||
source: notifyWidget.appIcon
|
||||
}
|
||||
|
||||
Canvas{
|
||||
|
@ -177,10 +183,11 @@ Rectangle {
|
|||
MouseArea {
|
||||
id: mouseAreaCloseButton
|
||||
anchors.fill: parent
|
||||
onClicked: window.dismissed()
|
||||
onClicked: notifyWidget.dismissed()
|
||||
hoverEnabled: true
|
||||
onEntered: parent.requestPaint()
|
||||
onExited: parent.requestPaint()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,9 +47,12 @@ 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
|
||||
#else
|
||||
|
@ -58,9 +60,6 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
|
|||
#endif
|
||||
);
|
||||
|
||||
// setFocusPolicy(Qt::NoFocus);
|
||||
// setAttribute(Qt::WA_ShowWithoutActivating, true);
|
||||
|
||||
if (m_mem.create(sizeof(SHARED_MEM_TYPE))) {
|
||||
m_mem.lock();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
setResizeMode(QQuickView::SizeViewToRootObject);
|
||||
}
|
||||
|
||||
NotifyWidget::~NotifyWidget()
|
||||
|
@ -112,12 +109,12 @@ void NotifyWidget::display(const Notification ¬ification)
|
|||
|
||||
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();
|
||||
|
|
|
@ -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 ¬ification();
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue