[nostalgia/core/studio] Fix memory leaks

This commit is contained in:
Gary Talent 2020-08-28 21:35:38 -05:00
parent f15ad04d33
commit cef8c54515
4 changed files with 49 additions and 38 deletions

View File

@ -10,7 +10,6 @@
#include <QItemDelegate> #include <QItemDelegate>
#include <QPainter> #include <QPainter>
#include <QPushButton> #include <QPushButton>
#include <QStyledItemDelegate>
#include <QTableWidget> #include <QTableWidget>
#include <QToolBar> #include <QToolBar>
@ -28,26 +27,6 @@ enum class PaletteEditorCommandId {
MoveColor, 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 { class AddColorCommand: public QUndoCommand {
private: 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) { static QTableWidgetItem *mkCell(QString v, bool editable = true) {
auto c = new QTableWidgetItem; auto c = new QTableWidgetItem;
c->setText(v); c->setText(v);
@ -206,7 +203,7 @@ PaletteEditor::PaletteEditor(QString path, const studio::Context *ctx, QWidget *
canvasLyt->setMenuBar(tb); canvasLyt->setMenuBar(tb);
m_table = new QTableWidget(this); m_table = new QTableWidget(this);
m_table->setItemDelegate(new PaletteEditorColorTableDelegate); m_table->setItemDelegate(&m_colorTableDelegate);
m_table->setColumnCount(5); m_table->setColumnCount(5);
m_table->setSelectionBehavior(QAbstractItemView::SelectRows); m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
m_table->setSelectionMode(QAbstractItemView::SingleSelection); m_table->setSelectionMode(QAbstractItemView::SingleSelection);

View File

@ -8,11 +8,21 @@
#pragma once #pragma once
#include <QStyledItemDelegate>
#include <nostalgia/core/gfx.hpp> #include <nostalgia/core/gfx.hpp>
#include <nostalgia/studio/studio.hpp> #include <nostalgia/studio/studio.hpp>
namespace nostalgia::core { 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 { class PaletteEditor: public studio::Editor {
friend class AddColorCommand; friend class AddColorCommand;
@ -21,6 +31,7 @@ class PaletteEditor: public studio::Editor {
friend class MoveColorCommand; friend class MoveColorCommand;
private: private:
PaletteEditorColorTableDelegate m_colorTableDelegate;
const studio::Context *m_ctx = nullptr; const studio::Context *m_ctx = nullptr;
QString m_itemPath; QString m_itemPath;
std::unique_ptr<NostalgiaPalette> m_pal; std::unique_ptr<NostalgiaPalette> m_pal;

View File

@ -20,7 +20,6 @@
#include <QSettings> #include <QSettings>
#include <QSpinBox> #include <QSpinBox>
#include <QSplitter> #include <QSplitter>
#include <QStyledItemDelegate>
#include <QUndoCommand> #include <QUndoCommand>
#include <QTableWidget> #include <QTableWidget>
#include <QToolBar> #include <QToolBar>
@ -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 { class UpdateDimensionsCommand: public QUndoCommand {
public: public:
enum class Dimension { 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) { SheetData::SheetData(QUndoStack *undoStack): m_cmdStack(undoStack) {
} }
@ -506,7 +501,7 @@ QWidget *TileSheetEditor::setupColorPicker(QWidget *parent) {
auto lyt = new QVBoxLayout(colorPicker); auto lyt = new QVBoxLayout(colorPicker);
m_colorPicker.palette = new QComboBox(colorPicker); m_colorPicker.palette = new QComboBox(colorPicker);
m_colorPicker.colorTable = new QTableWidget(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->setColumnCount(2);
m_colorPicker.colorTable->setSelectionBehavior(QAbstractItemView::SelectRows); m_colorPicker.colorTable->setSelectionBehavior(QAbstractItemView::SelectRows);
m_colorPicker.colorTable->setSelectionMode(QAbstractItemView::SingleSelection); m_colorPicker.colorTable->setSelectionMode(QAbstractItemView::SingleSelection);

View File

@ -9,6 +9,7 @@
#pragma once #pragma once
#include <QStringList> #include <QStringList>
#include <QStyledItemDelegate>
#include <QUndoStack> #include <QUndoStack>
#include <QVariant> #include <QVariant>
@ -17,6 +18,12 @@
namespace nostalgia::core { namespace nostalgia::core {
struct TileSheetEditorColorTableDelegate: public QStyledItemDelegate {
void paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &idx) const;
};
class SheetData: public QObject { class SheetData: public QObject {
Q_OBJECT Q_OBJECT
Q_PROPERTY(int columns READ columns WRITE setColumns NOTIFY columnsChanged) Q_PROPERTY(int columns READ columns WRITE setColumns NOTIFY columnsChanged)
@ -113,6 +120,7 @@ class TileSheetEditor: public studio::Editor {
Q_OBJECT Q_OBJECT
private: private:
TileSheetEditorColorTableDelegate m_colorTableDelegate;
QString m_itemPath; QString m_itemPath;
QString m_itemName; QString m_itemName;
const studio::Context *m_ctx = nullptr; const studio::Context *m_ctx = nullptr;