[nostalgia/core/studio] Fix memory leaks

This commit is contained in:
2020-08-28 21:35:38 -05:00
parent f15ad04d33
commit cef8c54515
4 changed files with 49 additions and 38 deletions
+19 -22
View File
@@ -10,7 +10,6 @@
#include <QItemDelegate>
#include <QPainter>
#include <QPushButton>
#include <QStyledItemDelegate>
#include <QTableWidget>
#include <QToolBar>
@@ -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);