File indexing completed on 2024-05-19 05:40:34

0001 /***************************************************************************
0002  *  Copyright (C) 2019 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software 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 "controller/item_controllers/ellipsecontroller.h"
0021 
0022 #include <QVariant>
0023 
0024 namespace vmap
0025 {
0026 EllipseController::EllipseController(const std::map<QString, QVariant>& params, VectorialMapController* ctrl,
0027                                      QObject* parent)
0028     : VisualItemController(VisualItemController::ELLIPSE, params, ctrl, parent)
0029 {
0030     namespace cv= Core::vmapkeys;
0031     if(params.end() != params.find(cv::KEY_TOOL))
0032     {
0033         m_tool= params.at(cv::KEY_TOOL).value<Core::SelectableTool>();
0034         m_filled= (m_tool == Core::SelectableTool::FILLEDELLIPSE);
0035     }
0036 
0037     if(params.end() != params.find(cv::KEY_FILLED))
0038     {
0039         m_filled= params.at(cv::KEY_FILLED).toBool();
0040         m_tool= m_filled ? Core::SelectableTool::FILLEDELLIPSE : Core::SelectableTool::EMPTYELLIPSE;
0041     }
0042 
0043     if(params.end() != params.find(cv::KEY_PENWIDTH))
0044         m_penWidth= static_cast<quint16>(params.at(cv::KEY_PENWIDTH).toInt());
0045 
0046     if(params.end() != params.find(cv::KEY_RX))
0047         m_rx= params.at(cv::KEY_RX).toReal();
0048 
0049     if(params.end() != params.find(cv::KEY_RY))
0050         m_ry= params.at(cv::KEY_RY).toReal();
0051 
0052     connect(this, &EllipseController::filledChanged, this, [this] { setModified(); });
0053     connect(this, &EllipseController::penWidthChanged, this, [this] { setModified(); });
0054     connect(this, &EllipseController::ryChanged, this, [this] { setModified(); });
0055     connect(this, &EllipseController::rxChanged, this, [this] { setModified(); });
0056 }
0057 
0058 bool EllipseController::filled() const
0059 {
0060     return m_filled;
0061 }
0062 
0063 quint16 EllipseController::penWidth() const
0064 {
0065     return m_penWidth;
0066 }
0067 
0068 qreal EllipseController::rx() const
0069 {
0070     return m_rx;
0071 }
0072 
0073 qreal EllipseController::ry() const
0074 {
0075     return m_ry;
0076 }
0077 
0078 void EllipseController::aboutToBeRemoved()
0079 {
0080     emit removeItem();
0081 }
0082 
0083 void EllipseController::endGeometryChange()
0084 {
0085     VisualItemController::endGeometryChange();
0086     if(m_editingRx)
0087     {
0088         emit rxEditionChanged();
0089         m_editingRx= false;
0090     }
0091 
0092     if(m_editingRy)
0093     {
0094         emit ryEditionChanged();
0095         m_editingRy= false;
0096     }
0097 }
0098 
0099 QRectF EllipseController::rect() const
0100 {
0101     return QRectF(-rx(), -ry(), rx() * 2, ry() * 2);
0102 }
0103 
0104 void EllipseController::setCorner(const QPointF& move, int corner, Core::TransformType tt)
0105 {
0106     if(move.isNull())
0107         return;
0108 
0109     auto rx= m_rx;
0110     auto ry= m_ry;
0111     switch(corner)
0112     {
0113     case 0:
0114         rx+= move.x();
0115         break;
0116     case 1:
0117         ry+= move.y();
0118         break;
0119     case 2:
0120         rx+= move.x() / 2;
0121         ry+= move.y() / 2;
0122         break;
0123     }
0124     setRx(rx);
0125     setRy(ry);
0126 }
0127 
0128 void EllipseController::setRx(qreal rx)
0129 {
0130     if(qFuzzyCompare(rx, m_rx) || qFuzzyIsNull(rx))
0131         return;
0132     m_rx= rx;
0133     emit rxChanged();
0134     m_editingRx= true;
0135 }
0136 
0137 void EllipseController::setRy(qreal ry)
0138 {
0139     if(qFuzzyCompare(ry, m_ry) || qFuzzyIsNull(ry))
0140         return;
0141     m_ry= ry;
0142     emit ryChanged();
0143     m_editingRy= true;
0144 }
0145 } // namespace vmap