diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -5,6 +5,7 @@ previewplugin.cpp sourcesmodel.cpp draghelper.cpp + mousehelper.cpp ) add_library(milou SHARED ${lib_SRCS}) diff --git a/lib/mousehelper.h b/lib/mousehelper.h new file mode 100644 --- /dev/null +++ b/lib/mousehelper.h @@ -0,0 +1,44 @@ +/* + * Copyright 2017 Aetf + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef MOUSEHELPER_H +#define MOUSEHELPER_H + +#include +#include + +#include "milou_export.h" + +namespace Milou { + +class MILOU_EXPORT MouseHelper : public QObject +{ + Q_OBJECT + +public: + explicit MouseHelper(QObject* parent = nullptr); + ~MouseHelper(); + + Q_INVOKABLE QPointF globalMousePosition() const; +}; + +} +#endif // MOUSEHELPER_H diff --git a/lib/mousehelper.cpp b/lib/mousehelper.cpp new file mode 100644 --- /dev/null +++ b/lib/mousehelper.cpp @@ -0,0 +1,40 @@ +/* + * Copyright 2017 Aetf + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "mousehelper.h" + +#include + +using namespace Milou; + +MouseHelper::MouseHelper(QObject* parent) + : QObject(parent) +{ +} + +MouseHelper::~MouseHelper() +{ +} + +QPointF MouseHelper::globalMousePosition() const +{ + return QCursor::pos(); +} diff --git a/lib/qml/ResultDelegate.qml b/lib/qml/ResultDelegate.qml --- a/lib/qml/ResultDelegate.qml +++ b/lib/qml/ResultDelegate.qml @@ -81,10 +81,6 @@ acceptedButtons: Qt.LeftButton hoverEnabled: true - onEntered: { - listView.currentIndex = index - } - onPressed: { __pressed = true; __pressX = mouse.x; @@ -112,13 +108,25 @@ __pressY = -1; } } + + if (!listView.moved && listView.mouseMovedGlobally()) { + listView.moved = true + listView.currentIndex = index + } } onContainsMouseChanged: { if (!containsMouse) { __pressed = false; __pressX = -1; __pressY = -1; + } else { + if (listView.moved) { + listView.currentIndex = index + } else if (listView.mouseMovedGlobally()) { + listView.moved = true + listView.currentIndex = index + } } } diff --git a/lib/qml/ResultsView.qml b/lib/qml/ResultsView.qml --- a/lib/qml/ResultsView.qml +++ b/lib/qml/ResultsView.qml @@ -54,6 +54,13 @@ // be run when the model is populated property bool runAutomatically + // This is used to disable mouse selection if the user interacts only with keyboard + property bool moved: false + property point savedMousePosition: Milou.MouseHelper.globalMousePosition() + function mouseMovedGlobally() { + return savedMousePosition != Milou.MouseHelper.globalMousePosition(); + } + Milou.DragHelper { id: dragHelper dragIconSize: units.iconSizes.medium @@ -67,6 +74,8 @@ // and the results are presented onModelReset: { listView.currentIndex = 0 + listView.moved = false + listView.savedMousePosition = Milou.MouseHelper.globalMousePosition() if (runAutomatically) { runCurrentIndex(); diff --git a/lib/qml/qmlplugins.cpp b/lib/qml/qmlplugins.cpp --- a/lib/qml/qmlplugins.cpp +++ b/lib/qml/qmlplugins.cpp @@ -25,6 +25,7 @@ #include "sourcesmodel.h" #include "preview.h" #include "draghelper.h" +#include "mousehelper.h" #include @@ -37,5 +38,9 @@ qmlRegisterType (uri, 0, 1, "SourcesModel"); qmlRegisterType (uri, 0, 1, "Preview"); qmlRegisterType (uri, 0, 2, "DragHelper"); + qmlRegisterSingletonType (uri, 0, 1, "MouseHelper", + [](QQmlEngine*, QJSEngine*) -> QObject* { + return new Milou::MouseHelper(); + }); }