1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "kstandarditemlistview.h"
23 #include <KIconLoader>
24 #include "kstandarditemlistwidget.h"
25 #include "kstandarditemlistgroupheader.h"
27 KStandardItemListView::KStandardItemListView(QGraphicsWidget
* parent
) :
28 KItemListView(parent
),
29 m_itemLayout(DetailsLayout
)
32 setScrollOrientation(Qt::Vertical
);
33 setVisibleRoles(QList
<QByteArray
>() << "text");
36 KStandardItemListView::~KStandardItemListView()
40 void KStandardItemListView::setItemLayout(ItemLayout layout
)
42 if (m_itemLayout
== layout
) {
48 const ItemLayout previous
= m_itemLayout
;
49 m_itemLayout
= layout
;
51 setSupportsItemExpanding(itemLayoutSupportsItemExpanding(layout
));
52 setScrollOrientation(layout
== CompactLayout
? Qt::Horizontal
: Qt::Vertical
);
54 onItemLayoutChanged(layout
, previous
);
59 KStandardItemListView::ItemLayout
KStandardItemListView::itemLayout() const
64 KItemListWidgetCreatorBase
* KStandardItemListView::defaultWidgetCreator() const
66 return new KItemListWidgetCreator
<KStandardItemListWidget
>();
69 KItemListGroupHeaderCreatorBase
* KStandardItemListView::defaultGroupHeaderCreator() const
71 return new KItemListGroupHeaderCreator
<KStandardItemListGroupHeader
>();
74 void KStandardItemListView::initializeItemListWidget(KItemListWidget
* item
)
76 KStandardItemListWidget
* standardItemListWidget
= qobject_cast
<KStandardItemListWidget
*>(item
);
77 Q_ASSERT(standardItemListWidget
);
79 switch (itemLayout()) {
80 case IconsLayout
: standardItemListWidget
->setLayout(KStandardItemListWidget::IconsLayout
); break;
81 case CompactLayout
: standardItemListWidget
->setLayout(KStandardItemListWidget::CompactLayout
); break;
82 case DetailsLayout
: standardItemListWidget
->setLayout(KStandardItemListWidget::DetailsLayout
); break;
83 default: Q_ASSERT(false); break;
86 standardItemListWidget
->setSupportsItemExpanding(supportsItemExpanding());
90 bool KStandardItemListView::itemSizeHintUpdateRequired(const QSet
<QByteArray
>& changedRoles
) const
92 // The only thing that can modify the item's size hint is the amount of space
93 // needed to display the text for the visible roles.
94 // Even if the icons have a different size they are always aligned within
95 // the area defined by KItemStyleOption.iconSize and hence result in no
96 // change of the item-size.
97 foreach (const QByteArray
& role
, visibleRoles()) {
98 if (changedRoles
.contains(role
)) {
105 bool KStandardItemListView::itemLayoutSupportsItemExpanding(ItemLayout layout
) const
107 return layout
== DetailsLayout
;
110 void KStandardItemListView::onItemLayoutChanged(ItemLayout current
, ItemLayout previous
)
114 updateLayoutOfVisibleItems();
117 void KStandardItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
121 updateLayoutOfVisibleItems();
124 void KStandardItemListView::onSupportsItemExpandingChanged(bool supportsExpanding
)
126 Q_UNUSED(supportsExpanding
);
127 updateLayoutOfVisibleItems();
131 void KStandardItemListView::polishEvent()
133 switch (m_itemLayout
) {
134 case IconsLayout
: applyDefaultStyleOption(style()->pixelMetric(QStyle::PM_LargeIconSize
), 2, 4, 8); break;
135 case CompactLayout
: applyDefaultStyleOption(style()->pixelMetric(QStyle::PM_SmallIconSize
), 2, 8, 0); break;
136 case DetailsLayout
: applyDefaultStyleOption(style()->pixelMetric(QStyle::PM_SmallIconSize
), 2, 0, 0); break;
137 default: Q_ASSERT(false); break;
140 QGraphicsWidget::polishEvent();
143 void KStandardItemListView::applyDefaultStyleOption(int iconSize
,
145 int horizontalMargin
,
148 KItemListStyleOption option
= styleOption();
150 bool changed
= false;
151 if (option
.iconSize
< 0) {
152 option
.iconSize
= iconSize
;
155 if (option
.padding
< 0) {
156 option
.padding
= padding
;
159 if (option
.horizontalMargin
< 0) {
160 option
.horizontalMargin
= horizontalMargin
;
163 if (option
.verticalMargin
< 0) {
164 option
.verticalMargin
= verticalMargin
;
169 setStyleOption(option
);
173 void KStandardItemListView::updateLayoutOfVisibleItems()
176 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
177 initializeItemListWidget(widget
);
182 #include "kstandarditemlistview.moc"