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 "ratio_layouted_frame.h"
00034
00035 #include <assert.h>
00036
00037 namespace rqt_image_view {
00038
00039 RatioLayoutedFrame::RatioLayoutedFrame(QWidget* parent, Qt::WFlags flags)
00040 : QFrame()
00041 , aspect_ratio_(4, 3)
00042 {
00043 }
00044
00045 RatioLayoutedFrame::~RatioLayoutedFrame()
00046 {
00047 }
00048
00049 void RatioLayoutedFrame::resizeToFitAspectRatio()
00050 {
00051 QRect rect = contentsRect();
00052
00053
00054 double width = double(rect.width());
00055 double height = double(rect.height());
00056 if (width * aspect_ratio_.height() / height > aspect_ratio_.width())
00057 {
00058
00059 width = height * aspect_ratio_.width() / aspect_ratio_.height();
00060 rect.setWidth(int(width));
00061 }
00062 else
00063 {
00064
00065 height = width * aspect_ratio_.height() / aspect_ratio_.width();
00066 rect.setHeight(int(height));
00067 }
00068
00069
00070 int border = lineWidth();
00071 resize(rect.width() + 2 * border, rect.height() + 2 * border);
00072 }
00073
00074 void RatioLayoutedFrame::setInnerFrameMinimumSize(const QSize& size)
00075 {
00076 int border = lineWidth();
00077 QSize new_size = size;
00078 new_size += QSize(2 * border, 2 * border);
00079 setMinimumSize(new_size);
00080 update();
00081 }
00082
00083 void RatioLayoutedFrame::setInnerFrameMaximumSize(const QSize& size)
00084 {
00085 int border = lineWidth();
00086 QSize new_size = size;
00087 new_size += QSize(2 * border, 2 * border);
00088 setMaximumSize(new_size);
00089 update();
00090 }
00091
00092 void RatioLayoutedFrame::setInnerFrameFixedSize(const QSize& size)
00093 {
00094 setInnerFrameMinimumSize(size);
00095 setInnerFrameMaximumSize(size);
00096 }
00097
00098 void RatioLayoutedFrame::setAspectRatio(unsigned short width, unsigned short height)
00099 {
00100 int divisor = greatestCommonDivisor(width, height);
00101 if (divisor != 0) {
00102 aspect_ratio_.setWidth(width / divisor);
00103 aspect_ratio_.setHeight(height / divisor);
00104 }
00105 }
00106
00107 int RatioLayoutedFrame::greatestCommonDivisor(int a, int b)
00108 {
00109 if (b==0)
00110 {
00111 return a;
00112 }
00113 return greatestCommonDivisor(b, a % b);
00114 }
00115
00116 }