Skip to content

Commit

Permalink
cleanup: Properly resize RenderWidget on high-DPI displays (#1263)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaschke committed Aug 31, 2019
1 parent 9895106 commit 2b295ff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
27 changes: 9 additions & 18 deletions src/rviz/ogre_helpers/render_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ RenderWidget::RenderWidget( RenderSystem* render_system, QWidget *parent )
QApplication::flush();
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
QApplication::syncX();
pixel_ratio_ = 1.0;
#else
QApplication::sync();
QWindow* window = windowHandle();
pixel_ratio_ = window ? window->devicePixelRatio() : 1.0;
#endif

render_window_ = render_system_->makeRenderWindow(win_id, width(), height(), pixelRatio());
render_window_ = render_system_->makeRenderWindow(win_id, width(), height(), pixel_ratio_);
}

RenderWidget::~RenderWidget()
Expand Down Expand Up @@ -121,25 +124,13 @@ void RenderWidget::resizeEvent(QResizeEvent *)
{
if( render_window_ )
{
// render_window_->writeContentsToFile() (used in
// VisualizationFrame::onSaveImage()) does not work right for
// window with an odd width, so here I just always force it to be
// even.
const qreal ratio = pixelRatio();
const int w = width() * ratio;
render_window_->resize(w + (w % 2), height() * ratio);
/* render_window_->writeContentsToFile() (used in VisualizationFrame::onSaveImage())
* does not work right for window with an odd width.
* So here we just always force it to be even. */
const int w = width() * pixel_ratio_;
render_window_->resize(w + (w % 2), height() * pixel_ratio_);
render_window_->windowMovedOrResized();
}
}

qreal RenderWidget::pixelRatio() const
{
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
return 1.0;
#else
QWindow* window = windowHandle();
return window ? window->devicePixelRatio() : 1.0;
#endif
}

} // end namespace rviz
3 changes: 1 addition & 2 deletions src/rviz/ogre_helpers/render_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,14 @@ class RenderWidget: public QWidget
virtual void moveEvent(QMoveEvent *e);
virtual void paintEvent(QPaintEvent *e);
virtual void resizeEvent(QResizeEvent *e);
qreal pixelRatio() const;

#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
QPaintEngine *paintEngine() const { return 0; }
#endif

RenderSystem* render_system_;
Ogre::RenderWindow* render_window_;

qreal pixel_ratio_;
QFrame* renderFrame;
};

Expand Down

0 comments on commit 2b295ff

Please sign in to comment.