File indexing completed on 2022-11-23 10:43:25

0001 /*
0002     KBlackBox - A simple game inspired by an emacs module
0003 
0004     SPDX-FileCopyrightText: 1999-2000 Robert Cimrman <cimrman3@students.zcu.cz>
0005     SPDX-FileCopyrightText: 2007 Nicolas Roffet <nicolas-kde@roffet.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "kbbgraphicsitemrayresult.h"
0011 
0012 
0013 
0014 #include <QFont>
0015 #include <QGraphicsScene>
0016 
0017 
0018 #include "kbbscalablegraphicwidget.h"
0019 #include "kbbthememanager.h"
0020 
0021 
0022 
0023 //
0024 // Constructor / Destructor
0025 //
0026 
0027 KBBGraphicsItemRayResult::KBBGraphicsItemRayResult( KBBScalableGraphicWidget* parent, KBBThemeManager* themeManager, QGraphicsScene* scene, const int borderPosition, const int columns, const int rows, const int rayNumber) : KBBGraphicsItemBorder( borderPosition, columns, rows, KBBScalableGraphicWidget::BORDER_SIZE/2), KBBGraphicsItem(KBBScalableGraphicWidget::resultBackground, scene, themeManager), KBBItemWithPosition()
0028 {
0029     m_widget = parent;
0030     m_scene = scene;
0031     m_elementIdResultBackground = themeManager->elementId(KBBScalableGraphicWidget::resultBackground);
0032     m_elementIdResultBackgroundHighlight = themeManager->elementId(KBBScalableGraphicWidget::resultBackgroundHighlight);
0033     m_number = nullptr;
0034     m_notNumber = nullptr;
0035     m_pause = false;
0036     
0037     float centerRadius = 3*KBBScalableGraphicWidget::RATIO/8.;
0038     float radius = KBBScalableGraphicWidget::BORDER_SIZE/4.;
0039 
0040     m_opposite = this;
0041     
0042     setPos(m_centerX - radius, m_centerY - radius);
0043     
0044     if(rayNumber<=0) {
0045 
0046         if (rayNumber==0)
0047             m_notNumber = new KBBGraphicsItem(KBBScalableGraphicWidget::resultReflection, m_scene, themeManager);
0048         else
0049             m_notNumber = new KBBGraphicsItem(KBBScalableGraphicWidget::resultHit, m_scene, themeManager);
0050         m_notNumber->setTransform(QTransform::fromTranslate(radius,radius), true);
0051         m_notNumber->setTransform(QTransform().rotate(rotationAngle()), true);
0052         m_notNumber->setTransform(QTransform::fromTranslate(-radius,-radius), true);
0053         m_notNumber->setPos(m_centerX - radius, m_centerY - radius);
0054     } else {
0055         QString text;
0056         text.setNum(rayNumber);
0057 
0058         m_number = new QGraphicsSimpleTextItem ( text, this);
0059         QFont font;
0060         font.setStyleHint(QFont::SansSerif);
0061         font.setWeight(QFont::DemiBold);
0062         float offset;
0063         if (rayNumber<10) {
0064             font.setPixelSize((int)(3*centerRadius/2));
0065             offset = 0.;
0066         } else {
0067             font.setPixelSize((int)(5*centerRadius/4));
0068             offset = 1.*centerRadius/6;
0069         }
0070         m_number->setFont(font);
0071         m_number->setPos(radius - centerRadius/2 - 2*offset, radius - centerRadius + offset);
0072         m_number->setZValue(themeManager->zValue(KBBScalableGraphicWidget::resultText));
0073     }
0074     setAcceptHoverEvents(true);
0075 }
0076 
0077 
0078 
0079 //
0080 // Public
0081 //
0082 
0083 void KBBGraphicsItemRayResult::cleanDelete()
0084 {
0085     delete m_notNumber;
0086     delete m_number;
0087     delete this;
0088 }
0089 
0090 
0091 void KBBGraphicsItemRayResult::highlight(bool state)
0092 {
0093     if (state && !m_pause)
0094         setElementId(m_elementIdResultBackgroundHighlight);
0095     else
0096         setElementId(m_elementIdResultBackground);
0097 }
0098 
0099 
0100 void KBBGraphicsItemRayResult::highlightBoth(bool state)
0101 {
0102     m_opposite->highlight(state);
0103     highlight(state);
0104 }
0105 
0106 
0107 int KBBGraphicsItemRayResult::position ()
0108 {
0109     return m_borderPosition;
0110 }
0111 
0112 
0113 void KBBGraphicsItemRayResult::setOpposite(KBBGraphicsItemRayResult* opposite)
0114 {
0115     m_opposite = opposite;
0116 }
0117 
0118 
0119 void KBBGraphicsItemRayResult::setPause(bool state)
0120 {
0121     if (m_number!=nullptr)
0122         m_number->setVisible(!state);
0123     if (m_notNumber!=nullptr)
0124         m_notNumber->setVisible(!state);
0125 
0126     m_pause = state;
0127 }
0128 
0129 
0130 
0131 //
0132 // Private
0133 //
0134 
0135 void KBBGraphicsItemRayResult::hoverEnterEvent (QGraphicsSceneHoverEvent*)
0136 {
0137     highlightBoth(true);
0138     m_widget->drawRay(position());
0139 }
0140 
0141 
0142 void KBBGraphicsItemRayResult::hoverLeaveEvent (QGraphicsSceneHoverEvent*)
0143 {
0144     highlightBoth(false);
0145     m_widget->removeRay();
0146 }