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

0001 /***************************************************************************
0002  *   Copyright (C) 2015 by Renaud Guezennec                                *
0003  *   https://rolisteam.org/contact                   *
0004  *                                                                         *
0005  *   rolisteam 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 
0021 #include "ruleitem.h"
0022 
0023 #include "controller/view_controller/vectorialmapcontroller.h"
0024 #include <QPainter>
0025 #include <cmath>
0026 
0027 #define FONT_SIZE 15
0028 
0029 QString unitToText(Core::ScaleUnit unit)
0030 {
0031     QString result;
0032     switch(unit)
0033     {
0034     case Core::M:
0035         result= QStringLiteral("m");
0036         break;
0037     case Core::CM:
0038         result= QStringLiteral("cm");
0039         break;
0040     case Core::INCH:
0041         result= QStringLiteral("″");
0042         break;
0043     case Core::FEET:
0044         result= QStringLiteral("′");
0045         break;
0046     case Core::YARD:
0047         result= QStringLiteral("yd");
0048         break;
0049     case Core::MILE:
0050         result= QStringLiteral("mi");
0051         break;
0052     case Core::KM:
0053         result= QStringLiteral("km");
0054         break;
0055     case Core::PX:
0056         result= QStringLiteral("px");
0057         break;
0058     }
0059     return result;
0060 }
0061 
0062 RuleItem::RuleItem(VectorialMapController* ctrl) : QGraphicsObject(), m_ctrl(ctrl) {}
0063 
0064 RuleItem::~RuleItem() {}
0065 QRectF RuleItem::boundingRect() const
0066 {
0067     return QRectF(m_startPoint, m_endPoint);
0068 }
0069 
0070 void RuleItem::setNewEnd(const QPointF& nendConst, bool onAxis)
0071 {
0072     if(nendConst.isNull())
0073         return;
0074     if(!onAxis)
0075     {
0076         m_endPoint+= nendConst;
0077         return;
0078     }
0079 
0080     QLineF line(m_startPoint, nendConst);
0081     if(std::fabs(line.dx()) > std::fabs(line.dy()))
0082     {
0083         m_endPoint= QPointF(m_endPoint.x() + nendConst.x(), 0);
0084     }
0085     else
0086     {
0087         m_endPoint= QPointF(0, m_endPoint.y() + nendConst.y());
0088     }
0089 }
0090 void RuleItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
0091 {
0092     Q_UNUSED(option)
0093     Q_UNUSED(widget)
0094 
0095     painter->save();
0096     painter->setRenderHint(QPainter::Antialiasing, true);
0097 
0098     m_pen.setWidth(m_ctrl->penSize() * 1 / m_ctrl->zoomLevel());
0099     m_pen.setColor(Qt::red);
0100     painter->setPen(m_pen);
0101     QLineF line(m_startPoint, m_endPoint);
0102 
0103     painter->drawLine(line);
0104     painter->restore();
0105 
0106     qreal len= line.length();
0107 
0108     qreal unitCount= len / m_ctrl->gridSize() * m_ctrl->gridScale();
0109 
0110     QFont f= painter->font();
0111     f.setPixelSize(FONT_SIZE * 1 / m_ctrl->zoomLevel());
0112     painter->setFont(f);
0113 
0114     auto text= QString("%1 %2").arg(QString::number(unitCount, 'f', 2), unitToText(m_ctrl->scaleUnit()));
0115 
0116     QFontMetricsF metrics(f);
0117     auto rect= metrics.boundingRect(text);
0118     painter->save();
0119     painter->fillRect(rect.translated(m_endPoint), QBrush("#88FFFFFF"));
0120     painter->restore();
0121     painter->drawText(m_endPoint, text);
0122 }