]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
* Fix memory leak (DolphinCategoryDrawer is leaking...)
[dolphin.git] / src / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <QApplication>
24 #include <QClipboard>
25 #include <QKeyEvent>
26 #include <QItemSelection>
27 #include <QBoxLayout>
28 #include <QTimer>
29 #include <QScrollBar>
30
31 #include <kcolorscheme.h>
32 #include <kdirlister.h>
33 #include <kfileitemdelegate.h>
34 #include <klocale.h>
35 #include <kiconeffect.h>
36 #include <kio/netaccess.h>
37 #include <kio/renamedialog.h>
38 #include <kio/previewjob.h>
39 #include <kmimetyperesolver.h>
40 #include <konqmimedata.h>
41 #include <konq_operations.h>
42 #include <kurl.h>
43
44 #include "dolphinmodel.h"
45 #include "dolphincolumnview.h"
46 #include "dolphincontroller.h"
47 #include "dolphinsortfilterproxymodel.h"
48 #include "dolphindetailsview.h"
49 #include "dolphiniconsview.h"
50 #include "renamedialog.h"
51 #include "viewproperties.h"
52 #include "dolphinsettings.h"
53 #include "dolphin_generalsettings.h"
54
55 DolphinView::DolphinView(QWidget* parent,
56 const KUrl& url,
57 KDirLister* dirLister,
58 DolphinModel* dolphinModel,
59 DolphinSortFilterProxyModel* proxyModel) :
60 QWidget(parent),
61 m_active(true),
62 m_loadingDirectory(false),
63 m_initializeColumnView(false),
64 m_mode(DolphinView::IconsView),
65 m_topLayout(0),
66 m_controller(0),
67 m_iconsView(0),
68 m_detailsView(0),
69 m_columnView(0),
70 m_fileItemDelegate(0),
71 m_dolphinModel(dolphinModel),
72 m_dirLister(dirLister),
73 m_proxyModel(proxyModel)
74 {
75 setFocusPolicy(Qt::StrongFocus);
76 m_topLayout = new QVBoxLayout(this);
77 m_topLayout->setSpacing(0);
78 m_topLayout->setMargin(0);
79
80 QClipboard* clipboard = QApplication::clipboard();
81 connect(clipboard, SIGNAL(dataChanged()),
82 this, SLOT(updateCutItems()));
83
84 connect(m_dirLister, SIGNAL(completed()),
85 this, SLOT(updateCutItems()));
86 connect(m_dirLister, SIGNAL(newItems(const QList<KFileItem>&)),
87 this, SLOT(generatePreviews(const QList<KFileItem>&)));
88
89 m_controller = new DolphinController(this);
90 m_controller->setUrl(url);
91 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
92 this, SIGNAL(urlChanged(const KUrl&)));
93 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
94 this, SLOT(openContextMenu(const QPoint&)));
95 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&, const QModelIndex&, QWidget*)),
96 this, SLOT(dropUrls(const KUrl::List&, const KUrl&, const QModelIndex&, QWidget*)));
97 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
98 this, SLOT(updateSorting(DolphinView::Sorting)));
99 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
100 this, SLOT(updateSortOrder(Qt::SortOrder)));
101 connect(m_controller, SIGNAL(itemTriggered(const QModelIndex&)),
102 this, SLOT(triggerItem(const QModelIndex&)));
103 connect(m_controller, SIGNAL(activated()),
104 this, SLOT(activate()));
105 connect(m_controller, SIGNAL(itemEntered(const QModelIndex&)),
106 this, SLOT(showHoverInformation(const QModelIndex&)));
107 connect(m_controller, SIGNAL(viewportEntered()),
108 this, SLOT(clearHoverInformation()));
109
110 applyViewProperties(url);
111 m_topLayout->addWidget(itemView());
112 }
113
114 DolphinView::~DolphinView()
115 {
116 }
117
118 const KUrl& DolphinView::url() const
119 {
120 return m_controller->url();
121 }
122
123 KUrl DolphinView::rootUrl() const
124 {
125 return isColumnViewActive() ? m_dirLister->url() : url();
126 }
127
128 void DolphinView::setActive(bool active)
129 {
130 if (active == m_active) {
131 return;
132 }
133
134 m_active = active;
135
136 updateViewportColor();
137 update();
138
139 if (active) {
140 emit activated();
141 }
142 }
143
144 bool DolphinView::isActive() const
145 {
146 return m_active;
147 }
148
149 void DolphinView::setMode(Mode mode)
150 {
151 if (mode == m_mode) {
152 return; // the wished mode is already set
153 }
154
155 m_mode = mode;
156
157 if (isColumnViewActive()) {
158 // When changing the mode in the column view, it makes sense
159 // to go back to the root URL of the column view automatically.
160 // Otherwise there it would not be possible to turn off the column view
161 // without focusing the first column.
162 setUrl(m_dirLister->url());
163 m_controller->setUrl(m_dirLister->url());
164 }
165
166 const KUrl viewPropsUrl = viewPropertiesUrl();
167 ViewProperties props(viewPropsUrl);
168 props.setViewMode(m_mode);
169
170 createView();
171 startDirLister(viewPropsUrl);
172
173 emit modeChanged();
174 }
175
176 DolphinView::Mode DolphinView::mode() const
177 {
178 return m_mode;
179 }
180
181 void DolphinView::setShowPreview(bool show)
182 {
183 const KUrl viewPropsUrl = viewPropertiesUrl();
184 ViewProperties props(viewPropsUrl);
185 props.setShowPreview(show);
186
187 m_controller->setShowPreview(show);
188 emit showPreviewChanged();
189
190 startDirLister(viewPropsUrl, true);
191 }
192
193 bool DolphinView::showPreview() const
194 {
195 return m_controller->showPreview();
196 }
197
198 void DolphinView::setShowHiddenFiles(bool show)
199 {
200 if (m_dirLister->showingDotFiles() == show) {
201 return;
202 }
203
204 const KUrl viewPropsUrl = viewPropertiesUrl();
205 ViewProperties props(viewPropsUrl);
206 props.setShowHiddenFiles(show);
207
208 m_dirLister->setShowingDotFiles(show);
209 emit showHiddenFilesChanged();
210
211 startDirLister(viewPropsUrl, true);
212 }
213
214 bool DolphinView::showHiddenFiles() const
215 {
216 return m_dirLister->showingDotFiles();
217 }
218
219 void DolphinView::setCategorizedSorting(bool categorized)
220 {
221 if (categorized == categorizedSorting()) {
222 return;
223 }
224
225 if (!categorized && !supportsCategorizedSorting())
226 {
227 m_proxyModel->setCategorizedModel(categorized);
228 m_proxyModel->sort(m_proxyModel->sortColumn(), m_proxyModel->sortOrder());
229
230 emit categorizedSortingChanged();
231
232 return;
233 }
234
235 Q_ASSERT(m_iconsView != 0);
236
237 ViewProperties props(viewPropertiesUrl());
238 props.setCategorizedSorting(categorized);
239 props.save();
240
241 m_proxyModel->setCategorizedModel(categorized);
242 m_proxyModel->sort(m_proxyModel->sortColumn(), m_proxyModel->sortOrder());
243
244 emit categorizedSortingChanged();
245 }
246
247 bool DolphinView::categorizedSorting() const
248 {
249 return m_proxyModel->isCategorizedModel();
250 }
251
252 bool DolphinView::supportsCategorizedSorting() const
253 {
254 return m_iconsView != 0;
255 }
256
257 void DolphinView::selectAll()
258 {
259 itemView()->selectAll();
260 }
261
262 void DolphinView::invertSelection()
263 {
264 if (isColumnViewActive()) {
265 // QAbstractItemView does not offer a virtual method invertSelection()
266 // as counterpart to QAbstractItemView::selectAll(). This makes it
267 // necessary to delegate the inverting of the selection to the
268 // column view, as only the selection of the active column should get
269 // inverted.
270 m_columnView->invertSelection();
271 } else {
272 QItemSelectionModel* selectionModel = itemView()->selectionModel();
273 const QAbstractItemModel* itemModel = selectionModel->model();
274
275 const QModelIndex topLeft = itemModel->index(0, 0);
276 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
277 itemModel->columnCount() - 1);
278
279 const QItemSelection selection(topLeft, bottomRight);
280 selectionModel->select(selection, QItemSelectionModel::Toggle);
281 }
282 }
283
284 bool DolphinView::hasSelection() const
285 {
286 return itemView()->selectionModel()->hasSelection();
287 }
288
289 void DolphinView::clearSelection()
290 {
291 itemView()->selectionModel()->clear();
292 }
293
294 QList<KFileItem> DolphinView::selectedItems() const
295 {
296 const QAbstractItemView* view = itemView();
297
298 // Our view has a selection, we will map them back to the DolphinModel
299 // and then fill the KFileItemList.
300 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
301
302 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
303 QList<KFileItem> itemList;
304
305 const QModelIndexList indexList = selection.indexes();
306 foreach (QModelIndex index, indexList) {
307 KFileItem item = m_dolphinModel->itemForIndex(index);
308 if (!item.isNull()) {
309 itemList.append(item);
310 }
311 }
312
313 return itemList;
314 }
315
316 KUrl::List DolphinView::selectedUrls() const
317 {
318 KUrl::List urls;
319 const QList<KFileItem> list = selectedItems();
320 for ( QList<KFileItem>::const_iterator it = list.begin(), end = list.end();
321 it != end; ++it ) {
322 urls.append((*it).url());
323 }
324 return urls;
325 }
326
327 KFileItem DolphinView::fileItem(const QModelIndex& index) const
328 {
329 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
330 return m_dolphinModel->itemForIndex(dolphinModelIndex);
331 }
332
333 void DolphinView::setContentsPosition(int x, int y)
334 {
335 QAbstractItemView* view = itemView();
336
337 // the ColumnView takes care itself for the horizontal scrolling
338 if (!isColumnViewActive()) {
339 view->horizontalScrollBar()->setValue(x);
340 }
341 view->verticalScrollBar()->setValue(y);
342
343 m_loadingDirectory = false;
344 }
345
346 QPoint DolphinView::contentsPosition() const
347 {
348 const int x = itemView()->horizontalScrollBar()->value();
349 const int y = itemView()->verticalScrollBar()->value();
350 return QPoint(x, y);
351 }
352
353 void DolphinView::zoomIn()
354 {
355 m_controller->triggerZoomIn();
356 }
357
358 void DolphinView::zoomOut()
359 {
360 m_controller->triggerZoomOut();
361 }
362
363 bool DolphinView::isZoomInPossible() const
364 {
365 return m_controller->isZoomInPossible();
366 }
367
368 bool DolphinView::isZoomOutPossible() const
369 {
370 return m_controller->isZoomOutPossible();
371 }
372
373 void DolphinView::setSorting(Sorting sorting)
374 {
375 if (sorting != this->sorting()) {
376 updateSorting(sorting);
377 }
378 }
379
380 DolphinView::Sorting DolphinView::sorting() const
381 {
382 return m_proxyModel->sorting();
383 }
384
385 void DolphinView::setSortOrder(Qt::SortOrder order)
386 {
387 if (sortOrder() != order) {
388 updateSortOrder(order);
389 }
390 }
391
392 Qt::SortOrder DolphinView::sortOrder() const
393 {
394 return m_proxyModel->sortOrder();
395 }
396
397 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
398 {
399 const KUrl viewPropsUrl = viewPropertiesUrl();
400 ViewProperties props(viewPropsUrl);
401 props.setAdditionalInfo(info);
402
403 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
404 m_fileItemDelegate->setAdditionalInformation(info);
405
406 emit additionalInfoChanged(info);
407 startDirLister(viewPropsUrl, true);
408 }
409
410 KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
411 {
412 return m_fileItemDelegate->additionalInformation();
413 }
414
415 void DolphinView::reload()
416 {
417 setUrl(url());
418 startDirLister(url(), true);
419 }
420
421 void DolphinView::refresh()
422 {
423 createView();
424 applyViewProperties(m_controller->url());
425 reload();
426 updateViewportColor();
427 }
428
429 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
430 {
431 if (m_controller->url() == url) {
432 return;
433 }
434
435 const bool restoreColumnView = !rootUrl.isEmpty()
436 && !rootUrl.equals(url, KUrl::CompareWithoutTrailingSlash)
437 && rootUrl.isParentOf(url);
438
439 m_controller->setUrl(url); // emits urlChanged, which we forward
440
441 if (restoreColumnView) {
442 applyViewProperties(rootUrl);
443 Q_ASSERT(itemView() == m_columnView);
444 startDirLister(rootUrl);
445 m_columnView->showColumn(url);
446 } else {
447 applyViewProperties(url);
448 startDirLister(url);
449 }
450
451 itemView()->setFocus();
452
453 emit startedPathLoading(url);
454 }
455
456 void DolphinView::setUrl(const KUrl& url)
457 {
458 updateView(url, KUrl());
459 }
460
461 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
462 {
463 QWidget::mouseReleaseEvent(event);
464 setActive(true);
465 }
466 void DolphinView::activate()
467 {
468 setActive(true);
469 }
470
471 void DolphinView::triggerItem(const QModelIndex& index)
472 {
473 Q_ASSERT(index.isValid());
474
475 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
476 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
477 // items are selected by the user, hence don't trigger the
478 // item specified by 'index'
479 return;
480 }
481
482 const KFileItem item = m_dolphinModel->itemForIndex(m_proxyModel->mapToSource(index));
483
484 if (item.isNull()) {
485 return;
486 }
487
488 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
489 }
490
491 void DolphinView::generatePreviews(const QList<KFileItem>& items)
492 {
493 if (m_controller->showPreview()) {
494 KIO::PreviewJob* job = KIO::filePreview(items, 128);
495 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
496 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
497 }
498 }
499
500 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
501 {
502 Q_ASSERT(!item.isNull());
503 if (item.url().directory() != m_dirLister->url().path()) {
504 // the preview job is still working on items of an older URL, hence
505 // the item is not part of the directory model anymore
506 return;
507 }
508
509 const QModelIndex idx = m_dolphinModel->indexForItem(item);
510 if (idx.isValid() && (idx.column() == 0)) {
511 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
512 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
513 KIconEffect iconEffect;
514 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
515 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
516 } else {
517 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
518 }
519 }
520 }
521
522 void DolphinView::emitSelectionChangedSignal()
523 {
524 emit selectionChanged(DolphinView::selectedItems());
525 }
526
527 void DolphinView::startDirLister(const KUrl& url, bool reload)
528 {
529 if (!url.isValid()) {
530 const QString location(url.pathOrUrl());
531 if (location.isEmpty()) {
532 emit errorMessage(i18nc("@info:status", "The location is empty."));
533 } else {
534 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
535 }
536 return;
537 }
538
539 m_cutItemsCache.clear();
540 m_loadingDirectory = true;
541
542 m_dirLister->stop();
543
544 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
545 m_initializeColumnView = false;
546
547 if (keepOldDirs) {
548 // keeping old directories is only necessary for hierarchical views
549 // like the column view
550 if (reload) {
551 // for the column view it is not enough to reload the directory lister,
552 // so this task is delegated to the column view directly
553 m_columnView->reload();
554 } else if (m_dirLister->directories().contains(url)) {
555 // The dir lister contains the directory already, so
556 // KDirLister::openUrl() may not get invoked twice.
557 m_dirLister->updateDirectory(url);
558 } else {
559 const KUrl& dirListerUrl = m_dirLister->url();
560 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
561 // The current URL is not a child of the dir lister
562 // URL. This may happen when e. g. a place has been selected
563 // and hence the view must be reset.
564 m_dirLister->openUrl(url, false, false);
565 }
566 }
567 } else {
568 m_dirLister->openUrl(url, false, reload);
569 }
570 }
571
572 KUrl DolphinView::viewPropertiesUrl() const
573 {
574 if (isColumnViewActive()) {
575 return m_dirLister->url();
576 }
577
578 return url();
579 }
580
581 void DolphinView::applyViewProperties(const KUrl& url)
582 {
583 if (isColumnViewActive() && m_dirLister->url().isParentOf(url)) {
584 // The column view is active, hence don't apply the view properties
585 // of sub directories (represented by columns) to the view. The
586 // view always represents the properties of the first column.
587 return;
588 }
589
590 const ViewProperties props(url);
591
592 const Mode mode = props.viewMode();
593 if (m_mode != mode) {
594 m_mode = mode;
595 createView();
596 emit modeChanged();
597
598 if (m_mode == ColumnView) {
599 // The mode has been changed to the Column View. When starting the dir
600 // lister with DolphinView::startDirLister() it is important to give a
601 // hint that the dir lister may not keep the current directory
602 // although this is the default for showing a hierarchy.
603 m_initializeColumnView = true;
604 }
605 }
606 if (itemView() == 0) {
607 createView();
608 }
609 Q_ASSERT(itemView() != 0);
610 Q_ASSERT(m_fileItemDelegate != 0);
611
612 const bool showHiddenFiles = props.showHiddenFiles();
613 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
614 m_dirLister->setShowingDotFiles(showHiddenFiles);
615 emit showHiddenFilesChanged();
616 }
617
618 const bool categorized = props.categorizedSorting();
619 if (categorized != categorizedSorting()) {
620 m_proxyModel->setCategorizedModel(categorized);
621 emit categorizedSortingChanged();
622 }
623
624 const DolphinView::Sorting sorting = props.sorting();
625 if (sorting != m_proxyModel->sorting()) {
626 m_proxyModel->setSorting(sorting);
627 emit sortingChanged(sorting);
628 }
629
630 const Qt::SortOrder sortOrder = props.sortOrder();
631 if (sortOrder != m_proxyModel->sortOrder()) {
632 m_proxyModel->setSortOrder(sortOrder);
633 emit sortOrderChanged(sortOrder);
634 }
635
636 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
637 if (info != m_fileItemDelegate->additionalInformation()) {
638 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
639 m_fileItemDelegate->setAdditionalInformation(info);
640 emit additionalInfoChanged(info);
641 }
642
643 const bool showPreview = props.showPreview();
644 if (showPreview != m_controller->showPreview()) {
645 m_controller->setShowPreview(showPreview);
646 emit showPreviewChanged();
647 }
648 }
649
650 void DolphinView::changeSelection(const QList<KFileItem>& selection)
651 {
652 clearSelection();
653 if (selection.isEmpty()) {
654 return;
655 }
656 const KUrl& baseUrl = url();
657 KUrl url;
658 QItemSelection new_selection;
659 foreach(const KFileItem& item, selection) {
660 url = item.url().upUrl();
661 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
662 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
663 new_selection.select(index, index);
664 }
665 }
666 itemView()->selectionModel()->select(new_selection,
667 QItemSelectionModel::ClearAndSelect
668 | QItemSelectionModel::Current);
669 }
670
671 void DolphinView::openContextMenu(const QPoint& pos)
672 {
673 KFileItem item;
674
675 const QModelIndex index = itemView()->indexAt(pos);
676 if (isValidNameIndex(index)) {
677 item = fileItem(index);
678 }
679
680 emit requestContextMenu(item, url());
681 }
682
683 void DolphinView::dropUrls(const KUrl::List& urls,
684 const KUrl& destPath,
685 const QModelIndex& destIndex,
686 QWidget* source)
687 {
688 KFileItem directory;
689 if (isValidNameIndex(destIndex)) {
690 KFileItem item = fileItem(destIndex);
691 Q_ASSERT(!item.isNull());
692 if (item.isDir()) {
693 // the URLs are dropped above a directory
694 directory = item;
695 }
696 }
697
698 if ((directory.isNull()) && (source == itemView())) {
699 // The dropping is done into the same viewport where
700 // the dragging has been started. Just ignore this...
701 return;
702 }
703
704 const KUrl& destination = (directory.isNull()) ?
705 destPath : directory.url();
706 dropUrls(urls, destination);
707 }
708
709 void DolphinView::dropUrls(const KUrl::List& urls,
710 const KUrl& destination)
711 {
712 emit urlsDropped(urls, destination);
713 }
714
715 void DolphinView::updateSorting(DolphinView::Sorting sorting)
716 {
717 ViewProperties props(viewPropertiesUrl());
718 props.setSorting(sorting);
719
720 m_proxyModel->setSorting(sorting);
721
722 emit sortingChanged(sorting);
723 }
724
725 void DolphinView::updateSortOrder(Qt::SortOrder order)
726 {
727 ViewProperties props(viewPropertiesUrl());
728 props.setSortOrder(order);
729
730 m_proxyModel->setSortOrder(order);
731
732 emit sortOrderChanged(order);
733 }
734
735 void DolphinView::emitContentsMoved()
736 {
737 // only emit the contents moved signal if:
738 // - no directory loading is ongoing (this would reset the contents position
739 // always to (0, 0))
740 // - if the Column View is active: the column view does an automatic
741 // positioning during the loading operation, which must be remembered
742 if (!m_loadingDirectory || isColumnViewActive()) {
743 const QPoint pos(contentsPosition());
744 emit contentsMoved(pos.x(), pos.y());
745 }
746 }
747
748 void DolphinView::updateCutItems()
749 {
750 // restore the icons of all previously selected items to the
751 // original state...
752 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
753 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
754 while (it != end) {
755 const QModelIndex index = m_dolphinModel->indexForUrl((*it).url);
756 if (index.isValid()) {
757 m_dolphinModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
758 }
759 ++it;
760 }
761 m_cutItemsCache.clear();
762
763 // ... and apply an item effect to all currently cut items
764 applyCutItemEffect();
765 }
766
767 void DolphinView::showHoverInformation(const QModelIndex& index)
768 {
769 if (hasSelection()) {
770 return;
771 }
772
773 const KFileItem item = fileItem(index);
774 if (!item.isNull()) {
775 emit requestItemInfo(item);
776 }
777 }
778
779 void DolphinView::clearHoverInformation()
780 {
781 emit requestItemInfo(KFileItem());
782 }
783
784
785 void DolphinView::createView()
786 {
787 // delete current view
788 QAbstractItemView* view = itemView();
789 if (view != 0) {
790 m_topLayout->removeWidget(view);
791 view->close();
792 view->deleteLater();
793 view = 0;
794 m_iconsView = 0;
795 m_detailsView = 0;
796 m_columnView = 0;
797 m_fileItemDelegate = 0;
798 }
799
800 Q_ASSERT(m_iconsView == 0);
801 Q_ASSERT(m_detailsView == 0);
802 Q_ASSERT(m_columnView == 0);
803
804 // ... and recreate it representing the current mode
805 switch (m_mode) {
806 case IconsView: {
807 m_iconsView = new DolphinIconsView(this, m_controller);
808 view = m_iconsView;
809 break;
810 }
811
812 case DetailsView:
813 m_detailsView = new DolphinDetailsView(this, m_controller);
814 view = m_detailsView;
815
816 // categorized sorting is not supported yet for the details
817 // view, even if the view properties indicate this
818 setCategorizedSorting(false);
819 break;
820
821 case ColumnView:
822 m_columnView = new DolphinColumnView(this, m_controller);
823 view = m_columnView;
824
825 // categorized sorting is not supported yet for the column
826 // view, even if the view properties indicate this
827 setCategorizedSorting(false);
828 break;
829 }
830
831 Q_ASSERT(view != 0);
832
833 m_fileItemDelegate = new KFileItemDelegate(view);
834 view->setItemDelegate(m_fileItemDelegate);
835
836 view->setModel(m_proxyModel);
837 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
838
839 new KMimeTypeResolver(view, m_dolphinModel);
840 m_topLayout->insertWidget(1, view);
841
842 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
843 this, SLOT(emitSelectionChangedSignal()));
844 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
845 this, SLOT(emitContentsMoved()));
846 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
847 this, SLOT(emitContentsMoved()));
848 view->setFocus();
849 }
850
851 QAbstractItemView* DolphinView::itemView() const
852 {
853 if (m_detailsView != 0) {
854 return m_detailsView;
855 } else if (m_columnView != 0) {
856 return m_columnView;
857 }
858
859 return m_iconsView;
860 }
861
862 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
863 {
864 return index.isValid() && (index.column() == DolphinModel::Name);
865 }
866
867 bool DolphinView::isCutItem(const KFileItem& item) const
868 {
869 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
870 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
871
872 const KUrl& itemUrl = item.url();
873 KUrl::List::const_iterator it = cutUrls.begin();
874 const KUrl::List::const_iterator end = cutUrls.end();
875 while (it != end) {
876 if (*it == itemUrl) {
877 return true;
878 }
879 ++it;
880 }
881
882 return false;
883 }
884
885 void DolphinView::applyCutItemEffect()
886 {
887 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
888 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
889 return;
890 }
891
892 KFileItemList items(m_dirLister->items());
893 KFileItemList::const_iterator it = items.begin();
894 const KFileItemList::const_iterator end = items.end();
895 while (it != end) {
896 KFileItem* item = *it;
897 if (isCutItem(*item)) {
898 const QModelIndex index = m_dolphinModel->indexForItem(*item);
899 // Huh? the item is already known
900 //const KFileItem item = m_dolphinModel->itemForIndex(index);
901 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
902 if (value.type() == QVariant::Icon) {
903 const QIcon icon(qvariant_cast<QIcon>(value));
904 QPixmap pixmap = icon.pixmap(128, 128);
905
906 // remember current pixmap for the item to be able
907 // to restore it when other items get cut
908 CutItem cutItem;
909 cutItem.url = item->url();
910 cutItem.pixmap = pixmap;
911 m_cutItemsCache.append(cutItem);
912
913 // apply icon effect to the cut item
914 KIconEffect iconEffect;
915 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
916 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
917 }
918 }
919 ++it;
920 }
921 }
922
923 void DolphinView::updateViewportColor()
924 {
925 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
926 if (m_active) {
927 emit urlChanged(url()); // Hmm, this is a hack; the url hasn't really changed.
928 emit selectionChanged(selectedItems());
929 } else {
930 color.setAlpha(0);
931 }
932
933 QWidget* viewport = itemView()->viewport();
934 QPalette palette;
935 palette.setColor(viewport->backgroundRole(), color);
936 viewport->setPalette(palette);
937 }
938
939 #include "dolphinview.moc"