Small fixes to delegate rendering.
[emufront] / src / delegates / stringlistdelegate.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
12 **
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "stringlistdelegate.h"
22 #include "fileextensionwidget.h"
23 #include <QPainter>
24 #include <QDebug>
25
26 StringListDelegate::StringListDelegate(QString separator, QObject *parent) :
27     QStyledItemDelegate(parent), separator(separator)
28 {
29 }
30
31 /*void StringListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
32 {
33     QString str = index.model()->data(index, Qt::DisplayRole).toString();
34     // TODO:...
35 }*/
36
37 QWidget* StringListDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
38 {
39     StringListWidget *editor = new StringListWidget(parent);
40         editor->setFixedSize(WIDTH, HEIGHT);
41     QString str = index.model()->data(index, Qt::DisplayRole).toString();
42     editor->setItems(str.split(separator, QString::SkipEmptyParts));
43     connect(editor, SIGNAL(stringListUpdated()), this, SLOT(commitAndCloseEditor()));
44     return editor;
45 }
46
47 void StringListDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
48 {
49     QString str = index.model()->data(index, Qt::DisplayRole).toString();
50     StringListWidget *strListWdg = qobject_cast<StringListWidget *>(editor);
51     strListWdg->setItems(str.split(separator, QString::SkipEmptyParts));
52 }
53
54 void StringListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
55 {
56     StringListWidget *strListWdg = qobject_cast<StringListWidget *>(editor);
57     QStringList ls = strListWdg->getItems();
58     model->setData(index, ls.empty() ? "" : ls.join(separator));
59 }
60
61 void StringListDelegate::commitAndCloseEditor()
62 {
63     StringListWidget *editor = qobject_cast<StringListWidget *>(sender());
64     emit commitData(editor);
65     emit closeEditor(editor);
66 }
67
68 QSize StringListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
69 {
70     QSize sz(WIDTH, HEIGHT);
71     return sz;
72 }
73
74 void StringListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
75 {
76         if (option.state & QStyle::State_Editing) {
77                 QRect rc(option.rect.x(), option.rect.y(), WIDTH, HEIGHT);
78                 painter->fillRect(rc, option.palette.highlight());
79         }
80         else {
81                 painter->save();
82                 QRect rc(option.rect.x(), option.rect.y(), WIDTH, HEIGHT);
83                 painter->fillRect(rc, (option.state & QStyle::State_Selected) ? option.palette.highlight() : option.palette.light() );
84                 QString str = index.model()->data(index, Qt::DisplayRole).toString();
85                 painter->setBrush(option.palette.foreground());
86                 painter->drawText(rc, str);
87                 painter->restore();
88         }
89 }