Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <bwi_rqt_plugins/ratio_layouted_frame.h>
00034
00035 #include <assert.h>
00036
00037 namespace bwi_rqt_plugins {
00038
00039 RatioLayoutedFrame::RatioLayoutedFrame(QWidget* parent, Qt::WFlags flags)
00040 : QFrame()
00041 , aspect_ratio_(4, 3)
00042 {
00043 connect(this, SIGNAL(delayed_update()), this, SLOT(update()), Qt::QueuedConnection);
00044 }
00045
00046 RatioLayoutedFrame::~RatioLayoutedFrame()
00047 {
00048 }
00049
00050 const QImage& RatioLayoutedFrame::getImage() const
00051 {
00052 return qimage_;
00053 }
00054
00055 void RatioLayoutedFrame::setImage(const QImage& image)
00056 {
00057 qimage_mutex_.lock();
00058 qimage_ = image.copy();
00059 qimage_mutex_.unlock();
00060 setAspectRatio(qimage_.width(), qimage_.height());
00061 emit delayed_update();
00062 }
00063
00064 void RatioLayoutedFrame::resizeToFitAspectRatio()
00065 {
00066 QRect rect = contentsRect();
00067
00068
00069 double width = double(rect.width());
00070 double height = double(rect.height());
00071 if (width * aspect_ratio_.height() / height > aspect_ratio_.width())
00072 {
00073
00074 width = height * aspect_ratio_.width() / aspect_ratio_.height();
00075 rect.setWidth(int(width + 0.5));
00076 }
00077 else
00078 {
00079
00080 height = width * aspect_ratio_.height() / aspect_ratio_.width();
00081 rect.setHeight(int(height + 0.5));
00082 }
00083
00084
00085 int border = lineWidth();
00086 resize(rect.width() + 2 * border, rect.height() + 2 * border);
00087 }
00088
00089 void RatioLayoutedFrame::setInnerFrameMinimumSize(const QSize& size)
00090 {
00091 int border = lineWidth();
00092 QSize new_size = size;
00093 new_size += QSize(2 * border, 2 * border);
00094 setMinimumSize(new_size);
00095 emit delayed_update();
00096 }
00097
00098 void RatioLayoutedFrame::setInnerFrameMaximumSize(const QSize& size)
00099 {
00100 int border = lineWidth();
00101 QSize new_size = size;
00102 new_size += QSize(2 * border, 2 * border);
00103 setMaximumSize(new_size);
00104 emit delayed_update();
00105 }
00106
00107 void RatioLayoutedFrame::setInnerFrameFixedSize(const QSize& size)
00108 {
00109 setInnerFrameMinimumSize(size);
00110 setInnerFrameMaximumSize(size);
00111 }
00112
00113 void RatioLayoutedFrame::setAspectRatio(unsigned short width, unsigned short height)
00114 {
00115 int divisor = greatestCommonDivisor(width, height);
00116 if (divisor != 0) {
00117 aspect_ratio_.setWidth(width / divisor);
00118 aspect_ratio_.setHeight(height / divisor);
00119 }
00120 }
00121
00122 void RatioLayoutedFrame::paintEvent(QPaintEvent* event)
00123 {
00124 QPainter painter(this);
00125 qimage_mutex_.lock();
00126 if (!qimage_.isNull())
00127 {
00128 resizeToFitAspectRatio();
00129
00130
00131
00132 painter.drawImage(contentsRect(), qimage_);
00133 } else {
00134
00135 QLinearGradient gradient(0, 0, frameRect().width(), frameRect().height());
00136 gradient.setColorAt(0, Qt::white);
00137 gradient.setColorAt(1, Qt::black);
00138 painter.setBrush(gradient);
00139 painter.drawRect(0, 0, frameRect().width() + 1, frameRect().height() + 1);
00140 }
00141 qimage_mutex_.unlock();
00142 }
00143
00144 int RatioLayoutedFrame::greatestCommonDivisor(int a, int b)
00145 {
00146 if (b==0)
00147 {
00148 return a;
00149 }
00150 return greatestCommonDivisor(b, a % b);
00151 }
00152
00153 }