From 2a9c9f2d707d37e9cac8712aeac049c35fd820e7 Mon Sep 17 00:00:00 2001 From: jkobus Date: Fri, 24 Oct 2014 14:11:12 +0200 Subject: [PATCH] Hide some diff editor controls when diff editor is bound to a file. Remove unnecessary anymore "ignoreWhitespace" argument from functions which read a patch file. Transfer the ownership of reloader into controller. Task-number: QTCREATORBUG-13250 Change-Id: I68183005b845d6ece9ea2be9888abc8597310426 Reviewed-by: Tobias Hunger --- src/plugins/diffeditor/diffeditor.cpp | 9 ++-- src/plugins/diffeditor/diffeditor.h | 3 ++ .../diffeditor/diffeditorcontroller.cpp | 8 ++-- src/plugins/diffeditor/diffeditordocument.cpp | 11 +++-- src/plugins/diffeditor/diffeditormanager.cpp | 9 ++++ src/plugins/diffeditor/diffeditormanager.h | 1 + src/plugins/diffeditor/diffeditorplugin.cpp | 15 +++---- src/plugins/diffeditor/diffeditorreloader.cpp | 5 +-- src/plugins/diffeditor/diffeditorreloader.h | 2 +- src/plugins/diffeditor/diffutils.cpp | 44 ++++--------------- src/plugins/diffeditor/diffutils.h | 1 - src/plugins/git/gitclient.cpp | 12 +++-- 12 files changed, 52 insertions(+), 68 deletions(-) diff --git a/src/plugins/diffeditor/diffeditor.cpp b/src/plugins/diffeditor/diffeditor.cpp index 5dac850ce8e..0d4412c1acf 100644 --- a/src/plugins/diffeditor/diffeditor.cpp +++ b/src/plugins/diffeditor/diffeditor.cpp @@ -322,12 +322,12 @@ QWidget *DiffEditor::toolBar() whitespaceButton->setText(tr("Ignore Whitespace")); whitespaceButton->setCheckable(true); whitespaceButton->setChecked(m_controller->isIgnoreWhitespace()); - m_toolBar->addWidget(whitespaceButton); + m_whitespaceButtonAction = m_toolBar->addWidget(whitespaceButton); QLabel *contextLabel = new QLabel(m_toolBar); contextLabel->setText(tr("Context Lines:")); contextLabel->setContentsMargins(6, 0, 6, 0); - m_toolBar->addWidget(contextLabel); + m_contextLabelAction = m_toolBar->addWidget(contextLabel); QSpinBox *contextSpinBox = new QSpinBox(m_toolBar); contextSpinBox->setRange(1, 100); @@ -335,7 +335,7 @@ QWidget *DiffEditor::toolBar() contextSpinBox->setFrame(false); contextSpinBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding); // Mac Qt5 - m_toolBar->addWidget(contextSpinBox); + m_contextSpinBoxAction = m_toolBar->addWidget(contextSpinBox); QToolButton *toggleDescription = new QToolButton(m_toolBar); toggleDescription->setIcon( @@ -497,6 +497,9 @@ void DiffEditor::slotDescriptionVisibilityChanged() void DiffEditor::slotReloaderChanged(DiffEditorReloader *reloader) { + m_whitespaceButtonAction->setVisible(reloader); + m_contextLabelAction->setVisible(reloader); + m_contextSpinBoxAction->setVisible(reloader); m_reloadAction->setVisible(reloader); } diff --git a/src/plugins/diffeditor/diffeditor.h b/src/plugins/diffeditor/diffeditor.h index c843fd33261..74ebb4bf1f8 100644 --- a/src/plugins/diffeditor/diffeditor.h +++ b/src/plugins/diffeditor/diffeditor.h @@ -104,6 +104,9 @@ private: DiffEditorGuiController *m_guiController; QToolBar *m_toolBar; QComboBox *m_entriesComboBox; + QAction *m_whitespaceButtonAction; + QAction *m_contextLabelAction; + QAction *m_contextSpinBoxAction; QAction *m_toggleDescriptionAction; QAction *m_reloadAction; QToolButton *m_diffEditorSwitcher; diff --git a/src/plugins/diffeditor/diffeditorcontroller.cpp b/src/plugins/diffeditor/diffeditorcontroller.cpp index 1a9e145fec4..5aeb8d27058 100644 --- a/src/plugins/diffeditor/diffeditorcontroller.cpp +++ b/src/plugins/diffeditor/diffeditorcontroller.cpp @@ -64,7 +64,7 @@ DiffEditorController::DiffEditorController(QObject *parent) DiffEditorController::~DiffEditorController() { - + delete m_reloader; } QString DiffEditorController::clearMessage() const @@ -137,20 +137,20 @@ DiffEditorReloader *DiffEditorController::reloader() const return m_reloader; } +// The ownership of reloader is passed to the controller void DiffEditorController::setReloader(DiffEditorReloader *reloader) { if (m_reloader == reloader) return; // nothing changes - if (m_reloader) - m_reloader->setController(0); + delete m_reloader; m_reloader = reloader; if (m_reloader) m_reloader->setController(this); - reloaderChanged(m_reloader); + emit reloaderChanged(m_reloader); } void DiffEditorController::clear() diff --git a/src/plugins/diffeditor/diffeditordocument.cpp b/src/plugins/diffeditor/diffeditordocument.cpp index 517717c92d9..db28ee601de 100644 --- a/src/plugins/diffeditor/diffeditordocument.cpp +++ b/src/plugins/diffeditor/diffeditordocument.cpp @@ -31,6 +31,8 @@ #include "diffeditordocument.h" #include "diffeditorconstants.h" #include "diffeditorcontroller.h" +#include "diffeditormanager.h" +#include "diffeditorreloader.h" #include "diffutils.h" #include @@ -83,6 +85,10 @@ bool DiffEditorDocument::save(QString *errorString, const QString &fileName, boo if (!ok) return false; + if (m_controller->reloader()) + m_controller->setReloader(0); + + DiffEditorManager::removeDocument(this); const QFileInfo fi(fileName); setTemporary(false); setFilePath(QDir::cleanPath(fi.absoluteFilePath())); @@ -105,10 +111,7 @@ bool DiffEditorDocument::open(QString *errorString, const QString &fileName) return false; bool ok = false; - QList fileDataList - = DiffUtils::readPatch(patch, - m_controller->isIgnoreWhitespace(), - &ok); + QList fileDataList = DiffUtils::readPatch(patch, &ok); if (!ok) { *errorString = tr("Could not parse patch file \"%1\". " "The content is not of unified diff format.") diff --git a/src/plugins/diffeditor/diffeditormanager.cpp b/src/plugins/diffeditor/diffeditormanager.cpp index ee883f93154..a8d306ad079 100644 --- a/src/plugins/diffeditor/diffeditormanager.cpp +++ b/src/plugins/diffeditor/diffeditormanager.cpp @@ -115,5 +115,14 @@ DiffEditorDocument *DiffEditorManager::findOrCreate(const QString &documentId, c return document; } +void DiffEditorManager::removeDocument(DiffEditorDocument *document) +{ + if (!instance()->documentToId.contains(document)) + return; + const QString documentId = instance()->documentToId.value(document); + instance()->documentToId.remove(document); + instance()->idToDocument.remove(documentId); +} + } // namespace DiffEditor diff --git a/src/plugins/diffeditor/diffeditormanager.h b/src/plugins/diffeditor/diffeditormanager.h index 9bc0d60e598..9a4be095818 100644 --- a/src/plugins/diffeditor/diffeditormanager.h +++ b/src/plugins/diffeditor/diffeditormanager.h @@ -53,6 +53,7 @@ public: static DiffEditorDocument *find(const QString &documentId); static DiffEditorDocument *findOrCreate(const QString &documentId, const QString &displayName); + static void removeDocument(DiffEditorDocument *document); private slots: void slotEditorsClosed(const QList &editors); diff --git a/src/plugins/diffeditor/diffeditorplugin.cpp b/src/plugins/diffeditor/diffeditorplugin.cpp index fc88e8fffd6..3c48515d105 100644 --- a/src/plugins/diffeditor/diffeditorplugin.cpp +++ b/src/plugins/diffeditor/diffeditorplugin.cpp @@ -54,8 +54,7 @@ class SimpleDiffEditorReloader : public DiffEditorReloader { Q_OBJECT public: - SimpleDiffEditorReloader(QObject *parent, - const QString &leftFileName, + SimpleDiffEditorReloader(const QString &leftFileName, const QString &rightFileName); protected: @@ -66,11 +65,9 @@ private: QString m_rightFileName; }; -SimpleDiffEditorReloader::SimpleDiffEditorReloader(QObject *parent, - const QString &leftFileName, +SimpleDiffEditorReloader::SimpleDiffEditorReloader(const QString &leftFileName, const QString &rightFileName) - : DiffEditorReloader(parent), - m_leftFileName(leftFileName), + : m_leftFileName(leftFileName), m_rightFileName(rightFileName) { } @@ -206,7 +203,7 @@ void DiffEditorPlugin::diff() DiffEditorController *controller = document->controller(); if (!controller->reloader()) { SimpleDiffEditorReloader *reloader = - new SimpleDiffEditorReloader(controller, fileName1, fileName2); + new SimpleDiffEditorReloader(fileName1, fileName2); controller->setReloader(reloader); } @@ -475,7 +472,7 @@ void DiffEditor::Internal::DiffEditorPlugin::testMakePatch() QCOMPARE(result, patchText); bool ok; - QList resultList = DiffUtils::readPatch(result, false, &ok); + QList resultList = DiffUtils::readPatch(result, &ok); QVERIFY(ok); QCOMPARE(resultList.count(), 1); @@ -905,7 +902,7 @@ void DiffEditor::Internal::DiffEditorPlugin::testReadPatch() QFETCH(QList, fileDataList); bool ok; - QList result = DiffUtils::readPatch(sourcePatch, false, &ok); + QList result = DiffUtils::readPatch(sourcePatch, &ok); QVERIFY(ok); QCOMPARE(fileDataList.count(), result.count()); diff --git a/src/plugins/diffeditor/diffeditorreloader.cpp b/src/plugins/diffeditor/diffeditorreloader.cpp index 78fd9bf8d1d..88bc93795a6 100644 --- a/src/plugins/diffeditor/diffeditorreloader.cpp +++ b/src/plugins/diffeditor/diffeditorreloader.cpp @@ -33,9 +33,8 @@ namespace DiffEditor { -DiffEditorReloader::DiffEditorReloader(QObject *parent) - : QObject(parent), - m_controller(0), +DiffEditorReloader::DiffEditorReloader() + : m_controller(0), m_reloading(false) { } diff --git a/src/plugins/diffeditor/diffeditorreloader.h b/src/plugins/diffeditor/diffeditorreloader.h index 918a213eeb0..11e593a214d 100644 --- a/src/plugins/diffeditor/diffeditorreloader.h +++ b/src/plugins/diffeditor/diffeditorreloader.h @@ -43,7 +43,7 @@ class DIFFEDITOR_EXPORT DiffEditorReloader : public QObject { Q_OBJECT public: - DiffEditorReloader(QObject *parent = 0); + DiffEditorReloader(); ~DiffEditorReloader(); bool isReloading() const; diff --git a/src/plugins/diffeditor/diffutils.cpp b/src/plugins/diffeditor/diffutils.cpp index 589c4ddb84e..3f9c43a20de 100644 --- a/src/plugins/diffeditor/diffutils.cpp +++ b/src/plugins/diffeditor/diffutils.cpp @@ -517,7 +517,6 @@ QString DiffUtils::makePatch(const QList &fileDataList) } static QList readLines(const QString &patch, - bool ignoreWhitespace, bool lastChunk, bool *lastChunkAtTheEndOfFile, bool *ok) @@ -678,28 +677,16 @@ static QList readLines(const QString &patch, QList outputLeftDiffList; QList outputRightDiffList; - if (ignoreWhitespace) { - const QList leftIntermediate = - Differ::moveWhitespaceIntoEqualities(leftDiffList); - const QList rightIntermediate = - Differ::moveWhitespaceIntoEqualities(rightDiffList); - Differ::ignoreWhitespaceBetweenEqualities(leftIntermediate, - rightIntermediate, - &outputLeftDiffList, - &outputRightDiffList); - } else { - Differ::diffBetweenEqualities(leftDiffList, - rightDiffList, - &outputLeftDiffList, - &outputRightDiffList); - } + Differ::diffBetweenEqualities(leftDiffList, + rightDiffList, + &outputLeftDiffList, + &outputRightDiffList); return DiffUtils::calculateOriginalData(outputLeftDiffList, outputRightDiffList).rows; } static QList readChunks(const QString &patch, - bool ignoreWhitespace, bool *lastChunkAtTheEndOfFile, bool *ok) { @@ -728,7 +715,6 @@ static QList readChunks(const QString &patch, const QString lines = patch.mid(endOfLastChunk, pos - endOfLastChunk); chunkDataList.last().rows = readLines(lines, - ignoreWhitespace, false, lastChunkAtTheEndOfFile, &readOk); @@ -747,7 +733,6 @@ static QList readChunks(const QString &patch, if (endOfLastChunk > 0) { const QString lines = patch.mid(endOfLastChunk); chunkDataList.last().rows = readLines(lines, - ignoreWhitespace, true, lastChunkAtTheEndOfFile, &readOk); @@ -761,7 +746,6 @@ static QList readChunks(const QString &patch, } static FileData readDiffHeaderAndChunks(const QString &headerAndChunks, - bool ignoreWhitespace, bool *ok) { QString patch = headerAndChunks; @@ -789,7 +773,6 @@ static FileData readDiffHeaderAndChunks(const QString &headerAndChunks, fileData.rightFileInfo.fileName = rightFileRegExp.cap(1); fileData.chunks = readChunks(patch, - ignoreWhitespace, &fileData.lastChunkAtTheEndOfFile, &readOk); } @@ -811,7 +794,6 @@ static FileData readDiffHeaderAndChunks(const QString &headerAndChunks, } static QList readDiffPatch(const QString &patch, - bool ignoreWhitespace, bool *ok) { const QRegExp diffRegExp(QLatin1String("(?:\\n|^)" // new line of the beginning of a patch @@ -844,7 +826,6 @@ static QList readDiffPatch(const QString &patch, pos - lastPos); const FileData fileData = readDiffHeaderAndChunks(headerAndChunks, - ignoreWhitespace, &readOk); if (!readOk) @@ -861,7 +842,6 @@ static QList readDiffPatch(const QString &patch, patch.count() - lastPos - 1); const FileData fileData = readDiffHeaderAndChunks(headerAndChunks, - ignoreWhitespace, &readOk); if (readOk) @@ -880,7 +860,6 @@ static QList readDiffPatch(const QString &patch, static FileData readGitHeaderAndChunks(const QString &headerAndChunks, const QString &fileName, - bool ignoreWhitespace, bool *ok) { FileData fileData; @@ -944,7 +923,6 @@ static FileData readGitHeaderAndChunks(const QString &headerAndChunks, patch.remove(0, rightFileRegExp.matchedLength()); fileData.chunks = readChunks(patch, - ignoreWhitespace, &fileData.lastChunkAtTheEndOfFile, &readOk); } @@ -966,7 +944,6 @@ static FileData readCopyRenameChunks(const QString ©RenameChunks, FileData::FileOperation fileOperation, const QString &leftFileName, const QString &rightFileName, - bool ignoreWhitespace, bool *ok) { FileData fileData; @@ -1005,7 +982,6 @@ static FileData readCopyRenameChunks(const QString ©RenameChunks, patch.remove(0, rightFileRegExp.matchedLength()); fileData.chunks = readChunks(patch, - ignoreWhitespace, &fileData.lastChunkAtTheEndOfFile, &readOk); } @@ -1024,7 +1000,7 @@ static FileData readCopyRenameChunks(const QString ©RenameChunks, return fileData; } -static QList readGitPatch(const QString &patch, bool ignoreWhitespace, bool *ok) +static QList readGitPatch(const QString &patch, bool *ok) { const QRegExp simpleGitRegExp(QLatin1String("(?:\\n|^)diff --git a/([^\\n]+) b/\\1\\n")); // diff --git a/cap1 b/cap1 @@ -1074,14 +1050,12 @@ static QList readGitPatch(const QString &patch, bool ignoreWhitespace, if (lastOperation == FileData::ChangeFile) { fileData = readGitHeaderAndChunks(headerAndChunks, lastLeftFileName, - ignoreWhitespace, &readOk); } else { fileData = readCopyRenameChunks(headerAndChunks, lastOperation, lastLeftFileName, lastRightFileName, - ignoreWhitespace, &readOk); } if (!readOk) @@ -1124,14 +1098,12 @@ static QList readGitPatch(const QString &patch, bool ignoreWhitespace, fileData = readGitHeaderAndChunks(headerAndChunks, lastLeftFileName, - ignoreWhitespace, &readOk); } else { fileData = readCopyRenameChunks(headerAndChunks, lastOperation, lastLeftFileName, lastRightFileName, - ignoreWhitespace, &readOk); } if (readOk) @@ -1148,7 +1120,7 @@ static QList readGitPatch(const QString &patch, bool ignoreWhitespace, return fileDataList; } -QList DiffUtils::readPatch(const QString &patch, bool ignoreWhitespace, bool *ok) +QList DiffUtils::readPatch(const QString &patch, bool *ok) { bool readOk = false; @@ -1161,9 +1133,9 @@ QList DiffUtils::readPatch(const QString &patch, bool ignoreWhitespace if (pos != -1) croppedPatch = patch.left(pos + 1); // crop the ending for git format-patch - fileDataList = readGitPatch(croppedPatch, ignoreWhitespace, &readOk); + fileDataList = readGitPatch(croppedPatch, &readOk); if (!readOk) - fileDataList = readDiffPatch(croppedPatch, ignoreWhitespace, &readOk); + fileDataList = readDiffPatch(croppedPatch, &readOk); if (ok) *ok = readOk; diff --git a/src/plugins/diffeditor/diffutils.h b/src/plugins/diffeditor/diffutils.h index c1e3dd9e6ff..bb0d3ed0d48 100644 --- a/src/plugins/diffeditor/diffutils.h +++ b/src/plugins/diffeditor/diffutils.h @@ -148,7 +148,6 @@ public: bool lastChunk = false); static QString makePatch(const QList &fileDataList); static QList readPatch(const QString &patch, - bool ignoreWhitespace, bool *ok = 0); }; diff --git a/src/plugins/git/gitclient.cpp b/src/plugins/git/gitclient.cpp index 1f559109d75..fcdfbf43ea4 100644 --- a/src/plugins/git/gitclient.cpp +++ b/src/plugins/git/gitclient.cpp @@ -331,8 +331,7 @@ void GitDiffHandler::slotTextualDiffOutputReceived(const QString &contents) bool ok; QList fileDataList - = DiffEditor::DiffUtils::readPatch( - contents, m_controller->isIgnoreWhitespace(), &ok); + = DiffEditor::DiffUtils::readPatch(contents, &ok); m_controller->setDiffFiles(fileDataList, m_workingDirectory); m_controller->requestRestoreState(); deleteLater(); @@ -368,7 +367,7 @@ public: DiffShow }; - GitDiffEditorReloader(QObject *parent); + GitDiffEditorReloader(); void setWorkingDirectory(const QString &workingDir) { m_workingDirectory = workingDir; } @@ -408,9 +407,8 @@ private: QString m_displayName; }; -GitDiffEditorReloader::GitDiffEditorReloader(QObject *parent) - : DiffEditorReloader(parent), - m_gitClient(GitPlugin::instance()->gitClient()) +GitDiffEditorReloader::GitDiffEditorReloader() + : m_gitClient(GitPlugin::instance()->gitClient()) { } @@ -831,7 +829,7 @@ GitDiffEditorReloader *GitClient::findOrCreateDiffEditor(const QString &document connect(controller, SIGNAL(expandBranchesRequested(QString)), this, SLOT(branchesForCommit(QString))); - reloader = new GitDiffEditorReloader(controller); + reloader = new GitDiffEditorReloader(); controller->setReloader(reloader); reloader->setWorkingDirectory(workingDirectory);