Skip to content

Commit

Permalink
feat: quick send selection to scratchpad (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
variar committed Dec 18, 2021
1 parent 80771ee commit fb478e6
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 42 deletions.
2 changes: 2 additions & 0 deletions src/ui/include/abstractlogview.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
void clearSearchLimits();

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

void addColorLabel( size_t label );
Expand Down Expand Up @@ -443,6 +444,7 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
QMenu* popupMenu_;
QAction* copyAction_;
QAction* markAction_;
QAction* sendToScratchpadAction_;
QAction* saveToFileAction_;
QAction* findNextAction_;
QAction* findPreviousAction_;
Expand Down
2 changes: 2 additions & 0 deletions src/ui/include/crawlerwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ class CrawlerWidget : public QSplitter,
// Sent up when user wants to save new predefined filter from current search
void saveCurrentSearchAsPredefinedFilter( QString newFilter );

void sendToScratchpad(QString data);

// "auto-refresh" check has been changed
void searchRefreshChanged( bool isRefreshing );
// "ignore case" check has been changed
Expand Down
10 changes: 3 additions & 7 deletions src/ui/include/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,16 @@ class MainWindow : public QMainWindow {
bool event( QEvent* event ) override;

private:
enum class ActionInitiator
{
User,
App
};
enum class ActionInitiator { User, App };

private slots:
void open();
void openFileFromRecent( QAction* action );
void openFileFromFavorites( QAction* action );
void switchToOpenedFile( QAction* action );
void setCurrentHighlighter( QAction* action );
void closeTab(ActionInitiator initiator);
void closeAll(ActionInitiator initiator);
void closeTab( ActionInitiator initiator );
void closeAll( ActionInitiator initiator );
void selectAll();
void copy();
void find();
Expand Down
8 changes: 6 additions & 2 deletions src/ui/include/scratchpad.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QWidget>

#include <functional>
#include <qobjectdefs.h>

class QPlainTextEdit;
class QStatusBar;
Expand All @@ -37,6 +38,9 @@ class ScratchPad : public QWidget {
ScratchPad( const ScratchPad& ) = delete;
ScratchPad& operator=( const ScratchPad& ) = delete;

public slots:
void addData( QString data );

signals:
void updateTransformation();

Expand All @@ -60,9 +64,9 @@ class ScratchPad : public QWidget {

void decodeUrl();

QString transformText(const std::function<QString(QString)>& transform);
QString transformText( const std::function<QString( QString )>& transform );

void transformTextInPlace(const std::function<QString(QString)>& transform);
void transformTextInPlace( const std::function<QString( QString )>& transform );

private:
QPlainTextEdit* textEdit_;
Expand Down
11 changes: 7 additions & 4 deletions src/ui/include/tabbedscratchpad.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,30 @@
#ifndef KLOGG_TABBEDSCRATCHPAD_H
#define KLOGG_TABBEDSCRATCHPAD_H

#include <QWidget>
#include <QTabWidget>
#include <QKeyEvent>
#include <QTabWidget>
#include <QWidget>

class TabbedScratchPad : public QWidget {
Q_OBJECT
Q_OBJECT
public:
explicit TabbedScratchPad( QWidget* parent = nullptr );

~TabbedScratchPad() = default;
TabbedScratchPad( const TabbedScratchPad& ) = delete;
TabbedScratchPad& operator=( const TabbedScratchPad& ) = delete;

public slots:
void addData( QString data );

protected:
void keyPressEvent( QKeyEvent* event ) override;

private:
void addTab();

private:
QTabWidget* tabWidget_ { nullptr};
QTabWidget* tabWidget_{ nullptr };
int tabCounter_{};
};

Expand Down
38 changes: 24 additions & 14 deletions src/ui/src/abstractlogview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1731,21 +1731,23 @@ void AbstractLogView::createMenu()
{
copyAction_ = new QAction( tr( "&Copy" ), this );
// No text as this action title depends on the type of selection
connect( copyAction_, &QAction::triggered, [ this ]( auto ) { this->copy(); } );
connect( copyAction_, &QAction::triggered, this, [ this ]( auto ) { this->copy(); } );

markAction_ = new QAction( tr( "&Mark" ), this );
connect( markAction_, &QAction::triggered, [ this ]( auto ) { this->markSelected(); } );
connect( markAction_, &QAction::triggered, this, [ this ]( auto ) { this->markSelected(); } );

saveToFileAction_ = new QAction( tr( "Save to file" ), this );
connect( saveToFileAction_, &QAction::triggered, [ this ]( auto ) { this->saveToFile(); } );
connect( saveToFileAction_, &QAction::triggered, this,
[ this ]( auto ) { this->saveToFile(); } );

// For '#' and '*', shortcuts doesn't seem to work but
// at least it displays them in the menu, we manually handle those keys
// as keys event anyway (in keyPressEvent).
findNextAction_ = new QAction( tr( "Find &next" ), this );
findNextAction_->setShortcut( Qt::Key_Asterisk );
findNextAction_->setStatusTip( tr( "Find the next occurrence" ) );
connect( findNextAction_, &QAction::triggered, [ this ]( auto ) { this->findNextSelected(); } );
connect( findNextAction_, &QAction::triggered, this,
[ this ]( auto ) { this->findNextSelected(); } );

findPreviousAction_ = new QAction( tr( "Find &previous" ), this );
findPreviousAction_->setShortcut( tr( "/" ) );
Expand All @@ -1755,49 +1757,56 @@ void AbstractLogView::createMenu()

replaceSearchAction_ = new QAction( tr( "&Replace search" ), this );
replaceSearchAction_->setStatusTip( tr( "Replace the search expression with the selection" ) );
connect( replaceSearchAction_, &QAction::triggered,
connect( replaceSearchAction_, &QAction::triggered, this,
[ this ]( auto ) { this->replaceSearch(); } );

addToSearchAction_ = new QAction( tr( "&Add to search" ), this );
addToSearchAction_->setStatusTip( tr( "Add the selection to the current search" ) );
connect( addToSearchAction_, &QAction::triggered, [ this ]( auto ) { this->addToSearch(); } );
connect( addToSearchAction_, &QAction::triggered, this,
[ this ]( auto ) { this->addToSearch(); } );

excludeFromSearchAction_ = new QAction( tr( "&Exclude from search" ), this );
excludeFromSearchAction_->setStatusTip( tr( "Excludes the selection from search" ) );
connect( excludeFromSearchAction_, &QAction::triggered,
connect( excludeFromSearchAction_, &QAction::triggered, this,
[ this ]( auto ) { this->excludeFromSearch(); } );

setSearchStartAction_ = new QAction( tr( "Set search start" ), this );
connect( setSearchStartAction_, &QAction::triggered,
connect( setSearchStartAction_, &QAction::triggered, this,
[ this ]( auto ) { this->setSearchStart(); } );

setSearchEndAction_ = new QAction( tr( "Set search end" ), this );
connect( setSearchEndAction_, &QAction::triggered, [ this ]( auto ) { this->setSearchEnd(); } );
connect( setSearchEndAction_, &QAction::triggered, this,
[ this ]( auto ) { this->setSearchEnd(); } );

clearSearchLimitAction_ = new QAction( tr( "Clear search limits" ), this );
connect( clearSearchLimitAction_, &QAction::triggered,
connect( clearSearchLimitAction_, &QAction::triggered, this,
[ this ]( auto ) { this->clearSearchLimits(); } );

setSelectionStartAction_ = new QAction( tr( "Set selection start" ), this );
connect( setSelectionStartAction_, &QAction::triggered,
connect( setSelectionStartAction_, &QAction::triggered, this,
[ this ]( auto ) { this->setSelectionStart(); } );

setSelectionEndAction_ = new QAction( tr( "Set selection end" ), this );
connect( setSelectionEndAction_, &QAction::triggered,
connect( setSelectionEndAction_, &QAction::triggered, this,
[ this ]( auto ) { this->setSelectionEnd(); } );

saveDefaultSplitterSizesAction_ = new QAction( tr( "Save splitter position" ), this );
connect( saveDefaultSplitterSizesAction_, &QAction::triggered,
connect( saveDefaultSplitterSizesAction_, &QAction::triggered, this,
[ this ]( auto ) { emit saveDefaultSplitterSizes(); } );

sendToScratchpadAction_ = new QAction( tr( "Send to scratchpad" ), this );
connect( sendToScratchpadAction_, &QAction::triggered, this,
[ this ]( auto ) { emit sendSelectionToScratchpad(); } );

popupMenu_ = new QMenu( this );
highlightersMenu_ = popupMenu_->addMenu( "Highlighters" );
colorLabelsMenu_ = popupMenu_->addMenu( "Color labels" );

popupMenu_->addSeparator();
popupMenu_->addAction( markAction_ );
popupMenu_->addSeparator();
popupMenu_->addAction( copyAction_ );
popupMenu_->addAction( saveToFileAction_ );
popupMenu_->addAction( sendToScratchpadAction_ );
popupMenu_->addSeparator();
popupMenu_->addAction( findNextAction_ );
popupMenu_->addAction( findPreviousAction_ );
Expand All @@ -1814,6 +1823,7 @@ void AbstractLogView::createMenu()
popupMenu_->addAction( setSelectionEndAction_ );
popupMenu_->addSeparator();
popupMenu_->addAction( saveDefaultSplitterSizesAction_ );
popupMenu_->addAction( saveToFileAction_ );
}

void AbstractLogView::considerMouseHovering( int xPos, int yPos )
Expand Down
8 changes: 7 additions & 1 deletion src/ui/src/crawlerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
// the UI elements. It implements the connection between the UI elements.
// It also interacts with the sets of data (full and filtered).

#include "abstractlogview.h"
#include "log.h"

#include <algorithm>
Expand Down Expand Up @@ -1262,7 +1263,12 @@ void CrawlerWidget::setup()
&CrawlerWidget::addColorLabelToSelection );
connect( filteredView_, &AbstractLogView::addColorLabel, this,
&CrawlerWidget::addColorLabelToSelection );


connect( logMainView_, &AbstractLogView::sendSelectionToScratchpad, this,
[ this ]() { emit sendToScratchpad( logMainView_->getSelection() ); } );
connect( filteredView_, &AbstractLogView::sendSelectionToScratchpad, this,
[ this ]() { emit sendToScratchpad( filteredView_->getSelection() ); } );

const auto defaultEncodingMib = config.defaultEncodingMib();
if ( defaultEncodingMib >= 0 ) {
encodingMib_ = defaultEncodingMib;
Expand Down
6 changes: 5 additions & 1 deletion src/ui/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
// managing the menus, the toolbar, and the CrawlerWidget. It also
// load/save the settings on opening/closing of the app

#include "configuration.h"
#include <QNetworkReply>
#include <cassert>
#include <exception>
Expand Down Expand Up @@ -161,6 +162,9 @@ MainWindow::MainWindow( WindowSession session )
signalMux_.connect( SIGNAL( saveCurrentSearchAsPredefinedFilter( QString ) ), this,
SLOT( newPredefinedFilterHandler( QString ) ) );

signalMux_.connect( SIGNAL( sendToScratchpad( QString ) ), &scratchPad_,
SLOT( addData( QString ) ) );

// Register for progress status bar
signalMux_.connect( SIGNAL( loadingProgressed( int ) ), this,
SLOT( updateLoadingProgress( int ) ) );
Expand Down Expand Up @@ -393,7 +397,7 @@ void MainWindow::createActions()
[ this ]( auto ) { this->editHighlighters(); } );

optionsAction = new QAction( tr( "&Options..." ), this );
optionsAction->setMenuRole(QAction::PreferencesRole);
optionsAction->setMenuRole( QAction::PreferencesRole );
optionsAction->setStatusTip( tr( "Show the Options box" ) );
connect( optionsAction, &QAction::triggered, this, [ this ]( auto ) { this->options(); } );

Expand Down
30 changes: 20 additions & 10 deletions src/ui/src/scratchpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@
#include <QToolBar>
#include <QUrl>
#include <QVBoxLayout>
#include <qchar.h>

#include "crc32.h"

namespace {

template <typename T> QString formatHex( T value )
template <typename T>
QString formatHex( T value )
{
return QString::fromLatin1( QByteArray::number( value, 16 ).rightJustified( 8, '0', false ) );
}

template <typename T> QString formatDec( T value )
template <typename T>
QString formatDec( T value )
{
return QString::fromLatin1( QByteArray::number( value, 10 ) );
}
Expand Down Expand Up @@ -75,33 +78,33 @@ ScratchPad::ScratchPad( QWidget* parent )
auto toolBar = std::make_unique<QToolBar>();

auto decodeBase64Action = std::make_unique<QAction>( "From base64" );
connect( decodeBase64Action.get(), &QAction::triggered, [this]( auto ) { decodeBase64(); } );
connect( decodeBase64Action.get(), &QAction::triggered, [ this ]( auto ) { decodeBase64(); } );
toolBar->addAction( decodeBase64Action.release() );

auto encodeBase64Action = std::make_unique<QAction>( "To base64" );
connect( encodeBase64Action.get(), &QAction::triggered, [this]( auto ) { encodeBase64(); } );
connect( encodeBase64Action.get(), &QAction::triggered, [ this ]( auto ) { encodeBase64(); } );
toolBar->addAction( encodeBase64Action.release() );

auto decodeHexAction = std::make_unique<QAction>( "From hex" );
connect( decodeHexAction.get(), &QAction::triggered, [this]( auto ) { decodeHex(); } );
connect( decodeHexAction.get(), &QAction::triggered, [ this ]( auto ) { decodeHex(); } );
toolBar->addAction( decodeHexAction.release() );

auto encodeHexAction = std::make_unique<QAction>( "To hex" );
connect( encodeHexAction.get(), &QAction::triggered, [this]( auto ) { encodeHex(); } );
connect( encodeHexAction.get(), &QAction::triggered, [ this ]( auto ) { encodeHex(); } );
toolBar->addAction( encodeHexAction.release() );

auto decodeUrlAction = std::make_unique<QAction>( "Decode url" );
connect( decodeUrlAction.get(), &QAction::triggered, [this]( auto ) { decodeUrl(); } );
connect( decodeUrlAction.get(), &QAction::triggered, [ this ]( auto ) { decodeUrl(); } );
toolBar->addAction( decodeUrlAction.release() );

toolBar->addSeparator();

auto formatJsonAction = std::make_unique<QAction>( "Format json" );
connect( formatJsonAction.get(), &QAction::triggered, [this]( auto ) { formatJson(); } );
connect( formatJsonAction.get(), &QAction::triggered, [ this ]( auto ) { formatJson(); } );
toolBar->addAction( formatJsonAction.release() );

auto formatXmlAction = std::make_unique<QAction>( "Format xml" );
connect( formatXmlAction.get(), &QAction::triggered, [this]( auto ) { formatXml(); } );
connect( formatXmlAction.get(), &QAction::triggered, [ this ]( auto ) { formatXml(); } );
toolBar->addAction( formatXmlAction.release() );

toolBar->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
Expand All @@ -111,7 +114,7 @@ ScratchPad::ScratchPad( QWidget* parent )
auto transLayout = std::make_unique<QFormLayout>();

auto addBoxToLayout
= [&transLayout, this]( const QString& label, QLineEdit** widget, auto changeFunction ) {
= [ &transLayout, this ]( const QString& label, QLineEdit** widget, auto changeFunction ) {
auto box = std::make_unique<QLineEdit>();
box->setReadOnly( true );
*widget = box.get();
Expand Down Expand Up @@ -146,6 +149,13 @@ ScratchPad::ScratchPad( QWidget* parent )
&ScratchPad::updateTransformation );
}

void ScratchPad::addData( QString data )
{
auto cursor = textEdit_->textCursor();
cursor.insertText( data );
cursor.insertText( QString{QChar::LineFeed} );
}

QString ScratchPad::transformText( const std::function<QString( QString )>& transform )
{
auto cursor = textEdit_->textCursor();
Expand Down
14 changes: 11 additions & 3 deletions src/ui/src/tabbedscratchpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ TabbedScratchPad::TabbedScratchPad( QWidget* parent )
tabWidget_->setTabsClosable( true );

connect( tabWidget_, &QTabWidget::tabCloseRequested,
[this]( const auto index ) { tabWidget_->removeTab( index ); } );
[ this ]( const auto index ) { tabWidget_->removeTab( index ); } );

auto addTabButton = std::make_unique<QToolButton>();
addTabButton->setText( "+" );
addTabButton->setAutoRaise( true );

connect( addTabButton.get(), &QToolButton::clicked, [this]( auto ) { addTab(); } );
connect( addTabButton.get(), &QToolButton::clicked, [ this ]( auto ) { addTab(); } );

tabWidget_->addTab( new QLabel( "You can add tabs by pressing <b>\"+\"</b> or Ctrl+N" ),
QString() );
tabWidget_->setTabEnabled( 0, false );

auto deleteTabButton = [this]( QTabBar::ButtonPosition position ) {
auto deleteTabButton = [ this ]( QTabBar::ButtonPosition position ) {
auto button = tabWidget_->tabBar()->tabButton( 0, position );
if ( button ) {
button->deleteLater();
Expand Down Expand Up @@ -111,3 +111,11 @@ void TabbedScratchPad::addTab()
= tabWidget_->addTab( new ScratchPad(), QString( "Scratchpad %1" ).arg( ++tabCounter_ ) );
tabWidget_->setCurrentIndex( newIndex );
}

void TabbedScratchPad::addData( QString data )
{
auto curretScratchPad = qobject_cast<ScratchPad*>( tabWidget_->currentWidget() );
if ( curretScratchPad ) {
curretScratchPad->addData( data );
}
}

0 comments on commit fb478e6

Please sign in to comment.