QPainterWidget.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package
17  * @author Fabian Paus (fabian dot paus at kit dot edu)
18  * @date 2017
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
25 #include <functional>
26 
27 #include <QLayout>
28 #include <QMouseEvent>
29 #include <QPainter>
30 #include <QWidget>
31 
32 namespace armarx
33 {
34  using PaintCallback = std::function<void(QPainter& painter)>;
35 
36  class QPainterWidget : public QWidget
37  {
38  Q_OBJECT
39 
40  signals:
41  void mousePressed(QPoint p);
42 
43  public:
44  void
45  setupUi(QWidget* parent = 0)
46  {
47  parent->layout()->addWidget(this);
48 
49  if (objectName().isEmpty())
50  {
51  setObjectName(QString::fromUtf8("QPainterWidget"));
52  }
53  resize(372, 318);
54  QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
55  sizePolicy.setHorizontalStretch(0);
56  sizePolicy.setVerticalStretch(0);
57  sizePolicy.setHeightForWidth(this->sizePolicy().hasHeightForWidth());
58  setSizePolicy(sizePolicy);
59 
60  QMetaObject::connectSlotsByName(this);
61  }
62 
63  void
64  paintEvent(QPaintEvent*) override
65  {
66  if (callback)
67  {
68  QPainter painter(this);
69  callback(painter);
70  }
71  }
72 
73  void
74  mousePressEvent(QMouseEvent* event) override
75  {
76  emit mousePressed(event->pos());
77  }
78 
79  void
81  {
82  this->callback = callback;
83  }
84 
85  private:
86  PaintCallback callback;
87  };
88 } // namespace armarx
armarx::QPainterWidget::setPaintCallback
void setPaintCallback(PaintCallback const &callback)
Definition: QPainterWidget.h:80
armarx::QPainterWidget::setupUi
void setupUi(QWidget *parent=0)
Definition: QPainterWidget.h:45
armarx::QPainterWidget::paintEvent
void paintEvent(QPaintEvent *) override
Definition: QPainterWidget.h:64
armarx::QPainterWidget
Definition: QPainterWidget.h:36
armarx::QPainterWidget::mousePressEvent
void mousePressEvent(QMouseEvent *event) override
Definition: QPainterWidget.h:74
armarx::QPainterWidget::mousePressed
void mousePressed(QPoint p)
armarx::PaintCallback
std::function< void(QPainter &painter)> PaintCallback
Definition: QPainterWidget.h:34
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27