#include "emufrontquerymodel.h"
#include "emufrontobject.h"
-ComboBoxDelegate::ComboBoxDelegate(int viewColumn, QSqlQueryModel *model, int modelIdColumn, int modelDisplayColumn, QWidget *parent) :
- QStyledItemDelegate(parent), viewColumn(viewColumn), model(model), modelIdColumn(modelIdColumn), modelDisplayColumn(modelDisplayColumn)
+ComboBoxDelegate::ComboBoxDelegate(int viewColumn, QSqlQueryModel *cbmodel, int cbmodelIdColumn, int cbmodelDisplayColumn, QWidget *parent) :
+ QStyledItemDelegate(parent), viewColumn(viewColumn), cbmodel(cbmodel), cbmodelIdColumn(cbmodelIdColumn), cbmodelDisplayColumn(cbmodelDisplayColumn)
{ }
void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
int objid = index.model()->data(index, Qt::DisplayRole).toInt();
// find matching object from QComboBox's model
- QModelIndex startInd = model->index(0, modelIdColumn);
- QModelIndexList indList = model->match(startInd, Qt::DisplayRole, objid);
+ QModelIndex startInd = cbmodel->index(0, cbmodelIdColumn);
+ QModelIndexList indList = cbmodel->match(startInd, Qt::DisplayRole, objid);
+
QModelIndex ind = indList.empty() ?
QModelIndex() :
- model->index(startInd.row(), modelDisplayColumn);
+ cbmodel->index(indList.first().row(), cbmodelDisplayColumn);
QString txt = ind.isValid() ?
- model->data(ind).toString() : "";
+ cbmodel->data(ind).toString() : "";
painter->save();
//initStyleOption(&option, index);
QComboBox *editor = new QComboBox(parent);
editor->setEditable(false);
- editor->setModel(model);
+ editor->setModel(cbmodel);
+ editor->setModelColumn(cbmodelDisplayColumn);
connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
return editor;
}
int id = index.model()->data(index, Qt::DisplayRole).toInt();
QComboBox *cbox = qobject_cast<QComboBox *>(editor);
// create a start index from combobox model
- QModelIndex startInd = model->index(0, modelIdColumn);
+ QModelIndex startInd = cbmodel->index(0, cbmodelIdColumn);
// search an index for selected item in parent view
- QModelIndexList indList = model->match(startInd, Qt::DisplayRole, id);
+ QModelIndexList indList = cbmodel->match(startInd, Qt::DisplayRole, id);
// if no match set no item selected
int ind = indList.empty() ? -1 : indList.first().row();
cbox->setCurrentIndex(ind);
}
QComboBox *cbox = qobject_cast<QComboBox *>(editor);
if (cbox->currentIndex() == -1) return;
- QModelIndex mi = model->index(cbox->currentIndex(), modelIdColumn);
- int id = model->data(mi).toInt();
+ QModelIndex mi = cbmodel->index(cbox->currentIndex(), cbmodelIdColumn);
+ int id = cbmodel->data(mi).toInt();
model->setData(index, id);
}
{
Q_OBJECT
public:
- ComboBoxDelegate(int column, QSqlQueryModel *, int modelIdColumn, int modelDisplayColumn, QWidget *parent = 0);
+ ComboBoxDelegate(int column,
+ QSqlQueryModel *,
+ int cbmodelIdColumn,
+ int cbmodelDisplayColumn,
+ QWidget *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 commitAndCloseEditor();
private:
int viewColumn;
- QSqlQueryModel *model;
- int modelIdColumn;
- int modelDisplayColumn;
+ QSqlQueryModel *cbmodel;
+ int cbmodelIdColumn;
+ int cbmodelDisplayColumn;
};
#endif // COMBOBOXDELEGATE_H
// along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
#include "setupmodel.h"
+#include <QtSql>
SetupModel::SetupModel(QObject *parent) :
EmuFrontQueryModel(parent)
"ORDER BY SetupName"
).arg(where);
}
+
+Qt::ItemFlags SetupModel::flags(const QModelIndex &index) const
+{
+ Qt::ItemFlags flags = QSqlQueryModel::flags(index);
+ if (index.column() == Setup_PlatformId) {
+ flags |= Qt::ItemIsEditable;
+ }
+ return flags;
+}
+
+bool SetupModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
+{
+ if(index.column() != Setup_PlatformId)
+ return false;
+
+ QModelIndex primaryKeyIndex
+ = QSqlQueryModel::index(index.row(), Setup_Id);
+
+ int id = data(primaryKeyIndex).toInt();
+ clear();
+
+ bool ok;
+ if (index.column() == Setup_PlatformId) {
+ ok = setPlatform(id, value.toInt());
+ }
+
+ refresh();
+ return ok;
+}
+
+bool SetupModel::setPlatform(int id, int platformId)
+{
+ qDebug() << "updating setup " << id << " to platform " << platformId;
+ QSqlQuery query;
+ query.prepare(QString("update setup set platformid = :platformid where id = :id"));
+ query.bindValue(":platformid", platformId);
+ query.bindValue(":id", id);
+ qDebug() << query.lastQuery();
+ qDebug() << query.lastError();
+ return query.exec();
+}
#include <QtGui>
#include "setupeditview.h"
#include "setupmodel.h"
+#include "comboboxdelegate.h"
+#include "platformmodel.h"
+#include <QSqlTableModel>
SetupEditView::SetupEditView(QWidget *parent) :
EmuFrontEditView(parent)
{
model = new SetupModel(this);
objectList->setModel(model);
+ /*QSqlTableModel *platformModel = new QSqlTableModel(this);
+ platformModel->setTable("platform");
+ platformModel->removeColumn(Plat)*/
+ PlatformModel *plfModel = new PlatformModel(this);
+ ComboBoxDelegate *platformDelegate = new ComboBoxDelegate(SetupModel::Setup_PlatformId, plfModel, PlatformModel::EmuFrontFileObject_Id, PlatformModel::EmuFrontFileObject_Name, this);
+ objectList->setItemDelegate(platformDelegate);
postInit();
}