Engauge Digitizer  2
 All Classes Functions Variables Typedefs Enumerations Friends Pages
TutorialButton.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "Logger.h"
8 #include <qdebug.h>
9 #include <QGraphicsRectItem>
10 #include <QGraphicsScene>
11 #include <QGraphicsTextItem>
12 #include <qmath.h>
13 #include "TutorialButton.h"
14 #include "TutorialButtonRect.h"
15 #include "TutorialButtonText.h"
16 
17 const int HORIZONTAL_PADDING = 10;
18 const int VERTICAL_PADDING = 5;
19 const double Z_IN_FRONT = 1;
20 
21 TutorialButton::TutorialButton (const QString &text,
22  QGraphicsScene &scene) :
23  m_rect (nullptr),
24  m_text (nullptr)
25 {
26  createRect (scene);
27  createText (text);
28 }
29 
30 TutorialButton::~TutorialButton ()
31 {
32  QGraphicsScene *scene = m_rect->scene();
33  scene->removeItem (m_rect); // This also removes m_text from the scene
34 
35  delete m_rect;
36  delete m_text;
37 }
38 
39 void TutorialButton::createRect (QGraphicsScene &scene)
40 {
41  // Create rectangle and text items
42  m_rect = new TutorialButtonRect (*this);
43  m_rect->show ();
44  m_rect->setPen (QPen (Qt::gray));
45  m_rect->setBrush (QBrush (Qt::white));
46  m_rect->setZValue (Z_IN_FRONT);
47  scene.addItem (m_rect);
48 }
49 
50 void TutorialButton::createText (const QString &text)
51 {
52  // Create text. There is no need to call QGraphicsScene::addItem since it gets added automatically as the
53  // child of m_rect
54  m_text = new TutorialButtonText (*this,
55  text,
56  m_rect);
57  m_text->show ();
58 }
59 
60 QSize TutorialButton::size () const
61 {
62  // The size of the rectangle is not updated until later so we use the size of the text
63  return QSize (qFloor (m_text->boundingRect().size().width() + 2 * HORIZONTAL_PADDING),
64  qFloor (m_text->boundingRect().size().height() + 2 * VERTICAL_PADDING));
65 }
66 
68 {
69  LOG4CPP_INFO_S ((*mainCat)) << "TutorialButton::handleTriggered";
70 
71  // Relay signal from internal widgets to outside world
72  emit signalTriggered ();
73 }
74 
75 void TutorialButton::setGeometry (const QPoint &pos)
76 {
77  // Size the rectangle to fit the text, now that the extent of the text is known, with padding on the four sides
78  m_rect->setRect(pos.x(),
79  pos.y(),
80  m_text->boundingRect().width() + 2 * HORIZONTAL_PADDING,
81  m_text->boundingRect().height() + 2 * VERTICAL_PADDING);
82 
83  // Put text at the center of the rectangle
84  m_text->setPos (pos.x() + m_rect->boundingRect().width() / 2.0 - m_text->boundingRect().width() / 2.0,
85  pos.y() + m_rect->boundingRect().height() / 2.0 - m_text->boundingRect().height() / 2.0);
86 }
void setGeometry(const QPoint &pos)
Set the position. This is called after creation so screen extent is available for positioning calcula...
void signalTriggered()
Signal that button was triggered.
TutorialButton(const QString &text, QGraphicsScene &scene)
Single constructor. Position is set after creation using setGeometry.
This class customizes QGraphicsTextItem so it performs a callback after a mouse event.
This class customizes QGraphicsRectItem so it performs a callback after a mouse event.
void handleTriggered()
Callback to be called when button was triggered by mouse event.
QSize size() const
Size of this button.