UImageView.h
Go to the documentation of this file.
1 /*
2  * ImageView.h
3  *
4  * Created on: 2012-06-20
5  * Author: mathieu
6  */
7 
8 #ifndef IMAGEVIEW_H_
9 #define IMAGEVIEW_H_
10 
11 #include "rtabmap/gui/RtabmapGuiExp.h" // DLL export/import defines
12 
13 #include <QWidget>
14 #include <QtGui/QPainter>
15 
16 class RTABMAPGUI_EXP UImageView : public QWidget
17 {
18  Q_OBJECT;
19 public:
20  UImageView(QWidget * parent = 0) : QWidget(parent) {}
22  void setBackgroundBrush(const QBrush & brush) {brush_ = brush;}
23 
24 public Q_SLOTS:
25  void setImage(const QImage & image)
26  {
27  pixmap_ = QPixmap::fromImage(image);
28  this->update();
29  }
30 
31 private:
32  void computeScaleOffsets(float & scale, float & offsetX, float & offsetY)
33  {
34  scale = 1.0f;
35  offsetX = 0.0f;
36  offsetY = 0.0f;
37 
38  if(!pixmap_.isNull())
39  {
40  float w = pixmap_.width();
41  float h = pixmap_.height();
42  float widthRatio = float(this->rect().width()) / w;
43  float heightRatio = float(this->rect().height()) / h;
44 
45  if(widthRatio < heightRatio)
46  {
47  scale = widthRatio;
48  }
49  else
50  {
51  scale = heightRatio;
52  }
53 
54  w *= scale;
55  h *= scale;
56 
57  if(w < this->rect().width())
58  {
59  offsetX = (this->rect().width() - w)/2.0f;
60  }
61  if(h < this->rect().height())
62  {
63  offsetY = (this->rect().height() - h)/2.0f;
64  }
65  }
66  }
67 
68 protected:
69  virtual void paintEvent(QPaintEvent *event)
70  {
71  QPainter painter(this);
72 
73  //Draw background
74  painter.save();
75  painter.setBrush(brush_);
76  painter.drawRect(this->rect());
77  painter.restore();
78 
79  if(!pixmap_.isNull())
80  {
81  painter.save();
82  //Scale
83  float ratio, offsetX, offsetY;
84  this->computeScaleOffsets(ratio, offsetX, offsetY);
85  painter.translate(offsetX, offsetY);
86  painter.scale(ratio, ratio);
87  painter.drawPixmap(QPoint(0,0), pixmap_);
88  painter.restore();
89  }
90  }
91 
92 private:
93  QPixmap pixmap_;
94  QBrush brush_;
95 };
96 
97 
98 #endif /* IMAGEVIEW_H_ */
#define RTABMAPGUI_EXP
Definition: RtabmapGuiExp.h:38
void computeScaleOffsets(float &scale, float &offsetX, float &offsetY)
Definition: UImageView.h:32
f
UImageView(QWidget *parent=0)
Definition: UImageView.h:20
QBrush brush_
Definition: UImageView.h:94
GLM_FUNC_DECL detail::tmat4x4< T, P > scale(detail::tmat4x4< T, P > const &m, detail::tvec3< T, P > const &v)
void setImage(const QImage &image)
Definition: UImageView.h:25
virtual void paintEvent(QPaintEvent *event)
Definition: UImageView.h:69
void setBackgroundBrush(const QBrush &brush)
Definition: UImageView.h:22
QPixmap pixmap_
Definition: UImageView.h:93


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:37:06