Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't shrink the image when the aspect ratio changes #4

Merged
merged 4 commits into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/rqt_image_view/ratio_layouted_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class RatioLayoutedFrame

void resizeToFitAspectRatio();

void setOuterLayout(QHBoxLayout* outer_layout);

void setInnerFrameMinimumSize(const QSize& size);

void setInnerFrameMaximumSize(const QSize& size);
Expand Down Expand Up @@ -98,6 +100,8 @@ protected slots:

void mousePressEvent(QMouseEvent * mouseEvent);

QHBoxLayout* outer_layout_;

QSize aspect_ratio_;

QImage qimage_;
Expand Down
6 changes: 5 additions & 1 deletion src/rqt_image_view/image_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ void ImageView::initPlugin(qt_gui_cpp::PluginContext& context)
}
pub_topic_custom_ = false;

ui_.image_frame->setOuterLayout(ui_.image_layout);

QRegExp rx("([a-zA-Z/][a-zA-Z0-9_/]*)?"); //see http://www.ros.org/wiki/ROS/Concepts#Names.Valid_Names (but also accept an empty field)
ui_.publish_click_location_topic_line_edit->setValidator(new QRegExpValidator(rx, this));
connect(ui_.publish_click_location_check_box, SIGNAL(toggled(bool)), this, SLOT(onMousePublish(bool)));
Expand Down Expand Up @@ -434,8 +436,10 @@ void ImageView::callbackImage(const sensor_msgs::Image::ConstPtr& msg)
if (!ui_.zoom_1_push_button->isEnabled())
{
ui_.zoom_1_push_button->setEnabled(true);
onZoom1(ui_.zoom_1_push_button->isChecked());
}
// Need to update the zoom 1 every new image in case the image aspect ratio changed,
// though could check and see if the aspect ratio changed or not.
onZoom1(ui_.zoom_1_push_button->isChecked());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rqt_image_view/image_view.ui
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2" stretch="1,0">
<layout class="QHBoxLayout" name="image_layout" stretch="1,0">
<property name="spacing">
<number>0</number>
</property>
Expand Down
39 changes: 31 additions & 8 deletions src/rqt_image_view/ratio_layouted_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace rqt_image_view {

RatioLayoutedFrame::RatioLayoutedFrame(QWidget* parent, Qt::WindowFlags flags)
: QFrame()
, outer_layout_(NULL)
, aspect_ratio_(4, 3)
, smoothImage_(false)
{
Expand Down Expand Up @@ -68,7 +69,6 @@ void RatioLayoutedFrame::setImage(const QImage& image)//, QMutex* image_mutex)
qimage_mutex_.lock();
qimage_ = image.copy();
qimage_mutex_.unlock();
setAspectRatio(qimage_.width(), qimage_.height());
emit delayed_update();
}

Expand All @@ -77,26 +77,48 @@ void RatioLayoutedFrame::resizeToFitAspectRatio()
QRect rect = contentsRect();

// reduce longer edge to aspect ration
double width = double(rect.width());
double height = double(rect.height());
if (width * aspect_ratio_.height() / height > aspect_ratio_.width())
double width;
double height;

if (outer_layout_)
{
width = outer_layout_->contentsRect().width();
height = outer_layout_->contentsRect().height();
}
else
{
// if outer layout isn't available, this will use the old
// width and height, but this can shrink the display image if the
// aspect ratio changes.
width = rect.width();
height = rect.height();
}

double layout_ar = width / height;
const double image_ar = double(aspect_ratio_.width()) / double(aspect_ratio_.height());
if (layout_ar > image_ar)
{
// too large width
width = height * aspect_ratio_.width() / aspect_ratio_.height();
rect.setWidth(int(width + 0.5));
width = height * image_ar;
}
else
{
// too large height
height = width * aspect_ratio_.height() / aspect_ratio_.width();
rect.setHeight(int(height + 0.5));
height = width / image_ar;
}
rect.setWidth(int(width + 0.5));
rect.setHeight(int(height + 0.5));

// resize taking the border line into account
int border = lineWidth();
resize(rect.width() + 2 * border, rect.height() + 2 * border);
}

void RatioLayoutedFrame::setOuterLayout(QHBoxLayout* outer_layout)
{
outer_layout_ = outer_layout;
}

void RatioLayoutedFrame::setInnerFrameMinimumSize(const QSize& size)
{
int border = lineWidth();
Expand Down Expand Up @@ -136,6 +158,7 @@ void RatioLayoutedFrame::paintEvent(QPaintEvent* event)
qimage_mutex_.lock();
if (!qimage_.isNull())
{
setAspectRatio(qimage_.width(), qimage_.height());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the paint event can happen multiple times for the same image I don't think this command should be repeated every time but only be done once when a new image is received.

resizeToFitAspectRatio();
// TODO: check if full draw is really necessary
//QPaintEvent* paint_event = dynamic_cast<QPaintEvent*>(event);
Expand Down