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/gridcontroller.h" 0021 #include "media/mediatype.h" 0022 0023 #include "controller/view_controller/vectorialmapcontroller.h" 0024 0025 #include <QDebug> 0026 #include <QPainter> 0027 #include <cmath> 0028 #include <math.h> 0029 0030 namespace vmap 0031 { 0032 vmap::GridController::GridController(VectorialMapController* ctrl, QObject* parent) 0033 : VisualItemController(VisualItemController::GRID, std::map<QString, QVariant>(), ctrl, parent) 0034 { 0035 setLayer(Core::Layer::GRIDLAYER); 0036 setVisible(false); 0037 0038 connect(ctrl, &VectorialMapController::gridPatternChanged, this, &vmap::GridController::computePattern); 0039 connect(ctrl, &VectorialMapController::gridColorChanged, this, &vmap::GridController::computePattern); 0040 connect(ctrl, &VectorialMapController::gridVisibilityChanged, this, &vmap::GridController::computePattern); 0041 connect(ctrl, &VectorialMapController::gridSizeChanged, this, &vmap::GridController::computePattern); 0042 connect(ctrl, &VectorialMapController::gridScaleChanged, this, &vmap::GridController::computePattern); 0043 connect(ctrl, &VectorialMapController::gridAboveChanged, this, &vmap::GridController::computePattern); 0044 connect(ctrl, &VectorialMapController::scaleUnitChanged, this, &vmap::GridController::computePattern); 0045 0046 connect(m_ctrl, &VectorialMapController::visualRectChanged, this, &vmap::GridController::rectChanged); 0047 connect(this, &vmap::GridController::rectChanged, this, [this] { setModified(); }); 0048 connect(this, &vmap::GridController::gridPatternChanged, this, [this] { setModified(); }); 0049 0050 0051 computePattern(); 0052 } 0053 bool GridController::gm() const 0054 { 0055 return m_gm; 0056 } 0057 0058 QImage GridController::gridPattern() const 0059 { 0060 return m_gridPattern; 0061 } 0062 0063 void GridController::aboutToBeRemoved() 0064 { 0065 emit removeItem(); 0066 } 0067 0068 void GridController::setCorner(const QPointF& move, int corner, 0069 Core::TransformType tt) 0070 { 0071 } 0072 0073 void GridController::endGeometryChange() {} 0074 0075 void GridController::setGm(bool gm) 0076 { 0077 if(m_gm == gm) 0078 return; 0079 0080 m_gm= gm; 0081 emit gmChanged(m_gm); 0082 } 0083 0084 void GridController::setGridPattern(const QImage &gridPattern) 0085 { 0086 if(m_gridPattern == gridPattern) 0087 return; 0088 0089 m_gridPattern= gridPattern; 0090 emit gridPatternChanged(m_gridPattern); 0091 } 0092 0093 void GridController::setRect(const QRectF& rect) 0094 { 0095 if(m_rect == rect) 0096 return; 0097 0098 m_rect= rect; 0099 emit rectChanged(m_rect); 0100 } 0101 0102 QRectF GridController::rect() const 0103 { 0104 auto rect= m_ctrl->visualRect(); 0105 rect= rect.united(m_rect); 0106 return rect; 0107 } 0108 0109 void GridController::computePattern() 0110 { 0111 if(!m_ctrl) 0112 return; 0113 0114 if(m_ctrl->gridPattern() == Core::GridPattern::NONE || !m_ctrl->gridVisibility() || !m_ctrl->gridAbove()) 0115 setVisible(false); 0116 else 0117 setVisible(true); 0118 0119 QImage pattern; 0120 QPolygonF polygon; 0121 0122 QColor background= m_ctrl->gridAbove() ? Qt::transparent : m_ctrl->backgroundColor(); 0123 0124 if(m_ctrl->gridPattern() == Core::GridPattern::HEXAGON) 0125 { 0126 qreal radius= m_ctrl->gridSize() / 2; 0127 qreal hlimit= radius * std::sin(M_PI / 3); 0128 qreal offset= radius - hlimit; 0129 QPointF A(2 * radius, radius - offset); 0130 QPointF B(radius * 1.5, radius - hlimit - offset); 0131 QPointF C(radius * 0.5, radius - hlimit - offset); 0132 QPointF D(0, radius - offset); 0133 QPointF E(radius * 0.5, radius + hlimit - offset - 1); 0134 QPointF F(radius * 1.5, radius + hlimit - offset - 1); 0135 0136 QPointF G(2 * radius + radius, radius - offset); 0137 polygon << C << D << E << F << A << B << A << G; 0138 0139 pattern 0140 = QImage(static_cast<int>(m_ctrl->gridSize() * 1.5), static_cast<int>(2 * hlimit), QImage::Format_ARGB32); 0141 pattern.fill(background); 0142 } 0143 else if(m_ctrl->gridPattern() == Core::GridPattern::SQUARE) 0144 { 0145 pattern= QImage(m_ctrl->gridSize(), m_ctrl->gridSize(), QImage::Format_ARGB32); 0146 pattern.fill(background); 0147 int sizeP= m_ctrl->gridSize(); 0148 QPointF A(0, 0); 0149 QPointF B(0, sizeP - 1); 0150 QPointF C(sizeP - 1, sizeP - 1); 0151 0152 polygon << A << B << C; 0153 } 0154 if(!pattern.isNull()) 0155 { 0156 QPainter painter(&pattern); 0157 QPen pen; 0158 pen.setColor(m_ctrl->gridColor()); 0159 pen.setWidth(1); 0160 painter.setPen(pen); 0161 painter.drawPolyline(polygon); 0162 painter.end(); 0163 } 0164 setGridPattern(pattern); 0165 } 0166 } // namespace vmap