Skip to content

Commit

Permalink
feat: allow to activate multiple highlighter sets (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
variar committed Aug 26, 2021
1 parent 567df0c commit cfe29e6
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 53 deletions.
22 changes: 13 additions & 9 deletions src/ui/include/highlighterset.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ class HighlighterSet {
// To simplify this class interface, HighlightersDialog can access our
// internal structure directly.
friend class HighlighterSetEdit;
friend class HighlighterSetCollection;
};

struct QuickHighlighter {
QString name;
HighlightColor color;
bool useInCycle;
QString name;
HighlightColor color;
bool useInCycle;
};

class HighlighterSetCollection final : public Persistable<HighlighterSetCollection> {
Expand All @@ -164,25 +165,28 @@ class HighlighterSetCollection final : public Persistable<HighlighterSetCollecti
QList<HighlighterSet> highlighterSets() const;
void setHighlighterSets( const QList<HighlighterSet>& highlighters );

HighlighterSet currentSet() const;
HighlighterSet currentActiveSet() const;

bool hasSet( const QString& setId ) const;
QString currentSetId() const;
void setCurrentSet( const QString& setId );

QStringList activeSetIds() const;
void activateSet( const QString& setId );
void deactivateSet( const QString& setId );
void deactivateAll();

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

// Reads/writes the current config in the QSettings object passed
void saveToStorage( QSettings& settings ) const;
void retrieveFromStorage( QSettings& settings );

private:
static constexpr int HighlighterSetCollection_VERSION = 1;
static constexpr int HighlighterSetCollection_VERSION = 2;

private:
QList<HighlighterSet> highlighters_;
QString currentSet_;
QStringList activeSets_;

QList<QuickHighlighter> quickHighlighters_;

Expand Down
26 changes: 18 additions & 8 deletions src/ui/include/highlightersmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,51 @@ inline void populateHighlightersMenu( QMenu* highlightersMenu,
{
const auto& highlightersCollection = HighlighterSetCollection::get();
const auto& highlighterSets = highlightersCollection.highlighterSets();
const auto currentSetId = highlightersCollection.currentSetId();
const auto& activeSetIds = highlightersCollection.activeSetIds();

auto noneAction = highlightersMenu->addAction( "None" );
noneAction->setActionGroup( highlightersActionGroup );
noneAction->setCheckable( true );
noneAction->setChecked( !highlightersCollection.hasSet( currentSetId ) );
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( currentSetId == highlighter.id() );
setAction->setChecked( activeSetIds.contains( highlighter.id() ) );
setAction->setData( highlighter.id() );
}
}

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

const auto selectNone = !highlightersCollection.hasSet( currentSetId );
const auto selectNone = activeSets.isEmpty();

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

inline void saveCurrentHighlighterFromAction( const QAction* action )
{
auto setId = action->data().toString();
auto& highlighterSets = HighlighterSetCollection::get();
highlighterSets.setCurrentSet( setId );

if ( setId.isEmpty() ) {
highlighterSets.deactivateAll();
}
else if ( action->isChecked() ) {
highlighterSets.activateSet( setId );
}
else {
highlighterSets.deactivateSet( setId );
}

highlighterSets.save();
}

Expand Down
3 changes: 2 additions & 1 deletion src/ui/src/abstractlogview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ void AbstractLogView::mousePressEvent( QMouseEvent* mouseEvent )
}

auto highlightersActionGroup = new QActionGroup( this );
highlightersActionGroup->setExclusive( false );
connect( highlightersActionGroup, &QActionGroup::triggered, this,
&AbstractLogView::setHighlighterSet );
highlightersMenu_->clear();
Expand Down Expand Up @@ -1868,7 +1869,7 @@ void AbstractLogView::drawTextArea( QPaintDevice* paintDevice )
const int paintDeviceHeight = paintDevice->height() / viewport()->devicePixelRatio();
const int paintDeviceWidth = paintDevice->width() / viewport()->devicePixelRatio();
const QPalette& palette = viewport()->palette();
const auto& highlighterSet = HighlighterSetCollection::get().currentSet();
const auto& highlighterSet = HighlighterSetCollection::get().currentActiveSet();
const auto& quickHighlighters = HighlighterSetCollection::get().quickHighlighters();
QColor foreColor, backColor;

Expand Down
19 changes: 9 additions & 10 deletions src/ui/src/highlightersdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ HighlightersDialog::HighlightersDialog( QWidget* parent )
HighlighterEdit::updateIcon( backButton, quickHighlighters[ i ].color.backColor );
cycleCheckbox->setChecked( quickHighlighters[ i ].useInCycle );

connect( nameEdit, &QLineEdit::textChanged, nameEdit, [ this, index = i ](const QString& newName) {
auto highlighters = highlighterSetCollection_.quickHighlighters();
if ( !newName.isEmpty() ) {
highlighters[ index ].name = newName;
highlighterSetCollection_.setQuickHighlighters( highlighters );
}
} );
connect( nameEdit, &QLineEdit::textChanged, nameEdit,
[ this, index = i ]( const QString& newName ) {
auto highlighters = highlighterSetCollection_.quickHighlighters();
if ( !newName.isEmpty() ) {
highlighters[ index ].name = newName;
highlighterSetCollection_.setQuickHighlighters( highlighters );
}
} );

connect( foreButton, &QPushButton::clicked, foreButton, [ foreButton, this, index = i ]() {
auto highlighters = highlighterSetCollection_.quickHighlighters();
Expand Down Expand Up @@ -243,9 +244,7 @@ void HighlightersDialog::removeHighlighterSet()
dispatchToMainThread( [ this, index ] {
{
const auto& set = highlighterSetCollection_.highlighters_.at( index );
if ( set.id() == highlighterSetCollection_.currentSetId() ) {
highlighterSetCollection_.setCurrentSet( {} );
}
highlighterSetCollection_.deactivateSet( set.id() );
}

highlighterSetCollection_.highlighters_.removeAt( index );
Expand Down
81 changes: 58 additions & 23 deletions src/ui/src/highlighterset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,29 +367,53 @@ QList<HighlighterSet> HighlighterSetCollection::highlighterSets() const
void HighlighterSetCollection::setHighlighterSets( const QList<HighlighterSet>& highlighters )
{
highlighters_ = highlighters;

activeSets_.erase( std::remove_if( activeSets_.begin(), activeSets_.end(),
[ this ]( const auto& setId ) { return !hasSet( setId ); } ),
activeSets_.end() );
}

HighlighterSet HighlighterSetCollection::currentSet() const
HighlighterSet HighlighterSetCollection::currentActiveSet() const
{
auto set = std::find_if( highlighters_.begin(), highlighters_.end(),
[ this ]( const auto& s ) { return s.id() == currentSet_; } );
HighlighterSet combinedSet;

std::vector<HighlighterSet> activeSetsInOrder{ static_cast<size_t>( activeSets_.size() ) };
std::copy_if( highlighters_.begin(), highlighters_.end(), activeSetsInOrder.begin(),
[ this ]( const auto& set ) { return activeSets_.contains( set.id() ); } );

if ( set != highlighters_.end() ) {
return *set;
for ( auto& set : activeSetsInOrder ) {
combinedSet.highlighterList_.append( set.highlighterList_ );
}
else {
return {};

return combinedSet;
}

QStringList HighlighterSetCollection::activeSetIds() const
{
return activeSets_;
}

void HighlighterSetCollection::activateSet( const QString& setId )
{
LOG_INFO << "activating set " << setId;
if ( !hasSet( setId ) || activeSets_.contains( setId ) ) {
LOG_WARNING << "Set not found or already active";
return;
}

activeSets_.append( setId );
}

QString HighlighterSetCollection::currentSetId() const
void HighlighterSetCollection::deactivateSet( const QString& setId )
{
return currentSet_;
LOG_INFO << "deactivating set " << setId;
activeSets_.removeAll( setId );
}

void HighlighterSetCollection::setCurrentSet( const QString& current )
void HighlighterSetCollection::deactivateAll()
{
currentSet_ = current;
LOG_INFO << "deactivating all sets";
activeSets_.clear();
}

bool HighlighterSetCollection::hasSet( const QString& setId ) const
Expand All @@ -411,18 +435,22 @@ void HighlighterSetCollection::setQuickHighlighters(

void HighlighterSetCollection::saveToStorage( QSettings& settings ) const
{
LOG_INFO << "HighlighterSetCollection::saveToStorage";
LOG_INFO << "HighlighterSetCollection::saveToStorage, v" << HighlighterSetCollection_VERSION;

settings.beginGroup( "HighlighterSetCollection" );
settings.setValue( "version", HighlighterSetCollection_VERSION );
settings.setValue( "current", currentSet_ );
settings.setValue( "active_sets", activeSets_ );

LOG_INFO << activeSets_;

settings.remove( "sets" );
settings.beginWriteArray( "sets" );
for ( int i = 0; i < highlighters_.size(); ++i ) {
settings.setArrayIndex( i );
highlighters_[ i ].saveToStorage( settings );
}
settings.endArray();
settings.remove( "quick" );
settings.beginWriteArray( "quick" );
for ( int i = 0; i < quickHighlighters_.size(); ++i ) {
settings.setArrayIndex( i );
Expand Down Expand Up @@ -455,8 +483,15 @@ void HighlighterSetCollection::retrieveFromStorage( QSettings& settings )
highlighters_.append( std::move( highlighterSet ) );
}
settings.endArray();

activeSets_ = settings.value( "active_sets" ).toStringList();

auto currentSet = settings.value( "current" ).toString();
setCurrentSet( currentSet );
settings.remove( "current" );
if ( !currentSet.isEmpty() ) {
activateSet( currentSet );
settings.setValue("active_sets", activeSets_);
}

size = settings.beginReadArray( "quick" );
for ( int i = 0; i < size; ++i ) {
Expand All @@ -477,14 +512,14 @@ void HighlighterSetCollection::retrieveFromStorage( QSettings& settings )

QList<QuickHighlighter> defaultLabels;
defaultLabels.append( { "Color label 1", { QColor{}, Qt::red }, true } );
defaultLabels.append( { "Color label 2",{ QColor{}, Qt::green }, true } );
defaultLabels.append( { "Color label 3",{ QColor{}, Qt::cyan }, true } );
defaultLabels.append( { "Color label 4",{ QColor{}, Qt::darkRed }, true } );
defaultLabels.append( { "Color label 5",{ QColor{}, Qt::darkGreen }, true } );
defaultLabels.append( { "Color label 6",{ QColor{}, Qt::darkCyan }, true } );
defaultLabels.append( { "Color label 7",{ QColor{}, Qt::magenta }, true } );
defaultLabels.append( { "Color label 8",{ QColor{}, Qt::darkMagenta }, true } );
defaultLabels.append( { "Color label 9",{ QColor{}, Qt::gray }, true } );
defaultLabels.append( { "Color label 2", { QColor{}, Qt::green }, true } );
defaultLabels.append( { "Color label 3", { QColor{}, Qt::cyan }, true } );
defaultLabels.append( { "Color label 4", { QColor{}, Qt::darkRed }, true } );
defaultLabels.append( { "Color label 5", { QColor{}, Qt::darkGreen }, true } );
defaultLabels.append( { "Color label 6", { QColor{}, Qt::darkCyan }, true } );
defaultLabels.append( { "Color label 7", { QColor{}, Qt::magenta }, true } );
defaultLabels.append( { "Color label 8", { QColor{}, Qt::darkMagenta }, true } );
defaultLabels.append( { "Color label 9", { QColor{}, Qt::gray }, true } );

if ( quickHighlighters_.size() < defaultLabels.size() ) {
LOG_WARNING << "Got " << quickHighlighters_.size() << " quick highlighters";
Expand All @@ -508,7 +543,7 @@ void HighlighterSetCollection::retrieveFromStorage( QSettings& settings )
oldHighlighterSet.retrieveFromStorage( settings );
if ( !oldHighlighterSet.isEmpty() ) {
LOG_INFO << "Importing old HighlighterSet";
setCurrentSet( oldHighlighterSet.id() );
activateSet( oldHighlighterSet.id() );
highlighters_.append( std::move( oldHighlighterSet ) );
settings.remove( "HighlighterSet" );
saveToStorage( settings );
Expand Down
5 changes: 3 additions & 2 deletions src/ui/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
#include "mainwindow.h"

#include "crawlerwidget.h"
#include "readablesize.h"
#include "decompressor.h"
#include "dispatch_to.h"
#include "downloader.h"
Expand All @@ -97,6 +96,7 @@
#include "predefinedfilters.h"
#include "predefinedfiltersdialog.h"
#include "progress.h"
#include "readablesize.h"
#include "recentfiles.h"
#include "sessioninfo.h"
#include "shortcuts.h"
Expand Down Expand Up @@ -580,7 +580,7 @@ void MainWindow::createMenus()

toolsMenu = menuBar()->addMenu( tr( "&Tools" ) );

highlightersMenu = toolsMenu->addMenu( "Highlighters" );
highlightersMenu = menuBar()->addMenu( "Highlighters" );
connect( highlightersMenu, &QMenu::aboutToShow,
[ this ]() { setCurrentHighlighterAction( highlightersActionGroup ); } );

Expand Down Expand Up @@ -1696,6 +1696,7 @@ void MainWindow::updateHighlightersMenu()
}

highlightersActionGroup = new QActionGroup( this );
highlightersActionGroup->setExclusive( false );
connect( highlightersActionGroup, &QActionGroup::triggered, this,
&MainWindow::setCurrentHighlighter );

Expand Down

0 comments on commit cfe29e6

Please sign in to comment.