Skip to content

Commit

Permalink
Migrate hotcue color logic to mixxx::RgbColor
Browse files Browse the repository at this point in the history
  • Loading branch information
Holzhaus committed Feb 26, 2020
1 parent 3b78714 commit 4c82eee
Show file tree
Hide file tree
Showing 21 changed files with 168 additions and 116 deletions.
19 changes: 10 additions & 9 deletions src/controllers/colorjsproxy.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "controllers/colorjsproxy.h"

#include "preferences/hotcuecolorpalettesettings.h"
#include "util/color/rgbcolor.h"

ColorJSProxy::ColorJSProxy(QScriptEngine* pScriptEngine,
HotcueColorPaletteSettings colorPaletteSettings)
Expand All @@ -16,25 +18,24 @@ Q_INVOKABLE QScriptValue ColorJSProxy::hotcueColorPalette() {
}

QScriptValue ColorJSProxy::colorFromHexCode(uint colorCode) {
QColor color = QColor::fromRgba(colorCode);
QRgb rgb = QRgb(colorCode);
QScriptValue jsColor = m_pScriptEngine->newObject();
jsColor.setProperty("red", color.red());
jsColor.setProperty("green", color.green());
jsColor.setProperty("blue", color.blue());
jsColor.setProperty("alpha", color.alpha());
jsColor.setProperty("red", qRed(rgb));
jsColor.setProperty("green", qGreen(rgb));
jsColor.setProperty("blue", qBlue(rgb));
jsColor.setProperty("alpha", qAlpha(rgb));
return jsColor;
}

QScriptValue ColorJSProxy::makeHotcueColorPalette(QScriptEngine* pScriptEngine,
HotcueColorPaletteSettings colorPaletteSettings) {
// TODO: make sure we get notified when the palette changes
QList<QColor> colorList =
colorPaletteSettings.getHotcueColorPalette().m_colorList;
QList<mixxx::RgbColor> colorList = colorPaletteSettings.getHotcueColorPalette().m_colorList;
int numColors = colorList.length();
QScriptValue jsColorList = pScriptEngine->newArray(numColors);
for (int i = 0; i < numColors; ++i) {
QColor color = colorList.at(i);
jsColorList.setProperty(i, colorFromHexCode(color.rgba()));
mixxx::RgbColor color = colorList.at(i);
jsColorList.setProperty(i, colorFromHexCode(color));
}
return jsColorList;
}
36 changes: 23 additions & 13 deletions src/engine/controls/cuecontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ void CueControl::detachCue(HotcueControl* pControl) {
disconnect(pCue.get(), 0, this, 0);
pControl->resetCue();
// Reset the color CO to -1
pControl->setColor(QColor());
pControl->setColor(std::nullopt);
}

void CueControl::trackLoaded(TrackPointer pNewTrack) {
Expand Down Expand Up @@ -1694,8 +1694,11 @@ void CueControl::hotcueFocusColorPrev(double v) {
return;
}

QColor color = pControl->getColor();
DEBUG_ASSERT(color.isValid());
mixxx::RgbColor::optional_t controlColor = pControl->getColor();
if (!controlColor) {
return;
}
mixxx::RgbColor color = controlColor.value();

HotcueColorPaletteSettings m_colorPaletteSettings(m_pConfig);
ColorPalette colorPalette = m_colorPaletteSettings.getHotcueColorPalette();
Expand Down Expand Up @@ -1725,8 +1728,11 @@ void CueControl::hotcueFocusColorNext(double v) {
return;
}

QColor color = pControl->getColor();
DEBUG_ASSERT(color.isValid());
mixxx::RgbColor::optional_t controlColor = pControl->getColor();
if (!controlColor) {
return;
}
mixxx::RgbColor color = controlColor.value();

HotcueColorPaletteSettings colorPaletteSettings(m_pConfig);
ColorPalette colorPalette = colorPaletteSettings.getHotcueColorPalette();
Expand Down Expand Up @@ -1857,7 +1863,7 @@ void HotcueControl::slotHotcuePositionChanged(double newPosition) {
}

void HotcueControl::slotHotcueColorChanged(double newColor) {
m_pCue->setColor(QColor::fromRgba(newColor));
m_pCue->setColor(mixxx::RgbColor(newColor));
emit(hotcueColorChanged(this, newColor));
}

Expand All @@ -1872,16 +1878,20 @@ void HotcueControl::setCue(CuePointer pCue) {
// because we have a null check for valid data else where in the code
m_pCue = pCue;
}
QColor HotcueControl::getColor() const {
return QColor::fromRgba(m_hotcueColor->get());
mixxx::RgbColor::optional_t HotcueControl::getColor() const {
double value = m_hotcueColor->get();
if (value < 0) {
return std::nullopt;
}
return mixxx::RgbColor(value);
}

void HotcueControl::setColor(const QColor& newColor) {
if (newColor.isValid()) {
m_hotcueColor->set(newColor.rgba());
} else {
m_hotcueColor->set(-1);
void HotcueControl::setColor(const mixxx::RgbColor::optional_t& newColor) {
if (newColor) {
m_hotcueColor->set(newColor.value());
return;
}
m_hotcueColor->set(-1);
}
void HotcueControl::resetCue() {
// clear pCue first because we have a null check for valid data else where
Expand Down
4 changes: 2 additions & 2 deletions src/engine/controls/cuecontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class HotcueControl : public QObject {
void setCue(CuePointer pCue);
void resetCue();
void setPosition(double position);
void setColor(const QColor& newColor);
QColor getColor() const;
void setColor(const mixxx::RgbColor::optional_t& newColor);
mixxx::RgbColor::optional_t getColor() const;

// Used for caching the preview state of this hotcue control.
inline bool isPreviewing() {
Expand Down
14 changes: 8 additions & 6 deletions src/library/dao/cuedao.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// cuedao.cpp
// Created 10/26/2009 by RJ Ryan (rryan@mit.edu)

#include "library/dao/cuedao.h"

#include <QVariant>
#include <QtDebug>
#include <QtSql>
#include <QVariant>

#include "library/dao/cuedao.h"
#include "library/queryutil.h"
#include "track/cue.h"
#include "track/track.h"
#include "library/queryutil.h"
#include "util/assert.h"
#include "util/color/rgbcolor.h"
#include "util/performancetimer.h"

int CueDAO::cueCount() {
Expand Down Expand Up @@ -52,7 +54,7 @@ CuePointer CueDAO::cueFromRow(const QSqlQuery& query) const {
int hotcue = record.value(record.indexOf("hotcue")).toInt();
QString label = record.value(record.indexOf("label")).toString();
uint colorValue = record.value(record.indexOf("color")).toUInt();
QColor color = QColor::fromRgba(colorValue);
mixxx::RgbColor color = mixxx::RgbColor(colorValue);
CuePointer pCue(new Cue(id,
trackId,
(Cue::Type)type,
Expand Down Expand Up @@ -152,7 +154,7 @@ bool CueDAO::saveCue(Cue* cue) {
query.bindValue(":length", cue->getLength());
query.bindValue(":hotcue", cue->getHotCue());
query.bindValue(":label", cue->getLabel());
query.bindValue(":color", cue->getColor().rgba());
query.bindValue(":color", static_cast<quint32>(cue->getColor()));

if (query.exec()) {
int id = query.lastInsertId().toInt();
Expand Down Expand Up @@ -180,7 +182,7 @@ bool CueDAO::saveCue(Cue* cue) {
query.bindValue(":length", cue->getLength());
query.bindValue(":hotcue", cue->getHotCue());
query.bindValue(":label", cue->getLabel());
query.bindValue(":color", cue->getColor().rgba());
query.bindValue(":color", static_cast<quint32>(cue->getColor()));

if (query.exec()) {
cue->setDirty(false);
Expand Down
51 changes: 40 additions & 11 deletions src/preferences/configobject.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "preferences/configobject.h"

#include <QIODevice>
#include <QTextStream>
#include <QApplication>
#include <QDir>
#include <QIODevice>
#include <QTextStream>
#include <QtDebug>

#include "widget/wwidget.h"
#include "util/cmdlineargs.h"
#include "util/color/rgbcolor.h"
#include "util/xml.h"
#include "widget/wwidget.h"

// TODO(rryan): Move to a utility file.
namespace {
Expand Down Expand Up @@ -285,8 +286,19 @@ void ConfigObject<ConfigValue>::setValue(
template<>
template<>
void ConfigObject<ConfigValue>::setValue(
const ConfigKey& key, const QColor& value) {
set(key, ConfigValue(value.name(QColor::NameFormat::HexArgb)));
const ConfigKey& key, const mixxx::RgbColor::optional_t& value) {
if (!value) {
set(key, ConfigValue(""));
return;
}
set(key, ConfigValue(mixxx::toQColor(value.value()).name(QColor::NameFormat::HexArgb)));
}

template<>
template<>
void ConfigObject<ConfigValue>::setValue(
const ConfigKey& key, const mixxx::RgbColor& value) {
set(key, ConfigValue(mixxx::toQColor(value).name(QColor::NameFormat::HexArgb)));
}

template <> template <>
Expand Down Expand Up @@ -327,20 +339,37 @@ double ConfigObject<ConfigValue>::getValue(

template<>
template<>
QColor ConfigObject<ConfigValue>::getValue(
const ConfigKey& key, const QColor& default_value) const {
mixxx::RgbColor::optional_t ConfigObject<ConfigValue>::getValue(
const ConfigKey& key, const mixxx::RgbColor::optional_t& default_value) const {
const ConfigValue value = get(key);
if (value.isNull()) {
return default_value;
}
auto color = mixxx::RgbColor::optional_t(value.value.toUInt());
return color ? color : default_value;
}

template<>
template<>
mixxx::RgbColor::optional_t ConfigObject<ConfigValue>::getValue(const ConfigKey& key) const {
return getValue(key, mixxx::RgbColor::optional_t(std::nullopt));
}

template<>
template<>
mixxx::RgbColor ConfigObject<ConfigValue>::getValue(
const ConfigKey& key, const mixxx::RgbColor& default_value) const {
const ConfigValue value = get(key);
if (value.isNull()) {
return default_value;
}
auto color = QColor(value.value);
return color.isValid() ? color : default_value;
return mixxx::RgbColor(value.value.toUInt());
}

template<>
template<>
QColor ConfigObject<ConfigValue>::getValue(const ConfigKey& key) const {
return getValue(key, QColor());
mixxx::RgbColor ConfigObject<ConfigValue>::getValue(const ConfigKey& key) const {
return getValue(key, mixxx::RgbColor(0));
}

// For string literal default
Expand Down
12 changes: 5 additions & 7 deletions src/preferences/hotcuecolorpalettesettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
const QString HotcueColorPaletteSettings::sGroup = "[HotcueColorPalette]";

ColorPalette HotcueColorPaletteSettings::getHotcueColorPalette() const {
QList<QColor> colorList;
QList<mixxx::RgbColor> colorList;
for (const ConfigKey& key : m_pConfig->getKeysWithGroup(sGroup)) {
QColor color = m_pConfig->getValue<QColor>(key);
if (color.isValid()) {
colorList.append(color);
}
mixxx::RgbColor color = mixxx::RgbColor(m_pConfig->getValue<mixxx::RgbColor>(key, mixxx::RgbColor(0)));
colorList.append(color);
}

// If no palette is defined in the settings, we use the default one.
Expand All @@ -24,8 +22,8 @@ void HotcueColorPaletteSettings::setHotcueColorPalette(
removePalette();

for (int index = 0; index < colorPalette.m_colorList.count(); ++index) {
QColor color = colorPalette.m_colorList[index];
m_pConfig->setValue<QColor>(keyForIndex(index), color);
mixxx::RgbColor color = colorPalette.m_colorList[index];
m_pConfig->setValue<mixxx::RgbColor>(keyForIndex(index), color);
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/test/controllerengine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,23 +620,23 @@ TEST_F(ControllerEngineTest, connectionExecutesWithCorrectThisObject) {
}

TEST_F(ControllerEngineTest, colorProxyTestMixxxPalette) {
QList<QColor> allColors = ColorPalette::mixxxPalette.m_colorList;
QList<mixxx::RgbColor> allColors = ColorPalette::mixxxPalette.m_colorList;
for (int i = 0; i < allColors.length(); ++i) {
QColor color = allColors[i];
qDebug() << "Testing color " << color.name();
QString colorCode = QString::number(color.rgba());
mixxx::RgbColor color = allColors[i];
qDebug() << "Testing color " << toQColor(color).name();
QString colorCode = QString::number(color);
QScriptValue jsColor = pScriptEngine->evaluate(
"color.colorFromHexCode(" + colorCode + ")");
EXPECT_EQ(jsColor.property("red").toInt32(), color.red());
EXPECT_EQ(jsColor.property("green").toInt32(), color.green());
EXPECT_EQ(jsColor.property("blue").toInt32(), color.blue());
EXPECT_EQ(jsColor.property("alpha").toInt32(), color.alpha());
EXPECT_EQ(jsColor.property("red").toInt32(), qRed(color));
EXPECT_EQ(jsColor.property("green").toInt32(), qGreen(color));
EXPECT_EQ(jsColor.property("blue").toInt32(), qBlue(color));
EXPECT_EQ(jsColor.property("alpha").toInt32(), qAlpha(color));

QScriptValue jsColor2 = pScriptEngine->evaluate(
"color.hotcueColorPalette()[" + QString::number(i) + "]");
EXPECT_EQ(jsColor2.property("red").toInt32(), color.red());
EXPECT_EQ(jsColor2.property("green").toInt32(), color.green());
EXPECT_EQ(jsColor2.property("blue").toInt32(), color.blue());
EXPECT_EQ(jsColor2.property("alpha").toInt32(), color.alpha());
EXPECT_EQ(jsColor2.property("red").toInt32(), qRed(color));
EXPECT_EQ(jsColor2.property("green").toInt32(), qGreen(color));
EXPECT_EQ(jsColor2.property("blue").toInt32(), qBlue(color));
EXPECT_EQ(jsColor2.property("alpha").toInt32(), qAlpha(color));
}
}
9 changes: 5 additions & 4 deletions src/track/cue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace {
const QString kDefaultLabel = ""; // empty string, not null
const mixxx::RgbColor kDefaultCueColor = mixxx::RgbColor(0xFFF2F2FF); // white
}

//static
Expand All @@ -28,7 +29,7 @@ Cue::Cue(TrackId trackId)
m_sampleEndPosition(Cue::kNoPosition),
m_iHotCue(-1),
m_label(kDefaultLabel),
m_color(QColor()) {
m_color(kDefaultCueColor) {
DEBUG_ASSERT(!m_label.isNull());
}

Expand All @@ -39,7 +40,7 @@ Cue::Cue(int id,
double length,
int hotCue,
QString label,
QColor color)
mixxx::RgbColor color)
: m_bDirty(false),
m_iId(id),
m_trackId(trackId),
Expand Down Expand Up @@ -159,12 +160,12 @@ void Cue::setLabel(const QString label) {
emit updated();
}

QColor Cue::getColor() const {
mixxx::RgbColor Cue::getColor() const {
QMutexLocker lock(&m_mutex);
return m_color;
}

void Cue::setColor(const QColor& color) {
void Cue::setColor(const mixxx::RgbColor& color) {
QMutexLocker lock(&m_mutex);
m_color = color;
m_bDirty = true;
Expand Down
Loading

0 comments on commit 4c82eee

Please sign in to comment.