]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
fix for languages having more than one plural form (thanks to Andrius Štikonas for...
[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 <kdirmodel.h>
32 #include <kdirlister.h>
33 #include <kfileitemdelegate.h>
34 #include <kglobalsettings.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 Mode mode) :
62 QWidget(parent),
63 m_active(true),
64 m_loadingDirectory(false),
65 m_initializeColumnView(false),
66 m_mode(mode),
67 m_topLayout(0),
68 m_controller(0),
69 m_iconsView(0),
70 m_detailsView(0),
71 m_columnView(0),
72 m_fileItemDelegate(0),
73 m_dirModel(dirModel),
74 m_dirLister(dirLister),
75 m_proxyModel(proxyModel)
76 {
77 setFocusPolicy(Qt::StrongFocus);
78 m_topLayout = new QVBoxLayout(this);
79 m_topLayout->setSpacing(0);
80 m_topLayout->setMargin(0);
81
82 QClipboard* clipboard = QApplication::clipboard();
83 connect(clipboard, SIGNAL(dataChanged()),
84 this, SLOT(updateCutItems()));
85
86 connect(m_dirLister, SIGNAL(completed()),
87 this, SLOT(updateCutItems()));
88 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
89 this, SLOT(generatePreviews(const KFileItemList&)));
90
91 m_controller = new DolphinController(this);
92 m_controller->setUrl(url);
93 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
94 this, SLOT(openContextMenu(const QPoint&)));
95 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&, QWidget*)),
96 this, SLOT(dropUrls(const KUrl::List&, 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 createView();
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 QColor color = KGlobalSettings::baseColor();
137 if (active) {
138 emit urlChanged(url());
139 emit selectionChanged(selectedItems());
140 } else {
141 color.setAlpha(0);
142 }
143
144 QWidget* viewport = itemView()->viewport();
145 QPalette palette;
146 palette.setColor(viewport->backgroundRole(), color);
147 viewport->setPalette(palette);
148
149 update();
150
151 if (active) {
152 emit activated();
153 }
154 }
155
156 bool DolphinView::isActive() const
157 {
158 return m_active;
159 }
160
161 void DolphinView::setMode(Mode mode)
162 {
163 if (mode == m_mode) {
164 return; // the wished mode is already set
165 }
166
167 m_mode = mode;
168
169 if (isColumnViewActive()) {
170 // When changing the mode in the column view, it makes sense
171 // to go back to the root URL of the column view automatically.
172 // Otherwise there it would not be possible to turn off the column view
173 // without focusing the first column.
174 // TODO: reactivate again after DolphinView/DolphinViewController split works
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 reload();
421 }
422
423 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
424 {
425 QWidget::mouseReleaseEvent(event);
426 setActive(true);;
427 }
428 void DolphinView::activate()
429 {
430 setActive(true);
431 }
432
433 void DolphinView::triggerItem(const QModelIndex& index)
434 {
435 if (!isValidNameIndex(index)) {
436 clearSelection();
437 showHoverInformation(index);
438 return;
439 }
440
441 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
442 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
443 // items are selected by the user, hence don't trigger the
444 // item specified by 'index'
445 return;
446 }
447
448 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
449 if (item == 0) {
450 return;
451 }
452
453 // Prefer the local path over the URL. This assures that the
454 // volume space information is correct. Assuming that the URL is media:/sda1,
455 // and the local path is /windows/C: For the URL the space info is related
456 // to the root partition (and hence wrong) and for the local path the space
457 // info is related to the windows partition (-> correct).
458 const QString localPath(item->localPath());
459 KUrl url;
460 if (localPath.isEmpty()) {
461 url = item->url();
462 } else {
463 url = localPath;
464 }
465
466 if (item->isDir()) {
467 setUrl(url);
468 } else if (item->isFile()) {
469 // allow to browse through ZIP and tar files
470 KMimeType::Ptr mime = item->mimeTypePtr();
471 if (mime->is("application/zip")) {
472 url.setProtocol("zip");
473 setUrl(url);
474 } else if (mime->is("application/x-tar") ||
475 mime->is("application/x-tarz") ||
476 mime->is("application/x-bzip-compressed-tar") ||
477 mime->is("application/x-compressed-tar") ||
478 mime->is("application/x-tzo")) {
479 url.setProtocol("tar");
480 setUrl(url);
481 } else {
482 item->run();
483 }
484 } else {
485 item->run();
486 }
487 }
488
489 void DolphinView::generatePreviews(const KFileItemList& items)
490 {
491 if (m_controller->showPreview()) {
492
493 // Must turn QList<KFileItem *> to QList<KFileItem>...
494 QList<KFileItem> itemsToPreview;
495 foreach( KFileItem* it, items )
496 itemsToPreview.append( *it );
497
498 KIO::PreviewJob* job = KIO::filePreview(itemsToPreview, 128);
499 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
500 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
501 }
502 }
503
504 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
505 {
506 Q_ASSERT(!item.isNull());
507 if (item.url().directory() != m_dirLister->url().path()) {
508 // the preview job is still working on items of an older URL, hence
509 // the item is not part of the directory model anymore
510 return;
511 }
512
513 const QModelIndex idx = m_dirModel->indexForItem(item);
514 if (idx.isValid() && (idx.column() == 0)) {
515 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
516 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
517 KIconEffect iconEffect;
518 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
519 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
520 } else {
521 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
522 }
523 }
524 }
525
526 void DolphinView::emitSelectionChangedSignal()
527 {
528 emit selectionChanged(DolphinView::selectedItems());
529 }
530
531 void DolphinView::startDirLister(const KUrl& url, bool reload)
532 {
533 if (!url.isValid()) {
534 const QString location(url.pathOrUrl());
535 if (location.isEmpty()) {
536 emit errorMessage(i18n("The location is empty."));
537 } else {
538 emit errorMessage(i18n("The location '%1' is invalid.", location));
539 }
540 return;
541 }
542
543 m_cutItemsCache.clear();
544 m_loadingDirectory = true;
545
546 m_dirLister->stop();
547
548 bool openDir = true;
549 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
550 m_initializeColumnView = false;
551
552 if (keepOldDirs) {
553 if (reload) {
554 keepOldDirs = false;
555
556 const KUrl& dirListerUrl = m_dirLister->url();
557 if (dirListerUrl.isValid()) {
558 const KUrl::List dirs = m_dirLister->directories();
559 KUrl url;
560 foreach(url, dirs) {
561 m_dirLister->updateDirectory(url);
562 }
563 openDir = false;
564 }
565 } else if (m_dirLister->directories().contains(url)) {
566 // The dir lister contains the directory already, so
567 // KDirLister::openUrl() may not been invoked twice.
568 m_dirLister->updateDirectory(url);
569 openDir = false;
570 } else {
571 const KUrl& dirListerUrl = m_dirLister->url();
572 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
573 // The current URL is not a child of the dir lister
574 // URL. This may happen when e. g. a bookmark has been selected
575 // and hence the view must be reset.
576 keepOldDirs = false;
577 }
578 }
579 }
580
581 if (openDir) {
582 m_dirLister->openUrl(url, keepOldDirs, reload);
583 }
584 }
585
586 void DolphinView::setUrl(const KUrl& url)
587 {
588 if (m_controller->url() == url) {
589 return;
590 }
591
592 m_controller->setUrl(url);
593
594 const ViewProperties props(url);
595
596 const Mode mode = props.viewMode();
597 bool changeMode = (m_mode != mode);
598 if (changeMode && isColumnViewActive()) {
599 // The column view is active. Only change the
600 // mode if the current URL is no child of the column view.
601 if (m_dirLister->url().isParentOf(url)) {
602 changeMode = false;
603 }
604 }
605
606 if (changeMode) {
607 m_mode = mode;
608 createView();
609 emit modeChanged();
610
611 if (m_mode == ColumnView) {
612 // The mode has been changed to the Column View. When starting the dir
613 // lister with DolphinView::startDirLister() it is important to give a
614 // hint that the dir lister may not keep the current directory
615 // although this is the default for showing a hierarchy.
616 m_initializeColumnView = true;
617 }
618 }
619
620 const bool showHiddenFiles = props.showHiddenFiles();
621 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
622 m_dirLister->setShowingDotFiles(showHiddenFiles);
623 emit showHiddenFilesChanged();
624 }
625
626 const bool categorized = props.categorizedSorting();
627 if (categorized != categorizedSorting()) {
628 if (supportsCategorizedSorting()) {
629 Q_ASSERT(m_iconsView != 0);
630 if (categorized) {
631 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
632 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
633 } else {
634 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
635 m_iconsView->setItemCategorizer(0);
636 delete categorizer;
637 }
638 }
639 emit categorizedSortingChanged();
640 }
641
642 const DolphinView::Sorting sorting = props.sorting();
643 if (sorting != m_proxyModel->sorting()) {
644 m_proxyModel->setSorting(sorting);
645 emit sortingChanged(sorting);
646 }
647
648 const Qt::SortOrder sortOrder = props.sortOrder();
649 if (sortOrder != m_proxyModel->sortOrder()) {
650 m_proxyModel->setSortOrder(sortOrder);
651 emit sortOrderChanged(sortOrder);
652 }
653
654 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
655 if (info != m_fileItemDelegate->additionalInformation()) {
656 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
657 m_fileItemDelegate->setAdditionalInformation(info);
658 emit additionalInfoChanged(info);
659 }
660
661 const bool showPreview = props.showPreview();
662 if (showPreview != m_controller->showPreview()) {
663 m_controller->setShowPreview(showPreview);
664 emit showPreviewChanged();
665 }
666
667 startDirLister(url);
668 emit urlChanged(url);
669 }
670
671 void DolphinView::changeSelection(const KFileItemList& selection)
672 {
673 clearSelection();
674 if (selection.isEmpty()) {
675 return;
676 }
677 const KUrl& baseUrl = url();
678 KUrl url;
679 QItemSelection new_selection;
680 foreach(KFileItem* item, selection) {
681 url = item->url().upUrl();
682 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
683 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(*item));
684 new_selection.select(index, index);
685 }
686 }
687 itemView()->selectionModel()->select(new_selection,
688 QItemSelectionModel::ClearAndSelect
689 | QItemSelectionModel::Current);
690 }
691
692 void DolphinView::openContextMenu(const QPoint& pos)
693 {
694 KFileItem* item = 0;
695
696 const QModelIndex index = itemView()->indexAt(pos);
697 if (isValidNameIndex(index)) {
698 item = fileItem(index);
699 }
700
701 emit requestContextMenu(item, url());
702 }
703
704 void DolphinView::dropUrls(const KUrl::List& urls,
705 const QModelIndex& index,
706 QWidget* source)
707 {
708 KFileItem* directory = 0;
709 if (isValidNameIndex(index)) {
710 KFileItem* item = fileItem(index);
711 Q_ASSERT(item != 0);
712 if (item->isDir()) {
713 // the URLs are dropped above a directory
714 directory = item;
715 }
716 }
717
718 if ((directory == 0) && (source == itemView())) {
719 // The dropping is done into the same viewport where
720 // the dragging has been started. Just ignore this...
721 return;
722 }
723
724 const KUrl& destination = (directory == 0) ?
725 url() : directory->url();
726 dropUrls(urls, destination);
727 }
728
729 void DolphinView::dropUrls(const KUrl::List& urls,
730 const KUrl& destination)
731 {
732 emit urlsDropped(urls, destination);
733 }
734
735 void DolphinView::updateSorting(DolphinView::Sorting sorting)
736 {
737 ViewProperties props(url());
738 props.setSorting(sorting);
739
740 m_proxyModel->setSorting(sorting);
741
742 emit sortingChanged(sorting);
743 }
744
745 void DolphinView::updateSortOrder(Qt::SortOrder order)
746 {
747 ViewProperties props(url());
748 props.setSortOrder(order);
749
750 m_proxyModel->setSortOrder(order);
751
752 emit sortOrderChanged(order);
753 }
754
755 void DolphinView::emitContentsMoved()
756 {
757 // only emit the contents moved signal if:
758 // - no directory loading is ongoing (this would reset the contents position
759 // always to (0, 0))
760 // - if the Column View is active: the column view does an automatic
761 // positioning during the loading operation, which must be remembered
762 if (!m_loadingDirectory || isColumnViewActive()) {
763 const QPoint pos(contentsPosition());
764 emit contentsMoved(pos.x(), pos.y());
765 }
766 }
767
768 void DolphinView::updateCutItems()
769 {
770 // restore the icons of all previously selected items to the
771 // original state...
772 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
773 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
774 while (it != end) {
775 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
776 if (index.isValid()) {
777 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
778 }
779 ++it;
780 }
781 m_cutItemsCache.clear();
782
783 // ... and apply an item effect to all currently cut items
784 applyCutItemEffect();
785 }
786
787 void DolphinView::showHoverInformation(const QModelIndex& index)
788 {
789 if (hasSelection()) {
790 return;
791 }
792
793 const KFileItem* item = fileItem(index);
794 if (item != 0) {
795 emit requestItemInfo(item->url());
796 }
797 }
798
799 void DolphinView::clearHoverInformation()
800 {
801 emit requestItemInfo(KUrl());
802 }
803
804
805 void DolphinView::createView()
806 {
807 // delete current view
808 QAbstractItemView* view = itemView();
809 if (view != 0) {
810 m_topLayout->removeWidget(view);
811 view->close();
812 if (view == m_iconsView) {
813 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
814 m_iconsView->setItemCategorizer(0);
815 delete categorizer;
816 }
817 view->deleteLater();
818 view = 0;
819 m_iconsView = 0;
820 m_detailsView = 0;
821 m_columnView = 0;
822 m_fileItemDelegate = 0;
823 }
824
825 Q_ASSERT(m_iconsView == 0);
826 Q_ASSERT(m_detailsView == 0);
827 Q_ASSERT(m_columnView == 0);
828
829 // ... and recreate it representing the current mode
830 switch (m_mode) {
831 case IconsView:
832 m_iconsView = new DolphinIconsView(this, m_controller);
833 view = m_iconsView;
834 break;
835
836 case DetailsView:
837 m_detailsView = new DolphinDetailsView(this, m_controller);
838 view = m_detailsView;
839 break;
840
841 case ColumnView:
842 m_columnView = new DolphinColumnView(this, m_controller);
843 view = m_columnView;
844 break;
845 }
846
847 Q_ASSERT(view != 0);
848
849 m_fileItemDelegate = new KFileItemDelegate(view);
850 view->setItemDelegate(m_fileItemDelegate);
851
852 view->setModel(m_proxyModel);
853 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
854
855 new KMimeTypeResolver(view, m_dirModel);
856 m_topLayout->insertWidget(1, view);
857
858 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
859 this, SLOT(emitSelectionChangedSignal()));
860 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
861 this, SLOT(emitContentsMoved()));
862 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
863 this, SLOT(emitContentsMoved()));
864 }
865
866 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
867 {
868 QItemSelectionModel* selectionModel = itemView()->selectionModel();
869 const QAbstractItemModel* itemModel = selectionModel->model();
870
871 const QModelIndex topLeft = itemModel->index(0, 0);
872 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
873 itemModel->columnCount() - 1);
874
875 QItemSelection selection(topLeft, bottomRight);
876 selectionModel->select(selection, flags);
877 }
878
879 QAbstractItemView* DolphinView::itemView() const
880 {
881 if (m_detailsView != 0) {
882 return m_detailsView;
883 } else if (m_columnView != 0) {
884 return m_columnView;
885 }
886
887 return m_iconsView;
888 }
889
890 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
891 {
892 return index.isValid() && (index.column() == KDirModel::Name);
893 }
894
895 bool DolphinView::isCutItem(const KFileItem& item) const
896 {
897 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
898 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
899
900 const KUrl& itemUrl = item.url();
901 KUrl::List::const_iterator it = cutUrls.begin();
902 const KUrl::List::const_iterator end = cutUrls.end();
903 while (it != end) {
904 if (*it == itemUrl) {
905 return true;
906 }
907 ++it;
908 }
909
910 return false;
911 }
912
913 void DolphinView::applyCutItemEffect()
914 {
915 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
916 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
917 return;
918 }
919
920 KFileItemList items(m_dirLister->items());
921 KFileItemList::const_iterator it = items.begin();
922 const KFileItemList::const_iterator end = items.end();
923 while (it != end) {
924 KFileItem* item = *it;
925 if (isCutItem(*item)) {
926 const QModelIndex index = m_dirModel->indexForItem(*item);
927 const KFileItem* item = m_dirModel->itemForIndex(index);
928 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
929 if ((value.type() == QVariant::Icon) && (item != 0)) {
930 const QIcon icon(qvariant_cast<QIcon>(value));
931 QPixmap pixmap = icon.pixmap(128, 128);
932
933 // remember current pixmap for the item to be able
934 // to restore it when other items get cut
935 CutItem cutItem;
936 cutItem.url = item->url();
937 cutItem.pixmap = pixmap;
938 m_cutItemsCache.append(cutItem);
939
940 // apply icon effect to the cut item
941 KIconEffect iconEffect;
942 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
943 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
944 }
945 }
946 ++it;
947 }
948 }
949
950 #include "dolphinview.moc"