]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Assure that the categorized sorting is applied correctly to the proxy model if the...
[dolphin.git] / src / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <QApplication>
24 #include <QClipboard>
25 #include <QKeyEvent>
26 #include <QItemSelection>
27 #include <QBoxLayout>
28 #include <QTimer>
29 #include <QScrollBar>
30
31 #include <kcolorscheme.h>
32 #include <kdirlister.h>
33 #include <kfileitemdelegate.h>
34 #include <klocale.h>
35 #include <kiconeffect.h>
36 #include <kio/netaccess.h>
37 #include <kio/renamedialog.h>
38 #include <kio/previewjob.h>
39 #include <kmimetyperesolver.h>
40 #include <konqmimedata.h>
41 #include <konq_operations.h>
42 #include <kurl.h>
43
44 #include "dolphinmodel.h"
45 #include "dolphincolumnview.h"
46 #include "dolphincontroller.h"
47 #include "dolphinsortfilterproxymodel.h"
48 #include "dolphindetailsview.h"
49 #include "dolphiniconsview.h"
50 #include "renamedialog.h"
51 #include "viewproperties.h"
52 #include "dolphinsettings.h"
53 #include "dolphin_generalsettings.h"
54
55 DolphinView::DolphinView(QWidget* parent,
56 const KUrl& url,
57 KDirLister* dirLister,
58 DolphinModel* dolphinModel,
59 DolphinSortFilterProxyModel* proxyModel) :
60 QWidget(parent),
61 m_active(true),
62 m_loadingDirectory(false),
63 m_initializeColumnView(false),
64 m_mode(DolphinView::IconsView),
65 m_topLayout(0),
66 m_controller(0),
67 m_iconsView(0),
68 m_detailsView(0),
69 m_columnView(0),
70 m_fileItemDelegate(0),
71 m_dolphinModel(dolphinModel),
72 m_dirLister(dirLister),
73 m_proxyModel(proxyModel)
74 {
75 setFocusPolicy(Qt::StrongFocus);
76 m_topLayout = new QVBoxLayout(this);
77 m_topLayout->setSpacing(0);
78 m_topLayout->setMargin(0);
79
80 QClipboard* clipboard = QApplication::clipboard();
81 connect(clipboard, SIGNAL(dataChanged()),
82 this, SLOT(updateCutItems()));
83
84 connect(m_dirLister, SIGNAL(completed()),
85 this, SLOT(updateCutItems()));
86 connect(m_dirLister, SIGNAL(newItems(const QList<KFileItem>&)),
87 this, SLOT(generatePreviews(const QList<KFileItem>&)));
88
89 m_controller = new DolphinController(this);
90 m_controller->setUrl(url);
91 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
92 this, SIGNAL(urlChanged(const KUrl&)));
93 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
94 this, SLOT(openContextMenu(const QPoint&)));
95 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&, const QModelIndex&, QWidget*)),
96 this, SLOT(dropUrls(const KUrl::List&, const KUrl&, const QModelIndex&, QWidget*)));
97 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
98 this, SLOT(updateSorting(DolphinView::Sorting)));
99 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
100 this, SLOT(updateSortOrder(Qt::SortOrder)));
101 connect(m_controller, SIGNAL(itemTriggered(const QModelIndex&)),
102 this, SLOT(triggerItem(const QModelIndex&)));
103 connect(m_controller, SIGNAL(activated()),
104 this, SLOT(activate()));
105 connect(m_controller, SIGNAL(itemEntered(const QModelIndex&)),
106 this, SLOT(showHoverInformation(const QModelIndex&)));
107 connect(m_controller, SIGNAL(viewportEntered()),
108 this, SLOT(clearHoverInformation()));
109
110 applyViewProperties(url);
111 m_topLayout->addWidget(itemView());
112 }
113
114 DolphinView::~DolphinView()
115 {
116 }
117
118 const KUrl& DolphinView::url() const
119 {
120 return m_controller->url();
121 }
122
123 KUrl DolphinView::rootUrl() const
124 {
125 return isColumnViewActive() ? m_dirLister->url() : url();
126 }
127
128 void DolphinView::setActive(bool active)
129 {
130 if (active == m_active) {
131 return;
132 }
133
134 m_active = active;
135
136 updateViewportColor();
137 update();
138
139 if (active) {
140 emit activated();
141 }
142 }
143
144 bool DolphinView::isActive() const
145 {
146 return m_active;
147 }
148
149 void DolphinView::setMode(Mode mode)
150 {
151 if (mode == m_mode) {
152 return; // the wished mode is already set
153 }
154
155 m_mode = mode;
156
157 if (isColumnViewActive()) {
158 // When changing the mode in the column view, it makes sense
159 // to go back to the root URL of the column view automatically.
160 // Otherwise there it would not be possible to turn off the column view
161 // without focusing the first column.
162 setUrl(m_dirLister->url());
163 m_controller->setUrl(m_dirLister->url());
164 }
165
166 const KUrl viewPropsUrl = viewPropertiesUrl();
167 ViewProperties props(viewPropsUrl);
168 props.setViewMode(m_mode);
169
170 createView();
171
172 // Not all view modes support categorized sorting. Adjust the sorting model
173 // if changing the view mode results in a change of the categorized sorting
174 // capabilities.
175 const bool categorized = props.categorizedSorting() && supportsCategorizedSorting();
176 if (categorized != categorizedSorting()) {
177 m_proxyModel->setCategorizedModel(categorized);
178 m_proxyModel->sort(m_proxyModel->sortColumn(), m_proxyModel->sortOrder());
179 emit categorizedSortingChanged();
180 }
181
182 startDirLister(viewPropsUrl);
183
184 emit modeChanged();
185 }
186
187 DolphinView::Mode DolphinView::mode() const
188 {
189 return m_mode;
190 }
191
192 void DolphinView::setShowPreview(bool show)
193 {
194 const KUrl viewPropsUrl = viewPropertiesUrl();
195 ViewProperties props(viewPropsUrl);
196 props.setShowPreview(show);
197
198 m_controller->setShowPreview(show);
199 emit showPreviewChanged();
200
201 startDirLister(viewPropsUrl, true);
202 }
203
204 bool DolphinView::showPreview() const
205 {
206 return m_controller->showPreview();
207 }
208
209 void DolphinView::setShowHiddenFiles(bool show)
210 {
211 if (m_dirLister->showingDotFiles() == show) {
212 return;
213 }
214
215 const KUrl viewPropsUrl = viewPropertiesUrl();
216 ViewProperties props(viewPropsUrl);
217 props.setShowHiddenFiles(show);
218
219 m_dirLister->setShowingDotFiles(show);
220 emit showHiddenFilesChanged();
221
222 startDirLister(viewPropsUrl, true);
223 }
224
225 bool DolphinView::showHiddenFiles() const
226 {
227 return m_dirLister->showingDotFiles();
228 }
229
230 void DolphinView::setCategorizedSorting(bool categorized)
231 {
232 if (categorized == categorizedSorting()) {
233 return;
234 }
235
236 // setCategorizedSorting(true) may only get invoked
237 // if the view supports categorized sorting
238 Q_ASSERT(!categorized || supportsCategorizedSorting());
239
240 ViewProperties props(viewPropertiesUrl());
241 props.setCategorizedSorting(categorized);
242 props.save();
243
244 m_proxyModel->setCategorizedModel(categorized);
245 m_proxyModel->sort(m_proxyModel->sortColumn(), m_proxyModel->sortOrder());
246
247 emit categorizedSortingChanged();
248 }
249
250 bool DolphinView::categorizedSorting() const
251 {
252 return m_proxyModel->isCategorizedModel();
253 }
254
255 bool DolphinView::supportsCategorizedSorting() const
256 {
257 return m_iconsView != 0;
258 }
259
260 void DolphinView::selectAll()
261 {
262 itemView()->selectAll();
263 }
264
265 void DolphinView::invertSelection()
266 {
267 if (isColumnViewActive()) {
268 // QAbstractItemView does not offer a virtual method invertSelection()
269 // as counterpart to QAbstractItemView::selectAll(). This makes it
270 // necessary to delegate the inverting of the selection to the
271 // column view, as only the selection of the active column should get
272 // inverted.
273 m_columnView->invertSelection();
274 } else {
275 QItemSelectionModel* selectionModel = itemView()->selectionModel();
276 const QAbstractItemModel* itemModel = selectionModel->model();
277
278 const QModelIndex topLeft = itemModel->index(0, 0);
279 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
280 itemModel->columnCount() - 1);
281
282 const QItemSelection selection(topLeft, bottomRight);
283 selectionModel->select(selection, QItemSelectionModel::Toggle);
284 }
285 }
286
287 bool DolphinView::hasSelection() const
288 {
289 return itemView()->selectionModel()->hasSelection();
290 }
291
292 void DolphinView::clearSelection()
293 {
294 itemView()->selectionModel()->clear();
295 }
296
297 QList<KFileItem> DolphinView::selectedItems() const
298 {
299 const QAbstractItemView* view = itemView();
300
301 // Our view has a selection, we will map them back to the DolphinModel
302 // and then fill the KFileItemList.
303 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
304
305 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
306 QList<KFileItem> itemList;
307
308 const QModelIndexList indexList = selection.indexes();
309 foreach (QModelIndex index, indexList) {
310 KFileItem item = m_dolphinModel->itemForIndex(index);
311 if (!item.isNull()) {
312 itemList.append(item);
313 }
314 }
315
316 return itemList;
317 }
318
319 KUrl::List DolphinView::selectedUrls() const
320 {
321 KUrl::List urls;
322 const QList<KFileItem> list = selectedItems();
323 for ( QList<KFileItem>::const_iterator it = list.begin(), end = list.end();
324 it != end; ++it ) {
325 urls.append((*it).url());
326 }
327 return urls;
328 }
329
330 KFileItem DolphinView::fileItem(const QModelIndex& index) const
331 {
332 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
333 return m_dolphinModel->itemForIndex(dolphinModelIndex);
334 }
335
336 void DolphinView::setContentsPosition(int x, int y)
337 {
338 QAbstractItemView* view = itemView();
339
340 // the ColumnView takes care itself for the horizontal scrolling
341 if (!isColumnViewActive()) {
342 view->horizontalScrollBar()->setValue(x);
343 }
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::updateView(const KUrl& url, const KUrl& rootUrl)
433 {
434 if (m_controller->url() == url) {
435 return;
436 }
437
438 const bool restoreColumnView = !rootUrl.isEmpty()
439 && !rootUrl.equals(url, KUrl::CompareWithoutTrailingSlash)
440 && rootUrl.isParentOf(url);
441
442 m_controller->setUrl(url); // emits urlChanged, which we forward
443
444 if (restoreColumnView) {
445 applyViewProperties(rootUrl);
446 Q_ASSERT(itemView() == m_columnView);
447 startDirLister(rootUrl);
448 m_columnView->showColumn(url);
449 } else {
450 applyViewProperties(url);
451 startDirLister(url);
452 }
453
454 itemView()->setFocus();
455
456 emit startedPathLoading(url);
457 }
458
459 void DolphinView::setUrl(const KUrl& url)
460 {
461 updateView(url, KUrl());
462 }
463
464 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
465 {
466 QWidget::mouseReleaseEvent(event);
467 setActive(true);
468 }
469 void DolphinView::activate()
470 {
471 setActive(true);
472 }
473
474 void DolphinView::triggerItem(const QModelIndex& index)
475 {
476 Q_ASSERT(index.isValid());
477
478 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
479 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
480 // items are selected by the user, hence don't trigger the
481 // item specified by 'index'
482 return;
483 }
484
485 const KFileItem item = m_dolphinModel->itemForIndex(m_proxyModel->mapToSource(index));
486
487 if (item.isNull()) {
488 return;
489 }
490
491 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
492 }
493
494 void DolphinView::generatePreviews(const QList<KFileItem>& items)
495 {
496 if (m_controller->showPreview()) {
497 KIO::PreviewJob* job = KIO::filePreview(items, 128);
498 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
499 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
500 }
501 }
502
503 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
504 {
505 Q_ASSERT(!item.isNull());
506 if (item.url().directory() != m_dirLister->url().path()) {
507 // the preview job is still working on items of an older URL, hence
508 // the item is not part of the directory model anymore
509 return;
510 }
511
512 const QModelIndex idx = m_dolphinModel->indexForItem(item);
513 if (idx.isValid() && (idx.column() == 0)) {
514 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
515 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
516 KIconEffect iconEffect;
517 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
518 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
519 } else {
520 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
521 }
522 }
523 }
524
525 void DolphinView::emitSelectionChangedSignal()
526 {
527 emit selectionChanged(DolphinView::selectedItems());
528 }
529
530 void DolphinView::startDirLister(const KUrl& url, bool reload)
531 {
532 if (!url.isValid()) {
533 const QString location(url.pathOrUrl());
534 if (location.isEmpty()) {
535 emit errorMessage(i18nc("@info:status", "The location is empty."));
536 } else {
537 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
538 }
539 return;
540 }
541
542 m_cutItemsCache.clear();
543 m_loadingDirectory = true;
544
545 m_dirLister->stop();
546
547 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
548 m_initializeColumnView = false;
549
550 if (keepOldDirs) {
551 // keeping old directories is only necessary for hierarchical views
552 // like the column view
553 if (reload) {
554 // for the column view it is not enough to reload the directory lister,
555 // so this task is delegated to the column view directly
556 m_columnView->reload();
557 } else if (m_dirLister->directories().contains(url)) {
558 // The dir lister contains the directory already, so
559 // KDirLister::openUrl() may not get invoked twice.
560 m_dirLister->updateDirectory(url);
561 } else {
562 const KUrl& dirListerUrl = m_dirLister->url();
563 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
564 // The current URL is not a child of the dir lister
565 // URL. This may happen when e. g. a place has been selected
566 // and hence the view must be reset.
567 m_dirLister->openUrl(url, false, false);
568 }
569 }
570 } else {
571 m_dirLister->openUrl(url, false, reload);
572 }
573 }
574
575 KUrl DolphinView::viewPropertiesUrl() const
576 {
577 if (isColumnViewActive()) {
578 return m_dirLister->url();
579 }
580
581 return url();
582 }
583
584 void DolphinView::applyViewProperties(const KUrl& url)
585 {
586 if (isColumnViewActive() && m_dirLister->url().isParentOf(url)) {
587 // The column view is active, hence don't apply the view properties
588 // of sub directories (represented by columns) to the view. The
589 // view always represents the properties of the first column.
590 return;
591 }
592
593 const ViewProperties props(url);
594
595 const Mode mode = props.viewMode();
596 if (m_mode != mode) {
597 m_mode = mode;
598 createView();
599 emit modeChanged();
600
601 if (m_mode == ColumnView) {
602 // The mode has been changed to the Column View. When starting the dir
603 // lister with DolphinView::startDirLister() it is important to give a
604 // hint that the dir lister may not keep the current directory
605 // although this is the default for showing a hierarchy.
606 m_initializeColumnView = true;
607 }
608 }
609 if (itemView() == 0) {
610 createView();
611 }
612 Q_ASSERT(itemView() != 0);
613 Q_ASSERT(m_fileItemDelegate != 0);
614
615 const bool showHiddenFiles = props.showHiddenFiles();
616 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
617 m_dirLister->setShowingDotFiles(showHiddenFiles);
618 emit showHiddenFilesChanged();
619 }
620
621 const bool categorized = props.categorizedSorting() && supportsCategorizedSorting();
622 if (categorized != categorizedSorting()) {
623 m_proxyModel->setCategorizedModel(categorized);
624 m_proxyModel->sort(m_proxyModel->sortColumn(), m_proxyModel->sortOrder());
625 emit categorizedSortingChanged();
626 }
627
628 const DolphinView::Sorting sorting = props.sorting();
629 if (sorting != m_proxyModel->sorting()) {
630 m_proxyModel->setSorting(sorting);
631 emit sortingChanged(sorting);
632 }
633
634 const Qt::SortOrder sortOrder = props.sortOrder();
635 if (sortOrder != m_proxyModel->sortOrder()) {
636 m_proxyModel->setSortOrder(sortOrder);
637 emit sortOrderChanged(sortOrder);
638 }
639
640 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
641 if (info != m_fileItemDelegate->additionalInformation()) {
642 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
643 m_fileItemDelegate->setAdditionalInformation(info);
644 emit additionalInfoChanged(info);
645 }
646
647 const bool showPreview = props.showPreview();
648 if (showPreview != m_controller->showPreview()) {
649 m_controller->setShowPreview(showPreview);
650 emit showPreviewChanged();
651 }
652 }
653
654 void DolphinView::changeSelection(const QList<KFileItem>& selection)
655 {
656 clearSelection();
657 if (selection.isEmpty()) {
658 return;
659 }
660 const KUrl& baseUrl = url();
661 KUrl url;
662 QItemSelection new_selection;
663 foreach(const KFileItem& item, selection) {
664 url = item.url().upUrl();
665 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
666 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
667 new_selection.select(index, index);
668 }
669 }
670 itemView()->selectionModel()->select(new_selection,
671 QItemSelectionModel::ClearAndSelect
672 | QItemSelectionModel::Current);
673 }
674
675 void DolphinView::openContextMenu(const QPoint& pos)
676 {
677 KFileItem item;
678
679 const QModelIndex index = itemView()->indexAt(pos);
680 if (isValidNameIndex(index)) {
681 item = fileItem(index);
682 }
683
684 emit requestContextMenu(item, url());
685 }
686
687 void DolphinView::dropUrls(const KUrl::List& urls,
688 const KUrl& destPath,
689 const QModelIndex& destIndex,
690 QWidget* source)
691 {
692 KFileItem directory;
693 if (isValidNameIndex(destIndex)) {
694 KFileItem item = fileItem(destIndex);
695 Q_ASSERT(!item.isNull());
696 if (item.isDir()) {
697 // the URLs are dropped above a directory
698 directory = item;
699 }
700 }
701
702 if ((directory.isNull()) && (source == itemView())) {
703 // The dropping is done into the same viewport where
704 // the dragging has been started. Just ignore this...
705 return;
706 }
707
708 const KUrl& destination = (directory.isNull()) ?
709 destPath : directory.url();
710 dropUrls(urls, destination);
711 }
712
713 void DolphinView::dropUrls(const KUrl::List& urls,
714 const KUrl& destination)
715 {
716 emit urlsDropped(urls, destination);
717 }
718
719 void DolphinView::updateSorting(DolphinView::Sorting sorting)
720 {
721 ViewProperties props(viewPropertiesUrl());
722 props.setSorting(sorting);
723
724 m_proxyModel->setSorting(sorting);
725
726 emit sortingChanged(sorting);
727 }
728
729 void DolphinView::updateSortOrder(Qt::SortOrder order)
730 {
731 ViewProperties props(viewPropertiesUrl());
732 props.setSortOrder(order);
733
734 m_proxyModel->setSortOrder(order);
735
736 emit sortOrderChanged(order);
737 }
738
739 void DolphinView::emitContentsMoved()
740 {
741 // only emit the contents moved signal if:
742 // - no directory loading is ongoing (this would reset the contents position
743 // always to (0, 0))
744 // - if the Column View is active: the column view does an automatic
745 // positioning during the loading operation, which must be remembered
746 if (!m_loadingDirectory || isColumnViewActive()) {
747 const QPoint pos(contentsPosition());
748 emit contentsMoved(pos.x(), pos.y());
749 }
750 }
751
752 void DolphinView::updateCutItems()
753 {
754 // restore the icons of all previously selected items to the
755 // original state...
756 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
757 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
758 while (it != end) {
759 const QModelIndex index = m_dolphinModel->indexForUrl((*it).url);
760 if (index.isValid()) {
761 m_dolphinModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
762 }
763 ++it;
764 }
765 m_cutItemsCache.clear();
766
767 // ... and apply an item effect to all currently cut items
768 applyCutItemEffect();
769 }
770
771 void DolphinView::showHoverInformation(const QModelIndex& index)
772 {
773 if (hasSelection()) {
774 return;
775 }
776
777 const KFileItem item = fileItem(index);
778 if (!item.isNull()) {
779 emit requestItemInfo(item);
780 }
781 }
782
783 void DolphinView::clearHoverInformation()
784 {
785 emit requestItemInfo(KFileItem());
786 }
787
788
789 void DolphinView::createView()
790 {
791 // delete current view
792 QAbstractItemView* view = itemView();
793 if (view != 0) {
794 m_topLayout->removeWidget(view);
795 view->close();
796 view->deleteLater();
797 view = 0;
798 m_iconsView = 0;
799 m_detailsView = 0;
800 m_columnView = 0;
801 m_fileItemDelegate = 0;
802 }
803
804 Q_ASSERT(m_iconsView == 0);
805 Q_ASSERT(m_detailsView == 0);
806 Q_ASSERT(m_columnView == 0);
807
808 // ... and recreate it representing the current mode
809 switch (m_mode) {
810 case IconsView: {
811 m_iconsView = new DolphinIconsView(this, m_controller);
812 view = m_iconsView;
813 break;
814 }
815
816 case DetailsView:
817 m_detailsView = new DolphinDetailsView(this, m_controller);
818 view = m_detailsView;
819 break;
820
821 case ColumnView:
822 m_columnView = new DolphinColumnView(this, m_controller);
823 view = m_columnView;
824 break;
825 }
826
827 Q_ASSERT(view != 0);
828
829 m_fileItemDelegate = new KFileItemDelegate(view);
830 view->setItemDelegate(m_fileItemDelegate);
831
832 view->setModel(m_proxyModel);
833 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
834
835 new KMimeTypeResolver(view, m_dolphinModel);
836 m_topLayout->insertWidget(1, view);
837
838 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
839 this, SLOT(emitSelectionChangedSignal()));
840 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
841 this, SLOT(emitContentsMoved()));
842 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
843 this, SLOT(emitContentsMoved()));
844 view->setFocus();
845 }
846
847 QAbstractItemView* DolphinView::itemView() const
848 {
849 if (m_detailsView != 0) {
850 return m_detailsView;
851 } else if (m_columnView != 0) {
852 return m_columnView;
853 }
854
855 return m_iconsView;
856 }
857
858 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
859 {
860 return index.isValid() && (index.column() == DolphinModel::Name);
861 }
862
863 bool DolphinView::isCutItem(const KFileItem& item) const
864 {
865 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
866 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
867
868 const KUrl& itemUrl = item.url();
869 KUrl::List::const_iterator it = cutUrls.begin();
870 const KUrl::List::const_iterator end = cutUrls.end();
871 while (it != end) {
872 if (*it == itemUrl) {
873 return true;
874 }
875 ++it;
876 }
877
878 return false;
879 }
880
881 void DolphinView::applyCutItemEffect()
882 {
883 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
884 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
885 return;
886 }
887
888 KFileItemList items(m_dirLister->items());
889 KFileItemList::const_iterator it = items.begin();
890 const KFileItemList::const_iterator end = items.end();
891 while (it != end) {
892 KFileItem* item = *it;
893 if (isCutItem(*item)) {
894 const QModelIndex index = m_dolphinModel->indexForItem(*item);
895 // Huh? the item is already known
896 //const KFileItem item = m_dolphinModel->itemForIndex(index);
897 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
898 if (value.type() == QVariant::Icon) {
899 const QIcon icon(qvariant_cast<QIcon>(value));
900 QPixmap pixmap = icon.pixmap(128, 128);
901
902 // remember current pixmap for the item to be able
903 // to restore it when other items get cut
904 CutItem cutItem;
905 cutItem.url = item->url();
906 cutItem.pixmap = pixmap;
907 m_cutItemsCache.append(cutItem);
908
909 // apply icon effect to the cut item
910 KIconEffect iconEffect;
911 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
912 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
913 }
914 }
915 ++it;
916 }
917 }
918
919 void DolphinView::updateViewportColor()
920 {
921 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
922 if (m_active) {
923 emit urlChanged(url()); // Hmm, this is a hack; the url hasn't really changed.
924 emit selectionChanged(selectedItems());
925 } else {
926 color.setAlpha(0);
927 }
928
929 QWidget* viewport = itemView()->viewport();
930 QPalette palette;
931 palette.setColor(viewport->backgroundRole(), color);
932 viewport->setPalette(palette);
933 }
934
935 #include "dolphinview.moc"