Commit f6790b6c authored by Julius Künzel's avatar Julius Künzel 💬
Browse files

Refactor color chooser code to reduce duplication

Also add ChooseColorWidget and ColorPickerWidget to Qt Designer plugin
parent 2b26ce0d
Pipeline #243012 failed with stage
in 5 minutes and 18 seconds
......@@ -6,14 +6,12 @@
#include "coloreditwidget.hpp"
#include "assets/model/assetparametermodel.hpp"
#include "utils/qcolorutils.h"
#include "widgets/colorpickerwidget.h"
#include "widgets/choosecolorwidget.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QTextStream>
#include <KColorButton>
ColorEditWidget::ColorEditWidget(std::shared_ptr<AssetParameterModel> model, QModelIndex index, QWidget *parent)
: AbstractParamWidget(std::move(model), index, parent)
{
......@@ -23,40 +21,20 @@ ColorEditWidget::ColorEditWidget(std::shared_ptr<AssetParameterModel> model, QMo
QString color = m_model->data(m_index, AssetParameterModel::ValueRole).toString();
QString comment = m_model->data(m_index, AssetParameterModel::CommentRole).toString();
QLabel *label = new QLabel(name, this);
m_choosecolor = new ChooseColorWidget(this, QColorUtils::stringToColor(color), alphaEnabled);
auto *layout = new QHBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
QLabel *label = new QLabel(name, this);
QWidget *rightSide = new QWidget(this);
auto *rightSideLayout = new QHBoxLayout(rightSide);
rightSideLayout->setContentsMargins(0, 0, 0, 0);
rightSideLayout->setSpacing(0);
m_button = new KColorButton(QColorUtils::stringToColor(color), rightSide);
if (alphaEnabled) {
m_button->setAlphaChannelEnabled(alphaEnabled);
}
// m_button->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
auto *picker = new ColorPickerWidget(rightSide);
layout->addWidget(label, 1);
layout->addWidget(rightSide, 1);
rightSideLayout->addStretch();
rightSideLayout->addWidget(m_button, 2);
rightSideLayout->addWidget(picker);
connect(picker, &ColorPickerWidget::colorPicked, this, &ColorEditWidget::setColor);
connect(picker, &ColorPickerWidget::disableCurrentFilter, this, &ColorEditWidget::disableCurrentFilter);
connect(m_button, &KColorButton::changed, this, &ColorEditWidget::modified);
layout->addWidget(m_choosecolor, 1);
// emit the signal of the base class when appropriate
connect(this, &ColorEditWidget::modified, [this](const QColor &) { emit valueChanged(m_index, getColor(), true); });
connect(m_choosecolor, &ChooseColorWidget::modified, [this](const QColor &) { emit valueChanged(m_index, getColor(), true); });
// setup comment
setToolTip(comment);
setMinimumHeight(m_button->sizeHint().height());
}
void ColorEditWidget::slotShowComment(bool) {}
......@@ -65,22 +43,15 @@ void ColorEditWidget::slotRefresh()
{
QSignalBlocker bk(this);
QString color = m_model->data(m_index, AssetParameterModel::ValueRole).toString();
m_button->setColor(QColorUtils::stringToColor(color));
m_choosecolor->setColor(QColorUtils::stringToColor(color));
}
QString ColorEditWidget::getColor() const
{
return QColorUtils::colorToString(m_button->color(), m_button->isAlphaChannelEnabled());
return QColorUtils::colorToString(m_choosecolor->color(), m_choosecolor->isAlphaChannelEnabled());
}
void ColorEditWidget::setColor(const QColor &color)
{
m_button->setColor(color);
}
void ColorEditWidget::slotColorModified(const QColor &color)
{
blockSignals(true);
m_button->setColor(color);
blockSignals(false);
m_choosecolor->setColor(color);
}
......@@ -9,6 +9,7 @@
#include <QWidget>
class KColorButton;
class ChooseColorWidget;
/** @class ColorEditWidget
@brief Provides options to choose a color.
......@@ -27,10 +28,7 @@ public:
QString getColor() const;
private:
KColorButton *m_button;
public slots:
void slotColorModified(const QColor &color);
ChooseColorWidget *m_choosecolor;
public slots:
/** @brief Toggle the comments on or off
......
......@@ -374,9 +374,18 @@ ClipPropertiesController::ClipPropertiesController(ClipController *controller, Q
// Edit color widget
m_originalProperties.insert(QStringLiteral("resource"), m_properties->get("resource"));
mlt_color color = m_properties->get_color("resource");
ChooseColorWidget *choosecolor = new ChooseColorWidget(i18n("Color"), QColor::fromRgb(color.r, color.g, color.b).name(), "", false, this);
fpBox->addWidget(choosecolor);
// connect(choosecolor, SIGNAL(displayMessage(QString,int)), this, SIGNAL(displayMessage(QString,int)));
QLabel *label = new QLabel(i18n("Color"), this);
ChooseColorWidget *choosecolor = new ChooseColorWidget(this, QColor::fromRgb(color.r, color.g, color.b), false);
auto *colorLay = new QHBoxLayout(this);
colorLay->setContentsMargins(0, 0, 0, 0);
colorLay->setSpacing(0);
colorLay->addWidget(label);
colorLay->addStretch();
colorLay->addWidget(choosecolor);
fpBox->addLayout(colorLay);
connect(choosecolor, &ChooseColorWidget::modified, this, &ClipPropertiesController::slotColorModified);
connect(this, static_cast<void (ClipPropertiesController::*)(const QColor &)>(&ClipPropertiesController::modified), choosecolor,
&ChooseColorWidget::slotColorModified);
......
......@@ -19,10 +19,22 @@ if (BUILD_DESIGNERPLUGIN)
GROUP "Kdenlive Widgets"
)
ecm_qtdesignerplugin_widget(ColorPickerWidget
TOOLTIP "A custom QSpinBox widget to enter timecodes (Kdenlive)"
GROUP "Kdenlive Widgets"
)
ecm_qtdesignerplugin_widget(ChooseColorWidget
TOOLTIP "A custom QSpinBox widget to enter timecodes (Kdenlive)"
GROUP "Kdenlive Widgets"
)
ecm_add_qtdesignerplugin(kdenlivewidgets
NAME KdenliveWidgets
WIDGETS
TimecodeDisplay
ColorPickerWidget
ChooseColorWidget
LINK_LIBRARIES
kdenliveLib
INSTALL_DESTINATION "${KDE_INSTALL_QTPLUGINDIR}/designer"
......
......@@ -6,7 +6,6 @@ SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#include "choosecolorwidget.h"
#include "colorpickerwidget.h"
#include "utils/qcolorutils.h"
#include <QHBoxLayout>
#include <QLabel>
......@@ -14,48 +13,30 @@ SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#include <KColorButton>
ChooseColorWidget::ChooseColorWidget(const QString &text, const QString &color, const QString &comment, bool alphaEnabled, QWidget *parent)
ChooseColorWidget::ChooseColorWidget(QWidget *parent, const QColor &color, bool alphaEnabled)
: QWidget(parent)
{
auto *layout = new QHBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
QLabel *label = new QLabel(text, this);
QWidget *rightSide = new QWidget(this);
auto *rightSideLayout = new QHBoxLayout(rightSide);
rightSideLayout->setContentsMargins(0, 0, 0, 0);
rightSideLayout->setSpacing(0);
m_button = new KColorButton(QColorUtils::stringToColor(color), rightSide);
m_button = new KColorButton(color, this);
if (alphaEnabled) {
m_button->setAlphaChannelEnabled(alphaEnabled);
}
// m_button->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
auto *picker = new ColorPickerWidget(rightSide);
auto *picker = new ColorPickerWidget(this);
layout->addWidget(label, 1);
layout->addWidget(rightSide, 1);
rightSideLayout->addStretch();
rightSideLayout->addWidget(m_button, 2);
rightSideLayout->addWidget(picker);
layout->addWidget(m_button, 2);
layout->addWidget(picker);
connect(picker, &ColorPickerWidget::colorPicked, this, &ChooseColorWidget::setColor);
connect(picker, &ColorPickerWidget::disableCurrentFilter, this, &ChooseColorWidget::disableCurrentFilter);
connect(m_button, &KColorButton::changed, this, &ChooseColorWidget::modified);
// connect the signal of the derived class to the signal of the base class
connect(this, &ChooseColorWidget::modified, [this](const QColor &) { emit valueChanged(); });
// setup comment
setToolTip(comment);
}
QString ChooseColorWidget::getColor() const
QColor ChooseColorWidget::color() const
{
bool alphaChannel = m_button->isAlphaChannelEnabled();
return QColorUtils::colorToString(m_button->color(), alphaChannel);
return m_button->color();
}
void ChooseColorWidget::setColor(const QColor &color)
......@@ -63,6 +44,16 @@ void ChooseColorWidget::setColor(const QColor &color)
m_button->setColor(color);
}
bool ChooseColorWidget::isAlphaChannelEnabled() const
{
return m_button->isAlphaChannelEnabled();
}
void ChooseColorWidget::setAlphaChannelEnabled(bool alpha)
{
m_button->setAlphaChannelEnabled(alpha);
}
void ChooseColorWidget::slotColorModified(const QColor &color)
{
blockSignals(true);
......
......@@ -9,6 +9,7 @@ SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#include <QWidget>
class KColorButton;
class QLabel;
/** @class ChooseColorWidget
@brief Provides options to choose a color.
......@@ -18,34 +19,38 @@ class KColorButton;
class ChooseColorWidget : public QWidget
{
Q_OBJECT
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY modified USER true)
Q_PROPERTY(bool alphaChannelEnabled READ isAlphaChannelEnabled WRITE setAlphaChannelEnabled)
public:
/** @brief Sets up the widget.
* @param name (optional) What the color will be used for (name of the parameter)
* @param parent(optional) Parent widget
* @param color (optional) initial color
* @param comment (optional) Comment about the parameter
* @param alphaEnabled (optional) Should transparent colors be enabled
* @param parent(optional) Parent widget
*/
explicit ChooseColorWidget(const QString &name = QString(), const QString &color = QStringLiteral("0xffffffff"), const QString &comment = QString(),
bool alphaEnabled = false, QWidget *parent = nullptr);
explicit ChooseColorWidget(QWidget *parent = nullptr, const QColor &color = QColor(), bool alphaEnabled = false);
/** @brief Gets the chosen color. */
QString getColor() const;
/** @brief Gets the choosen color. */
QColor color() const;
/** @brief Returns true if the user is allowed to change the alpha component. */
bool isAlphaChannelEnabled() const;
private:
KColorButton *m_button;
QLabel *m_nameLabel;
public slots:
/** @brief Updates the different color choosing options to have all selected @param color. */
void setColor(const QColor &color);
/** @brief Updates the color to @param color without emitting signals. */
void slotColorModified(const QColor &color);
private slots:
/** @brief Updates the different color choosing options to have all selected @param color. */
void setColor(const QColor &color);
void setAlphaChannelEnabled(bool alpha);
signals:
/** @brief Emitted whenever a different color was chosen. */
void modified(QColor = QColor());
void disableCurrentFilter(bool);
void valueChanged();
};
......@@ -52,10 +52,10 @@ ColorPickerWidget::ColorPickerWidget(QWidget *parent)
auto *button = new QToolButton(this);
button->setIcon(QIcon::fromTheme(QStringLiteral("color-picker")));
button->setToolTip(QStringLiteral("<p>") +
i18n("Pick a color on the screen. By pressing the mouse button and then moving your mouse you can select a "
"section of the screen from which to get an average color.") +
QStringLiteral("</p>"));
button->setToolTip(i18n("Pick a color on the screen."));
button->setWhatsThis(xi18nc("@info:whatsthis", "Pick a color on the screen. By pressing the mouse button and then moving your mouse you can select a "
"section of the screen from which to get an average color."));
button->setAutoRaise(true);
connect(button, &QAbstractButton::clicked, this, &ColorPickerWidget::slotSetupEventFilter);
......
......@@ -79,8 +79,7 @@ private slots:
void slotGetAverageColor();
signals:
/** @brief Signal fired when a new color has been picked
*/
/** @brief Signal fired when a new color has been picked */
void colorPicked(const QColor &);
/** @brief When user wants to pick a color, it's better to disable filter so we get proper color values. */
void disableCurrentFilter(bool);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment