Skip to content

Commit

Permalink
feat: allow to select cycling color labels (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
variar committed Aug 22, 2021
1 parent c104fd2 commit 4963c2c
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 40 deletions.
3 changes: 2 additions & 1 deletion src/ui/include/colorlabelsmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define KLOGG_COLORLABELSMANAGER_H

#include "abstractlogview.h"
#include <optional>
#include <qobject.h>
#include <qobjectdefs.h>
#include <vector>
Expand All @@ -41,7 +42,7 @@ class ColorLabelsManager {
bool replaceCurrent );

QuickHighlightersCollection quickHighlighters_ = QuickHighlightersCollection{9};
size_t nextQuickHighlighterIndex_ = 0;
std::optional<size_t> currentLabel_;
};

#endif
21 changes: 14 additions & 7 deletions src/ui/include/highlightersdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,30 @@
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<layout class="QGridLayout" name="quickHighlightLayout">
<item row="2" column="1">
<widget class="QLabel" name="colorLabelsForeColor">
<property name="text">
<string>Fore color</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QWidget" name="colorLabelsPlaceholder" native="true"/>
</item>
<item row="2" column="2">
<widget class="QLabel" name="label_5">
<widget class="QLabel" name="colorLabelsBackColor">
<property name="text">
<string>Back color</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label_4">
<item row="2" column="3">
<widget class="QLabel" name="colorLabelsCycle">
<property name="text">
<string>Fore color</string>
<string>Include into cycle</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QWidget" name="placeholder" native="true"/>
</item>
</layout>
</item>
<item>
Expand Down
11 changes: 8 additions & 3 deletions src/ui/include/highlighterset.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ class HighlighterSet {
friend class HighlighterSetEdit;
};

struct QuickHighlighter {
HighlightColor color;
bool useInCycle;
};

class HighlighterSetCollection final : public Persistable<HighlighterSetCollection> {
public:
static const char* persistableName()
Expand All @@ -164,8 +169,8 @@ class HighlighterSetCollection final : public Persistable<HighlighterSetCollecti
QString currentSetId() const;
void setCurrentSet( const QString& setId );

QList<HighlightColor> quickHighlighters() const;
void setQuickHighlighters(const QList<HighlightColor>& quickHighlighters);
QList<QuickHighlighter> quickHighlighters() const;
void setQuickHighlighters(const QList<QuickHighlighter>& quickHighlighters);

// Reads/writes the current config in the QSettings object passed
void saveToStorage( QSettings& settings ) const;
Expand All @@ -178,7 +183,7 @@ class HighlighterSetCollection final : public Persistable<HighlighterSetCollecti
QList<HighlighterSet> highlighters_;
QString currentSet_;

QList<HighlightColor> quickHighlighters_;
QList<QuickHighlighter> quickHighlighters_;

// To simplify this class interface, HighlightersDialog can access our
// internal structure directly.
Expand Down
7 changes: 4 additions & 3 deletions src/ui/src/abstractlogview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,8 @@ void AbstractLogView::mousePressEvent( QMouseEvent* mouseEvent )
colorLabelAction->setData( i );

QPixmap pixmap( 20, 10 );
pixmap.fill( quickHighlightersConfiguration.at( static_cast<int>( i ) ).backColor );
pixmap.fill(
quickHighlightersConfiguration.at( static_cast<int>( i ) ).color.backColor );
colorLabelAction->setIcon( QIcon( pixmap ) );
}
colorLabelsMenu_->addSeparator();
Expand Down Expand Up @@ -1976,8 +1977,8 @@ void AbstractLogView::drawTextArea( QPaintDevice* paintDevice )
std::transform( quickHighlighters_[ i ].begin(), quickHighlighters_[ i ].end(),
std::back_inserter( additionalHighlighters ),
[ quickHighlighter ]( const QString& word ) {
Highlighter h{ word, false, true, quickHighlighter.foreColor,
quickHighlighter.backColor };
Highlighter h{ word, false, true, quickHighlighter.color.foreColor,
quickHighlighter.color.backColor };
h.setUseRegex( false );
return h;
} );
Expand Down
33 changes: 29 additions & 4 deletions src/ui/src/colorlabelsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/

#include "colorlabelsmanager.h"
#include "highlighterset.h"
#include <algorithm>
#include <cstddef>
#include <vector>

ColorLabelsManager::QuickHighlightersCollection ColorLabelsManager::colorLabels() const
{
Expand All @@ -41,12 +45,33 @@ ColorLabelsManager::setColorLabel( size_t label, const QString& text )
ColorLabelsManager::QuickHighlightersCollection
ColorLabelsManager::setNextColorLabel( const QString& text )
{
const auto nextLabel = nextQuickHighlighterIndex_;
nextQuickHighlighterIndex_++;
if ( nextQuickHighlighterIndex_ == quickHighlighters_.size() ) {
nextQuickHighlighterIndex_ = 0;
const auto& quickHighlightersConfiguration
= HighlighterSetCollection::get().quickHighlighters();

std::vector<size_t> cycle;
cycle.reserve( static_cast<size_t>( quickHighlightersConfiguration.size() ) );

for ( auto i = 0; i < quickHighlightersConfiguration.size(); ++i ) {
if ( quickHighlightersConfiguration[ i ].useInCycle ) {
cycle.push_back( static_cast<size_t>( i ) );
}
}

if ( cycle.empty() ) {
return quickHighlighters_;
}

auto nextLabel = cycle.front();

if ( currentLabel_.has_value() ) {
auto nextIt = std::upper_bound( cycle.cbegin(), cycle.cend(), *currentLabel_ );
if ( nextIt != cycle.cend() ) {
nextLabel = *nextIt;
}
}

currentLabel_ = nextLabel;

return updateColorLabel( nextLabel, text, false );
}

Expand Down
35 changes: 29 additions & 6 deletions src/ui/src/highlightersdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <QFileDialog>
#include <QTimer>

#include <qcheckbox.h>
#include <qcolor.h>
#include <qlabel.h>
#include <qnamespace.h>
Expand Down Expand Up @@ -106,21 +107,34 @@ HighlightersDialog::HighlightersDialog( QWidget* parent )
setCurrentRow( 0 );
}

quickHighlightLayout->removeWidget( colorLabelsPlaceholder );
quickHighlightLayout->removeWidget( colorLabelsForeColor );
quickHighlightLayout->removeWidget( colorLabelsBackColor );
quickHighlightLayout->removeWidget( colorLabelsCycle );

quickHighlightLayout->addWidget( colorLabelsPlaceholder, 0, 0 );
quickHighlightLayout->addWidget( colorLabelsForeColor, 0, 1, Qt::AlignCenter );
quickHighlightLayout->addWidget( colorLabelsBackColor, 0, 2, Qt::AlignCenter );
quickHighlightLayout->addWidget( colorLabelsCycle, 0, 3, Qt::AlignCenter );

const auto quickHighlighters = highlighterSetCollection_.quickHighlighters();
for ( int i = 0; i < quickHighlighters.size(); ++i ) {
const auto row = i + 1;
auto quickHighlightLabel = new QLabel( QString( "Color label %1" ).arg( row ) );
auto foreButton = new QPushButton;
auto backButton = new QPushButton;
auto cycleCheckbox = new QCheckBox;

HighlighterEdit::updateIcon( foreButton, quickHighlighters[ i ].foreColor );
HighlighterEdit::updateIcon( backButton, quickHighlighters[ i ].backColor );
HighlighterEdit::updateIcon( foreButton, quickHighlighters[ i ].color.foreColor );
HighlighterEdit::updateIcon( backButton, quickHighlighters[ i ].color.backColor );
cycleCheckbox->setChecked( quickHighlighters[ i ].useInCycle );

connect( foreButton, &QPushButton::clicked, foreButton, [ foreButton, this, index = i ]() {
auto highlighters = highlighterSetCollection_.quickHighlighters();
QColor newColor;
if ( HighlighterEdit::showColorPicker( highlighters[ index ].foreColor, newColor ) ) {
highlighters[ index ].foreColor = newColor;
if ( HighlighterEdit::showColorPicker( highlighters[ index ].color.foreColor,
newColor ) ) {
highlighters[ index ].color.foreColor = newColor;
highlighterSetCollection_.setQuickHighlighters( highlighters );
HighlighterEdit::updateIcon( foreButton, newColor );
}
Expand All @@ -129,16 +143,25 @@ HighlightersDialog::HighlightersDialog( QWidget* parent )
connect( backButton, &QPushButton::clicked, backButton, [ backButton, this, index = i ]() {
auto highlighters = highlighterSetCollection_.quickHighlighters();
QColor newColor;
if ( HighlighterEdit::showColorPicker( highlighters[ index ].backColor, newColor ) ) {
highlighters[ index ].backColor = newColor;
if ( HighlighterEdit::showColorPicker( highlighters[ index ].color.backColor,
newColor ) ) {
highlighters[ index ].color.backColor = newColor;
highlighterSetCollection_.setQuickHighlighters( highlighters );
HighlighterEdit::updateIcon( backButton, newColor );
}
} );

connect( cycleCheckbox, &QCheckBox::clicked, cycleCheckbox,
[ this, index = i ]( bool isChecked ) {
auto highlighters = highlighterSetCollection_.quickHighlighters();
highlighters[ index ].useInCycle = isChecked;
highlighterSetCollection_.setQuickHighlighters( highlighters );
} );

quickHighlightLayout->addWidget( quickHighlightLabel, row, 0 );
quickHighlightLayout->addWidget( foreButton, row, 1 );
quickHighlightLayout->addWidget( backButton, row, 2 );
quickHighlightLayout->addWidget( cycleCheckbox, row, 3, Qt::AlignCenter );
}

dispatchToMainThread( [ this ] {
Expand Down
37 changes: 21 additions & 16 deletions src/ui/src/highlighterset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,13 @@ bool HighlighterSetCollection::hasSet( const QString& setId ) const
[ setId ]( const auto& s ) { return s.id() == setId; } );
}

QList<HighlightColor> HighlighterSetCollection::quickHighlighters() const
QList<QuickHighlighter> HighlighterSetCollection::quickHighlighters() const
{
return quickHighlighters_;
}

void HighlighterSetCollection::setQuickHighlighters(
const QList<HighlightColor>& quickHighlighters )
const QList<QuickHighlighter>& quickHighlighters )
{
quickHighlighters_ = quickHighlighters;
}
Expand All @@ -426,9 +426,10 @@ void HighlighterSetCollection::saveToStorage( QSettings& settings ) const
for ( int i = 0; i < quickHighlighters_.size(); ++i ) {
settings.setArrayIndex( i );
settings.setValue( "fore_colour",
quickHighlighters_[ i ].foreColor.name( QColor::HexArgb ) );
quickHighlighters_[ i ].color.foreColor.name( QColor::HexArgb ) );
settings.setValue( "back_colour",
quickHighlighters_[ i ].backColor.name( QColor::HexArgb ) );
quickHighlighters_[ i ].color.backColor.name( QColor::HexArgb ) );
settings.setValue( "cycle", quickHighlighters_[ i ].useInCycle );
}
settings.endArray();
settings.endGroup();
Expand Down Expand Up @@ -458,24 +459,28 @@ void HighlighterSetCollection::retrieveFromStorage( QSettings& settings )
size = settings.beginReadArray( "quick" );
for ( int i = 0; i < size; ++i ) {
settings.setArrayIndex( i );
HighlightColor quickHighlighter;
quickHighlighter.foreColor = QColor( settings.value( "fore_colour" ).toString() );
quickHighlighter.backColor = QColor( settings.value( "back_colour" ).toString() );
QuickHighlighter quickHighlighter;
quickHighlighter.color.foreColor
= QColor( settings.value( "fore_colour" ).toString() );
quickHighlighter.color.backColor
= QColor( settings.value( "back_colour" ).toString() );
quickHighlighter.useInCycle = settings.value( "cycle", true ).toBool();

quickHighlighters_.append( std::move( quickHighlighter ) );
}
settings.endArray();
if ( quickHighlighters_.size() != 9 ) {
LOG_WARNING << "Got " << quickHighlighters_.size() << " quick highlighters";
quickHighlighters_.clear();
quickHighlighters_.append( { QColor{}, Qt::red } );
quickHighlighters_.append( { QColor{}, Qt::green } );
quickHighlighters_.append( { QColor{}, Qt::cyan } );
quickHighlighters_.append( { QColor{}, Qt::darkRed } );
quickHighlighters_.append( { QColor{}, Qt::darkGreen } );
quickHighlighters_.append( { QColor{}, Qt::darkCyan } );
quickHighlighters_.append( { QColor{}, Qt::magenta } );
quickHighlighters_.append( { QColor{}, Qt::darkMagenta } );
quickHighlighters_.append( { QColor{}, Qt::gray } );
quickHighlighters_.append( { { QColor{}, Qt::red }, true } );
quickHighlighters_.append( { { QColor{}, Qt::green }, true } );
quickHighlighters_.append( { { QColor{}, Qt::cyan }, true } );
quickHighlighters_.append( { { QColor{}, Qt::darkRed }, true } );
quickHighlighters_.append( { { QColor{}, Qt::darkGreen }, true } );
quickHighlighters_.append( { { QColor{}, Qt::darkCyan }, true } );
quickHighlighters_.append( { { QColor{}, Qt::magenta }, true } );
quickHighlighters_.append( { { QColor{}, Qt::darkMagenta }, true } );
quickHighlighters_.append( { { QColor{}, Qt::gray }, true } );
}
}
else {
Expand Down

0 comments on commit 4963c2c

Please sign in to comment.