src/MaterialComboDelegate.cpp
/*-----------------------------------------------------------------------------------A QtMeshEditor file Copyright (c) Fernando Tonon (https://github.com/fernandotonon) The MIT License Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE.-----------------------------------------------------------------------------------*/ #include <QComboBox> #include <QDebug>#include <OgreEntity.h>#include <OgreSubEntity.h>#include <OgreResourceManager.h>#include <OgreMaterialManager.h> #include "GlobalDefinitions.h" #include "MaterialComboDelegate.h" MaterialComboDelegate::MaterialComboDelegate(QObject *parent) : QStyledItemDelegate(parent){} The function `createEditor` is never used.QWidget *MaterialComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const{ QComboBox *editor = new QComboBox(parent); QStringList materialList; Ogre::ResourceManager::ResourceMapIterator materialIterator = Ogre::MaterialManager::getSingleton().getResourceIterator(); while (materialIterator.hasMoreElements()) { materialList.append(Ogre::static_pointer_cast<Ogre::Material>(materialIterator.peekNextValue())->getName().data()); materialIterator.moveNext(); } editor->addItems(materialList); editor->setFrame(false); editor->setEditable(false); connect(editor, SIGNAL(currentIndexChanged(int)), this, SLOT(commitAndCloseEditor())); return editor;}void MaterialComboDelegate::commitAndCloseEditor(){ QWidget* signalSender = qobject_cast<QWidget*>(sender()); if(signalSender) { emit commitData(signalSender); emit closeEditor(signalSender); }} void MaterialComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{ if(index.column()!=2) return QStyledItemDelegate::setEditorData(editor, index); QComboBox* box = static_cast<QComboBox*>(editor); box->setCurrentText(materialNameFromIndex(index));} void MaterialComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{ if(index.column()!=2) return QStyledItemDelegate::setModelData(editor, model, index); QComboBox* box = static_cast<QComboBox*>(editor); QString value = box->currentText(); model->setData(index, value, Qt::DisplayRole/*Qt::EditRole*/); } The function `updateEditorGeometry` is never used.void MaterialComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const{ editor->setGeometry(option.rect);} void MaterialComboDelegate::initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const{ if(index.column()!=2) return QStyledItemDelegate::initStyleOption(option, index); option->text = materialNameFromIndex(index); } QString MaterialComboDelegate::materialNameFromIndex(const QModelIndex& index) const{ QModelIndex secondColumn = index.model()->index(index.row(),1); Ogre::SubEntity* subEntity = (Ogre::SubEntity*)index.model()->data(secondColumn , SUBENTITY_DATA).value<void *>(); if(subEntity) return subEntity->getMaterialName().data(); return QString();}