ratio_layouted_frame.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Dirk Thomas, TU Darmstadt
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  *   * Redistributions of source code must retain the above copyright
00010  *     notice, this list of conditions and the following disclaimer.
00011  *   * Redistributions in binary form must reproduce the above
00012  *     copyright notice, this list of conditions and the following
00013  *     disclaimer in the documentation and/or other materials provided
00014  *     with the distribution.
00015  *   * Neither the name of the TU Darmstadt nor the names of its
00016  *     contributors may be used to endorse or promote products derived
00017  *     from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00027  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00028  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00029  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030  * POSSIBILITY OF SUCH DAMAGE.
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   // reduce longer edge to aspect ration
00054   double width = double(rect.width());
00055   double height = double(rect.height());
00056   if (width * aspect_ratio_.height() / height > aspect_ratio_.width())
00057   {
00058     // too large width
00059     width = height * aspect_ratio_.width() / aspect_ratio_.height();
00060     rect.setWidth(int(width));
00061   }
00062   else
00063   {
00064     // too large height
00065     height = width * aspect_ratio_.height() / aspect_ratio_.width();
00066     rect.setHeight(int(height));
00067   }
00068 
00069   // resize taking the border line into account
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 }


rqt_image_view
Author(s): Dirk Thomas
autogenerated on Fri Jan 3 2014 11:54:15