diff options
author | Arno <am@disconnect.de> | 2010-06-26 15:24:00 +0200 |
---|---|---|
committer | Arno <am@disconnect.de> | 2010-06-26 15:24:00 +0200 |
commit | 0b807eba97e65bf9e25f83387826ef2579b79c90 (patch) | |
tree | 9a86166a4ebd77d95e9a26b723c49ebab417e337 /mappingtablewidget.cpp | |
parent | 6567a92bec5ca8b2bc6b7156bddc5ba8508f31b3 (diff) | |
download | SheMov-0b807eba97e65bf9e25f83387826ef2579b79c90.tar.gz SheMov-0b807eba97e65bf9e25f83387826ef2579b79c90.tar.bz2 SheMov-0b807eba97e65bf9e25f83387826ef2579b79c90.zip |
Actors and genre edit
Implemented widgets for actor and genre editing of movies. Created new
MappingTableWidget for both genres and actors and revamped
ArchiveTreeView to show 2 widgets below the FilesTreeView separated by a
splitter.
While testing the new setup several bugs were fixed:
-an SQL syntax error in FilesTreeModel
-fixed SmModelSingleton to properly work with table names
I also changed the signature of MappingTableModel::addMapping for the
ease of use and added MappingTableModel::removeMapping.
MappingTableModel got 2 new convenience functions:
1. bool contains(QString) to check if an item is already present
2. QModelIndex find() to get the index of a specific value from the
model.
Diffstat (limited to 'mappingtablewidget.cpp')
-rw-r--r-- | mappingtablewidget.cpp | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/mappingtablewidget.cpp b/mappingtablewidget.cpp new file mode 100644 index 0000000..e4a177c --- /dev/null +++ b/mappingtablewidget.cpp @@ -0,0 +1,146 @@ +/* + 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 <QIcon> +#include <QTreeView> +#include <QPushButton> +#include <QLineEdit> +#include <QHBoxLayout> +#include <QVBoxLayout> +#include <QLabel> +#include <QCompleter> +#include <QGroupBox> + +#include "mappingtablewidget.h" +#include "mappingtablemodel.h" +#include "smmodelsingleton.h" + +MappingTableWidget::MappingTableWidget(const QString &table, QWidget *parent) : QWidget(parent), mTable(table), mCurrentId(-1){ + //the view + mView = new QTreeView; + mView->setHeaderHidden(true); + mModel = new MappingTableItemModel(this); + mModel->setHeaderData(0, Qt::Horizontal, mTable); + mView->setModel(mModel); + + //mapping model + mMappingModel = static_cast<MappingTableModel*>(SmModelSingleton::instance()->model(mTable)); + + //editor + QHBoxLayout *itemEditLayout = new QHBoxLayout; + QString l1Text = QString(tr("&Edit %1")).arg(mTable); + QLabel *l1 = new QLabel(l1Text); + mItemEdit = new QLineEdit; + QCompleter *completer = new QCompleter(this); + completer->setModel(mMappingModel); + mItemEdit->setCompleter(completer); + l1->setBuddy(mItemEdit); + itemEditLayout->addWidget(l1); + itemEditLayout->addWidget(mItemEdit); + connect(mItemEdit, SIGNAL(returnPressed()), this, SLOT(addItem())); + + //buttons + QHBoxLayout *buttonLayout = new QHBoxLayout; + buttonLayout->addStretch(); + mAddItem = new QPushButton(tr("Add item")); + mRemoveItem = new QPushButton(tr("Remove item")); + buttonLayout->addWidget(mAddItem); + buttonLayout->addWidget(mRemoveItem); + connect(mAddItem, SIGNAL(clicked()), this, SLOT(addItem())); + connect(mRemoveItem, SIGNAL(clicked()), this, SLOT(removeItem())); + + //layout + QVBoxLayout *groupBoxLayout = new QVBoxLayout; + groupBoxLayout->addWidget(mView); + groupBoxLayout->addLayout(itemEditLayout); + groupBoxLayout->addLayout(buttonLayout); + QString gbCaption = QString(tr("Edit %1")).arg(mTable); + QGroupBox *gb = new QGroupBox(gbCaption); + gb->setLayout(groupBoxLayout); + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(gb); + setLayout(mainLayout); + setContentsMargins(0, 0, 0, 0); +} + +void MappingTableWidget::setStringList(const QStringList &list){ + mModel->setStringList(list); +} + +void MappingTableWidget::setEditEnabled(bool enabled){ + mAddItem->setEnabled(enabled); + mRemoveItem->setEnabled(enabled); + mItemEdit->setEnabled(enabled); +} +void MappingTableWidget::addItem(){ + QString value = mItemEdit->text().toLower().trimmed(); + if(mModel->stringList().contains(value)){ + return; + } + if(!mMappingModel->contains(value)){ + mMappingModel->addItem(value); + } + int row = mModel->lowerBound(value); + if(mModel->insertRows(row, 1)){ + QModelIndex idx = mModel->index(row, 0); + mModel->setData(idx, value); + QModelIndex mapIndex = mMappingModel->find(value, 0, QModelIndex()); + if(mapIndex.isValid()){ + int id = mapIndex.data(MappingTableModel::ItemIdRole).toInt(); + mMappingModel->addMapping(mCurrentId, id); + } + } +} + +void MappingTableWidget::removeItem(){ + QModelIndexList selected = mView->selectionModel()->selectedRows(); + if(selected.isEmpty()){ + return; + } + foreach(QPersistentModelIndex i, selected){ + QString item = i.data().toString(); + mModel->removeRows(i.column(), 1); + QModelIndex itemIdx = mMappingModel->find(item); + if(itemIdx.isValid()){ + mMappingModel->removeMapping(mCurrentId, itemIdx.data(MappingTableModel::ItemIdRole).toInt()); + } + } +} + +MappingTableItemModel::MappingTableItemModel(QObject *parent) : QStringListModel(parent) {} + +QVariant MappingTableItemModel::data(const QModelIndex &index, int role) const{ + if(role == Qt::DecorationRole){ + return QIcon(":/dildo.png"); + } + return QStringListModel::data(index, role); +} + +Qt::ItemFlags MappingTableItemModel::flags(const QModelIndex &index) const{ + Q_UNUSED(index); + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} + +int MappingTableItemModel::lowerBound(const QString &value) const{ + int retval = 0; + for(int i = 0; i < stringList().count(); ++i){ + if(value < stringList().at(i)){ + ++retval; + }else{ + break; + } + } + return retval; +} + +QModelIndex MappingTableItemModel::find(const QString &value) const{ + int row = stringList().indexOf(value); + if(row == -1){ + return QModelIndex(); + } + return index(row, 0); +} |