Engauge Digitizer  2
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 "TutorialButton.h"
13 #include "TutorialButtonRect.h"
14 #include "TutorialButtonText.h"
15 
16 const int HORIZONTAL_PADDING = 10;
17 const int VERTICAL_PADDING = 5;
18 const double Z_IN_FRONT = 1;
19 
20 TutorialButton::TutorialButton (const QString &text,
21  QGraphicsScene &scene) :
22  m_rect (0),
23  m_text (0)
24 {
25  createRect (scene);
26  createText (text);
27 }
28 
29 TutorialButton::~TutorialButton ()
30 {
31  QGraphicsScene *scene = m_rect->scene();
32  scene->removeItem (m_rect); // This also removes m_text from the scene
33 
34  delete m_rect;
35  delete m_text;
36 }
37 
38 void TutorialButton::createRect (QGraphicsScene &scene)
39 {
40  // Create rectangle and text items
41  m_rect = new TutorialButtonRect (*this);
42  m_rect->show ();
43  m_rect->setPen (QPen (Qt::gray));
44  m_rect->setBrush (QBrush (Qt::white));
45  m_rect->setZValue (Z_IN_FRONT);
46  scene.addItem (m_rect);
47 }
48 
49 void TutorialButton::createText (const QString &text)
50 {
51  // Create text. There is no need to call QGraphicsScene::addItem since it gets added automatically as the
52  // child of m_rect
53  m_text = new TutorialButtonText (*this,
54  text,
55  m_rect);
56  m_text->show ();
57 }
58 
59 QSize TutorialButton::size () const
60 {
61  // The size of the rectangle is not updated until later so we use the size of the text
62  return QSize (m_text->boundingRect().size().width() + 2 * HORIZONTAL_PADDING,
63  m_text->boundingRect().size().height() + 2 * VERTICAL_PADDING);
64 }
65 
67 {
68  LOG4CPP_INFO_S ((*mainCat)) << "TutorialButton::handleTriggered";
69 
70  // Relay signal from internal widgets to outside world
71  emit signalTriggered ();
72 }
73 
74 void TutorialButton::setGeometry (const QPoint &pos)
75 {
76  // Size the rectangle to fit the text, now that the extent of the text is known, with padding on the four sides
77  m_rect->setRect(pos.x(),
78  pos.y(),
79  m_text->boundingRect().width() + 2 * HORIZONTAL_PADDING,
80  m_text->boundingRect().height() + 2 * VERTICAL_PADDING);
81 
82  // Put text at the center of the rectangle
83  m_text->setPos (pos.x() + m_rect->boundingRect().width() / 2.0 - m_text->boundingRect().width() / 2.0,
84  pos.y() + m_rect->boundingRect().height() / 2.0 - m_text->boundingRect().height() / 2.0);
85 }
QSize size() const
Size of this button.
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.