1 /* This file is part of the KDE project
2 Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.com>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "KoSopranoTableModelDelegate.h"
21
22 // lib
23 #include "KoSopranoTableModel.h"
24 #include "KoDocumentRdf.h"
25 // main
26 #include <KoDocument.h>
27 // KF5
28 #include <klocalizedstring.h>
29 // Qt
30 #include <QComboBox>
31
KoSopranoTableModelDelegate(QObject * parent)32 KoSopranoTableModelDelegate::KoSopranoTableModelDelegate(QObject *parent)
33 : QStyledItemDelegate(parent)
34 {
35 }
36
createEditor(QWidget * parent,const QStyleOptionViewItem & option,const QModelIndex & index) const37 QWidget *KoSopranoTableModelDelegate::createEditor(QWidget *parent,
38 const QStyleOptionViewItem &option,
39 const QModelIndex &index) const
40 {
41 QComboBox *comboBox = new QComboBox(parent);
42 if (index.column() == KoSopranoTableModel::ColObjType) {
43 comboBox->addItem(i18n("URI"));
44 comboBox->addItem(i18n("Literal"));
45 comboBox->addItem(i18n("Blank"));
46 } else {
47 return QStyledItemDelegate::createEditor(parent, option, index);
48 }
49 connect(comboBox, SIGNAL(activated(int)), this, SLOT(emitCommitData()));
50 return comboBox;
51 }
52
setEditorData(QWidget * editor,const QModelIndex & index) const53 void KoSopranoTableModelDelegate::setEditorData(QWidget *editor,
54 const QModelIndex &index) const
55 {
56 QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
57 if (!comboBox) {
58 return QStyledItemDelegate::setEditorData(editor, index);
59 }
60 int pos = comboBox->findText(index.model()->data(index).toString(),
61 Qt::MatchExactly);
62 comboBox->setCurrentIndex(pos);
63 }
64
setModelData(QWidget * editor,QAbstractItemModel * model,const QModelIndex & index) const65 void KoSopranoTableModelDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
66 const QModelIndex &index) const
67 {
68 QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
69 if (!comboBox) {
70 return QStyledItemDelegate::setModelData(editor, model, index);
71 }
72 model->setData(index, comboBox->currentText());
73 }
74
emitCommitData()75 void KoSopranoTableModelDelegate::emitCommitData()
76 {
77 emit commitData(qobject_cast<QWidget *>(sender()));
78 }
79