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
;
53 setScrollOrientation(Qt::Vertical
);
54 setSupportsItemExpanding(false);
57 setScrollOrientation(Qt::Vertical
);
58 setSupportsItemExpanding(true);
61 setScrollOrientation(Qt::Horizontal
);
62 setSupportsItemExpanding(false);
69 onItemLayoutChanged(layout
, previous
);
74 KStandardItemListView::ItemLayout
KStandardItemListView::itemLayout() const
79 KItemListWidgetCreatorBase
* KStandardItemListView::defaultWidgetCreator() const
81 return new KItemListWidgetCreator
<KStandardItemListWidget
>();
84 KItemListGroupHeaderCreatorBase
* KStandardItemListView::defaultGroupHeaderCreator() const
86 return new KItemListGroupHeaderCreator
<KStandardItemListGroupHeader
>();
89 void KStandardItemListView::initializeItemListWidget(KItemListWidget
* item
)
91 KStandardItemListWidget
* standardItemListWidget
= qobject_cast
<KStandardItemListWidget
*>(item
);
92 Q_ASSERT(standardItemListWidget
);
94 switch (itemLayout()) {
95 case IconsLayout
: standardItemListWidget
->setLayout(KStandardItemListWidget::IconsLayout
); break;
96 case CompactLayout
: standardItemListWidget
->setLayout(KStandardItemListWidget::CompactLayout
); break;
97 case DetailsLayout
: standardItemListWidget
->setLayout(KStandardItemListWidget::DetailsLayout
); break;
98 default: Q_ASSERT(false); break;
101 standardItemListWidget
->setSupportsItemExpanding(supportsItemExpanding());
105 bool KStandardItemListView::itemSizeHintUpdateRequired(const QSet
<QByteArray
>& changedRoles
) const
107 // Even if the icons have a different size they are always aligned within
108 // the area defined by KItemStyleOption.iconSize and hence result in no
109 // change of the item-size.
110 const bool containsIconName
= changedRoles
.contains("iconName");
111 const bool containsIconPixmap
= changedRoles
.contains("iconPixmap");
112 const int count
= changedRoles
.count();
114 const bool iconChanged
= (containsIconName
&& containsIconPixmap
&& count
== 2) ||
115 (containsIconName
&& count
== 1) ||
116 (containsIconPixmap
&& count
== 1);
120 void KStandardItemListView::onItemLayoutChanged(ItemLayout current
, ItemLayout previous
)
124 updateLayoutOfVisibleItems();
127 void KStandardItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
131 updateLayoutOfVisibleItems();
134 void KStandardItemListView::onSupportsItemExpandingChanged(bool supportsExpanding
)
136 Q_UNUSED(supportsExpanding
);
137 updateLayoutOfVisibleItems();
141 void KStandardItemListView::polishEvent()
143 switch (m_itemLayout
) {
144 case IconsLayout
: applyDefaultStyleOption(KIconLoader::SizeMedium
, 2, 4, 8); break;
145 case CompactLayout
: applyDefaultStyleOption(KIconLoader::SizeSmall
, 2, 8, 0); break;
146 case DetailsLayout
: applyDefaultStyleOption(KIconLoader::SizeSmall
, 2, 0, 0); break;
147 default: Q_ASSERT(false); break;
150 QGraphicsWidget::polishEvent();
153 void KStandardItemListView::applyDefaultStyleOption(int iconSize
,
155 int horizontalMargin
,
158 KItemListStyleOption option
= styleOption();
160 bool changed
= false;
161 if (option
.iconSize
< 0) {
162 option
.iconSize
= iconSize
;
165 if (option
.padding
< 0) {
166 option
.padding
= padding
;
169 if (option
.horizontalMargin
< 0) {
170 option
.horizontalMargin
= horizontalMargin
;
173 if (option
.verticalMargin
< 0) {
174 option
.verticalMargin
= verticalMargin
;
179 setStyleOption(option
);
183 void KStandardItemListView::updateLayoutOfVisibleItems()
186 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
187 initializeItemListWidget(widget
);
192 #include "kstandarditemlistview.moc"