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 "kstandarditemlistwidget.h"
24 #include <KIconLoader>
26 KStandardItemListView::KStandardItemListView(QGraphicsWidget
* parent
) :
27 KItemListView(parent
),
28 m_itemLayout(DetailsLayout
)
31 setScrollOrientation(Qt::Vertical
);
32 setVisibleRoles({"text"});
35 KStandardItemListView::~KStandardItemListView()
39 void KStandardItemListView::setItemLayout(ItemLayout layout
)
41 if (m_itemLayout
== layout
) {
47 const ItemLayout previous
= m_itemLayout
;
48 m_itemLayout
= layout
;
50 setSupportsItemExpanding(itemLayoutSupportsItemExpanding(layout
));
51 setScrollOrientation(layout
== CompactLayout
? Qt::Horizontal
: Qt::Vertical
);
53 onItemLayoutChanged(layout
, previous
);
58 KStandardItemListView::ItemLayout
KStandardItemListView::itemLayout() const
63 KItemListWidgetCreatorBase
* KStandardItemListView::defaultWidgetCreator() const
65 return new KItemListWidgetCreator
<KStandardItemListWidget
>();
68 KItemListGroupHeaderCreatorBase
* KStandardItemListView::defaultGroupHeaderCreator() const
70 return new KItemListGroupHeaderCreator
<KStandardItemListGroupHeader
>();
73 void KStandardItemListView::initializeItemListWidget(KItemListWidget
* item
)
75 KStandardItemListWidget
* standardItemListWidget
= qobject_cast
<KStandardItemListWidget
*>(item
);
76 Q_ASSERT(standardItemListWidget
);
78 switch (itemLayout()) {
79 case IconsLayout
: standardItemListWidget
->setLayout(KStandardItemListWidget::IconsLayout
); break;
80 case CompactLayout
: standardItemListWidget
->setLayout(KStandardItemListWidget::CompactLayout
); break;
81 case DetailsLayout
: standardItemListWidget
->setLayout(KStandardItemListWidget::DetailsLayout
); break;
82 default: Q_ASSERT(false); break;
85 standardItemListWidget
->setSupportsItemExpanding(supportsItemExpanding());
89 bool KStandardItemListView::itemSizeHintUpdateRequired(const QSet
<QByteArray
>& changedRoles
) const
91 // The only thing that can modify the item's size hint is the amount of space
92 // needed to display the text for the visible roles.
93 // Even if the icons have a different size they are always aligned within
94 // the area defined by KItemStyleOption.iconSize and hence result in no
95 // change of the item-size.
96 foreach (const QByteArray
& role
, visibleRoles()) {
97 if (changedRoles
.contains(role
)) {
104 bool KStandardItemListView::itemLayoutSupportsItemExpanding(ItemLayout layout
) const
106 return layout
== DetailsLayout
;
109 void KStandardItemListView::onItemLayoutChanged(ItemLayout current
, ItemLayout previous
)
113 updateLayoutOfVisibleItems();
116 void KStandardItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
120 updateLayoutOfVisibleItems();
123 void KStandardItemListView::onSupportsItemExpandingChanged(bool supportsExpanding
)
125 Q_UNUSED(supportsExpanding
);
126 updateLayoutOfVisibleItems();
130 void KStandardItemListView::polishEvent()
132 switch (m_itemLayout
) {
133 case IconsLayout
: applyDefaultStyleOption(style()->pixelMetric(QStyle::PM_LargeIconSize
), 2, 4, 8); break;
134 case CompactLayout
: applyDefaultStyleOption(style()->pixelMetric(QStyle::PM_SmallIconSize
), 2, 8, 0); break;
135 case DetailsLayout
: applyDefaultStyleOption(style()->pixelMetric(QStyle::PM_SmallIconSize
), 2, 0, 0); break;
136 default: Q_ASSERT(false); break;
139 QGraphicsWidget::polishEvent();
142 void KStandardItemListView::applyDefaultStyleOption(int iconSize
,
144 int horizontalMargin
,
147 KItemListStyleOption option
= styleOption();
149 if (option
.iconSize
< 0) {
150 option
.iconSize
= iconSize
;
152 if (option
.padding
< 0) {
153 option
.padding
= padding
;
155 if (option
.horizontalMargin
< 0) {
156 option
.horizontalMargin
= horizontalMargin
;
158 if (option
.verticalMargin
< 0) {
159 option
.verticalMargin
= verticalMargin
;
162 setStyleOption(option
);
165 void KStandardItemListView::updateLayoutOfVisibleItems()
168 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
169 initializeItemListWidget(widget
);