From fba4f6a5433dcca8628ff2e6c368c1747e0d8726 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 29 Aug 2020 17:21:28 -0500 Subject: [PATCH] [nostalgia/core/studio] Add 8 bit to 5 bit color conversion optiont to PaletteEditor --- src/nostalgia/core/studio/paletteeditor.cpp | 52 +++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/nostalgia/core/studio/paletteeditor.cpp b/src/nostalgia/core/studio/paletteeditor.cpp index 453f5d4b..a49e114d 100644 --- a/src/nostalgia/core/studio/paletteeditor.cpp +++ b/src/nostalgia/core/studio/paletteeditor.cpp @@ -28,6 +28,51 @@ enum class PaletteEditorCommandId { }; +class ColorChannelValidator: public QValidator { + + public: + ColorChannelValidator(QLineEdit *parent); + + QValidator::State validate(QString &input, int&) const override; + + private: + QString convert(const QString &input) const; + +}; + +ColorChannelValidator::ColorChannelValidator(QLineEdit *parent): QValidator(parent) { + connect(parent, &QLineEdit::editingFinished, [this, parent] { + parent->setText(convert(parent->text())); + }); +} + +QString ColorChannelValidator::convert(const QString &input) const { + int num = 0; + if (input[0] == '_') { + num = input.mid(1).toInt() >> 3; + } + return QString::number(num); +} + +QValidator::State ColorChannelValidator::validate(QString &input, int&) const { + if (input.size() == 0) { + return QValidator::State::Intermediate; + } + const auto convert = input[0] == '_'; + const auto max = convert ? 255 : 31; + const auto numTxt = convert ? input.mid(1) : input; + bool isNumber = false; + const auto num = numTxt.toInt(&isNumber); + if (isNumber && num >= 0 && num <= max) { + return QValidator::State::Acceptable; + } else if (numTxt == "") { + return QValidator::State::Intermediate; + } else { + return QValidator::State::Invalid; + } +} + + class AddColorCommand: public QUndoCommand { private: PaletteEditor *m_editor = nullptr; @@ -149,10 +194,11 @@ class MoveColorCommand: public QUndoCommand { QWidget *PaletteEditorColorTableDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem&, const QModelIndex &idx) const { - const auto max = idx.column() != 3 ? 31 : 1; auto le = new QLineEdit(parent); - auto validator = new QIntValidator(0, max, le); - le->setValidator(validator); + if (idx.column()) { + auto validator = new ColorChannelValidator(le); + le->setValidator(validator); + } return le; }