From 2ddb726dd537a1505b8d0208fe638cc759a68c38 Mon Sep 17 00:00:00 2001 From: jkobus Date: Thu, 23 May 2013 13:36:27 +0200 Subject: Add file list combobox to diff editor Change-Id: I2a40207ed3c4a5c07ba544d681aed6649a0b1a11 Reviewed-by: Friedemann Kleint Reviewed-by: Tobias Hunger --- src/plugins/diffeditor/diffeditorwidget.cpp | 76 ++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 8 deletions(-) (limited to 'src/plugins/diffeditor/diffeditorwidget.cpp') diff --git a/src/plugins/diffeditor/diffeditorwidget.cpp b/src/plugins/diffeditor/diffeditorwidget.cpp index f68381adaf..7d9bba2001 100644 --- a/src/plugins/diffeditor/diffeditorwidget.cpp +++ b/src/plugins/diffeditor/diffeditorwidget.cpp @@ -131,6 +131,8 @@ public: void setSkippedLines(int blockNumber, int skippedLines) { m_skippedLines[blockNumber] = skippedLines; setSeparator(blockNumber, true); } void setSeparator(int blockNumber, bool separator) { m_separators[blockNumber] = separator; } bool isFileLine(int blockNumber) const { return m_fileInfo.contains(blockNumber); } + int blockNumberForFileIndex(int fileIndex) const; + int fileIndexForBlockNumber(int blockNumber) const; bool isChunkLine(int blockNumber) const { return m_skippedLines.contains(blockNumber); } void clearAll(); void clearAll(const QString &message); @@ -243,6 +245,36 @@ void DiffViewEditorWidget::setLineNumber(int blockNumber, int lineNumber) m_lineNumberDigits = qMax(m_lineNumberDigits, lineNumberString.count()); } +int DiffViewEditorWidget::blockNumberForFileIndex(int fileIndex) const +{ + if (fileIndex < 0 || fileIndex >= m_fileInfo.count()) + return -1; + + QMap::const_iterator it + = m_fileInfo.constBegin(); + for (int i = 0; i < fileIndex; i++) + ++it; + + return it.key(); +} + +int DiffViewEditorWidget::fileIndexForBlockNumber(int blockNumber) const +{ + QMap::const_iterator it + = m_fileInfo.constBegin(); + QMap::const_iterator itEnd + = m_fileInfo.constEnd(); + + int i = -1; + while (it != itEnd) { + if (it.key() > blockNumber) + break; + ++it; + ++i; + } + return i; +} + void DiffViewEditorWidget::clearAll() { clearAll(tr("No difference")); @@ -512,16 +544,14 @@ DiffEditorWidget::DiffEditorWidget(QWidget *parent) this, SLOT(leftVSliderChanged())); connect(m_leftEditor->verticalScrollBar(), SIGNAL(actionTriggered(int)), this, SLOT(leftVSliderChanged())); - connect(m_leftEditor, SIGNAL(cursorPositionChanged()), - this, SLOT(leftVSliderChanged())); connect(m_leftEditor->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(leftHSliderChanged())); connect(m_leftEditor->horizontalScrollBar(), SIGNAL(actionTriggered(int)), this, SLOT(leftHSliderChanged())); - connect(m_leftEditor, SIGNAL(cursorPositionChanged()), - this, SLOT(leftHSliderChanged())); + connect(m_leftEditor, SIGNAL(cursorPositionChanged()), + this, SLOT(leftCursorPositionChanged())); connect(m_leftEditor->document()->documentLayout(), SIGNAL(documentSizeChanged(QSizeF)), this, SLOT(leftDocumentSizeChanged())); @@ -529,16 +559,14 @@ DiffEditorWidget::DiffEditorWidget(QWidget *parent) this, SLOT(rightVSliderChanged())); connect(m_rightEditor->verticalScrollBar(), SIGNAL(actionTriggered(int)), this, SLOT(rightVSliderChanged())); - connect(m_rightEditor, SIGNAL(cursorPositionChanged()), - this, SLOT(rightVSliderChanged())); connect(m_rightEditor->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(rightHSliderChanged())); connect(m_rightEditor->horizontalScrollBar(), SIGNAL(actionTriggered(int)), this, SLOT(rightHSliderChanged())); - connect(m_rightEditor, SIGNAL(cursorPositionChanged()), - this, SLOT(rightHSliderChanged())); + connect(m_rightEditor, SIGNAL(cursorPositionChanged()), + this, SLOT(rightCursorPositionChanged())); connect(m_rightEditor->document()->documentLayout(), SIGNAL(documentSizeChanged(QSizeF)), this, SLOT(rightDocumentSizeChanged())); @@ -630,6 +658,24 @@ void DiffEditorWidget::setIgnoreWhitespaces(bool ignore) setDiff(m_diffList); } +void DiffEditorWidget::navigateToDiffFile(int diffFileIndex) +{ + const int blockNumber = m_leftEditor->blockNumberForFileIndex(diffFileIndex); + + QTextBlock leftBlock = m_leftEditor->document()->findBlockByNumber(blockNumber); + QTextCursor leftCursor = m_leftEditor->textCursor(); + leftCursor.setPosition(leftBlock.position()); + m_leftEditor->setTextCursor(leftCursor); + + QTextBlock rightBlock = m_rightEditor->document()->findBlockByNumber(blockNumber); + QTextCursor rightCursor = m_rightEditor->textCursor(); + rightCursor.setPosition(rightBlock.position()); + m_rightEditor->setTextCursor(rightCursor); + + m_leftEditor->centerCursor(); + m_rightEditor->centerCursor(); +} + QTextCodec *DiffEditorWidget::codec() const { return const_cast(m_leftEditor->codec()); @@ -1329,6 +1375,20 @@ void DiffEditorWidget::rightHSliderChanged() m_leftEditor->horizontalScrollBar()->setValue(m_rightEditor->horizontalScrollBar()->value()); } +void DiffEditorWidget::leftCursorPositionChanged() +{ + leftVSliderChanged(); + leftHSliderChanged(); + emit navigatedToDiffFile(m_leftEditor->fileIndexForBlockNumber(m_leftEditor->textCursor().blockNumber())); +} + +void DiffEditorWidget::rightCursorPositionChanged() +{ + rightVSliderChanged(); + rightHSliderChanged(); + emit navigatedToDiffFile(m_rightEditor->fileIndexForBlockNumber(m_rightEditor->textCursor().blockNumber())); +} + void DiffEditorWidget::leftDocumentSizeChanged() { synchronizeFoldings(m_leftEditor, m_rightEditor); -- cgit v1.2.1