Skip to content

Commit

Permalink
feat: trigger the highlisters menu without closing (#660) (#551)
Browse files Browse the repository at this point in the history
* feat: trigger the highlisters menu without closing(#551)

* fix: display error of None Action in the highlighters menu

* fix: Right-click menu highlighters are invalid in other logview
  • Loading branch information
nowhszh committed Aug 13, 2023
1 parent 340261c commit e7b361f
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 110 deletions.
5 changes: 3 additions & 2 deletions src/ui/CMakeLists.txt
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 @@ -56,6 +56,7 @@ add_library(
${CMAKE_CURRENT_SOURCE_DIR}/src/filteredview.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/highlightersdialog.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/highlighteredit.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/highlightersmenu.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/highlightersetedit.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/highlighterset.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/predefinedfilters.cpp
Expand All @@ -66,7 +67,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
5 changes: 3 additions & 2 deletions src/ui/include/abstractlogview.h
Expand Up @@ -69,6 +69,7 @@
class QMenu;
class QAction;
class QShortcut;
class HighlightersMenu;

// Utility class representing a buffer for number entered on the keyboard
// The buffer keep at most 7 digits, and reset itself after a timeout.
Expand Down Expand Up @@ -237,6 +238,7 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
void addColorLabel( size_t label );
void addNextColorLabel();
void clearColorLabels();
void highlightersChange();

public Q_SLOTS:
// Makes the widget select and display the passed line.
Expand Down Expand Up @@ -299,7 +301,6 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
void setSelectionStart();
void setSelectionEnd();
void setQuickFindResult( bool hasMatch, const Portion& selection );
void setHighlighterSet( QAction* action );
void setColorLabel( QAction* action );

private:
Expand Down Expand Up @@ -397,7 +398,7 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
QAction* setSelectionStartAction_;
QAction* setSelectionEndAction_;
QAction* saveDefaultSplitterSizesAction_;
QMenu* highlightersMenu_;
HighlightersMenu* highlightersMenu_;
QMenu* colorLabelsMenu_;

std::map<QString, QShortcut*> shortcuts_;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/include/highlighterset.h
Expand Up @@ -44,9 +44,9 @@
#include <QRegularExpression>
#include <qcolor.h>

#include "containers.h"
#include "highlightedmatch.h"
#include "persistable.h"
#include "containers.h"

struct HighlightColor {
QColor foreColor;
Expand Down
80 changes: 35 additions & 45 deletions src/ui/include/highlightersmenu.h
Expand Up @@ -22,61 +22,51 @@

#include "highlighterset.h"

#include <functional>

#include <QAction>
#include <QActionGroup>
#include <QMenu>

inline void populateHighlightersMenu( QMenu* highlightersMenu,
QActionGroup* highlightersActionGroup )
{
const auto& highlightersCollection = HighlighterSetCollection::get();
const auto& highlighterSets = highlightersCollection.highlighterSets();
const auto& activeSetIds = highlightersCollection.activeSetIds();

auto noneAction = highlightersMenu->addAction( QApplication::tr( "None" ) );
noneAction->setActionGroup( highlightersActionGroup );
noneAction->setEnabled( !activeSetIds.isEmpty() );

highlightersMenu->addSeparator();
for ( const auto& highlighter : qAsConst( highlighterSets ) ) {
auto setAction = highlightersMenu->addAction( highlighter.name() );
setAction->setActionGroup( highlightersActionGroup );
setAction->setCheckable( true );
setAction->setChecked( activeSetIds.contains( highlighter.id() ) );
setAction->setData( highlighter.id() );
}
}
#include "menu.h"

inline void setCurrentHighlighterAction( QActionGroup* highlightersActionGroup )
{
const auto& highlightersCollection = HighlighterSetCollection::get();
const auto activeSets = highlightersCollection.activeSetIds();
class CrawlerWidget;

const auto selectNone = activeSets.isEmpty();
class HighlightersMenu : public HoverMenu {
Q_OBJECT

for ( auto* action : highlightersActionGroup->actions() ) {
const auto actionSet = action->data().toString();
action->setChecked( activeSets.contains( actionSet )
|| ( actionSet.isEmpty() && selectNone ) );
}
}
public:
using HoverMenu::addAction;

inline void saveCurrentHighlighterFromAction( const QAction* action )
{
auto setId = action->data().toString();
auto& highlighterSets = HighlighterSetCollection::get();
HighlightersMenu( const QString& title, QWidget* parent = nullptr );

if ( setId.isEmpty() ) {
highlighterSets.deactivateAll();
}
else if ( action->isChecked() ) {
highlighterSets.activateSet( setId );
}
else {
highlighterSets.deactivateSet( setId );
void addAction( QAction* action, bool seq );

void clearHighlightersMenu();

void createHighlightersMenu();

void populateHighlightersMenu();

inline void setApplyChange( std::function<void()> apply )
{
applyChange_ = apply;
}

highlighterSets.save();
}
private:
using HoverMenu::clear;

// save highlighter action
void saveCurrentHighlighterFromAction( const QAction* action ) const;

private Q_SLOTS:
void applySelectionHighlighters( QAction* action ) const;

void updateActionsStatus() const;

private:
QActionGroup* highLighters_;
std::function<void()> applyChange_;
};

#endif
5 changes: 3 additions & 2 deletions src/ui/include/mainwindow.h
Expand Up @@ -40,6 +40,7 @@
#define MAINWINDOW_H

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

Expand All @@ -64,6 +65,7 @@ class QAction;
class QActionGroup;
class Session;
class RecentFiles;
class HighlightersMenu;

// Main window of the application, creates menus, toolbar and
// the CrawlerWidget
Expand Down Expand Up @@ -108,7 +110,6 @@ class MainWindow : public QMainWindow {
void openFileFromRecent( QAction* action );
void openFileFromFavorites( QAction* action );
void switchToOpenedFile( QAction* action );
void setCurrentHighlighter( QAction* action );
void closeTab( ActionInitiator initiator );
void closeAll( ActionInitiator initiator );
void selectAll();
Expand Down Expand Up @@ -236,7 +237,7 @@ class MainWindow : public QMainWindow {
QMenu* viewMenu;
QMenu* toolsMenu;
QMenu* favoritesMenu;
QMenu* highlightersMenu;
HighlightersMenu* highlightersMenu;
QMenu* openedFilesMenu;
QMenu* helpMenu;

Expand Down
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
21 changes: 7 additions & 14 deletions src/ui/src/abstractlogview.cpp
Expand Up @@ -655,12 +655,9 @@ void AbstractLogView::mousePressEvent( QMouseEvent* mouseEvent )
replaceSearchAction_->setEnabled( false );
}

auto highlightersActionGroup = new QActionGroup( this );
highlightersActionGroup->setExclusive( false );
connect( highlightersActionGroup, &QActionGroup::triggered, this,
&AbstractLogView::setHighlighterSet );
highlightersMenu_->clear();
populateHighlightersMenu( highlightersMenu_, highlightersActionGroup );
highlightersMenu_->createHighlightersMenu();
highlightersMenu_->populateHighlightersMenu();
highlightersMenu_->setApplyChange( [ this ]() { Q_EMIT highlightersChange(); } );

auto colorLablesActionGroup = new QActionGroup( this );
connect( colorLablesActionGroup, &QActionGroup::triggered, this,
Expand Down Expand Up @@ -717,7 +714,8 @@ void AbstractLogView::mousePressEvent( QMouseEvent* mouseEvent )
}
// Display the popup (blocking)
popupMenu_->exec( QCursor::pos( activeScreen( this ) ) );
highlightersActionGroup->deleteLater();

highlightersMenu_->clearHighlightersMenu();
colorLablesActionGroup->deleteLater();
}

Expand Down Expand Up @@ -2179,7 +2177,8 @@ void AbstractLogView::createMenu()
[ this ]( auto ) { Q_EMIT replaceScratchpadWithSelection(); } );

popupMenu_ = new QMenu( this );
highlightersMenu_ = popupMenu_->addMenu( tr( "Highlighters" ) );
highlightersMenu_ = new HighlightersMenu( tr( "Highlighters" ) );
popupMenu_->addMenu( highlightersMenu_ );
colorLabelsMenu_ = popupMenu_->addMenu( tr( "Color labels" ) );

popupMenu_->addSeparator();
Expand Down Expand Up @@ -2711,12 +2710,6 @@ void AbstractLogView::disableFollow()
followElasticHook_.hook( false );
}

void AbstractLogView::setHighlighterSet( QAction* action )
{
saveCurrentHighlighterFromAction( action );
forceRefresh();
}

void AbstractLogView::setColorLabel( QAction* action )
{
if ( action->data().isValid() ) {
Expand Down
5 changes: 5 additions & 0 deletions src/ui/src/crawlerwidget.cpp
Expand Up @@ -1207,6 +1207,9 @@ void CrawlerWidget::setup()

connect( logMainView_, &LogMainView::markLines, this, &CrawlerWidget::markLinesFromMain );

connect( logMainView_, &LogMainView::highlightersChange, this,
&CrawlerWidget::applyConfiguration );

connect( logMainView_, QOverload<const QString&>::of( &LogMainView::addToSearch ), this,
&CrawlerWidget::addToSearch );

Expand Down Expand Up @@ -1363,6 +1366,8 @@ void CrawlerWidget::connectAllFilteredViewSlots( FilteredView* view )

connect( view, &FilteredView::markLines, this, &CrawlerWidget::markLinesFromFiltered );

connect( view, &FilteredView::highlightersChange, this, &CrawlerWidget::applyConfiguration );

connect( view, QOverload<const QString&>::of( &FilteredView::addToSearch ), this,
&CrawlerWidget::addToSearch );

Expand Down

0 comments on commit e7b361f

Please sign in to comment.