diff --git a/src/nostalgia/core/studio/paletteeditor.cpp b/src/nostalgia/core/studio/paletteeditor.cpp index fcbcb460..453f5d4b 100644 --- a/src/nostalgia/core/studio/paletteeditor.cpp +++ b/src/nostalgia/core/studio/paletteeditor.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -28,26 +27,6 @@ enum class PaletteEditorCommandId { MoveColor, }; -struct PaletteEditorColorTableDelegate: public QStyledItemDelegate { - - 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 { private: @@ -169,6 +148,24 @@ 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); + return le; +} + +void PaletteEditorColorTableDelegate::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)); + } +} + + static QTableWidgetItem *mkCell(QString v, bool editable = true) { auto c = new QTableWidgetItem; c->setText(v); @@ -206,7 +203,7 @@ PaletteEditor::PaletteEditor(QString path, const studio::Context *ctx, QWidget * canvasLyt->setMenuBar(tb); m_table = new QTableWidget(this); - m_table->setItemDelegate(new PaletteEditorColorTableDelegate); + m_table->setItemDelegate(&m_colorTableDelegate); m_table->setColumnCount(5); m_table->setSelectionBehavior(QAbstractItemView::SelectRows); m_table->setSelectionMode(QAbstractItemView::SingleSelection); diff --git a/src/nostalgia/core/studio/paletteeditor.hpp b/src/nostalgia/core/studio/paletteeditor.hpp index 24bcfb2f..cedd45dc 100644 --- a/src/nostalgia/core/studio/paletteeditor.hpp +++ b/src/nostalgia/core/studio/paletteeditor.hpp @@ -8,11 +8,21 @@ #pragma once +#include + #include #include namespace nostalgia::core { +struct PaletteEditorColorTableDelegate: public QStyledItemDelegate { + + QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem&, const QModelIndex &idx) const; + + void paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &idx) const; + +}; + class PaletteEditor: public studio::Editor { friend class AddColorCommand; @@ -21,6 +31,7 @@ class PaletteEditor: public studio::Editor { friend class MoveColorCommand; private: + PaletteEditorColorTableDelegate m_colorTableDelegate; const studio::Context *m_ctx = nullptr; QString m_itemPath; std::unique_ptr m_pal; diff --git a/src/nostalgia/core/studio/tilesheeteditor.cpp b/src/nostalgia/core/studio/tilesheeteditor.cpp index 1c467ba4..50275576 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include @@ -55,20 +54,6 @@ 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: enum class Dimension { @@ -236,6 +221,16 @@ class InsertTileCommand: public QUndoCommand { }; +void TileSheetEditorColorTableDelegate::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)); + } +} + + SheetData::SheetData(QUndoStack *undoStack): m_cmdStack(undoStack) { } @@ -506,7 +501,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->setItemDelegate(&m_colorTableDelegate); m_colorPicker.colorTable->setColumnCount(2); m_colorPicker.colorTable->setSelectionBehavior(QAbstractItemView::SelectRows); m_colorPicker.colorTable->setSelectionMode(QAbstractItemView::SingleSelection); diff --git a/src/nostalgia/core/studio/tilesheeteditor.hpp b/src/nostalgia/core/studio/tilesheeteditor.hpp index 70365faf..e6d29d58 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.hpp +++ b/src/nostalgia/core/studio/tilesheeteditor.hpp @@ -9,6 +9,7 @@ #pragma once #include +#include #include #include @@ -17,6 +18,12 @@ namespace nostalgia::core { +struct TileSheetEditorColorTableDelegate: public QStyledItemDelegate { + + void paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &idx) const; + +}; + class SheetData: public QObject { Q_OBJECT Q_PROPERTY(int columns READ columns WRITE setColumns NOTIFY columnsChanged) @@ -113,6 +120,7 @@ class TileSheetEditor: public studio::Editor { Q_OBJECT private: + TileSheetEditorColorTableDelegate m_colorTableDelegate; QString m_itemPath; QString m_itemName; const studio::Context *m_ctx = nullptr;