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