diff options
| author | 2016-06-13 18:42:42 -0400 | |
|---|---|---|
| committer | 2016-06-13 18:42:42 -0400 | |
| commit | 5d9314f134ddd3dc4c853e398ac90ba247fb2e4f (patch) | |
| tree | 5c457fc188036988d7abd29a3eb09931e406510f /SQLiteStudio3/guiSQLiteStudio/common/centerediconitemdelegate.cpp | |
| parent | 8e640722c62692818ab840d50b3758f89a41a54e (diff) | |
Imported Upstream version 3.1.0upstream/3.1.0
Diffstat (limited to 'SQLiteStudio3/guiSQLiteStudio/common/centerediconitemdelegate.cpp')
| -rw-r--r-- | SQLiteStudio3/guiSQLiteStudio/common/centerediconitemdelegate.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/SQLiteStudio3/guiSQLiteStudio/common/centerediconitemdelegate.cpp b/SQLiteStudio3/guiSQLiteStudio/common/centerediconitemdelegate.cpp new file mode 100644 index 0000000..83d8cc3 --- /dev/null +++ b/SQLiteStudio3/guiSQLiteStudio/common/centerediconitemdelegate.cpp @@ -0,0 +1,35 @@ +#include "centerediconitemdelegate.h" +#include <QApplication> +#include <QPainter> + +CenteredIconItemDelegate::CenteredIconItemDelegate(QObject* parent) : + QStyledItemDelegate(parent) +{ +} + +void CenteredIconItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + if (!index.isValid()) + return; + + QStyleOptionViewItem opt = option; + initStyleOption(&opt, index); + + // disable default icon + opt.icon = QIcon(); + if (opt.features.testFlag(QStyleOptionViewItem::HasDecoration)) + opt.features ^= QStyleOptionViewItem::HasDecoration; + + // draw default item + QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, 0); + + const QRect r = option.rect; + + // get pixmap + QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole)); + QPixmap pix = icon.pixmap(r.size()); + + // draw pixmap at center of item + const QPoint p = QPoint((r.width() - pix.width()) / 2, (r.height() - pix.height()) / 2); + painter->drawPixmap(r.topLeft() + p, pix); +} |
