DiffEditor: Introduce a base class for the different views

... of the diff. Currently that is side-by-side and unified, just
as before.

Change-Id: I62a5462344c4b4ae652899f9d5b2936aa5a692b8
Reviewed-by: Jarek Kobus <jaroslaw.kobus@theqtcompany.com>
This commit is contained in:
Tobias Hunger
2015-01-30 16:10:04 +01:00
parent 7121315a71
commit f9f6f0b2c9
7 changed files with 295 additions and 93 deletions

View File

@@ -32,8 +32,7 @@
#include "diffeditorconstants.h"
#include "diffeditordocument.h"
#include "diffeditorguicontroller.h"
#include "sidebysidediffeditorwidget.h"
#include "unifieddiffeditorwidget.h"
#include "diffview.h"
#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
@@ -44,6 +43,7 @@
#include <texteditor/displaysettings.h>
#include <texteditor/marginsettings.h>
#include <utils/algorithm.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
@@ -61,8 +61,6 @@
static const char settingsGroupC[] = "DiffEditor";
static const char diffEditorTypeKeyC[] = "DiffEditorType";
static const char sideBySideDiffEditorValueC[] = "SideBySide";
static const char unifiedDiffEditorValueC[] = "Unified";
static const char legacySettingsGroupC[] = "Git";
static const char useDiffEditorKeyC[] = "UseDiffEditor";
@@ -204,9 +202,7 @@ DiffEditor::DiffEditor(const QSharedPointer<DiffEditorDocument> &doc)
: m_document(doc)
, m_descriptionWidget(0)
, m_stackedWidget(0)
, m_sideBySideEditor(0)
, m_unifiedEditor(0)
, m_currentEditor(0)
, m_currentViewIndex(0)
, m_guiController(0)
, m_toolBar(0)
, m_entriesComboBox(0)
@@ -225,15 +221,11 @@ DiffEditor::DiffEditor(const QSharedPointer<DiffEditorDocument> &doc)
m_stackedWidget = new QStackedWidget(splitter);
splitter->addWidget(m_stackedWidget);
m_sideBySideEditor = new SideBySideDiffEditorWidget(m_stackedWidget);
m_stackedWidget->addWidget(m_sideBySideEditor);
m_unifiedEditor = new UnifiedDiffEditorWidget(m_stackedWidget);
m_stackedWidget->addWidget(m_unifiedEditor);
addView(new SideBySideView);
addView(new UnifiedView);
setWidget(splitter);
DiffEditorController *control = controller();
m_guiController = new DiffEditorGuiController(control, this);
@@ -254,7 +246,7 @@ DiffEditor::DiffEditor(const QSharedPointer<DiffEditorDocument> &doc)
slotDescriptionChanged(control->description());
slotDescriptionVisibilityChanged();
showDiffEditor(readCurrentDiffEditorSetting());
showDiffView(readCurrentDiffEditorSetting());
toolBar();
}
@@ -283,12 +275,12 @@ Core::IDocument *DiffEditor::document()
return m_document.data();
}
static QToolBar *createToolBar(const QWidget *someWidget)
static QToolBar *createToolBar(IDiffView *someView)
{
// Create
QToolBar *toolBar = new QToolBar;
toolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
const int size = someWidget->style()->pixelMetric(QStyle::PM_SmallIconSize);
const int size = someView->widget()->style()->pixelMetric(QStyle::PM_SmallIconSize);
toolBar->setIconSize(QSize(size, size));
return toolBar;
@@ -296,13 +288,15 @@ static QToolBar *createToolBar(const QWidget *someWidget)
QWidget *DiffEditor::toolBar()
{
QTC_ASSERT(!m_views.isEmpty(), return 0);
if (m_toolBar)
return m_toolBar;
DiffEditorController *control = controller();
// Create
m_toolBar = createToolBar(m_sideBySideEditor);
m_toolBar = createToolBar(m_views.at(0));
m_entriesComboBox = new QComboBox;
m_entriesComboBox->setMinimumContentsLength(20);
@@ -334,8 +328,7 @@ QWidget *DiffEditor::toolBar()
m_contextSpinBoxAction = m_toolBar->addWidget(contextSpinBox);
QToolButton *toggleDescription = new QToolButton(m_toolBar);
toggleDescription->setIcon(
QIcon(QLatin1String(Constants::ICON_TOP_BAR)));
toggleDescription->setIcon(QIcon(QLatin1String(Constants::ICON_TOP_BAR)));
toggleDescription->setCheckable(true);
toggleDescription->setChecked(m_guiController->isDescriptionVisible());
m_toggleDescriptionAction = m_toolBar->addWidget(toggleDescription);
@@ -371,7 +364,7 @@ QWidget *DiffEditor::toolBar()
connect(toggleDescription, &QAbstractButton::clicked,
m_guiController, &DiffEditorGuiController::setDescriptionVisible);
connect(m_diffEditorSwitcher, &QAbstractButton::clicked,
this, &DiffEditor::slotDiffEditorSwitched);
this, [this]() { showDiffView(nextView()); });
connect(reloadButton, &QAbstractButton::clicked,
control, &DiffEditorController::requestReload);
connect(control, &DiffEditorController::reloaderChanged,
@@ -505,65 +498,67 @@ void DiffEditor::slotReloaderChanged()
m_reloadAction->setVisible(reloader);
}
void DiffEditor::slotDiffEditorSwitched()
{
QWidget *oldEditor = m_currentEditor;
QWidget *newEditor = 0;
if (oldEditor == m_sideBySideEditor)
newEditor = m_unifiedEditor;
else if (oldEditor == m_unifiedEditor)
newEditor = m_sideBySideEditor;
else
newEditor = readCurrentDiffEditorSetting();
showDiffEditor(newEditor);
}
void DiffEditor::updateDiffEditorSwitcher()
{
if (!m_diffEditorSwitcher)
return;
QIcon actionIcon;
QString actionToolTip;
if (m_currentEditor == m_unifiedEditor) {
actionIcon = QIcon(QLatin1String(Constants::ICON_SIDE_BY_SIDE_DIFF));
actionToolTip = tr("Switch to Side By Side Diff Editor");
} else if (m_currentEditor == m_sideBySideEditor) {
actionIcon = QIcon(QLatin1String(Constants::ICON_UNIFIED_DIFF));
actionToolTip = tr("Switch to Unified Diff Editor");
}
m_diffEditorSwitcher->setIcon(actionIcon);
m_diffEditorSwitcher->setToolTip(actionToolTip);
m_diffEditorSwitcher->setIcon(currentView()->icon());
m_diffEditorSwitcher->setToolTip(currentView()->toolTip());
}
void DiffEditor::showDiffEditor(QWidget *newEditor)
void DiffEditor::addView(IDiffView *view)
{
if (m_currentEditor == newEditor)
QTC_ASSERT(!m_views.contains(view), return);
m_views.append(view);
m_stackedWidget->addWidget(view->widget());
}
IDiffView *DiffEditor::currentView() const
{
return m_views.at(m_currentViewIndex);
}
void DiffEditor::setCurrentView(IDiffView *view)
{
const int pos = Utils::indexOf(m_views, [view](IDiffView *v) { return v == view; });
QTC_ASSERT(pos >= 0 && pos < m_views.count(), return);
m_currentViewIndex = pos;
}
IDiffView *DiffEditor::nextView()
{
++m_currentViewIndex;
if (m_currentViewIndex >= m_views.count())
m_currentViewIndex = 0;
return currentView();
}
void DiffEditor::showDiffView(IDiffView *newView)
{
QTC_ASSERT(newView, return);
if (currentView() == newView)
return;
if (m_currentEditor == m_sideBySideEditor)
m_sideBySideEditor->setDiffEditorGuiController(0);
else if (m_currentEditor == m_unifiedEditor)
m_unifiedEditor->setDiffEditorGuiController(0);
currentView()->setDiffEditorGuiController(0);
setCurrentView(newView);
currentView()->setDiffEditorGuiController(m_guiController);
m_currentEditor = newEditor;
m_stackedWidget->setCurrentWidget(currentView()->widget());
if (m_currentEditor == m_unifiedEditor)
m_unifiedEditor->setDiffEditorGuiController(m_guiController);
else if (m_currentEditor == m_sideBySideEditor)
m_sideBySideEditor->setDiffEditorGuiController(m_guiController);
m_stackedWidget->setCurrentWidget(m_currentEditor);
writeCurrentDiffEditorSetting(m_currentEditor);
writeCurrentDiffEditorSetting(currentView());
updateDiffEditorSwitcher();
widget()->setFocusProxy(m_currentEditor);
widget()->setFocusProxy(currentView()->widget());
}
QWidget *DiffEditor::readLegacyCurrentDiffEditorSetting()
// TODO: Remove in 3.6:
IDiffView *DiffEditor::readLegacyCurrentDiffEditorSetting()
{
QTC_ASSERT(!m_views.isEmpty(), return 0);
QTC_ASSERT(m_views.count() == 2, return m_views.at(0));
QSettings *s = Core::ICore::settings();
s->beginGroup(QLatin1String(legacySettingsGroupC));
@@ -574,43 +569,35 @@ QWidget *DiffEditor::readLegacyCurrentDiffEditorSetting()
s->remove(QLatin1String(useDiffEditorKeyC));
s->endGroup();
QWidget *currentEditor = m_sideBySideEditor;
IDiffView *currentEditor = m_views.at(0);
if (!legacyEditor)
currentEditor = m_unifiedEditor;
currentEditor = m_views.at(1);
if (legacyExists && currentEditor == m_unifiedEditor)
if (legacyExists)
writeCurrentDiffEditorSetting(currentEditor);
return currentEditor;
}
QWidget *DiffEditor::readCurrentDiffEditorSetting()
IDiffView *DiffEditor::readCurrentDiffEditorSetting()
{
// replace it with m_sideBySideEditor when dropping legacy stuff
QWidget *defaultEditor = readLegacyCurrentDiffEditorSetting();
IDiffView *view = readLegacyCurrentDiffEditorSetting();
QSettings *s = Core::ICore::settings();
s->beginGroup(QLatin1String(settingsGroupC));
const QString editorString = s->value(
QLatin1String(diffEditorTypeKeyC)).toString();
const Core::Id id = Core::Id::fromSetting(s->value(QLatin1String(diffEditorTypeKeyC)));
s->endGroup();
if (editorString == QLatin1String(unifiedDiffEditorValueC))
return m_unifiedEditor;
if (editorString == QLatin1String(sideBySideDiffEditorValueC))
return m_sideBySideEditor;
return defaultEditor;
return Utils::findOr(m_views, view, [id](IDiffView *v) { return v->id() == id; });
}
void DiffEditor::writeCurrentDiffEditorSetting(QWidget *currentEditor)
void DiffEditor::writeCurrentDiffEditorSetting(IDiffView *currentEditor)
{
const QString editorString = currentEditor == m_unifiedEditor
? QLatin1String(unifiedDiffEditorValueC)
: QLatin1String(sideBySideDiffEditorValueC);
QTC_ASSERT(currentEditor, return);
QSettings *s = Core::ICore::settings();
s->beginGroup(QLatin1String(settingsGroupC));
s->setValue(QLatin1String(diffEditorTypeKeyC), editorString);
s->setValue(QLatin1String(diffEditorTypeKeyC), currentEditor->id().toSetting());
s->endGroup();
}

View File

@@ -51,8 +51,7 @@ namespace Internal {
class DescriptionEditorWidget;
class DiffEditorDocument;
class DiffEditorGuiController;
class UnifiedDiffEditorWidget;
class SideBySideDiffEditorWidget;
class IDiffView;
class DiffEditor : public Core::IEditor
{
@@ -85,22 +84,24 @@ private slots:
void slotDescriptionChanged(const QString &description);
void slotDescriptionVisibilityChanged();
void slotReloaderChanged();
void slotDiffEditorSwitched();
private:
void updateEntryToolTip();
void showDiffEditor(QWidget *newEditor);
void showDiffView(IDiffView *newEditor);
void updateDiffEditorSwitcher();
QWidget *readLegacyCurrentDiffEditorSetting();
QWidget *readCurrentDiffEditorSetting();
void writeCurrentDiffEditorSetting(QWidget *currentEditor);
void addView(IDiffView *view);
IDiffView *currentView() const;
void setCurrentView(IDiffView *view);
IDiffView *nextView();
IDiffView *readLegacyCurrentDiffEditorSetting();
IDiffView *readCurrentDiffEditorSetting();
void writeCurrentDiffEditorSetting(IDiffView *currentEditor);
QSharedPointer<DiffEditorDocument> m_document;
DescriptionEditorWidget *m_descriptionWidget;
QStackedWidget *m_stackedWidget;
SideBySideDiffEditorWidget *m_sideBySideEditor;
UnifiedDiffEditorWidget *m_unifiedEditor;
QWidget *m_currentEditor;
QVector<IDiffView *> m_views;
int m_currentViewIndex;
DiffEditorGuiController *m_guiController;
QToolBar *m_toolBar;
QComboBox *m_entriesComboBox;

View File

@@ -13,6 +13,7 @@ HEADERS += diffeditor_global.h \
diffeditorreloader.h \
differ.h \
diffutils.h \
diffview.h \
selectabletexteditorwidget.h \
sidebysidediffeditorwidget.h \
unifieddiffeditorwidget.h
@@ -27,6 +28,7 @@ SOURCES += diffeditor.cpp \
diffeditorreloader.cpp \
differ.cpp \
diffutils.cpp \
diffview.cpp \
selectabletexteditorwidget.cpp \
sidebysidediffeditorwidget.cpp \
unifieddiffeditorwidget.cpp

View File

@@ -34,6 +34,8 @@ QtcPlugin {
"differ.h",
"diffutils.cpp",
"diffutils.h",
"diffview.cpp",
"diffview.h",
"selectabletexteditorwidget.cpp",
"selectabletexteditorwidget.h",
"sidebysidediffeditorwidget.cpp",

View File

@@ -41,8 +41,6 @@ const char DIFF_EDITOR_DISPLAY_NAME[] = QT_TRANSLATE_NOOP("DiffEditor", "Diff Ed
const char DIFF_EDITOR_MIMETYPE[] = "text/x-patch";
const char G_TOOLS_DIFF[] = "QtCreator.Group.Tools.Options";
const char ICON_SIDE_BY_SIDE_DIFF[] = ":/diffeditor/images/sidebysidediff.png";
const char ICON_UNIFIED_DIFF[] = ":/diffeditor/images/unifieddiff.png";
const char ICON_TOP_BAR[] = ":/diffeditor/images/topbar.png";
const char EXPAND_BRANCHES[] = "Branches: <Expand>";

View File

@@ -0,0 +1,113 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** 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://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** 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.
**
****************************************************************************/
#include "diffview.h"
#include "unifieddiffeditorwidget.h"
#include "sidebysidediffeditorwidget.h"
#include <utils/qtcassert.h>
namespace DiffEditor {
namespace Internal {
QIcon IDiffView::icon() const
{
return m_icon;
}
QString IDiffView::toolTip() const
{
return m_toolTip;
}
Core::Id IDiffView::id() const
{
return m_id;
}
void IDiffView::setIcon(const QIcon &icon)
{
m_icon = icon;
}
void IDiffView::setToolTip(const QString &toolTip)
{
m_toolTip = toolTip;
}
void IDiffView::setId(const Core::Id &id)
{
m_id = id;
}
UnifiedView::UnifiedView() : m_widget(0)
{
setId(UNIFIED_VIEW_ID);
setIcon(QIcon(QLatin1String(":/diffeditor/images/unifieddiff.png")));
setToolTip(QCoreApplication::translate("DiffEditor::UnifiedView", "Switch to Unified Diff Editor"));
}
QWidget *UnifiedView::widget()
{
if (!m_widget)
m_widget = new UnifiedDiffEditorWidget;
return m_widget;
}
void UnifiedView::setDiffEditorGuiController(DiffEditorGuiController *controller)
{
QTC_ASSERT(m_widget, return);
m_widget->setDiffEditorGuiController(controller);
}
SideBySideView::SideBySideView() : m_widget(0)
{
setId(SIDE_BY_SIDE_VIEW_ID);
setIcon(QIcon(QLatin1String(":/diffeditor/images/sidebysidediff.png")));
setToolTip(QCoreApplication::translate("DiffEditor::SideBySideView",
"Switch to Side By Side Diff Editor"));
}
QWidget *SideBySideView::widget()
{
if (!m_widget)
m_widget = new SideBySideDiffEditorWidget;
return m_widget;
}
void SideBySideView::setDiffEditorGuiController(DiffEditorGuiController *controller)
{
QTC_ASSERT(m_widget, return);
m_widget->setDiffEditorGuiController(controller);
}
} // namespace Internal
} // namespace DiffEditor

View File

@@ -0,0 +1,99 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** 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://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** 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.
**
****************************************************************************/
#ifndef DIFFVIEW_H
#define DIFFVIEW_H
#include <coreplugin/id.h>
#include <QIcon>
#include <QString>
#include <QWidget>
namespace DiffEditor {
namespace Internal {
class DiffEditorGuiController;
class SideBySideDiffEditorWidget;
class UnifiedDiffEditorWidget;
const char SIDE_BY_SIDE_VIEW_ID[] = "SideBySide";
const char UNIFIED_VIEW_ID[] = "Unified";
class IDiffView
{
public:
IDiffView() { }
virtual ~IDiffView() { }
QIcon icon() const;
QString toolTip() const;
Core::Id id() const;
virtual QWidget *widget() = 0;
virtual void setDiffEditorGuiController(DiffEditorGuiController *controller) = 0;
protected:
void setIcon(const QIcon &icon);
void setToolTip(const QString &toolTip);
void setId(const Core::Id &id);
private:
QIcon m_icon;
QString m_toolTip;
Core::Id m_id;
};
class UnifiedView : public IDiffView {
public:
UnifiedView();
QWidget *widget();
void setDiffEditorGuiController(DiffEditorGuiController *controller);
private:
UnifiedDiffEditorWidget *m_widget;
};
class SideBySideView : public IDiffView {
public:
SideBySideView();
QWidget *widget();
void setDiffEditorGuiController(DiffEditorGuiController *controller);
private:
SideBySideDiffEditorWidget *m_widget;
};
} // namespace Internal
} // namespace DiffEditor
#endif // DIFFVIEW_H