1 | // |
2 | // KBlackBox |
3 | // |
4 | // A simple game inspired by an emacs module |
5 | // |
6 | /*************************************************************************** |
7 | * Copyright (c) 1999-2000, Robert Cimrman * |
8 | * cimrman3@students.zcu.cz * |
9 | * * |
10 | * Copyright (c) 2007, Nicolas Roffet * |
11 | * nicolas-kde@roffet.com * |
12 | * * |
13 | * * |
14 | * This program is free software; you can redistribute it and/or modify * |
15 | * it under the terms of the GNU General Public License as published by * |
16 | * the Free Software Foundation; either version 2 of the License, or * |
17 | * (at your option) any later version. * |
18 | * * |
19 | * This program is distributed in the hope that it will be useful, * |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
22 | * GNU General Public License for more details. * |
23 | * * |
24 | * You should have received a copy of the GNU General Public License * |
25 | * along with this program; if not, write to the * |
26 | * Free Software Foundation, Inc., * |
27 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA * |
28 | ***************************************************************************/ |
29 | |
30 | #include "kbbgraphicsitemrayresult.h" |
31 | |
32 | |
33 | |
34 | #include <QFont> |
35 | #include <QGraphicsScene> |
36 | |
37 | |
38 | #include "kbbgraphicsitem.h" |
39 | #include "kbbgraphicsitemborder.h" |
40 | #include "kbbitemwithposition.h" |
41 | #include "kbbscalablegraphicwidget.h" |
42 | #include "kbbthememanager.h" |
43 | |
44 | |
45 | |
46 | // |
47 | // Constructor / Destructor |
48 | // |
49 | |
50 | 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() |
51 | { |
52 | m_widget = parent; |
53 | m_scene = scene; |
54 | m_elementIdResultBackground = themeManager->elementId(KBBScalableGraphicWidget::resultBackground); |
55 | m_elementIdResultBackgroundHighlight = themeManager->elementId(KBBScalableGraphicWidget::resultBackgroundHighlight); |
56 | m_number = NULL; |
57 | m_notNumber = NULL; |
58 | m_pause = false; |
59 | |
60 | float centerRadius = 3*KBBScalableGraphicWidget::RATIO/8.; |
61 | float radius = KBBScalableGraphicWidget::BORDER_SIZE/4.; |
62 | |
63 | m_opposite = this; |
64 | |
65 | setPos(m_centerX - radius, m_centerY - radius); |
66 | |
67 | if(rayNumber<=0) { |
68 | |
69 | if (rayNumber==0) |
70 | m_notNumber = new KBBGraphicsItem(KBBScalableGraphicWidget::resultReflection, m_scene, themeManager); |
71 | else |
72 | m_notNumber = new KBBGraphicsItem(KBBScalableGraphicWidget::resultHit, m_scene, themeManager); |
73 | m_notNumber->translate(radius,radius); |
74 | m_notNumber->rotate(rotationAngle()); |
75 | m_notNumber->translate(-radius,-radius); |
76 | m_notNumber->setPos(m_centerX - radius, m_centerY - radius); |
77 | } else { |
78 | QString text; |
79 | text.setNum(rayNumber); |
80 | |
81 | m_number = new QGraphicsSimpleTextItem ( text, this, scene); |
82 | QFont font; |
83 | font.setStyleHint(QFont::SansSerif); |
84 | font.setWeight(QFont::DemiBold); |
85 | float offset; |
86 | if (rayNumber<10) { |
87 | font.setPixelSize((int)(3*centerRadius/2)); |
88 | offset = 0.; |
89 | } else { |
90 | font.setPixelSize((int)(5*centerRadius/4)); |
91 | offset = 1.*centerRadius/6; |
92 | } |
93 | m_number->setFont(font); |
94 | m_number->setPos(radius - centerRadius/2 - 2*offset, radius - centerRadius + offset); |
95 | m_number->setZValue(themeManager->zValue(KBBScalableGraphicWidget::resultText)); |
96 | } |
97 | setAcceptsHoverEvents(true); |
98 | } |
99 | |
100 | |
101 | |
102 | // |
103 | // Public |
104 | // |
105 | |
106 | void KBBGraphicsItemRayResult::cleanDelete() |
107 | { |
108 | delete m_notNumber; |
109 | delete m_number; |
110 | delete this; |
111 | } |
112 | |
113 | |
114 | void KBBGraphicsItemRayResult::highlight(bool state) |
115 | { |
116 | if (state && !m_pause) |
117 | setElementId(m_elementIdResultBackgroundHighlight); |
118 | else |
119 | setElementId(m_elementIdResultBackground); |
120 | } |
121 | |
122 | |
123 | void KBBGraphicsItemRayResult::highlightBoth(bool state) |
124 | { |
125 | m_opposite->highlight(state); |
126 | highlight(state); |
127 | } |
128 | |
129 | |
130 | int KBBGraphicsItemRayResult::position () |
131 | { |
132 | return m_borderPosition; |
133 | } |
134 | |
135 | |
136 | void KBBGraphicsItemRayResult::setOpposite(KBBGraphicsItemRayResult* opposite) |
137 | { |
138 | m_opposite = opposite; |
139 | } |
140 | |
141 | |
142 | void KBBGraphicsItemRayResult::setPause(bool state) |
143 | { |
144 | if (m_number!=NULL) |
145 | m_number->setVisible(!state); |
146 | if (m_notNumber!=NULL) |
147 | m_notNumber->setVisible(!state); |
148 | |
149 | m_pause = state; |
150 | } |
151 | |
152 | |
153 | |
154 | // |
155 | // Private |
156 | // |
157 | |
158 | void KBBGraphicsItemRayResult::hoverEnterEvent (QGraphicsSceneHoverEvent*) |
159 | { |
160 | highlightBoth(true); |
161 | m_widget->drawRay(position()); |
162 | } |
163 | |
164 | |
165 | void KBBGraphicsItemRayResult::hoverLeaveEvent (QGraphicsSceneHoverEvent*) |
166 | { |
167 | highlightBoth(false); |
168 | m_widget->removeRay(); |
169 | } |
170 | |