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