File indexing completed on 2024-04-28 05:38:11
0001 /*************************************************************************** 0002 * Copyright (C) 2010 by Renaud Guezennec * 0003 * * 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 #include "highlighteritem.h" 0021 0022 #include <QDebug> 0023 #include <QPainter> 0024 #include <QPropertyAnimation> 0025 0026 #include "preferences/preferencesmanager.h" 0027 #include <cmath> 0028 #include <math.h> 0029 0030 HighlighterItem::HighlighterItem(const QPointF& center, int penSize, const QColor& penColor, QGraphicsItem* parent, 0031 bool autoDestruction) 0032 : QGraphicsObject(parent), m_center(center), m_color(penColor), m_penSize(static_cast<quint16>(penSize)) 0033 { 0034 setZValue(std::numeric_limits<qreal>::max()); 0035 m_center= center; 0036 setPos(m_center); 0037 m_center.setX(0); 0038 m_center.setY(0); 0039 m_radius= 0; 0040 0041 initAnimation(autoDestruction); 0042 } 0043 0044 void HighlighterItem::initAnimation(bool autoDestruction) 0045 { 0046 if(autoDestruction) 0047 { 0048 auto const preferences= PreferencesManager::getInstance(); 0049 m_animation= new QPropertyAnimation(this, "radius"); 0050 m_animation->setDuration(preferences->value("Map_Highlighter_time", 1000).toInt()); 0051 m_animation->setStartValue(0); 0052 m_animation->setEndValue(preferences->value("Map_Highlighter_radius", 100).toInt()); 0053 m_animation->setEasingCurve(QEasingCurve::Linear); 0054 m_animation->setLoopCount(preferences->value("Map_Highlighter_loop", 3).toInt()); 0055 0056 connect(m_animation, &QPropertyAnimation::finished, this, 0057 [this]() 0058 { 0059 // setVisible(false); 0060 // emit itemRemoved(m_id, true, false); 0061 deleteLater(); 0062 }); 0063 m_animation->start(); 0064 } 0065 } 0066 QRectF HighlighterItem::boundingRect() const 0067 { 0068 return {0, 0, m_radius * 2, m_radius * 2}; 0069 } 0070 QPainterPath HighlighterItem::shape() const 0071 { 0072 QPainterPath path; 0073 path.addEllipse(boundingRect()); 0074 return path; 0075 } 0076 void HighlighterItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) 0077 { 0078 Q_UNUSED(option) 0079 Q_UNUSED(widget) 0080 painter->save(); 0081 QPen pen= painter->pen(); 0082 pen.setColor(m_color); 0083 pen.setWidth(m_penSize); 0084 painter->setPen(pen); 0085 0086 painter->drawEllipse(m_center, m_radius, m_radius); 0087 0088 painter->restore(); 0089 } 0090 void HighlighterItem::setRadius(qreal radius) 0091 { 0092 if(qFuzzyCompare(radius, m_radius)) 0093 return; 0094 0095 m_radius= radius; 0096 update(); 0097 emit radiusChanged(); 0098 } 0099 qreal HighlighterItem::getRadius() const 0100 { 0101 return m_radius; 0102 }