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/rectcontroller.h"
0021 
0022 #include "controller/view_controller/vectorialmapcontroller.h"
0023 #include "worker/utilshelper.h"
0024 
0025 namespace vmap
0026 {
0027 RectController::RectController(const std::map<QString, QVariant>& params, VectorialMapController* ctrl, QObject* parent)
0028     : VisualItemController(VisualItemController::RECT, params, ctrl, parent)
0029 {
0030     namespace hu= helper::utils;
0031     namespace cv= Core::vmapkeys;
0032     using std::placeholders::_1;
0033 
0034     std::function<void(Core::SelectableTool)> lambda= [this](Core::SelectableTool s)
0035     {
0036         m_tool= s;
0037         m_filled= (m_tool == Core::SelectableTool::FILLRECT);
0038     };
0039     std::function<void(bool)> lambda2= [this](bool s)
0040     {
0041         m_filled= s;
0042         m_tool= m_filled ? Core::SelectableTool::FILLRECT : Core::SelectableTool::EMPTYRECT;
0043     };
0044 
0045     hu::setParamIfAny<Core::SelectableTool>(cv::KEY_TOOL, params, lambda);
0046     hu::setParamIfAny<bool>(cv::KEY_FILLED, params, lambda2);
0047     hu::setParamIfAny<quint16>(cv::KEY_PENWIDTH, params, std::bind(&RectController::setPenWidth, this, _1));
0048     hu::setParamIfAny<QRectF>(cv::KEY_RECT, params, std::bind(&RectController::setRect, this, _1));
0049 
0050     connect(this, &RectController::rectChanged, this, [this] { setModified(); });
0051     connect(this, &RectController::filledChanged, this, [this] { setModified(); });
0052     connect(this, &RectController::penWidthChanged, this, [this] { setModified(); });
0053 }
0054 
0055 bool RectController::filled() const
0056 {
0057     return m_filled;
0058 }
0059 
0060 QRectF RectController::rect() const
0061 {
0062     return m_rect;
0063 }
0064 
0065 void RectController::setRect(QRectF rect)
0066 {
0067     if(rect == m_rect)
0068         return;
0069     m_rect= rect;
0070     emit rectChanged();
0071     m_rectEdited= true;
0072 }
0073 
0074 void RectController::setPenWidth(qreal w)
0075 {
0076     if(qFuzzyCompare(w, m_penWidth))
0077         return;
0078     m_penWidth= w;
0079     emit penWidthChanged();
0080 }
0081 
0082 void RectController::setCorner(const QPointF& move, int corner, Core::TransformType tt)
0083 {
0084     if(move.isNull())
0085         return;
0086 
0087     auto rect= m_rect;
0088     qreal x2= rect.right();
0089     qreal y2= rect.bottom();
0090     qreal x= rect.x();
0091     qreal y= rect.y();
0092     switch(corner)
0093     {
0094     case TopLeft:
0095         x+= move.x();
0096         y+= move.y();
0097         break;
0098     case TopRight:
0099         x2+= move.x();
0100         y+= move.y();
0101         break;
0102     case BottomRight:
0103         x2+= move.x();
0104         y2+= move.y();
0105         break;
0106     case BottomLeft:
0107         x+= move.x();
0108         y2+= move.y();
0109         break;
0110     }
0111     rect.setCoords(x, y, x2, y2);
0112     if(!rect.isValid())
0113         rect= rect.normalized();
0114     setRect(rect);
0115 }
0116 
0117 void RectController::aboutToBeRemoved()
0118 {
0119     emit removeItem();
0120 }
0121 
0122 void RectController::endGeometryChange()
0123 {
0124     VisualItemController::endGeometryChange();
0125     if(m_rectEdited)
0126     {
0127         emit rectEditFinished();
0128         m_rectEdited= false;
0129     }
0130 }
0131 
0132 quint16 RectController::penWidth() const
0133 {
0134     return m_penWidth;
0135 }
0136 } // namespace vmap