Skip to content

Commit

Permalink
Merge pull request #2016 from ferranpujolcamins/Enable-Setting-hotcue…
Browse files Browse the repository at this point in the history
…-color-via-controlobject

Enable setting hotcue color via controlobject
  • Loading branch information
daschuer authored Feb 13, 2019
2 parents b701577 + a625424 commit 4c2f240
Show file tree
Hide file tree
Showing 27 changed files with 611 additions and 62 deletions.
2 changes: 2 additions & 0 deletions build/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ def sources(self, build):
"src/controllers/midi/midioutputhandler.cpp",
"src/controllers/softtakeover.cpp",
"src/controllers/keyboard/keyboardeventfilter.cpp",
"src/controllers/colorjsproxy.cpp",

"src/main.cpp",
"src/mixxx.cpp",
Expand Down Expand Up @@ -1225,6 +1226,7 @@ def sources(self, build):
"src/util/widgetrendertimer.cpp",
"src/util/workerthread.cpp",
"src/util/workerthreadscheduler.cpp",
"src/util/color/predefinedcolor.cpp"
]

proto_args = {
Expand Down
36 changes: 36 additions & 0 deletions src/controllers/colorjsproxy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "controllers/colorjsproxy.h"

ColorJSProxy::ColorJSProxy(QScriptEngine* pScriptEngine)
: m_pScriptEngine(pScriptEngine),
m_predefinedColorsList(makePredefinedColorsList(pScriptEngine)){};

ColorJSProxy::~ColorJSProxy() {};

QScriptValue ColorJSProxy::predefinedColorFromId(int iId) {
PredefinedColorPointer color(Color::predefinedColorSet.predefinedColorFromId(iId));
return jsColorFrom(color);
};

Q_INVOKABLE QScriptValue ColorJSProxy::predefinedColorsList() {
return m_predefinedColorsList;
}

QScriptValue ColorJSProxy::jsColorFrom(PredefinedColorPointer predefinedColor) {
QScriptValue jsColor = m_pScriptEngine->newObject();
jsColor.setProperty("red", predefinedColor->m_defaultRgba.red());
jsColor.setProperty("green", predefinedColor->m_defaultRgba.green());
jsColor.setProperty("blue", predefinedColor->m_defaultRgba.blue());
jsColor.setProperty("alpha", predefinedColor->m_defaultRgba.alpha());
jsColor.setProperty("id", predefinedColor->m_iId);
return jsColor;
}

QScriptValue ColorJSProxy::makePredefinedColorsList(QScriptEngine* pScriptEngine) {
int numColors = Color::predefinedColorSet.allColors.length();
QScriptValue colorList = pScriptEngine->newArray(numColors);
for (int i = 0; i < numColors; ++i) {
PredefinedColorPointer color = Color::predefinedColorSet.allColors.at(i);
colorList.setProperty(i, jsColorFrom(color));
}
return colorList;
}
27 changes: 27 additions & 0 deletions src/controllers/colorjsproxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef COLORJSPROXY_H
#define COLORJSPROXY_H

#include <QObject>
#include <QScriptEngine>
#include <QScriptValue>

#include "util/color/color.h"

class ColorJSProxy: public QObject {
Q_OBJECT
public:
ColorJSProxy(QScriptEngine* pScriptEngine);

virtual ~ColorJSProxy();

Q_INVOKABLE QScriptValue predefinedColorFromId(int iId);
Q_INVOKABLE QScriptValue predefinedColorsList();

private:
QScriptValue jsColorFrom(PredefinedColorPointer predefinedColor);
QScriptValue makePredefinedColorsList(QScriptEngine* pScriptEngine);
QScriptEngine* m_pScriptEngine;
QScriptValue m_predefinedColorsList;
};

#endif /* COLORJSPROXY_H */
4 changes: 4 additions & 0 deletions src/controllers/controllerengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ void ControllerEngine::gracefulShutdown() {
++it;
}

m_pColorJSProxy.reset();
delete m_pBaClass;
m_pBaClass = nullptr;
}
Expand Down Expand Up @@ -212,6 +213,9 @@ void ControllerEngine::initializeScriptEngine() {
engineGlobalObject.setProperty("midi", m_pEngine->newQObject(m_pController));
}

m_pColorJSProxy = std::make_unique<ColorJSProxy>(m_pEngine);
engineGlobalObject.setProperty("color", m_pEngine->newQObject(m_pColorJSProxy.get()));

m_pBaClass = new ByteArrayClass(m_pEngine);
engineGlobalObject.setProperty("ByteArray", m_pBaClass->constructor());
}
Expand Down
3 changes: 3 additions & 0 deletions src/controllers/controllerengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
#include "bytearrayclass.h"
#include "preferences/usersettings.h"
#include "controllers/controllerpreset.h"
#include "controllers/colorjsproxy.h"
#include "controllers/softtakeover.h"
#include "util/alphabetafilter.h"
#include "util/duration.h"
#include "util/memory.h"

// Forward declaration(s)
class Controller;
Expand Down Expand Up @@ -204,6 +206,7 @@ class ControllerEngine : public QObject {
QHash<int, TimerInfo> m_timers;
SoftTakeoverCtrl m_st;
ByteArrayClass* m_pBaClass;
std::unique_ptr<ColorJSProxy> m_pColorJSProxy;
// 256 (default) available virtual decks is enough I would think.
// If more are needed at run-time, these will move to the heap automatically
QVarLengthArray<int> m_intervalAccumulator;
Expand Down
20 changes: 20 additions & 0 deletions src/engine/controls/cuecontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "control/controlindicator.h"
#include "vinylcontrol/defs_vinylcontrol.h"
#include "util/sample.h"
#include "util/color/color.h"

// TODO: Convert these doubles to a standard enum
// and convert elseif logic to switch statements
Expand Down Expand Up @@ -297,6 +298,7 @@ void CueControl::trackCuesUpdated() {
} else {
// If the old hotcue is the same, then we only need to update
pControl->setPosition(pCue->getPosition());
pControl->setColor(pCue->getColor());
}
// Add the hotcue to the list of active hotcues
active_hotcues.insert(hotcue);
Expand Down Expand Up @@ -1036,6 +1038,12 @@ HotcueControl::HotcueControl(QString group, int i)
m_hotcueEnabled = new ControlObject(keyForControl(i, "enabled"));
m_hotcueEnabled->setReadOnly();

// The id of the predefined color assigned to this color.
m_hotcueColor = new ControlObject(keyForControl(i, "color_id"));
connect(m_hotcueColor, SIGNAL(valueChanged(double)),
this, SLOT(slotHotcueColorChanged(double)),
Qt::DirectConnection);

m_hotcueSet = new ControlPushButton(keyForControl(i, "set"));
connect(m_hotcueSet, &ControlObject::valueChanged,
this, &HotcueControl::slotHotcueSet,
Expand Down Expand Up @@ -1075,6 +1083,7 @@ HotcueControl::HotcueControl(QString group, int i)
HotcueControl::~HotcueControl() {
delete m_hotcuePosition;
delete m_hotcueEnabled;
delete m_hotcueColor;
delete m_hotcueSet;
delete m_hotcueGoto;
delete m_hotcueGotoAndPlay;
Expand Down Expand Up @@ -1117,6 +1126,11 @@ void HotcueControl::slotHotcuePositionChanged(double newPosition) {
emit(hotcuePositionChanged(this, newPosition));
}

void HotcueControl::slotHotcueColorChanged(double newColorId) {
m_pCue->setColor(Color::predefinedColorSet.predefinedColorFromId(newColorId));
emit(hotcueColorChanged(this, newColorId));
}

double HotcueControl::getPosition() const {
return m_hotcuePosition->get();
}
Expand All @@ -1127,7 +1141,13 @@ void HotcueControl::setCue(CuePointer pCue) {
// because we have a null check for valid data else where in the code
m_pCue = pCue;
}
PredefinedColorPointer HotcueControl::getColor() const {
return Color::predefinedColorSet.predefinedColorFromId(m_hotcueColor->get());
}

void HotcueControl::setColor(PredefinedColorPointer newColor) {
m_hotcueColor->set(static_cast<double>(newColor->m_iId));
}
void HotcueControl::resetCue() {
// clear pCue first because we have a null check for valid data else where
// in the code
Expand Down
5 changes: 5 additions & 0 deletions src/engine/controls/cuecontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class HotcueControl : public QObject {
void setCue(CuePointer pCue);
void resetCue();
void setPosition(double position);
void setColor(PredefinedColorPointer newColor);
PredefinedColorPointer getColor() const;

// Used for caching the preview state of this hotcue control.
inline bool isPreviewing() {
Expand All @@ -54,6 +56,7 @@ class HotcueControl : public QObject {
void slotHotcueActivatePreview(double v);
void slotHotcueClear(double v);
void slotHotcuePositionChanged(double newPosition);
void slotHotcueColorChanged(double newColorId);

signals:
void hotcueSet(HotcueControl* pHotcue, double v);
Expand All @@ -64,6 +67,7 @@ class HotcueControl : public QObject {
void hotcueActivatePreview(HotcueControl* pHotcue, double v);
void hotcueClear(HotcueControl* pHotcue, double v);
void hotcuePositionChanged(HotcueControl* pHotcue, double newPosition);
void hotcueColorChanged(HotcueControl* pHotcue, double newColorId);
void hotcuePlay(double v);

private:
Expand All @@ -76,6 +80,7 @@ class HotcueControl : public QObject {
// Hotcue state controls
ControlObject* m_hotcuePosition;
ControlObject* m_hotcueEnabled;
ControlObject* m_hotcueColor;
// Hotcue button controls
ControlObject* m_hotcueSet;
ControlObject* m_hotcueGoto;
Expand Down
10 changes: 5 additions & 5 deletions src/library/dao/cue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "library/dao/cue.h"
#include "util/assert.h"
#include "util/color/color.h"

namespace {
const QColor kDefaultColor = QColor("#FF0000");
const QString kDefaultLabel = ""; // empty string, not null
}

Expand All @@ -25,13 +25,13 @@ Cue::Cue(TrackId trackId)
m_length(0.0),
m_iHotCue(-1),
m_label(kDefaultLabel),
m_color(kDefaultColor) {
m_color(Color::predefinedColorSet.noColor) {
DEBUG_ASSERT(!m_label.isNull());
}


Cue::Cue(int id, TrackId trackId, Cue::CueType type, double position, double length,
int hotCue, QString label, QColor color)
int hotCue, QString label, PredefinedColorPointer color)
: m_bDirty(false),
m_iId(id),
m_trackId(trackId),
Expand Down Expand Up @@ -138,12 +138,12 @@ void Cue::setLabel(const QString label) {
emit(updated());
}

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

void Cue::setColor(const QColor color) {
void Cue::setColor(const PredefinedColorPointer color) {
QMutexLocker lock(&m_mutex);
m_color = color;
m_bDirty = true;
Expand Down
9 changes: 5 additions & 4 deletions src/library/dao/cue.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QColor>

#include "track/trackid.h"
#include "util/color/predefinedcolor.h"
#include "util/memory.h"

class CueDAO;
Expand Down Expand Up @@ -44,16 +45,16 @@ class Cue : public QObject {
QString getLabel() const;
void setLabel(QString label);

QColor getColor() const;
void setColor(QColor color);
PredefinedColorPointer getColor() const;
void setColor(PredefinedColorPointer color);

signals:
void updated();

private:
explicit Cue(TrackId trackId);
Cue(int id, TrackId trackId, CueType type, double position, double length,
int hotCue, QString label, QColor color);
int hotCue, QString label, PredefinedColorPointer color);
void setDirty(bool dirty);
void setId(int id);
void setTrackId(TrackId trackId);
Expand All @@ -68,7 +69,7 @@ class Cue : public QObject {
double m_length;
int m_iHotCue;
QString m_label;
QColor m_color;
PredefinedColorPointer m_color;

friend class Track;
friend class CueDAO;
Expand Down
9 changes: 6 additions & 3 deletions src/library/dao/cuedao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "library/queryutil.h"
#include "util/assert.h"
#include "util/performancetimer.h"
#include "util/color/color.h"
#include "util/color/predefinedcolor.h"

int CueDAO::cueCount() {
qDebug() << "CueDAO::cueCount" << QThread::currentThread() << m_database.connectionName();
Expand Down Expand Up @@ -51,7 +53,8 @@ CuePointer CueDAO::cueFromRow(const QSqlQuery& query) const {
int length = record.value(record.indexOf("length")).toInt();
int hotcue = record.value(record.indexOf("hotcue")).toInt();
QString label = record.value(record.indexOf("label")).toString();
QColor color = QColor::fromRgba(record.value(record.indexOf("color")).toInt());
int iColorId = record.value(record.indexOf("color")).toInt();
PredefinedColorPointer color = Color::predefinedColorSet.predefinedColorFromId(iColorId);
CuePointer pCue(new Cue(id, trackId, (Cue::CueType)type,
position, length, hotcue, label, color));
m_cues[id] = pCue;
Expand Down Expand Up @@ -145,7 +148,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", cue->getColor()->m_iId);

if (query.exec()) {
int id = query.lastInsertId().toInt();
Expand Down Expand Up @@ -173,7 +176,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", cue->getColor()->m_iId);

if (query.exec()) {
cue->setDirty(false);
Expand Down
Loading

0 comments on commit 4c2f240

Please sign in to comment.