]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistview.cpp
Remove unused #include
[dolphin.git] / src / kitemviews / kstandarditemlistview.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "kstandarditemlistview.h"
21
22 #include <KIconLoader>
23 #include "kstandarditemlistwidget.h"
24
25 KStandardItemListView::KStandardItemListView(QGraphicsWidget* parent) :
26 KItemListView(parent),
27 m_itemLayout(DetailsLayout)
28 {
29 setAcceptDrops(true);
30 setScrollOrientation(Qt::Vertical);
31 setVisibleRoles({"text"});
32 }
33
34 KStandardItemListView::~KStandardItemListView()
35 {
36 }
37
38 void KStandardItemListView::setItemLayout(ItemLayout layout)
39 {
40 if (m_itemLayout == layout) {
41 return;
42 }
43
44 beginTransaction();
45
46 const ItemLayout previous = m_itemLayout;
47 m_itemLayout = layout;
48
49 setSupportsItemExpanding(itemLayoutSupportsItemExpanding(layout));
50 setScrollOrientation(layout == CompactLayout ? Qt::Horizontal : Qt::Vertical);
51
52 onItemLayoutChanged(layout, previous);
53
54 endTransaction();
55 }
56
57 KStandardItemListView::ItemLayout KStandardItemListView::itemLayout() const
58 {
59 return m_itemLayout;
60 }
61
62 KItemListWidgetCreatorBase* KStandardItemListView::defaultWidgetCreator() const
63 {
64 return new KItemListWidgetCreator<KStandardItemListWidget>();
65 }
66
67 KItemListGroupHeaderCreatorBase* KStandardItemListView::defaultGroupHeaderCreator() const
68 {
69 return new KItemListGroupHeaderCreator<KStandardItemListGroupHeader>();
70 }
71
72 void KStandardItemListView::initializeItemListWidget(KItemListWidget* item)
73 {
74 KStandardItemListWidget* standardItemListWidget = qobject_cast<KStandardItemListWidget*>(item);
75 Q_ASSERT(standardItemListWidget);
76
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;
82 }
83
84 standardItemListWidget->setSupportsItemExpanding(supportsItemExpanding());
85 }
86
87
88 bool KStandardItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
89 {
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)) {
97 return true;
98 }
99 }
100 return false;
101 }
102
103 bool KStandardItemListView::itemLayoutSupportsItemExpanding(ItemLayout layout) const
104 {
105 return layout == DetailsLayout;
106 }
107
108 void KStandardItemListView::onItemLayoutChanged(ItemLayout current, ItemLayout previous)
109 {
110 Q_UNUSED(current);
111 Q_UNUSED(previous);
112 updateLayoutOfVisibleItems();
113 }
114
115 void KStandardItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
116 {
117 Q_UNUSED(current);
118 Q_UNUSED(previous);
119 updateLayoutOfVisibleItems();
120 }
121
122 void KStandardItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
123 {
124 Q_UNUSED(supportsExpanding);
125 updateLayoutOfVisibleItems();
126 }
127
128
129 void KStandardItemListView::polishEvent()
130 {
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;
136 }
137
138 QGraphicsWidget::polishEvent();
139 }
140
141 void KStandardItemListView::applyDefaultStyleOption(int iconSize,
142 int padding,
143 int horizontalMargin,
144 int verticalMargin)
145 {
146 KItemListStyleOption option = styleOption();
147
148 bool changed = false;
149 if (option.iconSize < 0) {
150 option.iconSize = iconSize;
151 changed = true;
152 }
153 if (option.padding < 0) {
154 option.padding = padding;
155 changed = true;
156 }
157 if (option.horizontalMargin < 0) {
158 option.horizontalMargin = horizontalMargin;
159 changed = true;
160 }
161 if (option.verticalMargin < 0) {
162 option.verticalMargin = verticalMargin;
163 changed = true;
164 }
165
166 if (changed) {
167 setStyleOption(option);
168 }
169 }
170
171 void KStandardItemListView::updateLayoutOfVisibleItems()
172 {
173 if (model()) {
174 foreach (KItemListWidget* widget, visibleItemListWidgets()) {
175 initializeItemListWidget(widget);
176 }
177 }
178 }
179