diff --git a/src/include/ktexteditor/inlinenote.h b/src/include/ktexteditor/inlinenote.h --- a/src/include/ktexteditor/inlinenote.h +++ b/src/include/ktexteditor/inlinenote.h @@ -28,6 +28,7 @@ #include class QFont; +class KateInlineNoteData; namespace KTextEditor { class InlineNoteProvider; } namespace KTextEditor { @@ -53,34 +54,13 @@ * notes are created from the columns returned by InlineNoteProvider::inlineNotes(int line), * and then passed around as handles grouping useful information. */ - InlineNote(InlineNoteProvider* provider, const KTextEditor::Cursor& position, int index, - const KTextEditor::View* view, QFont font, int lineHeight, bool hasFocus); - - /** - * Constructs an invalid inline note, i.e. isValid() will return false. - */ - InlineNote(); - - /** - * Returns the column this note appears in. - */ - int column() const; + InlineNote(const KateInlineNoteData & data); /** * Returns the width of this note in pixels. */ qreal width() const; - /** - * Tells whether this note is valid, i.e. whether it has a valid provider and location set. - */ - bool isValid() const; - - /** - * Equality of notes. Only checks provider, index, and position. - */ - bool operator==(const InlineNote& other) const; - /** * Transforms the given @p pos from note coordinates to global (screen) coordinates. * @@ -111,10 +91,10 @@ int index() const; /** - * Whether the mouse cursor is currently over this note; only set - * when paintInlineNote() is called + * Returns whether the mouse cursor is currently over this note. + * @note This flag is only valid when in InlineNoteProvider::paintInlineNote(). */ - bool hasFocus() const; + bool underMouse() const; /** * The font of the text surrounding this note @@ -127,17 +107,8 @@ int lineHeight() const; private: - InlineNoteProvider* m_provider = nullptr; - const KTextEditor::View* m_view = nullptr; - KTextEditor::Cursor m_position; - int m_index = -1; - bool m_hasFocus = false; - QFont m_font; - int m_lineHeight = -1; - - // For future use, in case members need to be added. - // TODO KF6: remove if it turns out this is unneeded. - class InlineNotePrivate* d = nullptr; + // Internal implementation data structure. + const KateInlineNoteData & d; }; } diff --git a/src/render/katerenderer.h b/src/render/katerenderer.h --- a/src/render/katerenderer.h +++ b/src/render/katerenderer.h @@ -35,7 +35,6 @@ namespace KTextEditor { class DocumentPrivate; } namespace KTextEditor { class ViewPrivate; } -namespace KTextEditor { class InlineNote; } class KateRendererConfig; class KateRenderRange; namespace Kate diff --git a/src/render/katerenderer.cpp b/src/render/katerenderer.cpp --- a/src/render/katerenderer.cpp +++ b/src/render/katerenderer.cpp @@ -30,6 +30,7 @@ #include "katerenderrange.h" #include "katetextlayout.h" #include "katebuffer.h" +#include "inlinenotedata.h" #include "ktexteditor/inlinenote.h" #include "ktexteditor/inlinenoteprovider.h" @@ -763,8 +764,9 @@ // Draw inline notes const auto inlineNotes = m_view->inlineNotes(range->line()); - for (const KTextEditor::InlineNote& inlineNote: inlineNotes) { - int column = inlineNote.column(); + for (const auto& inlineNoteData: inlineNotes) { + KTextEditor::InlineNote inlineNote(inlineNoteData); + const int column = inlineNote.position().column(); int viewLine = range->viewLineForColumn(column); // Determine the position where to paint the note. @@ -1047,7 +1049,7 @@ const auto inlineNotes = m_view->inlineNotes(lineLayout->line()); for (const KTextEditor::InlineNote& inlineNote: inlineNotes) { - int column = inlineNote.column(); + const int column = inlineNote.position().column(); int width = inlineNote.width(); // Make space for every inline note. diff --git a/src/utils/ktexteditor.cpp b/src/utils/ktexteditor.cpp --- a/src/utils/ktexteditor.cpp +++ b/src/utils/ktexteditor.cpp @@ -34,6 +34,7 @@ #include "command.h" #include "inlinenoteinterface.h" #include "inlinenote.h" +#include "inlinenotedata.h" #include "inlinenoteprovider.h" #include "markinterface.h" #include "modificationinterface.h" @@ -225,33 +226,35 @@ InlineNoteProvider::~InlineNoteProvider() {} -InlineNote::InlineNote(InlineNoteProvider* provider, const KTextEditor::Cursor& position, int index, - const KTextEditor::View* view, QFont font, int lineHeight, bool hasFocus) +KateInlineNoteData::KateInlineNoteData(KTextEditor::InlineNoteProvider* provider, + const KTextEditor::View* view, + const KTextEditor::Cursor& position, + int index, + bool underMouse, + const QFont& font, + int lineHeight) : m_provider(provider) , m_view(view) , m_position(position) , m_index(index) - , m_hasFocus(hasFocus) + , m_underMouse(underMouse) , m_font(font) , m_lineHeight(lineHeight) -{ -} - -InlineNote::InlineNote() = default; +{} -int InlineNote::column() const +InlineNote::InlineNote(const KateInlineNoteData& data) + : d(data) { - return m_position.column(); } qreal InlineNote::width() const { - return m_provider->inlineNoteSize(*this).width(); + return d.m_provider->inlineNoteSize(*this).width(); } -bool KTextEditor::InlineNote::hasFocus() const +bool KTextEditor::InlineNote::underMouse() const { - return m_hasFocus; + return d.m_underMouse; } void KTextEditor::InlineNoteProvider::inlineNoteActivated(const InlineNote& note, Qt::MouseButtons buttons, const QPoint& pos) @@ -278,49 +281,40 @@ Q_UNUSED(pos); } -bool InlineNote::isValid() const -{ - return m_provider != nullptr && m_position.isValid(); -} - -bool InlineNote::operator==(const InlineNote& other) const { - return m_provider == other.m_provider && m_position == other.m_position && m_index == other.m_index; -} - QPoint InlineNote::mapToGlobal(const QPoint& pos) const { - auto localPos = static_cast(m_view)->inlineNoteRect(*this).topLeft() + pos; - return m_view->mapToGlobal(localPos); + auto localPos = static_cast(d.m_view)->inlineNoteRect(d).topLeft() + pos; + return d.m_view->mapToGlobal(localPos); } KTextEditor::InlineNoteProvider* InlineNote::provider() const { - return m_provider; + return d.m_provider; } const KTextEditor::View* InlineNote::view() const { - return m_view; + return d.m_view; } QFont InlineNote::font() const { - return m_font; + return d.m_font; } int InlineNote::index() const { - return m_index; + return d.m_index; } int InlineNote::lineHeight() const { - return m_lineHeight; + return d.m_lineHeight; } KTextEditor::Cursor InlineNote::position() const { - return m_position; + return d.m_position; } Command::Command(const QStringList &cmds, QObject *parent) diff --git a/src/view/inlinenotedata.h b/src/view/inlinenotedata.h new file mode 100644 --- /dev/null +++ b/src/view/inlinenotedata.h @@ -0,0 +1,60 @@ +/* This file is part of the KDE libraries + + Copyright 2018 Sven Brauch + Copyright 2018 Michal Srb + Copyright 2018 Dominik Haumann + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ +#ifndef KATE_INLINENOTE_DATA_H +#define KATE_INLINENOTE_DATA_H + +#include +#include + +namespace KTextEditor { + class InlineNoteProvider; + class View; +} + +/** + * Internal data container for KTextEditor::InlineNote interface. + */ +class KateInlineNoteData +{ +public: + KateInlineNoteData() = default; + KateInlineNoteData(KTextEditor::InlineNoteProvider* provider, + const KTextEditor::View* view, + const KTextEditor::Cursor& position, + int index, + bool underMouse, + const QFont& font, + int lineHeight); + + KTextEditor::InlineNoteProvider* m_provider = nullptr; + const KTextEditor::View* m_view = nullptr; + KTextEditor::Cursor m_position; + int m_index = -1; + bool m_underMouse = false; + QFont m_font; + int m_lineHeight = -1; +}; + +#endif diff --git a/src/view/kateview.h b/src/view/kateview.h --- a/src/view/kateview.h +++ b/src/view/kateview.h @@ -70,6 +70,7 @@ class KateAbstractInputMode; class KateScriptActionMenu; class KateMessageLayout; +class KateInlineNoteData; class KToggleAction; class KSelectAction; @@ -260,9 +261,9 @@ public: void registerInlineNoteProvider(KTextEditor::InlineNoteProvider *provider) Q_DECL_OVERRIDE; void unregisterInlineNoteProvider(KTextEditor::InlineNoteProvider *provider) Q_DECL_OVERRIDE; - QRect inlineNoteRect(const KTextEditor::InlineNote& note) const; + QRect inlineNoteRect(const KateInlineNoteData& note) const; - QVarLengthArray inlineNotes(int line) const; + QVarLengthArray inlineNotes(int line) const; private: QVector m_inlineNoteProviders; diff --git a/src/view/kateview.cpp b/src/view/kateview.cpp --- a/src/view/kateview.cpp +++ b/src/view/kateview.cpp @@ -57,6 +57,7 @@ #include "printing/kateprinter.h" #include "katestatusbar.h" #include "kateabstractinputmode.h" +#include "inlinenotedata.h" #include #include @@ -3670,29 +3671,29 @@ } } -QVarLengthArray KTextEditor::ViewPrivate::inlineNotes(int line) const +QVarLengthArray KTextEditor::ViewPrivate::inlineNotes(int line) const { - QVarLengthArray allInlineNotes; + QVarLengthArray allInlineNotes; for (KTextEditor::InlineNoteProvider *provider: m_inlineNoteProviders) { int index = 0; for (auto column: provider->inlineNotes(line)) { - KTextEditor::InlineNote note = { + KateInlineNoteData note = { provider, + this, {line, column}, index, - this, + m_viewInternal->m_activeInlineNote.m_underMouse, m_viewInternal->renderer()->currentFont(), - m_viewInternal->renderer()->lineHeight(), - m_viewInternal->m_activeInlineNote.hasFocus(), + m_viewInternal->renderer()->lineHeight() }; allInlineNotes.append(note); index++; } } return allInlineNotes; } -QRect KTextEditor::ViewPrivate::inlineNoteRect(const KTextEditor::InlineNote& note) const +QRect KTextEditor::ViewPrivate::inlineNoteRect(const KateInlineNoteData& note) const { return m_viewInternal->inlineNoteRect(note); } @@ -3705,7 +3706,7 @@ void KTextEditor::ViewPrivate::inlineNotesLineChanged(int line) { - if ( line == m_viewInternal->m_activeInlineNote.position().line() ) { + if ( line == m_viewInternal->m_activeInlineNote.m_position.line() ) { m_viewInternal->m_activeInlineNote = {}; } tagLines(line, line, true); diff --git a/src/view/kateviewinternal.h b/src/view/kateviewinternal.h --- a/src/view/kateviewinternal.h +++ b/src/view/kateviewinternal.h @@ -27,13 +27,13 @@ #define _KATE_VIEW_INTERNAL_ #include -#include #include "katetextcursor.h" #include "katetextline.h" #include "katedocument.h" #include "kateview.h" #include "katerenderer.h" +#include "inlinenotedata.h" #include #include @@ -468,10 +468,10 @@ QMap m_inputModes; KateAbstractInputMode *m_currentInputMode; - KTextEditor::InlineNote m_activeInlineNote; - KTextEditor::InlineNote inlineNoteAt(const QPoint& pos) const; - QRect inlineNoteRect(const KTextEditor::InlineNote& note) const; - QPoint toNoteCoordinates(const QPoint& pos, const KTextEditor::InlineNote& note) const; + KateInlineNoteData m_activeInlineNote; + KateInlineNoteData inlineNoteAt(const QPoint& pos) const; + QRect inlineNoteRect(const KateInlineNoteData& note) const; + QPoint toNoteCoordinates(const QPoint& pos, const KateInlineNoteData& note) const; }; #endif diff --git a/src/view/kateviewinternal.cpp b/src/view/kateviewinternal.cpp --- a/src/view/kateviewinternal.cpp +++ b/src/view/kateviewinternal.cpp @@ -44,6 +44,7 @@ #include "kateabstractinputmodefactory.h" #include "kateabstractinputmode.h" #include "katepartdebug.h" +#include "inlinenotedata.h" #include #include @@ -2589,9 +2590,10 @@ void KateViewInternal::mousePressEvent(QMouseEvent *e) { // was an inline note clicked? - auto note = inlineNoteAt(e->pos()); - if (note.isValid()) { - note.provider()->inlineNoteActivated(note, e->button(), toNoteCoordinates(e->pos(), note)); + const auto noteData = inlineNoteAt(e->pos()); + const KTextEditor::InlineNote note(noteData); + if (note.position().isValid()) { + note.provider()->inlineNoteActivated(noteData, e->button(), toNoteCoordinates(e->pos(), noteData)); return; } @@ -2886,23 +2888,25 @@ } if (e->buttons() == Qt::NoButton) { - auto note = inlineNoteAt(e->pos()); - if (note.isValid()) { - const auto notePos = toNoteCoordinates(e->pos(), note); - if (!m_activeInlineNote.isValid()) { + const auto noteData = inlineNoteAt(e->pos()); + const KTextEditor::InlineNote note(noteData); + const KTextEditor::InlineNote activeNote(m_activeInlineNote); + if (note.position().isValid()) { + const auto notePos = toNoteCoordinates(e->pos(), noteData); + if (!activeNote.position().isValid()) { // no active note -- focus in note.provider()->inlineNoteFocusInEvent(note, notePos); - m_activeInlineNote = note; + m_activeInlineNote = noteData; } else { note.provider()->inlineNoteMouseMoveEvent(note, notePos); } // the note might change its appearance in result to the event tagLines(note.position(), note.position(), true); } - else if (m_activeInlineNote.isValid()) { - m_activeInlineNote.provider()->inlineNoteFocusOutEvent(m_activeInlineNote); - tagLines(m_activeInlineNote.position(), m_activeInlineNote.position(), true); + else if (activeNote.position().isValid()) { + activeNote.provider()->inlineNoteFocusOutEvent(activeNote); + tagLines(activeNote.position(), activeNote.position(), true); m_activeInlineNote = {}; } } @@ -3903,11 +3907,12 @@ #endif } -QRect KateViewInternal::inlineNoteRect(const KTextEditor::InlineNote& note) const +QRect KateViewInternal::inlineNoteRect(const KateInlineNoteData& noteData) const { + KTextEditor::InlineNote note(noteData); // compute note width and position - auto noteWidth = note.width(); - auto noteCursor = KTextEditor::Cursor(note.position().line(), note.column()); + const auto noteWidth = note.width(); + auto noteCursor = note.position(); // The cursor might be outside of the text. In that case, clamp it to the text and // later on add the missing x offset. @@ -3924,7 +3929,7 @@ return noteRect; } -KTextEditor::InlineNote KateViewInternal::inlineNoteAt(const QPoint& pos) const +KateInlineNoteData KateViewInternal::inlineNoteAt(const QPoint& pos) const { // compute the associated cursor to get the right line auto cursor = m_view->coordinatesToCursor(pos); @@ -3940,7 +3945,7 @@ return {}; } -QPoint KateViewInternal::toNoteCoordinates(const QPoint& pos, const KTextEditor::InlineNote& note) const +QPoint KateViewInternal::toNoteCoordinates(const QPoint& pos, const KateInlineNoteData& note) const { return pos - inlineNoteRect(note).topLeft(); }