forked from mirror/openmw-tes3mp
Refactor Enum- and DataDisplayDelegate code
This commit is contained in:
parent
0ffb2bc6bc
commit
2c1ca33a20
3 changed files with 45 additions and 56 deletions
|
@ -62,16 +62,11 @@ void CSVWorld::DataDisplayDelegate::paint (QPainter *painter, const QStyleOption
|
||||||
EnumDelegate::paint(painter, option, index);
|
EnumDelegate::paint(painter, option, index);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
unsigned int i = 0;
|
int valueIndex = getValueIndex(index);
|
||||||
|
if (valueIndex != -1)
|
||||||
for (; i < mValues.size(); ++i)
|
|
||||||
{
|
{
|
||||||
if (mValues.at(i).first == index.data().toInt())
|
paintIcon(painter, option, valueIndex);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < mValues.size() )
|
|
||||||
paintIcon (painter, option, i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
painter->restore();
|
painter->restore();
|
||||||
|
|
|
@ -10,6 +10,24 @@
|
||||||
|
|
||||||
#include "../../model/world/commands.hpp"
|
#include "../../model/world/commands.hpp"
|
||||||
|
|
||||||
|
int CSVWorld::EnumDelegate::getValueIndex(const QModelIndex &index, int role) const
|
||||||
|
{
|
||||||
|
if (index.isValid() && index.data(role).isValid())
|
||||||
|
{
|
||||||
|
int value = index.data(role).toInt();
|
||||||
|
|
||||||
|
int size = static_cast<int>(mValues.size());
|
||||||
|
for (int i = 0; i < size; ++i)
|
||||||
|
{
|
||||||
|
if (value == mValues.at(i).first)
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
void CSVWorld::EnumDelegate::setModelDataImp (QWidget *editor, QAbstractItemModel *model,
|
void CSVWorld::EnumDelegate::setModelDataImp (QWidget *editor, QAbstractItemModel *model,
|
||||||
const QModelIndex& index) const
|
const QModelIndex& index) const
|
||||||
{
|
{
|
||||||
|
@ -67,60 +85,43 @@ QWidget *CSVWorld::EnumDelegate::createEditor(QWidget *parent, const QStyleOptio
|
||||||
|
|
||||||
void CSVWorld::EnumDelegate::setEditorData (QWidget *editor, const QModelIndex& index, bool tryDisplay) const
|
void CSVWorld::EnumDelegate::setEditorData (QWidget *editor, const QModelIndex& index, bool tryDisplay) const
|
||||||
{
|
{
|
||||||
if (QComboBox *comboBox = dynamic_cast<QComboBox *> (editor))
|
if (QComboBox *comboBox = dynamic_cast<QComboBox *>(editor))
|
||||||
{
|
{
|
||||||
QVariant data = index.data (Qt::EditRole);
|
int role = Qt::EditRole;
|
||||||
|
if (tryDisplay && !index.data(role).isValid())
|
||||||
if (tryDisplay && !data.isValid())
|
|
||||||
{
|
{
|
||||||
data = index.data (Qt::DisplayRole);
|
role = Qt::DisplayRole;
|
||||||
if (!data.isValid())
|
if (!index.data(role).isValid())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int value = data.toInt();
|
int valueIndex = getValueIndex(index, role);
|
||||||
|
if (valueIndex != -1)
|
||||||
std::size_t size = mValues.size();
|
{
|
||||||
|
comboBox->setCurrentIndex(valueIndex);
|
||||||
for (std::size_t i=0; i<size; ++i)
|
}
|
||||||
if (mValues[i].first==value)
|
|
||||||
{
|
|
||||||
comboBox->setCurrentIndex (i);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSVWorld::EnumDelegate::paint (QPainter *painter, const QStyleOptionViewItem& option,
|
void CSVWorld::EnumDelegate::paint (QPainter *painter, const QStyleOptionViewItem& option,
|
||||||
const QModelIndex& index) const
|
const QModelIndex& index) const
|
||||||
{
|
{
|
||||||
if (index.data().isValid())
|
int valueIndex = getValueIndex(index);
|
||||||
|
if (valueIndex != -1)
|
||||||
{
|
{
|
||||||
QStyleOptionViewItemV4 option2 (option);
|
QStyleOptionViewItemV4 itemOption(option);
|
||||||
|
itemOption.text = mValues.at(valueIndex).second;
|
||||||
int value = index.data().toInt();
|
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &itemOption, painter);
|
||||||
|
|
||||||
for (std::vector<std::pair<int, QString> >::const_iterator iter (mValues.begin());
|
|
||||||
iter!=mValues.end(); ++iter)
|
|
||||||
if (iter->first==value)
|
|
||||||
{
|
|
||||||
option2.text = iter->second;
|
|
||||||
|
|
||||||
QApplication::style()->drawControl (QStyle::CE_ItemViewItem, &option2, painter);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QSize CSVWorld::EnumDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
QSize CSVWorld::EnumDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
if (index.data().isValid())
|
int valueIndex = getValueIndex(index);
|
||||||
|
if (valueIndex != -1)
|
||||||
{
|
{
|
||||||
int value = index.data().toInt();
|
|
||||||
|
|
||||||
// Calculate the size hint as for a combobox.
|
// Calculate the size hint as for a combobox.
|
||||||
// So, the whole text is visible (isn't elided) when the editor is created
|
// So, the whole text is visible (isn't elided) when the editor is created
|
||||||
QStyleOptionComboBox itemOption;
|
QStyleOptionComboBox itemOption;
|
||||||
|
@ -129,20 +130,11 @@ QSize CSVWorld::EnumDelegate::sizeHint(const QStyleOptionViewItem &option, const
|
||||||
itemOption.rect = option.rect;
|
itemOption.rect = option.rect;
|
||||||
itemOption.state = option.state;
|
itemOption.state = option.state;
|
||||||
|
|
||||||
std::vector<std::pair<int, QString> >::const_iterator current = mValues.begin();
|
const QString &valueText = mValues.at(valueIndex).second;
|
||||||
std::vector<std::pair<int, QString> >::const_iterator end = mValues.end();
|
QSize valueSize = QSize(itemOption.fontMetrics.width(valueText), itemOption.fontMetrics.height());
|
||||||
for (; current != end; ++current)
|
|
||||||
{
|
itemOption.currentText = valueText;
|
||||||
if (current->first == value)
|
return QApplication::style()->sizeFromContents(QStyle::CT_ComboBox, &itemOption, valueSize);
|
||||||
{
|
|
||||||
QSize valueSize = QSize(itemOption.fontMetrics.width(current->second),
|
|
||||||
itemOption.fontMetrics.height());
|
|
||||||
itemOption.currentText = current->second;
|
|
||||||
return QApplication::style()->sizeFromContents(QStyle::CT_ComboBox,
|
|
||||||
&itemOption,
|
|
||||||
valueSize);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return option.rect.size();
|
return option.rect.size();
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,8 @@ namespace CSVWorld
|
||||||
|
|
||||||
std::vector<std::pair<int, QString> > mValues;
|
std::vector<std::pair<int, QString> > mValues;
|
||||||
|
|
||||||
|
int getValueIndex(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
virtual void setModelDataImp (QWidget *editor, QAbstractItemModel *model,
|
virtual void setModelDataImp (QWidget *editor, QAbstractItemModel *model,
|
||||||
|
|
Loading…
Reference in a new issue