From 6aa828d1255e83dbc51df7b3ef88d5cfcdc68b75 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mikko=20Kein=C3=A4nen?= Date: Tue, 7 Dec 2010 23:55:07 +0200 Subject: [PATCH] Implemented StringListDelegate. --- src/delegates/stringlistdelegate.cpp | 47 ++++++++++++++++++++++++++++++++-- src/delegates/stringlistdelegate.h | 14 +++++++--- src/models/setupmodel.cpp | 30 ++++++++++++++++++++-- src/models/setupmodel.h | 1 + src/views/setupeditview.cpp | 4 +++ 5 files changed, 88 insertions(+), 8 deletions(-) diff --git a/src/delegates/stringlistdelegate.cpp b/src/delegates/stringlistdelegate.cpp index 1833aeb..7775ab3 100644 --- a/src/delegates/stringlistdelegate.cpp +++ b/src/delegates/stringlistdelegate.cpp @@ -18,8 +18,51 @@ // along with EmuFront. If not, see . #include "stringlistdelegate.h" +#include "fileextensionwidget.h" -StringListDelegate::StringListDelegate(QObject *parent) : - QStyledItemDelegate(parent) +StringListDelegate::StringListDelegate(QString separator, QObject *parent) : + QStyledItemDelegate(parent), separator(separator) { } + +/*void StringListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + QString str = index.model()->data(index, Qt::DisplayRole).toString(); + // TODO:... +}*/ + +QWidget* StringListDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + StringListWidget *editor = new StringListWidget(parent); + QString str = index.model()->data(index, Qt::DisplayRole).toString(); + editor->setItems(str.split(separator, QString::SkipEmptyParts)); + connect(editor, SIGNAL(stringListUpdated()), this, SLOT(commitAndCloseEditor())); + return editor; +} + +void StringListDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const +{ + QString str = index.model()->data(index, Qt::DisplayRole).toString(); + StringListWidget *strListWdg = qobject_cast(editor); + strListWdg->setItems(str.split(separator, QString::SkipEmptyParts)); +} + +void StringListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const +{ + StringListWidget *strListWdg = qobject_cast(editor); + QStringList ls = strListWdg->getItems(); + model->setData(index, ls.empty() ? "" : ls.join(separator)); +} + +void StringListDelegate::commitAndCloseEditor() +{ + StringListWidget *editor = qobject_cast(sender()); + emit commitData(editor); + emit closeEditor(editor); +} + +QSize StringListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + QSize sz(300,300); + return sz; +} diff --git a/src/delegates/stringlistdelegate.h b/src/delegates/stringlistdelegate.h index e9830c3..d26be65 100644 --- a/src/delegates/stringlistdelegate.h +++ b/src/delegates/stringlistdelegate.h @@ -26,12 +26,18 @@ class StringListDelegate : public QStyledItemDelegate { Q_OBJECT public: - explicit StringListDelegate(QObject *parent = 0); + StringListDelegate(QString separator, QObject *parent = 0); + //void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; + QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; + QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; + void setEditorData(QWidget *editor, const QModelIndex &index) const; + void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; -signals: - -public slots: +private slots: + void commitAndCloseEditor(); +private: + QString separator; }; #endif // STRINGLISTDELEGATE_H diff --git a/src/models/setupmodel.cpp b/src/models/setupmodel.cpp index f70b5bb..f1cdc0d 100644 --- a/src/models/setupmodel.cpp +++ b/src/models/setupmodel.cpp @@ -20,6 +20,8 @@ #include "setupmodel.h" #include +const QString SetupModel::FILE_TYPE_EXTENSION_SEPARATOR = QString("|"); + SetupModel::SetupModel(QObject *parent) : EmuFrontQueryModel(parent) { @@ -54,7 +56,10 @@ QString SetupModel::constructSelect(QString where) const Qt::ItemFlags SetupModel::flags(const QModelIndex &index) const { Qt::ItemFlags flags = QSqlQueryModel::flags(index); - if (index.column() == Setup_PlatformId || index.column() == Setup_MediaTypeId) { + int col = index.column(); + if (col == Setup_PlatformId || + col == Setup_MediaTypeId || + col == Setup_FileTypeExtensions) { flags |= Qt::ItemIsEditable; } return flags; @@ -62,7 +67,10 @@ Qt::ItemFlags SetupModel::flags(const QModelIndex &index) const bool SetupModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/) { - if(index.column() != Setup_PlatformId && index.column() != Setup_MediaTypeId) + int col = index.column(); + if(col != Setup_PlatformId && + col != Setup_MediaTypeId && + col != Setup_FileTypeExtensions) return false; QModelIndex primaryKeyIndex @@ -81,6 +89,13 @@ bool SetupModel::setData(const QModelIndex &index, const QVariant &value, int /* case Setup_MediaTypeId: ok = setMediaType(id, value.toInt()); break; + + case Setup_FileTypeExtensions: + ok = setSupportedExtensions(id, value.toString()); + break; + + default: + qDebug() << "Setup model, this shouldn't be happening!"; }; refresh(); return ok; @@ -103,3 +118,14 @@ bool SetupModel::setMediaType(int id, int mediaTypeId) query.bindValue(":id", id); return query.exec(); } + +bool SetupModel::setSupportedExtensions(int id, QString exts) +{ + QSqlQuery query; + query.prepare(QString("update setup set filetypeextensions = :exts where id = :id")); + query.bindValue(":exts", exts); + query.bindValue(":id", id); + return query.exec(); +} + + diff --git a/src/models/setupmodel.h b/src/models/setupmodel.h index df58b35..6ea93d3 100644 --- a/src/models/setupmodel.h +++ b/src/models/setupmodel.h @@ -41,6 +41,7 @@ protected: virtual QString constructSelect(QString where = "") const; virtual bool setPlatform(int id, int platformId); virtual bool setMediaType(int id, int platformId); + virtual bool setSupportedExtensions(int id, QString); }; #endif // SETUPMODEL_H diff --git a/src/views/setupeditview.cpp b/src/views/setupeditview.cpp index 6ea4567..80507f9 100644 --- a/src/views/setupeditview.cpp +++ b/src/views/setupeditview.cpp @@ -21,6 +21,7 @@ #include "setupeditview.h" #include "setupmodel.h" #include "comboboxdelegate.h" +#include "stringlistdelegate.h" #include "platformmodel.h" #include "mediatypemodel.h" #include @@ -46,5 +47,8 @@ SetupEditView::SetupEditView(QWidget *parent) : this ); objectList->setItemDelegateForColumn(SetupModel::Setup_MediaTypeId, mediatypeDelegate); + + StringListDelegate *fileTypeDelegate = new StringListDelegate(SetupModel::FILE_TYPE_EXTENSION_SEPARATOR, this); + objectList->setItemDelegateForColumn(SetupModel::Setup_FileTypeExtensions, fileTypeDelegate); postInit(); } -- 1.7.9.5