From 4dc598455f58ce9addcfb9474f396f6955cac464 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Fri, 4 Oct 2024 13:36:50 -0400 Subject: [PATCH] Improve color slider edit history --- include/ui/colorinputwidget.h | 1 + include/ui/paletteeditor.h | 1 + src/ui/colorinputwidget.cpp | 15 +++++++++++++++ src/ui/paletteeditor.cpp | 10 +++++++--- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/include/ui/colorinputwidget.h b/include/ui/colorinputwidget.h index 5b414776..cd871e0b 100644 --- a/include/ui/colorinputwidget.h +++ b/include/ui/colorinputwidget.h @@ -25,6 +25,7 @@ public: signals: void colorChanged(QRgb color); void bitDepthChanged(int bits); + void editingFinished(); private: Ui::ColorInputWidget *ui; diff --git a/include/ui/paletteeditor.h b/include/ui/paletteeditor.h index 6aa9e131..63581eb1 100644 --- a/include/ui/paletteeditor.h +++ b/include/ui/paletteeditor.h @@ -39,6 +39,7 @@ private: Tileset* getTileset(int paletteId); void refreshColorInputs(); + void commitEditHistory(); void commitEditHistory(int paletteId); void restoreWindowState(); void closeEvent(QCloseEvent*); diff --git a/src/ui/colorinputwidget.cpp b/src/ui/colorinputwidget.cpp index bbd6f764..8b40be27 100644 --- a/src/ui/colorinputwidget.cpp +++ b/src/ui/colorinputwidget.cpp @@ -34,6 +34,7 @@ ColorInputWidget::ColorInputWidget(const QString &title, QWidget *parent) : void ColorInputWidget::init() { ui->setupUi(this); + // Connect color change signals connect(ui->slider_Red, &QSlider::valueChanged, this, &ColorInputWidget::setRgbFromSliders); connect(ui->slider_Green, &QSlider::valueChanged, this, &ColorInputWidget::setRgbFromSliders); connect(ui->slider_Blue, &QSlider::valueChanged, this, &ColorInputWidget::setRgbFromSliders); @@ -46,6 +47,19 @@ void ColorInputWidget::init() { ui->lineEdit_Hex->setValidator(&hexValidator); connect(ui->lineEdit_Hex, &QLineEdit::textEdited, this, &ColorInputWidget::setRgbFromHexString); + // We have separate signals for when color input editing finishes. + // This is mostly useful for external commit histories, esp. for the sliders which can rapidly emit color change signals. + connect(ui->slider_Red, &QSlider::sliderReleased, this, &ColorInputWidget::editingFinished); + connect(ui->slider_Green, &QSlider::sliderReleased, this, &ColorInputWidget::editingFinished); + connect(ui->slider_Blue, &QSlider::sliderReleased, this, &ColorInputWidget::editingFinished); + + connect(ui->spinBox_Red, &QSpinBox::editingFinished, this, &ColorInputWidget::editingFinished); + connect(ui->spinBox_Green, &QSpinBox::editingFinished, this, &ColorInputWidget::editingFinished); + connect(ui->spinBox_Blue, &QSpinBox::editingFinished, this, &ColorInputWidget::editingFinished); + + connect(ui->lineEdit_Hex, &QLineEdit::editingFinished, this, &ColorInputWidget::editingFinished); + + // Connect color picker connect(ui->button_Eyedrop, &QToolButton::clicked, this, &ColorInputWidget::pickColor); setBitDepth(24); @@ -210,5 +224,6 @@ void ColorInputWidget::pickColor() { if (picker.exec() == QDialog::Accepted) { QColor c = picker.getColor(); setColor(c.rgb()); + emit editingFinished(); } } diff --git a/src/ui/paletteeditor.cpp b/src/ui/paletteeditor.cpp index 456d1f99..393f3a6e 100644 --- a/src/ui/paletteeditor.cpp +++ b/src/ui/paletteeditor.cpp @@ -24,6 +24,7 @@ PaletteEditor::PaletteEditor(Project *project, Tileset *primaryTileset, Tileset for (int i = 0; i < this->numColors; i++) { auto colorInput = new ColorInputWidget(QString("Color %1").arg(i)); connect(colorInput, &ColorInputWidget::colorChanged, [this, i](QRgb color) { setRgb(i, color); }); + connect(colorInput, &ColorInputWidget::editingFinished, [this] { commitEditHistory(); }); this->colorInputs.append(colorInput); ui->layout_Colors->addWidget(colorInput, i / numColorsPerRow, i % numColorsPerRow); } @@ -46,7 +47,7 @@ PaletteEditor::PaletteEditor(Project *project, Tileset *primaryTileset, Tileset connect(this->ui->bit_depth_24, &QRadioButton::toggled, [this](bool checked){ if (checked) this->setBitDepth(24); }); this->setPaletteId(paletteId); - this->commitEditHistory(this->ui->spinBox_PaletteId->value()); + this->commitEditHistory(); this->restoreWindowState(); } @@ -75,8 +76,7 @@ void PaletteEditor::setRgb(int colorIndex, QRgb rgb) { Tileset *tileset = getTileset(paletteId); tileset->palettes[paletteId][colorIndex] = rgb; tileset->palettePreviews[paletteId][colorIndex] = rgb; - - commitEditHistory(paletteId); + emit changedPaletteColor(); } @@ -120,6 +120,10 @@ void PaletteEditor::on_spinBox_PaletteId_valueChanged(int paletteId) { emit this->changedPalette(paletteId); } +void PaletteEditor::commitEditHistory() { + commitEditHistory(ui->spinBox_PaletteId->value()); +} + void PaletteEditor::commitEditHistory(int paletteId) { QList colors; for (int i = 0; i < this->numColors; i++) {