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

Scale bottom and left panel buttons according to font size #4031

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/gui/SkyGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ void SkyGui::updateBarsPos()
progressBarMgr->setZValue(400);

// Update position of the auto-hide buttons
btHorizAutoHide->setPos(1,btVertAutoHide->getButtonPixmapHeight()-btHorizAutoHide->getButtonPixmapHeight()+1);
autoHidebts->setPos(0, hh-autoHidebts->childrenBoundingRect().height()+1);
double opacity = qMax(animLeftBarTimeLine->currentValue(), animBottomBarTimeLine->currentValue());
autoHidebts->setOpacity(qMax(0.01, opacity)); // Work around a qt bug
Expand Down
46 changes: 42 additions & 4 deletions src/gui/StelGuiItems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
#include <QSettings>
#include <QGuiApplication>

namespace
{

constexpr double DEFAULT_FONT_SIZE = 13;

double fontSizeRatio()
{
return StelApp::getInstance().getScreenFontSize() / DEFAULT_FONT_SIZE;
}

void brightenImage(QImage &img, float factor)
{
for (int y=0; y<img.height(); y++)
Expand All @@ -78,6 +88,7 @@ void brightenImage(QImage &img, float factor)
}
}

}

void StelButton::initCtor(const QPixmap& apixOn,
const QPixmap& apixOff,
Expand Down Expand Up @@ -349,6 +360,17 @@ QRectF StelButton::boundingRect() const
return QRectF(0,0, getButtonPixmapWidth(), getButtonPixmapHeight());
}

int StelButton::getButtonPixmapWidth() const
{
const double baseWidth = pixOn.width() / pixmapsScale * fontSizeRatio();
return std::lround(baseWidth);
}
int StelButton::getButtonPixmapHeight() const
{
const double baseHeight = pixOn.height() / pixmapsScale * fontSizeRatio();
return std::lround(baseHeight);
}

void StelButton::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
{
/* QPixmap::scaled has much better quality than that scaling via QPainter::drawPixmap, so let's
Expand All @@ -367,8 +389,8 @@ void StelButton::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidg
const double ratio = QOpenGLContext::currentContext()->screen()->devicePixelRatio();
if(scaledCurrentPixmap.isNull() || ratio != scaledCurrentPixmap.devicePixelRatioF())
{
const auto scale = ratio / pixmapsScale;
scaledCurrentPixmap = pixmap().scaled(pixOn.size()*scale, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
const auto size = boundingRect().size() * ratio;
scaledCurrentPixmap = pixmap().scaled(size.toSize(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
scaledCurrentPixmap.setDevicePixelRatio(ratio);
}
// Align the pixmap to pixel grid, otherwise we'll get artifacts at some scaling factors.
Expand Down Expand Up @@ -402,16 +424,28 @@ void LeftStelBar::addButton(StelButton* button)
if (QGraphicsItem::childItems().size()!=0)
{
const QRectF& r = childrenBoundingRect();
posY += r.bottom()-1;
posY += r.bottom();
}
button->setParentItem(this);
button->setFocusOnSky(false);
//button->prepareGeometryChange(); // could possibly be removed when qt 4.6 become stable
button->setPos(0., qRound(posY+10.5));
button->setPos(0., qRound(posY + 9.5 * fontSizeRatio()));

connect(button, SIGNAL(hoverChanged(bool)), this, SLOT(buttonHoverChanged(bool)));
}

void LeftStelBar::updateButtonPositions()
{
double posY = 0;
for (const auto button : childItems())
{
if (const auto b = dynamic_cast<StelButton*>(button))
b->animValueChanged(0.); // update button pixmap
button->setPos(0., posY);
posY += std::round(button->boundingRect().height() + 9.5 * fontSizeRatio());
}
}

void LeftStelBar::paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)
{
}
Expand Down Expand Up @@ -476,6 +510,7 @@ void LeftStelBar::setColor(const QColor& c)
//! connect from StelApp to resize fonts on the fly.
void LeftStelBar::setFontSizeFromApp(int size)
{
prepareGeometryChange();
// Font size was developed based on base font size 13, i.e. 12
int screenFontSize = size-1;
QFont font=QGuiApplication::font();
Expand All @@ -487,7 +522,10 @@ void LeftStelBar::setFontSizeFromApp(int size)
// to avoid crash
SkyGui* skyGui=gui->getSkyGui();
if (skyGui)
{
skyGui->updateBarsPos();
updateButtonPositions();
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/gui/StelGuiItems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ class StelButton : public QObject, public QGraphicsPixmapItem

//! Get the width of the button image.
//! The width is based on pixOn.
int getButtonPixmapWidth() const {return pixOn.width() / pixmapsScale;}
int getButtonPixmapHeight() const {return pixOn.height() / pixmapsScale;}
int getButtonPixmapWidth() const;
int getButtonPixmapHeight() const;

//! Set the button opacity
void setOpacity(double v) {opacity=v; updateIcon();}
Expand Down Expand Up @@ -220,6 +220,7 @@ private slots:
void setFontSizeFromApp(int size);
//! connect from StelApp to set font on the fly.
void setFont(const QFont &cfont);
void updateButtonPositions();
private:
QTimeLine* hideTimeLine;
QGraphicsSimpleTextItem* helpLabel;
Expand Down
Loading