Commit fc212ce7 authored by Marco Bubke's avatar Marco Bubke

QmlDesigner.FormEditor: Remove Anchor mode

Change-Id: I03935715e51777efffea533310a9d775cb676dca
Reviewed-by: default avatarAlessandro Portale <alessandro.portale@nokia.com>
parent 6a0e01cb
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: http://www.qt-project.org/
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**************************************************************************/
#ifndef ANCHORCONTROLLER_H
#define ANCHORCONTROLLER_H
#include <QSharedPointer>
#include <QPainterPath>
#include <QPair>
#include <QTransform>
#include <qmlanchors.h>
namespace QmlDesigner {
struct AnchorHandlePathData
{
QPainterPath sourceAnchorLinePath;
QPainterPath targetAnchorLinePath;
QPainterPath arrowPath;
QPainterPath targetNamePath;
QPointF beginArrowPoint;
QPointF endArrowPoint;
};
class FormEditorItem;
class LayerItem;
class AnchorHandleItem;
class AnchorControllerData
{
public:
AnchorControllerData(LayerItem *layerItem,
FormEditorItem *formEditorItem);
AnchorControllerData(const AnchorControllerData &other);
~AnchorControllerData();
QWeakPointer<LayerItem> layerItem;
FormEditorItem *formEditorItem;
AnchorHandleItem *topItem;
AnchorHandleItem *leftItem;
AnchorHandleItem *rightItem;
AnchorHandleItem *bottomItem;
QTransform sceneTransform;
};
class AnchorController
{
public:
AnchorController();
AnchorController(LayerItem *layerItem, FormEditorItem *formEditorItem);
AnchorController(const QSharedPointer<AnchorControllerData> &data);
void show();
void hide();
void updatePosition();
bool isValid() const;
QWeakPointer<AnchorControllerData> weakPointer() const;
FormEditorItem *formEditorItem() const;
bool isTopHandle(const AnchorHandleItem *handle) const;
bool isLeftHandle(const AnchorHandleItem *handle) const;
bool isRightHandle(const AnchorHandleItem *handle) const;
bool isBottomHandle(const AnchorHandleItem *handle) const;
void updateTargetPoint(AnchorLine::Type anchorLine, const QPointF &targetPoint);
void clearHighlight();
void highlight(AnchorLine::Type anchorLine);
private: //functions
AnchorHandlePathData createPainterPathForAnchor(const QRectF &boundingRect,
AnchorLine::Type anchorLine,
const QPointF &targetPoint = QPointF()) const;
QPainterPath createTargetAnchorLinePath(AnchorLine::Type anchorLine) const;
QPainterPath createTargetNamePathPath(AnchorLine::Type anchorLine) const;
private:
QSharedPointer<AnchorControllerData> m_data;
};
} // namespace QmlDesigner
#endif // ANCHORCONTROLLER_H
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: http://www.qt-project.org/
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**************************************************************************/
#include "anchorhandleitem.h"
#include <formeditoritem.h>
#include <QPen>
#include <QPainterPathStroker>
#include <cmath>
#include <QDebug>
namespace QmlDesigner {
AnchorHandleItem::AnchorHandleItem(QGraphicsItem *parent, const AnchorController &anchorController)
: QGraphicsItemGroup(parent),
m_anchorControllerData(anchorController.weakPointer()),
m_sourceAnchorLinePathItem(new QGraphicsPathItem(this)),
m_arrowPathItem(new QGraphicsPathItem(this)),
m_targetAnchorLinePathItem(new QGraphicsPathItem(this)),
m_targetNamePathItem(new QGraphicsPathItem(this))
{
addToGroup(m_sourceAnchorLinePathItem);
addToGroup(m_arrowPathItem);
addToGroup(m_targetAnchorLinePathItem);
addToGroup(m_targetNamePathItem);
setFlag(QGraphicsItem::ItemIsMovable, true);
}
AnchorLine::Type AnchorHandleItem::sourceAnchorLine() const
{
if (isTopHandle())
return AnchorLine::Top;
if (isBottomHandle())
return AnchorLine::Bottom;
if (isLeftHandle())
return AnchorLine::Left;
if (isRightHandle())
return AnchorLine::Right;
return AnchorLine::Invalid;
}
AnchorLine AnchorHandleItem::targetAnchorLine() const
{
QmlAnchors anchors(anchorController().formEditorItem()->qmlItemNode().anchors());
if (isTopHandle())
return anchors.instanceAnchor(AnchorLine::Top);
if (isBottomHandle())
return anchors.instanceAnchor(AnchorLine::Bottom);
if (isLeftHandle())
return anchors.instanceAnchor(AnchorLine::Left);
if (isRightHandle())
return anchors.instanceAnchor(AnchorLine::Right);
return AnchorLine();
}
static QString anchorLineToString(AnchorLine::Type anchorLineType)
{
switch(anchorLineType) {
case AnchorLine::Top: return "Top";
case AnchorLine::Bottom: return "Bottom";
case AnchorLine::Left: return "Left";
case AnchorLine::Right: return "Right";
default: break;
}
return QString();
}
QString AnchorHandleItem::toolTipString() const
{
QString templateString("<p>Anchor Handle</p><p>%1</p><p>%2</p>");
QmlItemNode fromNode(anchorController().formEditorItem()->qmlItemNode());
QString fromString(QString("%3: %1(%2)").arg(fromNode.simplifiedTypeName(), fromNode.id(), anchorLineToString(sourceAnchorLine())));
AnchorLine toAnchorLine(targetAnchorLine());
QmlItemNode toNode(toAnchorLine.qmlItemNode());
QString toString;
if (toNode.isValid())
toString = QString("%3: %1(%2)").arg(toNode.simplifiedTypeName(), toNode.id(), anchorLineToString(toAnchorLine.type()));
return templateString.arg(fromString).arg(toString);
}
void AnchorHandleItem::setHandlePath(const AnchorHandlePathData &pathData)
{
m_beginArrowPoint = pathData.beginArrowPoint;
m_endArrowPoint = pathData.endArrowPoint;
m_arrowPathItem->setPath(pathData.arrowPath);
m_sourceAnchorLinePathItem->setPath(pathData.sourceAnchorLinePath);
m_targetAnchorLinePathItem->setPath(pathData.targetAnchorLinePath);
m_targetNamePathItem->setPath(pathData.targetNamePath);
setHighlighted(false);
}
AnchorController AnchorHandleItem::anchorController() const
{
Q_ASSERT(!m_anchorControllerData.isNull());
return AnchorController(m_anchorControllerData.toStrongRef());
}
AnchorHandleItem* AnchorHandleItem::fromGraphicsItem(QGraphicsItem *item)
{
return qgraphicsitem_cast<AnchorHandleItem*>(item);
}
bool AnchorHandleItem::isTopHandle() const
{
return anchorController().isTopHandle(this);
}
bool AnchorHandleItem::isLeftHandle() const
{
return anchorController().isLeftHandle(this);
}
bool AnchorHandleItem::isRightHandle() const
{
return anchorController().isRightHandle(this);
}
bool AnchorHandleItem::isBottomHandle() const
{
return anchorController().isBottomHandle(this);
}
AnchorLine::Type AnchorHandleItem::anchorType() const
{
if (isTopHandle())
return AnchorLine::Top;
if (isBottomHandle())
return AnchorLine::Bottom;
if (isLeftHandle())
return AnchorLine::Left;
if (isRightHandle())
return AnchorLine::Right;
return AnchorLine::Invalid;
}
void AnchorHandleItem::setHighlighted(bool highlight)
{
QLinearGradient gradient(m_beginArrowPoint, m_endArrowPoint);
gradient.setCoordinateMode(QGradient::LogicalMode);
m_arrowPathItem->setPen(QPen(QBrush(Qt::gray), 1.0, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
m_targetAnchorLinePathItem->setPen(QColor(70, 0, 0, 90));
m_targetAnchorLinePathItem->setBrush(QColor(255, 0, 0, 50));
m_arrowPathItem->setPen(QPen(QBrush(Qt::gray), 1.0, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
m_targetNamePathItem->setPen(QColor(0, 0, 255, 90));
m_targetNamePathItem->setBrush(QColor(0, 0, 255, 50));
if (highlight) {
gradient.setColorAt(0.0, QColor(0, 0, 120, 255));
gradient.setColorAt(1.0, QColor(120, 0, 0, 255));
m_arrowPathItem->setBrush(gradient);
m_sourceAnchorLinePathItem->setPen(QColor(0, 0, 70, 255));
m_sourceAnchorLinePathItem->setBrush(QColor(0, 0, 70, 255));
m_targetAnchorLinePathItem->show();
m_targetNamePathItem->show();
} else {
gradient.setColorAt(0.0, QColor(0, 0, 255, 255));
gradient.setColorAt(1.0, QColor(255, 0, 0, 255));
m_arrowPathItem->setBrush(gradient);
m_sourceAnchorLinePathItem->setPen(QColor(0, 0, 100, 255));
m_sourceAnchorLinePathItem->setBrush(QColor(0, 0, 100, 255));
m_targetAnchorLinePathItem->hide();
m_targetNamePathItem->hide();
}
}
QPointF AnchorHandleItem::itemSpacePosition() const
{
return parentItem()->mapToItem(anchorController().formEditorItem(), pos());
}
} // namespace QmlDesigner
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: http://www.qt-project.org/
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**************************************************************************/
#ifndef ANCHORHANDLEITEM_H
#define ANCHORHANDLEITEM_H
#include <QGraphicsItemGroup>
#include "anchorcontroller.h"
namespace QmlDesigner {
class AnchorHandleItem : public QGraphicsItemGroup
{
public:
enum
{
Type = 0xEAEC
};
AnchorHandleItem(QGraphicsItem *parent, const AnchorController &anchorController);
void setHandlePath(const AnchorHandlePathData &pathData);
int type() const;
AnchorController anchorController() const;
static AnchorHandleItem* fromGraphicsItem(QGraphicsItem *item);
bool isTopHandle() const;
bool isLeftHandle() const;
bool isRightHandle() const;
bool isBottomHandle() const;
QPointF itemSpacePosition() const;
AnchorLine::Type anchorType() const;
QString toolTipString() const;
AnchorLine targetAnchorLine() const;
AnchorLine::Type sourceAnchorLine() const;
void setHighlighted(bool highlight);
private:
QWeakPointer<AnchorControllerData> m_anchorControllerData;
QGraphicsPathItem *m_sourceAnchorLinePathItem;
QGraphicsPathItem *m_arrowPathItem;
QGraphicsPathItem *m_targetAnchorLinePathItem;
QGraphicsPathItem *m_targetNamePathItem;
QPointF m_beginArrowPoint;
QPointF m_endArrowPoint;
};
inline int AnchorHandleItem::type() const
{
return Type;
}
} // namespace QmlDesigner
#endif // ANCHORHANDLEITEM_H
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: http://www.qt-project.org/
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**************************************************************************/
#include "anchorindicator.h"
#include <QSet>
namespace QmlDesigner {
AnchorIndicator::AnchorIndicator(LayerItem *layerItem)
: m_layerItem(layerItem)
{
Q_ASSERT(layerItem);
}
AnchorIndicator::~AnchorIndicator()
{
m_itemControllerHash.clear();
}
void AnchorIndicator::show()
{
QHashIterator<FormEditorItem*, AnchorController> itemControllerIterator(m_itemControllerHash);
while (itemControllerIterator.hasNext()) {
AnchorController controller = itemControllerIterator.next().value();
controller.show();
}
}
void AnchorIndicator::hide()
{
QHashIterator<FormEditorItem*, AnchorController> itemControllerIterator(m_itemControllerHash);
while (itemControllerIterator.hasNext()) {
AnchorController controller = itemControllerIterator.next().value();
controller.hide();
}
}
void AnchorIndicator::clear()
{
m_itemControllerHash.clear();
}
void AnchorIndicator::setItems(const QList<FormEditorItem*> &itemList)
{
clear();
foreach (FormEditorItem *item, itemList) {
AnchorController controller(m_layerItem, item);
m_itemControllerHash.insert(item, controller);
}
updateItems(itemList);
}
void AnchorIndicator::updateItems(const QList<FormEditorItem*> &itemList)
{
foreach (FormEditorItem *item, itemList) {
if (m_itemControllerHash.contains(item)) {
AnchorController controller(m_itemControllerHash.value(item));
controller.updatePosition();
}
}
}
void AnchorIndicator::updateTargetPoint(FormEditorItem *item, AnchorLine::Type anchorLine, const QPointF &targetPoint)
{
AnchorController controller(m_itemControllerHash.value(item));
controller.updateTargetPoint(anchorLine, targetPoint);
}
void AnchorIndicator::clearHighlight()
{
QHashIterator<FormEditorItem*, AnchorController> itemControllerIterator(m_itemControllerHash);
while (itemControllerIterator.hasNext()) {
AnchorController controller = itemControllerIterator.next().value();
controller.clearHighlight();
}
}
void AnchorIndicator::highlight(FormEditorItem *item, AnchorLine::Type anchorLine)
{
if (m_itemControllerHash.contains(item)) {
AnchorController controller(m_itemControllerHash.value(item));
controller.highlight(anchorLine);
}
}
} // namespace QmlDesigner
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: http://www.qt-project.org/
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**************************************************************************/
#ifndef ANCHORINDICATOR_H
#define ANCHORINDICATOR_H
#include "anchorcontroller.h"
#include <QList>
#include <QHash>
namespace QmlDesigner {
class AnchorIndicator
{
public:
AnchorIndicator(LayerItem *layerItem);
~AnchorIndicator();
void show();
void hide();
void clear();
void setItems(const QList<FormEditorItem*> &itemList);
void updateItems(const QList<FormEditorItem*> &itemList);
void updateTargetPoint(FormEditorItem *item, AnchorLine::Type anchorLine, const QPointF &targetPoint);
void clearHighlight();
void highlight(FormEditorItem *item, AnchorLine::Type anchorLine);
private:
QHash<FormEditorItem*, AnchorController> m_itemControllerHash;
LayerItem *m_layerItem;
};