summaryrefslogtreecommitdiffstats
path: root/mappingtableeditor.cpp
diff options
context:
space:
mode:
authorArno <am@disconnect.de>2011-02-19 13:59:19 +0100
committerArno <am@disconnect.de>2011-02-19 13:59:19 +0100
commit23289ad65a1b06d3c3e9bbd4706ce75ce40c4b52 (patch)
tree6a3599e525a29141f9d658b94e7e32a012896ca5 /mappingtableeditor.cpp
parenta2e6438b97a8b9055bee400ccc40a062f0a0e9db (diff)
downloadSheMov-23289ad65a1b06d3c3e9bbd4706ce75ce40c4b52.tar.gz
SheMov-23289ad65a1b06d3c3e9bbd4706ce75ce40c4b52.tar.bz2
SheMov-23289ad65a1b06d3c3e9bbd4706ce75ce40c4b52.zip
Edit genres and actors
Added dialog for editing actors and genres.
Diffstat (limited to 'mappingtableeditor.cpp')
-rw-r--r--mappingtableeditor.cpp236
1 files changed, 236 insertions, 0 deletions
diff --git a/mappingtableeditor.cpp b/mappingtableeditor.cpp
new file mode 100644
index 0000000..a5721d2
--- /dev/null
+++ b/mappingtableeditor.cpp
@@ -0,0 +1,236 @@
+/*
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version
+ 2 of the License, or (at your option) any later version.
+*/
+
+#include <QSqlDatabase>
+#include <QSqlQuery>
+#include <QContextMenuEvent>
+#include <QMenu>
+#include <QVBoxLayout>
+#include <QHBoxLayout>
+#include <QLineEdit>
+#include <QPushButton>
+#include <QLabel>
+#include <QMessageBox>
+#include <QAction>
+#include <QSortFilterProxyModel>
+
+#include "mappingtableeditor.h"
+
+//widget
+MappingTableEditor::MappingTableEditor(const QString &table, QWidget *parent) : QDialog(parent), mTable(table){
+ //caption
+ QString l1String = QString(tr("Edit %1s").arg(mTable));
+ QLabel *l1 = new QLabel(l1String);
+
+ //model + view
+ QStringList headers = QStringList() << QString("%1s").arg(mTable) << tr("Count");
+ mModel = new MappingTableEditorModel(table, headers, this);
+ mView = new MappingTableEditorView;
+ QSortFilterProxyModel *proxy = new QSortFilterProxyModel(this);
+ proxy->setSourceModel(mModel);
+ mView->setModel(proxy);
+ mView->setRootIsDecorated(false);
+ mView->setColumnHidden(2, true);
+ mView->resizeColumnToContents(0);
+ mView->setSortingEnabled(true);
+ mView->setAlternatingRowColors(true);
+ mView->sortByColumn(0, Qt::AscendingOrder);
+ mView->setSelectionMode(QAbstractItemView::SingleSelection);
+
+ //edit
+ QHBoxLayout *editLayout = new QHBoxLayout;
+ QLabel *l2 = new QLabel(tr("Item"));
+ editLayout->addWidget(l2);
+ mDataEdit = new QLineEdit;
+ editLayout->addWidget(mDataEdit);
+
+ //buttons
+ QHBoxLayout *buttonLayout = new QHBoxLayout;
+ buttonLayout->setAlignment(Qt::AlignRight);
+ mClose = new QPushButton(tr("Close"));
+ mClose->setDefault(true);
+ mRename = new QPushButton(tr("Rename"));
+ mDelete = new QPushButton(tr("Delete"));
+ buttonLayout->addWidget(mDelete);
+ buttonLayout->addWidget(mRename);
+ buttonLayout->addWidget(mClose);
+
+ //mainlayout
+ QVBoxLayout *mainLayout = new QVBoxLayout;
+ mainLayout->addWidget(l1);
+ mainLayout->addWidget(mView);
+ mainLayout->addLayout(editLayout);
+ mainLayout->addLayout(buttonLayout);
+ setLayout(mainLayout);
+
+ //actions
+ mDeleteA = new QAction(tr("Delete"), this);
+ mView->addAction(mDeleteA);
+ mEditA = new QAction(tr("Edit..."), this);
+ mView->addAction(mEditA);
+
+ //connections
+ connect(mView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(selectionChanged(QModelIndex,QModelIndex)));
+ connect(mClose, SIGNAL(clicked()), this, SLOT(accept()));
+ connect(mDelete, SIGNAL(clicked()), this, SLOT(removeItem()));
+ connect(mRename, SIGNAL(clicked()), this, SLOT(renameItem()));
+ connect(mDeleteA, SIGNAL(triggered()), this, SLOT(removeItem()));
+ connect(mEditA, SIGNAL(triggered()), this, SLOT(editItem()));
+}
+
+void MappingTableEditor::selectionChanged(const QModelIndex &cur, const QModelIndex &prev){
+ Q_UNUSED(prev);
+ mDataEdit->setText(cur.data(MappingTableEditorModel::NameRole).toString());
+}
+
+void MappingTableEditor::removeItem(){
+ QModelIndexList selected = mView->selectionModel()->selectedRows();
+ if(selected.isEmpty()){
+ return;
+ }
+ int count = selected.at(0).data(MappingTableEditorModel::CountRole).toInt();
+ if(count > 0){
+ QString warning = QString(tr("Really remove %1 %2? There are %3 items associated with it.")).arg(mTable).arg(selected.at(0).data(MappingTableEditorModel::NameRole).toString()).arg(QString::number(count));
+ int mbRetval = QMessageBox::warning(this, tr("Warning!"), warning, QMessageBox::Yes | QMessageBox::No);
+ if(mbRetval == QMessageBox::No){
+ return;
+ }
+ }
+ mModel->removeData(selected.at(0));
+}
+
+void MappingTableEditor::renameItem(){
+ QModelIndexList selected = mView->selectionModel()->selectedRows();
+ if(selected.isEmpty()){
+ return;
+ }
+ QString newValue = mDataEdit->text().toLower().trimmed();
+ QModelIndex newIdx = mModel->find(newValue, MappingTableEditorModel::Name);
+ if(newIdx.isValid()){
+ QString warning = QString(tr("New value %1 already exists. Merge?")).arg(newValue);
+ int mbRetval = QMessageBox::warning(this, tr("Warning!"), warning, QMessageBox::Yes | QMessageBox::No);
+ if(mbRetval == QMessageBox::No){
+ return;
+ }
+ }
+ mModel->setData(selected.at(0), newValue, Qt::EditRole);
+}
+
+void MappingTableEditor::editItem(){
+ QModelIndexList selected = mView->selectionModel()->selectedRows();
+ if(selected.isEmpty()){
+ return;
+ }
+ mView->edit(selected.at(0));
+}
+
+//view
+MappingTableEditorView::MappingTableEditorView(QWidget *parent) : QTreeView(parent) {}
+
+void MappingTableEditorView::contextMenuEvent(QContextMenuEvent *e){
+ QMenu contextMenu(this);
+ contextMenu.addActions(actions());
+ contextMenu.exec(e->globalPos());
+}
+
+//model
+MappingTableEditorModel::MappingTableEditorModel(const QString &table, const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent), mTable(table){
+ mDb = QSqlDatabase::database("treedb");
+ QString queryString = QString("SELECT t%1name, COUNT(iseriesparts_id), %1s.i%1s_id from %1s, seriesparts_%1map WHERE %1s.i%1s_id = seriesparts_%1map.i%1s_id GROUP BY %1s.t%1name, %1s.i%1s_id ORDER BY t%1name").arg(mTable);
+ mDataQuery = new QSqlQuery(mDb);
+ mDataQuery->prepare(queryString);
+ QString mergeQuery = QString("UPDATE %1s SET %1s_id = %2 WHERE %1s_id = %3");
+ populate();
+}
+
+MappingTableEditorModel::~MappingTableEditorModel(){
+ delete mDataQuery;
+ mDb = QSqlDatabase();
+}
+
+Qt::ItemFlags MappingTableEditorModel::flags(const QModelIndex &index) const{
+ if(!index.isValid()){
+ return 0;
+ }
+ Qt::ItemFlags retval = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
+ if(index.column() == 0){
+ retval |= Qt::ItemIsEditable;
+ }
+ return retval;
+}
+
+QVariant MappingTableEditorModel::data(const QModelIndex &index, int role) const{
+ if(!index.isValid()){
+ return QVariant();
+ }
+ SmTreeItem *item = static_cast<SmTreeItem*>(index.internalPointer());
+ if(role == NameRole){
+ return item->data(Name);
+ }
+ if(role == CountRole){
+ return item->data(Count);
+ }
+ if(role == IdRole){
+ return item->data(Id);
+ }
+ return SmTreeModel::data(index, role);
+}
+
+bool MappingTableEditorModel::setData(const QModelIndex &idx, const QVariant &value, int role){
+ if(!idx.isValid()){
+ return false;
+ }
+ if(role != Qt::EditRole){
+ return false;
+ }
+ bool retval = false;
+ QModelIndex newValue = find(value);
+ if(newValue.isValid()){
+ QSqlQuery mergeQuery(mDb);
+ QString query = QString("UPDATE %1s SET %1s_id = :newid WHERE %1s_id = :oldid").arg(mTable);
+ mergeQuery.prepare(query);
+ mergeQuery.bindValue(":newid", newValue.data(IdRole));
+ mergeQuery.bindValue(":oldid", idx.data(IdRole));
+ retval = mergeQuery.exec();
+ }else{
+ QSqlQuery renameQuery(mDb);
+ QString query = QString("UPDATE %1s SET t%1name = :name WHERE i%1s_id = :id").arg(mTable);
+ renameQuery.prepare(query);
+ renameQuery.bindValue(":name", value);
+ renameQuery.bindValue(":id", idx.data(IdRole));
+ retval = renameQuery.exec();
+ }
+ if(retval){
+ populate();
+ }
+ return retval;
+}
+
+bool MappingTableEditorModel::removeData(const QModelIndex &idx){
+ if(!idx.isValid()){
+ return false;
+ }
+ QString removeQuery = QString("DELETE FROM %1s WHERE i%1s_id = %2").arg(mTable).arg(QString::number(idx.data(IdRole).toInt()));
+ QSqlQuery remove(removeQuery, mDb);
+ bool retval = remove.exec();
+ if(retval){
+ populate();
+ }
+ return retval;
+}
+
+void MappingTableEditorModel::populate(){
+ SmTreeItem *rootItem = new SmTreeItem(3);
+ mDataQuery->exec();
+ while(mDataQuery->next()){
+ QList<QVariant> data;
+ data << mDataQuery->value(0) << mDataQuery->value(1) << mDataQuery->value(2);
+ SmTreeItem *childItem = new SmTreeItem(data, rootItem);
+ rootItem->appendChild(childItem);
+ }
+ setRoot(rootItem);
+}