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

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2013-2014 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 "Handle.h"
0021 
0022 #include <QPainter>
0023 #include <QStyleOptionGraphicsItem>
0024 #include <QGraphicsSceneMouseEvent>
0025 #include <QGraphicsView>
0026 
0027 #include <QDebug>
0028 
0029 namespace tikz {
0030 namespace ui {
0031 
0032 Handle::Handle(Type type, Position position)
0033     : TikzItem()
0034     , m_type(type)
0035     , m_position(position)
0036 {
0037     // by default, only left mouse button triggers events
0038     setAcceptedMouseButtons(Qt::LeftButton);
0039 
0040     // show above paths and nodes
0041     setZValue(10.0);
0042 
0043     // force visually the same size, independent of zooming
0044     setFlag(ItemIgnoresTransformations, true);
0045 
0046     // set correct rect size
0047     setRect(QRectF(-4, -4, 8, 8));
0048 }
0049 
0050 Handle::~Handle()
0051 {
0052 }
0053 
0054 int Handle::type() const
0055 {
0056     return UserType + 5;
0057 }
0058 
0059 Handle::Position Handle::handlePos() const
0060 {
0061     return m_position;
0062 }
0063 
0064 Handle::Type Handle::handleType() const
0065 {
0066     return m_type;
0067 }
0068 
0069 QRectF Handle::rect() const
0070 {
0071     return m_handleRect;
0072 }
0073 
0074 void Handle::setRect(const QRectF & rect)
0075 {
0076     prepareGeometryChange();
0077     m_handleRect = rect;
0078     update();
0079 }
0080 
0081 void Handle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
0082 {
0083     Q_UNUSED(widget);
0084     Q_UNUSED(option);
0085 
0086     // debugging
0087 //     painter->drawRect(boundingRect());
0088 }
0089 
0090 QRectF Handle::boundingRect() const
0091 {
0092     return m_handleRect.adjusted(0, 0, 1, 1);
0093 }
0094 
0095 bool Handle::isActive() const
0096 {
0097     return m_active;
0098 }
0099 
0100 void Handle::activate()
0101 {
0102     if (!m_active) {
0103         m_active = true;
0104         update();
0105     }
0106 }
0107 
0108 void Handle::deactivate()
0109 {
0110     if (m_active) {
0111         m_active = false;
0112         update();
0113     }
0114 }
0115 
0116 /**
0117  * Helper to find the QGraphicsView that sent this @p event.
0118  * The return value may be 0 for user-generated mouse events.
0119  */
0120 static QGraphicsView * viewForEvent(QGraphicsSceneEvent * event)
0121 {
0122     QWidget *viewport = event->widget();
0123     if (viewport) {
0124         return qobject_cast<QGraphicsView *>(viewport->parent());
0125     }
0126     return nullptr;
0127 }
0128 
0129 void Handle::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
0130 {
0131     Q_EMIT positionChanged(this, event->scenePos(), viewForEvent(event));
0132 
0133     event->accept();
0134 }
0135 
0136 void Handle::mousePressEvent(QGraphicsSceneMouseEvent * event)
0137 {
0138     if (event->button() == Qt::LeftButton) {
0139         Q_EMIT mousePressed(this, event->scenePos(), viewForEvent(event));
0140 
0141         activate();
0142 
0143         event->accept();
0144     }
0145 }
0146 
0147 void Handle::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
0148 {
0149     if (event->button() == Qt::LeftButton) {
0150         Q_EMIT mouseReleased(this, event->scenePos(), viewForEvent(event));
0151 
0152         deactivate();
0153 
0154         event->accept();
0155     }
0156 }
0157 
0158 }
0159 }
0160 
0161 // kate: indent-width 4; replace-tabs on;