summaryrefslogtreecommitdiffstats
path: root/src/preferences/colorpalettesettings.cpp
blob: b6ddf30e7167f1a2ab7bc3db436b01c97d293b8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "preferences/colorpalettesettings.h"

#include "util/color/predefinedcolorpalettes.h"

namespace {
const mixxx::RgbColor kColorBlack(0x000000);
const QString kColorPaletteConfigGroup = QStringLiteral("[Config]");
const QString kColorPaletteGroupStart = QStringLiteral("[ColorPalette ");
const QString kColorPaletteGroupEnd = QStringLiteral("]");
const QRegExp kColorPaletteGroupNameRegex(QStringLiteral("^\\[ColorPalette (.+)\\]$"));
const QString kColorPaletteHotcueIndicesConfigItem = QStringLiteral("hotcue_indices");
const QString kColorPaletteHotcueIndicesConfigSeparator = QStringLiteral(" ");
const QString kColorPaletteGroup = QStringLiteral("[ColorPalette %1]");
const ConfigKey kHotcueColorPaletteConfigKey(kColorPaletteConfigGroup, QStringLiteral("HotcueColorPalette"));
const ConfigKey kTrackColorPaletteConfigKey(kColorPaletteConfigGroup, QStringLiteral("TrackColorPalette"));

int numberOfDecimalDigits(int number) {
    int numDigits = 1;
    while (number /= 10) {
        numDigits++;
    }
    return numDigits;
}

ConfigKey keyForIndex(const QString& group, int index, int numDigits) {
    return ConfigKey(group, QString::number(index).rightJustified(numDigits, '0'));
}

} // anonymous namespace

ColorPalette ColorPaletteSettings::getColorPalette(
        const QString& name, const ColorPalette& defaultPalette) const {
    if (name.isEmpty()) {
        return defaultPalette;
    }

    // If we find a predefined palette with this name, return it
    for (const ColorPalette& palette : mixxx::PredefinedColorPalettes::kPalettes) {
        if (name == palette.getName()) {
            return palette;
        }
    }

    // Read colors from configuration
    const QString group = kColorPaletteGroupStart + name + kColorPaletteGroupEnd;
    QList<mixxx::RgbColor> colorList;
    QList<int> hotcueIndices;
    for (const ConfigKey& key : m_pConfig->getKeysWithGroup(group)) {
        if (key.item == kColorPaletteHotcueIndicesConfigItem) {
            for (const QString& stringIndex :
                    m_pConfig->getValueString(key).split(kColorPaletteHotcueIndicesConfigSeparator,
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
                            Qt::SkipEmptyParts)) {
#else
                            QString::SkipEmptyParts)) {
#endif
                bool ok;
                int index = stringIndex.toInt(&ok);
                if (ok && index >= 0) {
                    hotcueIndices << index;
                }
            }
        } else {
            mixxx::RgbColor color = mixxx::RgbColor(m_pConfig->getValue<mixxx::RgbColor>(key, kColorBlack));
            colorList.append(color);
        }
    }

    // If no palette is defined in the settings, we use the default one.
    if (colorList.isEmpty()) {
        return defaultPalette;
    }

    return ColorPalette(name, colorList, hotcueIndices);
}

void ColorPaletteSettings::setColorPalette(const QString& name, const ColorPalette& colorPalette) {
    VERIFY_OR_DEBUG_ASSERT(!name.isEmpty()) {
        qWarning() << "Palette name must not be empty!";
        return;
    }

    for (const ColorPalette& palette : mixxx::PredefinedColorPalettes::kPalettes) {
        if (name == palette.getName()) {
            qDebug() << "Color Palette" << name << "is a built-in palette, not writing to config!";
            return;
        }
    }
    removePalette(name);
    const QString group = kColorPaletteGroupStart + name + kColorPaletteGroupEnd;

    int numDigits = numberOfDecimalDigits(colorPalette.size() - 1);
    for (int index = 0; index < colorPalette.size(); ++index) {
        mixxx::RgbColor color = colorPalette.at(index);
        m_pConfig->setValue<mixxx::RgbColor>(keyForIndex(group, index, numDigits), color);
    }

    QStringList stringIndices;
    for (const unsigned int index : colorPalette.getIndicesByHotcue()) {
        stringIndices << QString::number(index);
    }
    if (!stringIndices.isEmpty()) {
        m_pConfig->setValue(
                ConfigKey(group, kColorPaletteHotcueIndicesConfigItem),
                stringIndices.join(kColorPaletteHotcueIndicesConfigSeparator));
    }
}

void ColorPaletteSettings::removePalette(const QString& name) {
    const QString group = kColorPaletteGroupStart + name + kColorPaletteGroupEnd;
    for (const ConfigKey& key : m_pConfig->getKeysWithGroup(group)) {
        m_pConfig->remove(key);
    }
}

ColorPalette ColorPaletteSettings::getHotcueColorPalette() const {
    QString name = m_pConfig->getValueString(kHotcueColorPaletteConfigKey);
    return getHotcueColorPalette(name);
}

ColorPalette ColorPaletteSettings::getHotcueColorPalette(
        const QString& name) const {
    return getColorPalette(
            name,
            mixxx::PredefinedColorPalettes::kDefaultHotcueColorPalette);
}

void ColorPaletteSettings::setHotcueColorPalette(const ColorPalette& colorPalette) {
    QString name = colorPalette.getName();
    VERIFY_OR_DEBUG_ASSERT(!name.isEmpty()) {
        qWarning() << "Palette name must not be empty!";
        return;
    }
    m_pConfig->setValue(kHotcueColorPaletteConfigKey, name);
    setColorPalette(name, colorPalette);
}

ColorPalette ColorPaletteSettings::getTrackColorPalette(
        const QString& name) const {
    return getColorPalette(
            name,
            mixxx::PredefinedColorPalettes::kDefaultTrackColorPalette);
}

ColorPalette ColorPaletteSettings::getTrackColorPalette() const {
    QString name = m_pConfig->getValueString(kTrackColorPaletteConfigKey);
    return getTrackColorPalette(name);
}

void ColorPaletteSettings::setTrackColorPalette(const ColorPalette& colorPalette) {
    QString name = colorPalette.getName();
    VERIFY_OR_DEBUG_ASSERT(!name.isEmpty()) {
        qWarning() << "Palette name must not be empty!";
        return;
    }
    m_pConfig->setValue(kTrackColorPaletteConfigKey, name);
    setColorPalette(name, colorPalette);
}

QSet<QString> ColorPaletteSettings::getColorPaletteNames() const {
    QSet<QString> names;
    for (const QString& group : m_pConfig->getGroups()) {
        int pos = kColorPaletteGroupNameRegex.indexIn(group);
        if (pos > -1) {
            names.insert(kColorPaletteGroupNameRegex.cap(1));
        }
    }
    return names;
}