QmlDesigner: Refactoring TextEditorView

* Using smart pointers
* Renaming DummyWidget to TextEditorWidget
* Moving TextEditorWidget to separate file

Change-Id: Idbe0e50f976bf39beac8632d4254d6d9a5afd5b2
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Thomas Hartmann
2017-01-04 15:01:14 +01:00
parent 5ad0cbc070
commit 79d8b9c133
6 changed files with 133 additions and 39 deletions

View File

@@ -1,4 +1,5 @@
VPATH += $$PWD
SOURCES += texteditorview.cpp
HEADERS += texteditorview.h
#RESOURCES += texteditor.qrc
SOURCES += texteditorview.cpp \
texteditorwidget.cpp
HEADERS += texteditorview.h \
texteditorwidget.h

View File

@@ -25,6 +25,8 @@
#include "texteditorview.h"
#include "texteditorwidget.h"
#include <designmodecontext.h>
#include <designdocument.h>
#include <modelnode.h>
@@ -41,48 +43,22 @@
#include <QPair>
#include <QString>
#include <QTimer>
#include <QVBoxLayout>
#include <QPointer>
namespace QmlDesigner {
class DummyWidget : public QWidget {
public:
DummyWidget(QWidget *parent = nullptr) : QWidget(parent) {
QBoxLayout *layout = new QVBoxLayout(this);
layout->setMargin(0);
}
void showEvent(QShowEvent *event) {
if (m_widget.isNull())
setWidget(QmlDesignerPlugin::instance()->currentDesignDocument()->textEditor()->duplicate()->widget());
QWidget::showEvent(event);
}
private:
void setWidget(QWidget *widget) {
if (m_widget)
m_widget->deleteLater();
m_widget = widget;
layout()->addWidget(widget);
}
QPointer<QWidget> m_widget;
};
TextEditorView::TextEditorView(QObject *parent)
: AbstractView(parent)
, m_dummyWidget(new DummyWidget)
, m_widget(new TextEditorWidget(this))
{
// not completely sure that we need this to just call the right help method ->
Internal::TextEditorContext *textEditorContext = new Internal::TextEditorContext(m_dummyWidget);
Internal::TextEditorContext *textEditorContext = new Internal::TextEditorContext(m_widget.get());
Core::ICore::addContextObject(textEditorContext);
}
TextEditorView::~TextEditorView()
{
m_textEditor->deleteLater();
m_dummyWidget->deleteLater();
m_widget->deleteLater();
}
void TextEditorView::modelAttached(Model *model)
@@ -90,6 +66,7 @@ void TextEditorView::modelAttached(Model *model)
Q_ASSERT(model);
AbstractView::modelAttached(model);
m_widget->setTextEditor(qobject_cast<TextEditor::BaseTextEditor*>(QmlDesignerPlugin::instance()->currentDesignDocument()->textEditor()->duplicate()));
}
void TextEditorView::modelAboutToBeDetached(Model *model)
@@ -119,15 +96,15 @@ void TextEditorView::nodeReparented(const ModelNode &/*node*/, const NodeAbstrac
WidgetInfo TextEditorView::widgetInfo()
{
return createWidgetInfo(m_dummyWidget, 0, "TextEditor", WidgetInfo::CentralPane, 0, tr("Text Editor"));
return createWidgetInfo(m_widget.get(), 0, "TextEditor", WidgetInfo::CentralPane, 0, tr("Text Editor"));
}
QString TextEditorView::contextHelpId() const
{
if (m_textEditor) {
QString contextHelpId = m_textEditor->contextHelpId();
if (m_widget->textEditor()) {
QString contextHelpId = m_widget->textEditor()->contextHelpId();
if (!contextHelpId.isEmpty())
return m_textEditor->contextHelpId();
return m_widget->textEditor()->contextHelpId();
}
return AbstractView::contextHelpId();
}
@@ -206,5 +183,6 @@ void TextEditorView::rewriterEndTransaction()
void TextEditorView::instancePropertyChanged(const QList<QPair<ModelNode, PropertyName> > &/*propertyList*/)
{
}
}
} // namespace QmlDesigner

View File

@@ -26,12 +26,16 @@
#include <abstractview.h>
#include <memory>
namespace TextEditor { class BaseTextEditor; }
namespace Utils { class CrumblePath; }
namespace QmlDesigner {
class TextEditorWidget;
class QMLDESIGNERCORE_EXPORT TextEditorView : public AbstractView
{
Q_OBJECT
@@ -84,8 +88,7 @@ public:
void deActivateItemCreator();
private:
QPointer<TextEditor::BaseTextEditor> m_textEditor;
QWidget *m_dummyWidget = 0;
std::unique_ptr<TextEditorWidget> m_widget;
};
} // namespace QmlDesigner

View File

@@ -0,0 +1,52 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "texteditorwidget.h"
#include <texteditorview.h>
#include <QVBoxLayout>
namespace QmlDesigner {
TextEditorWidget::TextEditorWidget(TextEditorView *textEditorView) : QWidget()
, m_textEditorView(textEditorView)
{
QBoxLayout *layout = new QVBoxLayout(this);
layout->setMargin(0);
}
void TextEditorWidget::setTextEditor(TextEditor::BaseTextEditor *textEditor) {
m_textEditor.reset(textEditor);
layout()->addWidget(textEditor->widget());
}
QString TextEditorWidget::contextHelpId() const
{
return m_textEditorView->contextHelpId();
}
} // namespace QmlDesigner

View File

@@ -0,0 +1,58 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <texteditor/texteditor.h>
#include <QWidget>
#include <memory>
namespace QmlDesigner {
class TextEditorView;
class TextEditorWidget : public QWidget {
Q_OBJECT
public:
TextEditorWidget(TextEditorView *textEditorView);
void setTextEditor(TextEditor::BaseTextEditor *textEditor);
TextEditor::BaseTextEditor *textEditor() const
{
return m_textEditor.get();
}
QString contextHelpId() const;
private:
std::unique_ptr<TextEditor::BaseTextEditor> m_textEditor;
QPointer<TextEditorView> m_textEditorView;
};
} // namespace QmlDesigner

View File

@@ -400,6 +400,8 @@ Project {
"componentcore/qmldesignericonprovider.h",
"texteditor/texteditorview.cpp",
"texteditor/texteditorview.h",
"texteditor/texteditorwidget.cpp",
"texteditor/texteditorwidget.h",
"debugview/debugview.cpp",
"debugview/debugview.h",
"debugview/debugviewwidget.cpp",