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