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