From 5f0217545cfd3889c4be091e6a48924336c99de7 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 30 Mar 2020 00:43:54 -0500 Subject: [PATCH] [nostalgia/core/studio] Make TileSheetEditor not highlight color cell --- src/nostalgia/core/studio/paletteeditor.cpp | 38 +++++++++---------- src/nostalgia/core/studio/tilesheeteditor.cpp | 18 ++++++++- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/nostalgia/core/studio/paletteeditor.cpp b/src/nostalgia/core/studio/paletteeditor.cpp index 122e0f5f..72bf79c8 100644 --- a/src/nostalgia/core/studio/paletteeditor.cpp +++ b/src/nostalgia/core/studio/paletteeditor.cpp @@ -27,27 +27,25 @@ enum class PaletteEditorCommandId { UpdateColor, }; -class ColorTableDelegate: public QStyledItemDelegate { - public: - QWidget *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); - return le; - } +struct PaletteEditorColorTableDelegate: public QStyledItemDelegate { - void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { - if (index.column() != 4) { - if (option.state & QStyle::State_Selected) { - painter->fillRect(option.rect, option.palette.highlight()); - } - QStyledItemDelegate::paint(painter, option, index); - } else { - auto color = index.model()->data(index, Qt::DisplayRole).toString(); - painter->fillRect(option.rect, QColor(color)); - } + QWidget *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); + return le; + } + + void paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &idx) const { + if (idx.column() != 4) { + QStyledItemDelegate::paint(painter, opt, idx); + } else { + auto color = idx.model()->data(idx, Qt::DisplayRole).toString(); + painter->fillRect(opt.rect, QColor(color)); } + } + }; class AddColorCommand: public QUndoCommand { @@ -172,7 +170,7 @@ PaletteEditor::PaletteEditor(QString path, const studio::Context *ctx, QWidget * canvasLyt->setMenuBar(tb); m_table = new QTableWidget(this); - m_table->setItemDelegate(new ColorTableDelegate); + m_table->setItemDelegate(new PaletteEditorColorTableDelegate); m_table->setColumnCount(5); m_table->setSelectionBehavior(QAbstractItemView::SelectRows); m_table->setSelectionMode(QAbstractItemView::SingleSelection); diff --git a/src/nostalgia/core/studio/tilesheeteditor.cpp b/src/nostalgia/core/studio/tilesheeteditor.cpp index d5fa15bd..e96d6f27 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -19,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +55,19 @@ struct LabeledSpinner: public QWidget { }; +struct TileSheetEditorColorTableDelegate: public QStyledItemDelegate { + + void paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &idx) const { + if (idx.column() != 1) { + QStyledItemDelegate::paint(painter, opt, idx); + } else { + auto color = idx.model()->data(idx, Qt::DisplayRole).toString(); + painter->fillRect(opt.rect, QColor(color)); + } + } + +}; + class UpdateDimensionsCommand: public QUndoCommand { public: @@ -491,6 +506,7 @@ QWidget *TileSheetEditor::setupColorPicker(QWidget *parent) { auto lyt = new QVBoxLayout(colorPicker); m_colorPicker.palette = new QComboBox(colorPicker); m_colorPicker.colorTable = new QTableWidget(colorPicker); + m_colorPicker.colorTable->setItemDelegate(new TileSheetEditorColorTableDelegate); m_colorPicker.colorTable->setColumnCount(2); m_colorPicker.colorTable->setSelectionBehavior(QAbstractItemView::SelectRows); m_colorPicker.colorTable->setSelectionMode(QAbstractItemView::SingleSelection); @@ -621,7 +637,7 @@ void TileSheetEditor::setColorTable() { auto hi = tbl->item(i, 0); hi->setFlags(hi->flags() & ~Qt::ItemIsEditable); auto color = new QTableWidgetItem; - color->setBackground(QColor(hexColors[i])); + color->setText(hexColors[i]); tbl->setItem(i, 1, color); auto ci = tbl->item(i, 1); ci->setFlags(ci->flags() & ~Qt::ItemIsEditable);