2013-02-15 12:49:50 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2013-02-15 12:49:50 +01:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** 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
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
#include "sidebysidediffeditorwidget.h"
|
2014-02-13 16:43:28 +01:00
|
|
|
#include "selectabletexteditorwidget.h"
|
2014-02-24 11:10:17 +01:00
|
|
|
#include "diffeditorguicontroller.h"
|
2014-02-28 10:40:20 +01:00
|
|
|
#include "diffutils.h"
|
2014-02-13 16:43:28 +01:00
|
|
|
#include "diffeditorconstants.h"
|
2014-02-24 11:10:17 +01:00
|
|
|
|
2013-02-15 12:49:50 +01:00
|
|
|
#include <QPlainTextEdit>
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
#include <QPlainTextDocumentLayout>
|
|
|
|
|
#include <QTextBlock>
|
|
|
|
|
#include <QScrollBar>
|
|
|
|
|
#include <QPainter>
|
2013-05-07 14:02:08 +02:00
|
|
|
#include <QDir>
|
2013-05-22 16:33:44 +02:00
|
|
|
#include <QToolButton>
|
2014-02-13 16:43:28 +01:00
|
|
|
#include <QTextCodec>
|
|
|
|
|
#include <QMessageBox>
|
2013-02-15 12:49:50 +01:00
|
|
|
|
|
|
|
|
#include <texteditor/basetexteditor.h>
|
|
|
|
|
#include <texteditor/basetextdocumentlayout.h>
|
2014-06-26 00:27:27 +02:00
|
|
|
#include <texteditor/highlighterfactory.h>
|
2013-02-15 12:49:50 +01:00
|
|
|
#include <texteditor/syntaxhighlighter.h>
|
|
|
|
|
#include <texteditor/basetextdocument.h>
|
|
|
|
|
#include <texteditor/texteditorsettings.h>
|
2013-06-05 15:25:42 +02:00
|
|
|
#include <texteditor/fontsettings.h>
|
2013-06-03 15:17:34 +02:00
|
|
|
#include <texteditor/displaysettings.h>
|
2013-09-13 12:13:10 +02:00
|
|
|
#include <texteditor/highlighterutils.h>
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-08-14 13:52:13 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2013-05-24 13:22:46 +02:00
|
|
|
#include <coreplugin/minisplitter.h>
|
2013-08-14 13:52:13 +02:00
|
|
|
#include <coreplugin/mimedatabase.h>
|
2014-02-13 16:43:28 +01:00
|
|
|
#include <coreplugin/patchtool.h>
|
2013-08-14 13:52:13 +02:00
|
|
|
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2013-05-24 13:22:46 +02:00
|
|
|
|
2013-06-18 14:09:54 +02:00
|
|
|
#include <utils/tooltip/tipcontents.h>
|
|
|
|
|
#include <utils/tooltip/tooltip.h>
|
|
|
|
|
|
2014-06-25 15:44:10 +02:00
|
|
|
//static const int FILE_LEVEL = 1;
|
|
|
|
|
//static const int CHUNK_LEVEL = 2;
|
2013-04-25 17:37:20 +02:00
|
|
|
|
2013-08-14 13:52:13 +02:00
|
|
|
using namespace Core;
|
2013-02-15 12:49:50 +01:00
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
2013-02-21 17:03:00 +01:00
|
|
|
namespace DiffEditor {
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-01-17 15:59:31 +01:00
|
|
|
//////////////////////
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
class SideDiffEditor : public BaseTextEditor
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2013-08-14 13:52:13 +02:00
|
|
|
Q_OBJECT
|
2013-02-15 12:49:50 +01:00
|
|
|
public:
|
2014-01-30 14:27:50 +01:00
|
|
|
SideDiffEditor(BaseTextEditorWidget *editorWidget)
|
2013-06-18 14:09:54 +02:00
|
|
|
: BaseTextEditor(editorWidget)
|
|
|
|
|
{
|
2014-03-05 15:58:12 +01:00
|
|
|
document()->setId("DiffEditor.SideDiffEditor");
|
2014-07-23 19:10:38 +02:00
|
|
|
connect(this, SIGNAL(tooltipRequested(TextEditor::BaseTextEditor*,QPoint,int)),
|
|
|
|
|
this, SLOT(slotTooltipRequested(TextEditor::BaseTextEditor*,QPoint,int)));
|
2013-06-18 14:09:54 +02:00
|
|
|
}
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-06-18 14:09:54 +02:00
|
|
|
private slots:
|
2014-07-23 19:10:38 +02:00
|
|
|
void slotTooltipRequested(TextEditor::BaseTextEditor *editor,
|
2014-02-13 16:43:28 +01:00
|
|
|
const QPoint &globalPoint,
|
|
|
|
|
int position);
|
2013-06-18 14:09:54 +02:00
|
|
|
|
2013-02-15 12:49:50 +01:00
|
|
|
};
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
////////////////////////
|
2014-02-13 16:43:28 +01:00
|
|
|
/*
|
2013-08-14 13:52:13 +02:00
|
|
|
class MultiHighlighter : public SyntaxHighlighter
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2014-01-30 14:27:50 +01:00
|
|
|
MultiHighlighter(SideDiffEditorWidget *editor, QTextDocument *document = 0);
|
2013-08-14 13:52:13 +02:00
|
|
|
~MultiHighlighter();
|
|
|
|
|
|
|
|
|
|
virtual void setFontSettings(const TextEditor::FontSettings &fontSettings);
|
2014-02-13 16:43:28 +01:00
|
|
|
void setDocuments(const QList<QPair<DiffFileInfo, QString> > &documents);
|
2013-08-14 13:52:13 +02:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
virtual void highlightBlock(const QString &text);
|
|
|
|
|
|
|
|
|
|
private:
|
2014-01-30 14:27:50 +01:00
|
|
|
SideDiffEditorWidget *m_editor;
|
2014-06-26 00:27:27 +02:00
|
|
|
QMap<QString, HighlighterFactory *> m_mimeTypeToHighlighterFactory;
|
2013-08-14 13:52:13 +02:00
|
|
|
QList<SyntaxHighlighter *> m_highlighters;
|
|
|
|
|
QList<QTextDocument *> m_documents;
|
|
|
|
|
};
|
2014-02-13 16:43:28 +01:00
|
|
|
*/
|
2013-02-15 12:49:50 +01:00
|
|
|
////////////////////////
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
class SideDiffEditorWidget : public SelectableTextEditorWidget
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2014-01-30 14:27:50 +01:00
|
|
|
SideDiffEditorWidget(QWidget *parent = 0);
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-08-14 13:52:13 +02:00
|
|
|
// block number, file info
|
2014-02-13 16:43:28 +01:00
|
|
|
QMap<int, DiffFileInfo> fileInfo() const { return m_fileInfo; }
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-04-26 11:50:10 +02:00
|
|
|
void setLineNumber(int blockNumber, int lineNumber);
|
2014-02-13 16:43:28 +01:00
|
|
|
void setFileInfo(int blockNumber, const DiffFileInfo &fileInfo);
|
|
|
|
|
void setSkippedLines(int blockNumber, int skippedLines) {
|
|
|
|
|
m_skippedLines[blockNumber] = skippedLines;
|
|
|
|
|
setSeparator(blockNumber, true);
|
|
|
|
|
}
|
|
|
|
|
void setChunkIndex(int startBlockNumber, int blockCount, int chunkIndex);
|
|
|
|
|
void setSeparator(int blockNumber, bool separator) {
|
|
|
|
|
m_separators[blockNumber] = separator;
|
|
|
|
|
}
|
|
|
|
|
bool isFileLine(int blockNumber) const {
|
|
|
|
|
return m_fileInfo.contains(blockNumber);
|
|
|
|
|
}
|
2013-05-23 13:36:27 +02:00
|
|
|
int blockNumberForFileIndex(int fileIndex) const;
|
|
|
|
|
int fileIndexForBlockNumber(int blockNumber) const;
|
2014-02-13 16:43:28 +01:00
|
|
|
int chunkIndexForBlockNumber(int blockNumber) const;
|
|
|
|
|
bool isChunkLine(int blockNumber) const {
|
|
|
|
|
return m_skippedLines.contains(blockNumber);
|
|
|
|
|
}
|
2013-05-07 14:02:08 +02:00
|
|
|
void clearAll(const QString &message);
|
2013-04-25 17:37:20 +02:00
|
|
|
void clearAllData();
|
2014-02-13 16:43:28 +01:00
|
|
|
QTextBlock firstVisibleBlock() const {
|
|
|
|
|
return BaseTextEditorWidget::firstVisibleBlock();
|
|
|
|
|
}
|
|
|
|
|
// void setDocuments(const QList<QPair<DiffFileInfo, QString> > &documents);
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-06-03 15:17:34 +02:00
|
|
|
public slots:
|
|
|
|
|
void setDisplaySettings(const DisplaySettings &ds);
|
|
|
|
|
|
2013-07-01 13:15:27 +02:00
|
|
|
signals:
|
|
|
|
|
void jumpToOriginalFileRequested(int diffFileIndex,
|
|
|
|
|
int lineNumber,
|
|
|
|
|
int columnNumber);
|
2014-02-13 16:43:28 +01:00
|
|
|
void contextMenuRequested(QMenu *menu,
|
|
|
|
|
int diffFileIndex,
|
|
|
|
|
int chunkIndex);
|
2013-07-01 13:15:27 +02:00
|
|
|
|
2013-02-15 12:49:50 +01:00
|
|
|
protected:
|
2014-02-13 16:43:28 +01:00
|
|
|
virtual int extraAreaWidth(int *markWidthPtr = 0) const {
|
|
|
|
|
return SelectableTextEditorWidget::extraAreaWidth(markWidthPtr);
|
|
|
|
|
}
|
2014-01-21 11:26:39 +01:00
|
|
|
void applyFontSettings();
|
2014-01-30 14:27:50 +01:00
|
|
|
BaseTextEditor *createEditor() { return new SideDiffEditor(this); }
|
2013-02-15 12:49:50 +01:00
|
|
|
virtual QString lineNumber(int blockNumber) const;
|
|
|
|
|
virtual int lineNumberDigits() const;
|
2013-04-09 10:26:31 +02:00
|
|
|
virtual bool selectionVisible(int blockNumber) const;
|
2013-04-10 13:51:04 +02:00
|
|
|
virtual bool replacementVisible(int blockNumber) const;
|
2013-06-05 15:25:42 +02:00
|
|
|
QColor replacementPenColor(int blockNumber) const;
|
2013-04-25 17:37:20 +02:00
|
|
|
virtual QString plainTextFromSelection(const QTextCursor &cursor) const;
|
2014-02-13 16:43:28 +01:00
|
|
|
// virtual void drawCollapsedBlockPopup(QPainter &painter,
|
|
|
|
|
// const QTextBlock &block,
|
|
|
|
|
// QPointF offset,
|
|
|
|
|
// const QRect &clip);
|
2013-04-26 11:50:10 +02:00
|
|
|
void mouseDoubleClickEvent(QMouseEvent *e);
|
2014-02-13 16:43:28 +01:00
|
|
|
void contextMenuEvent(QContextMenuEvent *e);
|
2013-02-15 12:49:50 +01:00
|
|
|
virtual void paintEvent(QPaintEvent *e);
|
|
|
|
|
virtual void scrollContentsBy(int dx, int dy);
|
|
|
|
|
|
|
|
|
|
private:
|
2014-02-13 16:43:28 +01:00
|
|
|
// void paintCollapsedBlockPopup(QPainter &painter, const QRect &clipRect);
|
2013-06-05 15:25:42 +02:00
|
|
|
void paintSeparator(QPainter &painter, QColor &color, const QString &text,
|
|
|
|
|
const QTextBlock &block, int top);
|
2013-04-26 11:50:10 +02:00
|
|
|
void jumpToOriginalFile(const QTextCursor &cursor);
|
2013-04-25 17:37:20 +02:00
|
|
|
|
2013-08-14 13:52:13 +02:00
|
|
|
// block number, visual line number.
|
2013-04-26 11:50:10 +02:00
|
|
|
QMap<int, int> m_lineNumbers;
|
2013-02-15 12:49:50 +01:00
|
|
|
int m_lineNumberDigits;
|
2013-08-14 13:52:13 +02:00
|
|
|
// block number, fileInfo. Set for file lines only.
|
2014-02-13 16:43:28 +01:00
|
|
|
QMap<int, DiffFileInfo> m_fileInfo;
|
2013-08-14 13:52:13 +02:00
|
|
|
// block number, skipped lines. Set for chunk lines only.
|
2013-04-09 10:26:31 +02:00
|
|
|
QMap<int, int> m_skippedLines;
|
2014-02-13 16:43:28 +01:00
|
|
|
// start block number, block count of a chunk, chunk index inside a file.
|
|
|
|
|
QMap<int, QPair<int, int> > m_chunkInfo;
|
2013-08-14 13:52:13 +02:00
|
|
|
// block number, separator. Set for file, chunk or span line.
|
2013-04-09 10:26:31 +02:00
|
|
|
QMap<int, bool> m_separators;
|
2013-04-25 17:37:20 +02:00
|
|
|
bool m_inPaintEvent;
|
2013-06-05 15:25:42 +02:00
|
|
|
QColor m_fileLineForeground;
|
|
|
|
|
QColor m_chunkLineForeground;
|
|
|
|
|
QColor m_textForeground;
|
2014-02-13 16:43:28 +01:00
|
|
|
// MultiHighlighter *m_highlighter;
|
2013-02-15 12:49:50 +01:00
|
|
|
};
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
////////////////////////
|
|
|
|
|
|
2014-07-23 19:10:38 +02:00
|
|
|
void SideDiffEditor::slotTooltipRequested(TextEditor::BaseTextEditor *editor,
|
2014-02-13 16:43:28 +01:00
|
|
|
const QPoint &globalPoint,
|
|
|
|
|
int position)
|
2014-01-30 14:27:50 +01:00
|
|
|
{
|
|
|
|
|
SideDiffEditorWidget *ew = qobject_cast<SideDiffEditorWidget *>(editorWidget());
|
|
|
|
|
if (!ew)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
QMap<int, DiffFileInfo> fi = ew->fileInfo();
|
|
|
|
|
QMap<int, DiffFileInfo>::const_iterator it
|
2014-01-30 14:27:50 +01:00
|
|
|
= fi.constFind(ew->document()->findBlock(position).blockNumber());
|
|
|
|
|
if (it != fi.constEnd()) {
|
|
|
|
|
Utils::ToolTip::show(globalPoint, Utils::TextContent(it.value().fileName),
|
|
|
|
|
editor->widget());
|
|
|
|
|
} else {
|
|
|
|
|
Utils::ToolTip::hide();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////
|
2014-02-13 16:43:28 +01:00
|
|
|
/*
|
2014-01-30 14:27:50 +01:00
|
|
|
MultiHighlighter::MultiHighlighter(SideDiffEditorWidget *editor, QTextDocument *document)
|
2013-08-14 13:52:13 +02:00
|
|
|
: SyntaxHighlighter(document),
|
|
|
|
|
m_editor(editor)
|
|
|
|
|
{
|
2014-06-26 00:27:27 +02:00
|
|
|
const QList<HighlighterFactory *> &factories =
|
|
|
|
|
ExtensionSystem::PluginManager::getObjects<TextEditor::HighlighterFactory>();
|
|
|
|
|
foreach (HighlighterFactory *factory, factories) {
|
2013-08-14 13:52:13 +02:00
|
|
|
QStringList mimeTypes = factory->mimeTypes();
|
|
|
|
|
foreach (const QString &mimeType, mimeTypes)
|
|
|
|
|
m_mimeTypeToHighlighterFactory.insert(mimeType, factory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MultiHighlighter::~MultiHighlighter()
|
|
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
setDocuments(QList<QPair<DiffFileInfo, QString> >());
|
2013-08-14 13:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MultiHighlighter::setFontSettings(const TextEditor::FontSettings &fontSettings)
|
|
|
|
|
{
|
|
|
|
|
foreach (SyntaxHighlighter *highlighter, m_highlighters) {
|
|
|
|
|
if (highlighter) {
|
|
|
|
|
highlighter->setFontSettings(fontSettings);
|
|
|
|
|
highlighter->rehighlight();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void MultiHighlighter::setDocuments(const QList<QPair<DiffFileInfo, QString> > &documents)
|
2013-08-14 13:52:13 +02:00
|
|
|
{
|
|
|
|
|
// clear old documents
|
|
|
|
|
qDeleteAll(m_documents);
|
|
|
|
|
m_documents.clear();
|
|
|
|
|
qDeleteAll(m_highlighters);
|
|
|
|
|
m_highlighters.clear();
|
|
|
|
|
|
|
|
|
|
// create new documents
|
|
|
|
|
for (int i = 0; i < documents.count(); i++) {
|
2014-02-13 16:43:28 +01:00
|
|
|
DiffFileInfo fileInfo = documents.at(i).first;
|
2013-08-14 13:52:13 +02:00
|
|
|
const QString contents = documents.at(i).second;
|
|
|
|
|
QTextDocument *document = new QTextDocument(contents);
|
2013-08-30 16:38:57 +02:00
|
|
|
const MimeType mimeType = MimeDatabase::findByFile(QFileInfo(fileInfo.fileName));
|
2013-08-14 13:52:13 +02:00
|
|
|
SyntaxHighlighter *highlighter = 0;
|
2014-06-26 00:27:27 +02:00
|
|
|
if (const HighlighterFactory *factory = m_mimeTypeToHighlighterFactory.value(mimeType.type())) {
|
2013-08-14 13:52:13 +02:00
|
|
|
highlighter = factory->createHighlighter();
|
|
|
|
|
if (highlighter)
|
|
|
|
|
highlighter->setDocument(document);
|
|
|
|
|
}
|
|
|
|
|
if (!highlighter) {
|
2013-09-13 12:13:10 +02:00
|
|
|
highlighter = createGenericSyntaxHighlighter(mimeType);
|
2013-08-14 13:52:13 +02:00
|
|
|
highlighter->setDocument(document);
|
|
|
|
|
}
|
|
|
|
|
m_documents.append(document);
|
|
|
|
|
m_highlighters.append(highlighter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MultiHighlighter::highlightBlock(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(text)
|
|
|
|
|
|
|
|
|
|
QTextBlock block = currentBlock();
|
|
|
|
|
const int fileIndex = m_editor->fileIndexForBlockNumber(block.blockNumber());
|
|
|
|
|
if (fileIndex < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
SyntaxHighlighter *currentHighlighter = m_highlighters.at(fileIndex);
|
|
|
|
|
if (!currentHighlighter)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// find block in document
|
|
|
|
|
QTextDocument *currentDocument = m_documents.at(fileIndex);
|
|
|
|
|
if (!currentDocument)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QTextBlock documentBlock = currentDocument->findBlockByNumber(
|
|
|
|
|
block.blockNumber() - m_editor->blockNumberForFileIndex(fileIndex));
|
|
|
|
|
|
2014-04-15 16:12:10 +02:00
|
|
|
if (!documentBlock.isValid())
|
|
|
|
|
return;
|
|
|
|
|
|
2013-08-14 13:52:13 +02:00
|
|
|
QList<QTextLayout::FormatRange> formats = documentBlock.layout()->additionalFormats();
|
|
|
|
|
setExtraAdditionalFormats(block, formats);
|
|
|
|
|
}
|
2014-02-13 16:43:28 +01:00
|
|
|
*/
|
2014-01-30 14:27:50 +01:00
|
|
|
////////////////////////
|
2013-06-18 14:09:54 +02:00
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
SideDiffEditorWidget::SideDiffEditorWidget(QWidget *parent)
|
2014-02-13 16:43:28 +01:00
|
|
|
: SelectableTextEditorWidget(parent),
|
|
|
|
|
m_lineNumberDigits(1),
|
|
|
|
|
m_inPaintEvent(false)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2013-06-03 15:17:34 +02:00
|
|
|
DisplaySettings settings = displaySettings();
|
|
|
|
|
settings.m_textWrapping = false;
|
|
|
|
|
settings.m_displayLineNumbers = true;
|
|
|
|
|
settings.m_highlightCurrentLine = false;
|
|
|
|
|
settings.m_displayFoldingMarkers = true;
|
|
|
|
|
settings.m_markTextChanges = false;
|
|
|
|
|
settings.m_highlightBlocks = false;
|
2014-02-13 16:43:28 +01:00
|
|
|
SelectableTextEditorWidget::setDisplaySettings(settings);
|
2013-06-03 15:17:34 +02:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
// setCodeFoldingSupported(true);
|
2013-08-14 13:52:13 +02:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
// m_highlighter = new MultiHighlighter(this, baseTextDocument()->document());
|
|
|
|
|
// baseTextDocument()->setSyntaxHighlighter(m_highlighter);
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::setDisplaySettings(const DisplaySettings &ds)
|
2013-06-03 15:17:34 +02:00
|
|
|
{
|
|
|
|
|
DisplaySettings settings = displaySettings();
|
|
|
|
|
settings.m_visualizeWhitespace = ds.m_visualizeWhitespace;
|
2014-02-13 16:43:28 +01:00
|
|
|
SelectableTextEditorWidget::setDisplaySettings(settings);
|
2013-06-03 15:17:34 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::applyFontSettings()
|
2013-06-05 15:25:42 +02:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
SelectableTextEditorWidget::applyFontSettings();
|
2014-08-01 23:31:56 +02:00
|
|
|
const TextEditor::FontSettings &fs = textDocument()->fontSettings();
|
2013-06-05 15:25:42 +02:00
|
|
|
m_fileLineForeground = fs.formatFor(C_DIFF_FILE_LINE).foreground();
|
|
|
|
|
m_chunkLineForeground = fs.formatFor(C_DIFF_CONTEXT_LINE).foreground();
|
|
|
|
|
m_textForeground = fs.toTextCharFormat(C_TEXT).foreground().color();
|
|
|
|
|
update();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
QString SideDiffEditorWidget::lineNumber(int blockNumber) const
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2013-04-26 11:50:10 +02:00
|
|
|
if (m_lineNumbers.contains(blockNumber))
|
|
|
|
|
return QString::number(m_lineNumbers.value(blockNumber));
|
|
|
|
|
return QString();
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
int SideDiffEditorWidget::lineNumberDigits() const
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2013-04-09 10:26:31 +02:00
|
|
|
return m_lineNumberDigits;
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
bool SideDiffEditorWidget::selectionVisible(int blockNumber) const
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2013-04-09 10:26:31 +02:00
|
|
|
return !m_separators.value(blockNumber, false);
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
bool SideDiffEditorWidget::replacementVisible(int blockNumber) const
|
2013-04-10 13:51:04 +02:00
|
|
|
{
|
2013-04-25 17:37:20 +02:00
|
|
|
return isChunkLine(blockNumber) || (isFileLine(blockNumber)
|
2014-02-13 16:43:28 +01:00
|
|
|
&& TextEditor::BaseTextDocumentLayout::isFolded(
|
|
|
|
|
document()->findBlockByNumber(blockNumber)));
|
2013-04-10 13:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
QColor SideDiffEditorWidget::replacementPenColor(int blockNumber) const
|
2013-06-05 15:25:42 +02:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(blockNumber)
|
|
|
|
|
return m_chunkLineForeground;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
QString SideDiffEditorWidget::plainTextFromSelection(const QTextCursor &cursor) const
|
2013-04-23 09:09:20 +02:00
|
|
|
{
|
|
|
|
|
const int startPosition = cursor.selectionStart();
|
|
|
|
|
const int endPosition = cursor.selectionEnd();
|
|
|
|
|
if (startPosition == endPosition)
|
|
|
|
|
return QString(); // no selection
|
|
|
|
|
|
|
|
|
|
QTextBlock startBlock = document()->findBlock(startPosition);
|
|
|
|
|
QTextBlock endBlock = document()->findBlock(endPosition);
|
|
|
|
|
QTextBlock block = startBlock;
|
|
|
|
|
QString text;
|
|
|
|
|
bool textInserted = false;
|
|
|
|
|
while (block.isValid() && block.blockNumber() <= endBlock.blockNumber()) {
|
|
|
|
|
if (selectionVisible(block.blockNumber())) {
|
|
|
|
|
if (block == startBlock) {
|
|
|
|
|
if (block == endBlock)
|
|
|
|
|
text = cursor.selectedText(); // just one line text
|
|
|
|
|
else
|
|
|
|
|
text = block.text().mid(startPosition - block.position());
|
|
|
|
|
} else {
|
|
|
|
|
if (textInserted)
|
|
|
|
|
text += QLatin1Char('\n');
|
|
|
|
|
if (block == endBlock)
|
|
|
|
|
text += block.text().left(endPosition - block.position());
|
|
|
|
|
else
|
|
|
|
|
text += block.text();
|
|
|
|
|
}
|
|
|
|
|
textInserted = true;
|
|
|
|
|
}
|
|
|
|
|
block = block.next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return convertToPlainText(text);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::setLineNumber(int blockNumber, int lineNumber)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2013-04-26 11:50:10 +02:00
|
|
|
const QString lineNumberString = QString::number(lineNumber);
|
2013-02-15 12:49:50 +01:00
|
|
|
m_lineNumbers.insert(blockNumber, lineNumber);
|
2013-04-26 11:50:10 +02:00
|
|
|
m_lineNumberDigits = qMax(m_lineNumberDigits, lineNumberString.count());
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideDiffEditorWidget::setFileInfo(int blockNumber, const DiffFileInfo &fileInfo)
|
2013-08-14 13:52:13 +02:00
|
|
|
{
|
|
|
|
|
m_fileInfo[blockNumber] = fileInfo;
|
|
|
|
|
setSeparator(blockNumber, true);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideDiffEditorWidget::setChunkIndex(int startBlockNumber, int blockCount, int chunkIndex)
|
|
|
|
|
{
|
|
|
|
|
m_chunkInfo.insert(startBlockNumber, qMakePair(blockCount, chunkIndex));
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
int SideDiffEditorWidget::blockNumberForFileIndex(int fileIndex) const
|
2013-05-23 13:36:27 +02:00
|
|
|
{
|
|
|
|
|
if (fileIndex < 0 || fileIndex >= m_fileInfo.count())
|
|
|
|
|
return -1;
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
QMap<int, DiffFileInfo>::const_iterator it
|
2013-05-23 13:36:27 +02:00
|
|
|
= m_fileInfo.constBegin();
|
|
|
|
|
for (int i = 0; i < fileIndex; i++)
|
|
|
|
|
++it;
|
|
|
|
|
|
|
|
|
|
return it.key();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
int SideDiffEditorWidget::fileIndexForBlockNumber(int blockNumber) const
|
2013-05-23 13:36:27 +02:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
QMap<int, DiffFileInfo>::const_iterator it
|
2013-05-23 13:36:27 +02:00
|
|
|
= m_fileInfo.constBegin();
|
2014-02-13 16:43:28 +01:00
|
|
|
QMap<int, DiffFileInfo>::const_iterator itEnd
|
2013-05-23 13:36:27 +02:00
|
|
|
= m_fileInfo.constEnd();
|
|
|
|
|
|
|
|
|
|
int i = -1;
|
|
|
|
|
while (it != itEnd) {
|
|
|
|
|
if (it.key() > blockNumber)
|
|
|
|
|
break;
|
|
|
|
|
++it;
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
int SideDiffEditorWidget::chunkIndexForBlockNumber(int blockNumber) const
|
|
|
|
|
{
|
|
|
|
|
if (m_chunkInfo.isEmpty())
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
QMap<int, QPair<int, int> >::const_iterator it
|
|
|
|
|
= m_chunkInfo.upperBound(blockNumber);
|
|
|
|
|
if (it == m_chunkInfo.constBegin())
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
--it;
|
|
|
|
|
|
|
|
|
|
if (blockNumber < it.key() + it.value().first)
|
|
|
|
|
return it.value().second;
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::clearAll(const QString &message)
|
2013-05-07 14:02:08 +02:00
|
|
|
{
|
|
|
|
|
setBlockSelection(false);
|
|
|
|
|
clear();
|
|
|
|
|
clearAllData();
|
2014-02-13 16:43:28 +01:00
|
|
|
setExtraSelections(BaseTextEditorWidget::OtherSelection,
|
|
|
|
|
QList<QTextEdit::ExtraSelection>());
|
2013-05-07 14:02:08 +02:00
|
|
|
setPlainText(message);
|
2014-02-13 16:43:28 +01:00
|
|
|
// m_highlighter->setDocuments(QList<QPair<DiffFileInfo, QString> >());
|
2013-05-07 14:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::clearAllData()
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
|
|
|
|
m_lineNumberDigits = 1;
|
2013-04-25 17:37:20 +02:00
|
|
|
m_lineNumbers.clear();
|
2013-05-07 14:02:08 +02:00
|
|
|
m_fileInfo.clear();
|
2013-04-25 17:37:20 +02:00
|
|
|
m_skippedLines.clear();
|
2014-02-13 16:43:28 +01:00
|
|
|
m_chunkInfo.clear();
|
2013-04-25 17:37:20 +02:00
|
|
|
m_separators.clear();
|
2014-02-13 16:43:28 +01:00
|
|
|
setSelections(QMap<int, QList<DiffSelection> >());
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
2014-02-13 16:43:28 +01:00
|
|
|
/*
|
|
|
|
|
void SideDiffEditorWidget::setDocuments(const QList<QPair<DiffFileInfo, QString> > &documents)
|
2013-08-14 13:52:13 +02:00
|
|
|
{
|
|
|
|
|
m_highlighter->setDocuments(documents);
|
|
|
|
|
}
|
2014-02-13 16:43:28 +01:00
|
|
|
*/
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::scrollContentsBy(int dx, int dy)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
SelectableTextEditorWidget::scrollContentsBy(dx, dy);
|
2013-02-15 12:49:50 +01:00
|
|
|
// TODO: update only chunk lines
|
|
|
|
|
viewport()->update();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::paintSeparator(QPainter &painter,
|
2013-06-05 15:25:42 +02:00
|
|
|
QColor &color,
|
|
|
|
|
const QString &text,
|
|
|
|
|
const QTextBlock &block,
|
|
|
|
|
int top)
|
2013-04-25 17:37:20 +02:00
|
|
|
{
|
|
|
|
|
QPointF offset = contentOffset();
|
|
|
|
|
painter.save();
|
2013-06-05 15:25:42 +02:00
|
|
|
|
|
|
|
|
QColor foreground = color;
|
|
|
|
|
if (!foreground.isValid())
|
|
|
|
|
foreground = m_textForeground;
|
|
|
|
|
if (!foreground.isValid())
|
|
|
|
|
foreground = palette().foreground().color();
|
|
|
|
|
|
|
|
|
|
painter.setPen(foreground);
|
|
|
|
|
|
2013-04-25 17:37:20 +02:00
|
|
|
const QString replacementText = QLatin1String(" {")
|
|
|
|
|
+ foldReplacementText(block)
|
|
|
|
|
+ QLatin1String("}; ");
|
|
|
|
|
const int replacementTextWidth = fontMetrics().width(replacementText) + 24;
|
|
|
|
|
int x = replacementTextWidth + offset.x();
|
2014-02-13 16:43:28 +01:00
|
|
|
if (x < document()->documentMargin()
|
|
|
|
|
|| !TextEditor::BaseTextDocumentLayout::isFolded(block)) {
|
2013-04-25 17:37:20 +02:00
|
|
|
x = document()->documentMargin();
|
2014-02-13 16:43:28 +01:00
|
|
|
}
|
2013-04-25 17:37:20 +02:00
|
|
|
const QString elidedText = fontMetrics().elidedText(text,
|
|
|
|
|
Qt::ElideRight,
|
|
|
|
|
viewport()->width() - x);
|
|
|
|
|
QTextLayout *layout = block.layout();
|
|
|
|
|
QTextLine textLine = layout->lineAt(0);
|
|
|
|
|
QRectF lineRect = textLine.naturalTextRect().translated(offset.x(), top);
|
|
|
|
|
QRect clipRect = contentsRect();
|
|
|
|
|
clipRect.setLeft(x);
|
|
|
|
|
painter.setClipRect(clipRect);
|
|
|
|
|
painter.drawText(QPointF(x, lineRect.top() + textLine.ascent()),
|
|
|
|
|
elidedText);
|
|
|
|
|
painter.restore();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::mouseDoubleClickEvent(QMouseEvent *e)
|
2013-04-26 11:50:10 +02:00
|
|
|
{
|
|
|
|
|
if (e->button() == Qt::LeftButton && !(e->modifiers() & Qt::ShiftModifier)) {
|
|
|
|
|
QTextCursor cursor = cursorForPosition(e->pos());
|
|
|
|
|
jumpToOriginalFile(cursor);
|
2013-07-01 11:38:16 +02:00
|
|
|
e->accept();
|
|
|
|
|
return;
|
2013-04-26 11:50:10 +02:00
|
|
|
}
|
2014-02-13 16:43:28 +01:00
|
|
|
SelectableTextEditorWidget::mouseDoubleClickEvent(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SideDiffEditorWidget::contextMenuEvent(QContextMenuEvent *e)
|
|
|
|
|
{
|
|
|
|
|
QPointer<QMenu> menu = createStandardContextMenu();
|
|
|
|
|
|
|
|
|
|
QTextCursor cursor = cursorForPosition(e->pos());
|
|
|
|
|
const int blockNumber = cursor.blockNumber();
|
|
|
|
|
|
|
|
|
|
emit contextMenuRequested(menu, fileIndexForBlockNumber(blockNumber),
|
|
|
|
|
chunkIndexForBlockNumber(blockNumber));
|
|
|
|
|
|
|
|
|
|
connect(this, SIGNAL(destroyed()), menu, SLOT(deleteLater()));
|
|
|
|
|
menu->exec(e->globalPos());
|
|
|
|
|
delete menu;
|
2013-04-26 11:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::jumpToOriginalFile(const QTextCursor &cursor)
|
2013-04-26 11:50:10 +02:00
|
|
|
{
|
2013-05-07 14:02:08 +02:00
|
|
|
if (m_fileInfo.isEmpty())
|
2013-04-26 11:50:10 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const int blockNumber = cursor.blockNumber();
|
2013-07-01 13:15:27 +02:00
|
|
|
const int columnNumber = cursor.positionInBlock();
|
2013-04-26 11:50:10 +02:00
|
|
|
if (!m_lineNumbers.contains(blockNumber))
|
|
|
|
|
return;
|
|
|
|
|
|
2013-07-01 13:15:27 +02:00
|
|
|
const int lineNumber = m_lineNumbers.value(blockNumber);
|
2013-04-26 11:50:10 +02:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
emit jumpToOriginalFileRequested(fileIndexForBlockNumber(blockNumber),
|
|
|
|
|
lineNumber, columnNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QString skippedText(int skippedNumber)
|
|
|
|
|
{
|
|
|
|
|
if (skippedNumber > 0)
|
|
|
|
|
return SideBySideDiffEditorWidget::tr("Skipped %n lines...", 0, skippedNumber);
|
|
|
|
|
if (skippedNumber == -2)
|
|
|
|
|
return SideBySideDiffEditorWidget::tr("Binary files differ");
|
|
|
|
|
return SideBySideDiffEditorWidget::tr("Skipped unknown number of lines...");
|
2013-04-26 11:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::paintEvent(QPaintEvent *e)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2013-04-25 17:37:20 +02:00
|
|
|
m_inPaintEvent = true;
|
2014-02-13 16:43:28 +01:00
|
|
|
SelectableTextEditorWidget::paintEvent(e);
|
2013-04-25 17:37:20 +02:00
|
|
|
m_inPaintEvent = false;
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
QPainter painter(viewport());
|
2013-02-15 12:49:50 +01:00
|
|
|
QPointF offset = contentOffset();
|
|
|
|
|
QTextBlock firstBlock = firstVisibleBlock();
|
2013-02-21 17:14:07 +01:00
|
|
|
QTextBlock currentBlock = firstBlock;
|
|
|
|
|
|
|
|
|
|
while (currentBlock.isValid()) {
|
|
|
|
|
if (currentBlock.isVisible()) {
|
|
|
|
|
qreal top = blockBoundingGeometry(currentBlock).translated(offset).top();
|
|
|
|
|
qreal bottom = top + blockBoundingRect(currentBlock).height();
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-02-21 17:14:07 +01:00
|
|
|
if (top > e->rect().bottom())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (bottom >= e->rect().top()) {
|
2013-04-25 17:37:20 +02:00
|
|
|
const int blockNumber = currentBlock.blockNumber();
|
2013-02-21 17:14:07 +01:00
|
|
|
|
2013-04-25 17:37:20 +02:00
|
|
|
const int skippedBefore = m_skippedLines.value(blockNumber);
|
2013-04-09 10:26:31 +02:00
|
|
|
if (skippedBefore) {
|
2014-02-13 16:43:28 +01:00
|
|
|
const QString skippedRowsText = skippedText(skippedBefore);
|
2013-06-05 15:25:42 +02:00
|
|
|
paintSeparator(painter, m_chunkLineForeground,
|
|
|
|
|
skippedRowsText, currentBlock, top);
|
2013-04-25 17:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
const DiffFileInfo fileInfo = m_fileInfo.value(blockNumber);
|
2013-05-07 14:02:08 +02:00
|
|
|
if (!fileInfo.fileName.isEmpty()) {
|
|
|
|
|
const QString fileNameText = fileInfo.typeInfo.isEmpty()
|
|
|
|
|
? fileInfo.fileName
|
2014-02-13 16:43:28 +01:00
|
|
|
: tr("[%1] %2").arg(fileInfo.typeInfo)
|
|
|
|
|
.arg(fileInfo.fileName);
|
2013-06-05 15:25:42 +02:00
|
|
|
paintSeparator(painter, m_fileLineForeground,
|
|
|
|
|
fileNameText, currentBlock, top);
|
2013-02-21 17:14:07 +01:00
|
|
|
}
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 17:14:07 +01:00
|
|
|
currentBlock = currentBlock.next();
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
2014-02-13 16:43:28 +01:00
|
|
|
// paintCollapsedBlockPopup(painter, e->rect());
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
2014-02-13 16:43:28 +01:00
|
|
|
/*
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::paintCollapsedBlockPopup(QPainter &painter, const QRect &clipRect)
|
2013-04-25 17:37:20 +02:00
|
|
|
{
|
|
|
|
|
QPointF offset(contentOffset());
|
|
|
|
|
QRect viewportRect = viewport()->rect();
|
|
|
|
|
QTextBlock block = firstVisibleBlock();
|
|
|
|
|
QTextBlock visibleCollapsedBlock;
|
|
|
|
|
QPointF visibleCollapsedBlockOffset;
|
|
|
|
|
|
|
|
|
|
while (block.isValid()) {
|
|
|
|
|
|
|
|
|
|
QRectF r = blockBoundingRect(block).translated(offset);
|
|
|
|
|
|
|
|
|
|
offset.ry() += r.height();
|
|
|
|
|
|
|
|
|
|
if (offset.y() > viewportRect.height())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
block = block.next();
|
|
|
|
|
|
|
|
|
|
if (!block.isVisible()) {
|
|
|
|
|
if (block.blockNumber() == visibleFoldedBlockNumber()) {
|
|
|
|
|
visibleCollapsedBlock = block;
|
|
|
|
|
visibleCollapsedBlockOffset = offset + QPointF(0,1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// invisible blocks do have zero line count
|
|
|
|
|
block = document()->findBlockByLineNumber(block.firstLineNumber());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (visibleCollapsedBlock.isValid()) {
|
|
|
|
|
drawCollapsedBlockPopup(painter,
|
|
|
|
|
visibleCollapsedBlock,
|
|
|
|
|
visibleCollapsedBlockOffset,
|
|
|
|
|
clipRect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::drawCollapsedBlockPopup(QPainter &painter,
|
2013-04-25 17:37:20 +02:00
|
|
|
const QTextBlock &block,
|
|
|
|
|
QPointF offset,
|
|
|
|
|
const QRect &clip)
|
|
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
// We ignore the call coming from the SelectableTextEditorWidget::paintEvent()
|
2013-04-25 17:37:20 +02:00
|
|
|
// since we will draw it later, after custom drawings of this paintEvent.
|
|
|
|
|
// We need to draw it after our custom drawings, otherwise custom
|
|
|
|
|
// drawings will appear in front of block popup.
|
|
|
|
|
if (m_inPaintEvent)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
int margin = block.document()->documentMargin();
|
|
|
|
|
qreal maxWidth = 0;
|
|
|
|
|
qreal blockHeight = 0;
|
|
|
|
|
QTextBlock b = block;
|
|
|
|
|
|
|
|
|
|
while (!b.isVisible()) {
|
|
|
|
|
if (!m_separators.contains(b.blockNumber())) {
|
|
|
|
|
b.setVisible(true); // make sure block bounding rect works
|
|
|
|
|
QRectF r = blockBoundingRect(b).translated(offset);
|
|
|
|
|
|
|
|
|
|
QTextLayout *layout = b.layout();
|
|
|
|
|
for (int i = layout->lineCount()-1; i >= 0; --i)
|
|
|
|
|
maxWidth = qMax(maxWidth, layout->lineAt(i).naturalTextWidth() + 2*margin);
|
|
|
|
|
|
|
|
|
|
blockHeight += r.height();
|
|
|
|
|
|
|
|
|
|
b.setVisible(false); // restore previous state
|
|
|
|
|
b.setLineCount(0); // restore 0 line count for invisible block
|
|
|
|
|
}
|
|
|
|
|
b = b.next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
painter.save();
|
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
|
painter.translate(.5, .5);
|
|
|
|
|
QBrush brush = palette().base();
|
|
|
|
|
painter.setBrush(brush);
|
|
|
|
|
painter.drawRoundedRect(QRectF(offset.x(),
|
|
|
|
|
offset.y(),
|
|
|
|
|
maxWidth, blockHeight).adjusted(0, 0, 0, 0), 3, 3);
|
|
|
|
|
painter.restore();
|
|
|
|
|
|
|
|
|
|
QTextBlock end = b;
|
|
|
|
|
b = block;
|
|
|
|
|
while (b != end) {
|
|
|
|
|
if (!m_separators.contains(b.blockNumber())) {
|
|
|
|
|
b.setVisible(true); // make sure block bounding rect works
|
|
|
|
|
QRectF r = blockBoundingRect(b).translated(offset);
|
|
|
|
|
QTextLayout *layout = b.layout();
|
|
|
|
|
QVector<QTextLayout::FormatRange> selections;
|
|
|
|
|
layout->draw(&painter, offset, selections, clip);
|
|
|
|
|
|
|
|
|
|
b.setVisible(false); // restore previous state
|
|
|
|
|
b.setLineCount(0); // restore 0 line count for invisible block
|
|
|
|
|
offset.ry() += r.height();
|
|
|
|
|
}
|
|
|
|
|
b = b.next();
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-13 16:43:28 +01:00
|
|
|
*/
|
2013-04-25 17:37:20 +02:00
|
|
|
|
2013-02-15 12:49:50 +01:00
|
|
|
//////////////////
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
SideBySideDiffEditorWidget::SideBySideDiffEditorWidget(QWidget *parent)
|
2013-12-16 16:19:40 +01:00
|
|
|
: QWidget(parent)
|
2014-02-24 11:10:17 +01:00
|
|
|
, m_guiController(0)
|
2013-12-16 16:19:40 +01:00
|
|
|
, m_controller(0)
|
2014-02-13 16:43:28 +01:00
|
|
|
, m_ignoreCurrentIndexChange(false)
|
2013-12-16 16:19:40 +01:00
|
|
|
, m_foldingBlocker(false)
|
2014-02-13 16:43:28 +01:00
|
|
|
, m_contextMenuFileIndex(-1)
|
|
|
|
|
, m_contextMenuChunkIndex(-1)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2014-01-30 14:27:50 +01:00
|
|
|
m_leftEditor = new SideDiffEditorWidget(this);
|
2013-02-15 12:49:50 +01:00
|
|
|
m_leftEditor->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
|
m_leftEditor->setReadOnly(true);
|
2013-09-19 17:59:27 +02:00
|
|
|
connect(TextEditorSettings::instance(),
|
|
|
|
|
SIGNAL(displaySettingsChanged(TextEditor::DisplaySettings)),
|
2013-06-03 15:17:34 +02:00
|
|
|
m_leftEditor, SLOT(setDisplaySettings(TextEditor::DisplaySettings)));
|
2013-09-19 17:59:27 +02:00
|
|
|
m_leftEditor->setDisplaySettings(TextEditorSettings::displaySettings());
|
|
|
|
|
m_leftEditor->setCodeStyle(TextEditorSettings::codeStyle());
|
2013-07-01 13:15:27 +02:00
|
|
|
connect(m_leftEditor, SIGNAL(jumpToOriginalFileRequested(int,int,int)),
|
|
|
|
|
this, SLOT(slotLeftJumpToOriginalFileRequested(int,int,int)));
|
2014-02-13 16:43:28 +01:00
|
|
|
connect(m_leftEditor, SIGNAL(contextMenuRequested(QMenu*,int,int)),
|
2014-07-28 22:25:27 +03:00
|
|
|
this, SLOT(slotLeftContextMenuRequested(QMenu*,int,int)),
|
|
|
|
|
Qt::DirectConnection);
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
m_rightEditor = new SideDiffEditorWidget(this);
|
2013-02-15 12:49:50 +01:00
|
|
|
m_rightEditor->setReadOnly(true);
|
2013-09-19 17:59:27 +02:00
|
|
|
connect(TextEditorSettings::instance(),
|
|
|
|
|
SIGNAL(displaySettingsChanged(TextEditor::DisplaySettings)),
|
2013-06-03 15:17:34 +02:00
|
|
|
m_rightEditor, SLOT(setDisplaySettings(TextEditor::DisplaySettings)));
|
2013-09-19 17:59:27 +02:00
|
|
|
m_rightEditor->setDisplaySettings(TextEditorSettings::displaySettings());
|
|
|
|
|
m_rightEditor->setCodeStyle(TextEditorSettings::codeStyle());
|
2013-07-01 13:15:27 +02:00
|
|
|
connect(m_rightEditor, SIGNAL(jumpToOriginalFileRequested(int,int,int)),
|
|
|
|
|
this, SLOT(slotRightJumpToOriginalFileRequested(int,int,int)));
|
2014-02-13 16:43:28 +01:00
|
|
|
connect(m_rightEditor, SIGNAL(contextMenuRequested(QMenu*,int,int)),
|
2014-07-28 22:25:27 +03:00
|
|
|
this, SLOT(slotRightContextMenuRequested(QMenu*,int,int)),
|
|
|
|
|
Qt::DirectConnection);
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-09-19 17:59:27 +02:00
|
|
|
connect(TextEditorSettings::instance(),
|
|
|
|
|
SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
|
2013-06-05 15:25:42 +02:00
|
|
|
this, SLOT(setFontSettings(TextEditor::FontSettings)));
|
2013-09-19 17:59:27 +02:00
|
|
|
setFontSettings(TextEditorSettings::fontSettings());
|
2013-06-05 15:25:42 +02:00
|
|
|
|
2013-02-15 12:49:50 +01:00
|
|
|
connect(m_leftEditor->verticalScrollBar(), SIGNAL(valueChanged(int)),
|
2013-05-22 16:33:44 +02:00
|
|
|
this, SLOT(leftVSliderChanged()));
|
2013-02-15 12:49:50 +01:00
|
|
|
connect(m_leftEditor->verticalScrollBar(), SIGNAL(actionTriggered(int)),
|
2013-05-22 16:33:44 +02:00
|
|
|
this, SLOT(leftVSliderChanged()));
|
|
|
|
|
|
|
|
|
|
connect(m_leftEditor->horizontalScrollBar(), SIGNAL(valueChanged(int)),
|
|
|
|
|
this, SLOT(leftHSliderChanged()));
|
|
|
|
|
connect(m_leftEditor->horizontalScrollBar(), SIGNAL(actionTriggered(int)),
|
|
|
|
|
this, SLOT(leftHSliderChanged()));
|
|
|
|
|
|
2013-05-23 13:36:27 +02:00
|
|
|
connect(m_leftEditor, SIGNAL(cursorPositionChanged()),
|
|
|
|
|
this, SLOT(leftCursorPositionChanged()));
|
2014-02-13 16:43:28 +01:00
|
|
|
// connect(m_leftEditor->document()->documentLayout(), SIGNAL(documentSizeChanged(QSizeF)),
|
|
|
|
|
// this, SLOT(leftDocumentSizeChanged()));
|
2013-05-22 16:33:44 +02:00
|
|
|
|
2013-02-15 12:49:50 +01:00
|
|
|
connect(m_rightEditor->verticalScrollBar(), SIGNAL(valueChanged(int)),
|
2013-05-22 16:33:44 +02:00
|
|
|
this, SLOT(rightVSliderChanged()));
|
2013-02-15 12:49:50 +01:00
|
|
|
connect(m_rightEditor->verticalScrollBar(), SIGNAL(actionTriggered(int)),
|
2013-05-22 16:33:44 +02:00
|
|
|
this, SLOT(rightVSliderChanged()));
|
|
|
|
|
|
|
|
|
|
connect(m_rightEditor->horizontalScrollBar(), SIGNAL(valueChanged(int)),
|
|
|
|
|
this, SLOT(rightHSliderChanged()));
|
|
|
|
|
connect(m_rightEditor->horizontalScrollBar(), SIGNAL(actionTriggered(int)),
|
|
|
|
|
this, SLOT(rightHSliderChanged()));
|
|
|
|
|
|
2013-05-23 13:36:27 +02:00
|
|
|
connect(m_rightEditor, SIGNAL(cursorPositionChanged()),
|
|
|
|
|
this, SLOT(rightCursorPositionChanged()));
|
2014-02-13 16:43:28 +01:00
|
|
|
// connect(m_rightEditor->document()->documentLayout(), SIGNAL(documentSizeChanged(QSizeF)),
|
|
|
|
|
// this, SLOT(rightDocumentSizeChanged()));
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-05-24 13:22:46 +02:00
|
|
|
m_splitter = new Core::MiniSplitter(this);
|
2013-02-15 12:49:50 +01:00
|
|
|
m_splitter->addWidget(m_leftEditor);
|
|
|
|
|
m_splitter->addWidget(m_rightEditor);
|
|
|
|
|
QVBoxLayout *l = new QVBoxLayout(this);
|
2013-05-22 16:33:44 +02:00
|
|
|
l->setMargin(0);
|
2013-02-15 12:49:50 +01:00
|
|
|
l->addWidget(m_splitter);
|
2013-05-07 14:02:08 +02:00
|
|
|
|
2013-12-16 16:19:40 +01:00
|
|
|
clear(tr("No controller"));
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
SideBySideDiffEditorWidget::~SideBySideDiffEditorWidget()
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideBySideDiffEditorWidget::setDiffEditorGuiController(
|
|
|
|
|
DiffEditorGuiController *controller)
|
2013-05-07 14:02:08 +02:00
|
|
|
{
|
2014-03-10 12:57:48 +01:00
|
|
|
if (m_guiController == controller)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-02-24 11:10:17 +01:00
|
|
|
if (m_guiController) {
|
2014-02-13 16:43:28 +01:00
|
|
|
disconnect(m_controller, SIGNAL(cleared(QString)),
|
|
|
|
|
this, SLOT(clearAll(QString)));
|
|
|
|
|
disconnect(m_controller, SIGNAL(diffFilesChanged(QList<FileData>,QString)),
|
|
|
|
|
this, SLOT(setDiff(QList<FileData>,QString)));
|
|
|
|
|
|
2014-02-24 11:10:17 +01:00
|
|
|
disconnect(m_guiController, SIGNAL(currentDiffFileIndexChanged(int)),
|
2014-02-13 16:43:28 +01:00
|
|
|
this, SLOT(setCurrentDiffFileIndex(int)));
|
2013-12-16 16:19:40 +01:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
clearAll(tr("No controller"));
|
2013-12-16 16:19:40 +01:00
|
|
|
}
|
2014-02-24 11:10:17 +01:00
|
|
|
m_guiController = controller;
|
|
|
|
|
m_controller = 0;
|
|
|
|
|
if (m_guiController) {
|
|
|
|
|
m_controller = m_guiController->controller();
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
connect(m_controller, SIGNAL(cleared(QString)),
|
|
|
|
|
this, SLOT(clearAll(QString)));
|
|
|
|
|
connect(m_controller, SIGNAL(diffFilesChanged(QList<FileData>,QString)),
|
|
|
|
|
this, SLOT(setDiff(QList<FileData>,QString)));
|
2014-02-24 11:10:17 +01:00
|
|
|
|
|
|
|
|
connect(m_guiController, SIGNAL(currentDiffFileIndexChanged(int)),
|
2013-12-16 16:19:40 +01:00
|
|
|
this, SLOT(setCurrentDiffFileIndex(int)));
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
setDiff(m_controller->diffFiles(), m_controller->workingDirectory());
|
|
|
|
|
setCurrentDiffFileIndex(m_guiController->currentDiffFileIndex());
|
2013-12-16 16:19:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-24 11:10:17 +01:00
|
|
|
|
|
|
|
|
DiffEditorGuiController *SideBySideDiffEditorWidget::diffEditorGuiController() const
|
2013-12-16 16:19:40 +01:00
|
|
|
{
|
2014-02-24 11:10:17 +01:00
|
|
|
return m_guiController;
|
2013-05-07 14:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::clear(const QString &message)
|
2013-05-07 14:02:08 +02:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
const bool oldIgnore = m_ignoreCurrentIndexChange;
|
|
|
|
|
m_ignoreCurrentIndexChange = true;
|
2013-05-07 14:02:08 +02:00
|
|
|
m_leftEditor->clearAll(message);
|
|
|
|
|
m_rightEditor->clearAll(message);
|
2014-02-13 16:43:28 +01:00
|
|
|
m_ignoreCurrentIndexChange = oldIgnore;
|
2013-05-07 14:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideBySideDiffEditorWidget::clearAll(const QString &message)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
setDiff(QList<FileData>(), QString());
|
|
|
|
|
clear(message);
|
2014-01-17 15:59:31 +01:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideBySideDiffEditorWidget::setDiff(const QList<FileData> &diffFileList,
|
|
|
|
|
const QString &workingDirectory)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
Q_UNUSED(workingDirectory)
|
2013-04-25 17:37:20 +02:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
m_contextFileData = diffFileList;
|
2013-02-15 12:49:50 +01:00
|
|
|
showDiff();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::setCurrentDiffFileIndex(int diffFileIndex)
|
2013-05-23 13:36:27 +02:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
if (m_ignoreCurrentIndexChange)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-05-23 13:36:27 +02:00
|
|
|
const int blockNumber = m_leftEditor->blockNumberForFileIndex(diffFileIndex);
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
const bool oldIgnore = m_ignoreCurrentIndexChange;
|
|
|
|
|
m_ignoreCurrentIndexChange = true;
|
2013-05-23 13:36:27 +02:00
|
|
|
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();
|
2014-02-13 16:43:28 +01:00
|
|
|
m_ignoreCurrentIndexChange = oldIgnore;
|
2013-05-23 13:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::showDiff()
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2013-04-09 10:26:31 +02:00
|
|
|
// TODO: remember the line number of the line in the middle
|
2013-02-15 12:49:50 +01:00
|
|
|
const int verticalValue = m_leftEditor->verticalScrollBar()->value();
|
|
|
|
|
const int leftHorizontalValue = m_leftEditor->horizontalScrollBar()->value();
|
|
|
|
|
const int rightHorizontalValue = m_rightEditor->horizontalScrollBar()->value();
|
|
|
|
|
|
2013-12-16 16:19:40 +01:00
|
|
|
clear(tr("No difference"));
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
QMap<int, QList<DiffSelection> > leftFormats;
|
|
|
|
|
QMap<int, QList<DiffSelection> > rightFormats;
|
|
|
|
|
|
|
|
|
|
// QList<QPair<DiffFileInfo, QString> > leftDocs, rightDocs;
|
2013-08-14 13:52:13 +02:00
|
|
|
QString leftTexts, rightTexts;
|
2013-04-09 10:26:31 +02:00
|
|
|
int blockNumber = 0;
|
|
|
|
|
QChar separator = QLatin1Char('\n');
|
2013-04-25 17:37:20 +02:00
|
|
|
for (int i = 0; i < m_contextFileData.count(); i++) {
|
2013-08-14 13:52:13 +02:00
|
|
|
QString leftText, rightText;
|
2013-04-25 17:37:20 +02:00
|
|
|
const FileData &contextFileData = m_contextFileData.at(i);
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
leftFormats[blockNumber].append(DiffSelection(&m_fileLineFormat));
|
|
|
|
|
rightFormats[blockNumber].append(DiffSelection(&m_fileLineFormat));
|
2013-05-07 14:02:08 +02:00
|
|
|
m_leftEditor->setFileInfo(blockNumber, contextFileData.leftFileInfo);
|
|
|
|
|
m_rightEditor->setFileInfo(blockNumber, contextFileData.rightFileInfo);
|
2013-08-14 13:52:13 +02:00
|
|
|
leftText = separator;
|
|
|
|
|
rightText = separator;
|
2013-04-25 17:37:20 +02:00
|
|
|
blockNumber++;
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
int lastLeftLineNumber = -1;
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
if (contextFileData.binaryFiles) {
|
|
|
|
|
leftFormats[blockNumber].append(DiffSelection(&m_chunkLineFormat));
|
|
|
|
|
rightFormats[blockNumber].append(DiffSelection(&m_chunkLineFormat));
|
|
|
|
|
m_leftEditor->setSkippedLines(blockNumber, -2);
|
|
|
|
|
m_rightEditor->setSkippedLines(blockNumber, -2);
|
|
|
|
|
leftText += separator;
|
|
|
|
|
rightText += separator;
|
|
|
|
|
blockNumber++;
|
|
|
|
|
} else {
|
|
|
|
|
for (int j = 0; j < contextFileData.chunks.count(); j++) {
|
|
|
|
|
ChunkData chunkData = contextFileData.chunks.at(j);
|
|
|
|
|
|
|
|
|
|
int leftLineNumber = chunkData.leftStartingLineNumber;
|
|
|
|
|
int rightLineNumber = chunkData.rightStartingLineNumber;
|
|
|
|
|
|
|
|
|
|
if (!chunkData.contextChunk) {
|
|
|
|
|
const int skippedLines = leftLineNumber - lastLeftLineNumber - 1;
|
|
|
|
|
if (skippedLines > 0) {
|
|
|
|
|
leftFormats[blockNumber].append(DiffSelection(&m_chunkLineFormat));
|
|
|
|
|
rightFormats[blockNumber].append(DiffSelection(&m_chunkLineFormat));
|
|
|
|
|
m_leftEditor->setSkippedLines(blockNumber, skippedLines);
|
|
|
|
|
m_rightEditor->setSkippedLines(blockNumber, skippedLines);
|
|
|
|
|
leftText += separator;
|
|
|
|
|
rightText += separator;
|
|
|
|
|
blockNumber++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_leftEditor->setChunkIndex(blockNumber, chunkData.rows.count(), j);
|
|
|
|
|
m_rightEditor->setChunkIndex(blockNumber, chunkData.rows.count(), j);
|
|
|
|
|
|
|
|
|
|
for (int k = 0; k < chunkData.rows.count(); k++) {
|
|
|
|
|
RowData rowData = chunkData.rows.at(k);
|
|
|
|
|
TextLineData leftLineData = rowData.leftLine;
|
|
|
|
|
TextLineData rightLineData = rowData.rightLine;
|
|
|
|
|
if (leftLineData.textLineType == TextLineData::TextLine) {
|
|
|
|
|
leftText += leftLineData.text;
|
|
|
|
|
lastLeftLineNumber = leftLineNumber;
|
|
|
|
|
leftLineNumber++;
|
|
|
|
|
m_leftEditor->setLineNumber(blockNumber, leftLineNumber);
|
|
|
|
|
} else if (leftLineData.textLineType == TextLineData::Separator) {
|
|
|
|
|
m_leftEditor->setSeparator(blockNumber, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rightLineData.textLineType == TextLineData::TextLine) {
|
|
|
|
|
rightText += rightLineData.text;
|
|
|
|
|
rightLineNumber++;
|
|
|
|
|
m_rightEditor->setLineNumber(blockNumber, rightLineNumber);
|
|
|
|
|
} else if (rightLineData.textLineType == TextLineData::Separator) {
|
|
|
|
|
m_rightEditor->setSeparator(blockNumber, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!rowData.equal) {
|
|
|
|
|
if (rowData.leftLine.textLineType == TextLineData::TextLine)
|
|
|
|
|
leftFormats[blockNumber].append(DiffSelection(&m_leftLineFormat));
|
|
|
|
|
else
|
|
|
|
|
leftFormats[blockNumber].append(DiffSelection(&m_spanLineFormat));
|
|
|
|
|
if (rowData.rightLine.textLineType == TextLineData::TextLine)
|
|
|
|
|
rightFormats[blockNumber].append(DiffSelection(&m_rightLineFormat));
|
|
|
|
|
else
|
|
|
|
|
rightFormats[blockNumber].append(DiffSelection(&m_spanLineFormat));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QMapIterator<int, int> itLeft(leftLineData.changedPositions);
|
|
|
|
|
while (itLeft.hasNext()) {
|
|
|
|
|
itLeft.next();
|
|
|
|
|
leftFormats[blockNumber].append(
|
|
|
|
|
DiffSelection(itLeft.key(), itLeft.value(),
|
|
|
|
|
&m_leftCharFormat));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QMapIterator<int, int> itRight(rightLineData.changedPositions);
|
|
|
|
|
while (itRight.hasNext()) {
|
|
|
|
|
itRight.next();
|
|
|
|
|
rightFormats[blockNumber].append(
|
|
|
|
|
DiffSelection(itRight.key(), itRight.value(),
|
|
|
|
|
&m_rightCharFormat));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
leftText += separator;
|
|
|
|
|
rightText += separator;
|
|
|
|
|
blockNumber++;
|
|
|
|
|
}
|
2013-04-25 17:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
if (j == contextFileData.chunks.count() - 1) { // the last chunk
|
|
|
|
|
int skippedLines = -2;
|
|
|
|
|
if (chunkData.contextChunk) {
|
|
|
|
|
// if it's context chunk
|
|
|
|
|
skippedLines = chunkData.rows.count();
|
|
|
|
|
} else if (!contextFileData.lastChunkAtTheEndOfFile
|
|
|
|
|
&& !contextFileData.contextChunksIncluded) {
|
|
|
|
|
// if not a context chunk and not a chunk at the end of file
|
|
|
|
|
// and context lines not included
|
|
|
|
|
skippedLines = -1; // unknown count skipped by the end of file
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (skippedLines >= -1) {
|
|
|
|
|
leftFormats[blockNumber].append(DiffSelection(&m_chunkLineFormat));
|
|
|
|
|
rightFormats[blockNumber].append(DiffSelection(&m_chunkLineFormat));
|
|
|
|
|
m_leftEditor->setSkippedLines(blockNumber, skippedLines);
|
|
|
|
|
m_rightEditor->setSkippedLines(blockNumber, skippedLines);
|
|
|
|
|
leftText += separator;
|
|
|
|
|
rightText += separator;
|
|
|
|
|
blockNumber++;
|
|
|
|
|
} // otherwise nothing skipped
|
2013-04-25 17:37:20 +02:00
|
|
|
}
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-04-16 14:26:07 +02:00
|
|
|
leftText.replace(QLatin1Char('\r'), QLatin1Char(' '));
|
|
|
|
|
rightText.replace(QLatin1Char('\r'), QLatin1Char(' '));
|
2013-08-14 13:52:13 +02:00
|
|
|
leftTexts += leftText;
|
|
|
|
|
rightTexts += rightText;
|
2014-02-13 16:43:28 +01:00
|
|
|
// leftDocs.append(qMakePair(contextFileData.leftFileInfo, leftText));
|
|
|
|
|
// rightDocs.append(qMakePair(contextFileData.rightFileInfo, rightText));
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
2013-05-07 14:02:08 +02:00
|
|
|
|
2013-08-14 13:52:13 +02:00
|
|
|
if (leftTexts.isEmpty() && rightTexts.isEmpty())
|
2013-05-07 14:02:08 +02:00
|
|
|
return;
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
// m_leftEditor->setDocuments(leftDocs);
|
|
|
|
|
// m_rightEditor->setDocuments(rightDocs);
|
2013-08-14 13:52:13 +02:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
const bool oldIgnore = m_ignoreCurrentIndexChange;
|
|
|
|
|
m_ignoreCurrentIndexChange = true;
|
|
|
|
|
m_leftEditor->clear();
|
2013-08-14 13:52:13 +02:00
|
|
|
m_leftEditor->setPlainText(leftTexts);
|
2014-02-13 16:43:28 +01:00
|
|
|
m_rightEditor->clear();
|
2013-08-14 13:52:13 +02:00
|
|
|
m_rightEditor->setPlainText(rightTexts);
|
2014-02-13 16:43:28 +01:00
|
|
|
m_ignoreCurrentIndexChange = oldIgnore;
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
m_leftEditor->setSelections(leftFormats);
|
|
|
|
|
m_rightEditor->setSelections(rightFormats);
|
2013-04-09 10:26:31 +02:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
|
|
|
|
|
/*
|
2013-04-25 17:37:20 +02:00
|
|
|
QTextBlock leftBlock = m_leftEditor->document()->firstBlock();
|
|
|
|
|
QTextBlock rightBlock = m_rightEditor->document()->firstBlock();
|
|
|
|
|
for (int i = 0; i < m_contextFileData.count(); i++) {
|
|
|
|
|
const FileData &contextFileData = m_contextFileData.at(i);
|
|
|
|
|
leftBlock = leftBlock.next();
|
|
|
|
|
rightBlock = rightBlock.next();
|
|
|
|
|
for (int j = 0; j < contextFileData.chunks.count(); j++) {
|
|
|
|
|
ChunkData chunkData = contextFileData.chunks.at(j);
|
|
|
|
|
if (chunkData.contextChunk) {
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::setFoldingIndent(leftBlock, FILE_LEVEL);
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::setFoldingIndent(rightBlock, FILE_LEVEL);
|
2013-04-09 10:26:31 +02:00
|
|
|
leftBlock = leftBlock.next();
|
2013-04-25 17:37:20 +02:00
|
|
|
rightBlock = rightBlock.next();
|
2013-04-09 10:26:31 +02:00
|
|
|
}
|
2013-04-25 17:37:20 +02:00
|
|
|
const int indent = chunkData.contextChunk ? CHUNK_LEVEL : FILE_LEVEL;
|
|
|
|
|
for (int k = 0; k < chunkData.rows.count(); k++) {
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::setFoldingIndent(leftBlock, indent);
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::setFoldingIndent(rightBlock, indent);
|
|
|
|
|
leftBlock = leftBlock.next();
|
2013-04-09 10:26:31 +02:00
|
|
|
rightBlock = rightBlock.next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-10 13:51:04 +02:00
|
|
|
blockNumber = 0;
|
2013-04-25 17:37:20 +02:00
|
|
|
for (int i = 0; i < m_contextFileData.count(); i++) {
|
|
|
|
|
const FileData &contextFileData = m_contextFileData.at(i);
|
|
|
|
|
blockNumber++;
|
|
|
|
|
for (int j = 0; j < contextFileData.chunks.count(); j++) {
|
|
|
|
|
ChunkData chunkData = contextFileData.chunks.at(j);
|
|
|
|
|
if (chunkData.contextChunk) {
|
|
|
|
|
QTextBlock leftBlock = m_leftEditor->document()->findBlockByNumber(blockNumber);
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::doFoldOrUnfold(leftBlock, false);
|
|
|
|
|
QTextBlock rightBlock = m_rightEditor->document()->findBlockByNumber(blockNumber);
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::doFoldOrUnfold(rightBlock, false);
|
|
|
|
|
blockNumber++;
|
|
|
|
|
}
|
|
|
|
|
blockNumber += chunkData.rows.count();
|
2013-04-10 13:51:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_foldingBlocker = true;
|
2013-04-09 10:26:31 +02:00
|
|
|
BaseTextDocumentLayout *leftLayout = qobject_cast<BaseTextDocumentLayout *>(m_leftEditor->document()->documentLayout());
|
|
|
|
|
if (leftLayout) {
|
|
|
|
|
leftLayout->requestUpdate();
|
|
|
|
|
leftLayout->emitDocumentSizeChanged();
|
|
|
|
|
}
|
|
|
|
|
BaseTextDocumentLayout *rightLayout = qobject_cast<BaseTextDocumentLayout *>(m_rightEditor->document()->documentLayout());
|
|
|
|
|
if (rightLayout) {
|
|
|
|
|
rightLayout->requestUpdate();
|
|
|
|
|
rightLayout->emitDocumentSizeChanged();
|
|
|
|
|
}
|
2013-04-10 13:51:04 +02:00
|
|
|
m_foldingBlocker = false;
|
2014-02-13 16:43:28 +01:00
|
|
|
*/
|
2013-02-15 12:49:50 +01:00
|
|
|
m_leftEditor->verticalScrollBar()->setValue(verticalValue);
|
|
|
|
|
m_rightEditor->verticalScrollBar()->setValue(verticalValue);
|
|
|
|
|
m_leftEditor->horizontalScrollBar()->setValue(leftHorizontalValue);
|
|
|
|
|
m_rightEditor->horizontalScrollBar()->setValue(rightHorizontalValue);
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
// m_leftEditor->updateFoldingHighlight(QPoint(-1, -1));
|
|
|
|
|
// m_rightEditor->updateFoldingHighlight(QPoint(-1, -1));
|
2013-06-05 15:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideBySideDiffEditorWidget::setFontSettings(
|
|
|
|
|
const TextEditor::FontSettings &fontSettings)
|
2013-06-05 15:25:42 +02:00
|
|
|
{
|
2014-08-01 23:31:56 +02:00
|
|
|
m_leftEditor->textDocument()->setFontSettings(fontSettings);
|
|
|
|
|
m_rightEditor->textDocument()->setFontSettings(fontSettings);
|
2013-06-05 15:25:42 +02:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
m_spanLineFormat = fontSettings.toTextCharFormat(C_LINE_NUMBER);
|
|
|
|
|
m_fileLineFormat = fontSettings.toTextCharFormat(C_DIFF_FILE_LINE);
|
|
|
|
|
m_chunkLineFormat = fontSettings.toTextCharFormat(C_DIFF_CONTEXT_LINE);
|
|
|
|
|
m_leftLineFormat = fontSettings.toTextCharFormat(C_DIFF_SOURCE_LINE);
|
|
|
|
|
m_leftCharFormat = fontSettings.toTextCharFormat(C_DIFF_SOURCE_CHAR);
|
|
|
|
|
m_rightLineFormat = fontSettings.toTextCharFormat(C_DIFF_DEST_LINE);
|
|
|
|
|
m_rightCharFormat = fontSettings.toTextCharFormat(C_DIFF_DEST_CHAR);
|
2013-06-05 15:25:42 +02:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
m_leftEditor->update();
|
|
|
|
|
m_rightEditor->update();
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideBySideDiffEditorWidget::slotLeftJumpToOriginalFileRequested(
|
|
|
|
|
int diffFileIndex,
|
|
|
|
|
int lineNumber,
|
|
|
|
|
int columnNumber)
|
2013-07-01 13:15:27 +02:00
|
|
|
{
|
|
|
|
|
if (diffFileIndex < 0 || diffFileIndex >= m_contextFileData.count())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const FileData fileData = m_contextFileData.at(diffFileIndex);
|
|
|
|
|
const QString leftFileName = fileData.leftFileInfo.fileName;
|
|
|
|
|
const QString rightFileName = fileData.rightFileInfo.fileName;
|
|
|
|
|
if (leftFileName == rightFileName) {
|
|
|
|
|
// The same file (e.g. in git diff), jump to the line number taken from the right editor.
|
|
|
|
|
// Warning: git show SHA^ vs SHA or git diff HEAD vs Index
|
|
|
|
|
// (when Working tree has changed in meantime) will not work properly.
|
|
|
|
|
for (int i = 0; i < fileData.chunks.count(); i++) {
|
|
|
|
|
const ChunkData chunkData = fileData.chunks.at(i);
|
2014-07-04 09:15:01 +02:00
|
|
|
|
|
|
|
|
int leftLineNumber = chunkData.leftStartingLineNumber;
|
|
|
|
|
int rightLineNumber = chunkData.rightStartingLineNumber;
|
|
|
|
|
|
2013-07-01 13:15:27 +02:00
|
|
|
for (int j = 0; j < chunkData.rows.count(); j++) {
|
|
|
|
|
const RowData rowData = chunkData.rows.at(j);
|
|
|
|
|
if (rowData.leftLine.textLineType == TextLineData::TextLine)
|
|
|
|
|
leftLineNumber++;
|
|
|
|
|
if (rowData.rightLine.textLineType == TextLineData::TextLine)
|
|
|
|
|
rightLineNumber++;
|
|
|
|
|
if (leftLineNumber == lineNumber) {
|
2014-02-27 12:22:47 +01:00
|
|
|
int colNr = rowData.equal ? columnNumber : 0;
|
2013-07-01 13:15:27 +02:00
|
|
|
jumpToOriginalFile(leftFileName, rightLineNumber, colNr);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// different file (e.g. in Tools | Diff...)
|
|
|
|
|
jumpToOriginalFile(leftFileName, lineNumber, columnNumber);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideBySideDiffEditorWidget::slotRightJumpToOriginalFileRequested(
|
|
|
|
|
int diffFileIndex,
|
|
|
|
|
int lineNumber,
|
|
|
|
|
int columnNumber)
|
2013-07-01 13:15:27 +02:00
|
|
|
{
|
|
|
|
|
if (diffFileIndex < 0 || diffFileIndex >= m_contextFileData.count())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const FileData fileData = m_contextFileData.at(diffFileIndex);
|
|
|
|
|
const QString fileName = fileData.rightFileInfo.fileName;
|
|
|
|
|
jumpToOriginalFile(fileName, lineNumber, columnNumber);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::jumpToOriginalFile(const QString &fileName,
|
2013-07-01 13:15:27 +02:00
|
|
|
int lineNumber, int columnNumber)
|
|
|
|
|
{
|
2013-12-16 16:19:40 +01:00
|
|
|
if (!m_controller)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QDir dir(m_controller->workingDirectory());
|
2013-07-01 13:15:27 +02:00
|
|
|
const QString absoluteFileName = dir.absoluteFilePath(fileName);
|
2014-06-18 12:04:14 +03:00
|
|
|
QFileInfo fi(absoluteFileName);
|
|
|
|
|
if (fi.exists() && !fi.isDir())
|
|
|
|
|
Core::EditorManager::openEditorAt(absoluteFileName, lineNumber, columnNumber);
|
2013-07-01 13:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideBySideDiffEditorWidget::slotLeftContextMenuRequested(QMenu *menu,
|
|
|
|
|
int diffFileIndex,
|
|
|
|
|
int chunkIndex)
|
|
|
|
|
{
|
|
|
|
|
menu->addSeparator();
|
|
|
|
|
QAction *sendChunkToCodePasterAction =
|
|
|
|
|
menu->addAction(tr("Send Chunk to CodePaster..."));
|
|
|
|
|
connect(sendChunkToCodePasterAction, SIGNAL(triggered()),
|
|
|
|
|
this, SLOT(slotSendChunkToCodePaster()));
|
|
|
|
|
menu->addSeparator();
|
|
|
|
|
QAction *applyAction = menu->addAction(tr("Apply Chunk..."));
|
|
|
|
|
connect(applyAction, SIGNAL(triggered()), this, SLOT(slotApplyChunk()));
|
|
|
|
|
applyAction->setEnabled(false);
|
|
|
|
|
|
|
|
|
|
m_contextMenuFileIndex = diffFileIndex;
|
|
|
|
|
m_contextMenuChunkIndex = chunkIndex;
|
|
|
|
|
|
|
|
|
|
if (m_contextMenuFileIndex < 0 || m_contextMenuChunkIndex < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (m_contextMenuFileIndex >= m_contextFileData.count())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const FileData fileData = m_contextFileData.at(m_contextMenuFileIndex);
|
|
|
|
|
if (m_contextMenuChunkIndex >= fileData.chunks.count())
|
|
|
|
|
return;
|
|
|
|
|
|
2014-06-06 14:35:31 +02:00
|
|
|
m_controller->requestChunkActions(menu, diffFileIndex, chunkIndex);
|
2014-02-13 16:43:28 +01:00
|
|
|
|
|
|
|
|
if (fileData.leftFileInfo.fileName == fileData.rightFileInfo.fileName)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
applyAction->setEnabled(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SideBySideDiffEditorWidget::slotRightContextMenuRequested(QMenu *menu,
|
|
|
|
|
int diffFileIndex,
|
|
|
|
|
int chunkIndex)
|
|
|
|
|
{
|
|
|
|
|
menu->addSeparator();
|
|
|
|
|
QAction *sendChunkToCodePasterAction =
|
|
|
|
|
menu->addAction(tr("Send Chunk to CodePaster..."));
|
|
|
|
|
connect(sendChunkToCodePasterAction, SIGNAL(triggered()),
|
|
|
|
|
this, SLOT(slotSendChunkToCodePaster()));
|
|
|
|
|
menu->addSeparator();
|
|
|
|
|
QAction *revertAction = menu->addAction(tr("Revert Chunk..."));
|
|
|
|
|
connect(revertAction, SIGNAL(triggered()), this, SLOT(slotRevertChunk()));
|
|
|
|
|
revertAction->setEnabled(false);
|
|
|
|
|
|
|
|
|
|
m_contextMenuFileIndex = diffFileIndex;
|
|
|
|
|
m_contextMenuChunkIndex = chunkIndex;
|
|
|
|
|
|
|
|
|
|
if (m_contextMenuFileIndex < 0 || m_contextMenuChunkIndex < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (m_contextMenuFileIndex >= m_contextFileData.count())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const FileData fileData = m_contextFileData.at(m_contextMenuFileIndex);
|
|
|
|
|
if (m_contextMenuChunkIndex >= fileData.chunks.count())
|
|
|
|
|
return;
|
|
|
|
|
|
2014-06-06 14:35:31 +02:00
|
|
|
m_controller->requestChunkActions(menu, diffFileIndex, chunkIndex);
|
2014-02-13 16:43:28 +01:00
|
|
|
|
|
|
|
|
revertAction->setEnabled(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SideBySideDiffEditorWidget::slotSendChunkToCodePaster()
|
|
|
|
|
{
|
|
|
|
|
if (!m_controller)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (m_contextMenuFileIndex < 0 || m_contextMenuChunkIndex < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (m_contextMenuFileIndex >= m_contextFileData.count())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const FileData fileData = m_contextFileData.at(m_contextMenuFileIndex);
|
|
|
|
|
if (m_contextMenuChunkIndex >= fileData.chunks.count())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QString patch = m_controller->makePatch(m_contextMenuFileIndex,
|
|
|
|
|
m_contextMenuChunkIndex,
|
|
|
|
|
false);
|
|
|
|
|
if (patch.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Retrieve service by soft dependency.
|
|
|
|
|
QObject *pasteService =
|
|
|
|
|
ExtensionSystem::PluginManager::getObjectByClassName(
|
|
|
|
|
QLatin1String("CodePaster::CodePasterService"));
|
|
|
|
|
if (pasteService) {
|
|
|
|
|
QMetaObject::invokeMethod(pasteService, "postText",
|
|
|
|
|
Q_ARG(QString, patch),
|
|
|
|
|
Q_ARG(QString, QLatin1String(DiffEditor::Constants::DIFF_EDITOR_MIMETYPE)));
|
|
|
|
|
} else {
|
|
|
|
|
QMessageBox::information(this, tr("Unable to Paste"),
|
|
|
|
|
tr("Code pasting services are not available."));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SideBySideDiffEditorWidget::slotApplyChunk()
|
|
|
|
|
{
|
|
|
|
|
patch(m_contextMenuFileIndex, m_contextMenuChunkIndex, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SideBySideDiffEditorWidget::slotRevertChunk()
|
|
|
|
|
{
|
|
|
|
|
patch(m_contextMenuFileIndex, m_contextMenuChunkIndex, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SideBySideDiffEditorWidget::patch(int diffFileIndex, int chunkIndex, bool revert)
|
|
|
|
|
{
|
|
|
|
|
if (!m_controller)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (diffFileIndex < 0 || chunkIndex < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (diffFileIndex >= m_contextFileData.count())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const FileData fileData = m_contextFileData.at(diffFileIndex);
|
|
|
|
|
if (chunkIndex >= fileData.chunks.count())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QString title = revert ? tr("Revert Chunk") : tr("Apply Chunk");
|
|
|
|
|
const QString question = revert
|
|
|
|
|
? tr("Would you like to revert the chunk?")
|
|
|
|
|
: tr("Would you like to apply the chunk?");
|
|
|
|
|
if (QMessageBox::No == QMessageBox::question(this, title,
|
|
|
|
|
question,
|
|
|
|
|
QMessageBox::Yes
|
|
|
|
|
| QMessageBox::No)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int strip = m_controller->workingDirectory().isEmpty() ? -1 : 0;
|
|
|
|
|
|
|
|
|
|
const QString fileName = revert
|
|
|
|
|
? fileData.rightFileInfo.fileName
|
|
|
|
|
: fileData.leftFileInfo.fileName;
|
|
|
|
|
|
|
|
|
|
const QString workingDirectory = m_controller->workingDirectory().isEmpty()
|
|
|
|
|
? QFileInfo(fileName).absolutePath()
|
|
|
|
|
: m_controller->workingDirectory();
|
|
|
|
|
|
|
|
|
|
const QString patch = m_controller->makePatch(diffFileIndex, chunkIndex, revert);
|
|
|
|
|
|
|
|
|
|
if (patch.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (PatchTool::runPatch(Core::EditorManager::defaultTextCodec()->fromUnicode(patch),
|
|
|
|
|
workingDirectory, strip, revert))
|
|
|
|
|
m_controller->requestReload();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::leftVSliderChanged()
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
m_rightEditor->verticalScrollBar()->setValue(
|
|
|
|
|
m_leftEditor->verticalScrollBar()->value());
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::rightVSliderChanged()
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
m_leftEditor->verticalScrollBar()->setValue(
|
|
|
|
|
m_rightEditor->verticalScrollBar()->value());
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::leftHSliderChanged()
|
2013-05-22 16:33:44 +02:00
|
|
|
{
|
2014-02-24 11:10:17 +01:00
|
|
|
if (!m_guiController || m_guiController->horizontalScrollBarSynchronization())
|
2014-02-13 16:43:28 +01:00
|
|
|
m_rightEditor->horizontalScrollBar()->setValue(
|
|
|
|
|
m_leftEditor->horizontalScrollBar()->value());
|
2013-05-22 16:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::rightHSliderChanged()
|
2013-05-22 16:33:44 +02:00
|
|
|
{
|
2014-02-24 11:10:17 +01:00
|
|
|
if (!m_guiController || m_guiController->horizontalScrollBarSynchronization())
|
2014-02-13 16:43:28 +01:00
|
|
|
m_leftEditor->horizontalScrollBar()->setValue(
|
|
|
|
|
m_rightEditor->horizontalScrollBar()->value());
|
2013-05-22 16:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::leftCursorPositionChanged()
|
2013-05-23 13:36:27 +02:00
|
|
|
{
|
|
|
|
|
leftVSliderChanged();
|
|
|
|
|
leftHSliderChanged();
|
2013-12-16 16:19:40 +01:00
|
|
|
|
2014-02-24 11:10:17 +01:00
|
|
|
if (!m_guiController)
|
2013-12-16 16:19:40 +01:00
|
|
|
return;
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
if (m_ignoreCurrentIndexChange)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const bool oldIgnore = m_ignoreCurrentIndexChange;
|
|
|
|
|
m_ignoreCurrentIndexChange = true;
|
|
|
|
|
m_guiController->setCurrentDiffFileIndex(
|
|
|
|
|
m_leftEditor->fileIndexForBlockNumber(
|
|
|
|
|
m_leftEditor->textCursor().blockNumber()));
|
|
|
|
|
m_ignoreCurrentIndexChange = oldIgnore;
|
2013-05-23 13:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::rightCursorPositionChanged()
|
2013-05-23 13:36:27 +02:00
|
|
|
{
|
|
|
|
|
rightVSliderChanged();
|
|
|
|
|
rightHSliderChanged();
|
2013-12-16 16:19:40 +01:00
|
|
|
|
2014-02-24 11:10:17 +01:00
|
|
|
if (!m_guiController)
|
2013-12-16 16:19:40 +01:00
|
|
|
return;
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
if (m_ignoreCurrentIndexChange)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const bool oldIgnore = m_ignoreCurrentIndexChange;
|
|
|
|
|
m_ignoreCurrentIndexChange = true;
|
|
|
|
|
m_guiController->setCurrentDiffFileIndex(
|
|
|
|
|
m_rightEditor->fileIndexForBlockNumber(
|
|
|
|
|
m_rightEditor->textCursor().blockNumber()));
|
|
|
|
|
m_ignoreCurrentIndexChange = oldIgnore;
|
2013-05-23 13:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
#if 0
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::leftDocumentSizeChanged()
|
2013-04-10 13:51:04 +02:00
|
|
|
{
|
|
|
|
|
synchronizeFoldings(m_leftEditor, m_rightEditor);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::rightDocumentSizeChanged()
|
2013-04-10 13:51:04 +02:00
|
|
|
{
|
|
|
|
|
synchronizeFoldings(m_rightEditor, m_leftEditor);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-25 17:37:20 +02:00
|
|
|
/* Special version of that method (original: TextEditor::BaseTextDocumentLayout::doFoldOrUnfold())
|
|
|
|
|
The hack lies in fact, that when unfolding all direct sub-blocks are made visible,
|
|
|
|
|
while some of them need to stay invisible (i.e. unfolded chunk lines)
|
|
|
|
|
*/
|
2014-01-30 14:27:50 +01:00
|
|
|
static void doFoldOrUnfold(SideDiffEditorWidget *editor, const QTextBlock &block, bool unfold)
|
2013-04-25 17:37:20 +02:00
|
|
|
{
|
|
|
|
|
if (!TextEditor::BaseTextDocumentLayout::canFold(block))
|
|
|
|
|
return;
|
|
|
|
|
QTextBlock b = block.next();
|
|
|
|
|
|
|
|
|
|
int indent = TextEditor::BaseTextDocumentLayout::foldingIndent(block);
|
|
|
|
|
while (b.isValid() && TextEditor::BaseTextDocumentLayout::foldingIndent(b) > indent && (unfold || b.next().isValid())) {
|
|
|
|
|
if (unfold && editor->isChunkLine(b.blockNumber()) && !TextEditor::BaseTextDocumentLayout::isFolded(b)) {
|
|
|
|
|
b.setVisible(false);
|
|
|
|
|
b.setLineCount(0);
|
|
|
|
|
} else {
|
|
|
|
|
b.setVisible(unfold);
|
|
|
|
|
b.setLineCount(unfold ? qMax(1, b.layout()->lineCount()) : 0);
|
|
|
|
|
}
|
|
|
|
|
if (unfold) { // do not unfold folded sub-blocks
|
|
|
|
|
if (TextEditor::BaseTextDocumentLayout::isFolded(b) && b.next().isValid()) {
|
|
|
|
|
int jndent = TextEditor::BaseTextDocumentLayout::foldingIndent(b);
|
|
|
|
|
b = b.next();
|
|
|
|
|
while (b.isValid() && TextEditor::BaseTextDocumentLayout::foldingIndent(b) > jndent)
|
|
|
|
|
b = b.next();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
b = b.next();
|
|
|
|
|
}
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::setFolded(block, !unfold);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::synchronizeFoldings(SideDiffEditorWidget *source, SideDiffEditorWidget *destination)
|
2013-04-10 13:51:04 +02:00
|
|
|
{
|
|
|
|
|
if (m_foldingBlocker)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_foldingBlocker = true;
|
|
|
|
|
QTextBlock sourceBlock = source->document()->firstBlock();
|
|
|
|
|
QTextBlock destinationBlock = destination->document()->firstBlock();
|
|
|
|
|
while (sourceBlock.isValid() && destinationBlock.isValid()) {
|
|
|
|
|
if (TextEditor::BaseTextDocumentLayout::canFold(sourceBlock)) {
|
|
|
|
|
const bool isSourceFolded = TextEditor::BaseTextDocumentLayout::isFolded(sourceBlock);
|
|
|
|
|
const bool isDestinationFolded = TextEditor::BaseTextDocumentLayout::isFolded(destinationBlock);
|
|
|
|
|
if (isSourceFolded != isDestinationFolded) {
|
2013-04-25 17:37:20 +02:00
|
|
|
if (source->isFileLine(sourceBlock.blockNumber())) {
|
|
|
|
|
doFoldOrUnfold(source, sourceBlock, !isSourceFolded);
|
|
|
|
|
doFoldOrUnfold(destination, destinationBlock, !isSourceFolded);
|
|
|
|
|
} else {
|
|
|
|
|
if (isSourceFolded) { // we fold the destination (shrinking)
|
|
|
|
|
QTextBlock previousSource = sourceBlock.previous(); // skippedLines
|
|
|
|
|
QTextBlock previousDestination = destinationBlock.previous(); // skippedLines
|
|
|
|
|
if (source->isChunkLine(previousSource.blockNumber())) {
|
|
|
|
|
QTextBlock firstVisibleDestinationBlock = destination->firstVisibleBlock();
|
|
|
|
|
QTextBlock firstDestinationBlock = destination->document()->firstBlock();
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::doFoldOrUnfold(destinationBlock, !isSourceFolded);
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::setFoldingIndent(sourceBlock, CHUNK_LEVEL);
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::setFoldingIndent(destinationBlock, CHUNK_LEVEL);
|
|
|
|
|
previousSource.setVisible(true);
|
|
|
|
|
previousSource.setLineCount(1);
|
|
|
|
|
previousDestination.setVisible(true);
|
|
|
|
|
previousDestination.setLineCount(1);
|
|
|
|
|
sourceBlock.setVisible(false);
|
|
|
|
|
sourceBlock.setLineCount(0);
|
|
|
|
|
destinationBlock.setVisible(false);
|
|
|
|
|
destinationBlock.setLineCount(0);
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::setFolded(previousSource, true);
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::setFolded(previousDestination, true);
|
|
|
|
|
|
|
|
|
|
if (firstVisibleDestinationBlock == destinationBlock) {
|
|
|
|
|
/*
|
|
|
|
|
The following hack is completely crazy. That's the only way to scroll 1 line up
|
|
|
|
|
in case destinationBlock was the top visible block.
|
|
|
|
|
There is no need to scroll the source since this is in sync anyway
|
|
|
|
|
(leftSliderChanged(), rightSliderChanged())
|
|
|
|
|
*/
|
|
|
|
|
destination->verticalScrollBar()->setValue(destination->verticalScrollBar()->value() - 1);
|
|
|
|
|
destination->verticalScrollBar()->setValue(destination->verticalScrollBar()->value() + 1);
|
|
|
|
|
if (firstVisibleDestinationBlock.previous() == firstDestinationBlock) {
|
|
|
|
|
/*
|
|
|
|
|
Even more crazy case: the destinationBlock was the first top visible block.
|
|
|
|
|
*/
|
|
|
|
|
destination->verticalScrollBar()->setValue(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else { // we unfold the destination (expanding)
|
|
|
|
|
if (source->isChunkLine(sourceBlock.blockNumber())) {
|
|
|
|
|
QTextBlock nextSource = sourceBlock.next();
|
|
|
|
|
QTextBlock nextDestination = destinationBlock.next();
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::doFoldOrUnfold(destinationBlock, !isSourceFolded);
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::setFoldingIndent(nextSource, FILE_LEVEL);
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::setFoldingIndent(nextDestination, FILE_LEVEL);
|
|
|
|
|
sourceBlock.setVisible(false);
|
|
|
|
|
sourceBlock.setLineCount(0);
|
|
|
|
|
destinationBlock.setVisible(false);
|
|
|
|
|
destinationBlock.setLineCount(0);
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::setFolded(nextSource, false);
|
|
|
|
|
TextEditor::BaseTextDocumentLayout::setFolded(nextDestination, false);
|
2013-04-10 13:51:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break; // only one should be synchronized
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sourceBlock = sourceBlock.next();
|
|
|
|
|
destinationBlock = destinationBlock.next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BaseTextDocumentLayout *sourceLayout = qobject_cast<BaseTextDocumentLayout *>(source->document()->documentLayout());
|
|
|
|
|
if (sourceLayout) {
|
|
|
|
|
sourceLayout->requestUpdate();
|
|
|
|
|
sourceLayout->emitDocumentSizeChanged();
|
|
|
|
|
}
|
2013-05-22 15:11:35 +02:00
|
|
|
|
|
|
|
|
QWidget *ea = source->extraArea();
|
|
|
|
|
if (ea->contentsRect().contains(ea->mapFromGlobal(QCursor::pos())))
|
|
|
|
|
source->updateFoldingHighlight(source->mapFromGlobal(QCursor::pos()));
|
2013-04-10 13:51:04 +02:00
|
|
|
|
|
|
|
|
BaseTextDocumentLayout *destinationLayout = qobject_cast<BaseTextDocumentLayout *>(destination->document()->documentLayout());
|
|
|
|
|
if (destinationLayout) {
|
|
|
|
|
destinationLayout->requestUpdate();
|
|
|
|
|
destinationLayout->emitDocumentSizeChanged();
|
|
|
|
|
}
|
|
|
|
|
m_foldingBlocker = false;
|
|
|
|
|
}
|
2014-02-13 16:43:28 +01:00
|
|
|
#endif
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-02-21 17:03:00 +01:00
|
|
|
} // namespace DiffEditor
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
#include "sidebysidediffeditorwidget.moc"
|