Skip to content

Commit

Permalink
ref edbee#107, Several improvements (Thanks @sebcaux)
Browse files Browse the repository at this point in the history
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 => ´ => é)
- Fixed gapvector destructor: it did not use an array delete.
- TextEditorWidget::setHorizontalScrollBar not emits the correct horizontalScrollBarChanged event.
  • Loading branch information
gamecreature authored and SlySven committed Oct 26, 2020
1 parent 0d5b039 commit 6fc9e87
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 33 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

edbee.lib:

- 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 => ´ => é)
- ref #107, Several improvements (Thanks @sebcaux)
- Fixed gapvector destructor: it did not use an array delete.
- TextEditorWidget::setHorizontalScrollBar not emits the correct horizontalScrollBarChanged event.
- (Did not include the condition defines, for older Qt versions)
- ref #106, Missing round function on SuSE. (Changed to qRound)
- ref #99, Speed improvements for markAll. (Added beginChanges and endChanges, to prevent updating)
- fix #96, Added support for readonly mode, via widget->setReadonly() or controller->setReadonly
Expand Down
23 changes: 15 additions & 8 deletions edbee-lib/edbee/models/textdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,14 @@ void TextDocument::beginChanges(TextEditorController* controller)


/// Replaces the given rangeset
void TextDocument::replaceRangeSet(TextRangeSet& rangeSet, const QString& textIn )
void TextDocument::replaceRangeSet(TextRangeSet& rangeSet, const QString& textIn, bool stickySelection)
{
return replaceRangeSet( rangeSet, QStringList(textIn) );
return replaceRangeSet(rangeSet, QStringList(textIn), stickySelection);
}


/// replaces the given rangeset
void TextDocument::replaceRangeSet(TextRangeSet& rangeSet, const QStringList& textsIn )
void TextDocument::replaceRangeSet(TextRangeSet& rangeSet, const QStringList& textsIn, bool stickySelection)
{

QStringList texts = textsIn;
Expand Down Expand Up @@ -237,14 +237,21 @@ void TextDocument::replaceRangeSet(TextRangeSet& rangeSet, const QStringList& te

// when a new caret position is supplied (can only happen via a TextDocumentFilter)
// access it and change the caret to the given position
int caret = 0;
if( effectiveChangeWithCaret && effectiveChangeWithCaret->caret() >= 0 ) {
range.setCaret( effectiveChangeWithCaret->caret() );

caret = effectiveChangeWithCaret->caret();
// Default caret location is change-independent: old location + length new text
} else {
range.setCaret(range.min() + text.length() );
caret = range.min() + text.length();
}

// sticky selection, keeps the selection around the text
if(stickySelection) {
range.set(range.min(), caret);
} else {
range.setCaret(caret);
range.reset();
}
range.reset();

// next range
if( rangeSet.rangeCount() < oldRangeCount ) {
Expand Down Expand Up @@ -321,7 +328,7 @@ void TextDocument::append(const QString& text, int coalesceId )
/// @param coalesceId (default 0) the coalesceId to use. Whe using the same number changes could be merged to one change. CoalesceId of 0 means no merging
void TextDocument::replace( int offset, int length, const QString& text, int coalesceId )
{
executeAndGiveChange( new TextChange( offset, length, text ), coalesceId );
executeAndGiveChange( new TextChange( offset, length, text ), coalesceId);
}


Expand Down
4 changes: 2 additions & 2 deletions edbee-lib/edbee/models/textdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ Q_OBJECT
virtual TextDocumentFilter* documentFilter();

void beginChanges( TextEditorController* controller );
void replaceRangeSet( TextRangeSet& rangeSet, const QString& text );
void replaceRangeSet( TextRangeSet& rangeSet, const QStringList& texts );
void replaceRangeSet(TextRangeSet& rangeSet, const QString& text, bool stickySelection = false);
void replaceRangeSet(TextRangeSet& rangeSet, const QStringList& texts, bool stickySelection = false);
void giveSelection( TextEditorController* controller, TextRangeSet* rangeSet);
void endChanges( int coalesceId );

Expand Down
22 changes: 11 additions & 11 deletions edbee-lib/edbee/texteditorcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,44 +579,44 @@ void TextEditorController::setReadonly(bool value)
/// @param length the number of characters to replace
/// @param text the text to replace
/// @param coalesceId the identifier for grouping undo operations
void TextEditorController::replace(int offset, int length, const QString& text, int coalesceId)
void TextEditorController::replace(int offset, int length, const QString& text, int coalesceId, bool stickySelection)
{
// SelectionTextChange* change = new SelectionTextChange(this);
TextRangeSet ranges(textDocument());
ranges.addRange(offset, offset+length);
replaceRangeSet(ranges,text,coalesceId);
replaceRangeSet(ranges,text,coalesceId, stickySelection);
}


/// This method replaces the selection with the given text
/// @param text the text to replace the selection with
/// @param coalesceId the identifier for grouping undo operations
void TextEditorController::replaceSelection(const QString& text, int coalesceId )
void TextEditorController::replaceSelection(const QString& text, int coalesceId, bool stickySelection)
{
replaceRangeSet( *dynamic_cast<TextRangeSet*>( textSelection() ), text, coalesceId );
replaceRangeSet( *dynamic_cast<TextRangeSet*>( textSelection() ), text, coalesceId, stickySelection);
}


/// This method replaces the given selection with the given texts
/// @param texts the list of texts that need to be replaced
/// @param coalesceID the identifier for grouping undo operation
void TextEditorController::replaceSelection(const QStringList& texts, int coalesceId)
void TextEditorController::replaceSelection(const QStringList& texts, int coalesceId, bool stickySelection)
{
replaceRangeSet( *dynamic_cast<TextRangeSet*>( textSelection() ), texts, coalesceId );
replaceRangeSet(*dynamic_cast<TextRangeSet*>( textSelection() ), texts, coalesceId, stickySelection);
}


/// Replaces the given rangeset with the given text
/// @param reangeSet hte ranges to replace
/// @param text the text to replace the selection with
/// @param coalesceId the identifier for grouping undo operations
void TextEditorController::replaceRangeSet(TextRangeSet& rangeSet, const QString& text, int coalesceId)
void TextEditorController::replaceRangeSet(TextRangeSet& rangeSet, const QString& text, int coalesceId, bool stickySelection)
{
if(readonly()) return;

textDocument()->beginChanges( this );
textDocument()->replaceRangeSet(rangeSet, text);
textDocument()->endChanges( coalesceId );
textDocument()->replaceRangeSet(rangeSet, text, stickySelection);
textDocument()->endChanges(coalesceId);
notifyStateChange();
}

Expand All @@ -625,12 +625,12 @@ void TextEditorController::replaceRangeSet(TextRangeSet& rangeSet, const QString
/// @param rangeSet the rangeset to fille
/// @param text the texts to fill the given ranges with.
/// @param coalesceId the identifier for grouping undo operations
void TextEditorController::replaceRangeSet(TextRangeSet& rangeSet, const QStringList& texts, int coalesceId)
void TextEditorController::replaceRangeSet(TextRangeSet& rangeSet, const QStringList& texts, int coalesceId, bool stickySelection)
{
if(readonly()) return;

textDocument()->beginChanges( this );
textDocument()->replaceRangeSet( rangeSet, texts );
textDocument()->replaceRangeSet( rangeSet, texts, stickySelection);
textDocument()->endChanges( coalesceId );
notifyStateChange();
}
Expand Down
10 changes: 5 additions & 5 deletions edbee-lib/edbee/texteditorcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ public slots:
virtual void storeSelection( int coalesceId=0 );

// replace the given selection
virtual void replace( int offset, int length, const QString& text, int coalesceId );
virtual void replaceSelection( const QString& text, int coalesceId=0 );
virtual void replaceSelection( const QStringList& texts, int coalesceId=0 );
virtual void replaceRangeSet(TextRangeSet& rangeSet, const QString& text, int coalesceId=0 );
virtual void replaceRangeSet(TextRangeSet& rangeSet, const QStringList& texts, int coalesceId=0 );
virtual void replace( int offset, int length, const QString& text, int coalesceId, bool stickySelection=false);
virtual void replaceSelection(const QString& text, int coalesceId=0, bool stickySelection=false);
virtual void replaceSelection(const QStringList& texts, int coalesceId=0, bool stickySelection=false);
virtual void replaceRangeSet(TextRangeSet& rangeSet, const QString& text, int coalesceId=0, bool stickySelection=false);
virtual void replaceRangeSet(TextRangeSet& rangeSet, const QStringList& texts, int coalesceId=0, bool stickySelection=false);

// caret movements
virtual void moveCaretTo( int line, int col, bool keepAnchors, int rangeIndex=-1 );
Expand Down
4 changes: 2 additions & 2 deletions edbee-lib/edbee/texteditorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ QScrollBar* TextEditorWidget::verticalScrollBar() const
void TextEditorWidget::setVerticalScrollBar(QScrollBar* scrollBar)
{
scrollAreaRef_->setVerticalScrollBar(scrollBar);
emit verticalScrollBarChanged( scrollBar );
emit verticalScrollBarChanged(scrollBar);
}


Expand All @@ -251,7 +251,7 @@ void TextEditorWidget::setVerticalScrollBar(QScrollBar* scrollBar)
void TextEditorWidget::setHorizontalScrollBar(QScrollBar* scrollBar)
{
scrollAreaRef_->setHorizontalScrollBar(scrollBar);
emit verticalScrollBarChanged( scrollBar );
emit horizontalScrollBarChanged(scrollBar);
}

/// Returns the auto scroll margin
Expand Down
2 changes: 1 addition & 1 deletion edbee-lib/edbee/util/gapvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class EDBEE_EXPORT GapVector {
}

~GapVector() {
delete items_;
delete[] items_;
}

/// returns the used length of the data
Expand Down
4 changes: 3 additions & 1 deletion edbee-lib/edbee/views/components/texteditorcomponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ void TextEditorComponent::keyReleaseEvent(QKeyEvent *event)

void TextEditorComponent::inputMethodEvent( QInputMethodEvent* m )
{
// replace the selection with an empty text

// replace the selection with an empty text
if( textSelection()->hasSelection() ) {
controller()->replaceSelection("",false);
}
Expand All @@ -355,6 +356,7 @@ void TextEditorComponent::inputMethodEvent( QInputMethodEvent* m )

if( !m->preeditString().isEmpty() ) {
// replaceSelection(m->preeditString());
controller()->replaceSelection(m->preeditString(),false, true);
} else {
controller()->replaceSelection(m->commitString(),false);
}
Expand Down
8 changes: 5 additions & 3 deletions edbee-lib/edbee/views/components/texteditorrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ void TextEditorRenderer::renderLineBorderedRanges(QPainter *painter,int line)
int lineHeight = renderer()->lineHeight();

QPen pen(themeRef_->foregroundColor(), 0.5);
// QPen pen(themeRef_->findHighlightForegroundColor(), 0.5);
// QBrush brush(themeRef_->findHighlightBackgroundColor());
painter->setRenderHint(QPainter::Antialiasing);

int firstRangeIdx=0;
Expand All @@ -143,15 +145,15 @@ void TextEditorRenderer::renderLineBorderedRanges(QPainter *painter,int line)
int startColumn = doc->columnFromOffsetAndLine( range.min(), line );
int endColumn = doc->columnFromOffsetAndLine( range.max(), line );

int startX = textLine.cursorToX( startColumn );
int endX = textLine.cursorToX( endColumn );
qreal startX = textLine.cursorToX( startColumn );
qreal endX = textLine.cursorToX( endColumn );

if( range.length() > 0 && endColumn+1 >= lastLineColumn) endX += 3;

QPainterPath path;
path.addRoundedRect(startX, line*lineHeight + rect.top(), endX - startX, rect.height(),5,5);
// painter->fillPath(path, brush);
painter->strokePath(path, pen);
// painter->strok(startX, line*lineHeight + rect.top(), endX - startX, rect.height(), themeRef_->selectionColor() ); //QColor::fromRgb(0xDD, 0x88, 0xEE) );
}
}
//PROF_END
Expand Down

0 comments on commit 6fc9e87

Please sign in to comment.