]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Refactored DolphinColumnWidget so that it does not need a hierarchical KDirLister...
[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 QModelIndex&)),
102 this, SLOT(triggerItem(const QModelIndex&)));
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_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
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->showColumn(url);
461 }
462 } else {
463 applyViewProperties(url);
464 loadDirectory(url);
465 }
466
467 itemView()->setFocus();
468
469 emit startedPathLoading(url);
470 }
471
472 void DolphinView::setUrl(const KUrl& url)
473 {
474 updateView(url, KUrl());
475 }
476
477 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
478 {
479 QWidget::mouseReleaseEvent(event);
480 setActive(true);
481 }
482 void DolphinView::activate()
483 {
484 setActive(true);
485 }
486
487 void DolphinView::triggerItem(const QModelIndex& index)
488 {
489 Q_ASSERT(index.isValid());
490
491 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
492 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
493 // items are selected by the user, hence don't trigger the
494 // item specified by 'index'
495 return;
496 }
497
498 const KFileItem item = m_dolphinModel->itemForIndex(m_proxyModel->mapToSource(index));
499
500 if (item.isNull()) {
501 return;
502 }
503
504 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
505 }
506
507 void DolphinView::generatePreviews(const KFileItemList& items)
508 {
509 if (m_controller->showPreview()) {
510 KIO::PreviewJob* job = KIO::filePreview(items, 128);
511 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
512 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
513 }
514 }
515
516 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
517 {
518 Q_ASSERT(!item.isNull());
519 if (item.url().directory() != m_dirLister->url().path()) {
520 // the preview job is still working on items of an older URL, hence
521 // the item is not part of the directory model anymore
522 return;
523 }
524
525 const QModelIndex idx = m_dolphinModel->indexForItem(item);
526 if (idx.isValid() && (idx.column() == 0)) {
527 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
528 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
529 KIconEffect iconEffect;
530 const QPixmap cutPixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
531 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
532 } else {
533 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
534 }
535 }
536 }
537
538 void DolphinView::emitSelectionChangedSignal()
539 {
540 emit selectionChanged(DolphinView::selectedItems());
541 }
542
543 void DolphinView::loadDirectory(const KUrl& url, bool reload)
544 {
545 if (!url.isValid()) {
546 const QString location(url.pathOrUrl());
547 if (location.isEmpty()) {
548 emit errorMessage(i18nc("@info:status", "The location is empty."));
549 } else {
550 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
551 }
552 return;
553 }
554
555 m_cutItemsCache.clear();
556 m_loadingDirectory = true;
557
558 m_dirLister->stop();
559 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
560
561 if (isColumnViewActive() && reload) {
562 // reloading the directory lister is not enough in the case of the
563 // column view, as each column has its own directory lister internally...
564 m_columnView->reload();
565 }
566 }
567
568 KUrl DolphinView::viewPropertiesUrl() const
569 {
570 if (isColumnViewActive()) {
571 return m_dirLister->url();
572 }
573
574 return url();
575 }
576
577 void DolphinView::applyViewProperties(const KUrl& url)
578 {
579 if (isColumnViewActive() && m_dirLister->url().isParentOf(url)) {
580 // The column view is active, hence don't apply the view properties
581 // of sub directories (represented by columns) to the view. The
582 // view always represents the properties of the first column.
583 return;
584 }
585
586 const ViewProperties props(url);
587
588 const Mode mode = props.viewMode();
589 if (m_mode != mode) {
590 m_mode = mode;
591 createView();
592 emit modeChanged();
593 }
594 if (itemView() == 0) {
595 createView();
596 }
597 Q_ASSERT(itemView() != 0);
598 Q_ASSERT(m_fileItemDelegate != 0);
599
600 const bool showHiddenFiles = props.showHiddenFiles();
601 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
602 m_dirLister->setShowingDotFiles(showHiddenFiles);
603 m_controller->setShowHiddenFiles(showHiddenFiles);
604 emit showHiddenFilesChanged();
605 }
606
607 m_storedCategorizedSorting = props.categorizedSorting();
608 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
609 if (categorized != m_proxyModel->isCategorizedModel()) {
610 m_proxyModel->setCategorizedModel(categorized);
611 m_proxyModel->sort(m_proxyModel->sortColumn(), m_proxyModel->sortOrder());
612 emit categorizedSortingChanged();
613 }
614
615 const DolphinView::Sorting sorting = props.sorting();
616 if (sorting != m_proxyModel->sorting()) {
617 m_proxyModel->setSorting(sorting);
618 emit sortingChanged(sorting);
619 }
620
621 const Qt::SortOrder sortOrder = props.sortOrder();
622 if (sortOrder != m_proxyModel->sortOrder()) {
623 m_proxyModel->setSortOrder(sortOrder);
624 emit sortOrderChanged(sortOrder);
625 }
626
627 KFileItemDelegate::InformationList info = props.additionalInfo();
628 if (info != m_fileItemDelegate->showInformation()) {
629 m_controller->setAdditionalInfoCount(info.count());
630 m_fileItemDelegate->setShowInformation(info);
631 emit additionalInfoChanged(info);
632 }
633
634 const bool showPreview = props.showPreview();
635 if (showPreview != m_controller->showPreview()) {
636 m_controller->setShowPreview(showPreview);
637 emit showPreviewChanged();
638 }
639 }
640
641 void DolphinView::changeSelection(const KFileItemList& selection)
642 {
643 clearSelection();
644 if (selection.isEmpty()) {
645 return;
646 }
647 const KUrl& baseUrl = url();
648 KUrl url;
649 QItemSelection new_selection;
650 foreach(const KFileItem& item, selection) {
651 url = item.url().upUrl();
652 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
653 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
654 new_selection.select(index, index);
655 }
656 }
657 itemView()->selectionModel()->select(new_selection,
658 QItemSelectionModel::ClearAndSelect
659 | QItemSelectionModel::Current);
660 }
661
662 void DolphinView::openContextMenu(const QPoint& pos)
663 {
664 KFileItem item;
665
666 const QModelIndex index = itemView()->indexAt(pos);
667 if (isValidNameIndex(index)) {
668 item = fileItem(index);
669 }
670
671 emit requestContextMenu(item, url());
672 }
673
674 void DolphinView::dropUrls(const KUrl::List& urls,
675 const KUrl& destPath,
676 const QModelIndex& destIndex,
677 QWidget* source)
678 {
679 KFileItem directory;
680 if (isValidNameIndex(destIndex)) {
681 KFileItem item = fileItem(destIndex);
682 Q_ASSERT(!item.isNull());
683 if (item.isDir()) {
684 // the URLs are dropped above a directory
685 directory = item;
686 }
687 }
688
689 if ((directory.isNull()) && (source == itemView())) {
690 // The dropping is done into the same viewport where
691 // the dragging has been started. Just ignore this...
692 return;
693 }
694
695 const KUrl& destination = (directory.isNull()) ?
696 destPath : directory.url();
697 dropUrls(urls, destination);
698 }
699
700 void DolphinView::dropUrls(const KUrl::List& urls,
701 const KUrl& destination)
702 {
703 emit urlsDropped(urls, destination);
704 }
705
706 void DolphinView::updateSorting(DolphinView::Sorting sorting)
707 {
708 ViewProperties props(viewPropertiesUrl());
709 props.setSorting(sorting);
710
711 m_proxyModel->setSorting(sorting);
712
713 emit sortingChanged(sorting);
714 }
715
716 void DolphinView::updateSortOrder(Qt::SortOrder order)
717 {
718 ViewProperties props(viewPropertiesUrl());
719 props.setSortOrder(order);
720
721 m_proxyModel->setSortOrder(order);
722
723 emit sortOrderChanged(order);
724 }
725
726 void DolphinView::emitContentsMoved()
727 {
728 // only emit the contents moved signal if:
729 // - no directory loading is ongoing (this would reset the contents position
730 // always to (0, 0))
731 // - if the Column View is active: the column view does an automatic
732 // positioning during the loading operation, which must be remembered
733 if (!m_loadingDirectory || isColumnViewActive()) {
734 const QPoint pos(contentsPosition());
735 emit contentsMoved(pos.x(), pos.y());
736 }
737 }
738
739 void DolphinView::updateCutItems()
740 {
741 // restore the icons of all previously selected items to the
742 // original state...
743 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
744 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
745 while (it != end) {
746 const QModelIndex index = m_dolphinModel->indexForUrl((*it).url);
747 if (index.isValid()) {
748 m_dolphinModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
749 }
750 ++it;
751 }
752 m_cutItemsCache.clear();
753
754 // ... and apply an item effect to all currently cut items
755 applyCutItemEffect();
756 }
757
758 void DolphinView::showHoverInformation(const KFileItem& item)
759 {
760 if (hasSelection()) {
761 return;
762 }
763
764 emit requestItemInfo(item);
765 }
766
767 void DolphinView::clearHoverInformation()
768 {
769 emit requestItemInfo(KFileItem());
770 }
771
772
773 void DolphinView::createView()
774 {
775 KFileItemDelegate::InformationList infoList;
776 if (m_fileItemDelegate != 0) {
777 infoList = m_fileItemDelegate->showInformation();
778 }
779
780 // delete current view
781 QAbstractItemView* view = itemView();
782 if (view != 0) {
783 m_topLayout->removeWidget(view);
784 view->close();
785 view->deleteLater();
786 view = 0;
787 m_iconsView = 0;
788 m_detailsView = 0;
789 m_columnView = 0;
790 m_fileItemDelegate = 0;
791 }
792
793 Q_ASSERT(m_iconsView == 0);
794 Q_ASSERT(m_detailsView == 0);
795 Q_ASSERT(m_columnView == 0);
796
797 // ... and recreate it representing the current mode
798 switch (m_mode) {
799 case IconsView: {
800 m_iconsView = new DolphinIconsView(this, m_controller);
801 view = m_iconsView;
802 break;
803 }
804
805 case DetailsView:
806 m_detailsView = new DolphinDetailsView(this, m_controller);
807 view = m_detailsView;
808 break;
809
810 case ColumnView:
811 m_columnView = new DolphinColumnView(this, m_controller);
812 view = m_columnView;
813 break;
814 }
815
816 Q_ASSERT(view != 0);
817
818 m_fileItemDelegate = new KFileItemDelegate(view);
819 m_fileItemDelegate->setShowInformation(infoList);
820 view->setItemDelegate(m_fileItemDelegate);
821
822 view->setModel(m_proxyModel);
823 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
824
825 new KMimeTypeResolver(view, m_dolphinModel);
826 m_topLayout->insertWidget(1, view);
827
828 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
829 this, SLOT(emitSelectionChangedSignal()));
830 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
831 this, SLOT(emitContentsMoved()));
832 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
833 this, SLOT(emitContentsMoved()));
834 view->setFocus();
835 }
836
837 QAbstractItemView* DolphinView::itemView() const
838 {
839 if (m_detailsView != 0) {
840 return m_detailsView;
841 } else if (m_columnView != 0) {
842 return m_columnView;
843 }
844
845 return m_iconsView;
846 }
847
848 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
849 {
850 return index.isValid() && (index.column() == DolphinModel::Name);
851 }
852
853 bool DolphinView::isCutItem(const KFileItem& item) const
854 {
855 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
856 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
857
858 const KUrl& itemUrl = item.url();
859 KUrl::List::const_iterator it = cutUrls.begin();
860 const KUrl::List::const_iterator end = cutUrls.end();
861 while (it != end) {
862 if (*it == itemUrl) {
863 return true;
864 }
865 ++it;
866 }
867
868 return false;
869 }
870
871 void DolphinView::applyCutItemEffect()
872 {
873 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
874 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
875 return;
876 }
877
878 KFileItemList items(m_dirLister->items());
879 KFileItemList::const_iterator it = items.begin();
880 const KFileItemList::const_iterator end = items.end();
881 while (it != end) {
882 const KFileItem item = *it;
883 if (isCutItem(item)) {
884 const QModelIndex index = m_dolphinModel->indexForItem(item);
885 // Huh? the item is already known
886 //const KFileItem item = m_dolphinModel->itemForIndex(index);
887 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
888 if (value.type() == QVariant::Icon) {
889 const QIcon icon(qvariant_cast<QIcon>(value));
890 QPixmap pixmap = icon.pixmap(128, 128);
891
892 // remember current pixmap for the item to be able
893 // to restore it when other items get cut
894 CutItem cutItem;
895 cutItem.url = item.url();
896 cutItem.pixmap = pixmap;
897 m_cutItemsCache.append(cutItem);
898
899 // apply icon effect to the cut item
900 KIconEffect iconEffect;
901 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
902 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
903 }
904 }
905 ++it;
906 }
907 }
908
909 void DolphinView::updateViewportColor()
910 {
911 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
912 if (m_active) {
913 emit urlChanged(url()); // Hmm, this is a hack; the url hasn't really changed.
914 emit selectionChanged(selectedItems());
915 } else {
916 color.setAlpha(0);
917 }
918
919 QWidget* viewport = itemView()->viewport();
920 QPalette palette;
921 palette.setColor(viewport->backgroundRole(), color);
922 viewport->setPalette(palette);
923 }
924
925 #include "dolphinview.moc"