File indexing completed on 2024-05-12 05:39:37

0001 /***************************************************************************
0002 * Copyright (C) 2014 by Renaud Guezennec                                   *
0003 * http://www.rolisteam.org/                                                *
0004 *                                                                          *
0005 *  This file is part of rcse                                               *
0006 *                                                                          *
0007 * rcse is free software; you can redistribute it and/or modify             *
0008 * it under the terms of the GNU General Public License as published by     *
0009 * the Free Software Foundation; either version 2 of the License, or        *
0010 * (at your option) any later version.                                      *
0011 *                                                                          *
0012 * rcse is distributed in the hope that it will be useful,                  *
0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of           *
0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
0015 * GNU General Public License for more details.                             *
0016 *                                                                          *
0017 * You should have received a copy of the GNU General Public License        *
0018 * along with this program; if not, write to the                            *
0019 * Free Software Foundation, Inc.,                                          *
0020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.                 *
0021 ***************************************************************************/
0022 #include "borderlisteditor.h"
0023 
0024 #include <QListWidget>
0025 
0026 BorderListEditor::BorderListEditor(QObject *widget)
0027     : QStyledItemDelegate(widget)
0028 {
0029     m_map.insert(FieldController::UP,tr("Up"));
0030     m_map.insert(FieldController::LEFT,tr("Left"));
0031     m_map.insert(FieldController::DOWN,tr("Down"));
0032     m_map.insert(FieldController::RIGHT,tr("Right"));
0033 }
0034 
0035 QString BorderListEditor::displayText(const QVariant &value, const QLocale &locale) const
0036 {
0037     Q_UNUSED(locale);
0038     int border = value.toInt();
0039 
0040     QString result;
0041     if(border==15)
0042     {
0043         result=tr("All");
0044     }
0045     else if(border==16)
0046     {
0047         result=tr("None");
0048     }
0049     else
0050     {
0051         QStringList list;
0052         foreach (FieldController::BorderLine line, m_map.keys())
0053         {
0054             if(border & line)
0055             {
0056                 list << m_map[line];
0057             }
0058         }
0059         result = list.join(',');
0060     }
0061     return result;
0062 }
0063 
0064 QWidget *BorderListEditor::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
0065 {
0066     Q_UNUSED(option);
0067     Q_UNUSED(index);
0068     QListWidget* cb = new QListWidget(parent);
0069     cb->setSelectionMode(QAbstractItemView::ExtendedSelection);
0070     cb->setMinimumHeight(80);
0071     return cb;
0072 }
0073 
0074 void BorderListEditor::setEditorData(QWidget *editor, const QModelIndex &index) const
0075 {
0076     QListWidget* list = qobject_cast<QListWidget*>(editor);
0077     int i = index.data().toInt();
0078 
0079     foreach (FieldController::BorderLine line, m_map.keys())
0080     {
0081         QListWidgetItem* item = new QListWidgetItem(list);
0082         item->setData(Qt::UserRole,(int)line);
0083         item->setText(m_map[line]);
0084         item->setSelected(i & line);
0085     }
0086 
0087 }
0088 
0089 void BorderListEditor::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
0090 {
0091     QListWidget* list = qobject_cast<QListWidget*>(editor);
0092     int value = 0;
0093     QList<QListWidgetItem*> selection = list->selectedItems();
0094     if(selection.isEmpty())
0095     {
0096         value = 16;
0097     }
0098     else if(selection.size() == m_map.size())
0099     {
0100         value = 15;
0101     }
0102     else
0103     {
0104         foreach(QListWidgetItem* item,selection)
0105         {
0106             value += item->data(Qt::UserRole).toInt();
0107         }
0108 
0109     }
0110     model->setData(index,value);
0111     QStyledItemDelegate::setEditorData(editor, index);
0112 }