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