1 // Copyright (c) 2016 The SigViewer Development Team
2 // Licensed under the GNU General Public License (GPL)
3 // https://www.gnu.org/licenses/gpl
4
5
6 #include "file_signal_reader.h"
7 #include <QSettings>
8
9
10 namespace sigviewer {
11
setEventTypeColors()12 int FileSignalReader::setEventTypeColors()
13 {
14 // Display each event type in a distinct color
15 QSharedPointer<ColorManager> colorPicker = ApplicationContextImpl::getInstance()->color_manager_;
16
17 //set event colors
18 srand (time(NULL)); /* initialize random seed: */
19
20 QVector<QColor> eventColorList = {"#0055ff", "#00aa00", "#aa00ff", "#ff0000", "#00557f",
21 "#5555ff", "#ff55ff", "#00aaff", "#00aa7f", "#ff5500"};
22
23 int colorChoice = 5; //Set the first event color to be pink
24
25 QSettings settings;
26 settings.beginGroup("ColorManager");
27 int size = settings.beginReadArray("event");
28
29 for (int type = 0; type < 254; type++)
30 {
31 QColor color;
32 if (type < size)
33 {
34 settings.setArrayIndex(type);
35 color = settings.value("color").toString();
36 color.setAlpha(settings.value("alpha").toInt());
37 }
38
39 /* if the user has specified color setting for the current event type previously
40 * in QSettings, we want to use it.
41 * If the color setting is #0000ff (default), we assume the user
42 * hasn't specified color for the current event type yet. Below is our algorithm
43 * to pick some good colors for event types. */
44
45 if (color.name().compare("#0000ff", Qt::CaseInsensitive) == 0)
46 {
47 // generate random number first:
48 int red = rand() % 41 + (-20);
49 int green = rand() % 41 + (-20);
50 int blue = rand() % 41 + (-20);
51
52 colorChoice++;
53 if (colorChoice == 10) //we only have 10 basic colors
54 colorChoice = 0;
55
56 color = eventColorList[colorChoice];
57
58 red += color.red();
59 green += color.green();
60 blue += color.blue();
61
62 if (red < 0)
63 red = 0;
64 if (red > 255)
65 red = 255;
66 if (green < 0)
67 green = 0;
68 if (green > 255)
69 green = 255;
70 if (blue < 0)
71 blue = 0;
72 if (blue > 255)
73 blue = 255;
74
75
76 color.setRgb(red, green, blue);
77 color.setAlpha(120);
78 }
79 colorPicker->setEventColor(type, color); //QColor(0, 85, 255, 80)
80 eventColorList[colorChoice] = color;
81 }
82
83 settings.endArray();
84 settings.endGroup();
85
86 colorPicker->saveSettings();
87
88 return 0;
89 }
90
91 }
92