Files
qt-creator/src/plugins/texteditor/basetexteditor.h

688 lines
21 KiB
C
Raw Normal View History

/****************************************************************************
2008-12-02 12:01:29 +01:00
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator.
2008-12-02 12:01:29 +01:00
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
2008-12-02 16:19:05 +01:00
2008-12-02 12:01:29 +01:00
#ifndef BASETEXTEDITOR_H
#define BASETEXTEDITOR_H
#include "itexteditor.h"
#include "codeassist/assistenums.h"
2008-12-02 12:01:29 +01:00
#include <coreplugin/editormanager/editormanager.h>
#include <find/ifindsupport.h>
#include <QPlainTextEdit>
#include <QSharedPointer>
2008-12-02 12:01:29 +01:00
QT_BEGIN_NAMESPACE
class QToolBar;
class QTimeLine;
class QPrinter;
2008-12-02 12:01:29 +01:00
QT_END_NAMESPACE
namespace Utils {
class LineColumnLabel;
class ChangeSet;
2008-12-02 12:01:29 +01:00
}
namespace TextEditor {
class TabSettings;
class RefactorOverlay;
struct RefactorMarker;
class IAssistMonitorInterface;
class IAssistInterface;
class IAssistProvider;
class ICodeStylePreferences;
typedef QList<RefactorMarker> RefactorMarkers;
2008-12-02 12:01:29 +01:00
namespace Internal {
class BaseTextEditorWidgetPrivate;
class TextEditorOverlay;
typedef QString (QString::*TransformationMethod)() const;
2008-12-02 12:01:29 +01:00
}
class ITextMarkable;
class BaseTextDocument;
class BaseTextEditor;
2008-12-02 12:01:29 +01:00
class FontSettings;
class BehaviorSettings;
class CompletionSettings;
class DisplaySettings;
class TypingSettings;
class StorageSettings;
class Indenter;
class AutoCompleter;
class ExtraEncodingSettings;
2008-12-02 12:01:29 +01:00
class TEXTEDITOR_EXPORT BaseTextEditorAnimator : public QObject
{
Q_OBJECT
public:
BaseTextEditorAnimator(QObject *parent);
inline void setPosition(int position) { m_position = position; }
inline int position() const { return m_position; }
void setData(QFont f, QPalette pal, const QString &text);
void draw(QPainter *p, const QPointF &pos);
QRectF rect() const;
inline qreal value() const { return m_value; }
inline QPointF lastDrawPos() const { return m_lastDrawPos; }
void finish();
2009-04-28 19:02:58 +02:00
bool isRunning() const;
signals:
void updateRequest(int position, QPointF lastPos, QRectF rect);
private slots:
void step(qreal v);
private:
QTimeLine *m_timeline;
qreal m_value;
int m_position;
QPointF m_lastDrawPos;
QFont m_font;
QPalette m_palette;
QString m_text;
QSizeF m_size;
};
class TEXTEDITOR_EXPORT BaseTextEditorWidget : public QPlainTextEdit
2008-12-02 12:01:29 +01:00
{
Q_OBJECT
Q_PROPERTY(int verticalBlockSelectionFirstColumn READ verticalBlockSelectionFirstColumn)
Q_PROPERTY(int verticalBlockSelectionLastColumn READ verticalBlockSelectionLastColumn)
2008-12-02 12:01:29 +01:00
public:
BaseTextEditorWidget(QWidget *parent);
~BaseTextEditorWidget();
2008-12-02 12:01:29 +01:00
static Core::IEditor *openEditorAt(const QString &fileName, int line, int column = 0,
const Core::Id &editorId = Core::Id(),
Core::EditorManager::OpenEditorFlags flags = Core::EditorManager::IgnoreNavigationHistory,
bool *newEditor = 0);
2008-12-02 12:01:29 +01:00
const Utils::ChangeSet &changeSet() const;
void setChangeSet(const Utils::ChangeSet &changeSet);
2008-12-02 12:01:29 +01:00
// EditorInterface
Core::IDocument *editorDocument() const;
2008-12-02 12:01:29 +01:00
bool createNew(const QString &contents);
virtual bool open(QString *errorString, const QString &fileName, const QString &realFileName);
2008-12-02 12:01:29 +01:00
QByteArray saveState() const;
bool restoreState(const QByteArray &state);
QString displayName() const;
// ITextEditor
void gotoLine(int line, int column = 0);
2008-12-02 12:01:29 +01:00
int position(
ITextEditor::PositionOperation posOp = ITextEditor::Current
, int at = -1) const;
void convertPosition(int pos, int *line, int *column) const;
BaseTextEditor *editor() const;
2008-12-02 12:01:29 +01:00
ITextMarkable *markableInterface() const;
QChar characterAt(int pos) const;
void print(QPrinter *);
void setSuggestedFileName(const QString &suggestedFileName);
QString mimeType() const;
virtual void setMimeType(const QString &mt);
2008-12-02 12:01:29 +01:00
void appendMenuActionsFromContext(QMenu *menu, const Core::Id menuContextId);
void appendStandardContextMenuActions(QMenu *menu);
2008-12-02 12:01:29 +01:00
// Works only in conjunction with a syntax highlighter that puts
// parentheses into text block user data
void setParenthesesMatchingEnabled(bool b);
bool isParenthesesMatchingEnabled() const;
void setHighlightCurrentLine(bool b);
bool highlightCurrentLine() const;
void setLineNumbersVisible(bool b);
bool lineNumbersVisible() const;
void setAlwaysOpenLinksInNextSplit(bool b);
bool alwaysOpenLinksInNextSplit() const;
2008-12-02 12:01:29 +01:00
void setMarksVisible(bool b);
bool marksVisible() const;
void setRequestMarkEnabled(bool b);
bool requestMarkEnabled() const;
void setLineSeparatorsAllowed(bool b);
bool lineSeparatorsAllowed() const;
void updateCodeFoldingVisible();
2008-12-02 12:01:29 +01:00
bool codeFoldingVisible() const;
void setCodeFoldingSupported(bool b);
bool codeFoldingSupported() const;
void setMouseNavigationEnabled(bool b);
bool mouseNavigationEnabled() const;
void setScrollWheelZoomingEnabled(bool b);
bool scrollWheelZoomingEnabled() const;
void setConstrainTooltips(bool b);
bool constrainTooltips() const;
void setCamelCaseNavigationEnabled(bool b);
bool camelCaseNavigationEnabled() const;
2008-12-02 12:01:29 +01:00
void setRevisionsVisible(bool b);
bool revisionsVisible() const;
void setVisibleWrapColumn(int column);
int visibleWrapColumn() const;
int columnCount() const;
int rowCount() const;
2008-12-02 12:01:29 +01:00
void setActionHack(QObject *);
QObject *actionHack() const;
void setTextCodec(QTextCodec *codec);
QTextCodec *textCodec() const;
void setReadOnly(bool b);
void setTextCursor(const QTextCursor &cursor);
void insertCodeSnippet(const QTextCursor &cursor, const QString &snippet);
void setBlockSelection(bool on);
bool hasBlockSelection() const;
int verticalBlockSelectionFirstColumn() const;
int verticalBlockSelectionLastColumn() const;
QRegion translatedLineRegion(int lineStart, int lineEnd) const;
void setIndenter(Indenter *indenter);
Indenter *indenter() const;
void setAutoCompleter(AutoCompleter *autoCompleter);
AutoCompleter *autoCompleter() const;
QPoint toolTipPosition(const QTextCursor &c) const;
void invokeAssist(AssistKind assistKind, IAssistProvider *provider = 0);
virtual IAssistInterface *createAssistInterface(AssistKind assistKind,
AssistReason assistReason) const;
QMimeData *duplicateMimeData(const QMimeData *source) const;
2008-12-02 12:01:29 +01:00
public slots:
void setDisplayName(const QString &title);
virtual void copy();
virtual void paste();
virtual void cut();
virtual void selectAll();
2008-12-02 12:01:29 +01:00
void circularPaste();
void switchUtf8bom();
2008-12-02 12:01:29 +01:00
void zoomIn(int range = 1);
void zoomOut(int range = 1);
void zoomReset();
2008-12-02 12:01:29 +01:00
void cutLine();
void copyLine();
2008-12-02 12:01:29 +01:00
void deleteLine();
void deleteEndOfWord();
void deleteEndOfWordCamelCase();
void deleteStartOfWord();
void deleteStartOfWordCamelCase();
void unfoldAll();
void fold();
void unfold();
2008-12-02 12:01:29 +01:00
void selectEncoding();
void gotoBlockStart();
void gotoBlockEnd();
void gotoBlockStartWithSelection();
void gotoBlockEndWithSelection();
void gotoLineStart();
void gotoLineStartWithSelection();
void gotoLineEnd();
void gotoLineEndWithSelection();
void gotoNextLine();
void gotoNextLineWithSelection();
void gotoPreviousLine();
void gotoPreviousLineWithSelection();
void gotoPreviousCharacter();
void gotoPreviousCharacterWithSelection();
void gotoNextCharacter();
void gotoNextCharacterWithSelection();
void gotoPreviousWord();
void gotoPreviousWordWithSelection();
void gotoNextWord();
void gotoNextWordWithSelection();
void gotoPreviousWordCamelCase();
void gotoPreviousWordCamelCaseWithSelection();
void gotoNextWordCamelCase();
void gotoNextWordCamelCaseWithSelection();
bool selectBlockUp();
bool selectBlockDown();
void moveLineUp();
void moveLineDown();
void copyLineUp();
void copyLineDown();
void joinLines();
void insertLineAbove();
void insertLineBelow();
void uppercaseSelection();
void lowercaseSelection();
void cleanWhitespace();
void indent();
void unindent();
void openLinkUnderCursor();
void openLinkUnderCursorInNextSplit();
/// Abort code assistant if it is running.
void abortAssist();
2008-12-02 12:01:29 +01:00
signals:
void changed();
void assistFinished();
2008-12-02 12:01:29 +01:00
// ITextEditor
void contentsChanged();
protected:
bool event(QEvent *e);
void keyPressEvent(QKeyEvent *e);
void wheelEvent(QWheelEvent *e);
void changeEvent(QEvent *e);
void focusInEvent(QFocusEvent *e);
void focusOutEvent(QFocusEvent *e);
2008-12-02 12:01:29 +01:00
2009-06-17 19:12:19 +02:00
void showEvent(QShowEvent *);
2008-12-02 12:01:29 +01:00
QMimeData *createMimeDataFromSelection() const;
bool canInsertFromMimeData(const QMimeData *source) const;
void insertFromMimeData(const QMimeData *source);
virtual QString plainTextFromSelection() const;
static QString convertToPlainText(const QString &txt);
virtual QString lineNumber(int blockNumber) const;
virtual int lineNumberTopPositionOffset(int blockNumber) const;
virtual int lineNumberDigits() const;
virtual bool selectionVisible(int blockNumber) const;
virtual bool replacementVisible(int blockNumber) const;
static QString msgTextTooLarge(quint64 size);
private:
void maybeSelectLine();
void updateCannotDecodeInfo();
2008-12-02 12:01:29 +01:00
public:
void duplicateFrom(BaseTextEditorWidget *editor);
2008-12-02 12:01:29 +01:00
protected:
QSharedPointer<BaseTextDocument> baseTextDocument() const;
void setBaseTextDocument(const QSharedPointer<BaseTextDocument> &doc);
2008-12-02 12:01:29 +01:00
void setDefaultPath(const QString &defaultPath);
virtual BaseTextEditor *createEditor() = 0;
2008-12-02 12:01:29 +01:00
private slots:
void editorContentsChange(int position, int charsRemoved, int charsAdded);
void documentAboutToBeReloaded();
void documentReloaded();
void highlightSearchResults(const QString &txt, Find::FindFlags findFlags);
void setFindScope(const QTextCursor &start, const QTextCursor &end, int, int);
bool inFindScope(const QTextCursor &cursor);
bool inFindScope(int selectionStart, int selectionEnd);
void inSnippetMode(bool *active);
void onCodeStylePreferencesDestroyed();
2008-12-02 12:01:29 +01:00
private:
Internal::BaseTextEditorWidgetPrivate *d;
friend class Internal::BaseTextEditorWidgetPrivate;
friend class Internal::TextEditorOverlay;
friend class RefactorOverlay;
2008-12-02 12:01:29 +01:00
public:
QWidget *extraArea() const;
virtual int extraAreaWidth(int *markWidthPtr = 0) const;
virtual void extraAreaPaintEvent(QPaintEvent *);
virtual void extraAreaLeaveEvent(QEvent *);
virtual void extraAreaContextMenuEvent(QContextMenuEvent *);
virtual void extraAreaMouseEvent(QMouseEvent *);
void updateFoldingHighlight(const QPoint &pos);
2008-12-02 12:01:29 +01:00
const TabSettings &tabSettings() const;
void setLanguageSettingsId(Core::Id settingsId);
Core::Id languageSettingsId() const;
void setCodeStyle(ICodeStylePreferences *settings);
2008-12-02 12:01:29 +01:00
const DisplaySettings &displaySettings() const;
void markBlocksAsChanged(QList<int> blockNumbers);
void ensureCursorVisible();
enum ExtraSelectionKind {
CurrentLineSelection,
ParenthesesMatchingSelection,
CodeWarningsSelection,
CodeSemanticsSelection,
UndefinedSymbolSelection,
2009-11-30 15:21:16 +01:00
UnusedSymbolSelection,
2009-01-13 13:08:21 +01:00
FakeVimSelection,
2009-11-30 15:21:16 +01:00
OtherSelection,
SnippetPlaceholderSelection,
ObjCSelection,
DebuggerExceptionSelection,
NExtraSelectionKinds
};
void setExtraSelections(ExtraSelectionKind kind, const QList<QTextEdit::ExtraSelection> &selections);
QList<QTextEdit::ExtraSelection> extraSelections(ExtraSelectionKind kind) const;
QString extraSelectionTooltip(int pos) const;
2008-12-02 12:01:29 +01:00
RefactorMarkers refactorMarkers() const;
void setRefactorMarkers(const RefactorMarkers &markers);
signals:
void refactorMarkerClicked(const TextEditor::RefactorMarker &marker);
public:
struct BlockRange
{
BlockRange() : first(0), last(-1) {}
BlockRange(int first_position, int last_position)
: first(first_position), last(last_position)
{}
2008-12-02 12:01:29 +01:00
int first;
int last;
inline bool isNull() const { return last < first; }
};
// the blocks list must be sorted
void setIfdefedOutBlocks(const QList<BlockRange> &blocks);
2008-12-02 12:01:29 +01:00
public slots:
virtual void format();
virtual void rewrapParagraph();
virtual void unCommentSelection();
virtual void setFontSettings(const TextEditor::FontSettings &);
2009-06-17 19:12:19 +02:00
void setFontSettingsIfVisible(const TextEditor::FontSettings &);
2008-12-02 12:01:29 +01:00
virtual void setTabSettings(const TextEditor::TabSettings &);
virtual void setDisplaySettings(const TextEditor::DisplaySettings &);
virtual void setBehaviorSettings(const TextEditor::BehaviorSettings &);
virtual void setTypingSettings(const TextEditor::TypingSettings &);
virtual void setStorageSettings(const TextEditor::StorageSettings &);
virtual void setCompletionSettings(const TextEditor::CompletionSettings &);
virtual void setExtraEncodingSettings(const TextEditor::ExtraEncodingSettings &);
2008-12-02 12:01:29 +01:00
protected:
bool viewportEvent(QEvent *event);
void resizeEvent(QResizeEvent *);
void paintEvent(QPaintEvent *);
void timerEvent(QTimerEvent *);
void mouseMoveEvent(QMouseEvent *);
void mousePressEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
void mouseDoubleClickEvent(QMouseEvent *);
void leaveEvent(QEvent *);
void keyReleaseEvent(QKeyEvent *);
2008-12-02 12:01:29 +01:00
void dragEnterEvent(QDragEnterEvent *e);
void showDefaultContextMenu(QContextMenuEvent *e, const Core::Id menuContextId);
public:
void indentInsertedText(const QTextCursor &tc);
void indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar);
void reindent(QTextDocument *doc, const QTextCursor &cursor);
2008-12-02 12:01:29 +01:00
struct Link
{
Link(const QString &fileName = QString(), int line = 0, int column = 0)
: linkTextStart(-1)
, linkTextEnd(-1)
, targetFileName(fileName)
, targetLine(line)
, targetColumn(column)
{}
bool hasValidTarget() const
{ return !targetFileName.isEmpty(); }
bool hasValidLinkText() const
{ return linkTextStart != linkTextEnd; }
bool operator==(const Link &other) const
{ return linkTextStart == other.linkTextStart && linkTextEnd == other.linkTextEnd; }
int linkTextStart;
int linkTextEnd;
QString targetFileName;
int targetLine;
int targetColumn;
};
protected:
/*!
Reimplement this function to enable code navigation.
\a resolveTarget is set to true when the target of the link is relevant
(it isn't until the link is used).
*/
virtual Link findLinkAt(const QTextCursor &, bool resolveTarget = true);
/*!
Reimplement this function if you want to customize the way a link is
2010-01-11 10:22:55 +01:00
opened. Returns whether the link was opened successfully.
*/
virtual bool openLink(const Link &link, bool inNextSplit = false);
void maybeClearSomeExtraSelections(const QTextCursor &cursor);
/*!
Reimplement this function to change the default replacement text.
*/
virtual QString foldReplacementText(const QTextBlock &block) const;
2008-12-02 12:01:29 +01:00
protected slots:
virtual void slotUpdateExtraArea();
2008-12-02 12:01:29 +01:00
virtual void slotUpdateExtraAreaWidth();
virtual void slotModificationChanged(bool);
virtual void slotUpdateRequest(const QRect &r, int dy);
virtual void slotCursorPositionChanged();
virtual void slotUpdateBlockNotify(const QTextBlock &);
virtual void slotCodeStyleSettingsChanged(const QVariant &);
2008-12-02 12:01:29 +01:00
signals:
void requestFontZoom(int zoom);
void requestZoomReset();
2008-12-02 12:01:29 +01:00
void requestBlockUpdate(const QTextBlock &);
private:
void indentOrUnindent(bool doIndent);
void handleHomeKey(bool anchor);
void handleBackspaceKey();
void moveLineUpDown(bool up);
void copyLineUpDown(bool up);
void saveCurrentCursorPositionForNavigation();
void updateHighlights();
void updateCurrentLineHighlight();
2008-12-02 12:01:29 +01:00
void drawFoldingMarker(QPainter *painter, const QPalette &pal,
const QRect &rect,
bool expanded,
bool active,
bool hovered) const;
void drawCollapsedBlockPopup(QPainter &painter,
const QTextBlock &block,
QPointF offset,
const QRect &clip);
2008-12-02 12:01:29 +01:00
void toggleBlockVisible(const QTextBlock &block);
QRect foldBox();
2008-12-02 12:01:29 +01:00
QTextBlock foldedBlockAt(const QPoint &pos, QRect *box = 0) const;
2008-12-02 12:01:29 +01:00
void updateLink(QMouseEvent *e);
void showLink(const Link &);
void clearLink();
2009-06-17 19:12:19 +02:00
void universalHelper(); // test function for development
bool cursorMoveKeyEvent(QKeyEvent *e);
bool camelCaseRight(QTextCursor &cursor, QTextCursor::MoveMode mode);
bool camelCaseLeft(QTextCursor &cursor, QTextCursor::MoveMode mode);
void processTooltipRequest(const QTextCursor &c);
void transformSelection(Internal::TransformationMethod method);
void transformBlockSelection(Internal::TransformationMethod method);
2008-12-02 12:01:29 +01:00
private slots:
void handleBlockSelection(int diff_row, int diff_col);
2010-07-19 14:06:00 +02:00
// parentheses matcher
2008-12-02 12:01:29 +01:00
void _q_matchParentheses();
void _q_highlightBlocks();
2008-12-02 12:01:29 +01:00
void slotSelectionChanged();
void _q_animateUpdate(int position, QPointF lastPos, QRectF rect);
void doFoo();
2008-12-02 12:01:29 +01:00
};
class TEXTEDITOR_EXPORT BaseTextEditor : public ITextEditor
2008-12-02 12:01:29 +01:00
{
Q_OBJECT
2008-12-02 12:01:29 +01:00
public:
BaseTextEditor(BaseTextEditorWidget *editorWidget);
~BaseTextEditor();
2008-12-02 12:01:29 +01:00
friend class BaseTextEditorWidget;
BaseTextEditorWidget *editorWidget() const { return e; }
2008-12-02 12:01:29 +01:00
// EditorInterface
//QWidget *widget() { return e; }
Core::IDocument * document() { return e->editorDocument(); }
bool createNew(const QString &contents) { return e->createNew(contents); }
bool open(QString *errorString, const QString &fileName, const QString &realFileName) { return e->open(errorString, fileName, realFileName); }
QString displayName() const { return e->displayName(); }
void setDisplayName(const QString &title) { e->setDisplayName(title); emit changed(); }
2008-12-02 12:01:29 +01:00
QByteArray saveState() const { return e->saveState(); }
bool restoreState(const QByteArray &state) { return e->restoreState(state); }
QWidget *toolBar();
2008-12-02 12:01:29 +01:00
enum Side { Left, Right };
void insertExtraToolBarWidget(Side side, QWidget *widget);
2008-12-02 12:01:29 +01:00
// ITextEditor
int find(const QString &string) const;
int currentLine() const;
int currentColumn() const;
void gotoLine(int line, int column = 0) { e->gotoLine(line, column); }
int columnCount() const;
int rowCount() const;
2008-12-02 12:01:29 +01:00
int position(PositionOperation posOp = Current, int at = -1) const
{ return e->position(posOp, at); }
void convertPosition(int pos, int *line, int *column) const
{ e->convertPosition(pos, line, column); }
2008-12-02 12:01:29 +01:00
QRect cursorRect(int pos = -1) const;
QString contents() const;
QString selectedText() const;
QString textAt(int pos, int length) const;
inline QChar characterAt(int pos) const { return e->characterAt(pos); }
inline ITextMarkable *markableInterface() { return e->markableInterface(); }
QString contextHelpId() const; // from IContext
inline void setTextCodec(QTextCodec *codec, TextCodecReason = TextCodecOtherReason) { e->setTextCodec(codec); }
2008-12-02 12:01:29 +01:00
inline QTextCodec *textCodec() const { return e->textCodec(); }
// ITextEditor
2008-12-02 12:01:29 +01:00
void remove(int length);
void insert(const QString &string);
void replace(int length, const QString &string);
void setCursorPosition(int pos);
2008-12-02 12:01:29 +01:00
void select(int toPos);
const Utils::CommentDefinition* commentDefinition() const;
2008-12-02 12:01:29 +01:00
private slots:
void updateCursorPosition();
private:
BaseTextEditorWidget *e;
2008-12-02 12:01:29 +01:00
QToolBar *m_toolBar;
QWidget *m_stretchWidget;
QAction *m_cursorPositionLabelAction;
Utils::LineColumnLabel *m_cursorPositionLabel;
2008-12-02 12:01:29 +01:00
};
} // namespace TextEditor
Q_DECLARE_METATYPE(TextEditor::BaseTextEditorWidget::Link)
2008-12-02 12:01:29 +01:00
#endif // BASETEXTEDITOR_H