Skip to content

Commit

Permalink
Track context menu positon (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
variar committed Apr 16, 2021
1 parent 28161da commit 10ce9b1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/logdata/src/logdataoperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void OperationQueue::tryStartOperation()
beforeOperationStart_();
operation.start( worker_.get() );
},
[this]( absl::monostate ) { LOG_INFO << "no operation to start"; } ),
[]( absl::monostate ) { LOG_INFO << "no operation to start"; } ),
executingOperation_ );
}

Expand Down
1 change: 1 addition & 0 deletions src/ui/include/abstractlogview.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ class AbstractLogView : public QAbstractScrollArea, public SearchableWidgetInter
LineNumber searchStart_;
LineNumber searchEnd_;

OptionalLineNumber contextMenuLine_;
OptionalLineNumber selectionStart_;

// Text handling
Expand Down
41 changes: 28 additions & 13 deletions src/ui/src/abstractlogview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,27 +316,35 @@ void AbstractLogView::mousePressEvent( QMouseEvent* mouseEvent )
textAreaCache_.invalid_ = true;
}
else if ( mouseEvent->button() == Qt::RightButton ) {
// Prepare the popup depending on selection type
if ( selection_.isSingleLine() ) {
copyAction_->setText( "&Copy this line" );
contextMenuLine_ = convertCoordToLine( mouseEvent->y() );
if ( contextMenuLine_.has_value() && contextMenuLine_ >= logData->getNbLine() ) {
contextMenuLine_ = {};
}

if ( contextMenuLine_.has_value() ) {
setSearchStartAction_->setEnabled( true );
setSearchEndAction_->setEnabled( true );

setSelectionStartAction_->setEnabled( true );
setSelectionEndAction_->setEnabled( !!selectionStart_ );
}
else {
copyAction_->setText( "&Copy" );
copyAction_->setStatusTip( tr( "Copy the selection" ) );

setSearchStartAction_->setEnabled( false );
setSearchEndAction_->setEnabled( false );

setSelectionStartAction_->setEnabled( false );
setSelectionEndAction_->setEnabled( false );
}

// Prepare the popup depending on selection type
if ( selection_.isSingleLine() || contextMenuLine_.has_value() ) {
copyAction_->setText( "&Copy this line" );
}
else {
copyAction_->setText( "&Copy" );
copyAction_->setStatusTip( tr( "Copy the selection" ) );
}

if ( selection_.isPortion() ) {
findNextAction_->setEnabled( true );
findPreviousAction_->setEnabled( true );
Expand Down Expand Up @@ -1166,32 +1174,39 @@ void AbstractLogView::updateSearchLimits()

void AbstractLogView::setSearchStart()
{
const auto selectedLine = selection_.selectedLine();
searchStart_
= selectedLine.has_value() ? displayLineNumber( *selectedLine ) - 1_lcount : 0_lnum;
= contextMenuLine_.has_value() ? displayLineNumber( *contextMenuLine_ ) - 1_lcount : 0_lnum;
updateSearchLimits();
}

void AbstractLogView::setSearchEnd()
{
const auto selectedLine = selection_.selectedLine();
searchEnd_ = selectedLine.has_value() ? displayLineNumber( *selectedLine )
: LineNumber( logData->getNbLine().get() );
searchEnd_ = contextMenuLine_.has_value() ? displayLineNumber( *contextMenuLine_ )
: LineNumber( logData->getNbLine().get() );
updateSearchLimits();
}

void AbstractLogView::setSelectionStart()
{
selectionStart_ = selection_.selectedLine();
selectionStart_ = contextMenuLine_;
if ( selectionStart_.has_value() ) {
selection_.selectLine( *selectionStart_ );
emit updateLineNumber( *selectionStart_ );
emit newSelection( *selectionStart_ );

textAreaCache_.invalid_ = true;
update();
}
}

void AbstractLogView::setSelectionEnd()
{
auto selectionEnd = selection_.selectedLine();
const auto selectionEnd = contextMenuLine_;

if ( selectionStart_ && selectionEnd ) {
selection_.selectRange( *selectionStart_, *selectionEnd );
selectionStart_ = {};

textAreaCache_.invalid_ = true;
update();
}
Expand Down
18 changes: 10 additions & 8 deletions src/utils/include/dispatch_to.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
* along with klogg. If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef KLOGG_DISPATCH_TO

#include <QApplication>
#include <QAbstractEventDispatcher>
#include <QApplication>
#include <QMetaObject>
#include <QThread>

#include "log.h"

// by
// https://github.com/KubaO/stackoverflown/blob/master/questions/metacall-21646467/main.cpp
#if QT_VERSION >= QT_VERSION_CHECK( 5, 10, 0 )
Expand All @@ -49,29 +50,30 @@ struct FEvent : public QEvent {

FEvent( Fun&& fun )
: QEvent( QEvent::None )
, fun( std::move( fun ) )
, fun_( std::move( fun ) )
{
}
FEvent( const Fun& fun )
: QEvent( QEvent::None )
, fun( fun )
, fun_( fun )
{
}
~FEvent()
{
fun();
fun_();
}

private:
Fun fun;
Fun fun_;
};
} // namespace detail

template <typename F>
static void dispatchToObject( F&& fun, QObject* obj = qApp )
{
if ( qobject_cast<QThread*>( obj ) )
qWarning() << "posting a call to a thread object - consider using postToThread";
if ( qobject_cast<QThread*>( obj ) ) {
LOG_WARNING << "posting a call to a thread object - consider using postToThread";
}
QCoreApplication::postEvent( obj, new detail::FEvent<F>( std::forward<F>( fun ) ) );
}

Expand Down

0 comments on commit 10ce9b1

Please sign in to comment.