Skip to content

Commit

Permalink
Add import-export for highlighters (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
variar committed Sep 13, 2020
1 parent 354809c commit e8e1dbd
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/ui/include/highlightersdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class HighlightersDialog : public QDialog, public Ui::HighlightersDialog {
// Update the selected HighlighterSet from the values in the property fields.
void updateHighlighterProperties();

void exportHighlighters();
void importHighlighters();

private:
void populateHighlighterList();
void setCurrentRow( int row );
Expand Down
27 changes: 27 additions & 0 deletions src/ui/include/highlightersdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,33 @@
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="exportButton">
<property name="text">
<string>Export</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="importButton">
<property name="text">
<string>Import</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
Expand Down
3 changes: 2 additions & 1 deletion src/ui/include/highlighterset.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ class HighlighterSetCollection final : public Persistable<HighlighterSetCollecti

HighlighterSet currentSet() const;

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

// Reads/writes the current config in the QSettings object passed
void saveToStorage( QSettings& settings ) const;
Expand Down
11 changes: 6 additions & 5 deletions src/ui/src/abstractlogview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,9 @@ void AbstractLogView::mousePressEvent( QMouseEvent* mouseEvent )
if ( config.mainRegexpType() != ExtendedRegexp )
addToSearchAction_->setEnabled( false );

const auto& highlighterSets = HighlighterSetCollection::get().highlighterSets();
const auto currentSetId = HighlighterSetCollection::get().currentSetId();
const auto& highlightersCollection = HighlighterSetCollection::get();
const auto& highlighterSets = highlightersCollection.highlighterSets();
const auto currentSetId = highlightersCollection.currentSetId();

auto highlightersActionGroup = new QActionGroup( this );
connect( highlightersActionGroup, &QActionGroup::triggered, this,
Expand All @@ -398,10 +399,10 @@ void AbstractLogView::mousePressEvent( QMouseEvent* mouseEvent )
auto noneAction = highlightersMenu_->addAction( "None" );
noneAction->setActionGroup( highlightersActionGroup );
noneAction->setCheckable( true );
noneAction->setChecked( currentSetId.isEmpty() );
noneAction->setChecked( !highlightersCollection.hasSet( currentSetId ) );

highlightersMenu_->addSeparator();
for (const auto& highlighter : qAsConst(highlighterSets)) {
for ( const auto& highlighter : qAsConst( highlighterSets ) ) {
auto setAction = highlightersMenu_->addAction( highlighter.name() );
setAction->setActionGroup( highlightersActionGroup );
setAction->setCheckable( true );
Expand Down Expand Up @@ -1979,7 +1980,7 @@ void AbstractLogView::disableFollow()

void AbstractLogView::setHighlighterSet( QAction* action )
{
auto setId= action->data().toString();
auto setId = action->data().toString();
auto& highlighterSets = HighlighterSetCollection::get();
highlighterSets.setCurrentSet( setId );
highlighterSets.save();
Expand Down
45 changes: 45 additions & 0 deletions src/ui/src/highlightersdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

#include "highlightersdialog.h"

#include <QFileDialog>
#include <QTimer>
#include <utility>

Expand Down Expand Up @@ -79,6 +80,9 @@ HighlightersDialog::HighlightersDialog( QWidget* parent )
connect( downHighlighterButton, &QToolButton::clicked, this,
&HighlightersDialog::moveHighlighterSetDown );

connect( exportButton, &QPushButton::clicked, this, &HighlightersDialog::exportHighlighters );
connect( importButton, &QPushButton::clicked, this, &HighlightersDialog::importHighlighters );

// No highlighter selected by default
selectedRow_ = -1;

Expand All @@ -99,6 +103,40 @@ HighlightersDialog::HighlightersDialog( QWidget* parent )
// Slots
//

void HighlightersDialog::exportHighlighters()
{
QString file = QFileDialog::getSaveFileName( this, "Export highlighters configuration", "",
"Highlighters (*.conf)" );

if ( file.isEmpty() ) {
return;
}

QSettings settings{ file, QSettings::IniFormat };
highlighterSetCollection_.saveToStorage( settings );
}

void HighlightersDialog::importHighlighters()
{
QStringList files = QFileDialog::getOpenFileNames( this, "Select one or more files to open", "",
"Highlighters (*.conf)" );

for ( const auto& file : qAsConst( files ) ) {
LOG( logDEBUG ) << "Loading highlighters from " << file;
QSettings settings{ file, QSettings::IniFormat };
HighlighterSetCollection collection;
collection.retrieveFromStorage( settings );
for ( const auto& set : qAsConst( collection.highlighters_ ) ) {
if ( highlighterSetCollection_.hasSet( set.id() ) ) {
continue;
}

highlighterSetCollection_.highlighters_.append( set );
highlighterListWidget->addItem( set.name() );
}
}
}

void HighlightersDialog::addHighlighterSet()
{
LOG( logDEBUG ) << "addHighlighter()";
Expand All @@ -119,6 +157,13 @@ void HighlightersDialog::removeHighlighterSet()
if ( index >= 0 ) {
setCurrentRow( -1 );
QTimer::singleShot( 0, [this, index] {
{
const auto& set = highlighterSetCollection_.highlighters_.at( index );
if ( set.id() == highlighterSetCollection_.currentSetId() ) {
highlighterSetCollection_.setCurrentSet( {} );
}
}

highlighterSetCollection_.highlighters_.removeAt( index );
delete highlighterListWidget->takeItem( index );

Expand Down
8 changes: 7 additions & 1 deletion src/ui/src/highlighterset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,17 @@ QString HighlighterSetCollection::currentSetId() const
return currentSet_;
}

void HighlighterSetCollection::setCurrentSet( QString current )
void HighlighterSetCollection::setCurrentSet( const QString& current )
{
currentSet_ = current;
}

bool HighlighterSetCollection::hasSet( const QString& setId ) const
{
return std::any_of( highlighters_.begin(), highlighters_.end(),
[setId]( const auto& s ) { return s.id() == setId; } );
}

void HighlighterSetCollection::saveToStorage( QSettings& settings ) const
{
LOG( logDEBUG ) << "HighlighterSetCollection::saveToStorage";
Expand Down

0 comments on commit e8e1dbd

Please sign in to comment.