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