forked from qt-creator/qt-creator
QmlDesigner: refactor textedititem
- disconnect QGraphicsItem code from QmlDesigner nodes code to be able to test the lineedit as a standalone class later Change-Id: I51c533726f4e55a2b84a3415c1eca476f4c785ce Reviewed-by: Tim Jenssen <tim.jenssen@theqtcompany.com>
This commit is contained in:
@@ -26,31 +26,22 @@
|
||||
#include "textedititem.h"
|
||||
|
||||
#include <formeditorscene.h>
|
||||
#include <QTextEdit>
|
||||
#include <QLineEdit>
|
||||
#include <nodemetainfo.h>
|
||||
|
||||
#include <QLineEdit>
|
||||
#include <QTextEdit>
|
||||
|
||||
namespace QmlDesigner {
|
||||
|
||||
TextEditItem::TextEditItem(FormEditorScene* scene)
|
||||
: QGraphicsProxyWidget(),
|
||||
m_lineEdit(new QLineEdit),
|
||||
m_textEdit(new QTextEdit),
|
||||
m_formEditorItem(0)
|
||||
: TextEditItemWidget(scene)
|
||||
, m_formEditorItem(0)
|
||||
{
|
||||
scene->addItem(this);
|
||||
setFlag(QGraphicsItem::ItemIsMovable, false);
|
||||
|
||||
setWidget(m_lineEdit.data());
|
||||
m_lineEdit->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
|
||||
m_lineEdit->setFocus();
|
||||
connect(m_lineEdit.data(), &QLineEdit::returnPressed, this, &TextEditItem::returnPressed);
|
||||
connect(lineEdit(), &QLineEdit::returnPressed, this, &TextEditItem::returnPressed);
|
||||
}
|
||||
|
||||
TextEditItem::~TextEditItem()
|
||||
{
|
||||
setWidget(0);
|
||||
m_formEditorItem = 0;
|
||||
}
|
||||
|
||||
@@ -66,28 +57,20 @@ void TextEditItem::writeTextToProperty()
|
||||
}
|
||||
}
|
||||
|
||||
QString TextEditItem::text() const
|
||||
{
|
||||
if (widget() == m_lineEdit.data())
|
||||
return m_lineEdit->text();
|
||||
else if (widget() == m_textEdit.data())
|
||||
return m_textEdit->toPlainText();
|
||||
return QString();
|
||||
}
|
||||
|
||||
void TextEditItem::setFormEditorItem(FormEditorItem *formEditorItem)
|
||||
{
|
||||
m_formEditorItem = formEditorItem;
|
||||
QRectF rect = formEditorItem->qmlItemNode().instancePaintedBoundingRect().united(formEditorItem->qmlItemNode().instanceBoundingRect()).adjusted(-12, -4, 12 ,4);
|
||||
setGeometry(rect);
|
||||
m_textEdit->setMaximumSize(rect.size().toSize());
|
||||
|
||||
NodeMetaInfo metaInfo = m_formEditorItem->qmlItemNode().modelNode().metaInfo();
|
||||
if (metaInfo.isValid() &&
|
||||
(metaInfo.isSubclassOf("QtQuick.TextEdit", -1, -1)
|
||||
|| metaInfo.isSubclassOf("QtQuick.Controls.TextArea", -1, -1))) {
|
||||
setWidget(m_textEdit.data());
|
||||
m_textEdit->setFocus();
|
||||
QSize maximumSize = rect.size().toSize();
|
||||
activateTextEdit(maximumSize);
|
||||
} else {
|
||||
activateLineEdit();
|
||||
}
|
||||
|
||||
setTransform(formEditorItem->sceneTransform());
|
||||
@@ -102,15 +85,8 @@ FormEditorItem *TextEditItem::formEditorItem() const
|
||||
void TextEditItem::updateText()
|
||||
{
|
||||
if (formEditorItem()) {
|
||||
if (widget() == m_lineEdit.data()) {
|
||||
m_lineEdit->setText(formEditorItem()->qmlItemNode().stripedTranslatableText("text"));
|
||||
m_lineEdit->selectAll();
|
||||
} else if (widget() == m_textEdit.data()) {
|
||||
m_textEdit->setText(formEditorItem()->qmlItemNode().stripedTranslatableText("text"));
|
||||
m_textEdit->selectAll();
|
||||
}
|
||||
TextEditItemWidget::updateText(formEditorItem()->qmlItemNode().
|
||||
stripedTranslatableText("text"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} // namespace QmlDesigner
|
||||
|
||||
@@ -22,32 +22,19 @@
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
#pragma once
|
||||
|
||||
#ifndef TEXTEDITITEM_H
|
||||
#define TEXTEDITITEM_H
|
||||
|
||||
#include <QGraphicsProxyWidget>
|
||||
#include <QWeakPointer>
|
||||
#include <QScopedPointer>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QTextEdit;
|
||||
class QLineEdit;
|
||||
QT_END_NAMESPACE
|
||||
#include "textedititemwidget.h"
|
||||
|
||||
namespace QmlDesigner {
|
||||
|
||||
class FormEditorScene;
|
||||
class FormEditorItem;
|
||||
|
||||
class TextEditItem : public QGraphicsProxyWidget
|
||||
class TextEditItem : public TextEditItemWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum
|
||||
{
|
||||
Type = 0xEAAB
|
||||
};
|
||||
TextEditItem(FormEditorScene* scene);
|
||||
~TextEditItem();
|
||||
int type() const;
|
||||
@@ -55,27 +42,17 @@ public:
|
||||
void setFormEditorItem(FormEditorItem *formEditorItem);
|
||||
FormEditorItem *formEditorItem() const;
|
||||
|
||||
QList<QGraphicsItem*> findAllChildItems() const;
|
||||
|
||||
void updateText();
|
||||
void writeTextToProperty();
|
||||
|
||||
signals:
|
||||
void textChanged(const QString &text);
|
||||
void returnPressed();
|
||||
|
||||
private:
|
||||
QString text() const;
|
||||
|
||||
QScopedPointer<QLineEdit> m_lineEdit;
|
||||
QScopedPointer<QTextEdit> m_textEdit;
|
||||
FormEditorItem *m_formEditorItem;
|
||||
};
|
||||
|
||||
inline int TextEditItem::type() const
|
||||
{
|
||||
return Type;
|
||||
return 0xEAAB;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // TEXTEDITITEM_H
|
||||
} // namespace QmlDesigner
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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 "textedititemwidget.h"
|
||||
|
||||
#include <QTextEdit>
|
||||
#include <QLineEdit>
|
||||
#include <QGraphicsScene>
|
||||
|
||||
namespace QmlDesigner {
|
||||
|
||||
TextEditItemWidget::TextEditItemWidget(QGraphicsScene* scene)
|
||||
: QGraphicsProxyWidget()
|
||||
{
|
||||
scene->addItem(this);
|
||||
setFlag(QGraphicsItem::ItemIsMovable, false);
|
||||
activateLineEdit();
|
||||
}
|
||||
|
||||
TextEditItemWidget::~TextEditItemWidget()
|
||||
{
|
||||
setWidget(0);
|
||||
}
|
||||
|
||||
QLineEdit* TextEditItemWidget::lineEdit() const
|
||||
{
|
||||
if (m_lineEdit.isNull()) {
|
||||
m_lineEdit.reset(new QLineEdit);
|
||||
m_lineEdit->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
|
||||
}
|
||||
return m_lineEdit.data();
|
||||
}
|
||||
|
||||
QTextEdit* TextEditItemWidget::textEdit() const
|
||||
{
|
||||
if (m_textEdit.isNull())
|
||||
m_textEdit.reset(new QTextEdit);
|
||||
return m_textEdit.data();
|
||||
}
|
||||
|
||||
void TextEditItemWidget::activateTextEdit(const QSize &maximumSize)
|
||||
{
|
||||
textEdit()->setMaximumSize(maximumSize);
|
||||
textEdit()->setFocus();
|
||||
setWidget(textEdit());
|
||||
}
|
||||
|
||||
void TextEditItemWidget::activateLineEdit()
|
||||
{
|
||||
lineEdit()->setFocus();
|
||||
setWidget(lineEdit());
|
||||
}
|
||||
|
||||
QString TextEditItemWidget::text() const
|
||||
{
|
||||
if (widget() == m_lineEdit.data())
|
||||
return m_lineEdit->text();
|
||||
else if (widget() == m_textEdit.data())
|
||||
return m_textEdit->toPlainText();
|
||||
return QString();
|
||||
}
|
||||
|
||||
void TextEditItemWidget::updateText(const QString &text)
|
||||
{
|
||||
if (widget() == m_lineEdit.data()) {
|
||||
m_lineEdit->setText(text);
|
||||
m_lineEdit->selectAll();
|
||||
} else if (widget() == m_textEdit.data()) {
|
||||
m_textEdit->setText(text);
|
||||
m_textEdit->selectAll();
|
||||
}
|
||||
}
|
||||
} // namespace QmlDesigner
|
||||
@@ -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 <QGraphicsProxyWidget>
|
||||
#include <QScopedPointer>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QTextEdit;
|
||||
class QLineEdit;
|
||||
class QGraphicsScene;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
namespace QmlDesigner {
|
||||
|
||||
class TextEditItemWidget : public QGraphicsProxyWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
TextEditItemWidget(QGraphicsScene *scene);
|
||||
~TextEditItemWidget();
|
||||
|
||||
void activateTextEdit(const QSize &maximumSize);
|
||||
void activateLineEdit();
|
||||
void updateText(const QString &text);
|
||||
|
||||
protected:
|
||||
QLineEdit* lineEdit() const;
|
||||
QTextEdit* textEdit() const;
|
||||
|
||||
QString text() const;
|
||||
private:
|
||||
mutable QScopedPointer<QLineEdit> m_lineEdit;
|
||||
mutable QScopedPointer<QTextEdit> m_textEdit;
|
||||
};
|
||||
} // namespace QmlDesigner
|
||||
@@ -1,5 +1,7 @@
|
||||
HEADERS += $$PWD/texttool.h
|
||||
HEADERS += $$PWD/textedititem.h
|
||||
HEADERS += $$PWD/textedititemwidget.h
|
||||
|
||||
SOURCES += $$PWD/texttool.cpp
|
||||
SOURCES += $$PWD/textedititem.cpp
|
||||
SOURCES += $$PWD/textedititemwidget.cpp
|
||||
|
||||
Reference in New Issue
Block a user