Moving some code together that belongs together

This commit is contained in:
Thorbjørn Lindeijer
2009-03-17 16:16:36 +01:00
parent 4e16705a27
commit 551b9f487e
2 changed files with 197 additions and 201 deletions

View File

@@ -1245,61 +1245,6 @@ int BaseTextEditor::visibleWrapColumn() const
return d->m_visibleWrapColumn; return d->m_visibleWrapColumn;
} }
void BaseTextEditor::setFontSettings(const TextEditor::FontSettings &fs)
{
const QTextCharFormat textFormat = fs.toTextCharFormat(QLatin1String(Constants::C_TEXT));
const QTextCharFormat selectionFormat = fs.toTextCharFormat(QLatin1String(Constants::C_SELECTION));
const QTextCharFormat lineNumberFormat = fs.toTextCharFormat(QLatin1String(Constants::C_LINE_NUMBER));
const QTextCharFormat searchResultFormat = fs.toTextCharFormat(QLatin1String(Constants::C_SEARCH_RESULT));
const QTextCharFormat searchScopeFormat = fs.toTextCharFormat(QLatin1String(Constants::C_SEARCH_SCOPE));
const QTextCharFormat parenthesesFormat = fs.toTextCharFormat(QLatin1String(Constants::C_PARENTHESES));
const QTextCharFormat currentLineFormat = fs.toTextCharFormat(QLatin1String(Constants::C_CURRENT_LINE));
const QTextCharFormat ifdefedOutFormat = fs.toTextCharFormat(QLatin1String(Constants::C_DISABLED_CODE));
QFont font(textFormat.font());
const QColor foreground = textFormat.foreground().color();
const QColor background = textFormat.background().color();
QPalette p = palette();
p.setColor(QPalette::Text, foreground);
p.setColor(QPalette::Foreground, foreground);
p.setColor(QPalette::Base, background);
p.setColor(QPalette::Highlight, (selectionFormat.background().style() != Qt::NoBrush) ?
selectionFormat.background().color() :
QApplication::palette().color(QPalette::Highlight));
p.setColor(QPalette::HighlightedText, selectionFormat.foreground().color());
p.setBrush(QPalette::Inactive, QPalette::Highlight, p.highlight());
p.setBrush(QPalette::Inactive, QPalette::HighlightedText, p.highlightedText());
setPalette(p);
setFont(font);
setTabSettings(d->m_document->tabSettings()); // update tabs, they depend on the font
// Line numbers
QPalette ep = d->m_extraArea->palette();
ep.setColor(QPalette::Dark, lineNumberFormat.foreground().color());
ep.setColor(QPalette::Background, lineNumberFormat.background().style() != Qt::NoBrush ?
lineNumberFormat.background().color() : background);
d->m_extraArea->setPalette(ep);
// Search results
d->m_searchResultFormat.setBackground(searchResultFormat.background());
d->m_searchScopeFormat.setBackground(searchScopeFormat.background());
d->m_currentLineFormat.setBackground(currentLineFormat.background());
// Matching braces
d->m_matchFormat.setForeground(parenthesesFormat.foreground());
d->m_rangeFormat.setBackground(parenthesesFormat.background());
// Disabled code
d->m_ifdefedOutFormat.setForeground(ifdefedOutFormat.foreground());
slotUpdateExtraAreaWidth();
}
void BaseTextEditor::setStorageSettings(const StorageSettings &storageSettings)
{
d->m_document->setStorageSettings(storageSettings);
}
//--------- BaseTextEditorPrivate ----------- //--------- BaseTextEditorPrivate -----------
BaseTextEditorPrivate::BaseTextEditorPrivate() BaseTextEditorPrivate::BaseTextEditorPrivate()
@@ -1698,6 +1643,18 @@ void BaseTextEditorPrivate::removeBlockSelection(const QString &text)
q->setTextCursor(cursor); q->setTextCursor(cursor);
} }
void BaseTextEditorPrivate::moveCursorVisible(bool ensureVisible)
{
QTextCursor cursor = q->textCursor();
if (!cursor.block().isVisible()) {
cursor.setVisualNavigation(true);
cursor.movePosition(QTextCursor::Up);
q->setTextCursor(cursor);
}
if (ensureVisible)
q->ensureCursorVisible();
}
void BaseTextEditor::paintEvent(QPaintEvent *e) void BaseTextEditor::paintEvent(QPaintEvent *e)
{ {
/* /*
@@ -2074,15 +2031,6 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
} }
} }
void BaseTextEditor::slotUpdateExtraAreaWidth()
{
if (isLeftToRight())
setViewportMargins(extraAreaWidth(), 0, 0, 0);
else
setViewportMargins(0, 0, extraAreaWidth(), 0);
}
QWidget *BaseTextEditor::extraArea() const QWidget *BaseTextEditor::extraArea() const
{ {
return d->m_extraArea; return d->m_extraArea;
@@ -2130,65 +2078,12 @@ int BaseTextEditor::extraAreaWidth(int *markWidthPtr) const
return space; return space;
} }
void BaseTextEditor::slotModificationChanged(bool m) void BaseTextEditor::slotUpdateExtraAreaWidth()
{ {
if (m) if (isLeftToRight())
return; setViewportMargins(extraAreaWidth(), 0, 0, 0);
else
QTextDocument *doc = document(); setViewportMargins(0, 0, extraAreaWidth(), 0);
TextEditDocumentLayout *documentLayout = qobject_cast<TextEditDocumentLayout*>(doc->documentLayout());
QTC_ASSERT(documentLayout, return);
int oldLastSaveRevision = documentLayout->lastSaveRevision;
documentLayout->lastSaveRevision = doc->revision();
if (oldLastSaveRevision != documentLayout->lastSaveRevision) {
QTextBlock block = doc->begin();
while (block.isValid()) {
if (block.revision() < 0 || block.revision() != oldLastSaveRevision) {
block.setRevision(-documentLayout->lastSaveRevision - 1);
} else {
block.setRevision(documentLayout->lastSaveRevision);
}
block = block.next();
}
}
d->m_extraArea->update();
}
void BaseTextEditor::slotUpdateBlockNotify(const QTextBlock &block)
{
static bool blockRecursion = false;
if (blockRecursion)
return;
if (block.previous().isValid() && block.userState() != block.previous().userState()) {
/* The syntax highlighting state changes. This opens up for
the possibility that the paragraph has braces that support
code folding. In this case, do the save thing and also
update the previous block, which might contain a collapse
box which now is invalid.*/
blockRecursion = true;
emit requestBlockUpdate(block.previous());
blockRecursion = false;
}
}
void BaseTextEditor::slotUpdateRequest(const QRect &r, int dy)
{
if (dy)
d->m_extraArea->scroll(0, dy);
else if (r.width() > 4) { // wider than cursor width, not just cursor blinking
d->m_extraArea->update(0, r.y(), d->m_extraArea->width(), r.height());
}
if (r.contains(viewport()->rect()))
slotUpdateExtraAreaWidth();
}
void BaseTextEditor::setCollapseIndicatorAlpha(int alpha)
{
d->extraAreaCollapseAlpha = alpha;
d->m_extraArea->update();
} }
void BaseTextEditor::extraAreaPaintEvent(QPaintEvent *e) void BaseTextEditor::extraAreaPaintEvent(QPaintEvent *e)
@@ -2389,6 +2284,85 @@ void BaseTextEditor::extraAreaPaintEvent(QPaintEvent *e)
} }
} }
void BaseTextEditor::slotModificationChanged(bool m)
{
if (m)
return;
QTextDocument *doc = document();
TextEditDocumentLayout *documentLayout = qobject_cast<TextEditDocumentLayout*>(doc->documentLayout());
QTC_ASSERT(documentLayout, return);
int oldLastSaveRevision = documentLayout->lastSaveRevision;
documentLayout->lastSaveRevision = doc->revision();
if (oldLastSaveRevision != documentLayout->lastSaveRevision) {
QTextBlock block = doc->begin();
while (block.isValid()) {
if (block.revision() < 0 || block.revision() != oldLastSaveRevision) {
block.setRevision(-documentLayout->lastSaveRevision - 1);
} else {
block.setRevision(documentLayout->lastSaveRevision);
}
block = block.next();
}
}
d->m_extraArea->update();
}
void BaseTextEditor::slotUpdateRequest(const QRect &r, int dy)
{
if (dy)
d->m_extraArea->scroll(0, dy);
else if (r.width() > 4) { // wider than cursor width, not just cursor blinking
d->m_extraArea->update(0, r.y(), d->m_extraArea->width(), r.height());
}
if (r.contains(viewport()->rect()))
slotUpdateExtraAreaWidth();
}
void BaseTextEditor::slotCursorPositionChanged()
{
QList<QTextEdit::ExtraSelection> extraSelections;
setExtraSelections(ParenthesesMatchingSelection, extraSelections); // clear
if (d->m_parenthesesMatchingEnabled)
d->m_parenthesesMatchingTimer->start(50);
if (d->m_highlightCurrentLine) {
QTextEdit::ExtraSelection sel;
sel.format.setBackground(d->m_currentLineFormat.background());
sel.format.setProperty(QTextFormat::FullWidthSelection, true);
sel.cursor = textCursor();
sel.cursor.clearSelection();
extraSelections.append(sel);
}
setExtraSelections(CurrentLineSelection, extraSelections);
}
void BaseTextEditor::slotUpdateBlockNotify(const QTextBlock &block)
{
static bool blockRecursion = false;
if (blockRecursion)
return;
if (block.previous().isValid() && block.userState() != block.previous().userState()) {
/* The syntax highlighting state changes. This opens up for
the possibility that the paragraph has braces that support
code folding. In this case, do the save thing and also
update the previous block, which might contain a collapse
box which now is invalid.*/
blockRecursion = true;
emit requestBlockUpdate(block.previous());
blockRecursion = false;
}
}
void BaseTextEditor::setCollapseIndicatorAlpha(int alpha)
{
d->extraAreaCollapseAlpha = alpha;
d->m_extraArea->update();
}
void BaseTextEditor::timerEvent(QTimerEvent *e) void BaseTextEditor::timerEvent(QTimerEvent *e)
{ {
if (e->timerId() == d->autoScrollTimer.timerId()) { if (e->timerId() == d->autoScrollTimer.timerId()) {
@@ -2580,25 +2554,6 @@ void BaseTextEditor::extraAreaMouseEvent(QMouseEvent *e)
} }
} }
void BaseTextEditor::slotCursorPositionChanged()
{
QList<QTextEdit::ExtraSelection> extraSelections;
setExtraSelections(ParenthesesMatchingSelection, extraSelections); // clear
if (d->m_parenthesesMatchingEnabled)
d->m_parenthesesMatchingTimer->start(50);
if (d->m_highlightCurrentLine) {
QTextEdit::ExtraSelection sel;
sel.format.setBackground(d->m_currentLineFormat.background());
sel.format.setProperty(QTextFormat::FullWidthSelection, true);
sel.cursor = textCursor();
sel.cursor.clearSelection();
extraSelections.append(sel);
}
setExtraSelections(CurrentLineSelection, extraSelections);
}
QTextBlock TextBlockUserData::testCollapse(const QTextBlock& block) QTextBlock TextBlockUserData::testCollapse(const QTextBlock& block)
{ {
QTextBlock info = block; QTextBlock info = block;
@@ -2842,50 +2797,6 @@ void BaseTextEditor::handleBackspaceKey()
cursor.endEditBlock(); cursor.endEditBlock();
} }
void BaseTextEditor::format()
{
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
indent(document(), cursor, QChar::Null);
cursor.endEditBlock();
}
void BaseTextEditor::unCommentSelection()
{
}
void BaseTextEditor::setTabSettings(const TabSettings &ts)
{
d->m_document->setTabSettings(ts);
int charWidth = QFontMetrics(font()).width(QChar(' '));
setTabStopWidth(charWidth * ts.m_tabSize);
}
void BaseTextEditor::setDisplaySettings(const DisplaySettings &ds)
{
setLineWrapMode(ds.m_textWrapping ? QPlainTextEdit::WidgetWidth : QPlainTextEdit::NoWrap);
setLineNumbersVisible(ds.m_displayLineNumbers);
setVisibleWrapColumn(ds.m_showWrapColumn ? ds.m_wrapColumn : 0);
setCodeFoldingVisible(ds.m_displayFoldingMarkers);
setHighlightCurrentLine(ds.m_highlightCurrentLine);
if (d->m_displaySettings.m_visualizeWhitespace != ds.m_visualizeWhitespace) {
if (QSyntaxHighlighter *highlighter = baseTextDocument()->syntaxHighlighter())
highlighter->rehighlight();
QTextOption option = document()->defaultTextOption();
if (ds.m_visualizeWhitespace)
option.setFlags(option.flags() | QTextOption::ShowTabsAndSpaces);
else
option.setFlags(option.flags() & ~QTextOption::ShowTabsAndSpaces);
option.setFlags(option.flags() | QTextOption::AddSpaceForLineAndParagraphSeparators);
document()->setDefaultTextOption(option);
}
d->m_displaySettings = ds;
}
void BaseTextEditor::wheelEvent(QWheelEvent *e) void BaseTextEditor::wheelEvent(QWheelEvent *e)
{ {
d->clearVisibleCollapsedBlock(); d->clearVisibleCollapsedBlock();
@@ -2962,7 +2873,8 @@ void BaseTextEditorPrivate::updateMarksLineNumber()
} }
} }
void BaseTextEditor::markBlocksAsChanged(QList<int> blockNumbers) { void BaseTextEditor::markBlocksAsChanged(QList<int> blockNumbers)
{
QTextBlock block = document()->begin(); QTextBlock block = document()->begin();
while (block.isValid()) { while (block.isValid()) {
if (block.revision() < 0) if (block.revision() < 0)
@@ -3403,17 +3315,101 @@ void BaseTextEditor::setIfdefedOutBlocks(const QList<BaseTextEditor::BlockRange>
documentLayout->requestUpdate(); documentLayout->requestUpdate();
} }
void BaseTextEditor::format()
void BaseTextEditorPrivate::moveCursorVisible(bool ensureVisible)
{ {
QTextCursor cursor = q->textCursor(); QTextCursor cursor = textCursor();
if (!cursor.block().isVisible()) { cursor.beginEditBlock();
cursor.setVisualNavigation(true); indent(document(), cursor, QChar::Null);
cursor.movePosition(QTextCursor::Up); cursor.endEditBlock();
q->setTextCursor(cursor);
} }
if (ensureVisible)
q->ensureCursorVisible(); void BaseTextEditor::unCommentSelection()
{
}
void BaseTextEditor::setFontSettings(const TextEditor::FontSettings &fs)
{
const QTextCharFormat textFormat = fs.toTextCharFormat(QLatin1String(Constants::C_TEXT));
const QTextCharFormat selectionFormat = fs.toTextCharFormat(QLatin1String(Constants::C_SELECTION));
const QTextCharFormat lineNumberFormat = fs.toTextCharFormat(QLatin1String(Constants::C_LINE_NUMBER));
const QTextCharFormat searchResultFormat = fs.toTextCharFormat(QLatin1String(Constants::C_SEARCH_RESULT));
const QTextCharFormat searchScopeFormat = fs.toTextCharFormat(QLatin1String(Constants::C_SEARCH_SCOPE));
const QTextCharFormat parenthesesFormat = fs.toTextCharFormat(QLatin1String(Constants::C_PARENTHESES));
const QTextCharFormat currentLineFormat = fs.toTextCharFormat(QLatin1String(Constants::C_CURRENT_LINE));
const QTextCharFormat ifdefedOutFormat = fs.toTextCharFormat(QLatin1String(Constants::C_DISABLED_CODE));
QFont font(textFormat.font());
const QColor foreground = textFormat.foreground().color();
const QColor background = textFormat.background().color();
QPalette p = palette();
p.setColor(QPalette::Text, foreground);
p.setColor(QPalette::Foreground, foreground);
p.setColor(QPalette::Base, background);
p.setColor(QPalette::Highlight, (selectionFormat.background().style() != Qt::NoBrush) ?
selectionFormat.background().color() :
QApplication::palette().color(QPalette::Highlight));
p.setColor(QPalette::HighlightedText, selectionFormat.foreground().color());
p.setBrush(QPalette::Inactive, QPalette::Highlight, p.highlight());
p.setBrush(QPalette::Inactive, QPalette::HighlightedText, p.highlightedText());
setPalette(p);
setFont(font);
setTabSettings(d->m_document->tabSettings()); // update tabs, they depend on the font
// Line numbers
QPalette ep = d->m_extraArea->palette();
ep.setColor(QPalette::Dark, lineNumberFormat.foreground().color());
ep.setColor(QPalette::Background, lineNumberFormat.background().style() != Qt::NoBrush ?
lineNumberFormat.background().color() : background);
d->m_extraArea->setPalette(ep);
// Search results
d->m_searchResultFormat.setBackground(searchResultFormat.background());
d->m_searchScopeFormat.setBackground(searchScopeFormat.background());
d->m_currentLineFormat.setBackground(currentLineFormat.background());
// Matching braces
d->m_matchFormat.setForeground(parenthesesFormat.foreground());
d->m_rangeFormat.setBackground(parenthesesFormat.background());
// Disabled code
d->m_ifdefedOutFormat.setForeground(ifdefedOutFormat.foreground());
slotUpdateExtraAreaWidth();
}
void BaseTextEditor::setTabSettings(const TabSettings &ts)
{
d->m_document->setTabSettings(ts);
int charWidth = QFontMetrics(font()).width(QChar(' '));
setTabStopWidth(charWidth * ts.m_tabSize);
}
void BaseTextEditor::setDisplaySettings(const DisplaySettings &ds)
{
setLineWrapMode(ds.m_textWrapping ? QPlainTextEdit::WidgetWidth : QPlainTextEdit::NoWrap);
setLineNumbersVisible(ds.m_displayLineNumbers);
setVisibleWrapColumn(ds.m_showWrapColumn ? ds.m_wrapColumn : 0);
setCodeFoldingVisible(ds.m_displayFoldingMarkers);
setHighlightCurrentLine(ds.m_highlightCurrentLine);
if (d->m_displaySettings.m_visualizeWhitespace != ds.m_visualizeWhitespace) {
if (QSyntaxHighlighter *highlighter = baseTextDocument()->syntaxHighlighter())
highlighter->rehighlight();
QTextOption option = document()->defaultTextOption();
if (ds.m_visualizeWhitespace)
option.setFlags(option.flags() | QTextOption::ShowTabsAndSpaces);
else
option.setFlags(option.flags() & ~QTextOption::ShowTabsAndSpaces);
option.setFlags(option.flags() | QTextOption::AddSpaceForLineAndParagraphSeparators);
document()->setDefaultTextOption(option);
}
d->m_displaySettings = ds;
}
void BaseTextEditor::setStorageSettings(const StorageSettings &storageSettings)
{
d->m_document->setStorageSettings(storageSettings);
} }
void BaseTextEditor::collapse() void BaseTextEditor::collapse()

View File

@@ -306,10 +306,6 @@ public:
public slots: public slots:
void setDisplayName(const QString &title); void setDisplayName(const QString &title);
virtual void setFontSettings(const TextEditor::FontSettings &);
virtual void format();
virtual void unCommentSelection();
virtual void setStorageSettings(const TextEditor::StorageSettings &);
void paste(); void paste();
void cut(); void cut();
@@ -355,6 +351,7 @@ protected:
public: public:
void duplicateFrom(BaseTextEditor *editor); void duplicateFrom(BaseTextEditor *editor);
protected: protected:
BaseTextDocument *baseTextDocument() const; BaseTextDocument *baseTextDocument() const;
void setBaseTextDocument(BaseTextDocument *doc); void setBaseTextDocument(BaseTextDocument *doc);
@@ -380,8 +377,8 @@ public:
QWidget *extraArea() const; QWidget *extraArea() const;
virtual int extraAreaWidth(int *markWidthPtr = 0) const; virtual int extraAreaWidth(int *markWidthPtr = 0) const;
virtual void extraAreaPaintEvent(QPaintEvent *); virtual void extraAreaPaintEvent(QPaintEvent *);
virtual void extraAreaMouseEvent(QMouseEvent *);
virtual void extraAreaLeaveEvent(QEvent *); virtual void extraAreaLeaveEvent(QEvent *);
virtual void extraAreaMouseEvent(QMouseEvent *);
const TabSettings &tabSettings() const; const TabSettings &tabSettings() const;
const DisplaySettings &displaySettings() const; const DisplaySettings &displaySettings() const;
@@ -418,8 +415,12 @@ public:
void setIfdefedOutBlocks(const QList<BlockRange> &blocks); void setIfdefedOutBlocks(const QList<BlockRange> &blocks);
public slots: public slots:
virtual void format();
virtual void unCommentSelection();
virtual void setFontSettings(const TextEditor::FontSettings &);
virtual void setTabSettings(const TextEditor::TabSettings &); virtual void setTabSettings(const TextEditor::TabSettings &);
virtual void setDisplaySettings(const TextEditor::DisplaySettings &); virtual void setDisplaySettings(const TextEditor::DisplaySettings &);
virtual void setStorageSettings(const TextEditor::StorageSettings &);
protected: protected:
bool viewportEvent(QEvent *event); bool viewportEvent(QEvent *event);
@@ -437,7 +438,6 @@ protected:
// Indent at cursor. Calls indentBlock for selection or current line. // Indent at cursor. Calls indentBlock for selection or current line.
virtual void indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar); virtual void indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar);
protected slots: protected slots:
virtual void slotUpdateExtraAreaWidth(); virtual void slotUpdateExtraAreaWidth();
virtual void slotModificationChanged(bool); virtual void slotModificationChanged(bool);