Skip to content

Commit 7f8e330

Browse files
committed
Fix scaling issue with hidpi
1 parent 9ddad7a commit 7f8e330

7 files changed

+20
-6
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.5)
2-
project(kColorPicker LANGUAGES CXX VERSION 0.1.0)
2+
project(kColorPicker LANGUAGES CXX VERSION 0.1.1)
33

44
set(QT_MIN_VERSION "5.6.1")
55

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# kColorPicker [![Build Status](https://travis-ci.org/ksnip/kColorPicker.svg?branch=master)](https://travis-ci.org/ksnip/kColorPicker)
22
QToolButton with color popup menu with lets you select a color. The popup features a color dialog button which can be used to add custom colors to the popup menu.
33

4-
Version 0.1.0
4+
Version 0.1.1
55

66
![kColorPicker](https://i.imgur.com/VkhUvFa.png "kColorPicker")
77

src/IconCreator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class IconCreator
3131
public:
3232
explicit IconCreator() = delete;
3333
~IconCreator() = delete;
34-
static QIcon createIcon(const QColor &color, const QSize &size = QSize(25, 25));
34+
static QIcon createIcon(const QColor &color, const QSize &size);
3535
};
3636

3737
} // namespace kColorPicker

src/PopupMenu.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@ void PopupMenu::generateGrid()
7676

7777
ColorButton *PopupMenu::createButton(const QColor &color)
7878
{
79-
auto icon = IconCreator::createIcon(color);
79+
auto iconSize = getIconSize();
80+
auto icon = IconCreator::createIcon(color, iconSize);
8081
auto button = new ColorButton(icon, color);
8182
return button;
8283
}
84+
QSize PopupMenu::getIconSize() const
85+
{ auto xScaleFactor = logicalDpiX() / 96.0;
86+
auto yScaleFactor = logicalDpiY() / 96.0;
87+
return {static_cast<int>(25 * xScaleFactor), static_cast<int>(25 * yScaleFactor) };
88+
}
8389

8490
void PopupMenu::addColorButton(const QColor &color)
8591
{

src/PopupMenu.h

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Q_OBJECT
5757

5858
private slots:
5959
void colorSelected(const QColor &color);
60+
QSize getIconSize() const;
6061
};
6162

6263
} // namespace kColorPicker

src/buttons/AbstractPopupMenuButton.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ namespace kColorPicker {
2323

2424
AbstractPopupMenuButton::AbstractPopupMenuButton(const QIcon &icon)
2525
{
26-
setIcon(icon);
27-
setFixedSize(iconSize() + QSize(4,4));
26+
setIcon(icon);
27+
setFixedSize(getSizeBasedOnIcon());
2828
connect(this, &QToolButton::clicked, this, &AbstractPopupMenuButton::buttonClicked);
2929
}
30+
QSize AbstractPopupMenuButton::getSizeBasedOnIcon() const
31+
{
32+
auto xScaleFactor = logicalDpiX() / 96.0;
33+
auto yScaleFactor = logicalDpiY() / 96.0;
34+
return iconSize() + QSize(static_cast<int>(4 * xScaleFactor), static_cast<int>(4 * yScaleFactor));
35+
}
3036

3137
void AbstractPopupMenuButton::paintEvent(QPaintEvent *event)
3238
{

src/buttons/AbstractPopupMenuButton.h

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Q_OBJECT
3939
protected slots:
4040
void paintEvent(QPaintEvent *event) override;
4141
virtual void buttonClicked() = 0;
42+
QSize getSizeBasedOnIcon() const;
4243
};
4344

4445
} // namespace kColorPicker

0 commit comments

Comments
 (0)