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 "lineitem.h" 0021 #include <QPainter> 0022 #include <QPainterPath> 0023 #include <QStyleOptionGraphicsItem> 0024 #include "controller/item_controllers/visualitemcontroller.h" 0025 0026 #include "controller/item_controllers/linecontroller.h" 0027 #include <QDebug> 0028 0029 #include <math.h> 0030 #define PI 3.14159265 0031 0032 LineItem::LineItem(vmap::LineController* ctrl) : VisualItem(ctrl), m_lineCtrl(ctrl) 0033 { 0034 auto func= [this]() 0035 { 0036 updateChildPosition(); 0037 update(); 0038 }; 0039 connect(m_lineCtrl, &vmap::LineController::endPointChanged, this, func); 0040 connect(m_lineCtrl, &vmap::LineController::startPointChanged, this, func); 0041 0042 for(int i= 0; i <= vmap::LineController::End; ++i) 0043 { 0044 ChildPointItem* tmp= new ChildPointItem(m_lineCtrl, i, this); 0045 tmp->setMotion(ChildPointItem::MOVE); 0046 m_children.append(tmp); 0047 } 0048 updateChildPosition(); 0049 } 0050 0051 void LineItem::setNewEnd(const QPointF& nend) 0052 { 0053 m_lineCtrl->setCorner(nend, 1); 0054 // m_rect.setBottomRight(nend); 0055 } 0056 QRectF LineItem::boundingRect() const 0057 { 0058 return m_lineCtrl->rect(); 0059 } 0060 QPainterPath LineItem::shape() const 0061 { 0062 QLineF line(m_lineCtrl->startPoint(), m_lineCtrl->endPoint()); 0063 line.setLength(line.length() + m_lineCtrl->penWidth() / 2.0); 0064 QLineF line2(line.p2(), line.p1()); 0065 line2.setLength(line2.length() + m_lineCtrl->penWidth() / 2.0); 0066 line.setPoints(line2.p2(), line2.p1()); 0067 0068 QLineF normal= line.normalVector(); 0069 normal.setLength(m_lineCtrl->penWidth() / 2.0); 0070 auto start= normal.p2(); 0071 auto end= normal.pointAt(-1); 0072 QLineF normal2= line2.normalVector(); 0073 normal2.setLength(m_lineCtrl->penWidth() / 2.0); 0074 auto p2= normal2.p2(); 0075 auto p3= normal2.pointAt(-1); 0076 0077 QPainterPath path; 0078 path.moveTo(start); 0079 path.lineTo(p3); 0080 path.lineTo(p2); 0081 path.lineTo(end); 0082 path.lineTo(start); 0083 return path; 0084 } 0085 void LineItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) 0086 { 0087 Q_UNUSED(widget) 0088 painter->save(); 0089 auto pen= painter->pen(); 0090 pen.setColor(m_lineCtrl->color()); 0091 pen.setWidth(m_lineCtrl->penWidth()); 0092 painter->setPen(pen); 0093 painter->drawLine(m_lineCtrl->startPoint(), m_lineCtrl->endPoint()); 0094 setChildrenVisible(hasFocusOrChild()); 0095 painter->restore(); 0096 0097 if(canBeMoved() && (option->state & QStyle::State_MouseOver || isSelected())) 0098 { 0099 painter->save(); 0100 auto shapePath= shape(); 0101 QPen pen= painter->pen(); 0102 pen.setColor(isSelected() ? m_selectedColor : m_highlightColor); 0103 pen.setWidth(m_highlightWidth); 0104 painter->setPen(pen); 0105 painter->drawPath(shapePath); 0106 painter->restore(); 0107 } 0108 } 0109 0110 /*void LineItem::setRectSize(qreal x, qreal y, qreal w, qreal h) 0111 { 0112 m_rect.setX(x); 0113 m_rect.setY(y); 0114 m_rect.setWidth(w); 0115 m_rect.setHeight(h); 0116 0117 m_startPoint= m_rect.topLeft(); 0118 m_endPoint= m_rect.bottomRight(); 0119 }*/ 0120 0121 void LineItem::updateChildPosition() 0122 { 0123 if(!m_lineCtrl) 0124 return; 0125 0126 m_children.value(0)->setPos(m_lineCtrl->startPoint()); 0127 m_children.value(0)->setPlacement(ChildPointItem::Center); 0128 m_children.value(1)->setPos(m_lineCtrl->endPoint()); 0129 m_children.value(1)->setPlacement(ChildPointItem::Center); 0130 } 0131 0132 /*void LineItem::endOfGeometryChange(ChildPointItem::Change change) 0133 { 0134 if(change == ChildPointItem::Resizing) 0135 { 0136 auto oldScenePos= scenePos(); 0137 setTransformOriginPoint(boundingRect().center()); 0138 auto newScenePos= scenePos(); 0139 auto oldPos= pos(); 0140 m_lineCtrl->setPos(QPointF(oldPos.x() + (oldScenePos.x() - newScenePos.x()), 0141 oldPos.y() + (oldScenePos.y() - newScenePos.y()))); 0142 } 0143 VisualItem::endOfGeometryChange(change); 0144 }*/