Commit ad23e7a9 authored by Tim Jenssen's avatar Tim Jenssen Committed by Thomas Hartmann
Browse files

QmlDesigner: move delegates to an extra file



Change-Id: Iaeba3ff7dcfc93a632a93aabc89953781a2a7139
Reviewed-by: default avatarThomas Hartmann <Thomas.Hartmann@theqtcompany.com>
parent 11997e08
......@@ -29,17 +29,12 @@
#include <nodemetainfo.h>
#include <nodeproperty.h>
#include <variantproperty.h>
#include <bindingproperty.h>
#include <variantproperty.h>
#include <rewritingexception.h>
#include <rewritertransaction.h>
#include <utils/fileutils.h>
#include <QItemEditorFactory>
#include <QComboBox>
#include <QMessageBox>
#include <QStyleFactory>
#include <QTimer>
namespace QmlDesigner {
......@@ -463,111 +458,6 @@ void BindingModel::handleException()
resetModel();
}
BindingDelegate::BindingDelegate(QWidget *parent) : QStyledItemDelegate(parent)
{
static QItemEditorFactory *factory = 0;
if (factory == 0) {
factory = new QItemEditorFactory;
QItemEditorCreatorBase *creator
= new QItemEditorCreator<BindingComboBox>("text");
factory->registerEditor(QVariant::String, creator);
}
setItemEditorFactory(factory);
}
QWidget *BindingDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QWidget *widget = QStyledItemDelegate::createEditor(parent, option, index);
const BindingModel *model = qobject_cast<const BindingModel*>(index.model());
model->connectionView()->allModelNodes();
BindingComboBox *bindingComboBox = qobject_cast<BindingComboBox*>(widget);
if (!model) {
qWarning() << "BindingDelegate::createEditor no model";
return widget;
}
if (!model->connectionView()) {
qWarning() << "BindingDelegate::createEditor no connection view";
return widget;
}
if (!bindingComboBox) {
qWarning() << "BindingDelegate::createEditor no bindingComboBox";
return widget;
}
BindingProperty bindingProperty = model->bindingPropertyForRow(index.row());
switch (index.column()) {
case BindingModel::TargetModelNodeRow: {
return 0; //no editor
foreach (const ModelNode &modelNode, model->connectionView()->allModelNodes()) {
if (!modelNode.id().isEmpty()) {
bindingComboBox->addItem(modelNode.id());
}
}
} break;
case BindingModel::TargetPropertyNameRow: {
bindingComboBox->addItems(model->possibleTargetProperties(bindingProperty));
} break;
case BindingModel::SourceModelNodeRow: {
foreach (const ModelNode &modelNode, model->connectionView()->allModelNodes()) {
if (!modelNode.id().isEmpty()) {
bindingComboBox->addItem(modelNode.id());
}
}
if (!bindingProperty.parentModelNode().isRootNode())
bindingComboBox->addItem(QLatin1String("parent"));
} break;
case BindingModel::SourcePropertyNameRow: {
bindingComboBox->addItems(model->possibleSourceProperties(bindingProperty));
} break;
default: qWarning() << "BindingDelegate::createEditor column" << index.column();
}
connect(bindingComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, [=]() {
auto delegate = const_cast<BindingDelegate*>(this);
emit delegate->commitData(bindingComboBox);
// TODO: The combobox does a change while it is opening and this would close it immediately.
// Making sure that this is not connected while data is initialized maybe with using
// QAbstractItemDelegate::setEditorData also this connect should maybe unique.
// emit delegate->closeEditor(bindingComboBox);
});
return widget;
}
void BindingDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
opt.state &= ~QStyle::State_HasFocus;
QStyledItemDelegate::paint(painter, opt, index);
}
BindingComboBox::BindingComboBox(QWidget *parent) : QComboBox(parent)
{
static QScopedPointer<QStyle> style(QStyleFactory::create(QLatin1String("windows")));
setEditable(true);
if (style)
setStyle(style.data());
}
QString BindingComboBox::text() const
{
return currentText();
}
void BindingComboBox::setText(const QString &text)
{
setEditText(text);
}
} // namespace Internal
} // namespace QmlDesigner
......@@ -25,18 +25,13 @@
#pragma once
#include <modelnode.h>
#include <nodemetainfo.h>
#include <bindingproperty.h>
#include <QStandardItem>
#include <QStyledItemDelegate>
#include <QStandardItemModel>
#include <QComboBox>
namespace QmlDesigner {
class Model;
class BindingProperty;
class ModelNode;
namespace Internal {
class ConnectionView;
......@@ -92,31 +87,6 @@ private:
};
class BindingDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
BindingDelegate(QWidget *parent = 0);
virtual QWidget *createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
};
class BindingComboBox : public QComboBox
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText USER true)
public:
BindingComboBox(QWidget *parent = 0);
QString text() const;
void setText(const QString &text);
};
} // namespace Internal
} // namespace QmlDesigner
VPATH += $$PWD
INCLUDEPATH += $$PWD
HEADERS += connectionview.h \
HEADERS += delegates.h \
connectionview.h \
connectionviewwidget.h \
connectionmodel.h \
bindingmodel.h \
dynamicpropertiesmodel.h
SOURCES += connectionview.cpp \
SOURCES += delegates.cpp \
connectionview.cpp \
connectionviewwidget.cpp \
connectionmodel.cpp \
bindingmodel.cpp \
......
......@@ -32,30 +32,15 @@
#include <rewritertransaction.h>
#include <nodeabstractproperty.h>
#include <exception.h>
#include <nodemetainfo.h>
#include <QStandardItemModel>
#include <QMessageBox>
#include <QTableView>
#include <QTimer>
#include <QItemEditorFactory>
#include <QStyleFactory>
namespace {
QStringList prependOnForSignalHandler(const QStringList &signalNames)
{
QStringList signalHandlerNames;
foreach (const QString &signalName, signalNames) {
QString signalHandlerName = signalName;
if (!signalHandlerName.isEmpty()) {
QChar firstChar = signalHandlerName.at(0).toUpper();
signalHandlerName[0] = firstChar;
signalHandlerName.prepend(QLatin1String("on"));
signalHandlerNames.append(signalHandlerName);
}
}
return signalHandlerNames;
}
QStringList propertyNameListToStringList(const QmlDesigner::PropertyNameList &propertyNameList)
{
QStringList stringList;
......@@ -390,119 +375,6 @@ QStringList ConnectionModel::getPossibleSignalsForConnection(const ModelNode &co
return stringList;
}
ConnectionDelegate::ConnectionDelegate(QWidget *parent) : QStyledItemDelegate(parent)
{
static QItemEditorFactory *factory = 0;
if (factory == 0) {
factory = new QItemEditorFactory;
QItemEditorCreatorBase *creator
= new QItemEditorCreator<ConnectionComboBox>("text");
factory->registerEditor(QVariant::String, creator);
}
setItemEditorFactory(factory);
}
QWidget *ConnectionDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QWidget *widget = QStyledItemDelegate::createEditor(parent, option, index);
const ConnectionModel *connectionModel = qobject_cast<const ConnectionModel*>(index.model());
//model->connectionView()->allModelNodes();
ConnectionComboBox *connectionComboBox = qobject_cast<ConnectionComboBox*>(widget);
if (!connectionModel) {
qWarning() << "ConnectionDelegate::createEditor no model";
return widget;
}
if (!connectionModel->connectionView()) {
qWarning() << "ConnectionDelegate::createEditor no connection view";
return widget;
}
if (!connectionComboBox) {
qWarning() << "ConnectionDelegate::createEditor no bindingComboBox";
return widget;
}
switch (index.column()) {
case ConnectionModel::TargetModelNodeRow: {
foreach (const ModelNode &modelNode, connectionModel->connectionView()->allModelNodes()) {
if (!modelNode.id().isEmpty()) {
connectionComboBox->addItem(modelNode.id());
}
}
} break;
case ConnectionModel::TargetPropertyNameRow: {
connectionComboBox->addItems(prependOnForSignalHandler(connectionModel->getSignalsForRow(index.row())));
} break;
case ConnectionModel::SourceRow: {
ModelNode rootModelNode = connectionModel->connectionView()->rootModelNode();
if (QmlItemNode::isValidQmlItemNode(rootModelNode) && !rootModelNode.id().isEmpty()) {
QString itemText = tr("Change to default state");
QString source = QString::fromLatin1("{ %1.state = \"\" }").arg(rootModelNode.id());
connectionComboBox->addItem(itemText, source);
foreach (const QmlModelState &state, QmlItemNode(rootModelNode).states().allStates()) {
QString itemText = tr("Change state to %1").arg(state.name());
QString source = QString::fromLatin1("{ %1.state = \"%2\" }").arg(rootModelNode.id()).arg(state.name());
connectionComboBox->addItem(itemText, source);
}
}
} break;
default: qWarning() << "ConnectionDelegate::createEditor column" << index.column();
}
connect(connectionComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, [=]() {
auto delegate = const_cast<ConnectionDelegate*>(this);
emit delegate->commitData(connectionComboBox);
// TODO: The combobox does a change while it is opening and this would close it immediately.
// Making sure that this is not connected while data is initialized maybe with using
// QAbstractItemDelegate::setEditorData also this connect should maybe unique.
// emit delegate->closeEditor(connectionComboBox);
});
return widget;
}
void ConnectionDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
opt.state &= ~QStyle::State_HasFocus;
QStyledItemDelegate::paint(painter, opt, index);
}
ConnectionComboBox::ConnectionComboBox(QWidget *parent) : QComboBox(parent)
{
static QScopedPointer<QStyle> style(QStyleFactory::create(QLatin1String("windows")));
setEditable(true);
if (style)
setStyle(style.data());
}
QString ConnectionComboBox::text() const
{
int index = findText(currentText());
if (index > -1) {
QVariant variantData = itemData(index);
if (variantData.isValid())
return variantData.toString();
}
return currentText();
}
void ConnectionComboBox::setText(const QString &text)
{
setEditText(text);
}
} // namespace Internal
} // namespace QmlDesigner
......@@ -25,20 +25,14 @@
#pragma once
#include <modelnode.h>
#include <nodemetainfo.h>
#include <bindingproperty.h>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QComboBox>
namespace QmlDesigner {
class Model;
class AbstractView;
class ModelNode;
class BindingProperty;
class SignalHandlerProperty;
class VariantProperty;
namespace Internal {
......@@ -90,32 +84,6 @@ private:
QString m_exceptionError;
};
class ConnectionDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
ConnectionDelegate(QWidget *parent = 0);
virtual QWidget *createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
};
class ConnectionComboBox : public QComboBox
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText USER true)
public:
ConnectionComboBox(QWidget *parent = 0);
QString text() const;
void setText(const QString &text);
};
} // namespace Internal
} // namespace QmlDesigner
......@@ -27,6 +27,7 @@
#include "connectionview.h"
#include "ui_connectionviewwidget.h"
#include "delegates.h"
#include "bindingmodel.h"
#include "connectionmodel.h"
#include "dynamicpropertiesmodel.h"
......
/****************************************************************************
**
** 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 "delegates.h"
#include "connectionmodel.h"
#include "bindingmodel.h"
#include "dynamicpropertiesmodel.h"
#include "connectionview.h"
#include <bindingproperty.h>
#include <QStyleFactory>
#include <QItemEditorFactory>
#include <QDebug>
namespace QmlDesigner {
namespace Internal {
QStringList prependOnForSignalHandler(const QStringList &signalNames)
{
QStringList signalHandlerNames;
foreach (const QString &signalName, signalNames) {
QString signalHandlerName = signalName;
if (!signalHandlerName.isEmpty()) {
QChar firstChar = signalHandlerName.at(0).toUpper();
signalHandlerName[0] = firstChar;
signalHandlerName.prepend(QLatin1String("on"));
signalHandlerNames.append(signalHandlerName);
}
}
return signalHandlerNames;
}
PropertiesComboBox::PropertiesComboBox(QWidget *parent) : QComboBox(parent)
{
static QScopedPointer<QStyle> style(QStyleFactory::create(QLatin1String("windows")));
setEditable(true);
if (style)
setStyle(style.data());
}
QString PropertiesComboBox::text() const
{
return currentText();
}
void PropertiesComboBox::setText(const QString &text)
{
setEditText(text);
}
ConnectionComboBox::ConnectionComboBox(QWidget *parent) : PropertiesComboBox(parent)
{
}
QString ConnectionComboBox::text() const
{
int index = findText(currentText());
if (index > -1) {
QVariant variantData = itemData(index);
if (variantData.isValid())
return variantData.toString();
}
return currentText();
}
ConnectionEditorDelegate::ConnectionEditorDelegate(QWidget *parent)
: QStyledItemDelegate(parent)
{
}
void ConnectionEditorDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
opt.state &= ~QStyle::State_HasFocus;
QStyledItemDelegate::paint(painter, opt, index);
}
BindingDelegate::BindingDelegate(QWidget *parent) : ConnectionEditorDelegate(parent)
{
static QItemEditorFactory *factory = 0;
if (factory == 0) {
factory = new QItemEditorFactory;
QItemEditorCreatorBase *creator
= new QItemEditorCreator<PropertiesComboBox>("text");
factory->registerEditor(QVariant::String, creator);
}
setItemEditorFactory(factory);
}
QWidget *BindingDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QWidget *widget = QStyledItemDelegate::createEditor(parent, option, index);
const BindingModel *model = qobject_cast<const BindingModel*>(index.model());
model->connectionView()->allModelNodes();
PropertiesComboBox *bindingComboBox = qobject_cast<PropertiesComboBox*>(widget);
if (!model) {
qWarning() << "BindingDelegate::createEditor no model";
return widget;
}
if (!model->connectionView()) {
qWarning() << "BindingDelegate::createEditor no connection view";
return widget;
}
if (!bindingComboBox) {
qWarning() << "BindingDelegate::createEditor no bindingComboBox";
return widget;
}
BindingProperty bindingProperty = model->bindingPropertyForRow(index.row());
switch (index.column()) {
case BindingModel::TargetModelNodeRow: {
return 0; //no editor
foreach (const ModelNode &modelNode, model->connectionView()->allModelNodes()) {
if (!modelNode.id().isEmpty()) {
bindingComboBox->addItem(modelNode.id());
}
}
} break;
case BindingModel::TargetPropertyNameRow: {
bindingComboBox->addItems(model->possibleTargetProperties(bindingProperty));