]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
preview did not work anymore (KDirLister emits the signal newItems(const KFileItemLis...
[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 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 KFileItemList& items)
501 {
502 if (m_controller->showPreview()) {
503 // QList<KFileItem*> must be turned to QList<KFileItem>...
504 QList<KFileItem> itemsToPreview;
505 foreach (KFileItem* it, items) {
506 itemsToPreview.append(*it);
507 }
508
509 KIO::PreviewJob* job = KIO::filePreview(itemsToPreview, 128);
510 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
511 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
512 }
513 }
514
515 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
516 {
517 Q_ASSERT(!item.isNull());
518 if (item.url().directory() != m_dirLister->url().path()) {
519 // the preview job is still working on items of an older URL, hence
520 // the item is not part of the directory model anymore
521 return;
522 }
523
524 const QModelIndex idx = m_dirModel->indexForItem(item);
525 if (idx.isValid() && (idx.column() == 0)) {
526 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
527 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
528 KIconEffect iconEffect;
529 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
530 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
531 } else {
532 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
533 }
534 }
535 }
536
537 void DolphinView::emitSelectionChangedSignal()
538 {
539 emit selectionChanged(DolphinView::selectedItems());
540 }
541
542 void DolphinView::startDirLister(const KUrl& url, bool reload)
543 {
544 if (!url.isValid()) {
545 const QString location(url.pathOrUrl());
546 if (location.isEmpty()) {
547 emit errorMessage(i18nc("@info:status", "The location is empty."));
548 } else {
549 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
550 }
551 return;
552 }
553
554 m_cutItemsCache.clear();
555 m_loadingDirectory = true;
556
557 m_dirLister->stop();
558
559 bool openDir = true;
560 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
561 m_initializeColumnView = false;
562
563 if (keepOldDirs) {
564 if (reload) {
565 keepOldDirs = false;
566
567 const KUrl& dirListerUrl = m_dirLister->url();
568 if (dirListerUrl.isValid()) {
569 const KUrl::List dirs = m_dirLister->directories();
570 KUrl url;
571 foreach(url, dirs) {
572 m_dirLister->updateDirectory(url);
573 }
574 openDir = false;
575 }
576 } else if (m_dirLister->directories().contains(url)) {
577 // The dir lister contains the directory already, so
578 // KDirLister::openUrl() may not been invoked twice.
579 m_dirLister->updateDirectory(url);
580 openDir = false;
581 } else {
582 const KUrl& dirListerUrl = m_dirLister->url();
583 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
584 // The current URL is not a child of the dir lister
585 // URL. This may happen when e. g. a bookmark has been selected
586 // and hence the view must be reset.
587 keepOldDirs = false;
588 }
589 }
590 }
591
592 if (openDir) {
593 m_dirLister->openUrl(url, keepOldDirs, reload);
594 }
595 }
596
597 KUrl DolphinView::viewPropertiesUrl() const
598 {
599 if (isColumnViewActive()) {
600 return m_dirLister->url();
601 }
602
603 return url();
604 }
605
606 void DolphinView::applyViewProperties(const KUrl& url)
607 {
608 if (isColumnViewActive() && m_dirLister->url().isParentOf(url)) {
609 // The column view is active, hence don't apply the view properties
610 // of sub directories (represented by columns) to the view. The
611 // view always represents the properties of the first column.
612 return;
613 }
614
615 const ViewProperties props(url);
616
617 const Mode mode = props.viewMode();
618 if (m_mode != mode) {
619 m_mode = mode;
620 createView();
621 emit modeChanged();
622
623 if (m_mode == ColumnView) {
624 // The mode has been changed to the Column View. When starting the dir
625 // lister with DolphinView::startDirLister() it is important to give a
626 // hint that the dir lister may not keep the current directory
627 // although this is the default for showing a hierarchy.
628 m_initializeColumnView = true;
629 }
630 }
631 if (itemView() == 0) {
632 createView();
633 }
634 Q_ASSERT(itemView() != 0);
635 Q_ASSERT(m_fileItemDelegate != 0);
636
637 const bool showHiddenFiles = props.showHiddenFiles();
638 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
639 m_dirLister->setShowingDotFiles(showHiddenFiles);
640 emit showHiddenFilesChanged();
641 }
642
643 const bool categorized = props.categorizedSorting();
644 if (categorized != categorizedSorting()) {
645 if (supportsCategorizedSorting()) {
646 Q_ASSERT(m_iconsView != 0);
647 if (categorized) {
648 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
649 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
650 } else {
651 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
652 m_iconsView->setItemCategorizer(0);
653 delete categorizer;
654 }
655 }
656 emit categorizedSortingChanged();
657 }
658
659 const DolphinView::Sorting sorting = props.sorting();
660 if (sorting != m_proxyModel->sorting()) {
661 m_proxyModel->setSorting(sorting);
662 emit sortingChanged(sorting);
663 }
664
665 const Qt::SortOrder sortOrder = props.sortOrder();
666 if (sortOrder != m_proxyModel->sortOrder()) {
667 m_proxyModel->setSortOrder(sortOrder);
668 emit sortOrderChanged(sortOrder);
669 }
670
671 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
672 if (info != m_fileItemDelegate->additionalInformation()) {
673 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
674 m_fileItemDelegate->setAdditionalInformation(info);
675 emit additionalInfoChanged(info);
676 }
677
678 const bool showPreview = props.showPreview();
679 if (showPreview != m_controller->showPreview()) {
680 m_controller->setShowPreview(showPreview);
681 emit showPreviewChanged();
682 }
683 }
684
685 void DolphinView::changeSelection(const QList<KFileItem>& selection)
686 {
687 clearSelection();
688 if (selection.isEmpty()) {
689 return;
690 }
691 const KUrl& baseUrl = url();
692 KUrl url;
693 QItemSelection new_selection;
694 foreach(const KFileItem& item, selection) {
695 url = item.url().upUrl();
696 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
697 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(item));
698 new_selection.select(index, index);
699 }
700 }
701 itemView()->selectionModel()->select(new_selection,
702 QItemSelectionModel::ClearAndSelect
703 | QItemSelectionModel::Current);
704 }
705
706 void DolphinView::openContextMenu(const QPoint& pos)
707 {
708 KFileItem item;
709
710 const QModelIndex index = itemView()->indexAt(pos);
711 if (isValidNameIndex(index)) {
712 item = fileItem(index);
713 }
714
715 emit requestContextMenu(item, url());
716 }
717
718 void DolphinView::dropUrls(const KUrl::List& urls,
719 const QModelIndex& index,
720 QWidget* source)
721 {
722 KFileItem directory;
723 if (isValidNameIndex(index)) {
724 KFileItem item = fileItem(index);
725 Q_ASSERT(!item.isNull());
726 if (item.isDir()) {
727 // the URLs are dropped above a directory
728 directory = item;
729 }
730 }
731
732 if ((directory.isNull()) && (source == itemView())) {
733 // The dropping is done into the same viewport where
734 // the dragging has been started. Just ignore this...
735 return;
736 }
737
738 const KUrl& destination = (directory.isNull()) ?
739 url() : directory.url();
740 dropUrls(urls, destination);
741 }
742
743 void DolphinView::dropUrls(const KUrl::List& urls,
744 const KUrl& destination)
745 {
746 emit urlsDropped(urls, destination);
747 }
748
749 void DolphinView::updateSorting(DolphinView::Sorting sorting)
750 {
751 ViewProperties props(viewPropertiesUrl());
752 props.setSorting(sorting);
753
754 m_proxyModel->setSorting(sorting);
755
756 emit sortingChanged(sorting);
757 }
758
759 void DolphinView::updateSortOrder(Qt::SortOrder order)
760 {
761 ViewProperties props(viewPropertiesUrl());
762 props.setSortOrder(order);
763
764 m_proxyModel->setSortOrder(order);
765
766 emit sortOrderChanged(order);
767 }
768
769 void DolphinView::emitContentsMoved()
770 {
771 // only emit the contents moved signal if:
772 // - no directory loading is ongoing (this would reset the contents position
773 // always to (0, 0))
774 // - if the Column View is active: the column view does an automatic
775 // positioning during the loading operation, which must be remembered
776 if (!m_loadingDirectory || isColumnViewActive()) {
777 const QPoint pos(contentsPosition());
778 emit contentsMoved(pos.x(), pos.y());
779 }
780 }
781
782 void DolphinView::updateCutItems()
783 {
784 // restore the icons of all previously selected items to the
785 // original state...
786 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
787 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
788 while (it != end) {
789 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
790 if (index.isValid()) {
791 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
792 }
793 ++it;
794 }
795 m_cutItemsCache.clear();
796
797 // ... and apply an item effect to all currently cut items
798 applyCutItemEffect();
799 }
800
801 void DolphinView::showHoverInformation(const QModelIndex& index)
802 {
803 if (hasSelection()) {
804 return;
805 }
806
807 const KFileItem item = fileItem(index);
808 if (!item.isNull()) {
809 emit requestItemInfo(item);
810 }
811 }
812
813 void DolphinView::clearHoverInformation()
814 {
815 emit requestItemInfo(KFileItem());
816 }
817
818
819 void DolphinView::createView()
820 {
821 // delete current view
822 QAbstractItemView* view = itemView();
823 if (view != 0) {
824 m_topLayout->removeWidget(view);
825 view->close();
826 if (view == m_iconsView) {
827 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
828 m_iconsView->setItemCategorizer(0);
829 delete categorizer;
830 }
831 view->deleteLater();
832 view = 0;
833 m_iconsView = 0;
834 m_detailsView = 0;
835 m_columnView = 0;
836 m_fileItemDelegate = 0;
837 }
838
839 Q_ASSERT(m_iconsView == 0);
840 Q_ASSERT(m_detailsView == 0);
841 Q_ASSERT(m_columnView == 0);
842
843 // ... and recreate it representing the current mode
844 switch (m_mode) {
845 case IconsView:
846 m_iconsView = new DolphinIconsView(this, m_controller);
847 view = m_iconsView;
848 break;
849
850 case DetailsView:
851 m_detailsView = new DolphinDetailsView(this, m_controller);
852 view = m_detailsView;
853 break;
854
855 case ColumnView:
856 m_columnView = new DolphinColumnView(this, m_controller);
857 view = m_columnView;
858 break;
859 }
860
861 Q_ASSERT(view != 0);
862
863 m_fileItemDelegate = new KFileItemDelegate(view);
864 view->setItemDelegate(m_fileItemDelegate);
865
866 view->setModel(m_proxyModel);
867 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
868
869 new KMimeTypeResolver(view, m_dirModel);
870 m_topLayout->insertWidget(1, view);
871
872 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
873 this, SLOT(emitSelectionChangedSignal()));
874 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
875 this, SLOT(emitContentsMoved()));
876 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
877 this, SLOT(emitContentsMoved()));
878 }
879
880 QAbstractItemView* DolphinView::itemView() const
881 {
882 if (m_detailsView != 0) {
883 return m_detailsView;
884 } else if (m_columnView != 0) {
885 return m_columnView;
886 }
887
888 return m_iconsView;
889 }
890
891 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
892 {
893 return index.isValid() && (index.column() == KDirModel::Name);
894 }
895
896 bool DolphinView::isCutItem(const KFileItem& item) const
897 {
898 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
899 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
900
901 const KUrl& itemUrl = item.url();
902 KUrl::List::const_iterator it = cutUrls.begin();
903 const KUrl::List::const_iterator end = cutUrls.end();
904 while (it != end) {
905 if (*it == itemUrl) {
906 return true;
907 }
908 ++it;
909 }
910
911 return false;
912 }
913
914 void DolphinView::applyCutItemEffect()
915 {
916 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
917 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
918 return;
919 }
920
921 KFileItemList items(m_dirLister->items());
922 KFileItemList::const_iterator it = items.begin();
923 const KFileItemList::const_iterator end = items.end();
924 while (it != end) {
925 KFileItem* item = *it;
926 if (isCutItem(*item)) {
927 const QModelIndex index = m_dirModel->indexForItem(*item);
928 // Huh? the item is already known
929 //const KFileItem item = m_dirModel->itemForIndex(index);
930 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
931 if (value.type() == QVariant::Icon) {
932 const QIcon icon(qvariant_cast<QIcon>(value));
933 QPixmap pixmap = icon.pixmap(128, 128);
934
935 // remember current pixmap for the item to be able
936 // to restore it when other items get cut
937 CutItem cutItem;
938 cutItem.url = item->url();
939 cutItem.pixmap = pixmap;
940 m_cutItemsCache.append(cutItem);
941
942 // apply icon effect to the cut item
943 KIconEffect iconEffect;
944 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
945 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
946 }
947 }
948 ++it;
949 }
950 }
951
952 void DolphinView::updateViewportColor()
953 {
954 QColor color = KColorScheme(KColorScheme::View).background();
955 if (m_active) {
956 emit urlChanged(url());
957 emit selectionChanged(selectedItems());
958 } else {
959 color.setAlpha(0);
960 }
961
962 QWidget* viewport = itemView()->viewport();
963 QPalette palette;
964 palette.setColor(viewport->backgroundRole(), color);
965 viewport->setPalette(palette);
966 }
967
968 #include "dolphinview.moc"