From 0c622658ad78dd6bfe4054dc553a1929be198599 Mon Sep 17 00:00:00 2001 From: Rick Blommers Date: Wed, 19 Aug 2020 15:35:34 +0200 Subject: [PATCH] =?UTF-8?q?ref=20#107,=20Several=20improvements=20=20(Than?= =?UTF-8?q?ks=20@sebcaux)=20Support=20for=20sticky-selection=20in=20replac?= =?UTF-8?q?eSelection=20methods.=20(Required=20for=20InpuMethod=20entry)?= =?UTF-8?q?=20Improved=20TextEditorComponent::InputMethodEvent...=20It=20n?= =?UTF-8?q?ow=20support=20special=20chars=20entry=20like=20expected.=20(Op?= =?UTF-8?q?tion+e,=20=20e=20=3D>=20=C2=B4=20=3D>=20=C3=A9)=20-=20Fixed=20g?= =?UTF-8?q?apvector=20destructor:=20it=20did=20not=20use=20an=20array=20de?= =?UTF-8?q?lete.=20-=20TextEditorWidget::setHorizontalScrollBar=20not=20em?= =?UTF-8?q?its=20the=20correct=20horizontalScrollBarChanged=20event.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 6 +++++ edbee-lib/edbee/models/textdocument.cpp | 23 ++++++++++++------- edbee-lib/edbee/models/textdocument.h | 4 ++-- edbee-lib/edbee/texteditorcontroller.cpp | 22 +++++++++--------- edbee-lib/edbee/texteditorcontroller.h | 10 ++++---- edbee-lib/edbee/texteditorwidget.cpp | 4 ++-- edbee-lib/edbee/util/gapvector.h | 2 +- .../views/components/texteditorcomponent.cpp | 4 +++- .../views/components/texteditorrenderer.cpp | 8 ++++--- 9 files changed, 50 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb2e6f1..9971743 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/edbee-lib/edbee/models/textdocument.cpp b/edbee-lib/edbee/models/textdocument.cpp index 5dd1749..cb81e60 100644 --- a/edbee-lib/edbee/models/textdocument.cpp +++ b/edbee-lib/edbee/models/textdocument.cpp @@ -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; @@ -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 ) { @@ -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); } diff --git a/edbee-lib/edbee/models/textdocument.h b/edbee-lib/edbee/models/textdocument.h index 98b759d..a18fb36 100644 --- a/edbee-lib/edbee/models/textdocument.h +++ b/edbee-lib/edbee/models/textdocument.h @@ -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 ); diff --git a/edbee-lib/edbee/texteditorcontroller.cpp b/edbee-lib/edbee/texteditorcontroller.cpp index 6dcec2f..999852c 100644 --- a/edbee-lib/edbee/texteditorcontroller.cpp +++ b/edbee-lib/edbee/texteditorcontroller.cpp @@ -579,30 +579,30 @@ 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( textSelection() ), text, coalesceId ); + replaceRangeSet( *dynamic_cast( 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( textSelection() ), texts, coalesceId ); + replaceRangeSet(*dynamic_cast( textSelection() ), texts, coalesceId, stickySelection); } @@ -610,13 +610,13 @@ void TextEditorController::replaceSelection(const QStringList& texts, int coales /// @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(); } @@ -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(); } diff --git a/edbee-lib/edbee/texteditorcontroller.h b/edbee-lib/edbee/texteditorcontroller.h index a7194cb..d760159 100644 --- a/edbee-lib/edbee/texteditorcontroller.h +++ b/edbee-lib/edbee/texteditorcontroller.h @@ -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 ); diff --git a/edbee-lib/edbee/texteditorwidget.cpp b/edbee-lib/edbee/texteditorwidget.cpp index e6dea21..101e012 100644 --- a/edbee-lib/edbee/texteditorwidget.cpp +++ b/edbee-lib/edbee/texteditorwidget.cpp @@ -241,7 +241,7 @@ QScrollBar* TextEditorWidget::verticalScrollBar() const void TextEditorWidget::setVerticalScrollBar(QScrollBar* scrollBar) { scrollAreaRef_->setVerticalScrollBar(scrollBar); - emit verticalScrollBarChanged( scrollBar ); + emit verticalScrollBarChanged(scrollBar); } @@ -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 diff --git a/edbee-lib/edbee/util/gapvector.h b/edbee-lib/edbee/util/gapvector.h index 8b8d8c5..e59e8e7 100644 --- a/edbee-lib/edbee/util/gapvector.h +++ b/edbee-lib/edbee/util/gapvector.h @@ -29,7 +29,7 @@ class EDBEE_EXPORT GapVector { } ~GapVector() { - delete items_; + delete[] items_; } /// returns the used length of the data diff --git a/edbee-lib/edbee/views/components/texteditorcomponent.cpp b/edbee-lib/edbee/views/components/texteditorcomponent.cpp index 8986e25..0d8501d 100644 --- a/edbee-lib/edbee/views/components/texteditorcomponent.cpp +++ b/edbee-lib/edbee/views/components/texteditorcomponent.cpp @@ -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); } @@ -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); } diff --git a/edbee-lib/edbee/views/components/texteditorrenderer.cpp b/edbee-lib/edbee/views/components/texteditorrenderer.cpp index 3877b95..3ec1ebf 100644 --- a/edbee-lib/edbee/views/components/texteditorrenderer.cpp +++ b/edbee-lib/edbee/views/components/texteditorrenderer.cpp @@ -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; @@ -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