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"
22 #include <KIconLoader>
23 #include "kstandarditemlistwidget.h"
25 KStandardItemListView::KStandardItemListView(QGraphicsWidget
* parent
) :
26 KItemListView(parent
),
27 m_itemLayout(DetailsLayout
)
30 setScrollOrientation(Qt::Vertical
);
31 setVisibleRoles({"text"});
34 KStandardItemListView::~KStandardItemListView()
38 void KStandardItemListView::setItemLayout(ItemLayout layout
)
40 if (m_itemLayout
== layout
) {
46 const ItemLayout previous
= m_itemLayout
;
47 m_itemLayout
= layout
;
49 setSupportsItemExpanding(itemLayoutSupportsItemExpanding(layout
));
50 setScrollOrientation(layout
== CompactLayout
? Qt::Horizontal
: Qt::Vertical
);
52 onItemLayoutChanged(layout
, previous
);
57 KStandardItemListView::ItemLayout
KStandardItemListView::itemLayout() const
62 KItemListWidgetCreatorBase
* KStandardItemListView::defaultWidgetCreator() const
64 return new KItemListWidgetCreator
<KStandardItemListWidget
>();
67 KItemListGroupHeaderCreatorBase
* KStandardItemListView::defaultGroupHeaderCreator() const
69 return new KItemListGroupHeaderCreator
<KStandardItemListGroupHeader
>();
72 void KStandardItemListView::initializeItemListWidget(KItemListWidget
* item
)
74 KStandardItemListWidget
* standardItemListWidget
= qobject_cast
<KStandardItemListWidget
*>(item
);
75 Q_ASSERT(standardItemListWidget
);
77 switch (itemLayout()) {
78 case IconsLayout
: standardItemListWidget
->setLayout(KStandardItemListWidget::IconsLayout
); break;
79 case CompactLayout
: standardItemListWidget
->setLayout(KStandardItemListWidget::CompactLayout
); break;
80 case DetailsLayout
: standardItemListWidget
->setLayout(KStandardItemListWidget::DetailsLayout
); break;
81 default: Q_ASSERT(false); break;
84 standardItemListWidget
->setSupportsItemExpanding(supportsItemExpanding());
88 bool KStandardItemListView::itemSizeHintUpdateRequired(const QSet
<QByteArray
>& changedRoles
) const
90 // The only thing that can modify the item's size hint is the amount of space
91 // needed to display the text for the visible roles.
92 // Even if the icons have a different size they are always aligned within
93 // the area defined by KItemStyleOption.iconSize and hence result in no
94 // change of the item-size.
95 foreach (const QByteArray
& role
, visibleRoles()) {
96 if (changedRoles
.contains(role
)) {
103 bool KStandardItemListView::itemLayoutSupportsItemExpanding(ItemLayout layout
) const
105 return layout
== DetailsLayout
;
108 void KStandardItemListView::onItemLayoutChanged(ItemLayout current
, ItemLayout previous
)
112 updateLayoutOfVisibleItems();
115 void KStandardItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
119 updateLayoutOfVisibleItems();
122 void KStandardItemListView::onSupportsItemExpandingChanged(bool supportsExpanding
)
124 Q_UNUSED(supportsExpanding
);
125 updateLayoutOfVisibleItems();
129 void KStandardItemListView::polishEvent()
131 switch (m_itemLayout
) {
132 case IconsLayout
: applyDefaultStyleOption(style()->pixelMetric(QStyle::PM_LargeIconSize
), 2, 4, 8); break;
133 case CompactLayout
: applyDefaultStyleOption(style()->pixelMetric(QStyle::PM_SmallIconSize
), 2, 8, 0); break;
134 case DetailsLayout
: applyDefaultStyleOption(style()->pixelMetric(QStyle::PM_SmallIconSize
), 2, 0, 0); break;
135 default: Q_ASSERT(false); break;
138 QGraphicsWidget::polishEvent();
141 void KStandardItemListView::applyDefaultStyleOption(int iconSize
,
143 int horizontalMargin
,
146 KItemListStyleOption option
= styleOption();
148 bool changed
= false;
149 if (option
.iconSize
< 0) {
150 option
.iconSize
= iconSize
;
153 if (option
.padding
< 0) {
154 option
.padding
= padding
;
157 if (option
.horizontalMargin
< 0) {
158 option
.horizontalMargin
= horizontalMargin
;
161 if (option
.verticalMargin
< 0) {
162 option
.verticalMargin
= verticalMargin
;
167 setStyleOption(option
);
171 void KStandardItemListView::updateLayoutOfVisibleItems()
174 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
175 initializeItemListWidget(widget
);