Skip to content

Commit

Permalink
Merge pull request #42 from nemomobile-ux/inversemousearea_to_cpp
Browse files Browse the repository at this point in the history
[InverseMouseArea] Move to cpp
  • Loading branch information
neochapay committed Oct 6, 2023
2 parents b366dc5 + 0c90999 commit 9cbfd68
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 89 deletions.
5 changes: 3 additions & 2 deletions src/controls/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(SRC
editfilter.cpp
nemofocussingleton.cpp
nemowindow.cpp
inversemousearea.cpp
)

set(HEADERS
Expand All @@ -16,7 +17,8 @@ set(HEADERS
filteringmousearea.h
nemofocussingleton.h
nemowindow.h
nemopage.h)
nemopage.h
inversemousearea.h )

set(QML
qml/ActionButton.qml
Expand All @@ -32,7 +34,6 @@ set(QML
qml/HeaderToolsIndicator.qml
qml/HeaderToolsLayout.qml
qml/IconButton.qml
qml/InverseMouseArea.qml
qml/Label.qml
qml/ListViewItemWithActions.qml
qml/ListView.qml
Expand Down
84 changes: 84 additions & 0 deletions src/controls/inversemousearea.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2023 Chupligin Sergey <neochapay@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#include "inversemousearea.h"
#include <QQuickWindow>

InverseMouseArea::InverseMouseArea(QQuickItem* parent)
: QQuickItem(parent)
, m_active(true)
{
connect(this, &QQuickItem::windowChanged, this, &InverseMouseArea::resetFilterOnWindowUpdate);

connect(this, &QQuickItem::enabledChanged, this, &InverseMouseArea::update);
connect(this, &QQuickItem::visibleChanged, this, &InverseMouseArea::update);
}

bool InverseMouseArea::active() const
{
return m_active;
}

void InverseMouseArea::setActive(bool newActive)
{
if (m_active == newActive) {
return;
}

m_active = newActive;

emit activeChanged();
}

bool InverseMouseArea::eventFilter(QObject* object, QEvent* event)
{
Q_UNUSED(object);
if (m_active && event->type() == QEvent::MouseButtonPress && !this->contains(static_cast<QMouseEvent*>(event)->pos())) {
emit pressed();
}
return false;
}

void InverseMouseArea::itemChange(ItemChange change, const ItemChangeData value)
{
Q_UNUSED(value);
if (m_active && change == ItemSceneChange && window()) {
window()->installEventFilter(this);
}
}

void InverseMouseArea::resetFilterOnWindowUpdate(QQuickWindow* win)
{
if (win) {
win->installEventFilter(this);
}
}

bool InverseMouseArea::contains(const QPointF& point) const
{
if (!m_active) {
return false;
}

QPointF scenePos = mapToItem(parentItem(), point);
QPointF global = parentItem()->mapToScene(QPointF(0, 0));

bool cont = QRectF(global.x(), global.y(), parentItem()->width(), parentItem()->height()).contains(scenePos);
return cont;
}
54 changes: 54 additions & 0 deletions src/controls/inversemousearea.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2023 Chupligin Sergey <neochapay@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#ifndef INVERSEMOUSEAREA_H
#define INVERSEMOUSEAREA_H

#include <QQuickItem>
class QGraphicsSceneMouseEvent;

class InverseMouseArea : public QQuickItem {
Q_OBJECT
Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)

public:
InverseMouseArea(QQuickItem* parent = nullptr);
bool active() const;
void setActive(bool newActive);

signals:
void activeChanged();
void pressed();

protected:
bool eventFilter(QObject* object, QEvent* event);
void itemChange(ItemChange change, const ItemChangeData value);
bool contains(const QPointF& point) const;

private slots:
void resetFilterOnWindowUpdate(QQuickWindow* win);

private:
bool m_active;
bool m_pressed;

QPointF mapToRootItem(QPointF pos);
};

#endif // INVERSEMOUSEAREA_H
4 changes: 3 additions & 1 deletion src/controls/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) 2013 Tomasz Olszak <olszak.tomasz@gmail.com>
* Copyright (C) 2013 Andrea Bernabei <and.bernabei@gmail.com>
* Copyright (C) 2017 Eetu Kahelin
* Copyright (C) 2021-2022 Chupligin Sergey <neochapay@gmail.com>
* Copyright (C) 2021-2023 Chupligin Sergey <neochapay@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
Expand All @@ -22,6 +22,7 @@

#include "plugin.h"
#include "filteringmousearea.h"
#include "inversemousearea.h"
#include "nemofocussingleton.h"
#include "nemopage.h"
#include "nemowindow.h"
Expand All @@ -46,4 +47,5 @@ void QQuickNemoControlsExtensionPlugin::registerTypes(const char* uri)
qmlRegisterType<NemoPage>(uri, 2, 0, "NemoPage");
qmlRegisterType<RingIndicator>(uri, 2, 0, "RingIndicator");
qmlRegisterType<FilteringMouseArea>(uri, 2, 0, "FilteringMouseArea");
qmlRegisterType<InverseMouseArea>(uri, 2, 0, "InverseMouseArea");
}
85 changes: 0 additions & 85 deletions src/controls/qml/InverseMouseArea.qml

This file was deleted.

1 change: 0 additions & 1 deletion src/controls/qml/qmldir
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ ListView 2.0 ListView.qml
ListViewItemWithActions 2.0 ListViewItemWithActions.qml
GlacierRoller 2.0 GlacierRoller.qml
GlacierRollerItem 2.0 GlacierRollerItem.qml
InverseMouseArea 2.0 InverseMouseArea.qml
IconButton 2.0 IconButton.qml
DatePicker 2.0 DatePicker.qml
TimePicker 2.0 TimePicker.qml
Expand Down

0 comments on commit 9cbfd68

Please sign in to comment.