Debian Codegraph @

src/PaletteDialog.cpp

raw symbols SCIP
1 #include <QSignalMapper>
2 #include <QPushButton>
3 #include <QGridLayout>
4 #include <QPainter>
5 #include "PaletteDialog.h"
6 #include "Convert.h"
7 #include "VDPDataStore.h"
8 #include "ranges.h"
9
10
11 PalettePatch::PalettePatch(QWidget* parent, int palNr)
12 : QPushButton(parent), msxPalNr(palNr)
13 {
14 }
15
16 //void PalettePatch::setColor(QRgb c)
17 //{
18 // myColor = c;
19 // update();
20 //}
21
22 void PalettePatch::updatePaletteChanged(const uint8_t* pal)
23 {
24 int r = (pal[2 * msxPalNr + 0] & 0xf0) >> 4;
25 int b = (pal[2 * msxPalNr + 0] & 0x0f);
26 int g = (pal[2 * msxPalNr + 1] & 0x0f);
27 auto scale = [](int x) { return (x >> 1) | (x << 2) | (x << 5); };
28 myColor = qRgb(scale(r), scale(g), scale(b));
29 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
30 update();
31 //printf("PalettePatch::updatePaletteChanged %i\n", msxPalNr);
32 }
33
34 void PalettePatch::setHighlightTest(int colorNr)
35 {
36 bool s = colorNr == msxPalNr;
37 if (isSelected == s) return;
38 isSelected = s;
39 update();
40 }
41
42 void PalettePatch::paintEvent(QPaintEvent* /*event*/)
43 {
44 QPainter painter(this);
45 painter.setPen(isSelected ? Qt::white : QColor(myColor));
46 painter.setBrush(QBrush(myColor));
47 painter.drawRect(0, 0, this->width() - 1, this->height() - 1);
48 }
49
50 PaletteDialog::PaletteDialog(QWidget* parent)
51 : QDialog(parent), ui(std::make_unique<Ui::PaletteDialog>())
52 {
53 ui->setupUi(this);
54 connect(ui->horizontalSlider_R, &QSlider::valueChanged, this, &PaletteDialog::on_horizontalSlider_R_valueChanged);
55 connect(ui->horizontalSlider_G, &QSlider::valueChanged, this, &PaletteDialog::on_horizontalSlider_G_valueChanged);
56 connect(ui->horizontalSlider_B, &QSlider::valueChanged, this, &PaletteDialog::on_horizontalSlider_B_valueChanged);
57 connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &PaletteDialog::on_buttonBox_clicked);
58 connect(ui->cb_autosync, &QCheckBox::stateChanged, this, &PaletteDialog::on_cb_autosync_stateChanged);
59
60 const QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
61 ui->plainTextEdit->setFont(fixedFont);
62
63 setWindowTitle("Palette editor");
64 ranges::fill(myPal, 0);
65 ranges::fill(myOriginalPal, 0);
66 // add colors to colorframe
67
68 signalMapper = new QSignalMapper(this);
69 auto* gridLayout = new QGridLayout;
70 gridLayout->setSpacing(0);
71 for (int i = 0; i < 16; ++i) {
72 auto* button = new PalettePatch(nullptr, i);
73 connect(button, &PalettePatch::clicked, signalMapper, qOverload<>(&QSignalMapper::map));
74 signalMapper->setMapping(button, i);
75 gridLayout->addWidget(button, i / 8, i % 8);
76 connect(this, &PaletteDialog::paletteChanged,
77 button, &PalettePatch::updatePaletteChanged);
78 connect(signalMapper, &QSignalMapper::mappedInt,
79 button, &PalettePatch::setHighlightTest);
80 }
81
82 connect(signalMapper, &QSignalMapper::mappedInt,
83 this, &PaletteDialog::colorSelected);
84
85 ui->colorsframe->setLayout(gridLayout);
86 connect(this, &PaletteDialog::paletteChanged,
87 this, &PaletteDialog::updateText);
88 }
89
90 void PaletteDialog::updateText()
91 {
92 ui->plainTextEdit->clear();
93 for (int i = 0; i < 4; ++i) {
94 QString txt(" db ");
95 for (int j = 0; j < 4; ++j) {
96 txt.append(QString("%1,%2 ")
97 .arg(hexValue(myPal[2 * (j + 4 * i) + 0], 2))
98 .arg(hexValue(myPal[2 * (j + 4 * i) + 1], 2)));
99 if (j < 3) txt.append(',');
100 }
101 ui->plainTextEdit->appendPlainText(txt);
102 }
103 }
104
105 void PaletteDialog::restoreOpeningsPalette()
106 {
107 memcpy(myPal, myOriginalPal, sizeof(myPal));
108 emit paletteChanged(myPal); // Resets the PalettePatches
109 colorSelected(currentColor); // Resets the sliders
110 if (autoSync) {
111 syncToSource();
112 }
113 }
114
115 void PaletteDialog::colorSelected(int colorNumber)
116 {
117 currentColor = colorNumber;
118 int r = (myPal[2 * currentColor + 0] & 0xf0) >> 4;
119 int b = (myPal[2 * currentColor + 0] & 0x0f);
120 int g = (myPal[2 * currentColor + 1] & 0x0f);
121 ui->horizontalSlider_R->setValue(r);
122 ui->horizontalSlider_G->setValue(g);
123 ui->horizontalSlider_B->setValue(b);
124 ui->label_colornr->setText(QString("Color %1").arg(colorNumber));
125 }
126
127 void PaletteDialog::setPalette(uint8_t* pal)
128 {
129 sourcePal = pal;
130 memcpy(myOriginalPal, pal, sizeof(myOriginalPal));
131 memcpy(myPal, pal, sizeof(myPal));
132 emit paletteChanged(myPal);
133 }
134
135 uint8_t* PaletteDialog::getPalette()
136 {
137 return myPal;
138 }
139
140 //void PaletteDialog::decodepalette()
141 //{
142 // for (int i = 0; i < 16; ++i) {
143 // int r = (myPal[2 * i + 0] & 0xf0) >> 4;
144 // int b = (myPal[2 * i + 0] & 0x0f);
145 // int g = (myPal[2 * i + 1] & 0x0f);
146 //
147 // auto scale = [](int x) { return (x >> 1) | (x << 2) | (x << 5); };
148 // msxPalette[i] = qRgb(scale(r), scale(g), scale(b));
149 //
150 // QGridLayout* l = dynamic_cast<QGridLayout*>(ui->colorsframe->layout());
151 // dynamic_cast<QPushButton*>(l->itemAtPosition(i % 8, i / 8)->widget())->setText(QString("%1(%2%3%4)").arg(i).arg(r).arg(g).arg(b));
152 // }
153 //}
154
155 void PaletteDialog::combineRGB()
156 {
157 int r = ui->horizontalSlider_R->value();
158 int g = ui->horizontalSlider_G->value();
159 int b = ui->horizontalSlider_B->value();
160 myPal[2 * currentColor + 0] = 16 * r + b;
161 myPal[2 * currentColor + 1] = g;
162 emit paletteChanged(myPal);
163 if (autoSync) {
164 syncToSource();
165 }
166 }
167
168 void PaletteDialog::syncToSource()
169 {
170 memcpy(sourcePal, myPal, sizeof(myPal));
171 emit paletteSynced();
172 }
173
174 void PaletteDialog::restoreDefaultPalette()
175 {
176 memcpy(myPal, VDPDataStore::instance().getDefaultPalettePointer(), 32);
177 emit paletteChanged(myPal);
178 if (autoSync) {
179 syncToSource();
180 }
181 }
182
183 void PaletteDialog::setAutoSync(bool value)
184 {
185 if (autoSync == value) return;
186 autoSync = value;
187 ui->cb_autosync->setChecked(value);
188 }
189
190 void PaletteDialog::on_horizontalSlider_R_valueChanged(int value)
191 {
192 ui->label_R->setText(QString("R=%1").arg(value));
193 combineRGB();
194 }
195
196 void PaletteDialog::on_horizontalSlider_G_valueChanged(int value)
197 {
198 ui->label_G->setText(QString("G=%1").arg(value));
199 combineRGB();
200 }
201
202 void PaletteDialog::on_horizontalSlider_B_valueChanged(int value)
203 {
204 ui->label_B->setText(QString("B=%1").arg(value));
205 combineRGB();
206 }
207
208 void PaletteDialog::on_buttonBox_clicked(QAbstractButton* button)
209 {
210 if (button== ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)) {
211 restoreDefaultPalette();
212 } else if (button== ui->buttonBox->button(QDialogButtonBox::Ok)) {
213 syncToSource();
214 } else if (button== ui->buttonBox->button(QDialogButtonBox::Reset) ||
215 button== ui->buttonBox->button(QDialogButtonBox::Cancel)) {
216 restoreOpeningsPalette();
217 }
218 }
219
220 void PaletteDialog::on_cb_autosync_stateChanged(int arg1)
221 {
222 autoSync = arg1 != Qt::Unchecked;
223 if (autoSync) {
224 syncToSource();
225 }
226 }