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