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