mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2024-11-29 07:24:13 +01:00
Handle non-1:1 aspect ratio bacgrounds.
git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@470 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
parent
ad9227d7a4
commit
c15aba6b84
@ -1,4 +1,4 @@
|
||||
Widget experiments. Compile and run test application
|
||||
to see widget.
|
||||
|
||||
Simple panel lets you test new backgrounds.
|
||||
Simple panel lets you test new backgrounds.
|
||||
|
@ -27,18 +27,22 @@ void BasicDial::paintEvent(QPaintEvent *event) {
|
||||
|
||||
void BasicDial::renderBackground(void) {
|
||||
int side = qMin(width(), height());
|
||||
if( bg.size() == QSize(side, side) ) {
|
||||
if( bg.size() == size() ) {
|
||||
qDebug() << "BasicDial::renderBackground(): Size not changed! Abort rerendering stuff";
|
||||
return;
|
||||
}
|
||||
QPixmap pixmap(QSize(side, side));
|
||||
/* Create buffer pixmap and make it transparent */
|
||||
QPixmap pixmap(size());
|
||||
pixmap.fill(Qt::transparent);
|
||||
|
||||
/* Configure painter */
|
||||
QPainter painter(&pixmap);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
QRectF frame( QPoint(0, 0), QPoint(side-1, side-1));
|
||||
renderer->load(backgroundFile);
|
||||
renderer->render(&painter, frame);
|
||||
bg = pixmap;
|
||||
|
||||
renderer->load(backgroundFile); /* load file; needed to calculate frame */
|
||||
QRectF frame = calculateCenteredFrame(); /* viewport from renderer (based on loaded file */
|
||||
renderer->render(&painter, frame); /* dump bg on pixmap */
|
||||
bg = pixmap; /* save it */
|
||||
}
|
||||
|
||||
void BasicDial::renderNeedle(qreal angle) {
|
||||
@ -93,3 +97,17 @@ void BasicDial::setPen(QPen p) {
|
||||
pen = p;
|
||||
pen.setWidth(2);
|
||||
}
|
||||
|
||||
QRectF BasicDial::calculateCenteredFrame(void) {
|
||||
QRectF vb = renderer->viewBoxF(); /* get SVG viewport */
|
||||
qreal scale = qMax( (vb.height()/height()), (vb.width()/width())); /* calc scale to fit SVG into widget */
|
||||
vb.setWidth( vb.width()/scale ); /* scale viewport so SVG fit widget size */
|
||||
vb.setHeight( vb.height()/scale );
|
||||
|
||||
QRectF frame; /* let's prepare render frame for bg */
|
||||
frame.setX( (width()-vb.width())/2.0 ); /* frame is centered on widget */
|
||||
frame.setY( (height()-vb.height())/2.0 );
|
||||
frame.setWidth( vb.width() ); /* derive size from scaled viewport */
|
||||
frame.setHeight( vb.height() );
|
||||
return frame;
|
||||
}
|
||||
|
@ -29,6 +29,8 @@ private:
|
||||
void renderNeedle(qreal angle);
|
||||
qreal value2angle(qreal value);
|
||||
|
||||
QRectF calculateCenteredFrame(void);
|
||||
|
||||
QSvgRenderer *renderer;
|
||||
QPixmap bg;
|
||||
qreal angle;
|
||||
|
Loading…
Reference in New Issue
Block a user