]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
use a transparent background to let the users eye focus on the folder content (e...
[dolphin.git] / src / dolphincolumnview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
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 "dolphincolumnview.h"
21
22 #include "dolphincontroller.h"
23 #include "dolphinsettings.h"
24
25 #include "dolphin_columnmodesettings.h"
26
27 #include <QtGui/QAbstractProxyModel>
28 #include <QtCore/QPoint>
29
30 /**
31 * Represents one column inside the DolphinColumnView and has been
32 * extended to respect view options and hovering information.
33 */
34 class ColumnWidget : public QListView
35 {
36 public:
37 ColumnWidget(QWidget* parent, DolphinColumnView* columnView);
38 virtual ~ColumnWidget();
39
40 void setDecorationSize(const QSize& size);
41
42 protected:
43 virtual QStyleOptionViewItem viewOptions() const;
44 virtual void dragEnterEvent(QDragEnterEvent* event);
45 virtual void dragLeaveEvent(QDragLeaveEvent* event);
46 virtual void dragMoveEvent(QDragMoveEvent* event);
47 virtual void dropEvent(QDropEvent* event);
48 virtual void paintEvent(QPaintEvent* event);
49
50 private:
51 DolphinColumnView* m_columnView;
52 QStyleOptionViewItem m_viewOptions;
53
54 bool m_dragging; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
55 QRect m_dropRect; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
56 };
57
58 ColumnWidget::ColumnWidget(QWidget* parent, DolphinColumnView* columnView) :
59 QListView(parent),
60 m_columnView(columnView),
61 m_dragging(false),
62 m_dropRect()
63 {
64 setAcceptDrops(true);
65 setSelectionBehavior(SelectItems);
66 setDragDropMode(QAbstractItemView::DragDrop);
67 setDropIndicatorShown(false);
68
69 setMouseTracking(true);
70 viewport()->setAttribute(Qt::WA_Hover);
71
72 // apply the column mode settings to the widget
73 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
74 Q_ASSERT(settings != 0);
75
76 m_viewOptions = QListView::viewOptions();
77
78 QFont font(settings->fontFamily(), settings->fontSize());
79 font.setItalic(settings->italicFont());
80 font.setBold(settings->boldFont());
81 m_viewOptions.font = font;
82 }
83
84 ColumnWidget::~ColumnWidget()
85 {
86 }
87
88 void ColumnWidget::setDecorationSize(const QSize& size)
89 {
90 m_viewOptions.decorationSize = size;
91 doItemsLayout();
92 }
93
94 QStyleOptionViewItem ColumnWidget::viewOptions() const
95 {
96 return m_viewOptions;
97 }
98
99 void ColumnWidget::dragEnterEvent(QDragEnterEvent* event)
100 {
101 if (event->mimeData()->hasUrls()) {
102 event->acceptProposedAction();
103 }
104
105 m_dragging = true;
106 }
107
108 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
109 {
110 QListView::dragLeaveEvent(event);
111
112 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
113 m_dragging = false;
114 setDirtyRegion(m_dropRect);
115 }
116
117 void ColumnWidget::dragMoveEvent(QDragMoveEvent* event)
118 {
119 QListView::dragMoveEvent(event);
120
121 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
122 const QModelIndex index = indexAt(event->pos());
123 setDirtyRegion(m_dropRect);
124 m_dropRect = visualRect(index);
125 setDirtyRegion(m_dropRect);
126 }
127
128 void ColumnWidget::dropEvent(QDropEvent* event)
129 {
130 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
131 if (!urls.isEmpty()) {
132 event->acceptProposedAction();
133 m_columnView->m_controller->indicateDroppedUrls(urls,
134 indexAt(event->pos()),
135 event->source());
136 }
137 QListView::dropEvent(event);
138 m_dragging = false;
139 }
140
141 void ColumnWidget::paintEvent(QPaintEvent* event)
142 {
143 QListView::paintEvent(event);
144
145 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
146 if (m_dragging) {
147 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
148 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
149 }
150 }
151
152 // ---
153
154 DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
155 QColumnView(parent),
156 m_controller(controller)
157 {
158 Q_ASSERT(controller != 0);
159
160 setAcceptDrops(true);
161 setSelectionBehavior(SelectItems);
162 setDragDropMode(QAbstractItemView::DragDrop);
163 setDropIndicatorShown(false);
164
165 if (KGlobalSettings::singleClick()) {
166 connect(this, SIGNAL(clicked(const QModelIndex&)),
167 controller, SLOT(triggerItem(const QModelIndex&)));
168 } else {
169 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
170 controller, SLOT(triggerItem(const QModelIndex&)));
171 }
172 connect(this, SIGNAL(activated(const QModelIndex&)),
173 controller, SLOT(triggerItem(const QModelIndex&)));
174 connect(this, SIGNAL(entered(const QModelIndex&)),
175 controller, SLOT(emitItemEntered(const QModelIndex&)));
176 connect(this, SIGNAL(viewportEntered()),
177 controller, SLOT(emitViewportEntered()));
178 connect(controller, SIGNAL(zoomIn()),
179 this, SLOT(zoomIn()));
180 connect(controller, SIGNAL(zoomOut()),
181 this, SLOT(zoomOut()));
182
183 updateDecorationSize();
184 }
185
186 DolphinColumnView::~DolphinColumnView()
187 {
188 }
189
190 QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
191 {
192 ColumnWidget* view = new ColumnWidget(viewport(), this);
193
194 // The following code has been copied 1:1 from QColumnView::createColumn().
195 // Copyright (C) 1992-2007 Trolltech ASA.
196 // It would be nice if QColumnView would offer a protected method for this
197 // (already send input to Benjamin, hopefully possible in Qt4.4)
198
199 view->setFrameShape(QFrame::NoFrame);
200 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
201 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
202 view->setMinimumWidth(100);
203 view->setAttribute(Qt::WA_MacShowFocusRect, false);
204
205 // copy the 'view' behavior
206 view->setDragDropMode(dragDropMode());
207 view->setDragDropOverwriteMode(dragDropOverwriteMode());
208 view->setDropIndicatorShown(showDropIndicator());
209 view->setAlternatingRowColors(alternatingRowColors());
210 view->setAutoScroll(hasAutoScroll());
211 view->setEditTriggers(editTriggers());
212 view->setHorizontalScrollMode(horizontalScrollMode());
213 view->setIconSize(iconSize());
214 view->setSelectionBehavior(selectionBehavior());
215 view->setSelectionMode(selectionMode());
216 view->setTabKeyNavigation(tabKeyNavigation());
217 view->setTextElideMode(textElideMode());
218 view->setVerticalScrollMode(verticalScrollMode());
219
220 view->setModel(model());
221
222 // set the delegate to be the columnview delegate
223 QAbstractItemDelegate *delegate = view->itemDelegate();
224 view->setItemDelegate(itemDelegate());
225 delete delegate;
226
227 view->setRootIndex(index);
228
229 if (model()->canFetchMore(index)) {
230 model()->fetchMore(index);
231 }
232
233 return view;
234 }
235
236 void DolphinColumnView::contextMenuEvent(QContextMenuEvent* event)
237 {
238 QColumnView::contextMenuEvent(event);
239 m_controller->triggerContextMenuRequest(event->pos());
240 }
241
242 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
243 {
244 m_controller->triggerActivation();
245 QColumnView::mousePressEvent(event);
246 }
247
248 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
249 {
250 if (event->mimeData()->hasUrls()) {
251 event->acceptProposedAction();
252 }
253 }
254
255 void DolphinColumnView::dropEvent(QDropEvent* event)
256 {
257 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
258 if (!urls.isEmpty()) {
259 m_controller->indicateDroppedUrls(urls,
260 indexAt(event->pos()),
261 event->source());
262 event->acceptProposedAction();
263 }
264 QColumnView::dropEvent(event);
265 }
266
267 void DolphinColumnView::zoomIn()
268 {
269 if (isZoomInPossible()) {
270 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
271 // TODO: get rid of K3Icon sizes
272 switch (settings->iconSize()) {
273 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
274 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
275 default: Q_ASSERT(false); break;
276 }
277 updateDecorationSize();
278 }
279 }
280
281 void DolphinColumnView::zoomOut()
282 {
283 if (isZoomOutPossible()) {
284 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
285 // TODO: get rid of K3Icon sizes
286 switch (settings->iconSize()) {
287 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
288 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
289 default: Q_ASSERT(false); break;
290 }
291 updateDecorationSize();
292 }
293 }
294
295 bool DolphinColumnView::isZoomInPossible() const
296 {
297 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
298 return settings->iconSize() < K3Icon::SizeLarge;
299 }
300
301 bool DolphinColumnView::isZoomOutPossible() const
302 {
303 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
304 return settings->iconSize() > K3Icon::SizeSmall;
305 }
306
307 void DolphinColumnView::updateDecorationSize()
308 {
309 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
310 const int iconSize = settings->iconSize();
311
312 foreach (QObject* object, viewport()->children()) {
313 if (object->inherits("QListView")) {
314 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
315 widget->setDecorationSize(QSize(iconSize, iconSize));
316 }
317 }
318
319 m_controller->setZoomInPossible(isZoomInPossible());
320 m_controller->setZoomOutPossible(isZoomOutPossible());
321
322 doItemsLayout();
323 }
324
325 #include "dolphincolumnview.moc"