forked from teamnwah/openmw-tes3coop
Proper size hint for DataDisplayDelegate
This commit is contained in:
parent
2c1ca33a20
commit
6cfee630aa
2 changed files with 33 additions and 13 deletions
|
@ -12,7 +12,8 @@ CSVWorld::DataDisplayDelegate::DataDisplayDelegate(const ValueList &values,
|
||||||
const QString &settingName,
|
const QString &settingName,
|
||||||
QObject *parent)
|
QObject *parent)
|
||||||
: EnumDelegate (values, dispatcher, document, parent), mDisplayMode (Mode_TextOnly),
|
: 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)
|
mTextLeftOffset(8), mSettingKey (pageName + '/' + settingName)
|
||||||
{
|
{
|
||||||
buildPixmaps();
|
buildPixmaps();
|
||||||
|
@ -43,16 +44,36 @@ void CSVWorld::DataDisplayDelegate::setIconSize(const QSize& size)
|
||||||
buildPixmaps();
|
buildPixmaps();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSVWorld::DataDisplayDelegate::setIconLeftOffset(int offset)
|
|
||||||
{
|
|
||||||
mIconLeftOffset = offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CSVWorld::DataDisplayDelegate::setTextLeftOffset(int offset)
|
void CSVWorld::DataDisplayDelegate::setTextLeftOffset(int offset)
|
||||||
{
|
{
|
||||||
mTextLeftOffset = offset;
|
mTextLeftOffset = offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QSize CSVWorld::DataDisplayDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||||
|
{
|
||||||
|
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
|
void CSVWorld::DataDisplayDelegate::paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
painter->save();
|
painter->save();
|
||||||
|
@ -77,13 +98,13 @@ void CSVWorld::DataDisplayDelegate::paintIcon (QPainter *painter, const QStyleOp
|
||||||
QRect iconRect = option.rect;
|
QRect iconRect = option.rect;
|
||||||
QRect textRect = iconRect;
|
QRect textRect = iconRect;
|
||||||
|
|
||||||
iconRect.setLeft(iconRect.left() + mIconLeftOffset);
|
iconRect.setLeft(iconRect.left() + mHorizontalMargin);
|
||||||
iconRect.setRight(option.rect.right());
|
iconRect.setRight(option.rect.right() - mHorizontalMargin);
|
||||||
if (mDisplayMode == Mode_IconAndText)
|
if (mDisplayMode == Mode_IconAndText)
|
||||||
{
|
{
|
||||||
iconRect.setWidth(mIconSize.width());
|
iconRect.setWidth(mIconSize.width());
|
||||||
textRect.setLeft(iconRect.right() + mTextLeftOffset);
|
textRect.setLeft(iconRect.right() + mTextLeftOffset);
|
||||||
textRect.setRight(option.rect.right());
|
textRect.setRight(option.rect.right() - mHorizontalMargin);
|
||||||
|
|
||||||
QString text = option.fontMetrics.elidedText(mValues.at(index).second,
|
QString text = option.fontMetrics.elidedText(mValues.at(index).second,
|
||||||
option.textElideMode,
|
option.textElideMode,
|
||||||
|
|
|
@ -31,7 +31,7 @@ namespace CSVWorld
|
||||||
|
|
||||||
std::vector <std::pair <int, QPixmap> > mPixmaps;
|
std::vector <std::pair <int, QPixmap> > mPixmaps;
|
||||||
QSize mIconSize;
|
QSize mIconSize;
|
||||||
int mIconLeftOffset;
|
int mHorizontalMargin;
|
||||||
int mTextLeftOffset;
|
int mTextLeftOffset;
|
||||||
|
|
||||||
QString mSettingKey;
|
QString mSettingKey;
|
||||||
|
@ -45,12 +45,11 @@ namespace CSVWorld
|
||||||
|
|
||||||
virtual void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
|
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).
|
/// pass a QSize defining height / width of icon. Default is QSize (16,16).
|
||||||
void setIconSize (const QSize& icon);
|
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.
|
/// offset the horizontal position of the text from the right edge of the icon. Default is 8 pixels.
|
||||||
void setTextLeftOffset (int offset);
|
void setTextLeftOffset (int offset);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue