Skip to content

Commit

Permalink
feat: trigger the highlisters menu without closing(variar#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
nowhszh committed Aug 3, 2023
1 parent 88abcec commit 33ed5fb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/ui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ add_library(
${CMAKE_CURRENT_SOURCE_DIR}/include/logmainview.h
${CMAKE_CURRENT_SOURCE_DIR}/include/mainwindow.h
${CMAKE_CURRENT_SOURCE_DIR}/include/mainwindowtext.h
${CMAKE_CURRENT_SOURCE_DIR}/include/menuactiontooltipbehavior.h
${CMAKE_CURRENT_SOURCE_DIR}/include/menu.h
${CMAKE_CURRENT_SOURCE_DIR}/include/optionsdialog.h
${CMAKE_CURRENT_SOURCE_DIR}/include/overview.h
${CMAKE_CURRENT_SOURCE_DIR}/include/overviewwidget.h
Expand Down Expand Up @@ -66,7 +66,7 @@ add_library(
${CMAKE_CURRENT_SOURCE_DIR}/src/logmainview.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/mainwindow.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/mainwindowtext.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/menuactiontooltipbehavior.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/menu.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/optionsdialog.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/overview.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/overviewwidget.cpp
Expand Down
1 change: 1 addition & 0 deletions src/ui/include/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QTemporaryDir>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,61 @@
* along with glogg. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef MANUACTIONTOOLTIPBEHAVIOR_H
#define MANUACTIONTOOLTIPBEHAVIOR_H
#ifndef KLOGG_MENU_H
#define KLOGG_MENU_H

#include <QMenu>
#include <QObject>
#include <QPoint>

class QAction;
class QMenu;
class QTimerEvent;

class QPoint;

// Provides a behavior to show an action's tooltip after mouse is unmoved for
// a specified number of 'ms'. E.g. used for tooltips with full-path for recent
// files in the file menu. Not thread-safe.
class MenuActionToolTipBehavior : public QObject
{
class MenuActionToolTipBehavior : public QObject {
Q_OBJECT

public:
MenuActionToolTipBehavior(QAction *menuAction, QMenu *menuParent,
QObject *parent);
public:
MenuActionToolTipBehavior( QAction* menuAction, QMenu* menuParent, QObject* parent );

// Time in ms that mouse needs to stay unmoved for tooltip to be shown
int toolTipDelay(); /* ms */
void setToolTipDelay(int ms);
void setToolTipDelay( int ms );

private:
void timerEvent(QTimerEvent *event) override;
void showToolTip(const QPoint &position);
private:
void timerEvent( QTimerEvent* event ) override;
void showToolTip( const QPoint& position );

private Q_SLOTS:
private Q_SLOTS:
void onActionHovered();

private:
QAction *action;
QMenu *parentMenu;
private:
QAction* action;
QMenu* parentMenu;
int toolTipDelayMs;
int timerId;
QPoint hoverPoint;
};

#endif
class HoverMenu : public QMenu {
public:
explicit HoverMenu( const QString& title, QWidget* parent = nullptr );

void mouseMoveEvent( QMouseEvent* ) override;
void mouseReleaseEvent( QMouseEvent* ) override;

private:
inline bool mouseInMenu( const QPoint& pos )
{
return this->rect().contains( pos );
}

private:
bool mouseInMenu_;
};

#endif // KLOGG_MENU_H
4 changes: 3 additions & 1 deletion src/ui/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
#include "klogg_version.h"
#include "logger.h"
#include "mainwindowtext.h"
#include "menu.h"
#include "openfilehelper.h"
#include "optionsdialog.h"
#include "predefinedfiltersdialog.h"
Expand Down Expand Up @@ -790,7 +791,8 @@ void MainWindow::createMenus()

toolsMenu = menuBar()->addMenu( tr( menu::toolsTitle ) );

highlightersMenu = menuBar()->addMenu( tr( menu::highlightersTitle ) );
highlightersMenu = new HoverMenu( tr( menu::highlightersTitle ), menuBar() );
menuBar()->addMenu( highlightersMenu );
connect( highlightersMenu, &QMenu::aboutToShow,
[ this ]() { setCurrentHighlighterAction( highlightersActionGroup ); } );

Expand Down
36 changes: 35 additions & 1 deletion src/ui/src/menuactiontooltipbehavior.cpp → src/ui/src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

#include <QAction>
#include <QMenu>
#include <QMouseEvent>
#include <QRect>
#include <QTimerEvent>
#include <QToolTip>

#include "active_screen.h"
#include "menuactiontooltipbehavior.h"
#include "menu.h"

// It would be nice to only need action, and have action be the parent,
// however implementation needs the parent menu (see showToolTip), and
Expand Down Expand Up @@ -108,3 +110,35 @@ void MenuActionToolTipBehavior::showToolTip( const QPoint& position )
QRect activeRegion( relativePos.x(), relativePos.y(), 1, 1 );
QToolTip::showText( position, toolTip, parentMenu, activeRegion );
}

HoverMenu::HoverMenu( const QString& title, QWidget* parent )
: QMenu( title, parent )
, mouseInMenu_{ false }
{
}

void HoverMenu::mouseMoveEvent( QMouseEvent* ev )
{
auto nowMousePosition = mouseInMenu( ev->pos() );

// hide the menu list if the mouse leaves the menu.
if ( mouseInMenu_ && !nowMousePosition ) {
this->hide();
}
mouseInMenu_ = nowMousePosition;

QMenu::mouseMoveEvent( ev );
}

void HoverMenu::mouseReleaseEvent( QMouseEvent* ev )
{
auto* action = this->actionAt( ev->pos() );

// responds to left-click event only
if ( action != nullptr && ev->button() == Qt::LeftButton ) {
action->activate( QAction::ActionEvent::Trigger );
}
else {
QMenu::mouseReleaseEvent( ev );
}
}

0 comments on commit 33ed5fb

Please sign in to comment.