ratio_layouted_frame.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Dirk Thomas, TU Darmstadt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following
13  * disclaimer in the documentation and/or other materials provided
14  * with the distribution.
15  * * Neither the name of the TU Darmstadt nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
34 
35 #include <assert.h>
36 #include <QMouseEvent>
37 
38 namespace rqt_image_view {
39 
40 RatioLayoutedFrame::RatioLayoutedFrame(QWidget* parent, Qt::WindowFlags flags)
41  : QFrame()
42  , outer_layout_(NULL)
43  , aspect_ratio_(4, 3)
44  , smoothImage_(false)
45 {
46  connect(this, SIGNAL(delayed_update()), this, SLOT(update()), Qt::QueuedConnection);
47 }
48 
50 {
51 }
52 
53 const QImage& RatioLayoutedFrame::getImage() const
54 {
55  return qimage_;
56 }
57 
59 {
60  QImage img;
61  qimage_mutex_.lock();
62  img = qimage_.copy();
63  qimage_mutex_.unlock();
64  return img;
65 }
66 
67 void RatioLayoutedFrame::setImage(const QImage& image)//, QMutex* image_mutex)
68 {
69  qimage_mutex_.lock();
70  qimage_ = image.copy();
71  setAspectRatio(qimage_.width(), qimage_.height());
72  qimage_mutex_.unlock();
73  emit delayed_update();
74 }
75 
77 {
78  QRect rect = contentsRect();
79 
80  // reduce longer edge to aspect ration
81  double width;
82  double height;
83 
84  if (outer_layout_)
85  {
86  width = outer_layout_->contentsRect().width();
87  height = outer_layout_->contentsRect().height();
88  }
89  else
90  {
91  // if outer layout isn't available, this will use the old
92  // width and height, but this can shrink the display image if the
93  // aspect ratio changes.
94  width = rect.width();
95  height = rect.height();
96  }
97 
98  double layout_ar = width / height;
99  const double image_ar = double(aspect_ratio_.width()) / double(aspect_ratio_.height());
100  if (layout_ar > image_ar)
101  {
102  // too large width
103  width = height * image_ar;
104  }
105  else
106  {
107  // too large height
108  height = width / image_ar;
109  }
110  rect.setWidth(int(width + 0.5));
111  rect.setHeight(int(height + 0.5));
112 
113  // resize taking the border line into account
114  int border = lineWidth();
115  resize(rect.width() + 2 * border, rect.height() + 2 * border);
116 }
117 
118 void RatioLayoutedFrame::setOuterLayout(QHBoxLayout* outer_layout)
119 {
120  outer_layout_ = outer_layout;
121 }
122 
124 {
125  int border = lineWidth();
126  QSize new_size = size;
127  new_size += QSize(2 * border, 2 * border);
128  setMinimumSize(new_size);
129  emit delayed_update();
130 }
131 
133 {
134  int border = lineWidth();
135  QSize new_size = size;
136  new_size += QSize(2 * border, 2 * border);
137  setMaximumSize(new_size);
138  emit delayed_update();
139 }
140 
142 {
145 }
146 
147 void RatioLayoutedFrame::setAspectRatio(unsigned short width, unsigned short height)
148 {
149  int divisor = greatestCommonDivisor(width, height);
150  if (divisor != 0) {
151  aspect_ratio_.setWidth(width / divisor);
152  aspect_ratio_.setHeight(height / divisor);
153  }
154 }
155 
156 void RatioLayoutedFrame::paintEvent(QPaintEvent* event)
157 {
158  QPainter painter(this);
159  qimage_mutex_.lock();
160  if (!qimage_.isNull())
161  {
163  // TODO: check if full draw is really necessary
164  //QPaintEvent* paint_event = dynamic_cast<QPaintEvent*>(event);
165  //painter.drawImage(paint_event->rect(), qimage_);
166  if (!smoothImage_) {
167  painter.drawImage(contentsRect(), qimage_);
168  } else {
169  if (contentsRect().width() == qimage_.width()) {
170  painter.drawImage(contentsRect(), qimage_);
171  } else {
172  QImage image = qimage_.scaled(contentsRect().width(), contentsRect().height(),
173  Qt::KeepAspectRatio, Qt::SmoothTransformation);
174  painter.drawImage(contentsRect(), image);
175  }
176  }
177  } else {
178  // default image with gradient
179  QLinearGradient gradient(0, 0, frameRect().width(), frameRect().height());
180  gradient.setColorAt(0, Qt::white);
181  gradient.setColorAt(1, Qt::black);
182  painter.setBrush(gradient);
183  painter.drawRect(0, 0, frameRect().width() + 1, frameRect().height() + 1);
184  }
185  qimage_mutex_.unlock();
186 }
187 
189 {
190  if (b==0)
191  {
192  return a;
193  }
194  return greatestCommonDivisor(b, a % b);
195 }
196 
197 void RatioLayoutedFrame::mousePressEvent(QMouseEvent * mouseEvent)
198 {
199  if(mouseEvent->button() == Qt::LeftButton)
200  {
201  emit mouseLeft(mouseEvent->x(), mouseEvent->y());
202  }
203  QFrame::mousePressEvent(mouseEvent);
204 }
205 
207  smoothImage_ = checked;
208 }
209 
210 }
rqt_image_view
Definition: image_view.h:58
rqt_image_view::RatioLayoutedFrame::getImage
const QImage & getImage() const
Definition: ratio_layouted_frame.cpp:53
rqt_image_view::RatioLayoutedFrame::resizeToFitAspectRatio
void resizeToFitAspectRatio()
Definition: ratio_layouted_frame.cpp:76
rqt_image_view::RatioLayoutedFrame::getImageCopy
QImage getImageCopy() const
Definition: ratio_layouted_frame.cpp:58
rqt_image_view::RatioLayoutedFrame::onSmoothImageChanged
void onSmoothImageChanged(bool checked)
Definition: ratio_layouted_frame.cpp:206
NULL
#define NULL
rqt_image_view::RatioLayoutedFrame::paintEvent
void paintEvent(QPaintEvent *event)
Definition: ratio_layouted_frame.cpp:156
rqt_image_view::RatioLayoutedFrame::smoothImage_
bool smoothImage_
Definition: ratio_layouted_frame.h:110
rqt_image_view::RatioLayoutedFrame::mouseLeft
void mouseLeft(int x, int y)
rqt_image_view::RatioLayoutedFrame::setInnerFrameFixedSize
void setInnerFrameFixedSize(const QSize &size)
Definition: ratio_layouted_frame.cpp:141
rqt_image_view::RatioLayoutedFrame::qimage_mutex_
QMutex qimage_mutex_
Definition: ratio_layouted_frame.h:108
rqt_image_view::RatioLayoutedFrame::qimage_
QImage qimage_
Definition: ratio_layouted_frame.h:107
rqt_image_view::RatioLayoutedFrame::setAspectRatio
void setAspectRatio(unsigned short width, unsigned short height)
Definition: ratio_layouted_frame.cpp:147
rqt_image_view::RatioLayoutedFrame::mousePressEvent
void mousePressEvent(QMouseEvent *mouseEvent)
Definition: ratio_layouted_frame.cpp:197
ratio_layouted_frame.h
update
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
rqt_image_view::RatioLayoutedFrame::aspect_ratio_
QSize aspect_ratio_
Definition: ratio_layouted_frame.h:105
rqt_image_view::RatioLayoutedFrame::setInnerFrameMaximumSize
void setInnerFrameMaximumSize(const QSize &size)
Definition: ratio_layouted_frame.cpp:132
rqt_image_view::RatioLayoutedFrame::outer_layout_
QHBoxLayout * outer_layout_
Definition: ratio_layouted_frame.h:103
rqt_image_view::RatioLayoutedFrame::setInnerFrameMinimumSize
void setInnerFrameMinimumSize(const QSize &size)
Definition: ratio_layouted_frame.cpp:123
rqt_image_view::RatioLayoutedFrame::~RatioLayoutedFrame
virtual ~RatioLayoutedFrame()
Definition: ratio_layouted_frame.cpp:49
assert.h
rqt_image_view::RatioLayoutedFrame::delayed_update
void delayed_update()
rqt_image_view::RatioLayoutedFrame::setOuterLayout
void setOuterLayout(QHBoxLayout *outer_layout)
Definition: ratio_layouted_frame.cpp:118
rqt_image_view::RatioLayoutedFrame::RatioLayoutedFrame
RatioLayoutedFrame(QWidget *parent, Qt::WindowFlags flags=0)
Definition: ratio_layouted_frame.cpp:40
rqt_image_view::RatioLayoutedFrame::greatestCommonDivisor
static int greatestCommonDivisor(int a, int b)
Definition: ratio_layouted_frame.cpp:188
rqt_image_view::RatioLayoutedFrame::setImage
void setImage(const QImage &image)
Definition: ratio_layouted_frame.cpp:67


rqt_image_view
Author(s): Dirk Thomas , Mabel Zhang
autogenerated on Fri May 26 2023 02:31:55