Skip to content

Commit

Permalink
Merge pull request #8 from edbee/master
Browse files Browse the repository at this point in the history
Update Mudlet edbee with latest double/triple-click support
  • Loading branch information
vadi2 authored Nov 26, 2020
2 parents 0f8823a + f4c1cca commit 25a23f1
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

edbee.lib:

- ref #115, basic Tripple-click support (TODO: still need to fix, anchor movement in controllers)
- fix #114, Double-click + drag should end at word boundaries
- #112, Workaround for missing Qt::endl in Qt 5.12
- Support for sticky-selection in replaceSelection methods. (Required for InpuMethod entry)
- Improved TextEditorComponent::InputMethodEvent... It now support special chars entry like expected. (Option+e, e => ´ => é)
Expand Down
1 change: 1 addition & 0 deletions edbee-lib/edbee/commands/selectioncommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class EDBEE_EXPORT SelectionCommand : public TextEditorCommand
MoveCaretByWord, ///< moves the caret(s) by the given amount of words
MoveCaretByLine, ///< moves the caret(s) by the given amount of lines
MoveCaretByPage, ///< moves the caret(s) by the given amount of pages
MoveCaretToWordBoundary, ///< moves the caret to a line-boundary (<0 begin of line, >0 end of line)
MoveCaretToLineBoundary, ///< moves the caret to a line-boundary (<0 begin of line, >0 end of line)
MoveCaretToDocumentBegin, ///< moves the caret to the document start
MoveCaretToDocumentEnd, ///< moves the caret to the document end
Expand Down
41 changes: 40 additions & 1 deletion edbee-lib/edbee/models/textrange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "textdocument.h"
#include "textrange.h"
#include "texteditorconfig.h"
#include "edbee/debug.h"

namespace edbee {
Expand Down Expand Up @@ -244,6 +245,45 @@ void TextRange::moveCaretToLineBoundary(TextDocument* doc, int amount, const QSt
setCaretBounded( doc, caret );
}

/// Moves the caret to a word boundary (used for word dragging selections)
void TextRange::moveCaretToWordBoundaryAtOffset(TextDocument *doc, int newOffset)
{
TextEditorConfig* config = doc->config();

// changed offset
setCaret(newOffset);
// left direction
if( newOffset < anchor()) {
moveCaretByCharGroup(doc, -1, config->whitespaces(), config->charGroups());
// right direction
} else {
moveCaretByCharGroup(doc, 1, config->whitespaces(), config->charGroups());
}
}

/// Moves the caret to a word boundary (used for word dragging selections)
void TextRange::moveCaretToLineBoundaryAtOffset(TextDocument *doc, int newOffset)
{
TextEditorConfig* config = doc->config();

// changed offset
setCaret(newOffset);

int firstLine = doc->lineFromOffset(min());
int lastLine = doc->lineFromOffset(max());

int newLine = doc->lineFromOffset(newOffset);

// left direction
if( newLine < lastLine ) {
this->caret_ = doc->offsetFromLine(newLine);
this->anchor_ = doc->offsetFromLine(lastLine+1);
// right direction
} else if( newLine > firstLine) {
this->anchor_ = doc->offsetFromLine(firstLine-1);
this->caret_ = doc->offsetFromLine(newLine+1);
}
}

/// Expands the selection range so it only consists of full lines
/// amount specifies the amount (and the direction) of the expansions
Expand Down Expand Up @@ -773,7 +813,6 @@ void TextRangeSetBase::toggleWordSelectionAt(int offset, const QString& whitespa
selectWordAt( offset, whitespace, characterGroups );
}


/// This method moves the carets by character
void TextRangeSetBase::moveCarets( int amount )
{
Expand Down
3 changes: 3 additions & 0 deletions edbee-lib/edbee/models/textrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class EDBEE_EXPORT TextRange {
void moveAnchorUntilChar( TextDocument* doc, int amount, const QString& chars );
void moveCaretByCharGroup( TextDocument* doc, int amount, const QString& whitespace, const QStringList& characterGroups );
void moveCaretToLineBoundary( TextDocument* doc, int amount, const QString& whitespace );
void moveCaretToWordBoundaryAtOffset( TextDocument* doc, int offset );
void moveCaretToLineBoundaryAtOffset( TextDocument* doc, int offset );


void expandToFullLine( TextDocument* doc, int amount );
void deselectTrailingNewLine( TextDocument* doc );
Expand Down
57 changes: 55 additions & 2 deletions edbee-lib/edbee/views/components/texteditorcomponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <QApplication>
#include <QClipboard>
#include <QDateTime>
#include <QMenu>
#include <QPainter>
#include <QPaintEvent>
Expand All @@ -31,6 +32,9 @@

namespace edbee {

const qint64 TRIPLE_CLICK_DELAY_IN_MS = 250; // 0.25 seconds


/// The default texteditor compenent constructor
/// @param controller the active controller for this editor
/// @param parent the parent QObject
Expand All @@ -39,6 +43,8 @@ TextEditorComponent::TextEditorComponent(TextEditorController* controller, QWidg
, caretTimer_(nullptr)
, controllerRef_(controller)
, textEditorRenderer_(nullptr)
, clickCount_(0)
, lastClickEvent_(0)
{
textEditorRenderer_ = new TextEditorRenderer( controller->textRenderer());

Expand Down Expand Up @@ -378,6 +384,19 @@ QVariant TextEditorComponent::inputMethodQuery( Qt::InputMethodQuery p ) const
return QVariant();
}

/// registers a click event and modifies the clickcount and last-click moment
void TextEditorComponent::registerClickEvent()
{
qint64 currentClickEvent = QDateTime::currentMSecsSinceEpoch();
if( currentClickEvent - lastClickEvent_ <=TRIPLE_CLICK_DELAY_IN_MS ) {
clickCount_ += 1;
} else {
clickCount_ = 1;
}
// qlog_info() << "clickcount: " << clickCount_;
lastClickEvent_ = currentClickEvent;
}


/// A mouse press happens
///
Expand All @@ -401,6 +420,17 @@ void TextEditorComponent::mousePressEvent(QMouseEvent* event)
int col = renderer->columnIndexForXpos( line, x );

if( event->button() == Qt::LeftButton ) {
qint64 currentClickEvent = QDateTime::currentMSecsSinceEpoch();
registerClickEvent();
if( clickCount_ > 1 ) {
if( clickCount_ == 3) {
TextRange range = textSelection()->range(0);
SelectionCommand toggleWordSelectionAtCommand( SelectionCommand::SelectFullLine, 0);
controller()->executeCommand( &toggleWordSelectionAtCommand );
}
return;
}

if( event->modifiers()&Qt::ControlModifier ) {
controller()->addCaretAt( line, col );
} else {
Expand Down Expand Up @@ -443,9 +473,13 @@ void TextEditorComponent::mouseDoubleClickEvent( QMouseEvent* event )
{
static SelectionCommand selectWord( SelectionCommand::SelectWord );
if( event->button() == Qt::LeftButton ) {
registerClickEvent();
if( clickCount_ > 2 ) {
return;
}

if( event->modifiers()&Qt::ControlModifier ) {

if( event->modifiers()&Qt::ControlModifier ) {
// get the location of the double
int x = event->x();
int y = event->y();
Expand All @@ -454,7 +488,7 @@ void TextEditorComponent::mouseDoubleClickEvent( QMouseEvent* event )

// add the word there
SelectionCommand toggleWordSelectionAtCommand( SelectionCommand::ToggleWordSelectionAt, textDocument()->offsetFromLineAndColumn(line,col) );
controller()->executeCommand( &toggleWordSelectionAtCommand );
controller()->executeCommand( &toggleWordSelectionAtCommand );
} else {
controller()->executeCommand( &selectWord );
}
Expand All @@ -480,6 +514,25 @@ void TextEditorComponent::mouseMoveEvent(QMouseEvent* event )
if( line >= 0 ) { col = renderer->columnIndexForXpos( line, x ); }
if( line < 0 ) { line = 0; }

if( clickCount_ == 2 ) {
TextRange range = textSelection()->range(0);

int newOffset = textDocument()->offsetFromLineAndColumn(line, col);
range.moveCaretToWordBoundaryAtOffset(textDocument(), newOffset);

controller()->moveCaretToOffset(range.caret(), true);
return;
} else if( clickCount_ == 3 ) {
TextRange range = textSelection()->range(0); // copy range

int newOffset = textDocument()->offsetFromLineAndColumn(line, col);
range.moveCaretToLineBoundaryAtOffset(textDocument(), newOffset);

controller()->moveCaretToOffset(range.caret(), true);
return;
}


if( event->modifiers() & Qt::ControlModifier) {
controller()->moveCaretTo( line, col, true, controller()->textSelection()->rangeCount() - 1 );
} else {
Expand Down
3 changes: 3 additions & 0 deletions edbee-lib/edbee/views/components/texteditorcomponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class EDBEE_EXPORT TextEditorComponent : public QWidget
virtual void keyReleaseEvent ( QKeyEvent* event );
void inputMethodEvent( QInputMethodEvent* m );
QVariant inputMethodQuery( Qt::InputMethodQuery p ) const;
void registerClickEvent();
virtual void mousePressEvent( QMouseEvent* event );
virtual void mouseReleaseEvent( QMouseEvent* event );
virtual void mouseDoubleClickEvent( QMouseEvent* event );
Expand Down Expand Up @@ -86,6 +87,8 @@ public slots:
TextEditorController* controllerRef_; ///< A reference to the controller
TextEditorRenderer* textEditorRenderer_; /// A text-editor renderer

int clickCount_; ///< The number of clicks
qint64 lastClickEvent_; ///< Last click event time
};

} // edbee
2 changes: 1 addition & 1 deletion edbee-test/edbee/models/textrangetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ do { \
/// @param definition the definition. In the format anchor>caret,anchor>caret
static void addRanges( TextRangeSet* sel, const QString& definition )
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QStringList ranges = definition.split(",", Qt::SkipEmptyParts);
#else
QStringList ranges = definition.split(",", QString::SkipEmptyParts);
Expand Down

0 comments on commit 25a23f1

Please sign in to comment.