]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistview.cpp
Provide additional default groups for the Places Panel
[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 <KDebug>
23 #include <KIconLoader>
24 #include "kstandarditemlistwidget.h"
25 #include "kstandarditemlistgroupheader.h"
26
27 KStandardItemListView::KStandardItemListView(QGraphicsWidget* parent) :
28 KItemListView(parent),
29 m_itemLayout(DetailsLayout)
30 {
31 setAcceptDrops(true);
32 setScrollOrientation(Qt::Vertical);
33 setVisibleRoles(QList<QByteArray>() << "text");
34 }
35
36 KStandardItemListView::~KStandardItemListView()
37 {
38 }
39
40 void KStandardItemListView::setItemLayout(ItemLayout layout)
41 {
42 if (m_itemLayout == layout) {
43 return;
44 }
45
46 beginTransaction();
47
48 const ItemLayout previous = m_itemLayout;
49 m_itemLayout = layout;
50
51 switch (layout) {
52 case IconsLayout:
53 setScrollOrientation(Qt::Vertical);
54 setSupportsItemExpanding(false);
55 break;
56 case DetailsLayout:
57 setScrollOrientation(Qt::Vertical);
58 setSupportsItemExpanding(true);
59 break;
60 case CompactLayout:
61 setScrollOrientation(Qt::Horizontal);
62 setSupportsItemExpanding(false);
63 break;
64 default:
65 Q_ASSERT(false);
66 break;
67 }
68
69 onItemLayoutChanged(layout, previous);
70
71 endTransaction();
72 }
73
74 KStandardItemListView::ItemLayout KStandardItemListView::itemLayout() const
75 {
76 return m_itemLayout;
77 }
78
79 KItemListWidgetCreatorBase* KStandardItemListView::defaultWidgetCreator() const
80 {
81 return new KItemListWidgetCreator<KStandardItemListWidget>();
82 }
83
84 KItemListGroupHeaderCreatorBase* KStandardItemListView::defaultGroupHeaderCreator() const
85 {
86 return new KItemListGroupHeaderCreator<KStandardItemListGroupHeader>();
87 }
88
89 void KStandardItemListView::initializeItemListWidget(KItemListWidget* item)
90 {
91 KStandardItemListWidget* standardItemListWidget = qobject_cast<KStandardItemListWidget*>(item);
92 Q_ASSERT(standardItemListWidget);
93
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;
99 }
100
101 standardItemListWidget->setSupportsItemExpanding(supportsItemExpanding());
102 }
103
104
105 bool KStandardItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
106 {
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();
113
114 const bool iconChanged = (containsIconName && containsIconPixmap && count == 2) ||
115 (containsIconName && count == 1) ||
116 (containsIconPixmap && count == 1);
117 return !iconChanged;
118 }
119
120 void KStandardItemListView::onItemLayoutChanged(ItemLayout current, ItemLayout previous)
121 {
122 Q_UNUSED(current);
123 Q_UNUSED(previous);
124 updateLayoutOfVisibleItems();
125 }
126
127 void KStandardItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
128 {
129 Q_UNUSED(current);
130 Q_UNUSED(previous);
131 updateLayoutOfVisibleItems();
132 }
133
134 void KStandardItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
135 {
136 Q_UNUSED(supportsExpanding);
137 updateLayoutOfVisibleItems();
138 }
139
140
141 void KStandardItemListView::polishEvent()
142 {
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;
148 }
149
150 QGraphicsWidget::polishEvent();
151 }
152
153 void KStandardItemListView::applyDefaultStyleOption(int iconSize,
154 int padding,
155 int horizontalMargin,
156 int verticalMargin)
157 {
158 KItemListStyleOption option = styleOption();
159
160 bool changed = false;
161 if (option.iconSize < 0) {
162 option.iconSize = iconSize;
163 changed = true;
164 }
165 if (option.padding < 0) {
166 option.padding = padding;
167 changed = true;
168 }
169 if (option.horizontalMargin < 0) {
170 option.horizontalMargin = horizontalMargin;
171 changed = true;
172 }
173 if (option.verticalMargin < 0) {
174 option.verticalMargin = verticalMargin;
175 changed = true;
176 }
177
178 if (changed) {
179 setStyleOption(option);
180 }
181 }
182
183 void KStandardItemListView::updateLayoutOfVisibleItems()
184 {
185 if (model()) {
186 foreach (KItemListWidget* widget, visibleItemListWidgets()) {
187 initializeItemListWidget(widget);
188 }
189 }
190 }
191
192 #include "kstandarditemlistview.moc"