File indexing completed on 2024-04-28 05:38:12

0001 /***************************************************************************
0002  *      Copyright (C) 2010 by Renaud Guezennec                             *
0003  *                                                                         *
0004  *   rolisteam is free software; you can redistribute it and/or modify     *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0018  ***************************************************************************/
0019 
0020 #include "rectitem.h"
0021 #include <QDebug>
0022 #include <QPainter>
0023 #include <QPen>
0024 #include <QStyle>
0025 #include <QStyleOptionGraphicsItem>
0026 #include <QStylePainter>
0027 #include <QTimer>
0028 
0029 #include "controller/item_controllers/rectcontroller.h"
0030 #include "controller/item_controllers/visualitemcontroller.h"
0031 
0032 #include <QDebug>
0033 
0034 RectItem::RectItem(vmap::RectController* ctrl) : VisualItem(ctrl), m_rectCtrl(ctrl)
0035 {
0036     connect(m_rectCtrl, &vmap::RectController::rectChanged, this, [this]() { updateChildPosition(); });
0037 
0038     for(int i= 0; i <= vmap::RectController::BottomLeft; ++i)
0039     {
0040         ChildPointItem* tmp= new ChildPointItem(m_rectCtrl, i, this);
0041         tmp->setMotion(ChildPointItem::MOUSE);
0042         m_children.append(tmp);
0043     }
0044 
0045     updateChildPosition();
0046 
0047     if(m_ctrl)
0048     {
0049         setTransformOriginPoint(m_ctrl->rotationOriginPoint());
0050         setRotation(m_ctrl->rotation());
0051     }
0052 }
0053 
0054 QRectF RectItem::boundingRect() const
0055 {
0056     return m_rectCtrl ? m_rectCtrl->rect() : QRectF();
0057 }
0058 QPainterPath RectItem::shape() const
0059 {
0060     if(!m_rectCtrl)
0061         return {};
0062 
0063     if(m_rectCtrl->filled())
0064         return VisualItem::shape();
0065 
0066     QPainterPath path;
0067     qreal halfPenSize= m_rectCtrl->penWidth() / 2.0;
0068     auto rect= m_rectCtrl->rect();
0069     qreal off= 0.5 * halfPenSize;
0070 
0071     path.moveTo(rect.topLeft().x() - off, rect.topLeft().y() - off);
0072 
0073     path.lineTo(rect.topRight().x() + off, rect.topRight().y() - off);
0074     path.lineTo(rect.bottomRight().x() + off, rect.bottomRight().y() + off);
0075     path.lineTo(rect.bottomLeft().x() - off, rect.bottomLeft().y() + off);
0076     path.lineTo(rect.topLeft().x() - off, rect.topLeft().y() - off);
0077 
0078     path.lineTo(rect.topLeft().x() + off, rect.topLeft().y() + off);
0079     path.lineTo(rect.topRight().x() - off, rect.topRight().y() + off);
0080     path.lineTo(rect.bottomRight().x() - off, rect.bottomRight().y() - off);
0081     path.lineTo(rect.bottomLeft().x() + off, rect.bottomLeft().y() - off);
0082     path.lineTo(rect.topLeft().x() + off, rect.topLeft().y() + off);
0083     return path;
0084 }
0085 void RectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
0086 {
0087     Q_UNUSED(option)
0088     Q_UNUSED(widget)
0089     if(!m_rectCtrl)
0090         return;
0091     painter->save();
0092     if(!m_rectCtrl->filled())
0093     {
0094         QPen pen= painter->pen();
0095         pen.setColor(m_rectCtrl->color());
0096         pen.setWidth(m_rectCtrl->penWidth());
0097         painter->setPen(pen);
0098         painter->drawRect(m_rectCtrl->rect());
0099     }
0100     else
0101     {
0102         painter->setBrush(QBrush(m_rectCtrl->color()));
0103         painter->fillRect(m_rectCtrl->rect(), m_rectCtrl->color());
0104     }
0105     setChildrenVisible(hasFocusOrChild());
0106     painter->restore();
0107 
0108     auto val= static_cast<bool>(option->state & QStyle::State_MouseOver);
0109 
0110     if(canBeMoved() && (isSelected() || val)) // option->state & QStyle::State_MouseOver || isUnderMouse())
0111     {
0112         painter->save();
0113         QPen pen= painter->pen();
0114         pen.setColor(isSelected() ? m_selectedColor : m_highlightColor);
0115         pen.setWidth(m_highlightWidth);
0116         painter->setPen(pen);
0117         painter->drawRect(m_rectCtrl->rect());
0118         painter->restore();
0119     }
0120 }
0121 void RectItem::setNewEnd(const QPointF& p)
0122 {
0123     m_ctrl->setCorner(p, 2);
0124 }
0125 
0126 void RectItem::updateChildPosition()
0127 {
0128     if(!m_rectCtrl)
0129         return;
0130 
0131     auto rect= m_rectCtrl->rect();
0132     m_children.value(0)->setPos(rect.topLeft());
0133     m_children.value(0)->setPlacement(ChildPointItem::TopLeft);
0134     m_children.value(1)->setPos(rect.topRight());
0135     m_children.value(1)->setPlacement(ChildPointItem::TopRight);
0136     m_children.value(2)->setPos(rect.bottomRight());
0137     m_children.value(2)->setPlacement(ChildPointItem::ButtomRight);
0138     m_children.value(3)->setPos(rect.bottomLeft());
0139     m_children.value(3)->setPlacement(ChildPointItem::ButtomLeft);
0140 
0141     if(!m_ctrl->localIsGM())
0142     {
0143         setTransformOriginPoint(m_rectCtrl->rect().center());
0144     }
0145 
0146     update();
0147 }
0148