1 /* This file is part of the KDE project
2 * Copyright (C) 2011 Gopalakrishna Bhat A <gopalakbhat@gmail.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 "TableOfContentsStyleDelegate.h"
21
22 #include <klocalizedstring.h>
23
24 #include <QSpinBox>
25
TableOfContentsStyleDelegate()26 TableOfContentsStyleDelegate::TableOfContentsStyleDelegate()
27 : QStyledItemDelegate()
28 {
29 }
30
sizeHint(const QStyleOptionViewItem & option,const QModelIndex & index) const31 QSize TableOfContentsStyleDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
32 {
33 Q_UNUSED(option);
34 Q_UNUSED(index);
35 return QSize(250, 48);
36 }
37
createEditor(QWidget * parent,const QStyleOptionViewItem &,const QModelIndex &) const38 QWidget *TableOfContentsStyleDelegate::createEditor(QWidget *parent,
39 const QStyleOptionViewItem &/* option */,
40 const QModelIndex &/* index */) const
41 {
42 QSpinBox *editor = new QSpinBox(parent);
43 editor->setMinimum(0);
44 editor->setMaximum(100);
45
46 return editor;
47 }
48
setEditorData(QWidget * editor,const QModelIndex & index) const49 void TableOfContentsStyleDelegate::setEditorData(QWidget *editor,
50 const QModelIndex &index) const
51 {
52 int value = index.model()->data(index, Qt::EditRole).toInt();
53 QSpinBox *spinBox = static_cast<QSpinBox *>(editor);
54 spinBox->setMinimum(0);
55 spinBox->setMaximum(10);
56 spinBox->setSpecialValueText(i18n("Disabled"));
57 spinBox->setValue(value);
58 }
59
setModelData(QWidget * editor,QAbstractItemModel * model,const QModelIndex & index) const60 void TableOfContentsStyleDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
61 const QModelIndex &index) const
62 {
63 QSpinBox *spinBox = static_cast<QSpinBox *>(editor);
64 spinBox->interpretText();
65 int value = spinBox->value();
66
67 model->setData(index, value, Qt::EditRole);
68 }
69
updateEditorGeometry(QWidget * editor,const QStyleOptionViewItem & option,const QModelIndex &) const70 void TableOfContentsStyleDelegate::updateEditorGeometry(QWidget *editor,
71 const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
72 {
73 editor->setGeometry(option.rect);
74 }
75