0
0
mirror of https://github.com/obsproject/obs-studio.git synced 2024-09-20 13:08:50 +02:00

UI: Support color picker with alpha

This commit is contained in:
jpark37 2020-12-29 19:51:40 -08:00 committed by Jim
parent f4a76f9daa
commit 0d1ffd8b30
2 changed files with 66 additions and 18 deletions

View File

@ -635,14 +635,16 @@ QWidget *OBSPropertiesView::AddButton(obs_property_t *prop)
return NewWidget(prop, button, SIGNAL(clicked()));
}
void OBSPropertiesView::AddColor(obs_property_t *prop, QFormLayout *layout,
QLabel *&label)
void OBSPropertiesView::AddColorInternal(obs_property_t *prop,
QFormLayout *layout, QLabel *&label,
bool supportAlpha)
{
QPushButton *button = new QPushButton;
QLabel *colorLabel = new QLabel;
const char *name = obs_property_name(prop);
long long val = obs_data_get_int(settings, name);
QColor color = color_from_int(val);
QColor::NameFormat format;
if (!obs_property_enabled(prop)) {
button->setEnabled(false);
@ -653,19 +655,21 @@ void OBSPropertiesView::AddColor(obs_property_t *prop, QFormLayout *layout,
button->setText(QTStr("Basic.PropertiesWindow.SelectColor"));
button->setToolTip(QT_UTF8(obs_property_long_description(prop)));
color.setAlpha(255);
if (supportAlpha) {
format = QColor::HexArgb;
} else {
format = QColor::HexRgb;
color.setAlpha(255);
}
QPalette palette = QPalette(color);
colorLabel->setFrameStyle(QFrame::Sunken | QFrame::Panel);
// The picker doesn't have an alpha option, show only RGB
colorLabel->setText(color.name(QColor::HexRgb));
colorLabel->setText(color.name(format));
colorLabel->setPalette(palette);
colorLabel->setStyleSheet(
QString("background-color :%1; color: %2;")
.arg(palette.color(QPalette::Window)
.name(QColor::HexRgb))
.arg(palette.color(QPalette::WindowText)
.name(QColor::HexRgb)));
.arg(palette.color(QPalette::Window).name(format))
.arg(palette.color(QPalette::WindowText).name(format)));
colorLabel->setAutoFillBackground(true);
colorLabel->setAlignment(Qt::AlignCenter);
colorLabel->setToolTip(QT_UTF8(obs_property_long_description(prop)));
@ -684,6 +688,18 @@ void OBSPropertiesView::AddColor(obs_property_t *prop, QFormLayout *layout,
layout->addRow(label, subLayout);
}
void OBSPropertiesView::AddColor(obs_property_t *prop, QFormLayout *layout,
QLabel *&label)
{
AddColorInternal(prop, layout, label, false);
}
void OBSPropertiesView::AddColorAlpha(obs_property_t *prop, QFormLayout *layout,
QLabel *&label)
{
AddColorInternal(prop, layout, label, true);
}
void MakeQFont(obs_data_t *font_obj, QFont &font, bool limit = false)
{
const char *face = obs_data_get_string(font_obj, "face");
@ -1419,6 +1435,9 @@ void OBSPropertiesView::AddProperty(obs_property_t *property,
break;
case OBS_PROPERTY_GROUP:
AddGroup(property, layout);
break;
case OBS_PROPERTY_COLOR_ALPHA:
AddColorAlpha(property, layout, label);
}
if (widget && !obs_property_enabled(property))
@ -1698,14 +1717,19 @@ void WidgetInfo::ListChanged(const char *setting)
}
}
bool WidgetInfo::ColorChanged(const char *setting)
bool WidgetInfo::ColorChangedInternal(const char *setting, bool supportAlpha)
{
const char *desc = obs_property_description(property);
long long val = obs_data_get_int(view->settings, setting);
QColor color = color_from_int(val);
QColor::NameFormat format;
QColorDialog::ColorDialogOptions options;
if (supportAlpha) {
options |= QColorDialog::ShowAlphaChannel;
}
/* The native dialog on OSX has all kinds of problems, like closing
* other open QDialogs on exit, and
* https://bugreports.qt-project.org/browse/QTBUG-34532
@ -1715,26 +1739,40 @@ bool WidgetInfo::ColorChanged(const char *setting)
#endif
color = QColorDialog::getColor(color, view, QT_UTF8(desc), options);
color.setAlpha(255);
if (!color.isValid())
return false;
if (supportAlpha) {
format = QColor::HexArgb;
} else {
color.setAlpha(255);
format = QColor::HexRgb;
}
QLabel *label = static_cast<QLabel *>(widget);
label->setText(color.name(QColor::HexRgb));
label->setText(color.name(format));
QPalette palette = QPalette(color);
label->setPalette(palette);
label->setStyleSheet(QString("background-color :%1; color: %2;")
.arg(palette.color(QPalette::Window)
.name(QColor::HexRgb))
.arg(palette.color(QPalette::WindowText)
.name(QColor::HexRgb)));
label->setStyleSheet(
QString("background-color :%1; color: %2;")
.arg(palette.color(QPalette::Window).name(format))
.arg(palette.color(QPalette::WindowText).name(format)));
obs_data_set_int(view->settings, setting, color_to_int(color));
return true;
}
bool WidgetInfo::ColorChanged(const char *setting)
{
return ColorChangedInternal(setting, false);
}
bool WidgetInfo::ColorAlphaChanged(const char *setting)
{
return ColorChangedInternal(setting, true);
}
bool WidgetInfo::FontChanged(const char *setting)
{
obs_data_t *font_obj = obs_data_get_obj(view->settings, setting);
@ -1889,6 +1927,10 @@ void WidgetInfo::ControlChanged()
case OBS_PROPERTY_GROUP:
GroupChanged(setting);
break;
case OBS_PROPERTY_COLOR_ALPHA:
if (!ColorAlphaChanged(setting))
return;
break;
}
if (view->callback && !view->deferUpdate)

View File

@ -30,7 +30,9 @@ private:
void TextChanged(const char *setting);
bool PathChanged(const char *setting);
void ListChanged(const char *setting);
bool ColorChangedInternal(const char *setting, bool supportAlpha);
bool ColorChanged(const char *setting);
bool ColorAlphaChanged(const char *setting);
bool FontChanged(const char *setting);
void GroupChanged(const char *setting);
void EditableListChanged();
@ -101,8 +103,12 @@ private:
void AddEditableList(obs_property_t *prop, QFormLayout *layout,
QLabel *&label);
QWidget *AddButton(obs_property_t *prop);
void AddColorInternal(obs_property_t *prop, QFormLayout *layout,
QLabel *&label, bool supportAlpha);
void AddColor(obs_property_t *prop, QFormLayout *layout,
QLabel *&label);
void AddColorAlpha(obs_property_t *prop, QFormLayout *layout,
QLabel *&label);
void AddFont(obs_property_t *prop, QFormLayout *layout, QLabel *&label);
void AddFrameRate(obs_property_t *prop, bool &warning,
QFormLayout *layout, QLabel *&label);