diff options
author | jkobus <jaroslaw.kobus@digia.com> | 2013-08-13 12:57:31 +0200 |
---|---|---|
committer | Jarek Kobus <jaroslaw.kobus@digia.com> | 2013-08-26 13:39:40 +0200 |
commit | e8801167aa7a0047c9c9be0942ed0b368e5b5aa4 (patch) | |
tree | eb1dcf7998b0457518681126ddf9b49f198dd2d4 /src/plugins/vcsbase | |
parent | 760aa0f8bce34e094abecdd99c77c359fb96bb67 (diff) | |
download | qt-creator-e8801167aa7a0047c9c9be0942ed0b368e5b5aa4.tar.gz |
Add common interface for text formats inside syntax highlighter
Change-Id: I87f64446161a57aea0896f68e4eafacef791969b
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
Diffstat (limited to 'src/plugins/vcsbase')
-rw-r--r-- | src/plugins/vcsbase/baseannotationhighlighter.cpp | 50 | ||||
-rw-r--r-- | src/plugins/vcsbase/baseannotationhighlighter.h | 9 | ||||
-rw-r--r-- | src/plugins/vcsbase/diffhighlighter.cpp | 86 | ||||
-rw-r--r-- | src/plugins/vcsbase/diffhighlighter.h | 9 | ||||
-rw-r--r-- | src/plugins/vcsbase/vcsbaseeditor.cpp | 32 | ||||
-rw-r--r-- | src/plugins/vcsbase/vcsbaseeditor.h | 6 |
6 files changed, 103 insertions, 89 deletions
diff --git a/src/plugins/vcsbase/baseannotationhighlighter.cpp b/src/plugins/vcsbase/baseannotationhighlighter.cpp index 3a04647103..841b7ed479 100644 --- a/src/plugins/vcsbase/baseannotationhighlighter.cpp +++ b/src/plugins/vcsbase/baseannotationhighlighter.cpp @@ -28,6 +28,7 @@ ****************************************************************************/ #include "baseannotationhighlighter.h" +#include <texteditor/fontsettings.h> #include <QDebug> #include <QColor> @@ -51,34 +52,63 @@ typedef QMap<QString, QTextCharFormat> ChangeNumberFormatMap; */ namespace VcsBase { -namespace Internal { class BaseAnnotationHighlighterPrivate { + BaseAnnotationHighlighter *q_ptr; + Q_DECLARE_PUBLIC(BaseAnnotationHighlighter) public: + enum Formats { + BackgroundFormat // C_TEXT + }; + + BaseAnnotationHighlighterPrivate(); + + void updateOtherFormats(); + ChangeNumberFormatMap m_changeNumberMap; QColor m_background; }; -} // namespace Internal +BaseAnnotationHighlighterPrivate::BaseAnnotationHighlighterPrivate() + : q_ptr(0) +{ +} + +void BaseAnnotationHighlighterPrivate::updateOtherFormats() +{ + Q_Q(BaseAnnotationHighlighter); + m_background = q->formatForCategory(BackgroundFormat).brushProperty(QTextFormat::BackgroundBrush).color(); + q->setChangeNumbers(m_changeNumberMap.keys().toSet()); +} + BaseAnnotationHighlighter::BaseAnnotationHighlighter(const ChangeNumbers &changeNumbers, - const QColor &bg, QTextDocument *document) : TextEditor::SyntaxHighlighter(document), - d(new Internal::BaseAnnotationHighlighterPrivate) + d_ptr(new BaseAnnotationHighlighterPrivate()) { - d->m_background = bg; + d_ptr->q_ptr = this; + + Q_D(BaseAnnotationHighlighter); + + static QVector<TextEditor::TextStyle> categories; + if (categories.isEmpty()) + categories << TextEditor::C_TEXT; + + setTextFormatCategories(categories); + d->updateOtherFormats(); + setChangeNumbers(changeNumbers); } BaseAnnotationHighlighter::~BaseAnnotationHighlighter() { - delete d; } void BaseAnnotationHighlighter::setChangeNumbers(const ChangeNumbers &changeNumbers) { + Q_D(BaseAnnotationHighlighter); d->m_changeNumberMap.clear(); if (!changeNumbers.isEmpty()) { // Assign a color gradient to annotation change numbers. Give @@ -99,6 +129,7 @@ void BaseAnnotationHighlighter::setChangeNumbers(const ChangeNumbers &changeNumb void BaseAnnotationHighlighter::highlightBlock(const QString &text) { + Q_D(BaseAnnotationHighlighter); if (text.isEmpty() || d->m_changeNumberMap.empty()) return; const QString change = changeNumber(text); @@ -107,10 +138,11 @@ void BaseAnnotationHighlighter::highlightBlock(const QString &text) setFormat(0, text.length(), it.value()); } -void BaseAnnotationHighlighter::setBackgroundColor(const QColor &color) +void BaseAnnotationHighlighter::setFontSettings(const TextEditor::FontSettings &fontSettings) { - d->m_background = color; - setChangeNumbers(d->m_changeNumberMap.keys().toSet()); + Q_D(BaseAnnotationHighlighter); + SyntaxHighlighter::setFontSettings(fontSettings); + d->updateOtherFormats(); } } // namespace VcsBase diff --git a/src/plugins/vcsbase/baseannotationhighlighter.h b/src/plugins/vcsbase/baseannotationhighlighter.h index e1845477e4..ff5fdfb1a4 100644 --- a/src/plugins/vcsbase/baseannotationhighlighter.h +++ b/src/plugins/vcsbase/baseannotationhighlighter.h @@ -35,17 +35,16 @@ #include <texteditor/syntaxhighlighter.h> namespace VcsBase { -namespace Internal { class BaseAnnotationHighlighterPrivate; -} // namespace Internal class VCSBASE_EXPORT BaseAnnotationHighlighter : public TextEditor::SyntaxHighlighter { Q_OBJECT + Q_DECLARE_PRIVATE(BaseAnnotationHighlighter) public: typedef QSet<QString> ChangeNumbers; - explicit BaseAnnotationHighlighter(const ChangeNumbers &changeNumbers, const QColor &bg, + explicit BaseAnnotationHighlighter(const ChangeNumbers &changeNumbers, QTextDocument *document = 0); virtual ~BaseAnnotationHighlighter(); @@ -53,13 +52,13 @@ public: virtual void highlightBlock(const QString &text); - void setBackgroundColor(const QColor &color); + virtual void setFontSettings(const TextEditor::FontSettings &fontSettings); private: // Implement this to return the change number of a line virtual QString changeNumber(const QString &block) const = 0; - Internal::BaseAnnotationHighlighterPrivate *const d; + QScopedPointer<BaseAnnotationHighlighterPrivate> d_ptr; }; } // namespace VcsBase diff --git a/src/plugins/vcsbase/diffhighlighter.cpp b/src/plugins/vcsbase/diffhighlighter.cpp index 9a26fa5f58..5798c30a73 100644 --- a/src/plugins/vcsbase/diffhighlighter.cpp +++ b/src/plugins/vcsbase/diffhighlighter.cpp @@ -72,8 +72,7 @@ enum DiffFormats { DiffInFormat, DiffOutFormat, DiffFileFormat, - DiffLocationFormat, - NumDiffFormats + DiffLocationFormat }; enum FoldingState { @@ -83,61 +82,92 @@ enum FoldingState { Location }; +}; // namespace Internal; + +static inline QTextCharFormat invertedColorFormat(const QTextCharFormat &in) +{ + QTextCharFormat rc = in; + rc.setForeground(in.background()); + rc.setBackground(in.foreground()); + return rc; +} + // --- DiffHighlighterPrivate class DiffHighlighterPrivate { + DiffHighlighter *q_ptr; + Q_DECLARE_PUBLIC(DiffHighlighter) public: DiffHighlighterPrivate(const QRegExp &filePattern); - DiffFormats analyzeLine(const QString &block) const; + Internal::DiffFormats analyzeLine(const QString &block) const; + void updateOtherFormats(); mutable QRegExp m_filePattern; const QString m_locationIndicator; const QChar m_diffInIndicator; const QChar m_diffOutIndicator; - QTextCharFormat m_formats[NumDiffFormats]; QTextCharFormat m_addedTrailingWhiteSpaceFormat; - FoldingState m_foldingState; + Internal::FoldingState m_foldingState; }; DiffHighlighterPrivate::DiffHighlighterPrivate(const QRegExp &filePattern) : + q_ptr(0), m_filePattern(filePattern), m_locationIndicator(QLatin1String("@@")), m_diffInIndicator(QLatin1Char('+')), m_diffOutIndicator(QLatin1Char('-')), - m_foldingState(StartOfFile) + m_foldingState(Internal::StartOfFile) { QTC_CHECK(filePattern.isValid()); } -DiffFormats DiffHighlighterPrivate::analyzeLine(const QString &text) const +Internal::DiffFormats DiffHighlighterPrivate::analyzeLine(const QString &text) const { // Do not match on git "--- a/" as a deleted line, check // file first if (m_filePattern.indexIn(text) == 0) - return DiffFileFormat; + return Internal::DiffFileFormat; if (text.startsWith(m_diffInIndicator)) - return DiffInFormat; + return Internal::DiffInFormat; if (text.startsWith(m_diffOutIndicator)) - return DiffOutFormat; + return Internal::DiffOutFormat; if (text.startsWith(m_locationIndicator)) - return DiffLocationFormat; - return DiffTextFormat; + return Internal::DiffLocationFormat; + return Internal::DiffTextFormat; } -} // namespace Internal +void DiffHighlighterPrivate::updateOtherFormats() +{ + Q_Q(DiffHighlighter); + m_addedTrailingWhiteSpaceFormat = + invertedColorFormat(q->formatForCategory(Internal::DiffInFormat)); + +} // --- DiffHighlighter DiffHighlighter::DiffHighlighter(const QRegExp &filePattern) : TextEditor::SyntaxHighlighter(static_cast<QTextDocument *>(0)), - d(new Internal::DiffHighlighterPrivate(filePattern)) + d_ptr(new DiffHighlighterPrivate(filePattern)) { + d_ptr->q_ptr = this; + Q_D(DiffHighlighter); + + static QVector<TextEditor::TextStyle> categories; + if (categories.isEmpty()) { + categories << TextEditor::C_TEXT + << TextEditor::C_ADDED_LINE + << TextEditor::C_REMOVED_LINE + << TextEditor::C_DIFF_FILE + << TextEditor::C_DIFF_LOCATION; + } + setTextFormatCategories(categories); + d->updateOtherFormats(); } DiffHighlighter::~DiffHighlighter() { - delete d; } // Check trailing spaces @@ -157,6 +187,7 @@ static inline int trimmedLength(const QString &in) */ void DiffHighlighter::highlightBlock(const QString &text) { + Q_D(DiffHighlighter); if (text.isEmpty()) return; @@ -168,13 +199,13 @@ void DiffHighlighter::highlightBlock(const QString &text) case Internal::DiffInFormat: { // Mark trailing whitespace. const int trimmedLen = trimmedLength(text); - setFormat(0, trimmedLen, d->m_formats[format]); + setFormat(0, trimmedLen, formatForCategory(format)); if (trimmedLen != length) setFormat(trimmedLen, length - trimmedLen, d->m_addedTrailingWhiteSpaceFormat); } break; default: - setFormat(0, length, d->m_formats[format]); + setFormat(0, length, formatForCategory(format)); break; } @@ -234,24 +265,11 @@ void DiffHighlighter::highlightBlock(const QString &text) } } -static inline QTextCharFormat invertedColorFormat(const QTextCharFormat &in) +void DiffHighlighter::setFontSettings(const TextEditor::FontSettings &fontSettings) { - QTextCharFormat rc = in; - rc.setForeground(in.background()); - rc.setBackground(in.foreground()); - return rc; -} - -void DiffHighlighter::setFormats(const QVector<QTextCharFormat> &s) -{ - if (s.size() == Internal::NumDiffFormats) { - qCopy(s.constBegin(), s.constEnd(), d->m_formats); - // Display trailing blanks with colors swapped - d->m_addedTrailingWhiteSpaceFormat = - invertedColorFormat(d->m_formats[Internal::DiffInFormat]); - } else { - qWarning("%s: insufficient setting size: %d", Q_FUNC_INFO, s.size()); - } + Q_D(DiffHighlighter); + SyntaxHighlighter::setFontSettings(fontSettings); + d->updateOtherFormats(); } } // namespace VcsBase diff --git a/src/plugins/vcsbase/diffhighlighter.h b/src/plugins/vcsbase/diffhighlighter.h index be55af5fc2..6ce2692e18 100644 --- a/src/plugins/vcsbase/diffhighlighter.h +++ b/src/plugins/vcsbase/diffhighlighter.h @@ -44,23 +44,22 @@ namespace TextEditor { class FontSettingsPage; } namespace VcsBase { -namespace Internal { class DiffHighlighterPrivate; } +class DiffHighlighterPrivate; class VCSBASE_EXPORT DiffHighlighter : public TextEditor::SyntaxHighlighter { Q_OBJECT - + Q_DECLARE_PRIVATE(DiffHighlighter) public: explicit DiffHighlighter(const QRegExp &filePattern); ~DiffHighlighter(); void highlightBlock(const QString &text); - // Set formats from a sequence of type QTextCharFormat - void setFormats(const QVector<QTextCharFormat> &s); + virtual void setFontSettings(const TextEditor::FontSettings &fontSettings); private: - Internal::DiffHighlighterPrivate *const d; + QScopedPointer<DiffHighlighterPrivate> d_ptr; }; } // namespace VcsBase diff --git a/src/plugins/vcsbase/vcsbaseeditor.cpp b/src/plugins/vcsbase/vcsbaseeditor.cpp index 1b90d8a284..b2f24db908 100644 --- a/src/plugins/vcsbase/vcsbaseeditor.cpp +++ b/src/plugins/vcsbase/vcsbaseeditor.cpp @@ -41,7 +41,6 @@ #include <projectexplorer/session.h> #include <texteditor/basetextdocument.h> #include <texteditor/basetextdocumentlayout.h> -#include <texteditor/fontsettings.h> #include <texteditor/texteditorsettings.h> #include <utils/qtcassert.h> @@ -571,8 +570,6 @@ public: bool m_mouseDragging; QList<AbstractTextCursorHandler *> m_textCursorHandlers; - QColor m_backgroundColor; - private: QComboBox *m_entriesComboBox; }; @@ -1048,7 +1045,7 @@ void VcsBaseEditorWidget::slotActivateAnnotation() ah->setChangeNumbers(changes); ah->rehighlight(); } else { - baseTextDocument()->setSyntaxHighlighter(createAnnotationHighlighter(changes, d->m_backgroundColor)); + baseTextDocument()->setSyntaxHighlighter(createAnnotationHighlighter(changes)); } } @@ -1186,33 +1183,6 @@ void VcsBaseEditorWidget::reportCommandFinished(bool ok, int exitCode, const QVa setPlainText(tr("Failed to retrieve data.")); } -void VcsBaseEditorWidget::setFontSettings(const TextEditor::FontSettings &fs) -{ - TextEditor::BaseTextEditorWidget::setFontSettings(fs); - d->m_backgroundColor = fs.toTextCharFormat(TextEditor::C_TEXT) - .brushProperty(QTextFormat::BackgroundBrush).color(); - - if (d->m_parameters->type == AnnotateOutput) { - if (BaseAnnotationHighlighter *highlighter = qobject_cast<BaseAnnotationHighlighter *>(baseTextDocument()->syntaxHighlighter())) { - highlighter->setBackgroundColor(d->m_backgroundColor); - highlighter->rehighlight(); - } - } else if (hasDiff()) { - if (DiffHighlighter *highlighter = qobject_cast<DiffHighlighter*>(baseTextDocument()->syntaxHighlighter())) { - static QVector<TextEditor::TextStyle> categories; - if (categories.isEmpty()) { - categories << TextEditor::C_TEXT - << TextEditor::C_ADDED_LINE - << TextEditor::C_REMOVED_LINE - << TextEditor::C_DIFF_FILE - << TextEditor::C_DIFF_LOCATION; - } - highlighter->setFormats(fs.toTextCharFormats(categories)); - highlighter->rehighlight(); - } - } -} - const VcsBaseEditorParameters *VcsBaseEditorWidget::findType(const VcsBaseEditorParameters *array, int arraySize, EditorContentType et) diff --git a/src/plugins/vcsbase/vcsbaseeditor.h b/src/plugins/vcsbase/vcsbaseeditor.h index 4d0ee1c901..b438b93bbe 100644 --- a/src/plugins/vcsbase/vcsbaseeditor.h +++ b/src/plugins/vcsbase/vcsbaseeditor.h @@ -217,9 +217,6 @@ protected: void mouseDoubleClickEvent(QMouseEvent *e); void keyPressEvent(QKeyEvent *); -public slots: - void setFontSettings(const TextEditor::FontSettings &); - private slots: void slotActivateAnnotation(); void slotPopulateDiffBrowser(); @@ -244,8 +241,7 @@ protected: // Implement to identify a change number at the cursor position virtual QString changeUnderCursor(const QTextCursor &) const = 0; // Factory functions for highlighters - virtual BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes, - const QColor &bg) const = 0; + virtual BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const = 0; // Returns a local file name from the diff file specification // (text cursor at position above change hunk) QString fileNameFromDiffSpecification(const QTextBlock &inBlock) const; |