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