Engauge Digitizer  2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TutorialButton.cpp
Go to the documentation of this file.
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 
31 {
32  if (m_rect != nullptr) {
33  QGraphicsScene *scene = m_rect->scene();
34  scene->removeItem (m_rect); // This also removes m_text from the scene
35 
36  delete m_rect;
37  }
38  delete m_text;
39 }
40 
41 void TutorialButton::createRect (QGraphicsScene &scene)
42 {
43  // Create rectangle and text items
44  m_rect = new TutorialButtonRect (*this);
45  m_rect->show ();
46  m_rect->setPen (QPen (Qt::gray));
47  m_rect->setBrush (QBrush (Qt::white));
48  m_rect->setZValue (Z_IN_FRONT);
49  scene.addItem (m_rect);
50 }
51 
52 void TutorialButton::createText (const QString &text)
53 {
54  // Create text. There is no need to call QGraphicsScene::addItem since it gets added automatically as the
55  // child of m_rect
56  m_text = new TutorialButtonText (*this,
57  text,
58  m_rect);
59  m_text->show ();
60 }
61 
62 QSize TutorialButton::size () const
63 {
64  // The size of the rectangle is not updated until later so we use the size of the text
65  return QSize (qFloor (m_text->boundingRect().size().width() + 2 * HORIZONTAL_PADDING),
66  qFloor (m_text->boundingRect().size().height() + 2 * VERTICAL_PADDING));
67 }
68 
70 {
71  LOG4CPP_INFO_S ((*mainCat)) << "TutorialButton::handleTriggered";
72 
73  // Relay signal from internal widgets to outside world
74  emit signalTriggered ();
75 }
76 
77 void TutorialButton::setGeometry (const QPoint &pos)
78 {
79  // Size the rectangle to fit the text, now that the extent of the text is known, with padding on the four sides
80  m_rect->setRect(pos.x(),
81  pos.y(),
82  m_text->boundingRect().width() + 2 * HORIZONTAL_PADDING,
83  m_text->boundingRect().height() + 2 * VERTICAL_PADDING);
84 
85  // Put text at the center of the rectangle
86  m_text->setPos (pos.x() + m_rect->boundingRect().width() / 2.0 - m_text->boundingRect().width() / 2.0,
87  pos.y() + m_rect->boundingRect().height() / 2.0 - m_text->boundingRect().height() / 2.0);
88 }
void setGeometry(const QPoint &pos)
Set the position. This is called after creation so screen extent is available for positioning calcula...
const double Z_IN_FRONT
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.
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
This class customizes QGraphicsRectItem so it performs a callback after a mouse event.
const int VERTICAL_PADDING
void handleTriggered()
Callback to be called when button was triggered by mouse event.
log4cpp::Category * mainCat
Definition: Logger.cpp:14
QSize size() const
Size of this button.
const int HORIZONTAL_PADDING