drumstick 2.9.0
C++ MIDI libraries using Qt objects, idioms, and style.
pianopalette.cpp
Go to the documentation of this file.
1/*
2 MIDI Virtual Piano Keyboard
3 Copyright (C) 2008-2023, Pedro Lopez-Cabanillas <plcl@users.sf.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <QGuiApplication>
20#include <QPalette>
21
24
30namespace drumstick {
31namespace widgets {
32
37const QString PianoPalette::QSTR_PALETTEPREFIX = QStringLiteral("Palette_");
38
44 m_paletteId(id)
45{
46 initialize();
49}
50
55void
57{
58 int maxcolors = 0;
59 switch(m_paletteId) {
60 case PAL_SINGLE:
61 maxcolors = 1;
62 break;
63 case PAL_DOUBLE:
64 maxcolors = 2;
65 break;
66 case PAL_CHANNELS:
67 maxcolors = 16;
68 break;
69 case PAL_SCALE:
70 maxcolors = 12;
71 break;
72 case PAL_KEYS:
73 maxcolors = 2;
74 break;
75 case PAL_FONT:
76 maxcolors = 4;
77 break;
78 case PAL_HISCALE:
79 maxcolors = 12;
80 break;
81 default:
82 return;
83 }
84 m_colors.reserve(maxcolors);
85 m_names.reserve(maxcolors);
86 for(int i=0; i<maxcolors; ++i) {
87 m_colors.insert(i, QColor());
88 m_names.insert(i, QString());
89 }
90}
91
96void
98{
99 switch(m_paletteId) {
100 case PAL_SINGLE:
102 break;
103 case PAL_DOUBLE:
105 break;
106 case PAL_CHANNELS:
108 break;
109 case PAL_SCALE:
111 break;
112 case PAL_KEYS:
114 break;
115 case PAL_FONT:
117 break;
118 case PAL_HISCALE:
120 break;
121 default:
122 return;
123 }
124}
125
130void
132{
133 setColor(0, QString(), qApp->palette().highlight().color());
134}
135
140void
142{
143 setColor(0, tr("N"), qApp->palette().highlight().color());
144 setColor(1, tr("#"), QColor("lawngreen"));
145}
146
151void
153{
154 setColor(0, tr("1"), QColor("red"));
155 setColor(1, tr("2"), QColor("lime"));
156 setColor(2, tr("3"), QColor("blue"));
157 setColor(3, tr("4"), QColor("gold"));
158 setColor(4, tr("5"), QColor("maroon"));
159 setColor(5, tr("6"), QColor("green"));
160 setColor(6, tr("7"), QColor("navy"));
161 setColor(7, tr("8"), QColor("darkorange"));
162 setColor(8, tr("9"), QColor("purple"));
163 setColor(9, tr("10"), qApp->palette().highlight().color());
164 setColor(10, tr("11"), QColor("teal"));
165 setColor(11, tr("12"), QColor("chocolate"));
166 setColor(12, tr("13"), QColor("fuchsia"));
167 setColor(13, tr("14"), QColor("olivedrab"));
168 setColor(14, tr("15"), QColor("aqua"));
169 setColor(15, tr("16"), QColor("greenyellow"));
170}
171
176void
178{
179 /*
180 R G B
181 C 100% - - 0
182 C# 100% 50% - 1
183 D 100% 100% - 2
184 D# 50% 100% - 3
185 E - 100% - 4
186 F - 100% 50% 5
187 F# - 100% 100% 6
188 G - 50% 100% 7
189 G# - - 100% 8
190 A 50% - 100% 9
191 A# 100% - 100% 10
192 B 100% - 50% 11
193 */
194 setColor(0, tr("C"), QColor::fromRgb(255,0,0));
195 setColor(1, tr("C#"), QColor::fromRgb(255,127,0));
196 setColor(2, tr("D"), QColor::fromRgb(255,255,0));
197 setColor(3, tr("D#"), QColor::fromRgb(127,255,0));
198 setColor(4, tr("E"), QColor::fromRgb(0,255,0));
199 setColor(5, tr("F"), QColor::fromRgb(0,255,127));
200 setColor(6, tr("F#"), QColor::fromRgb(0,255,255));
201 setColor(7, tr("G"), QColor::fromRgb(0,127,255));
202 setColor(8, tr("G#"), QColor::fromRgb(0,0,255));
203 setColor(9, tr("A"), QColor::fromRgb(127,0,255));
204 setColor(10, tr("A#"), QColor::fromRgb(255,0,255));
205 setColor(11, tr("B"), QColor::fromRgb(255,0,127));
206}
207
212void
214{
215 setColor(0, tr("N"), QColor("white"));
216 setColor(1, tr("#"), QColor("black"));
217}
218
223void
225{
226 setColor(0, tr("N"), QColor("black"));
227 setColor(1, tr("#"), QColor("white"));
228 setColor(2, tr("N*"), QColor("white"));
229 setColor(3, tr("#*"), QColor("white"));
230}
231
236void
238{
239 switch(m_paletteId) {
240 case PAL_SINGLE:
241 setPaletteName(tr("Single color highlight"));
242 setPaletteText(tr("A single color to highlight all note events"));
244 break;
245 case PAL_DOUBLE:
246 setPaletteName(tr("Two colors highlight"));
247 setPaletteText(tr("One color to highlight natural notes and a different one for accidentals"));
249 break;
250 case PAL_CHANNELS:
251 setPaletteName(tr("MIDI Channels highlight"));
252 setPaletteText(tr("A different color to highlight each MIDI channel. Enable Omni mode in the MIDI IN connection"));
254 break;
255 case PAL_SCALE:
256 setPaletteName(tr("Chromatic scale background"));
257 setPaletteText(tr("One color for each note in the chromatic scale"));
259 break;
260 case PAL_KEYS:
261 setPaletteName(tr("Keys background"));
262 setPaletteText(tr("One color for natural notes and another for accidentals"));
264 break;
265 case PAL_FONT:
266 setPaletteName(tr("Font foreground"));
267 setPaletteText(tr("Colors for note names"));
269 break;
270 case PAL_HISCALE:
271 setPaletteName(tr("Chromatic scale highlight"));
272 setPaletteText(tr("One color for each note in the chromatic scale"));
274 break;
275 default:
276 return;
277 }
278}
279
284void
286{
287 setColorName(0, QString());
288}
289
294void
296{
297 setColorName(0, tr("N"));
298 setColorName(1, tr("#"));
299}
300
305void
307{
308 setColorName(0, tr("1"));
309 setColorName(1, tr("2"));
310 setColorName(2, tr("3"));
311 setColorName(3, tr("4"));
312 setColorName(4, tr("5"));
313 setColorName(5, tr("6"));
314 setColorName(6, tr("7"));
315 setColorName(7, tr("8"));
316 setColorName(8, tr("9"));
317 setColorName(9, tr("10"));
318 setColorName(10, tr("11"));
319 setColorName(11, tr("12"));
320 setColorName(12, tr("13"));
321 setColorName(13, tr("14"));
322 setColorName(14, tr("15"));
323 setColorName(15, tr("16"));
324}
325
330void
332{
333 setColorName(0, tr("C"));
334 setColorName(1, tr("C#"));
335 setColorName(2, tr("D"));
336 setColorName(3, tr("D#"));
337 setColorName(4, tr("E"));
338 setColorName(5, tr("F"));
339 setColorName(6, tr("F#"));
340 setColorName(7, tr("G"));
341 setColorName(8, tr("G#"));
342 setColorName(9, tr("A"));
343 setColorName(10, tr("A#"));
344 setColorName(11, tr("B"));
345}
346
351void
353{
354 setColorName(0, tr("N"));
355 setColorName(1, tr("#"));
356}
357
362void
364{
365 setColorName(0, tr("N"));
366 setColorName(1, tr("#"));
367 setColorName(2, tr("N*"));
368 setColorName(3, tr("#*"));
369}
370
375bool
377{
378 return (m_paletteId == PAL_SINGLE) ||
379 (m_paletteId == PAL_DOUBLE) ||
380 (m_paletteId == PAL_CHANNELS) ||
381 (m_paletteId == PAL_HISCALE);
382}
383
388bool
390{
391 return (m_paletteId == PAL_SCALE) ||
392 (m_paletteId == PAL_KEYS);
393}
394
399bool
401{
402 return (m_paletteId == PAL_FONT);
403}
404
409int
411{
412 return m_paletteId;
413}
414
421void
422PianoPalette::setColor(const int n, const QString& s, const QColor& c)
423{
424 if (n < m_colors.size()) {
425 m_colors[n] = c;
426 m_names[n] = s;
427 }
428}
429
435void
436PianoPalette::setColor(const int n, const QColor& c)
437{
438 if (n < m_colors.size())
439 m_colors[n] = c;
440}
441
447void
448PianoPalette::setColorName(const int n, const QString& s)
449{
450 if (n < m_names.size())
451 m_names[n] = s;
452}
453
459QColor
460PianoPalette::getColor(const int i) const
461{
462 if (i < m_colors.size())
463 return m_colors[i];
464 return {};
465}
466
472QString
474{
475 if (i < m_names.size())
476 return m_names[i];
477 return QString();
478}
479
484int
486{
487 return m_colors.size();
488}
489
494QString
496{
497 return m_paletteName;
498}
499
504void
506{
507 if (m_paletteName != name) {
508 m_paletteName = name;
509 }
510}
511
516QString
518{
519 return m_paletteText;
520}
521
526void
528{
529 m_paletteText = help;
530}
531
535void
537{
538 SettingsFactory settings;
539 settings->beginWriteArray(QSTR_PALETTEPREFIX + QString::number(m_paletteId));
540 for(int i=0; i<m_colors.size(); ++i) {
541 settings->setArrayIndex(i);
542 settings->setValue("color", m_colors[i]);
543 }
544 settings->endArray();
545 settings->sync();
546}
547
551void
553{
554 SettingsFactory settings;
555 int size = settings->beginReadArray(QSTR_PALETTEPREFIX + QString::number(m_paletteId));
556 if (size > m_colors.size())
557 size = m_colors.size();
558 for(int i=0; i<size; ++i) {
559 settings->setArrayIndex(i);
560 QColor c = settings->value("color", QColor()).value<QColor>();
561 setColor(i, c);
562 }
563 settings->endArray();
564}
565
571bool
573{
574 return (m_paletteId == other.m_paletteId) &&
575 (m_colors == other.m_colors);
576}
577
583bool
585{
586 return !(*this == other);
587}
588
595QDataStream &operator<<(QDataStream &stream, const PianoPalette &palette)
596{
597 stream << palette.m_paletteId;
598 stream << palette.m_colors;
599 stream << palette.m_names;
600 stream << palette.m_paletteName;
601 stream << palette.m_paletteText;
602 return stream;
603}
604
611QDataStream &operator>>(QDataStream &stream, PianoPalette &palette)
612{
613 stream >> palette.m_paletteId;
614 stream >> palette.m_colors;
615 stream >> palette.m_names;
616 stream >> palette.m_paletteName;
617 stream >> palette.m_paletteText;
618 return stream;
619}
620
621} // namespace widgets
622} // namespace drumstick
The PianoPalette class.
Definition: pianopalette.h:71
void resetPaletteChannels()
PianoPalette::resetPaletteChannels resets the colors to the standard values for the PAL_CHANNELS pale...
void resetPaletteKeys()
PianoPalette::resetPaletteKeys resets the colors to the standard values for the PAL_KEYS palette poli...
PianoPalette(const int id)
PianoPalette::PianoPalette Constructor.
void initialize()
PianoPalette::initialize reserves and initializes space to store colors according to the palette poli...
int getNumColors() const
PianoPalette::getNumColors palette policy colors size.
bool isBackground() const
PianoPalette::isBackground palette function.
void retranslatePaletteSingle()
PianoPalette::retranslatePaletteSingle retranslates the color names for the PAL_SINGLE palette policy...
QString paletteText() const
PianoPalette::paletteText gets the palette description.
void retranslatePaletteDouble()
PianoPalette::retranslatePaletteDouble retranslates the color names for the PAL_DOUBLE palette policy...
QString getColorName(const int i) const
PianoPalette::getColorName gets a palette color name.
void retranslatePaletteFont()
PianoPalette::retranslatePaletteFont retranslates the color names for the PAL_FONT palette policy.
void retranslateStrings()
PianoPalette::retranslateStrings retranslates the names and description texts according to the palett...
void retranslatePaletteScale()
PianoPalette::retranslatePaletteScale retranslates the color names for the PAL_SCALE palette policy.
static const QString QSTR_PALETTEPREFIX
PianoPalette::QSTR_PALETTEPREFIX is the string prefix for all the settings stored as persisting setti...
Definition: pianopalette.h:108
bool operator!=(const PianoPalette &other) const
PianoPalette::operator != compares two palettes.
void resetPaletteSingle()
PianoPalette::resetPaletteSingle resets the colors to the standard values for the PAL_SINGLE palette ...
bool isForeground() const
PianoPalette::isForeground palette function.
QString paletteName() const
PianoPalette::paletteName palette policy name.
bool isHighLight() const
PianoPalette::isHighLight palette function.
int paletteId() const
PianoPalette::paletteId palette policy.
void saveColors() const
PianoPalette::saveColors stores the set of colors as persistent settings.
void setColor(const int n, const QString &s, const QColor &c)
PianoPalette::setColor changes a palette color.
void resetColors()
PianoPalette::resetColors resets the colors to the standard values according to the palette policy id...
void setPaletteName(const QString &name)
PianoPalette::setPaletteName changes the palette name.
void resetPaletteDouble()
PianoPalette::resetPaletteDouble resets the colors to the standard values for the PAL_DOUBLE palette ...
void loadColors()
PianoPalette::loadColors loads the set of colors from persistent settings.
void retranslatePaletteChannels()
PianoPalette::retranslatePaletteChannels retranslates the color names for the PAL_CHANNELS palette po...
void resetPaletteScale()
PianoPalette::resetPaletteScale resets the colors to the standard values for the PAL_SCALE palette po...
void retranslatePaletteKeys()
PianoPalette::retranslatePaletteKeys retranslates the color names for the PAL_KEYS palette policy.
QColor getColor(const int i) const
PianoPalette::getColor gets a palette color.
void setPaletteText(const QString &text)
PianoPalette::setPaletteText changes the palette description.
void setColorName(const int n, const QString &s)
PianoPalette::setColorName changes a palette color name.
void resetPaletteFont()
PianoPalette::resetPaletteFont resets the colors to the standard values for the PAL_FONT palette poli...
bool operator==(const PianoPalette &other) const
PianoPalette::operator == compares two palettes.
The SettingsFactory class holds a global QSettings object.
@ PAL_SCALE
Background colors for each chromatic scale note.
Definition: pianopalette.h:59
@ PAL_SINGLE
Single highlihgting color for all keys.
Definition: pianopalette.h:56
@ PAL_HISCALE
Highlighting colors for each chromatic scale note.
Definition: pianopalette.h:62
@ PAL_CHANNELS
Different highlihgting colors for each channel.
Definition: pianopalette.h:58
@ PAL_KEYS
Two background colors (naturals/alterations)
Definition: pianopalette.h:60
@ PAL_DOUBLE
Two highlihgting colors (naturals/alterations)
Definition: pianopalette.h:57
@ PAL_FONT
Foreground font colors for names.
Definition: pianopalette.h:61
QDataStream & operator>>(QDataStream &stream, PianoPalette &palette)
Deserialize a PianoPalette instance from a QDataStream.
QDataStream & operator<<(QDataStream &stream, const PianoPalette &palette)
Serialize a PianoPalette instance into a QDataStream.
Drumstick common.
Definition: alsaclient.cpp:68
Piano Palette declarations.
SettingsFactory class declaration.