Qt: Fix led dialog layout and use hidpi painting

This commit is contained in:
Megamouse 2020-03-05 22:13:30 +01:00
parent 9e449db0c2
commit e1b4cf1557
2 changed files with 32 additions and 28 deletions

View file

@ -10,14 +10,15 @@ pad_led_settings_dialog::pad_led_settings_dialog(const int& colorR, const int& c
, m_initial{colorR, colorG, colorB, led_low_battery_blink, led_battery_indicator, led_battery_indicator_brightness} , m_initial{colorR, colorG, colorB, led_low_battery_blink, led_battery_indicator, led_battery_indicator_brightness}
{ {
ui->setupUi(this); ui->setupUi(this);
setFixedSize(size());
m_new = m_initial; m_new = m_initial;
redraw_color_sample();
ui->hs_indicator_brightness->setValue(m_new.battery_indicator_brightness); ui->hs_indicator_brightness->setValue(m_new.battery_indicator_brightness);
ui->cb_led_blink->setChecked(m_new.low_battery_blink); ui->cb_led_blink->setChecked(m_new.low_battery_blink);
ui->cb_led_indicate->setChecked(m_new.battery_indicator); ui->cb_led_indicate->setChecked(m_new.battery_indicator);
switch_groupboxes(m_new.battery_indicator); switch_groupboxes(m_new.battery_indicator);
update_slider_label(m_new.battery_indicator_brightness); update_slider_label(m_new.battery_indicator_brightness);
connect(ui->bb_dialog_buttons, &QDialogButtonBox::clicked, [this](QAbstractButton* button) connect(ui->bb_dialog_buttons, &QDialogButtonBox::clicked, [this](QAbstractButton* button)
{ {
if (button == ui->bb_dialog_buttons->button(QDialogButtonBox::Ok)) if (button == ui->bb_dialog_buttons->button(QDialogButtonBox::Ok))
@ -35,7 +36,7 @@ pad_led_settings_dialog::pad_led_settings_dialog(const int& colorR, const int& c
read_form_values(); read_form_values();
} }
Q_EMIT pass_led_settings(m_new.cR, m_new.cG, m_new.cB, m_new.low_battery_blink, m_new.battery_indicator, m_new.battery_indicator_brightness); Q_EMIT pass_led_settings(m_new.cR, m_new.cG, m_new.cB, m_new.low_battery_blink, m_new.battery_indicator, m_new.battery_indicator_brightness);
}); });
connect(ui->b_colorpicker, &QPushButton::clicked, [this]() connect(ui->b_colorpicker, &QPushButton::clicked, [this]()
{ {
const QColor led_color(m_new.cR, m_new.cG, m_new.cB); const QColor led_color(m_new.cR, m_new.cG, m_new.cB);
@ -52,6 +53,11 @@ pad_led_settings_dialog::pad_led_settings_dialog(const int& colorR, const int& c
}); });
connect(ui->hs_indicator_brightness, &QAbstractSlider::valueChanged, this, &pad_led_settings_dialog::update_slider_label); connect(ui->hs_indicator_brightness, &QAbstractSlider::valueChanged, this, &pad_led_settings_dialog::update_slider_label);
connect(ui->cb_led_indicate, &QCheckBox::toggled, this, &pad_led_settings_dialog::switch_groupboxes); connect(ui->cb_led_indicate, &QCheckBox::toggled, this, &pad_led_settings_dialog::switch_groupboxes);
// Draw color sample after showing the dialog, in order to have proper dimensions
show();
redraw_color_sample();
setFixedSize(size());
} }
pad_led_settings_dialog::~pad_led_settings_dialog() pad_led_settings_dialog::~pad_led_settings_dialog()
@ -61,23 +67,33 @@ pad_led_settings_dialog::~pad_led_settings_dialog()
void pad_led_settings_dialog::redraw_color_sample() void pad_led_settings_dialog::redraw_color_sample()
{ {
const qreal dpr = devicePixelRatioF();
const qreal w = ui->w_color_sample->width(); const qreal w = ui->w_color_sample->width();
const qreal h = ui->w_color_sample->height(); const qreal h = ui->w_color_sample->height();
const qreal padding = 5; const qreal padding = 5;
const qreal radius = 5; const qreal radius = 5;
QColor led_color;
QPixmap color_sample(w, h); // Create the canvas for our color sample widget
color_sample.fill(QColor("transparent")); QPixmap color_sample(w * dpr, h * dpr);
QPainter painter(&color_sample); color_sample.setDevicePixelRatio(dpr);
color_sample.fill(Qt::transparent);
// Create the shape for our color sample widget
QPainterPath path; QPainterPath path;
QPen border(Qt::black, 1);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(border);
led_color.setRgb(m_new.cR, m_new.cG, m_new.cB);
path.addRoundedRect(QRectF(padding, padding, w - padding * 2, h - padding * 2), radius, radius); path.addRoundedRect(QRectF(padding, padding, w - padding * 2, h - padding * 2), radius, radius);
// Get new LED color
const QColor led_color(m_new.cR, m_new.cG, m_new.cB);
// Paint the shape with a black border and fill it with the LED color
QPainter painter(&color_sample);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QPen(Qt::black, 1));
painter.fillPath(path, led_color); painter.fillPath(path, led_color);
painter.drawPath(path); painter.drawPath(path);
ui->w_color_sample->setPixmap(color_sample);
// Update the color sample widget
ui->w_color_sample->setPixmap(color_sample.scaled(w * dpr, h * dpr, Qt::KeepAspectRatio, Qt::TransformationMode::SmoothTransformation));
} }
void pad_led_settings_dialog::update_slider_label(int val) void pad_led_settings_dialog::update_slider_label(int val)

View file

@ -6,14 +6,14 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>292</width> <width>287</width>
<height>287</height> <height>287</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>LED Settings</string> <string>LED Settings</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="1,1,1,0"> <layout class="QVBoxLayout" name="pad_led_settings_dialog_layout">
<item> <item>
<widget class="QGroupBox" name="gb_led_color"> <widget class="QGroupBox" name="gb_led_color">
<property name="minimumSize"> <property name="minimumSize">
@ -25,15 +25,9 @@
<property name="title"> <property name="title">
<string>LED Color</string> <string>LED Color</string>
</property> </property>
<layout class="QVBoxLayout" name="gb_led_color_layout" stretch="0,2"> <layout class="QVBoxLayout" name="gb_led_color_layout">
<item> <item>
<widget class="QLabel" name="w_color_sample"> <widget class="QLabel" name="w_color_sample">
<property name="minimumSize">
<size>
<width>249</width>
<height>39</height>
</size>
</property>
<property name="text"> <property name="text">
<string/> <string/>
</property> </property>
@ -42,7 +36,7 @@
<item> <item>
<widget class="QPushButton" name="b_colorpicker"> <widget class="QPushButton" name="b_colorpicker">
<property name="text"> <property name="text">
<string>Select color...</string> <string>Select color</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -51,12 +45,6 @@
</item> </item>
<item> <item>
<widget class="QGroupBox" name="gb_battery_status"> <widget class="QGroupBox" name="gb_battery_status">
<property name="minimumSize">
<size>
<width>0</width>
<height>75</height>
</size>
</property>
<property name="title"> <property name="title">
<string>In-game battery status</string> <string>In-game battery status</string>
</property> </property>