Merge remote-tracking branch 'smbas/fix-enumdelegates'

c++11
Marc Zinnschlag 10 years ago
commit 0b32b24d60

@ -12,11 +12,10 @@ CSVWorld::DataDisplayDelegate::DataDisplayDelegate(const ValueList &values,
const QString &settingName,
QObject *parent)
: EnumDelegate (values, dispatcher, document, parent), mDisplayMode (Mode_TextOnly),
mIcons (icons), mIconSize (QSize(16, 16)), mIconLeftOffset(3),
mIcons (icons), mIconSize (QSize(16, 16)),
mHorizontalMargin(QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1),
mTextLeftOffset(8), mSettingKey (pageName + '/' + settingName)
{
mTextAlignment.setAlignment (Qt::AlignLeft | Qt::AlignVCenter );
buildPixmaps();
QString value =
@ -45,14 +44,34 @@ void CSVWorld::DataDisplayDelegate::setIconSize(const QSize& size)
buildPixmaps();
}
void CSVWorld::DataDisplayDelegate::setIconLeftOffset(int offset)
void CSVWorld::DataDisplayDelegate::setTextLeftOffset(int offset)
{
mIconLeftOffset = offset;
mTextLeftOffset = offset;
}
void CSVWorld::DataDisplayDelegate::setTextLeftOffset(int offset)
QSize CSVWorld::DataDisplayDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
mTextLeftOffset = offset;
QSize size = EnumDelegate::sizeHint(option, index);
int valueIndex = getValueIndex(index);
if (valueIndex != -1)
{
if (mDisplayMode == Mode_IconOnly)
{
size.setWidth(mIconSize.width() + 2 * mHorizontalMargin);
}
else if (mDisplayMode == Mode_IconAndText)
{
int valueWidth = option.fontMetrics.width(mValues.at(valueIndex).second);
size.setWidth(size.width() + mIconSize.width() + mTextLeftOffset);
}
if (mDisplayMode != Mode_TextOnly)
{
size.setHeight(qMax(size.height(), mIconSize.height()));
}
}
return size;
}
void CSVWorld::DataDisplayDelegate::paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
@ -64,16 +83,11 @@ void CSVWorld::DataDisplayDelegate::paint (QPainter *painter, const QStyleOption
EnumDelegate::paint(painter, option, index);
else
{
unsigned int i = 0;
for (; i < mValues.size(); ++i)
int valueIndex = getValueIndex(index);
if (valueIndex != -1)
{
if (mValues.at(i).first == index.data().toInt())
break;
paintIcon(painter, option, valueIndex);
}
if (i < mValues.size() )
paintIcon (painter, option, i);
}
painter->restore();
@ -81,24 +95,28 @@ void CSVWorld::DataDisplayDelegate::paint (QPainter *painter, const QStyleOption
void CSVWorld::DataDisplayDelegate::paintIcon (QPainter *painter, const QStyleOptionViewItem &option, int index) const
{
//function-level statics
QRect iconRect = option.rect;
QRect textRect = iconRect;
const QString &text = mValues.at(index).second;
iconRect.setSize (mIconSize);
iconRect.translate(mIconLeftOffset, (option.rect.height() - iconRect.height())/2);
if (mDisplayMode == Mode_IconAndText )
iconRect.setLeft(iconRect.left() + mHorizontalMargin);
iconRect.setRight(option.rect.right() - mHorizontalMargin);
if (mDisplayMode == Mode_IconAndText)
{
textRect.translate (iconRect.width() + mTextLeftOffset, 0 );
painter->drawText (textRect, text, mTextAlignment);
iconRect.setWidth(mIconSize.width());
textRect.setLeft(iconRect.right() + mTextLeftOffset);
textRect.setRight(option.rect.right() - mHorizontalMargin);
QString text = option.fontMetrics.elidedText(mValues.at(index).second,
option.textElideMode,
textRect.width());
QApplication::style()->drawItemText(painter,
textRect,
Qt::AlignLeft | Qt::AlignVCenter,
option.palette,
true,
text);
}
else
iconRect.translate( (option.rect.width() - iconRect.width()) / 2, 0);
painter->drawPixmap (iconRect, mPixmaps.at(index).second);
QApplication::style()->drawItemPixmap(painter, iconRect, Qt::AlignCenter, mPixmaps.at(index).second);
}
void CSVWorld::DataDisplayDelegate::updateUserSetting (const QString &name,

@ -30,9 +30,8 @@ namespace CSVWorld
private:
std::vector <std::pair <int, QPixmap> > mPixmaps;
QTextOption mTextAlignment;
QSize mIconSize;
int mIconLeftOffset;
int mHorizontalMargin;
int mTextLeftOffset;
QString mSettingKey;
@ -46,12 +45,11 @@ namespace CSVWorld
virtual void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
/// pass a QSize defining height / width of icon. Default is QSize (16,16).
void setIconSize (const QSize& icon);
/// offset the horizontal position of the icon from the left edge of the cell. Default is 3 pixels.
void setIconLeftOffset (int offset);
/// offset the horizontal position of the text from the right edge of the icon. Default is 8 pixels.
void setTextLeftOffset (int offset);

@ -469,8 +469,7 @@ void CSVWorld::EditWidget::remake(int row)
mTable->data (mTable->index (row, idColumn)).toString().toUtf8().constData());
NestedTable* table = new NestedTable(mDocument, id, mNestedModels.back(), this);
// FIXME: does not work well when enum delegates are used
//table->resizeColumnsToContents();
table->resizeColumnsToContents();
if(mTable->index(row, i).data().type() == QVariant::UserType)
{

@ -10,6 +10,24 @@
#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,
const QModelIndex& index) const
{
@ -67,54 +85,59 @@ QWidget *CSVWorld::EnumDelegate::createEditor(QWidget *parent, const QStyleOptio
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);
if (tryDisplay && !data.isValid())
int role = Qt::EditRole;
if (tryDisplay && !index.data(role).isValid())
{
data = index.data (Qt::DisplayRole);
if (!data.isValid())
role = Qt::DisplayRole;
if (!index.data(role).isValid())
{
return;
}
}
int value = data.toInt();
std::size_t size = mValues.size();
for (std::size_t i=0; i<size; ++i)
if (mValues[i].first==value)
{
comboBox->setCurrentIndex (i);
break;
}
int valueIndex = getValueIndex(index, role);
if (valueIndex != -1)
{
comboBox->setCurrentIndex(valueIndex);
}
}
}
void CSVWorld::EnumDelegate::paint (QPainter *painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
if (index.data().isValid())
int valueIndex = getValueIndex(index);
if (valueIndex != -1)
{
QStyleOptionViewItemV4 option2 (option);
int value = index.data().toInt();
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;
}
QStyleOptionViewItemV4 itemOption(option);
itemOption.text = mValues.at(valueIndex).second;
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &itemOption, painter);
}
}
QSize CSVWorld::EnumDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
int valueIndex = getValueIndex(index);
if (valueIndex != -1)
{
// Calculate the size hint as for a combobox.
// So, the whole text is visible (isn't elided) when the editor is created
QStyleOptionComboBox itemOption;
itemOption.fontMetrics = option.fontMetrics;
itemOption.palette = option.palette;
itemOption.rect = option.rect;
itemOption.state = option.state;
const QString &valueText = mValues.at(valueIndex).second;
QSize valueSize = QSize(itemOption.fontMetrics.width(valueText), itemOption.fontMetrics.height());
itemOption.currentText = valueText;
return QApplication::style()->sizeFromContents(QStyle::CT_ComboBox, &itemOption, valueSize);
}
return option.rect.size();
}
CSVWorld::EnumDelegateFactory::EnumDelegateFactory() {}

@ -19,6 +19,8 @@ namespace CSVWorld
std::vector<std::pair<int, QString> > mValues;
int getValueIndex(const QModelIndex &index, int role = Qt::DisplayRole) const;
private:
virtual void setModelDataImp (QWidget *editor, QAbstractItemModel *model,
@ -46,6 +48,8 @@ namespace CSVWorld
virtual void paint (QPainter *painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const;
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
class EnumDelegateFactory : public CommandDelegateFactory

Loading…
Cancel
Save