File indexing completed on 2024-04-28 05:38:11
0001 /*************************************************************************** 0002 * Copyright (C) 2010 by Renaud Guezennec * 0003 * * 0004 * * 0005 * rolisteam is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program 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 General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include "ellipsitem.h" 0021 0022 #include <QPainter> 0023 #include <QStyleOptionGraphicsItem> 0024 0025 #include <cmath> 0026 #include <math.h> 0027 0028 #include "controller/item_controllers/ellipsecontroller.h" 0029 0030 EllipsItem::EllipsItem(vmap::EllipseController* ctrl) : VisualItem(ctrl), m_ellipseCtrl(ctrl) 0031 { 0032 connect(m_ellipseCtrl, &vmap::EllipseController::rxChanged, this, 0033 [this]() 0034 { 0035 setTransformOriginPoint(boundingRect().center()); 0036 updateChildPosition(); 0037 }); 0038 connect(m_ellipseCtrl, &vmap::EllipseController::ryChanged, this, 0039 [this]() 0040 { 0041 setTransformOriginPoint(boundingRect().center()); 0042 updateChildPosition(); 0043 }); 0044 connect(m_ellipseCtrl, &vmap::EllipseController::filledChanged, this, [this]() { update(); }); 0045 0046 initChildPointItem(); 0047 } 0048 0049 QRectF EllipsItem::boundingRect() const 0050 { 0051 return m_ellipseCtrl ? m_ellipseCtrl->rect() : QRectF{}; 0052 } 0053 0054 QPainterPath EllipsItem::shape() const 0055 { 0056 QPainterPath path; 0057 path.addEllipse(boundingRect()); 0058 0059 if(!m_ellipseCtrl || m_ellipseCtrl->filled()) 0060 return path; 0061 0062 QPainterPath subpath; 0063 0064 QRectF rect= boundingRect(); 0065 auto penW= m_ellipseCtrl->penWidth(); 0066 rect.adjust(penW, penW, -penW, -penW); 0067 subpath.addEllipse(rect); 0068 path-= subpath; 0069 0070 return path; 0071 } 0072 void EllipsItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) 0073 { 0074 Q_UNUSED(option) 0075 Q_UNUSED(widget) 0076 painter->save(); 0077 if(!m_ellipseCtrl->filled()) 0078 { 0079 QPen pen= painter->pen(); 0080 pen.setColor(m_ellipseCtrl->color()); 0081 pen.setWidth(m_ellipseCtrl->penWidth()); 0082 painter->setPen(pen); 0083 } 0084 else 0085 { 0086 painter->setPen(Qt::NoPen); 0087 painter->setBrush(QBrush(m_ellipseCtrl->color(), Qt::SolidPattern)); 0088 } 0089 0090 setChildrenVisible(hasFocusOrChild()); 0091 0092 painter->drawEllipse(QPointF(0, 0), m_ellipseCtrl->rx(), m_ellipseCtrl->ry()); 0093 0094 painter->restore(); 0095 0096 if(canBeMoved() && (option->state & QStyle::State_MouseOver || isSelected())) 0097 { 0098 painter->save(); 0099 QPen pen= painter->pen(); 0100 pen.setColor(isSelected() ? m_selectedColor : m_highlightColor); 0101 pen.setWidth(m_highlightWidth); 0102 painter->setPen(pen); 0103 painter->drawEllipse(QPointF(0, 0), m_ellipseCtrl->rx(), m_ellipseCtrl->ry()); 0104 painter->restore(); 0105 } 0106 } 0107 void EllipsItem::setNewEnd(const QPointF& p) 0108 { 0109 m_ellipseCtrl->setCorner(p, 2); 0110 } 0111 0112 void EllipsItem::updateChildPosition() 0113 { 0114 auto rect= boundingRect(); 0115 m_children.value(0)->setPos(QPointF(rect.right(), rect.center().y())); 0116 m_children.value(0)->setPlacement(ChildPointItem::MiddleRight); 0117 m_children.value(1)->setPos(QPointF(rect.center().x(), rect.bottom())); 0118 m_children.value(1)->setPlacement(ChildPointItem::ButtomCenter); 0119 update(); 0120 } 0121 0122 void EllipsItem::initChildPointItem() 0123 { 0124 if(!m_ellipseCtrl) 0125 return; 0126 setTransformOriginPoint(boundingRect().center()); 0127 0128 for(int i= 0; i < 2; ++i) 0129 { 0130 ChildPointItem* tmp= new ChildPointItem(m_ctrl, i, this); 0131 m_children.append(tmp); 0132 } 0133 m_children.value(0)->setPos(m_ellipseCtrl->rx(), 0); 0134 m_children.value(0)->setPlacement(ChildPointItem::MiddleRight); 0135 m_children.value(0)->setMotion(ChildPointItem::X_AXIS | ChildPointItem::ROTATION); 0136 m_children.value(1)->setPos(0, m_ellipseCtrl->ry()); 0137 m_children.value(1)->setMotion(ChildPointItem::Y_AXIS | ChildPointItem::ROTATION); 0138 m_children.value(1)->setPlacement(ChildPointItem::ButtomCenter); 0139 }