summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Holthuis <jan.holthuis@ruhr-uni-bochum.de>2020-03-17 16:46:57 +0100
committerJan Holthuis <jan.holthuis@ruhr-uni-bochum.de>2020-03-17 16:48:57 +0100
commit7403dffdcd63cc5c9ba90d7292742afc6fec41a3 (patch)
tree562d7c2b58c4b4b8dff0fb280047d0546a9c8216
parente779252dee0f3ec43a592ab86f9c47dee32503d7 (diff)
widget/wcolorpicker: Add support for custom colors
-rw-r--r--src/widget/wcolorpicker.cpp42
-rw-r--r--src/widget/wcolorpicker.h3
2 files changed, 44 insertions, 1 deletions
diff --git a/src/widget/wcolorpicker.cpp b/src/widget/wcolorpicker.cpp
index c65c876bd3..0c5fde4a76 100644
--- a/src/widget/wcolorpicker.cpp
+++ b/src/widget/wcolorpicker.cpp
@@ -1,5 +1,6 @@
#include "widget/wcolorpicker.h"
+#include <QColorDialog>
#include <QMapIterator>
#include <QPushButton>
#include <QStyle>
@@ -39,7 +40,8 @@ WColorPicker::WColorPicker(Options options, const ColorPalette& palette, QWidget
: QWidget(parent),
m_options(options),
m_palette(palette),
- m_pNoColorButton(nullptr) {
+ m_pNoColorButton(nullptr),
+ m_pCustomColorButton(nullptr) {
QGridLayout* pLayout = new QGridLayout();
pLayout->setMargin(0);
pLayout->setContentsMargins(0, 0, 0, 0);
@@ -72,6 +74,11 @@ void WColorPicker::removeColorButtons() {
return;
}
+ if (m_pCustomColorButton) {
+ pLayout->removeWidget(m_pCustomColorButton);
+ delete m_pCustomColorButton;
+ }
+
while (!m_colorButtons.isEmpty()) {
QPushButton* pColorButton = m_colorButtons.takeLast();
pLayout->removeWidget(pColorButton);
@@ -98,6 +105,9 @@ void WColorPicker::addColorButtons() {
if (m_options.testFlag(Option::AllowNoColor)) {
numColors++;
}
+ if (m_options.testFlag(Option::AllowCustomColor)) {
+ numColors++;
+ }
int numColumns = idealColumnCount(numColors);
if (m_options.testFlag(Option::AllowNoColor)) {
@@ -113,6 +123,11 @@ void WColorPicker::addColorButtons() {
row++;
}
}
+
+ if (m_options.testFlag(Option::AllowCustomColor)) {
+ addCustomColorButton(pLayout, row, column);
+ column++;
+ }
}
void WColorPicker::addColorButton(mixxx::RgbColor color, QGridLayout* pLayout, int row, int column) {
@@ -159,6 +174,31 @@ void WColorPicker::addNoColorButton(QGridLayout* pLayout, int row, int column) {
pLayout->addWidget(pButton, row, column);
}
+void WColorPicker::addCustomColorButton(QGridLayout* pLayout, int row, int column) {
+ QPushButton* pButton = m_pCustomColorButton;
+ if (!pButton) {
+ pButton = make_parented<QPushButton>("", this);
+ if (m_pStyle) {
+ pButton->setStyle(m_pStyle);
+ }
+
+ pButton->setProperty("customColor", true);
+ pButton->setToolTip(tr("Custom color"));
+ pButton->setCheckable(true);
+ connect(pButton,
+ &QPushButton::clicked,
+ this,
+ [this]() {
+ QColor color = QColorDialog::getColor();
+ if (color.isValid()) {
+ emit colorPicked(mixxx::RgbColor::fromQColor(color));
+ }
+ });
+ m_pCustomColorButton = pButton;
+ }
+ pLayout->addWidget(pButton, row, column);
+}
+
void WColorPicker::setColorButtonChecked(mixxx::RgbColor::optional_t color, bool checked) {
// Unset currently selected color
QPushButton* pButton = nullptr;
diff --git a/src/widget/wcolorpicker.h b/src/widget/wcolorpicker.h
index 8b358728a6..679f235b56 100644
--- a/src/widget/wcolorpicker.h
+++ b/src/widget/wcolorpicker.h
@@ -15,6 +15,7 @@ class WColorPicker : public QWidget {
enum class Option {
NoOptions = 0,
AllowNoColor = 1,
+ AllowCustomColor = 1 << 1,
};
Q_DECLARE_FLAGS(Options, Option);
@@ -36,10 +37,12 @@ class WColorPicker : public QWidget {
void removeColorButtons();
void addColorButton(mixxx::RgbColor color, QGridLayout* pLayout, int row, int column);
void addNoColorButton(QGridLayout* pLayout, int row, int column);
+ void addCustomColorButton(QGridLayout* pLayout, int row, int column);
Options m_options;
mixxx::RgbColor::optional_t m_selectedColor;
ColorPalette m_palette;
QPushButton* m_pNoColorButton;
+ QPushButton* m_pCustomColorButton;
QList<QPushButton*> m_colorButtons;
QStyle* m_pStyle;
};