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 "kbbgraphicsitemblackbox.h" |
31 | |
32 | |
33 | |
34 | #include <QGraphicsLineItem> |
35 | #include <QGraphicsRectItem> |
36 | #include <QGraphicsScene> |
37 | #include <QGraphicsSceneMouseEvent> |
38 | #include <QGraphicsView> |
39 | |
40 | |
41 | #include "kbbgraphicsitem.h" |
42 | #include "kbbscalablegraphicwidget.h" |
43 | #include "kbbthememanager.h" |
44 | |
45 | |
46 | |
47 | // |
48 | // Constructor / Destructor |
49 | // |
50 | |
51 | KBBGraphicsItemBlackBox::KBBGraphicsItemBlackBox(QGraphicsView* parent, QGraphicsScene* scene, KBBThemeManager* themeManager) : QGraphicsRectItem (0, scene) |
52 | { |
53 | m_columns = 1; |
54 | m_rows = 1; |
55 | m_widget = 0; |
56 | m_scene = scene; |
57 | |
58 | m_background = new KBBGraphicsItem(KBBScalableGraphicWidget::blackbox, m_scene, themeManager); |
59 | |
60 | //Grid |
61 | const KBBScalableGraphicWidget::itemType g = KBBScalableGraphicWidget::blackboxGrid; |
62 | m_zValueLines = themeManager->zValue(g); |
63 | m_penLines.setColor(themeManager->color(g)); |
64 | m_penLines.setStyle(themeManager->style(g)); |
65 | m_penLines.setWidthF(themeManager->width(g)); |
66 | } |
67 | |
68 | |
69 | |
70 | // |
71 | // Public |
72 | // |
73 | |
74 | void KBBGraphicsItemBlackBox::setSize(const int columns, const int rows) |
75 | { |
76 | m_background->scale(1./m_columns, 1./m_rows); |
77 | |
78 | if ((m_columns!=columns) || (m_rows!=rows)) { |
79 | m_columns = columns; |
80 | m_rows = rows; |
81 | |
82 | const int b = KBBScalableGraphicWidget::BORDER_SIZE; |
83 | const int r = KBBScalableGraphicWidget::RATIO; |
84 | |
85 | setRect(b, b, m_columns*r, m_rows*r); |
86 | |
87 | //remove old lines |
88 | for (int i=0; i<m_lines.count(); i++) |
89 | delete m_lines[i]; |
90 | m_lines.clear(); |
91 | |
92 | // add new lines |
93 | for (int i=0; i<m_columns+1; i++) |
94 | m_lines.append(new QGraphicsLineItem( b + i*r, b, b + i*r, b + m_rows*r, this, m_scene)); |
95 | for (int i=0; i<m_rows+1; i++) |
96 | m_lines.append(new QGraphicsLineItem( b, b + i*r, b + m_columns*r, b + i*r, this, m_scene)); |
97 | |
98 | // set line style |
99 | for (int i=0; i<m_lines.count(); i++) { |
100 | m_lines[i]->setPen(m_penLines); |
101 | m_lines[i]->setZValue(m_zValueLines); |
102 | } |
103 | m_background->setPos(b, b); |
104 | } |
105 | |
106 | m_background->scale(m_columns/1., m_rows/1.); |
107 | } |
108 | |
109 | |
110 | void KBBGraphicsItemBlackBox::setKBBScalableGraphicWidget(KBBScalableGraphicWidget* w) |
111 | { |
112 | m_widget = w; |
113 | } |
114 | |
115 | |
116 | |
117 | // |
118 | // Private |
119 | // |
120 | |
121 | void KBBGraphicsItemBlackBox::mousePressEvent (QGraphicsSceneMouseEvent* event) |
122 | { |
123 | int x = (int)(event->pos().x() - KBBScalableGraphicWidget::BORDER_SIZE)/KBBScalableGraphicWidget::RATIO; |
124 | int y = (int)(event->pos().y() - KBBScalableGraphicWidget::BORDER_SIZE)/KBBScalableGraphicWidget::RATIO; |
125 | |
126 | if (m_widget!=0) |
127 | m_widget->mouseBoxClick(event->button(), x + y*m_columns); |
128 | } |
129 | |