]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
DolphinPart: provide a way to switch between view modes in konqueror.
[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 #include <ktoggleaction.h>
23 #include <kactioncollection.h>
24
25 #include <QApplication>
26 #include <QClipboard>
27 #include <QKeyEvent>
28 #include <QItemSelection>
29 #include <QBoxLayout>
30 #include <QTimer>
31 #include <QScrollBar>
32
33 #include <kcolorscheme.h>
34 #include <kdirlister.h>
35 #include <kfileitemdelegate.h>
36 #include <klocale.h>
37 #include <kiconeffect.h>
38 #include <kio/netaccess.h>
39 #include <kio/renamedialog.h>
40 #include <kio/previewjob.h>
41 #include <kmimetyperesolver.h>
42 #include <konqmimedata.h>
43 #include <konq_operations.h>
44 #include <kurl.h>
45
46 #include "dolphinmodel.h"
47 #include "dolphincolumnview.h"
48 #include "dolphincontroller.h"
49 #include "dolphinsortfilterproxymodel.h"
50 #include "dolphindetailsview.h"
51 #include "dolphiniconsview.h"
52 #include "renamedialog.h"
53 #include "viewproperties.h"
54 #include "dolphinsettings.h"
55 #include "dolphin_generalsettings.h"
56
57 DolphinView::DolphinView(QWidget* parent,
58 const KUrl& url,
59 KDirLister* dirLister,
60 DolphinModel* dolphinModel,
61 DolphinSortFilterProxyModel* proxyModel) :
62 QWidget(parent),
63 m_active(true),
64 m_loadingDirectory(false),
65 m_storedCategorizedSorting(false),
66 m_mode(DolphinView::IconsView),
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_dolphinModel(dolphinModel),
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
94 // Receiver of the DolphinView signal 'urlChanged()' don't need
95 // to care whether the internal controller changed the URL already or whether
96 // the controller just requested an URL change and will be updated later.
97 // In both cases the URL has been changed:
98 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
99 this, SIGNAL(urlChanged(const KUrl&)));
100 connect(m_controller, SIGNAL(requestUrlChange(const KUrl&)),
101 this, SIGNAL(urlChanged(const KUrl&)));
102
103 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
104 this, SLOT(openContextMenu(const QPoint&)));
105 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&, const QModelIndex&, QWidget*)),
106 this, SLOT(dropUrls(const KUrl::List&, const KUrl&, const QModelIndex&, QWidget*)));
107 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
108 this, SLOT(updateSorting(DolphinView::Sorting)));
109 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
110 this, SLOT(updateSortOrder(Qt::SortOrder)));
111 connect(m_controller, SIGNAL(itemTriggered(const KFileItem&)),
112 this, SLOT(triggerItem(const KFileItem&)));
113 connect(m_controller, SIGNAL(activated()),
114 this, SLOT(activate()));
115 connect(m_controller, SIGNAL(itemEntered(const KFileItem&)),
116 this, SLOT(showHoverInformation(const KFileItem&)));
117 connect(m_controller, SIGNAL(viewportEntered()),
118 this, SLOT(clearHoverInformation()));
119
120 applyViewProperties(url);
121 m_topLayout->addWidget(itemView());
122 }
123
124 DolphinView::~DolphinView()
125 {
126 }
127
128 const KUrl& DolphinView::url() const
129 {
130 return m_controller->url();
131 }
132
133 KUrl DolphinView::rootUrl() const
134 {
135 return isColumnViewActive() ? m_columnView->rootUrl() : url();
136 }
137
138 void DolphinView::setActive(bool active)
139 {
140 if (active == m_active) {
141 return;
142 }
143
144 m_active = active;
145
146 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
147 if (active) {
148 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
149 // bypasses the problem when having a split view and changing the active view to
150 // update the some URL dependent states. A nicer approach should be no big deal...
151 emit urlChanged(url());
152 emit selectionChanged(selectedItems());
153 } else {
154 color.setAlpha(150);
155 }
156
157 QWidget* viewport = itemView()->viewport();
158 QPalette palette;
159 palette.setColor(viewport->backgroundRole(), color);
160 viewport->setPalette(palette);
161
162 update();
163
164 if (active) {
165 emit activated();
166 }
167
168 m_controller->indicateActivationChange(active);
169 }
170
171 bool DolphinView::isActive() const
172 {
173 return m_active;
174 }
175
176 void DolphinView::setMode(Mode mode)
177 {
178 if (mode == m_mode) {
179 return; // the wished mode is already set
180 }
181
182 m_mode = mode;
183
184 if (isColumnViewActive()) {
185 // When changing the mode in the column view, it makes sense
186 // to go back to the root URL of the column view automatically.
187 // Otherwise there it would not be possible to turn off the column view
188 // without focusing the first column.
189 const KUrl root = rootUrl();
190 setUrl(root);
191 m_controller->setUrl(root);
192 }
193
194 const KUrl viewPropsUrl = viewPropertiesUrl();
195 ViewProperties props(viewPropsUrl);
196 props.setViewMode(m_mode);
197
198 createView();
199
200 // Not all view modes support categorized sorting. Adjust the sorting model
201 // if changing the view mode results in a change of the categorized sorting
202 // capabilities.
203 m_storedCategorizedSorting = props.categorizedSorting();
204 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
205 if (categorized != m_proxyModel->isCategorizedModel()) {
206 m_proxyModel->setCategorizedModel(categorized);
207 m_proxyModel->sort(m_proxyModel->sortColumn(), m_proxyModel->sortOrder());
208 emit categorizedSortingChanged();
209 }
210
211 loadDirectory(viewPropsUrl);
212
213 emit modeChanged();
214 }
215
216 DolphinView::Mode DolphinView::mode() const
217 {
218 return m_mode;
219 }
220
221 void DolphinView::setShowPreview(bool show)
222 {
223 const KUrl viewPropsUrl = viewPropertiesUrl();
224 ViewProperties props(viewPropsUrl);
225 props.setShowPreview(show);
226
227 m_controller->setShowPreview(show);
228 emit showPreviewChanged();
229
230 loadDirectory(viewPropsUrl, true);
231 }
232
233 bool DolphinView::showPreview() const
234 {
235 return m_controller->showPreview();
236 }
237
238 void DolphinView::setShowHiddenFiles(bool show)
239 {
240 if (m_dirLister->showingDotFiles() == show) {
241 return;
242 }
243
244 const KUrl viewPropsUrl = viewPropertiesUrl();
245 ViewProperties props(viewPropsUrl);
246 props.setShowHiddenFiles(show);
247
248 m_dirLister->setShowingDotFiles(show);
249 m_controller->setShowHiddenFiles(show);
250 emit showHiddenFilesChanged();
251
252 loadDirectory(viewPropsUrl, true);
253 }
254
255 bool DolphinView::showHiddenFiles() const
256 {
257 return m_dirLister->showingDotFiles();
258 }
259
260 void DolphinView::setCategorizedSorting(bool categorized)
261 {
262 if (categorized == categorizedSorting()) {
263 return;
264 }
265
266 // setCategorizedSorting(true) may only get invoked
267 // if the view supports categorized sorting
268 Q_ASSERT(!categorized || supportsCategorizedSorting());
269
270 ViewProperties props(viewPropertiesUrl());
271 props.setCategorizedSorting(categorized);
272 props.save();
273
274 m_storedCategorizedSorting = categorized;
275 m_proxyModel->setCategorizedModel(categorized);
276 m_proxyModel->sort(m_proxyModel->sortColumn(), m_proxyModel->sortOrder());
277
278 emit categorizedSortingChanged();
279 }
280
281 bool DolphinView::categorizedSorting() const
282 {
283 // If all view modes would support categorized sorting, returning
284 // m_proxyModel->isCategorizedModel() would be the way to go. As
285 // currently only the icons view supports caterized sorting, we remember
286 // the stored view properties state in m_storedCategorizedSorting and
287 // return this state. The application takes care to disable the corresponding
288 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
289 // that this setting is not applied to the current view mode.
290 return m_storedCategorizedSorting;
291 }
292
293 bool DolphinView::supportsCategorizedSorting() const
294 {
295 return m_iconsView != 0;
296 }
297
298 void DolphinView::selectAll()
299 {
300 itemView()->selectAll();
301 }
302
303 void DolphinView::invertSelection()
304 {
305 if (isColumnViewActive()) {
306 // QAbstractItemView does not offer a virtual method invertSelection()
307 // as counterpart to QAbstractItemView::selectAll(). This makes it
308 // necessary to delegate the inverting of the selection to the
309 // column view, as only the selection of the active column should
310 // get inverted.
311 m_columnView->invertSelection();
312 } else {
313 QItemSelectionModel* selectionModel = itemView()->selectionModel();
314 const QAbstractItemModel* itemModel = selectionModel->model();
315
316 const QModelIndex topLeft = itemModel->index(0, 0);
317 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
318 itemModel->columnCount() - 1);
319
320 const QItemSelection selection(topLeft, bottomRight);
321 selectionModel->select(selection, QItemSelectionModel::Toggle);
322 }
323 }
324
325 bool DolphinView::hasSelection() const
326 {
327 return itemView()->selectionModel()->hasSelection();
328 }
329
330 void DolphinView::clearSelection()
331 {
332 itemView()->selectionModel()->clear();
333 }
334
335 KFileItemList DolphinView::selectedItems() const
336 {
337 const QAbstractItemView* view = itemView();
338
339 // Our view has a selection, we will map them back to the DolphinModel
340 // and then fill the KFileItemList.
341 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
342
343 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
344 KFileItemList itemList;
345
346 const QModelIndexList indexList = selection.indexes();
347 foreach (QModelIndex index, indexList) {
348 KFileItem item = m_dolphinModel->itemForIndex(index);
349 if (!item.isNull()) {
350 itemList.append(item);
351 }
352 }
353
354 return itemList;
355 }
356
357 KUrl::List DolphinView::selectedUrls() const
358 {
359 KUrl::List urls;
360 const KFileItemList list = selectedItems();
361 foreach (KFileItem item, list) {
362 urls.append(item.url());
363 }
364 return urls;
365 }
366
367 KFileItem DolphinView::fileItem(const QModelIndex& index) const
368 {
369 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
370 return m_dolphinModel->itemForIndex(dolphinModelIndex);
371 }
372
373 void DolphinView::setContentsPosition(int x, int y)
374 {
375 QAbstractItemView* view = itemView();
376
377 // the ColumnView takes care itself for the horizontal scrolling
378 if (!isColumnViewActive()) {
379 view->horizontalScrollBar()->setValue(x);
380 }
381 view->verticalScrollBar()->setValue(y);
382
383 m_loadingDirectory = false;
384 }
385
386 QPoint DolphinView::contentsPosition() const
387 {
388 const int x = itemView()->horizontalScrollBar()->value();
389 const int y = itemView()->verticalScrollBar()->value();
390 return QPoint(x, y);
391 }
392
393 void DolphinView::zoomIn()
394 {
395 m_controller->triggerZoomIn();
396 }
397
398 void DolphinView::zoomOut()
399 {
400 m_controller->triggerZoomOut();
401 }
402
403 bool DolphinView::isZoomInPossible() const
404 {
405 return m_controller->isZoomInPossible();
406 }
407
408 bool DolphinView::isZoomOutPossible() const
409 {
410 return m_controller->isZoomOutPossible();
411 }
412
413 void DolphinView::setSorting(Sorting sorting)
414 {
415 if (sorting != this->sorting()) {
416 updateSorting(sorting);
417 }
418 }
419
420 DolphinView::Sorting DolphinView::sorting() const
421 {
422 return m_proxyModel->sorting();
423 }
424
425 void DolphinView::setSortOrder(Qt::SortOrder order)
426 {
427 if (sortOrder() != order) {
428 updateSortOrder(order);
429 }
430 }
431
432 Qt::SortOrder DolphinView::sortOrder() const
433 {
434 return m_proxyModel->sortOrder();
435 }
436
437 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
438 {
439 const KUrl viewPropsUrl = viewPropertiesUrl();
440 ViewProperties props(viewPropsUrl);
441 props.setAdditionalInfo(info);
442
443 m_controller->setAdditionalInfoCount(info.count());
444 m_fileItemDelegate->setShowInformation(info);
445
446 emit additionalInfoChanged(info);
447 loadDirectory(viewPropsUrl, true);
448 }
449
450 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
451 {
452 return m_fileItemDelegate->showInformation();
453 }
454
455 void DolphinView::reload()
456 {
457 setUrl(url());
458 loadDirectory(url(), true);
459 }
460
461 void DolphinView::refresh()
462 {
463 const bool oldActivationState = m_active;
464 m_active = true;
465
466 createView();
467 applyViewProperties(m_controller->url());
468 reload();
469
470 setActive(oldActivationState);
471 }
472
473 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
474 {
475 if (m_controller->url() == url) {
476 return;
477 }
478
479 m_controller->setUrl(url); // emits urlChanged, which we forward
480
481 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
482 applyViewProperties(rootUrl);
483 loadDirectory(rootUrl);
484 if (itemView() == m_columnView) {
485 m_columnView->setRootUrl(rootUrl);
486 m_columnView->showColumn(url);
487 }
488 } else {
489 applyViewProperties(url);
490 loadDirectory(url);
491 }
492
493 itemView()->setFocus();
494
495 emit startedPathLoading(url);
496 }
497
498 void DolphinView::setNameFilter(const QString& nameFilter)
499 {
500 // The name filter of KDirLister does a 'hard' filtering, which
501 // means that only the items are shown where the names match
502 // exactly the filter. This is non-transparent for the user, which
503 // just wants to have a 'soft' filtering: does the name contain
504 // the filter string?
505 QString adjustedFilter(nameFilter);
506 adjustedFilter.insert(0, '*');
507 adjustedFilter.append('*');
508
509 m_dirLister->setNameFilter(adjustedFilter);
510 m_dirLister->emitChanges();
511
512 if (isColumnViewActive()) {
513 // adjusting the directory lister is not enough in the case of the
514 // column view, as each column has its own directory lister internally...
515 m_columnView->setNameFilter(nameFilter);
516 }
517 }
518
519 void DolphinView::calculateItemCount(int& fileCount, int& folderCount)
520 {
521 foreach (KFileItem item, m_dirLister->items()) {
522 if (item.isDir()) {
523 ++folderCount;
524 } else {
525 ++fileCount;
526 }
527 }
528 }
529
530 void DolphinView::setUrl(const KUrl& url)
531 {
532 updateView(url, KUrl());
533 }
534
535 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
536 {
537 QWidget::mouseReleaseEvent(event);
538 setActive(true);
539 }
540 void DolphinView::activate()
541 {
542 setActive(true);
543 }
544
545 void DolphinView::triggerItem(const KFileItem& item)
546 {
547 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
548 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
549 // items are selected by the user, hence don't trigger the
550 // item specified by 'index'
551 return;
552 }
553
554 if (item.isNull()) {
555 return;
556 }
557
558 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
559 }
560
561 void DolphinView::generatePreviews(const KFileItemList& items)
562 {
563 if (m_controller->showPreview()) {
564 KIO::PreviewJob* job = KIO::filePreview(items, 128);
565 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
566 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
567 }
568 }
569
570 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
571 {
572 Q_ASSERT(!item.isNull());
573 if (item.url().directory() != m_dirLister->url().path()) {
574 // the preview job is still working on items of an older URL, hence
575 // the item is not part of the directory model anymore
576 return;
577 }
578
579 const QModelIndex idx = m_dolphinModel->indexForItem(item);
580 if (idx.isValid() && (idx.column() == 0)) {
581 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
582 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
583 KIconEffect iconEffect;
584 const QPixmap cutPixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
585 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
586 } else {
587 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
588 }
589 }
590 }
591
592 void DolphinView::emitSelectionChangedSignal()
593 {
594 emit selectionChanged(DolphinView::selectedItems());
595 }
596
597 void DolphinView::loadDirectory(const KUrl& url, bool reload)
598 {
599 if (!url.isValid()) {
600 const QString location(url.pathOrUrl());
601 if (location.isEmpty()) {
602 emit errorMessage(i18nc("@info:status", "The location is empty."));
603 } else {
604 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
605 }
606 return;
607 }
608
609 m_cutItemsCache.clear();
610 m_loadingDirectory = true;
611
612 m_dirLister->stop();
613 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
614
615 if (isColumnViewActive()) {
616 // adjusting the directory lister is not enough in the case of the
617 // column view, as each column has its own directory lister internally...
618 if (reload) {
619 m_columnView->reload();
620 } else {
621 m_columnView->showColumn(url);
622 }
623 }
624 }
625
626 KUrl DolphinView::viewPropertiesUrl() const
627 {
628 if (isColumnViewActive()) {
629 return m_dirLister->url();
630 }
631
632 return url();
633 }
634
635 void DolphinView::applyViewProperties(const KUrl& url)
636 {
637 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
638 // The column view is active, hence don't apply the view properties
639 // of sub directories (represented by columns) to the view. The
640 // view always represents the properties of the first column.
641 return;
642 }
643
644 const ViewProperties props(url);
645
646 const Mode mode = props.viewMode();
647 if (m_mode != mode) {
648 m_mode = mode;
649 createView();
650 emit modeChanged();
651 }
652 if (itemView() == 0) {
653 createView();
654 }
655 Q_ASSERT(itemView() != 0);
656 Q_ASSERT(m_fileItemDelegate != 0);
657
658 const bool showHiddenFiles = props.showHiddenFiles();
659 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
660 m_dirLister->setShowingDotFiles(showHiddenFiles);
661 m_controller->setShowHiddenFiles(showHiddenFiles);
662 emit showHiddenFilesChanged();
663 }
664
665 m_storedCategorizedSorting = props.categorizedSorting();
666 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
667 if (categorized != m_proxyModel->isCategorizedModel()) {
668 m_proxyModel->setCategorizedModel(categorized);
669 m_proxyModel->sort(m_proxyModel->sortColumn(), m_proxyModel->sortOrder());
670 emit categorizedSortingChanged();
671 }
672
673 const DolphinView::Sorting sorting = props.sorting();
674 if (sorting != m_proxyModel->sorting()) {
675 m_proxyModel->setSorting(sorting);
676 emit sortingChanged(sorting);
677 }
678
679 const Qt::SortOrder sortOrder = props.sortOrder();
680 if (sortOrder != m_proxyModel->sortOrder()) {
681 m_proxyModel->setSortOrder(sortOrder);
682 emit sortOrderChanged(sortOrder);
683 }
684
685 KFileItemDelegate::InformationList info = props.additionalInfo();
686 if (info != m_fileItemDelegate->showInformation()) {
687 m_controller->setAdditionalInfoCount(info.count());
688 m_fileItemDelegate->setShowInformation(info);
689 emit additionalInfoChanged(info);
690 }
691
692 const bool showPreview = props.showPreview();
693 if (showPreview != m_controller->showPreview()) {
694 m_controller->setShowPreview(showPreview);
695 emit showPreviewChanged();
696 }
697 }
698
699 void DolphinView::changeSelection(const KFileItemList& selection)
700 {
701 clearSelection();
702 if (selection.isEmpty()) {
703 return;
704 }
705 const KUrl& baseUrl = url();
706 KUrl url;
707 QItemSelection new_selection;
708 foreach(const KFileItem& item, selection) {
709 url = item.url().upUrl();
710 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
711 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
712 new_selection.select(index, index);
713 }
714 }
715 itemView()->selectionModel()->select(new_selection,
716 QItemSelectionModel::ClearAndSelect
717 | QItemSelectionModel::Current);
718 }
719
720 void DolphinView::openContextMenu(const QPoint& pos)
721 {
722 KFileItem item;
723
724 const QModelIndex index = itemView()->indexAt(pos);
725 if (isValidNameIndex(index)) {
726 item = fileItem(index);
727 }
728
729 emit requestContextMenu(item, url());
730 }
731
732 void DolphinView::dropUrls(const KUrl::List& urls,
733 const KUrl& destPath,
734 const QModelIndex& destIndex,
735 QWidget* source)
736 {
737 KFileItem directory;
738 if (isValidNameIndex(destIndex)) {
739 KFileItem item = fileItem(destIndex);
740 Q_ASSERT(!item.isNull());
741 if (item.isDir()) {
742 // the URLs are dropped above a directory
743 directory = item;
744 }
745 }
746
747 if ((directory.isNull()) && (source == itemView())) {
748 // The dropping is done into the same viewport where
749 // the dragging has been started. Just ignore this...
750 return;
751 }
752
753 const KUrl& destination = (directory.isNull()) ?
754 destPath : directory.url();
755 dropUrls(urls, destination);
756 }
757
758 void DolphinView::dropUrls(const KUrl::List& urls,
759 const KUrl& destination)
760 {
761 emit urlsDropped(urls, destination);
762 }
763
764 void DolphinView::updateSorting(DolphinView::Sorting sorting)
765 {
766 ViewProperties props(viewPropertiesUrl());
767 props.setSorting(sorting);
768
769 m_proxyModel->setSorting(sorting);
770
771 emit sortingChanged(sorting);
772 }
773
774 void DolphinView::updateSortOrder(Qt::SortOrder order)
775 {
776 ViewProperties props(viewPropertiesUrl());
777 props.setSortOrder(order);
778
779 m_proxyModel->setSortOrder(order);
780
781 emit sortOrderChanged(order);
782 }
783
784 void DolphinView::emitContentsMoved()
785 {
786 // only emit the contents moved signal if:
787 // - no directory loading is ongoing (this would reset the contents position
788 // always to (0, 0))
789 // - if the Column View is active: the column view does an automatic
790 // positioning during the loading operation, which must be remembered
791 if (!m_loadingDirectory || isColumnViewActive()) {
792 const QPoint pos(contentsPosition());
793 emit contentsMoved(pos.x(), pos.y());
794 }
795 }
796
797 void DolphinView::updateCutItems()
798 {
799 // restore the icons of all previously selected items to the
800 // original state...
801 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
802 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
803 while (it != end) {
804 const QModelIndex index = m_dolphinModel->indexForUrl((*it).url);
805 if (index.isValid()) {
806 m_dolphinModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
807 }
808 ++it;
809 }
810 m_cutItemsCache.clear();
811
812 // ... and apply an item effect to all currently cut items
813 applyCutItemEffect();
814 }
815
816 void DolphinView::showHoverInformation(const KFileItem& item)
817 {
818 if (hasSelection()) {
819 return;
820 }
821
822 emit requestItemInfo(item);
823 }
824
825 void DolphinView::clearHoverInformation()
826 {
827 emit requestItemInfo(KFileItem());
828 }
829
830
831 void DolphinView::createView()
832 {
833 KFileItemDelegate::InformationList infoList;
834 if (m_fileItemDelegate != 0) {
835 infoList = m_fileItemDelegate->showInformation();
836 }
837
838 // delete current view
839 QAbstractItemView* view = itemView();
840 if (view != 0) {
841 m_topLayout->removeWidget(view);
842 view->close();
843 view->deleteLater();
844 view = 0;
845 m_iconsView = 0;
846 m_detailsView = 0;
847 m_columnView = 0;
848 m_fileItemDelegate = 0;
849 }
850
851 Q_ASSERT(m_iconsView == 0);
852 Q_ASSERT(m_detailsView == 0);
853 Q_ASSERT(m_columnView == 0);
854
855 // ... and recreate it representing the current mode
856 switch (m_mode) {
857 case IconsView: {
858 m_iconsView = new DolphinIconsView(this, m_controller);
859 view = m_iconsView;
860 break;
861 }
862
863 case DetailsView:
864 m_detailsView = new DolphinDetailsView(this, m_controller);
865 view = m_detailsView;
866 break;
867
868 case ColumnView:
869 m_columnView = new DolphinColumnView(this, m_controller);
870 view = m_columnView;
871 break;
872 }
873
874 Q_ASSERT(view != 0);
875
876 m_fileItemDelegate = new KFileItemDelegate(view);
877 m_fileItemDelegate->setShowInformation(infoList);
878 view->setItemDelegate(m_fileItemDelegate);
879
880 view->setModel(m_proxyModel);
881 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
882
883 new KMimeTypeResolver(view, m_dolphinModel);
884 m_topLayout->insertWidget(1, view);
885
886 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
887 this, SLOT(emitSelectionChangedSignal()));
888 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
889 this, SLOT(emitContentsMoved()));
890 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
891 this, SLOT(emitContentsMoved()));
892 view->setFocus();
893 }
894
895 QAbstractItemView* DolphinView::itemView() const
896 {
897 if (m_detailsView != 0) {
898 return m_detailsView;
899 } else if (m_columnView != 0) {
900 return m_columnView;
901 }
902
903 return m_iconsView;
904 }
905
906 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
907 {
908 return index.isValid() && (index.column() == DolphinModel::Name);
909 }
910
911 bool DolphinView::isCutItem(const KFileItem& item) const
912 {
913 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
914 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
915
916 const KUrl& itemUrl = item.url();
917 KUrl::List::const_iterator it = cutUrls.begin();
918 const KUrl::List::const_iterator end = cutUrls.end();
919 while (it != end) {
920 if (*it == itemUrl) {
921 return true;
922 }
923 ++it;
924 }
925
926 return false;
927 }
928
929 void DolphinView::applyCutItemEffect()
930 {
931 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
932 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
933 return;
934 }
935
936 KFileItemList items(m_dirLister->items());
937 KFileItemList::const_iterator it = items.begin();
938 const KFileItemList::const_iterator end = items.end();
939 while (it != end) {
940 const KFileItem item = *it;
941 if (isCutItem(item)) {
942 const QModelIndex index = m_dolphinModel->indexForItem(item);
943 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
944 if (value.type() == QVariant::Icon) {
945 const QIcon icon(qvariant_cast<QIcon>(value));
946 QPixmap pixmap = icon.pixmap(128, 128);
947
948 // remember current pixmap for the item to be able
949 // to restore it when other items get cut
950 CutItem cutItem;
951 cutItem.url = item.url();
952 cutItem.pixmap = pixmap;
953 m_cutItemsCache.append(cutItem);
954
955 // apply icon effect to the cut item
956 KIconEffect iconEffect;
957 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
958 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
959 }
960 }
961 ++it;
962 }
963 }
964
965 KToggleAction* DolphinView::iconsModeAction(KActionCollection* actionCollection)
966 {
967 KToggleAction* iconsView = actionCollection->add<KToggleAction>("icons");
968 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
969 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
970 iconsView->setIcon(KIcon("fileview-icon"));
971 iconsView->setData(QVariant::fromValue(IconsView));
972 return iconsView;
973 }
974
975 KToggleAction* DolphinView::detailsModeAction(KActionCollection* actionCollection)
976 {
977 KToggleAction* detailsView = actionCollection->add<KToggleAction>("details");
978 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
979 detailsView->setShortcut(Qt::CTRL | Qt::Key_2);
980 detailsView->setIcon(KIcon("fileview-detailed"));
981 detailsView->setData(QVariant::fromValue(DetailsView));
982 return detailsView;
983 }
984
985 KToggleAction* DolphinView::columnsModeAction(KActionCollection* actionCollection)
986 {
987 KToggleAction* columnView = actionCollection->add<KToggleAction>("columns");
988 columnView->setText(i18nc("@action:inmenu View Mode", "Columns"));
989 columnView->setShortcut(Qt::CTRL | Qt::Key_3);
990 columnView->setIcon(KIcon("fileview-column"));
991 columnView->setData(QVariant::fromValue(ColumnView));
992 return columnView;
993 }
994
995 QString DolphinView::currentViewModeActionName() const
996 {
997 switch (m_mode) {
998 case DolphinView::IconsView:
999 return "icons";
1000 case DolphinView::DetailsView:
1001 return "details";
1002 case DolphinView::ColumnView:
1003 return "columns";
1004 }
1005 return QString(); // can't happen
1006 }
1007
1008 #include "dolphinview.moc"