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 <QWidget>
26 #include <QPainter>
27 #include <QLayout>
28 #include <QMouseEvent>
29 
30 #include <functional>
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 setupUi(QWidget* parent = 0)
45  {
46  parent->layout()->addWidget(this);
47 
48  if (objectName().isEmpty())
49  {
50  setObjectName(QString::fromUtf8("QPainterWidget"));
51  }
52  resize(372, 318);
53  QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
54  sizePolicy.setHorizontalStretch(0);
55  sizePolicy.setVerticalStretch(0);
56  sizePolicy.setHeightForWidth(this->sizePolicy().hasHeightForWidth());
57  setSizePolicy(sizePolicy);
58 
59  QMetaObject::connectSlotsByName(this);
60  }
61 
62  void paintEvent(QPaintEvent*) override
63  {
64  if (callback)
65  {
66  QPainter painter(this);
67  callback(painter);
68  }
69  }
70 
71  void mousePressEvent(QMouseEvent* event) override
72  {
73  emit mousePressed(event->pos());
74  }
75 
76  void setPaintCallback(PaintCallback const& callback)
77  {
78  this->callback = callback;
79  }
80 
81  private:
82  PaintCallback callback;
83  };
84 }
85 
armarx::QPainterWidget::setPaintCallback
void setPaintCallback(PaintCallback const &callback)
Definition: QPainterWidget.h:76
armarx::QPainterWidget::setupUi
void setupUi(QWidget *parent=0)
Definition: QPainterWidget.h:44
armarx::QPainterWidget::paintEvent
void paintEvent(QPaintEvent *) override
Definition: QPainterWidget.h:62
armarx::QPainterWidget
Definition: QPainterWidget.h:36
armarx::QPainterWidget::mousePressEvent
void mousePressEvent(QMouseEvent *event) override
Definition: QPainterWidget.h:71
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:28