UImageView.h
Go to the documentation of this file.
00001 /*
00002  * ImageView.h
00003  *
00004  *  Created on: 2012-06-20
00005  *      Author: mathieu
00006  */
00007 
00008 #ifndef IMAGEVIEW_H_
00009 #define IMAGEVIEW_H_
00010 
00011 #include "rtabmap/gui/RtabmapGuiExp.h" // DLL export/import defines
00012 
00013 #include <QWidget>
00014 #include <QtGui/QPainter>
00015 
00016 class RTABMAPGUI_EXP UImageView : public QWidget
00017 {
00018         Q_OBJECT;
00019 public:
00020         UImageView(QWidget * parent = 0) : QWidget(parent) {}
00021         ~UImageView() {}
00022         void setBackgroundBrush(const QBrush & brush) {brush_ = brush;}
00023 
00024 public slots:
00025         void setImage(const QImage & image)
00026         {
00027                 pixmap_ = QPixmap::fromImage(image);
00028                 this->update();
00029         }
00030 
00031 private:
00032         void computeScaleOffsets(float & scale, float & offsetX, float & offsetY)
00033         {
00034                 scale = 1.0f;
00035                 offsetX = 0.0f;
00036                 offsetY = 0.0f;
00037 
00038                 if(!pixmap_.isNull())
00039                 {
00040                         float w = pixmap_.width();
00041                         float h = pixmap_.height();
00042                         float widthRatio = float(this->rect().width()) / w;
00043                         float heightRatio = float(this->rect().height()) / h;
00044 
00045                         if(widthRatio < heightRatio)
00046                         {
00047                                 scale = widthRatio;
00048                         }
00049                         else
00050                         {
00051                                 scale = heightRatio;
00052                         }
00053 
00054                         w *= scale;
00055                         h *= scale;
00056 
00057                         if(w < this->rect().width())
00058                         {
00059                                 offsetX = (this->rect().width() - w)/2.0f;
00060                         }
00061                         if(h < this->rect().height())
00062                         {
00063                                 offsetY = (this->rect().height() - h)/2.0f;
00064                         }
00065                 }
00066         }
00067 
00068 protected:
00069         virtual void paintEvent(QPaintEvent *event)
00070         {
00071                 QPainter painter(this);
00072 
00073                 //Draw background
00074                 painter.save();
00075                 painter.setBrush(brush_);
00076                 painter.drawRect(this->rect());
00077                 painter.restore();
00078 
00079                 if(!pixmap_.isNull())
00080                 {
00081                         painter.save();
00082                         //Scale
00083                         float ratio, offsetX, offsetY;
00084                         this->computeScaleOffsets(ratio, offsetX, offsetY);
00085                         painter.translate(offsetX, offsetY);
00086                         painter.scale(ratio, ratio);
00087                         painter.drawPixmap(QPoint(0,0), pixmap_);
00088                         painter.restore();
00089                 }
00090         }
00091 
00092 private:
00093         QPixmap pixmap_;
00094         QBrush brush_;
00095 };
00096 
00097 
00098 #endif /* IMAGEVIEW_H_ */


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sat Jul 23 2016 11:44:28