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

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/textcontroller.h"
0021 
0022 #include <QFontMetrics>
0023 
0024 #define MARGIN 6
0025 
0026 bool greater(const QRectF& r1, const QRectF& r2)
0027 {
0028     return r1.width() * r1.height() > r2.width() * r2.height();
0029 }
0030 
0031 namespace vmap
0032 {
0033 TextController::TextController(const std::map<QString, QVariant>& params, VectorialMapController* ctrl, QObject* parent)
0034     : VisualItemController(VisualItemController::TEXT, params, ctrl, parent)
0035 {
0036     if(params.end() != params.find("color"))
0037         setColor(params.at(QStringLiteral("color")).value<QColor>());
0038 
0039     if(params.end() != params.find("tool"))
0040     {
0041         m_tool= params.at(QStringLiteral("tool")).value<Core::SelectableTool>();
0042         m_border= (m_tool == Core::SelectableTool::TEXTBORDER);
0043     }
0044     else if(params.end() != params.find("border"))
0045     {
0046         m_border= params.at(QStringLiteral("border")).toBool();
0047         m_tool= m_border ? Core::SelectableTool::TEXTBORDER : Core::SelectableTool::TEXT;
0048     }
0049 
0050     if(params.end() != params.find("text"))
0051         setText(params.at(QStringLiteral("text")).toString());
0052 
0053     if(params.end() != params.find("textPos"))
0054         setTextPos(params.at(QStringLiteral("textPos")).toPointF());
0055 
0056     if(params.end() != params.find("textRect"))
0057         setTextRect(params.at(QStringLiteral("textRect")).toRectF());
0058 
0059     if(params.end() != params.find("rect"))
0060         setBorderRect(params.at(QStringLiteral("rect")).toRectF());
0061 
0062     if(params.end() != params.find("penWidth"))
0063         m_penWidth= static_cast<quint16>(params.at(QStringLiteral("penWidth")).toInt());
0064 
0065     if(m_text.isEmpty())
0066         setText(tr("Text"));
0067 
0068     connect(this, &TextController::textChanged, this, [this] { setModified(); });
0069     connect(this, &TextController::textRectChanged, this, [this] { setModified(); });
0070     connect(this, &TextController::borderRectChanged, this, [this] { setModified(); });
0071     connect(this, &TextController::borderChanged, this, [this] { setModified(); });
0072     connect(this, &TextController::fontChanged, this, [this] { setModified(); });
0073     connect(this, &TextController::textPosChanged, this, [this] { setModified(); });
0074 }
0075 QString TextController::text() const
0076 {
0077     return m_text;
0078 }
0079 void TextController::setText(QString text)
0080 {
0081     if(m_text == text)
0082         return;
0083 
0084     m_text= text;
0085     emit textChanged(m_text);
0086 }
0087 
0088 QRectF TextController::textRect() const
0089 {
0090     return m_textRect;
0091 }
0092 
0093 QRectF TextController::borderRect() const
0094 {
0095     return m_borderRect;
0096 }
0097 
0098 bool TextController::border() const
0099 {
0100     return m_border;
0101 }
0102 void TextController::aboutToBeRemoved()
0103 {
0104     emit removeItem();
0105 }
0106 
0107 void TextController::setBorderRect(QRectF rect)
0108 {
0109     if(m_borderRect == rect || !greater(rect, textRect()))
0110         return;
0111 
0112     m_borderRect= rect;
0113     emit borderRectChanged(m_borderRect);
0114     m_editingBorderRect= true;
0115 }
0116 
0117 void TextController::setTextRect(QRectF rect)
0118 {
0119     if(m_textRect == rect)
0120         return;
0121 
0122     m_textRect= rect;
0123     emit textRectChanged(m_textRect);
0124     updateSize();
0125     updateTextPosition();
0126 }
0127 QFont TextController::font() const
0128 {
0129     return m_font;
0130 }
0131 void TextController::setFont(QFont font)
0132 {
0133     if(m_font == font)
0134         return;
0135 
0136     m_font= font;
0137     emit fontChanged(m_font);
0138 }
0139 
0140 void TextController::updateSize()
0141 {
0142     if(greater(textRect(), borderRect()))
0143     {
0144         setBorderRect(textRect().adjusted(0, 0, 0, MARGIN));
0145     }
0146 }
0147 
0148 void TextController::increaseFontSize()
0149 {
0150     m_font.setPointSize(m_font.pointSize() + 1);
0151     updateSize();
0152 }
0153 
0154 void TextController::decreaseFontSize()
0155 {
0156     if(m_font.pointSize() < 5)
0157         return;
0158     m_font.setPointSize(m_font.pointSize() - 1);
0159     updateSize();
0160 }
0161 
0162 void TextController::endGeometryChange()
0163 {
0164 
0165     VisualItemController::endGeometryChange();
0166     if(m_editingBorderRect)
0167     {
0168         m_editingBorderRect= false;
0169         emit borderRectEditFinished();
0170     }
0171     /* auto rect= m_rect;
0172      auto pos= m_pos;
0173      qreal w= rect.width();
0174      qreal h= rect.height();
0175      rect.setCoords(-w / 2, -h / 2, w, h);
0176      pos+= QPointF(w / 2, h / 2);
0177      setRect(rect);
0178      setPos(pos);*/
0179 }
0180 
0181 QRectF TextController::rect() const
0182 {
0183     return borderRect();
0184 }
0185 
0186 void TextController::setTextPos(QPointF textPos)
0187 {
0188     if(m_textPos == textPos)
0189         return;
0190 
0191     m_textPos= textPos;
0192     emit textPosChanged(m_textPos);
0193 }
0194 QPointF TextController::textPos() const
0195 {
0196     return m_textPos;
0197 }
0198 
0199 quint16 TextController::penWidth() const
0200 {
0201     return m_penWidth;
0202 }
0203 
0204 void TextController::updateTextPosition()
0205 {
0206     setTextPos(QPointF(m_borderRect.x(), m_borderRect.center().y() - textRect().height() / 2));
0207 }
0208 
0209 void TextController::setCorner(const QPointF& move, int corner,
0210                                Core::TransformType tt)
0211 {
0212     if(move.isNull())
0213         return;
0214 
0215     auto rect= m_borderRect;
0216     qreal x2= rect.right();
0217     qreal y2= rect.bottom();
0218     qreal x= rect.x();
0219     qreal y= rect.y();
0220     switch(corner)
0221     {
0222     case TopLeft:
0223         x+= move.x();
0224         y+= move.y();
0225         break;
0226     case TopRight:
0227         x2+= move.x();
0228         y+= move.y();
0229         break;
0230     case BottomRight:
0231         x2+= move.x();
0232         y2+= move.y();
0233         break;
0234     case BottomLeft:
0235         x+= move.x();
0236         y2+= move.y();
0237         break;
0238     }
0239     rect.setCoords(x, y, x2, y2);
0240     if(!rect.isValid())
0241         rect= rect.normalized();
0242     setBorderRect(rect);
0243 
0244     // updateTextPosition();
0245 }
0246 } // namespace vmap