Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / editors / sc-ide / widgets / util / icon_list_widget.hpp
blob30a7a58c7c3a56091295042c88a53ee7b4e2977c
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef SCIDE_WIDGETS_UTIL_ICON_LIST_WIDGET_HPP_INCLUDED
22 #define SCIDE_WIDGETS_UTIL_ICON_LIST_WIDGET_HPP_INCLUDED
24 #include <QListWidget>
25 #include <QStyledItemDelegate>
26 #include <QPainter>
28 namespace ScIDE {
30 class IconListWidget : public QListWidget
32 public:
33 class ItemDelegate : public QStyledItemDelegate
35 public:
36 ItemDelegate(QObject *parent) : QStyledItemDelegate(parent) {}
38 virtual QSize sizeHint
39 ( const QStyleOptionViewItem & opt, const QModelIndex & index ) const
41 return QStyledItemDelegate::sizeHint(opt, index) + QSize(0, 10);
44 virtual void paint
45 ( QPainter * painter, const QStyleOptionViewItem & option,
46 const QModelIndex & index ) const
48 painter->save();
50 QStyle *style = QApplication::style();
52 style->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter );
54 QRect r( option.rect.adjusted(0,5,0,-5) );
56 QIcon icon(index.data(Qt::DecorationRole).value<QIcon>());
57 if( !icon.isNull() )
58 style->drawItemPixmap( painter, r, Qt::AlignTop | Qt::AlignHCenter, icon.pixmap(option.decorationSize) );
60 QString text = index.data(Qt::DisplayRole).toString();
61 if( !text.isEmpty() ) {
62 int alignment = !icon.isNull() ? Qt::AlignBottom | Qt::AlignHCenter : Qt::AlignCenter;
63 painter->setFont( option.font );
64 style->drawItemText( painter, r, alignment, option.palette, true, text );
67 painter->restore();
71 public:
72 IconListWidget(QWidget *parent = 0) : QListWidget(parent)
74 setItemDelegate( new ItemDelegate(this) );
77 virtual QStyleOptionViewItem viewOptions() const
79 QStyleOptionViewItem opt( QListWidget::viewOptions() );
80 opt.displayAlignment = Qt::AlignCenter;
81 opt.decorationAlignment = Qt::AlignCenter;
82 opt.decorationPosition = QStyleOptionViewItem::Top;
83 opt.font.setBold(true);
84 return opt;
88 } // namespace ScIDE
90 #endif // SCIDE_WIDGETS_UTIL_ICON_LIST_WIDGET_HPP_INCLUDED