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