• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

kblackbox

  • kde-4.x
  • kdegames
  • kblackbox
kbbgraphicsitemblackbox.cpp
Go to the documentation of this file.
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, bool isPreview) : QGraphicsRectItem (0)
52 {
53  scene->addItem(this);
54  m_columns = 1;
55  m_rows = 1;
56  m_widget = 0;
57  m_scene = scene;
58 
59  m_background = new KBBGraphicsItem(KBBScalableGraphicWidget::blackbox, m_scene, themeManager);
60 
61  //Grid
62  const KBBScalableGraphicWidget::itemType g = KBBScalableGraphicWidget::blackboxGrid;
63  m_zValueLines = themeManager->zValue(g);
64  m_penLines.setColor(themeManager->color(g));
65  m_penLines.setStyle(themeManager->style(g));
66  m_penLines.setWidthF(themeManager->width(g));
67  //accept hover events unless the central widget is a preview (crashes the program)
68  if (!isPreview)
69  setAcceptHoverEvents(true);
70 }
71 
72 
73 
74 //
75 // Public
76 //
77 
78 void KBBGraphicsItemBlackBox::setSize(const int columns, const int rows)
79 {
80  m_background->setTransform(QTransform::fromScale(1./m_columns, 1./m_rows), true);
81 
82  if ((m_columns!=columns) || (m_rows!=rows)) {
83  m_columns = columns;
84  m_rows = rows;
85 
86  const int b = KBBScalableGraphicWidget::BORDER_SIZE;
87  const int r = KBBScalableGraphicWidget::RATIO;
88 
89  setRect(b, b, m_columns*r, m_rows*r);
90 
91  //remove old lines
92  for (int i=0; i<m_lines.count(); i++)
93  delete m_lines[i];
94  m_lines.clear();
95 
96  // add new lines
97  for (int i=0; i<m_columns+1; i++) {
98  QGraphicsLineItem *item = new QGraphicsLineItem( b + i*r, b, b + i*r, b + m_rows*r, this);
99  m_lines.append(item);
100  }
101  for (int i=0; i<m_rows+1; i++) {
102  QGraphicsLineItem *item = new QGraphicsLineItem( b, b + i*r, b + m_columns*r, b + i*r, this);
103  m_lines.append(item);
104  }
105 
106  // set line style
107  for (int i=0; i<m_lines.count(); i++) {
108  m_lines[i]->setPen(m_penLines);
109  m_lines[i]->setZValue(m_zValueLines);
110  }
111  m_background->setPos(b, b);
112  }
113 
114  m_background->setTransform(QTransform::fromScale(m_columns/1., m_rows/1.), true);
115 }
116 
117 
118 void KBBGraphicsItemBlackBox::setKBBScalableGraphicWidget(KBBScalableGraphicWidget* w)
119 {
120  m_widget = w;
121 }
122 
123 
124 
125 //
126 // Private
127 //
128 
129 void KBBGraphicsItemBlackBox::mousePressEvent (QGraphicsSceneMouseEvent* event)
130 {
131  int x = (int)(event->pos().x() - KBBScalableGraphicWidget::BORDER_SIZE)/KBBScalableGraphicWidget::RATIO;
132  int y = (int)(event->pos().y() - KBBScalableGraphicWidget::BORDER_SIZE)/KBBScalableGraphicWidget::RATIO;
133 
134  if (m_widget!=0)
135  m_widget->mouseBoxClick(event->button(), x + y*m_columns);
136 }
137 
138 void KBBGraphicsItemBlackBox::hoverLeaveEvent(QGraphicsSceneHoverEvent*)
139 {
140  m_widget->cursorOff();
141 }
142 
143 void KBBGraphicsItemBlackBox::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
144 {
145  int x = (int)(event->pos().x() - KBBScalableGraphicWidget::BORDER_SIZE)/KBBScalableGraphicWidget::RATIO;
146  int y = (int)(event->pos().y() - KBBScalableGraphicWidget::BORDER_SIZE)/KBBScalableGraphicWidget::RATIO;
147  emit hoverMoved(x + y*m_columns);
148 }
QList::clear
void clear()
QTransform::fromScale
QTransform fromScale(qreal sx, qreal sy)
QGraphicsItem::x
qreal x() const
QGraphicsItem::y
qreal y() const
QPen::setStyle
void setStyle(Qt::PenStyle style)
KBBThemeManager::width
qreal width(const KBBScalableGraphicWidget::itemType itemType)
Get the width of the item.
Definition: kbbthememanager.cpp:191
QGraphicsScene
kbbgraphicsitem.h
KBBScalableGraphicWidget::cursorOff
void cursorOff()
Definition: kbbscalablegraphicwidget.cpp:622
KBBGraphicsItem
Graphic item of the scalable graphic widget.
Definition: kbbgraphicsitem.h:44
QGraphicsItem::setAcceptHoverEvents
void setAcceptHoverEvents(bool enabled)
KBBScalableGraphicWidget
Scalable graphic central widget for KBlackBox.
Definition: kbbscalablegraphicwidget.h:62
KBBThemeManager::zValue
int zValue(const KBBScalableGraphicWidget::itemType itemType)
Get the relative height of the item.
Definition: kbbthememanager.cpp:197
QGraphicsItem::scene
QGraphicsScene * scene() const
QGraphicsRectItem::setRect
void setRect(const QRectF &rectangle)
QGraphicsSceneHoverEvent::pos
QPointF pos() const
QList::count
int count(const T &value) const
QPointF::x
qreal x() const
QPointF::y
qreal y() const
QList::append
void append(const T &value)
QGraphicsLineItem
QGraphicsSceneMouseEvent
QGraphicsItem::setPos
void setPos(const QPointF &pos)
QPen::setWidthF
void setWidthF(qreal width)
kbbscalablegraphicwidget.h
KBBScalableGraphicWidget::BORDER_SIZE
static int const BORDER_SIZE
Distance between the black box and the widget border.
Definition: kbbscalablegraphicwidget.h:74
QGraphicsSceneMouseEvent::button
Qt::MouseButton button() const
KBBGraphicsItemBlackBox::setKBBScalableGraphicWidget
void setKBBScalableGraphicWidget(KBBScalableGraphicWidget *w)
Set the KBBScalableGraphicWidget.
Definition: kbbgraphicsitemblackbox.cpp:118
kbbgraphicsitemblackbox.h
QPen::setColor
void setColor(const QColor &color)
KBBThemeManager::color
QColor color(const KBBScalableGraphicWidget::itemType itemType)
Get the color of the item.
Definition: kbbthememanager.cpp:75
QGraphicsSceneHoverEvent
KBBScalableGraphicWidget::blackbox
Definition: kbbscalablegraphicwidget.h:92
KBBScalableGraphicWidget::mouseBoxClick
void mouseBoxClick(const Qt::MouseButton button, int boxPosition)
Definition: kbbscalablegraphicwidget.cpp:211
KBBScalableGraphicWidget::blackboxGrid
Definition: kbbscalablegraphicwidget.h:93
KBBGraphicsItemBlackBox::hoverMoved
void hoverMoved(int)
QGraphicsRectItem
KBBScalableGraphicWidget::RATIO
static int const RATIO
Width and height of a single square on the black box.
Definition: kbbscalablegraphicwidget.h:81
QGraphicsItem::setTransform
void setTransform(const QTransform &matrix, bool combine)
KBBThemeManager
Theme manager of the scalable graphic widget.
Definition: kbbthememanager.h:51
QGraphicsSceneMouseEvent::pos
QPointF pos() const
QGraphicsScene::addItem
void addItem(QGraphicsItem *item)
QGraphicsView
KBBThemeManager::style
Qt::PenStyle style(const KBBScalableGraphicWidget::itemType itemType)
Get the pen style of the item.
Definition: kbbthememanager.cpp:176
KBBScalableGraphicWidget::itemType
itemType
Every graphic items.
Definition: kbbscalablegraphicwidget.h:89
kbbthememanager.h
KBBGraphicsItemBlackBox::KBBGraphicsItemBlackBox
KBBGraphicsItemBlackBox(QGraphicsView *parent, QGraphicsScene *scene, KBBThemeManager *themeManager, bool isPreview)
Constructor.
Definition: kbbgraphicsitemblackbox.cpp:51
KBBGraphicsItemBlackBox::setSize
void setSize(const int columns, const int rows)
Define the (new) size of the black box.
Definition: kbbgraphicsitemblackbox.cpp:78
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Thu Feb 21 2019 22:22:23 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kblackbox

Skip menu "kblackbox"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal