drumstick  2.0.0
pianopalette.cpp
Go to the documentation of this file.
1 /*
2  MIDI Virtual Piano Keyboard
3  Copyright (C) 2008-2020, 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 
22 #include <drumstick/pianopalette.h>
24 
30 namespace drumstick {
31 namespace widgets {
32 
37 const QString PianoPalette::QSTR_PALETTEPREFIX = QStringLiteral("Palette_");
38 
44  m_paletteId(id)
45 {
46  initialize();
47  resetColors();
49 }
50 
55 void
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  default:
79  return;
80  }
81  m_colors.reserve(maxcolors);
82  m_names.reserve(maxcolors);
83  for(int i=0; i<maxcolors; ++i) {
84  m_colors.insert(i, QColor());
85  m_names.insert(i, QString());
86  }
87 }
88 
93 void
95 {
96  switch(m_paletteId) {
97  case PAL_SINGLE:
99  break;
100  case PAL_DOUBLE:
102  break;
103  case PAL_CHANNELS:
105  break;
106  case PAL_SCALE:
108  break;
109  case PAL_KEYS:
111  break;
112  case PAL_FONT:
114  break;
115  default:
116  return;
117  }
118 }
119 
124 void
126 {
127  setColor(0, QString(), qApp->palette().highlight().color());
128 }
129 
134 void
136 {
137  setColor(0, tr("N"), qApp->palette().highlight().color());
138  setColor(1, tr("#"), QColor("lawngreen"));
139 }
140 
145 void
147 {
148  setColor(0, tr("1"), QColor("red"));
149  setColor(1, tr("2"), QColor("lime"));
150  setColor(2, tr("3"), QColor("blue"));
151  setColor(3, tr("4"), QColor("gold"));
152  setColor(4, tr("5"), QColor("maroon"));
153  setColor(5, tr("6"), QColor("green"));
154  setColor(6, tr("7"), QColor("navy"));
155  setColor(7, tr("8"), QColor("darkorange"));
156  setColor(8, tr("9"), QColor("purple"));
157  setColor(9, tr("10"), qApp->palette().highlight().color());
158  setColor(10, tr("11"), QColor("teal"));
159  setColor(11, tr("12"), QColor("chocolate"));
160  setColor(12, tr("13"), QColor("fuchsia"));
161  setColor(13, tr("14"), QColor("olivedrab"));
162  setColor(14, tr("15"), QColor("aqua"));
163  setColor(15, tr("16"), QColor("greenyellow"));
164 }
165 
170 void
172 {
173  /*
174  R G B
175  C - - 100% 0
176  C# 50% - 100% 1
177  D 100% - 100% 2
178  D# 100% - 50% 3
179  E 100% - - 4
180  F 100% 50% - 5
181  F# 100% 100% - 6
182  G 50% 100% - 7
183  G# - 100% - 8
184  A - 100% 50% 9
185  A# - 100% 100% 10
186  B - 50% 100% 11
187  */
188  setColor(0, tr("C"), QColor::fromRgb(0,0,255));
189  setColor(1, tr("C#"), QColor::fromRgb(127,0,255));
190  setColor(2, tr("D"), QColor::fromRgb(255,0,255));
191  setColor(3, tr("D#"), QColor::fromRgb(255,0,127));
192  setColor(4, tr("E"), QColor::fromRgb(255,0,0));
193  setColor(5, tr("F"), QColor::fromRgb(255,127,0));
194  setColor(6, tr("F#"), QColor::fromRgb(255,255,0));
195  setColor(7, tr("G"), QColor::fromRgb(127,255,0));
196  setColor(8, tr("G#"), QColor::fromRgb(0,255,0));
197  setColor(9, tr("A"), QColor::fromRgb(0,255,127));
198  setColor(10, tr("A#"), QColor::fromRgb(0,255,255));
199  setColor(11, tr("B"), QColor::fromRgb(0,127,255));
200 }
201 
206 void
208 {
209  setColor(0, tr("N"), QColor("white"));
210  setColor(1, tr("#"), QColor("black"));
211 }
212 
217 void
219 {
220  setColor(0, tr("N"), QColor("black"));
221  setColor(1, tr("#"), QColor("white"));
222  setColor(2, tr("N*"), QColor("white"));
223  setColor(3, tr("#*"), QColor("white"));
224 }
225 
230 void
232 {
233  switch(m_paletteId) {
234  case PAL_SINGLE:
235  setPaletteName(tr("Single color highlight"));
236  setPaletteText(tr("A single color to highlight all note events"));
238  break;
239  case PAL_DOUBLE:
240  setPaletteName(tr("Two colors highlight"));
241  setPaletteText(tr("One color to highlight natural notes and a different one for accidentals"));
243  break;
244  case PAL_CHANNELS:
245  setPaletteName(tr("MIDI Channels highlight"));
246  setPaletteText(tr("A different color to highlight each MIDI channel. Enable Omni mode in the MIDI IN connection"));
248  break;
249  case PAL_SCALE:
250  setPaletteName(tr("Chromatic scale background"));
251  setPaletteText(tr("One color for each note in the chromatic scale"));
253  break;
254  case PAL_KEYS:
255  setPaletteName(tr("Keys background"));
256  setPaletteText(tr("One color for natural notes and another for accidentals"));
258  break;
259  case PAL_FONT:
260  setPaletteName(tr("Font foreground"));
261  setPaletteText(tr("Colors for note names"));
263  break;
264  default:
265  return;
266  }
267 }
268 
273 void
275 {
276  setColorName(0, QString());
277 }
278 
283 void
285 {
286  setColorName(0, tr("N"));
287  setColorName(1, tr("#"));
288 }
289 
294 void
296 {
297  setColorName(0, tr("1"));
298  setColorName(1, tr("2"));
299  setColorName(2, tr("3"));
300  setColorName(3, tr("4"));
301  setColorName(4, tr("5"));
302  setColorName(5, tr("6"));
303  setColorName(6, tr("7"));
304  setColorName(7, tr("8"));
305  setColorName(8, tr("9"));
306  setColorName(9, tr("10"));
307  setColorName(10, tr("11"));
308  setColorName(11, tr("12"));
309  setColorName(12, tr("13"));
310  setColorName(13, tr("14"));
311  setColorName(14, tr("15"));
312  setColorName(15, tr("16"));
313 }
314 
319 void
321 {
322  setColorName(0, tr("C"));
323  setColorName(1, tr("C#"));
324  setColorName(2, tr("D"));
325  setColorName(3, tr("D#"));
326  setColorName(4, tr("E"));
327  setColorName(5, tr("F"));
328  setColorName(6, tr("F#"));
329  setColorName(7, tr("G"));
330  setColorName(8, tr("G#"));
331  setColorName(9, tr("A"));
332  setColorName(10, tr("A#"));
333  setColorName(11, tr("B"));
334 }
335 
340 void
342 {
343  setColorName(0, tr("N"));
344  setColorName(1, tr("#"));
345 }
346 
351 void
353 {
354  setColorName(0, tr("N"));
355  setColorName(1, tr("#"));
356  setColorName(2, tr("N*"));
357  setColorName(3, tr("#*"));
358 }
359 
364 bool
366 {
367  return (m_paletteId == PAL_SINGLE) || (m_paletteId == PAL_DOUBLE) || (m_paletteId == PAL_CHANNELS);
368 }
369 
374 bool
376 {
377  return (m_paletteId == PAL_SCALE) || (m_paletteId == PAL_KEYS);
378 }
379 
384 bool
386 {
387  return (m_paletteId == PAL_FONT);
388 }
389 
394 int
396 {
397  return m_paletteId;
398 }
399 
406 void
407 PianoPalette::setColor(const int n, const QString& s, const QColor& c)
408 {
409  if (n < m_colors.size()) {
410  m_colors[n] = c;
411  m_names[n] = s;
412  }
413 }
414 
420 void
421 PianoPalette::setColor(const int n, const QColor& c)
422 {
423  if (n < m_colors.size())
424  m_colors[n] = c;
425 }
426 
432 void
433 PianoPalette::setColorName(const int n, const QString& s)
434 {
435  if (n < m_names.size())
436  m_names[n] = s;
437 }
438 
444 QColor
445 PianoPalette::getColor(const int i) const
446 {
447  if (i < m_colors.size())
448  return m_colors[i];
449  return {};
450 }
451 
457 QString
458 PianoPalette::getColorName(const int i) const
459 {
460  if (i < m_names.size())
461  return m_names[i];
462  return QString();
463 }
464 
469 int
471 {
472  return m_colors.size();
473 }
474 
479 QString
481 {
482  return m_paletteName;
483 }
484 
489 void
490 PianoPalette::setPaletteName(const QString& name)
491 {
492  if (m_paletteName != name) {
493  m_paletteName = name;
494  }
495 }
496 
501 QString
503 {
504  return m_paletteText;
505 }
506 
511 void
512 PianoPalette::setPaletteText(const QString& help)
513 {
514  m_paletteText = help;
515 }
516 
520 void
522 {
523  SettingsFactory settings;
524  settings->beginWriteArray(QSTR_PALETTEPREFIX + QString::number(m_paletteId));
525  for(int i=0; i<m_colors.size(); ++i) {
526  settings->setArrayIndex(i);
527  settings->setValue("color", m_colors[i]);
528  }
529  settings->endArray();
530  settings->sync();
531 }
532 
536 void
538 {
539  SettingsFactory settings;
540  int size = settings->beginReadArray(QSTR_PALETTEPREFIX + QString::number(m_paletteId));
541  if (size > m_colors.size())
542  size = m_colors.size();
543  for(int i=0; i<size; ++i) {
544  settings->setArrayIndex(i);
545  QColor c = settings->value("color", QColor()).value<QColor>();
546  setColor(i, c);
547  }
548  settings->endArray();
549 }
550 
556 bool
558 {
559  return (m_paletteId == other.m_paletteId) &&
560  (m_colors == other.m_colors);
561 }
562 
568 bool
570 {
571  return !(*this == other);
572 }
573 
574 } // namespace widgets
575 } // namespace drumstick
The PianoPalette class.
Definition: pianopalette.h:60
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:97
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:49
@ PAL_SINGLE
Single highlihgting color for all keys.
Definition: pianopalette.h:46
@ PAL_CHANNELS
Different highlihgting colors for each channel.
Definition: pianopalette.h:48
@ PAL_KEYS
Two background colors (naturals/alterations)
Definition: pianopalette.h:50
@ PAL_DOUBLE
Two highlihgting colors (naturals/alterations)
Definition: pianopalette.h:47
@ PAL_FONT
Foreground font colors for names.
Definition: pianopalette.h:51
Drumstick common.
Definition: alsaclient.cpp:68
Piano Palette declarations.
SettingsFactory class declaration.