File indexing completed on 2023-11-26 10:50:14
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 "kbbgraphicsitemblackbox.h" 0011 0012 0013 0014 #include <QGraphicsLineItem> 0015 #include <QGraphicsScene> 0016 #include <QGraphicsSceneMouseEvent> 0017 #include <QGraphicsView> 0018 0019 0020 #include "kbbgraphicsitem.h" 0021 #include "kbbthememanager.h" 0022 0023 0024 0025 // 0026 // Constructor / Destructor 0027 // 0028 0029 KBBGraphicsItemBlackBox::KBBGraphicsItemBlackBox(QGraphicsView* parent, QGraphicsScene* scene, KBBThemeManager* themeManager, bool isPreview) : QGraphicsRectItem (nullptr) 0030 { 0031 Q_UNUSED(parent); // TODO: check why not passing to QGraphicsRectItem? 0032 scene->addItem(this); 0033 m_columns = 1; 0034 m_rows = 1; 0035 m_widget = nullptr; 0036 m_scene = scene; 0037 0038 m_background = new KBBGraphicsItem(KBBScalableGraphicWidget::blackbox, m_scene, themeManager); 0039 0040 //Grid 0041 const KBBScalableGraphicWidget::itemType g = KBBScalableGraphicWidget::blackboxGrid; 0042 m_zValueLines = themeManager->zValue(g); 0043 m_penLines.setColor(themeManager->color(g)); 0044 m_penLines.setStyle(themeManager->style(g)); 0045 m_penLines.setWidthF(themeManager->width(g)); 0046 //accept hover events unless the central widget is a preview (crashes the program) 0047 if (!isPreview) 0048 setAcceptHoverEvents(true); 0049 } 0050 0051 0052 0053 // 0054 // Public 0055 // 0056 0057 void KBBGraphicsItemBlackBox::setSize(const int columns, const int rows) 0058 { 0059 m_background->setTransform(QTransform::fromScale(1./m_columns, 1./m_rows), true); 0060 0061 if ((m_columns!=columns) || (m_rows!=rows)) { 0062 m_columns = columns; 0063 m_rows = rows; 0064 0065 const int b = KBBScalableGraphicWidget::BORDER_SIZE; 0066 const int r = KBBScalableGraphicWidget::RATIO; 0067 0068 setRect(b, b, m_columns*r, m_rows*r); 0069 0070 //remove old lines 0071 for (int i=0; i<m_lines.count(); i++) 0072 delete m_lines[i]; 0073 m_lines.clear(); 0074 0075 // add new lines 0076 for (int i=0; i<m_columns+1; i++) { 0077 QGraphicsLineItem *item = new QGraphicsLineItem( b + i*r, b, b + i*r, b + m_rows*r, this); 0078 m_lines.append(item); 0079 } 0080 for (int i=0; i<m_rows+1; i++) { 0081 QGraphicsLineItem *item = new QGraphicsLineItem( b, b + i*r, b + m_columns*r, b + i*r, this); 0082 m_lines.append(item); 0083 } 0084 0085 // set line style 0086 for (int i=0; i<m_lines.count(); i++) { 0087 m_lines[i]->setPen(m_penLines); 0088 m_lines[i]->setZValue(m_zValueLines); 0089 } 0090 m_background->setPos(b, b); 0091 } 0092 0093 m_background->setTransform(QTransform::fromScale(m_columns/1., m_rows/1.), true); 0094 } 0095 0096 0097 void KBBGraphicsItemBlackBox::setKBBScalableGraphicWidget(KBBScalableGraphicWidget* w) 0098 { 0099 m_widget = w; 0100 } 0101 0102 0103 0104 // 0105 // Private 0106 // 0107 0108 void KBBGraphicsItemBlackBox::mousePressEvent (QGraphicsSceneMouseEvent* event) 0109 { 0110 int x = (int)(event->pos().x() - KBBScalableGraphicWidget::BORDER_SIZE)/KBBScalableGraphicWidget::RATIO; 0111 int y = (int)(event->pos().y() - KBBScalableGraphicWidget::BORDER_SIZE)/KBBScalableGraphicWidget::RATIO; 0112 0113 if (m_widget!=nullptr) 0114 m_widget->mouseBoxClick(event->button(), x + y*m_columns); 0115 } 0116 0117 void KBBGraphicsItemBlackBox::hoverLeaveEvent(QGraphicsSceneHoverEvent*) 0118 { 0119 m_widget->cursorOff(); 0120 } 0121 0122 void KBBGraphicsItemBlackBox::hoverMoveEvent(QGraphicsSceneHoverEvent* event) 0123 { 0124 int x = (int)(event->pos().x() - KBBScalableGraphicWidget::BORDER_SIZE)/KBBScalableGraphicWidget::RATIO; 0125 int y = (int)(event->pos().y() - KBBScalableGraphicWidget::BORDER_SIZE)/KBBScalableGraphicWidget::RATIO; 0126 Q_EMIT hoverMoved(x + y*m_columns); 0127 } 0128 0129 #include "moc_kbbgraphicsitemblackbox.cpp"