File indexing completed on 2024-05-19 04:36:34

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2013 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "AnchorHandle.h"
0021 #include "NodeItem.h"
0022 #include "DocumentPrivate.h"
0023 
0024 #include <QPointer>
0025 #include <QPainter>
0026 #include <QGraphicsScene>
0027 #include <QGraphicsView>
0028 #include <QStyle>
0029 #include <QStyleOptionGraphicsItem>
0030 #include <QEvent>
0031 #include <QGraphicsSceneMouseEvent>
0032 
0033 #include <QDebug>
0034 
0035 namespace tikz {
0036 namespace ui {
0037 
0038 AnchorHandle::AnchorHandle(NodeItem * node, const QString & anchor)
0039     : Handle(Handle::AnchorHandle)
0040     , m_metaPos(node->document())
0041     , m_node(node)
0042 {
0043     m_metaPos.setNode(node->node());
0044     m_metaPos.setAnchor(anchor);
0045 
0046     // set transform property correctly in case of NoAnchor type
0047     if (anchor.isEmpty()) {
0048         // no anchor means the tracked area spans the entire contents of the node,
0049         // therewith, view transformations must not be ignored
0050         setFlag(ItemIgnoresTransformations, false);
0051 
0052         // do not paint anything, since this Anchor represents the entire node
0053         setFlag(ItemHasNoContents, true);
0054 
0055         // track the rotation of the node
0056         // NOTE: right now, QGraphicsItem::scale() is not used, therewith we omit it here.
0057         setRotation(node->rotation());
0058     }
0059 
0060     // set position depending on the anchor
0061     setPos(node->anchor(anchor));
0062 }
0063 
0064 AnchorHandle::~AnchorHandle()
0065 {
0066 }
0067 
0068 int AnchorHandle::type() const
0069 {
0070     return UserType + 4;
0071 }
0072 
0073 NodeItem * AnchorHandle::node() const
0074 {
0075     return m_node;
0076 }
0077 
0078 QString AnchorHandle::anchor() const
0079 {
0080     return m_metaPos.anchor();
0081 }
0082 
0083 const tikz::core::MetaPos & AnchorHandle::metaPos() const
0084 {
0085     return m_metaPos;
0086 }
0087 
0088 void AnchorHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
0089 {
0090     Q_UNUSED(widget);
0091     Q_UNUSED(option);
0092 
0093     // see 'ItemHasNoContents' in constructor
0094     Q_ASSERT(! anchor().isEmpty());
0095 
0096     painter->save();
0097     painter->setRenderHints(QPainter::Antialiasing);
0098 
0099     painter->setPen(QColor(164, 0, 0)); // dark red
0100     painter->setBrush(isHovered() ? Qt::yellow : QColor(221, 99, 99));
0101 
0102     if (!isActive()) {
0103         painter->setOpacity(0.5);
0104     }
0105 
0106     painter->drawEllipse(rect());
0107 
0108     painter->restore();
0109 }
0110 
0111 QRectF AnchorHandle::boundingRect() const
0112 {
0113     if (anchor().isEmpty()) {
0114         return m_node->boundingRect();
0115     } else {
0116         return Handle::boundingRect();
0117     }
0118 }
0119 
0120 bool AnchorHandle::contains(const QPointF &point) const
0121 {
0122     if (anchor().isEmpty()) {
0123         return m_node->contains(point);
0124     }
0125 
0126     return Handle::contains(point);
0127 }
0128 
0129 }
0130 }
0131 
0132 // kate: indent-width 4; replace-tabs on;