Skip to content

Commit

Permalink
feat: add context menu for color labels (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
variar committed Aug 22, 2021
1 parent 0aa6f8c commit c104fd2
Show file tree
Hide file tree
Showing 12 changed files with 468 additions and 232 deletions.
7 changes: 7 additions & 0 deletions src/settings/include/shortcuts.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ struct ShortcutAction {
static constexpr auto LogViewAddColorLabel1 = "logview.add_color_label_1";
static constexpr auto LogViewAddColorLabel2 = "logview.add_color_label_2";
static constexpr auto LogViewAddColorLabel3 = "logview.add_color_label_3";
static constexpr auto LogViewAddColorLabel4 = "logview.add_color_label_4";
static constexpr auto LogViewAddColorLabel5 = "logview.add_color_label_5";
static constexpr auto LogViewAddColorLabel6 = "logview.add_color_label_6";
static constexpr auto LogViewAddColorLabel7 = "logview.add_color_label_7";
static constexpr auto LogViewAddColorLabel8 = "logview.add_color_label_8";
static constexpr auto LogViewAddColorLabel9 = "logview.add_color_label_9";

static constexpr auto LogViewClearColorLabels = "logview.clear_color_labels";
static constexpr auto LogViewAddNextColorLabel = "logview.add_next_color_label";

Expand Down
21 changes: 17 additions & 4 deletions src/settings/src/shortcuts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ const std::map<std::string, QStringList>& ShortcutAction::defaultShortcuts()
shortcuts.emplace( LogViewAddColorLabel1, QStringList() << "Ctrl+Shift+1" );
shortcuts.emplace( LogViewAddColorLabel2, QStringList() << "Ctrl+Shift+2" );
shortcuts.emplace( LogViewAddColorLabel3, QStringList() << "Ctrl+Shift+3" );
shortcuts.emplace( LogViewAddColorLabel4, QStringList() << "Ctrl+Shift+4" );
shortcuts.emplace( LogViewAddColorLabel5, QStringList() << "Ctrl+Shift+5" );
shortcuts.emplace( LogViewAddColorLabel6, QStringList() << "Ctrl+Shift+6" );
shortcuts.emplace( LogViewAddColorLabel7, QStringList() << "Ctrl+Shift+7" );
shortcuts.emplace( LogViewAddColorLabel8, QStringList() << "Ctrl+Shift+8" );
shortcuts.emplace( LogViewAddColorLabel9, QStringList() << "Ctrl+Shift+9" );
shortcuts.emplace( LogViewClearColorLabels, QStringList() << "Ctrl+Shift+0" );
shortcuts.emplace( LogViewAddNextColorLabel, QStringList() << "Ctrl+D" );

Expand Down Expand Up @@ -209,10 +215,17 @@ QString ShortcutAction::actionName( const std::string& action )

shortcuts.emplace( LogViewExitView, "Release focus from view" );

shortcuts.emplace( LogViewAddColorLabel1, "Highlight selected text with color 1" );
shortcuts.emplace( LogViewAddColorLabel2, "Highlight selected text with color 2" );
shortcuts.emplace( LogViewAddColorLabel3, "Highlight selected text with color 3" );
shortcuts.emplace( LogViewAddNextColorLabel, "Highlight selected text with next color" );
shortcuts.emplace( LogViewAddColorLabel1, "Highlight text with color 1" );
shortcuts.emplace( LogViewAddColorLabel2, "Highlight text with color 2" );
shortcuts.emplace( LogViewAddColorLabel3, "Highlight text with color 3" );
shortcuts.emplace( LogViewAddColorLabel4, "Highlight text with color 4" );
shortcuts.emplace( LogViewAddColorLabel5, "Highlight text with color 5" );
shortcuts.emplace( LogViewAddColorLabel6, "Highlight text with color 6" );
shortcuts.emplace( LogViewAddColorLabel7, "Highlight text with color 7" );
shortcuts.emplace( LogViewAddColorLabel8, "Highlight text with color 8" );
shortcuts.emplace( LogViewAddColorLabel9, "Highlight text with color 9" );

shortcuts.emplace( LogViewAddNextColorLabel, "Highlight text with next color" );

shortcuts.emplace( LogViewClearColorLabels, "Clear all color labels" );

Expand Down
3 changes: 3 additions & 0 deletions src/ui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ add_library(ui STATIC
${CMAKE_CURRENT_SOURCE_DIR}/include/downloader.h
${CMAKE_CURRENT_SOURCE_DIR}/include/decompressor.h
${CMAKE_CURRENT_SOURCE_DIR}/include/fontutils.h
${CMAKE_CURRENT_SOURCE_DIR}/include/colorlabelsmanager.h

${CMAKE_CURRENT_SOURCE_DIR}/include/highlighteredit.ui
${CMAKE_CURRENT_SOURCE_DIR}/include/highlightersetedit.ui
Expand Down Expand Up @@ -88,6 +89,8 @@ add_library(ui STATIC
${CMAKE_CURRENT_SOURCE_DIR}/src/displayfilepath.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/downloader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/decompressor.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/colorlabelsmanager.cpp

)

set_target_properties(ui PROPERTIES AUTOUIC ON)
Expand Down
23 changes: 14 additions & 9 deletions src/ui/include/abstractlogview.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@
#define ABSTRACTLOGVIEW_H

#include <array>
#include <cstddef>
#include <functional>
#include <vector>
#include <string_view>
#include <vector>

#include <QAbstractScrollArea>
#include <QBasicTimer>
#include <QEvent>
#include <QColor>
#include <QEvent>
#include <QFontMetrics>


#ifdef GLOGG_PERF_MEASURE_FPS
#include "perfcounter.h"
#endif
Expand Down Expand Up @@ -193,7 +193,7 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
AbstractLogView& operator=( const AbstractLogView& ) = delete;
AbstractLogView& operator=( AbstractLogView&& ) = delete;

void updateFont(const QFont& font);
void updateFont( const QFont& font );

// Refresh the widget when the data set has changed.
void updateData();
Expand All @@ -218,8 +218,8 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter

void setSearchPattern( const RegularExpressionPattern& pattern );

using WordsHighlighters = QStringList;
void setWordsHighlighters( const std::vector<WordsHighlighters>& wordHighlighters );
using QuickHighlighters = QStringList;
void setQuickHighlighters( const std::vector<QuickHighlighters>& wordHighlighters );

void registerShortcuts();

Expand Down Expand Up @@ -299,7 +299,11 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
void clearSearchLimits();

void saveDefaultSplitterSizes();
void changeFontSize(bool increase);
void changeFontSize( bool increase );

void addColorLabel( size_t label );
void addNextColorLabel();
void clearColorLabels();

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

private:
// Graphic parameters
Expand Down Expand Up @@ -415,8 +420,7 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
Selection selection_;
RegularExpressionPattern searchPattern_;


std::vector<WordsHighlighters> wordsHighlighters_;
std::vector<QuickHighlighters> quickHighlighters_ = std::vector<QuickHighlighters>{9};

// Position of the view, those are crucial to control drawing
// firstLine gives the position of the view,
Expand Down Expand Up @@ -452,6 +456,7 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
QAction* setSelectionEndAction_;
QAction* saveDefaultSplitterSizesAction_;
QMenu* highlightersMenu_;
QMenu* colorLabelsMenu_;

std::map<QString, QShortcut*> shortcuts_;

Expand Down
47 changes: 47 additions & 0 deletions src/ui/include/colorlabelsmanager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2021 Anton Filimonov and other contributors
*
* This file is part of klogg.
*
* klogg 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 3 of the License, or
* (at your option) any later version.
*
* klogg 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 klogg. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef KLOGG_COLORLABELSMANAGER_H
#define KLOGG_COLORLABELSMANAGER_H

#include "abstractlogview.h"
#include <qobject.h>
#include <qobjectdefs.h>
#include <vector>

class ColorLabelsManager {
public:
using QuickHighlightersCollection = std::vector<AbstractLogView::QuickHighlighters>;

QuickHighlightersCollection colorLabels() const;

QuickHighlightersCollection setColorLabel( size_t label, const QString& text );
QuickHighlightersCollection setNextColorLabel( const QString& text );

QuickHighlightersCollection clear();

private:
QuickHighlightersCollection updateColorLabel( size_t label, const QString& text,
bool replaceCurrent );

QuickHighlightersCollection quickHighlighters_ = QuickHighlightersCollection{9};
size_t nextQuickHighlighterIndex_ = 0;
};

#endif
16 changes: 9 additions & 7 deletions src/ui/include/crawlerwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include <QToolButton>
#include <QVBoxLayout>

#include "colorlabelsmanager.h"
#include "data/loadingstatus.h"
#include "data/logdata.h"
#include "data/logfiltereddata.h"
Expand Down Expand Up @@ -247,6 +248,10 @@ class CrawlerWidget : public QSplitter,
void setSearchLimits( LineNumber startLine, LineNumber endLine );
void clearSearchLimits();

void addColorLabelToSelection( size_t label );
void addNextColorLabelToSelection();
void clearColorLabels();

private:
// State machine holding the state of the search, used to allow/disallow
// auto-refresh and inform the user via the info line.
Expand Down Expand Up @@ -319,6 +324,8 @@ class CrawlerWidget : public QSplitter,

void resetStateOnSearchPatternChanges();

void updateColorLabels( const ColorLabelsManager::QuickHighlightersCollection& labels );

// Palette for error notification (yellow background)
static const QPalette ErrorPalette;

Expand Down Expand Up @@ -390,16 +397,11 @@ class CrawlerWidget : public QSplitter,

std::vector<LineNumber> savedMarkedLines_;

std::vector<AbstractLogView::WordsHighlighters> wordsHighlighters_ = {
{ {} },
{ {} },
{ {} },
};
size_t nextWordsHighlighterIndex_ = 0;

// Current encoding setting;
std::optional<int> encodingMib_;
QString encodingText_;

ColorLabelsManager colorLabelsManager_;
};

#endif
Loading

0 comments on commit c104fd2

Please sign in to comment.