3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
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.
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.
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/>.
21 #include "stringlistdelegate.h"
22 #include "fileextensionwidget.h"
26 StringListDelegate::StringListDelegate(QString separator, QObject *parent) :
27 QStyledItemDelegate(parent), separator(separator)
31 /*void StringListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
33 QString str = index.model()->data(index, Qt::DisplayRole).toString();
37 QWidget* StringListDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
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()));
47 void StringListDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
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));
54 void StringListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
56 StringListWidget *strListWdg = qobject_cast<StringListWidget *>(editor);
57 QStringList ls = strListWdg->getItems();
58 model->setData(index, ls.empty() ? "" : ls.join(separator));
61 void StringListDelegate::commitAndCloseEditor()
63 StringListWidget *editor = qobject_cast<StringListWidget *>(sender());
64 emit commitData(editor);
65 emit closeEditor(editor);
68 QSize StringListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
70 QSize sz(WIDTH, HEIGHT);
74 void StringListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
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());
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);