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