forked from qt-creator/qt-creator
DiffEditor: Pimpl plugin
The standard pattern, and allows to drop the QObject parent on the editor factory which gets into the way of de-QObject-ifing the IEditorFactory hierarchy. Change-Id: I5c6a50f8075a99592ed4459b417ca8ba7471ae96 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -36,8 +36,7 @@
|
|||||||
namespace DiffEditor {
|
namespace DiffEditor {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
DiffEditorFactory::DiffEditorFactory(QObject *parent)
|
DiffEditorFactory::DiffEditorFactory()
|
||||||
: IEditorFactory(parent)
|
|
||||||
{
|
{
|
||||||
setId(Constants::DIFF_EDITOR_ID);
|
setId(Constants::DIFF_EDITOR_ID);
|
||||||
setDisplayName(QCoreApplication::translate("DiffEditorFactory", Constants::DIFF_EDITOR_DISPLAY_NAME));
|
setDisplayName(QCoreApplication::translate("DiffEditorFactory", Constants::DIFF_EDITOR_DISPLAY_NAME));
|
||||||
|
@@ -33,7 +33,7 @@ namespace Internal {
|
|||||||
class DiffEditorFactory : public Core::IEditorFactory
|
class DiffEditorFactory : public Core::IEditorFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit DiffEditorFactory(QObject *parent);
|
DiffEditorFactory();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
|
@@ -35,7 +35,6 @@
|
|||||||
#include <QFutureWatcher>
|
#include <QFutureWatcher>
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QTextCodec>
|
#include <QTextCodec>
|
||||||
#include <QtPlugin>
|
|
||||||
|
|
||||||
#include <coreplugin/actionmanager/actioncontainer.h>
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
||||||
#include <coreplugin/actionmanager/actionmanager.h>
|
#include <coreplugin/actionmanager/actionmanager.h>
|
||||||
@@ -47,6 +46,7 @@
|
|||||||
|
|
||||||
#include <texteditor/textdocument.h>
|
#include <texteditor/textdocument.h>
|
||||||
#include <texteditor/texteditor.h>
|
#include <texteditor/texteditor.h>
|
||||||
|
#include "texteditor/texteditoractionhandler.h"
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
#include <utils/differ.h>
|
#include <utils/differ.h>
|
||||||
@@ -54,6 +54,7 @@
|
|||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
using namespace Core;
|
using namespace Core;
|
||||||
|
using namespace TextEditor;
|
||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
|
|
||||||
namespace DiffEditor {
|
namespace DiffEditor {
|
||||||
@@ -421,10 +422,7 @@ static TextEditor::TextDocument *currentTextDocument()
|
|||||||
EditorManager::currentDocument());
|
EditorManager::currentDocument());
|
||||||
}
|
}
|
||||||
|
|
||||||
DiffEditorServiceImpl::DiffEditorServiceImpl(QObject *parent) :
|
DiffEditorServiceImpl::DiffEditorServiceImpl() = default;
|
||||||
QObject(parent)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void DiffEditorServiceImpl::diffFiles(const QString &leftFileName, const QString &rightFileName)
|
void DiffEditorServiceImpl::diffFiles(const QString &leftFileName, const QString &rightFileName)
|
||||||
{
|
{
|
||||||
@@ -458,11 +456,28 @@ void DiffEditorServiceImpl::diffModifiedFiles(const QStringList &fileNames)
|
|||||||
document->reload();
|
document->reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DiffEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
|
class DiffEditorPluginPrivate : public QObject
|
||||||
{
|
{
|
||||||
Q_UNUSED(arguments)
|
Q_DECLARE_TR_FUNCTIONS(DiffEditor::Internal::DiffEditorPlugin)
|
||||||
Q_UNUSED(errorMessage)
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
DiffEditorPluginPrivate();
|
||||||
|
|
||||||
|
void updateDiffCurrentFileAction();
|
||||||
|
void updateDiffOpenFilesAction();
|
||||||
|
void diffCurrentFile();
|
||||||
|
void diffOpenFiles();
|
||||||
|
void diffExternalFiles();
|
||||||
|
|
||||||
|
QAction *m_diffCurrentFileAction = nullptr;
|
||||||
|
QAction *m_diffOpenFilesAction = nullptr;
|
||||||
|
|
||||||
|
DiffEditorFactory editorFactory;
|
||||||
|
DiffEditorServiceImpl service;
|
||||||
|
};
|
||||||
|
|
||||||
|
DiffEditorPluginPrivate::DiffEditorPluginPrivate()
|
||||||
|
{
|
||||||
//register actions
|
//register actions
|
||||||
ActionContainer *toolsContainer
|
ActionContainer *toolsContainer
|
||||||
= ActionManager::actionContainer(Core::Constants::M_TOOLS);
|
= ActionManager::actionContainer(Core::Constants::M_TOOLS);
|
||||||
@@ -474,51 +489,43 @@ bool DiffEditorPlugin::initialize(const QStringList &arguments, QString *errorMe
|
|||||||
m_diffCurrentFileAction = new QAction(tr("Diff Current File"), this);
|
m_diffCurrentFileAction = new QAction(tr("Diff Current File"), this);
|
||||||
Command *diffCurrentFileCommand = ActionManager::registerAction(m_diffCurrentFileAction, "DiffEditor.DiffCurrentFile");
|
Command *diffCurrentFileCommand = ActionManager::registerAction(m_diffCurrentFileAction, "DiffEditor.DiffCurrentFile");
|
||||||
diffCurrentFileCommand->setDefaultKeySequence(QKeySequence(useMacShortcuts ? tr("Meta+H") : tr("Ctrl+H")));
|
diffCurrentFileCommand->setDefaultKeySequence(QKeySequence(useMacShortcuts ? tr("Meta+H") : tr("Ctrl+H")));
|
||||||
connect(m_diffCurrentFileAction, &QAction::triggered, this, &DiffEditorPlugin::diffCurrentFile);
|
connect(m_diffCurrentFileAction, &QAction::triggered, this, &DiffEditorPluginPrivate::diffCurrentFile);
|
||||||
diffContainer->addAction(diffCurrentFileCommand);
|
diffContainer->addAction(diffCurrentFileCommand);
|
||||||
|
|
||||||
m_diffOpenFilesAction = new QAction(tr("Diff Open Files"), this);
|
m_diffOpenFilesAction = new QAction(tr("Diff Open Files"), this);
|
||||||
Command *diffOpenFilesCommand = ActionManager::registerAction(m_diffOpenFilesAction, "DiffEditor.DiffOpenFiles");
|
Command *diffOpenFilesCommand = ActionManager::registerAction(m_diffOpenFilesAction, "DiffEditor.DiffOpenFiles");
|
||||||
diffOpenFilesCommand->setDefaultKeySequence(QKeySequence(useMacShortcuts ? tr("Meta+Shift+H") : tr("Ctrl+Shift+H")));
|
diffOpenFilesCommand->setDefaultKeySequence(QKeySequence(useMacShortcuts ? tr("Meta+Shift+H") : tr("Ctrl+Shift+H")));
|
||||||
connect(m_diffOpenFilesAction, &QAction::triggered, this, &DiffEditorPlugin::diffOpenFiles);
|
connect(m_diffOpenFilesAction, &QAction::triggered, this, &DiffEditorPluginPrivate::diffOpenFiles);
|
||||||
diffContainer->addAction(diffOpenFilesCommand);
|
diffContainer->addAction(diffOpenFilesCommand);
|
||||||
|
|
||||||
QAction *diffExternalFilesAction = new QAction(tr("Diff External Files..."), this);
|
QAction *diffExternalFilesAction = new QAction(tr("Diff External Files..."), this);
|
||||||
Command *diffExternalFilesCommand = ActionManager::registerAction(diffExternalFilesAction, "DiffEditor.DiffExternalFiles");
|
Command *diffExternalFilesCommand = ActionManager::registerAction(diffExternalFilesAction, "DiffEditor.DiffExternalFiles");
|
||||||
connect(diffExternalFilesAction, &QAction::triggered, this, &DiffEditorPlugin::diffExternalFiles);
|
connect(diffExternalFilesAction, &QAction::triggered, this, &DiffEditorPluginPrivate::diffExternalFiles);
|
||||||
diffContainer->addAction(diffExternalFilesCommand);
|
diffContainer->addAction(diffExternalFilesCommand);
|
||||||
|
|
||||||
connect(EditorManager::instance(), &EditorManager::currentEditorChanged,
|
connect(EditorManager::instance(), &EditorManager::currentEditorChanged,
|
||||||
this, &DiffEditorPlugin::updateDiffCurrentFileAction);
|
this, &DiffEditorPluginPrivate::updateDiffCurrentFileAction);
|
||||||
connect(EditorManager::instance(), &EditorManager::currentDocumentStateChanged,
|
connect(EditorManager::instance(), &EditorManager::currentDocumentStateChanged,
|
||||||
this, &DiffEditorPlugin::updateDiffCurrentFileAction);
|
this, &DiffEditorPluginPrivate::updateDiffCurrentFileAction);
|
||||||
connect(EditorManager::instance(), &EditorManager::editorOpened,
|
connect(EditorManager::instance(), &EditorManager::editorOpened,
|
||||||
this, &DiffEditorPlugin::updateDiffOpenFilesAction);
|
this, &DiffEditorPluginPrivate::updateDiffOpenFilesAction);
|
||||||
connect(EditorManager::instance(), &EditorManager::editorsClosed,
|
connect(EditorManager::instance(), &EditorManager::editorsClosed,
|
||||||
this, &DiffEditorPlugin::updateDiffOpenFilesAction);
|
this, &DiffEditorPluginPrivate::updateDiffOpenFilesAction);
|
||||||
connect(EditorManager::instance(), &EditorManager::documentStateChanged,
|
connect(EditorManager::instance(), &EditorManager::documentStateChanged,
|
||||||
this, &DiffEditorPlugin::updateDiffOpenFilesAction);
|
this, &DiffEditorPluginPrivate::updateDiffOpenFilesAction);
|
||||||
|
|
||||||
updateDiffCurrentFileAction();
|
updateDiffCurrentFileAction();
|
||||||
updateDiffOpenFilesAction();
|
updateDiffOpenFilesAction();
|
||||||
|
|
||||||
new DiffEditorFactory(this);
|
|
||||||
new DiffEditorServiceImpl(this);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiffEditorPlugin::extensionsInitialized()
|
void DiffEditorPluginPrivate::updateDiffCurrentFileAction()
|
||||||
{ }
|
|
||||||
|
|
||||||
void DiffEditorPlugin::updateDiffCurrentFileAction()
|
|
||||||
{
|
{
|
||||||
auto textDocument = currentTextDocument();
|
auto textDocument = currentTextDocument();
|
||||||
const bool enabled = textDocument && textDocument->isModified();
|
const bool enabled = textDocument && textDocument->isModified();
|
||||||
m_diffCurrentFileAction->setEnabled(enabled);
|
m_diffCurrentFileAction->setEnabled(enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiffEditorPlugin::updateDiffOpenFilesAction()
|
void DiffEditorPluginPrivate::updateDiffOpenFilesAction()
|
||||||
{
|
{
|
||||||
const bool enabled = Utils::anyOf(DocumentModel::openedDocuments(), [](IDocument *doc) {
|
const bool enabled = Utils::anyOf(DocumentModel::openedDocuments(), [](IDocument *doc) {
|
||||||
return doc->isModified() && qobject_cast<TextEditor::TextDocument *>(doc);
|
return doc->isModified() && qobject_cast<TextEditor::TextDocument *>(doc);
|
||||||
@@ -526,7 +533,7 @@ void DiffEditorPlugin::updateDiffOpenFilesAction()
|
|||||||
m_diffOpenFilesAction->setEnabled(enabled);
|
m_diffOpenFilesAction->setEnabled(enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiffEditorPlugin::diffCurrentFile()
|
void DiffEditorPluginPrivate::diffCurrentFile()
|
||||||
{
|
{
|
||||||
auto textDocument = currentTextDocument();
|
auto textDocument = currentTextDocument();
|
||||||
if (!textDocument)
|
if (!textDocument)
|
||||||
@@ -551,7 +558,7 @@ void DiffEditorPlugin::diffCurrentFile()
|
|||||||
document->reload();
|
document->reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiffEditorPlugin::diffOpenFiles()
|
void DiffEditorPluginPrivate::diffOpenFiles()
|
||||||
{
|
{
|
||||||
const QString documentId = Constants::DIFF_EDITOR_PLUGIN
|
const QString documentId = Constants::DIFF_EDITOR_PLUGIN
|
||||||
+ QLatin1String(".DiffOpenFiles");
|
+ QLatin1String(".DiffOpenFiles");
|
||||||
@@ -567,7 +574,7 @@ void DiffEditorPlugin::diffOpenFiles()
|
|||||||
document->reload();
|
document->reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiffEditorPlugin::diffExternalFiles()
|
void DiffEditorPluginPrivate::diffExternalFiles()
|
||||||
{
|
{
|
||||||
const QString fileName1 = QFileDialog::getOpenFileName(ICore::dialogParent(),
|
const QString fileName1 = QFileDialog::getOpenFileName(ICore::dialogParent(),
|
||||||
tr("Select First File for Diff"),
|
tr("Select First File for Diff"),
|
||||||
@@ -599,6 +606,21 @@ void DiffEditorPlugin::diffExternalFiles()
|
|||||||
document->reload();
|
document->reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DiffEditorPlugin::~DiffEditorPlugin()
|
||||||
|
{
|
||||||
|
delete d;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DiffEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
|
||||||
|
{
|
||||||
|
d = new DiffEditorPluginPrivate;
|
||||||
|
|
||||||
|
Q_UNUSED(arguments)
|
||||||
|
Q_UNUSED(errorMessage)
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
} // namespace DiffEditor
|
} // namespace DiffEditor
|
||||||
|
|
||||||
|
@@ -30,10 +30,6 @@
|
|||||||
#include <coreplugin/diffservice.h>
|
#include <coreplugin/diffservice.h>
|
||||||
#include <extensionsystem/iplugin.h>
|
#include <extensionsystem/iplugin.h>
|
||||||
|
|
||||||
QT_FORWARD_DECLARE_CLASS(QAction)
|
|
||||||
|
|
||||||
namespace Core { class IEditor; }
|
|
||||||
|
|
||||||
namespace DiffEditor {
|
namespace DiffEditor {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
@@ -41,31 +37,27 @@ class DiffEditorServiceImpl : public QObject, public Core::DiffService
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_INTERFACES(Core::DiffService)
|
Q_INTERFACES(Core::DiffService)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit DiffEditorServiceImpl(QObject *parent = nullptr);
|
DiffEditorServiceImpl();
|
||||||
|
|
||||||
void diffFiles(const QString &leftFileName, const QString &rightFileName) override;
|
void diffFiles(const QString &leftFileName, const QString &rightFileName) override;
|
||||||
void diffModifiedFiles(const QStringList &fileNames) override;
|
void diffModifiedFiles(const QStringList &fileNames) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class DiffEditorPlugin : public ExtensionSystem::IPlugin
|
class DiffEditorPlugin final : public ExtensionSystem::IPlugin
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "DiffEditor.json")
|
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "DiffEditor.json")
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool initialize(const QStringList &arguments, QString *errorMessage = nullptr) override;
|
~DiffEditorPlugin() final;
|
||||||
void extensionsInitialized() override;
|
|
||||||
|
bool initialize(const QStringList &arguments, QString *errorMessage) final;
|
||||||
|
void extensionsInitialized() final {};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void updateDiffCurrentFileAction();
|
class DiffEditorPluginPrivate *d = nullptr;
|
||||||
void updateDiffOpenFilesAction();
|
|
||||||
void diffCurrentFile();
|
|
||||||
void diffOpenFiles();
|
|
||||||
void diffExternalFiles();
|
|
||||||
|
|
||||||
QAction *m_diffCurrentFileAction = nullptr;
|
|
||||||
QAction *m_diffOpenFilesAction = nullptr;
|
|
||||||
|
|
||||||
#ifdef WITH_TESTS
|
#ifdef WITH_TESTS
|
||||||
private slots:
|
private slots:
|
||||||
|
Reference in New Issue
Block a user