Skip to content

Commit

Permalink
[ci release] feature: add experimental color labels (#270)
Browse files Browse the repository at this point in the history
This is work in progress. Allows to assign color labels to selected
text. For now there are 3 color labels: red, green and cyan.
Selected words can be colored with one of these labels using shortcuts:
`Ctrl+Shift+1/2/3`. All labels are cleared using shortcut `Ctrl+Shift+0`.
  • Loading branch information
variar committed Jun 28, 2021
1 parent 0b75b9d commit cd6efb9
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 24 deletions.
5 changes: 5 additions & 0 deletions src/settings/include/shortcuts.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ struct ShortcutAction {

static constexpr auto LogViewExitView = "logview.exit_view";

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 LogViewClearColorLabels = "logview.clear_color_labels";

static const std::map<std::string, QStringList>& defaultShortcuts();

static QStringList defaultShortcuts( const std::string& action );
Expand Down
2 changes: 0 additions & 2 deletions src/settings/include/styles.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#include <QStringList>
#include <QLatin1String>

#include "log.h"

struct StyleManager {

static constexpr QLatin1String DarkStyleKey = QLatin1String( "Dark", 4 );
Expand Down
10 changes: 10 additions & 0 deletions src/settings/src/shortcuts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ const std::map<std::string, QStringList>& ShortcutAction::defaultShortcuts()
shortcuts.emplace( LogViewExitView, QStringList()
<< QKeySequence( Qt::Key_Space ).toString() );

shortcuts.emplace( LogViewAddColorLabel1, QStringList() << "Ctrl+Shift+1" );
shortcuts.emplace( LogViewAddColorLabel2, QStringList() << "Ctrl+Shift+2" );
shortcuts.emplace( LogViewAddColorLabel3, QStringList() << "Ctrl+Shift+3" );
shortcuts.emplace( LogViewClearColorLabels, QStringList() << "Ctrl+Shift+0" );

return shortcuts;
}();

Expand Down Expand Up @@ -201,6 +206,11 @@ 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( LogViewClearColorLabels, "Clear all color labels" );

return shortcuts;
}();

Expand Down
18 changes: 14 additions & 4 deletions src/ui/include/abstractlogview.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@
#ifndef ABSTRACTLOGVIEW_H
#define ABSTRACTLOGVIEW_H

#include <array>
#include <functional>
#include <qcolor.h>
#include <string_view>

#include <QAbstractScrollArea>
#include <QBasicTimer>
#include <QEvent>
#include <vector>

#ifdef GLOGG_PERF_MEASURE_FPS
#include "perfcounter.h"
Expand Down Expand Up @@ -210,6 +213,10 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
void allowFollowMode( bool allow );

void setSearchPattern( const RegularExpressionPattern& pattern );

using WordsHighlighters = std::tuple<QStringList, QColor, QColor>;
void setWordsHighlighters( const std::vector<WordsHighlighters>& wordHighlighters );

void registerShortcuts();

protected:
Expand Down Expand Up @@ -381,8 +388,8 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
OverviewWidget* overviewWidget_ = nullptr;

struct FilePos {
LineNumber line;
int column;
LineNumber line;
int column;
};

bool selectionStarted_ = false;
Expand All @@ -403,6 +410,9 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
Selection selection_;
RegularExpressionPattern searchPattern_;


std::vector<WordsHighlighters> wordsHighlighters_;

// Position of the view, those are crucial to control drawing
// firstLine gives the position of the view,
// lastLineAligned == true make the bottom of the last line aligned
Expand Down Expand Up @@ -476,7 +486,7 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
int convertCoordToColumn( int xPos ) const;

void displayLine( LineNumber line );
void moveSelection( LinesCount delta, bool isDeltaNegative );
void moveSelection( LinesCount delta, bool isDeltaNegative );
void moveSelectionUp();
void moveSelectionDown();
void jumpToStartOfLine();
Expand All @@ -497,7 +507,7 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
void searchUsingFunction( QuickFindSearchFn searchFunction );

void updateScrollBars();

LineNumber verticalScrollToLineNumber( int scrollPosition ) const;
int lineNumberToVerticalScroll( LineNumber line ) const;
double verticalScrollMultiplicator() const;
Expand Down
6 changes: 6 additions & 0 deletions src/ui/include/crawlerwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@ class CrawlerWidget : public QSplitter,

std::vector<LineNumber> savedMarkedLines_;

std::vector<AbstractLogView::WordsHighlighters> wordsHighlighters_ = {
{ {}, {}, Qt::red },
{ {}, {}, Qt::green },
{ {}, {}, Qt::cyan },
};

// Current encoding setting;
std::optional<int> encodingMib_;
QString encodingText_;
Expand Down
66 changes: 48 additions & 18 deletions src/ui/src/abstractlogview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@
#include <cstdint>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <plog/Log.h>
#include <qcoreevent.h>
#include <qevent.h>
#include <qglobal.h>
#include <qnamespace.h>
#include <string_view>
#include <utility>

Expand Down Expand Up @@ -115,7 +117,7 @@ inline int countLeadingZeroes( uint64_t value )
{
unsigned long leading_zero = 0;

if ( _BitScanReverse( &leading_zero, static_cast<uint32_t>(value) ) ) {
if ( _BitScanReverse( &leading_zero, static_cast<uint32_t>( value ) ) ) {
return 63ul - leading_zero;
}
else {
Expand Down Expand Up @@ -1010,6 +1012,13 @@ void AbstractLogView::setSearchPattern( const RegularExpressionPattern& pattern
update();
}

void AbstractLogView::setWordsHighlighters( const std::vector<WordsHighlighters>& wordsHighlighters )
{
wordsHighlighters_ = wordsHighlighters;
textAreaCache_.invalid_ = true;
update();
}

void AbstractLogView::followSet( bool checked )
{
followMode_ = checked;
Expand Down Expand Up @@ -1861,6 +1870,32 @@ void AbstractLogView::drawTextArea( QPaintDevice* paintDevice )
const auto highlightPatternMatches = Configuration::get().mainSearchHighlight();
const auto variateHighlightPatternMatches = Configuration::get().variateMainSearchHighlight();

std::optional<Highlighter> patternHighlight;
if ( highlightPatternMatches && !searchPattern_.isBoolean && !searchPattern_.isExclude
&& !searchPattern_.pattern.isEmpty() ) {
patternHighlight = Highlighter{};
patternHighlight->setHighlightOnlyMatch( true );
patternHighlight->setVariateColors( variateHighlightPatternMatches );
patternHighlight->setPattern( searchPattern_.pattern );
patternHighlight->setIgnoreCase( !searchPattern_.isCaseSensitive );
patternHighlight->setUseRegex( !searchPattern_.isPlainText );

patternHighlight->setBackColor( mainSearchBackColor );
patternHighlight->setForeColor( Qt::black );
}

std::vector<Highlighter> additionalHighlighters;
for ( const auto& wordHighlighter : wordsHighlighters_ ) {
const auto& [ words, wordForeColor, wordBackColor ] = wordHighlighter;

std::transform( words.begin(), words.end(), std::back_inserter( additionalHighlighters ),
[ wordForeColor, wordBackColor ]( const QString& word ) {
Highlighter h{ word, false, true, wordForeColor, wordBackColor };
h.setUseRegex( false );
return h;
} );
}

// Then draw each line
for ( auto currentLine = 0_lcount; currentLine < nbLines; ++currentLine ) {
const auto lineNumber = firstLine_ + currentLine;
Expand Down Expand Up @@ -1893,25 +1928,20 @@ void AbstractLogView::drawTextArea( QPaintDevice* paintDevice )

backColor = palette.color( QPalette::Base );
}
}

if ( highlightPatternMatches && !searchPattern_.isBoolean && !searchPattern_.isExclude
&& !searchPattern_.pattern.isEmpty() ) {
Highlighter patternHighlight;
patternHighlight.setHighlightOnlyMatch( true );
patternHighlight.setVariateColors( variateHighlightPatternMatches );
patternHighlight.setPattern( searchPattern_.pattern );
patternHighlight.setIgnoreCase( !searchPattern_.isCaseSensitive );
patternHighlight.setUseRegex( !searchPattern_.isPlainText );

patternHighlight.setBackColor( mainSearchBackColor );
patternHighlight.setForeColor( Qt::black );

std::vector<HighlightedMatch> patternMatches;
patternHighlight.matchLine( logLine, patternMatches );
if ( patternHighlight ) {
std::vector<HighlightedMatch> patternMatches;
patternHighlight->matchLine( logLine, patternMatches );
highlighterMatches.insert( highlighterMatches.end(), patternMatches.begin(),
patternMatches.end() );
}

highlighterMatches.insert( highlighterMatches.end(), patternMatches.begin(),
patternMatches.end() );
for ( const auto& highlighter : additionalHighlighters ) {
std::vector<HighlightedMatch> patternMatches;
highlighter.matchLine( logLine, patternMatches );
highlighterMatches.insert( highlighterMatches.end(), patternMatches.begin(),
patternMatches.end() );
}
}

const auto untabifyHighlight = [ &logLine ]( const auto& match ) {
Expand Down
31 changes: 31 additions & 0 deletions src/ui/src/crawlerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,37 @@ void CrawlerWidget::registerShortcuts()
}
} );

const auto addColorLabelToSelection = [ this ]( size_t label ) {
std::get<0>( wordsHighlighters_[ label ] ).append( getSelectedText() );
logMainView_->setWordsHighlighters( wordsHighlighters_ );
filteredView_->setWordsHighlighters( wordsHighlighters_ );
};

ShortcutAction::registerShortcut(
configuredShortcuts, shortcuts_, this, Qt::WidgetWithChildrenShortcut,
ShortcutAction::LogViewAddColorLabel1,
[ addColorLabelToSelection ]() { addColorLabelToSelection( 0u ); } );

ShortcutAction::registerShortcut(
configuredShortcuts, shortcuts_, this, Qt::WidgetWithChildrenShortcut,
ShortcutAction::LogViewAddColorLabel2,
[ addColorLabelToSelection ]() { addColorLabelToSelection( 1u ); } );

ShortcutAction::registerShortcut(
configuredShortcuts, shortcuts_, this, Qt::WidgetWithChildrenShortcut,
ShortcutAction::LogViewAddColorLabel3,
[ addColorLabelToSelection ]() { addColorLabelToSelection( 2u ); } );

ShortcutAction::registerShortcut( configuredShortcuts, shortcuts_, this,
Qt::WidgetWithChildrenShortcut,
ShortcutAction::LogViewClearColorLabels, [ this ]() {
for ( auto& wordsHighlighters : wordsHighlighters_ ) {
std::get<0>( wordsHighlighters ).clear();
}
logMainView_->setWordsHighlighters( wordsHighlighters_ );
filteredView_->setWordsHighlighters( wordsHighlighters_ );
} );

logMainView_->registerShortcuts();
filteredView_->registerShortcuts();
}
Expand Down

0 comments on commit cd6efb9

Please sign in to comment.