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/linecontroller.h"
0021 
0022 #include <QVariant>
0023 
0024 #include "controller/view_controller/vectorialmapcontroller.h"
0025 #include "worker/utilshelper.h"
0026 #include "media/mediatype.h"
0027 
0028 namespace vmap
0029 {
0030 LineController::LineController(const std::map<QString, QVariant>& params, VectorialMapController* ctrl, QObject* parent)
0031     : VisualItemController(VisualItemController::LINE, params, ctrl, parent)
0032 {
0033     m_tool= Core::SelectableTool::LINE;
0034 
0035     helper::utils::setParamIfAny<QColor>(Core::vmapkeys::KEY_COLOR, params, [this](const QColor& color) {
0036         setColor(color);
0037     });
0038     helper::utils::setParamIfAny<quint16>(Core::vmapkeys::KEY_PENWIDTH, params, [this](const quint16& penWidth) {
0039        m_penWidth = penWidth;
0040     });
0041     helper::utils::setParamIfAny<QPointF>(Core::vmapkeys::KEY_POS, params, [this](const QPointF& pos) {
0042         m_pos = pos;
0043     });
0044 
0045     connect(this, &LineController::startPointChanged, this, [this] { setModified(); });
0046     connect(this, &LineController::endPointChanged, this, [this] { setModified(); });
0047     connect(this, &LineController::penWidthChanged, this, [this] { setModified(); });
0048 }
0049 
0050 quint16 LineController::penWidth() const
0051 {
0052     return m_penWidth;
0053 }
0054 
0055 QPointF LineController::endPoint() const
0056 {
0057     return m_end;
0058 }
0059 
0060 QPointF LineController::startPoint() const
0061 {
0062     return m_start;
0063 }
0064 
0065 void LineController::aboutToBeRemoved()
0066 {
0067     emit removeItem();
0068 }
0069 
0070 void LineController::endGeometryChange()
0071 {
0072     VisualItemController::endGeometryChange();
0073 
0074     if(m_editEndPoint)
0075     {
0076         emit endPointEditFinished();
0077         m_editEndPoint= false;
0078     }
0079 
0080     if(m_editStartPoint)
0081     {
0082         emit startPointEditFinished();
0083         m_editStartPoint= false;
0084     }
0085 }
0086 
0087 QRectF LineController::rect() const
0088 {
0089     return QRectF(startPoint(), endPoint()).normalized();
0090 }
0091 
0092 void LineController::setCorner(const QPointF& move, int corner,
0093                                Core::TransformType tt)
0094 {
0095     switch(corner)
0096     {
0097     case Start:
0098         setStartPoint(m_start + move);
0099         break;
0100     case End:
0101         setEndPoint(m_end + move);
0102         break;
0103     }
0104 }
0105 
0106 void LineController::setStartPoint(const QPointF& p)
0107 {
0108     if(p == m_start)
0109         return;
0110     m_start= p;
0111     emit startPointChanged();
0112     m_editStartPoint= true;
0113 }
0114 
0115 void LineController::setEndPoint(const QPointF& p)
0116 {
0117     if(p == m_end)
0118         return;
0119     m_end= p;
0120     emit endPointChanged();
0121     m_editEndPoint= true;
0122 }
0123 } // namespace vmap