]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
* fixed issue that when having a split view, that both views get the same color after...
[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 KFileItemList&)),
88 this, SLOT(generatePreviews(const KFileItemList&)));
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 KFileItemList 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 KFileItemList itemList;
303
304 const QModelIndexList indexList = selection.indexes();
305 QModelIndexList::const_iterator end = indexList.end();
306 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
307 Q_ASSERT((*it).isValid());
308
309 KFileItem* item = m_dirModel->itemForIndex(*it);
310 if (item != 0) {
311 itemList.append(item);
312 }
313 }
314
315 return itemList;
316 }
317
318 KUrl::List DolphinView::selectedUrls() const
319 {
320 KUrl::List urls;
321
322 const KFileItemList list = selectedItems();
323 KFileItemList::const_iterator it = list.begin();
324 const KFileItemList::const_iterator end = list.end();
325 while (it != end) {
326 KFileItem* item = *it;
327 urls.append(item->url());
328 ++it;
329 }
330
331 return urls;
332 }
333
334 KFileItem* DolphinView::fileItem(const QModelIndex index) const
335 {
336 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
337 return m_dirModel->itemForIndex(dirModelIndex);
338 }
339
340 void DolphinView::setContentsPosition(int x, int y)
341 {
342 QAbstractItemView* view = itemView();
343 view->horizontalScrollBar()->setValue(x);
344 view->verticalScrollBar()->setValue(y);
345
346 m_loadingDirectory = false;
347 }
348
349 QPoint DolphinView::contentsPosition() const
350 {
351 const int x = itemView()->horizontalScrollBar()->value();
352 const int y = itemView()->verticalScrollBar()->value();
353 return QPoint(x, y);
354 }
355
356 void DolphinView::zoomIn()
357 {
358 m_controller->triggerZoomIn();
359 }
360
361 void DolphinView::zoomOut()
362 {
363 m_controller->triggerZoomOut();
364 }
365
366 bool DolphinView::isZoomInPossible() const
367 {
368 return m_controller->isZoomInPossible();
369 }
370
371 bool DolphinView::isZoomOutPossible() const
372 {
373 return m_controller->isZoomOutPossible();
374 }
375
376 void DolphinView::setSorting(Sorting sorting)
377 {
378 if (sorting != this->sorting()) {
379 updateSorting(sorting);
380 }
381 }
382
383 DolphinView::Sorting DolphinView::sorting() const
384 {
385 return m_proxyModel->sorting();
386 }
387
388 void DolphinView::setSortOrder(Qt::SortOrder order)
389 {
390 if (sortOrder() != order) {
391 updateSortOrder(order);
392 }
393 }
394
395 Qt::SortOrder DolphinView::sortOrder() const
396 {
397 return m_proxyModel->sortOrder();
398 }
399
400 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
401 {
402 const KUrl viewPropsUrl = viewPropertiesUrl();
403 ViewProperties props(viewPropsUrl);
404 props.setAdditionalInfo(info);
405
406 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
407 m_fileItemDelegate->setAdditionalInformation(info);
408
409 emit additionalInfoChanged(info);
410 startDirLister(viewPropsUrl, true);
411 }
412
413 KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
414 {
415 return m_fileItemDelegate->additionalInformation();
416 }
417
418 void DolphinView::reload()
419 {
420 setUrl(url());
421 startDirLister(url(), true);
422 }
423
424 void DolphinView::refresh()
425 {
426 createView();
427 applyViewProperties(m_controller->url());
428 reload();
429 updateViewportColor();
430 }
431
432 void DolphinView::setUrl(const KUrl& url)
433 {
434 if (m_controller->url() == url) {
435 return;
436 }
437
438 m_controller->setUrl(url);
439
440 applyViewProperties(url);
441
442 startDirLister(url);
443 emit urlChanged(url);
444 }
445
446 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
447 {
448 QWidget::mouseReleaseEvent(event);
449 setActive(true);;
450 }
451 void DolphinView::activate()
452 {
453 setActive(true);
454 }
455
456 void DolphinView::triggerItem(const QModelIndex& index)
457 {
458 if (!isValidNameIndex(index)) {
459 clearSelection();
460 showHoverInformation(index);
461 return;
462 }
463
464 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
465 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
466 // items are selected by the user, hence don't trigger the
467 // item specified by 'index'
468 return;
469 }
470
471 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
472 if (item == 0) {
473 return;
474 }
475
476 // The stuff below should be moved to ViewContainer and be just a signal?
477
478 // Prefer the local path over the URL.
479 bool isLocal;
480 KUrl url = item->mostLocalUrl(isLocal);
481
482 if (item->isDir()) {
483 setUrl(url);
484 } else if (item->isFile()) {
485 // allow to browse through ZIP and tar files
486 KMimeType::Ptr mime = item->mimeTypePtr();
487 if (mime->is("application/zip")) {
488 url.setProtocol("zip");
489 setUrl(url);
490 } else if (mime->is("application/x-tar") ||
491 mime->is("application/x-tarz") ||
492 mime->is("application/x-bzip-compressed-tar") ||
493 mime->is("application/x-compressed-tar") ||
494 mime->is("application/x-tzo")) {
495 url.setProtocol("tar");
496 setUrl(url);
497 } else {
498 item->run();
499 }
500 } else {
501 item->run();
502 }
503 }
504
505 void DolphinView::generatePreviews(const KFileItemList& items)
506 {
507 if (m_controller->showPreview()) {
508
509 // Must turn QList<KFileItem *> to QList<KFileItem>...
510 QList<KFileItem> itemsToPreview;
511 foreach( KFileItem* it, items )
512 itemsToPreview.append( *it );
513
514 KIO::PreviewJob* job = KIO::filePreview(itemsToPreview, 128);
515 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
516 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
517 }
518 }
519
520 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
521 {
522 Q_ASSERT(!item.isNull());
523 if (item.url().directory() != m_dirLister->url().path()) {
524 // the preview job is still working on items of an older URL, hence
525 // the item is not part of the directory model anymore
526 return;
527 }
528
529 const QModelIndex idx = m_dirModel->indexForItem(item);
530 if (idx.isValid() && (idx.column() == 0)) {
531 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
532 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
533 KIconEffect iconEffect;
534 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
535 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
536 } else {
537 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
538 }
539 }
540 }
541
542 void DolphinView::emitSelectionChangedSignal()
543 {
544 emit selectionChanged(DolphinView::selectedItems());
545 }
546
547 void DolphinView::startDirLister(const KUrl& url, bool reload)
548 {
549 if (!url.isValid()) {
550 const QString location(url.pathOrUrl());
551 if (location.isEmpty()) {
552 emit errorMessage(i18nc("@info:status", "The location is empty."));
553 } else {
554 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
555 }
556 return;
557 }
558
559 m_cutItemsCache.clear();
560 m_loadingDirectory = true;
561
562 m_dirLister->stop();
563
564 bool openDir = true;
565 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
566 m_initializeColumnView = false;
567
568 if (keepOldDirs) {
569 if (reload) {
570 keepOldDirs = false;
571
572 const KUrl& dirListerUrl = m_dirLister->url();
573 if (dirListerUrl.isValid()) {
574 const KUrl::List dirs = m_dirLister->directories();
575 KUrl url;
576 foreach(url, dirs) {
577 m_dirLister->updateDirectory(url);
578 }
579 openDir = false;
580 }
581 } else if (m_dirLister->directories().contains(url)) {
582 // The dir lister contains the directory already, so
583 // KDirLister::openUrl() may not been invoked twice.
584 m_dirLister->updateDirectory(url);
585 openDir = false;
586 } else {
587 const KUrl& dirListerUrl = m_dirLister->url();
588 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
589 // The current URL is not a child of the dir lister
590 // URL. This may happen when e. g. a bookmark has been selected
591 // and hence the view must be reset.
592 keepOldDirs = false;
593 }
594 }
595 }
596
597 if (openDir) {
598 m_dirLister->openUrl(url, keepOldDirs, reload);
599 }
600 }
601
602 KUrl DolphinView::viewPropertiesUrl() const
603 {
604 if (isColumnViewActive()) {
605 return m_dirLister->url();
606 }
607
608 return url();
609 }
610
611 void DolphinView::applyViewProperties(const KUrl& url)
612 {
613 if (isColumnViewActive() && m_dirLister->url().isParentOf(url)) {
614 // The column view is active, hence don't apply the view properties
615 // of sub directories (represented by columns) to the view. The
616 // view always represents the properties of the first column.
617 return;
618 }
619
620 const ViewProperties props(url);
621
622 const Mode mode = props.viewMode();
623 if (m_mode != mode) {
624 m_mode = mode;
625 createView();
626 emit modeChanged();
627
628 if (m_mode == ColumnView) {
629 // The mode has been changed to the Column View. When starting the dir
630 // lister with DolphinView::startDirLister() it is important to give a
631 // hint that the dir lister may not keep the current directory
632 // although this is the default for showing a hierarchy.
633 m_initializeColumnView = true;
634 }
635 }
636 if (itemView() == 0) {
637 createView();
638 }
639 Q_ASSERT(itemView() != 0);
640 Q_ASSERT(m_fileItemDelegate != 0);
641
642 const bool showHiddenFiles = props.showHiddenFiles();
643 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
644 m_dirLister->setShowingDotFiles(showHiddenFiles);
645 emit showHiddenFilesChanged();
646 }
647
648 const bool categorized = props.categorizedSorting();
649 if (categorized != categorizedSorting()) {
650 if (supportsCategorizedSorting()) {
651 Q_ASSERT(m_iconsView != 0);
652 if (categorized) {
653 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
654 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
655 } else {
656 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
657 m_iconsView->setItemCategorizer(0);
658 delete categorizer;
659 }
660 }
661 emit categorizedSortingChanged();
662 }
663
664 const DolphinView::Sorting sorting = props.sorting();
665 if (sorting != m_proxyModel->sorting()) {
666 m_proxyModel->setSorting(sorting);
667 emit sortingChanged(sorting);
668 }
669
670 const Qt::SortOrder sortOrder = props.sortOrder();
671 if (sortOrder != m_proxyModel->sortOrder()) {
672 m_proxyModel->setSortOrder(sortOrder);
673 emit sortOrderChanged(sortOrder);
674 }
675
676 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
677 if (info != m_fileItemDelegate->additionalInformation()) {
678 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
679 m_fileItemDelegate->setAdditionalInformation(info);
680 emit additionalInfoChanged(info);
681 }
682
683 const bool showPreview = props.showPreview();
684 if (showPreview != m_controller->showPreview()) {
685 m_controller->setShowPreview(showPreview);
686 emit showPreviewChanged();
687 }
688 }
689
690 void DolphinView::changeSelection(const KFileItemList& selection)
691 {
692 clearSelection();
693 if (selection.isEmpty()) {
694 return;
695 }
696 const KUrl& baseUrl = url();
697 KUrl url;
698 QItemSelection new_selection;
699 foreach(KFileItem* item, selection) {
700 url = item->url().upUrl();
701 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
702 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(*item));
703 new_selection.select(index, index);
704 }
705 }
706 itemView()->selectionModel()->select(new_selection,
707 QItemSelectionModel::ClearAndSelect
708 | QItemSelectionModel::Current);
709 }
710
711 void DolphinView::openContextMenu(const QPoint& pos)
712 {
713 KFileItem* item = 0;
714
715 const QModelIndex index = itemView()->indexAt(pos);
716 if (isValidNameIndex(index)) {
717 item = fileItem(index);
718 }
719
720 emit requestContextMenu(item, url());
721 }
722
723 void DolphinView::dropUrls(const KUrl::List& urls,
724 const QModelIndex& index,
725 QWidget* source)
726 {
727 KFileItem* directory = 0;
728 if (isValidNameIndex(index)) {
729 KFileItem* item = fileItem(index);
730 Q_ASSERT(item != 0);
731 if (item->isDir()) {
732 // the URLs are dropped above a directory
733 directory = item;
734 }
735 }
736
737 if ((directory == 0) && (source == itemView())) {
738 // The dropping is done into the same viewport where
739 // the dragging has been started. Just ignore this...
740 return;
741 }
742
743 const KUrl& destination = (directory == 0) ?
744 url() : directory->url();
745 dropUrls(urls, destination);
746 }
747
748 void DolphinView::dropUrls(const KUrl::List& urls,
749 const KUrl& destination)
750 {
751 emit urlsDropped(urls, destination);
752 }
753
754 void DolphinView::updateSorting(DolphinView::Sorting sorting)
755 {
756 ViewProperties props(viewPropertiesUrl());
757 props.setSorting(sorting);
758
759 m_proxyModel->setSorting(sorting);
760
761 emit sortingChanged(sorting);
762 }
763
764 void DolphinView::updateSortOrder(Qt::SortOrder order)
765 {
766 ViewProperties props(viewPropertiesUrl());
767 props.setSortOrder(order);
768
769 m_proxyModel->setSortOrder(order);
770
771 emit sortOrderChanged(order);
772 }
773
774 void DolphinView::emitContentsMoved()
775 {
776 // only emit the contents moved signal if:
777 // - no directory loading is ongoing (this would reset the contents position
778 // always to (0, 0))
779 // - if the Column View is active: the column view does an automatic
780 // positioning during the loading operation, which must be remembered
781 if (!m_loadingDirectory || isColumnViewActive()) {
782 const QPoint pos(contentsPosition());
783 emit contentsMoved(pos.x(), pos.y());
784 }
785 }
786
787 void DolphinView::updateCutItems()
788 {
789 // restore the icons of all previously selected items to the
790 // original state...
791 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
792 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
793 while (it != end) {
794 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
795 if (index.isValid()) {
796 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
797 }
798 ++it;
799 }
800 m_cutItemsCache.clear();
801
802 // ... and apply an item effect to all currently cut items
803 applyCutItemEffect();
804 }
805
806 void DolphinView::showHoverInformation(const QModelIndex& index)
807 {
808 if (hasSelection()) {
809 return;
810 }
811
812 const KFileItem* item = fileItem(index);
813 if (item != 0) {
814 emit requestItemInfo(*item);
815 }
816 }
817
818 void DolphinView::clearHoverInformation()
819 {
820 emit requestItemInfo(KFileItem());
821 }
822
823
824 void DolphinView::createView()
825 {
826 // delete current view
827 QAbstractItemView* view = itemView();
828 if (view != 0) {
829 m_topLayout->removeWidget(view);
830 view->close();
831 if (view == m_iconsView) {
832 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
833 m_iconsView->setItemCategorizer(0);
834 delete categorizer;
835 }
836 view->deleteLater();
837 view = 0;
838 m_iconsView = 0;
839 m_detailsView = 0;
840 m_columnView = 0;
841 m_fileItemDelegate = 0;
842 }
843
844 Q_ASSERT(m_iconsView == 0);
845 Q_ASSERT(m_detailsView == 0);
846 Q_ASSERT(m_columnView == 0);
847
848 // ... and recreate it representing the current mode
849 switch (m_mode) {
850 case IconsView:
851 m_iconsView = new DolphinIconsView(this, m_controller);
852 view = m_iconsView;
853 break;
854
855 case DetailsView:
856 m_detailsView = new DolphinDetailsView(this, m_controller);
857 view = m_detailsView;
858 break;
859
860 case ColumnView:
861 m_columnView = new DolphinColumnView(this, m_controller);
862 view = m_columnView;
863 break;
864 }
865
866 Q_ASSERT(view != 0);
867
868 m_fileItemDelegate = new KFileItemDelegate(view);
869 view->setItemDelegate(m_fileItemDelegate);
870
871 view->setModel(m_proxyModel);
872 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
873
874 new KMimeTypeResolver(view, m_dirModel);
875 m_topLayout->insertWidget(1, view);
876
877 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
878 this, SLOT(emitSelectionChangedSignal()));
879 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
880 this, SLOT(emitContentsMoved()));
881 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
882 this, SLOT(emitContentsMoved()));
883 }
884
885 QAbstractItemView* DolphinView::itemView() const
886 {
887 if (m_detailsView != 0) {
888 return m_detailsView;
889 } else if (m_columnView != 0) {
890 return m_columnView;
891 }
892
893 return m_iconsView;
894 }
895
896 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
897 {
898 return index.isValid() && (index.column() == KDirModel::Name);
899 }
900
901 bool DolphinView::isCutItem(const KFileItem& item) const
902 {
903 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
904 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
905
906 const KUrl& itemUrl = item.url();
907 KUrl::List::const_iterator it = cutUrls.begin();
908 const KUrl::List::const_iterator end = cutUrls.end();
909 while (it != end) {
910 if (*it == itemUrl) {
911 return true;
912 }
913 ++it;
914 }
915
916 return false;
917 }
918
919 void DolphinView::applyCutItemEffect()
920 {
921 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
922 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
923 return;
924 }
925
926 KFileItemList items(m_dirLister->items());
927 KFileItemList::const_iterator it = items.begin();
928 const KFileItemList::const_iterator end = items.end();
929 while (it != end) {
930 KFileItem* item = *it;
931 if (isCutItem(*item)) {
932 const QModelIndex index = m_dirModel->indexForItem(*item);
933 const KFileItem* item = m_dirModel->itemForIndex(index);
934 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
935 if ((value.type() == QVariant::Icon) && (item != 0)) {
936 const QIcon icon(qvariant_cast<QIcon>(value));
937 QPixmap pixmap = icon.pixmap(128, 128);
938
939 // remember current pixmap for the item to be able
940 // to restore it when other items get cut
941 CutItem cutItem;
942 cutItem.url = item->url();
943 cutItem.pixmap = pixmap;
944 m_cutItemsCache.append(cutItem);
945
946 // apply icon effect to the cut item
947 KIconEffect iconEffect;
948 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
949 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
950 }
951 }
952 ++it;
953 }
954 }
955
956 void DolphinView::updateViewportColor()
957 {
958 QColor color = KColorScheme(KColorScheme::View).background();
959 if (m_active) {
960 emit urlChanged(url());
961 emit selectionChanged(selectedItems());
962 } else {
963 color.setAlpha(0);
964 }
965
966 QWidget* viewport = itemView()->viewport();
967 QPalette palette;
968 palette.setColor(viewport->backgroundRole(), color);
969 viewport->setPalette(palette);
970 }
971
972 #include "dolphinview.moc"