]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
icon renamings:
[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 emit categorizedSortingChanged();
208 }
209
210 loadDirectory(viewPropsUrl);
211
212 emit modeChanged();
213 }
214
215 DolphinView::Mode DolphinView::mode() const
216 {
217 return m_mode;
218 }
219
220 void DolphinView::setShowPreview(bool show)
221 {
222 const KUrl viewPropsUrl = viewPropertiesUrl();
223 ViewProperties props(viewPropsUrl);
224 props.setShowPreview(show);
225
226 m_controller->setShowPreview(show);
227 emit showPreviewChanged();
228
229 loadDirectory(viewPropsUrl, true);
230 }
231
232 bool DolphinView::showPreview() const
233 {
234 return m_controller->showPreview();
235 }
236
237 void DolphinView::setShowHiddenFiles(bool show)
238 {
239 if (m_dirLister->showingDotFiles() == show) {
240 return;
241 }
242
243 const KUrl viewPropsUrl = viewPropertiesUrl();
244 ViewProperties props(viewPropsUrl);
245 props.setShowHiddenFiles(show);
246
247 m_dirLister->setShowingDotFiles(show);
248 m_controller->setShowHiddenFiles(show);
249 emit showHiddenFilesChanged();
250
251 loadDirectory(viewPropsUrl, true);
252 }
253
254 bool DolphinView::showHiddenFiles() const
255 {
256 return m_dirLister->showingDotFiles();
257 }
258
259 void DolphinView::setCategorizedSorting(bool categorized)
260 {
261 if (categorized == categorizedSorting()) {
262 return;
263 }
264
265 // setCategorizedSorting(true) may only get invoked
266 // if the view supports categorized sorting
267 Q_ASSERT(!categorized || supportsCategorizedSorting());
268
269 ViewProperties props(viewPropertiesUrl());
270 props.setCategorizedSorting(categorized);
271 props.save();
272
273 m_storedCategorizedSorting = categorized;
274 m_proxyModel->setCategorizedModel(categorized);
275
276 emit categorizedSortingChanged();
277 }
278
279 bool DolphinView::categorizedSorting() const
280 {
281 // If all view modes would support categorized sorting, returning
282 // m_proxyModel->isCategorizedModel() would be the way to go. As
283 // currently only the icons view supports caterized sorting, we remember
284 // the stored view properties state in m_storedCategorizedSorting and
285 // return this state. The application takes care to disable the corresponding
286 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
287 // that this setting is not applied to the current view mode.
288 return m_storedCategorizedSorting;
289 }
290
291 bool DolphinView::supportsCategorizedSorting() const
292 {
293 return m_iconsView != 0;
294 }
295
296 void DolphinView::selectAll()
297 {
298 itemView()->selectAll();
299 }
300
301 void DolphinView::invertSelection()
302 {
303 if (isColumnViewActive()) {
304 // QAbstractItemView does not offer a virtual method invertSelection()
305 // as counterpart to QAbstractItemView::selectAll(). This makes it
306 // necessary to delegate the inverting of the selection to the
307 // column view, as only the selection of the active column should
308 // get inverted.
309 m_columnView->invertSelection();
310 } else {
311 QItemSelectionModel* selectionModel = itemView()->selectionModel();
312 const QAbstractItemModel* itemModel = selectionModel->model();
313
314 const QModelIndex topLeft = itemModel->index(0, 0);
315 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
316 itemModel->columnCount() - 1);
317
318 const QItemSelection selection(topLeft, bottomRight);
319 selectionModel->select(selection, QItemSelectionModel::Toggle);
320 }
321 }
322
323 bool DolphinView::hasSelection() const
324 {
325 return itemView()->selectionModel()->hasSelection();
326 }
327
328 void DolphinView::clearSelection()
329 {
330 itemView()->selectionModel()->clear();
331 }
332
333 KFileItemList DolphinView::selectedItems() const
334 {
335 const QAbstractItemView* view = itemView();
336
337 // Our view has a selection, we will map them back to the DolphinModel
338 // and then fill the KFileItemList.
339 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
340
341 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
342 KFileItemList itemList;
343
344 const QModelIndexList indexList = selection.indexes();
345 foreach (QModelIndex index, indexList) {
346 KFileItem item = m_dolphinModel->itemForIndex(index);
347 if (!item.isNull()) {
348 itemList.append(item);
349 }
350 }
351
352 return itemList;
353 }
354
355 KUrl::List DolphinView::selectedUrls() const
356 {
357 KUrl::List urls;
358 const KFileItemList list = selectedItems();
359 foreach (KFileItem item, list) {
360 urls.append(item.url());
361 }
362 return urls;
363 }
364
365 KFileItem DolphinView::fileItem(const QModelIndex& index) const
366 {
367 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
368 return m_dolphinModel->itemForIndex(dolphinModelIndex);
369 }
370
371 void DolphinView::setContentsPosition(int x, int y)
372 {
373 QAbstractItemView* view = itemView();
374
375 // the ColumnView takes care itself for the horizontal scrolling
376 if (!isColumnViewActive()) {
377 view->horizontalScrollBar()->setValue(x);
378 }
379 view->verticalScrollBar()->setValue(y);
380
381 m_loadingDirectory = false;
382 }
383
384 QPoint DolphinView::contentsPosition() const
385 {
386 const int x = itemView()->horizontalScrollBar()->value();
387 const int y = itemView()->verticalScrollBar()->value();
388 return QPoint(x, y);
389 }
390
391 void DolphinView::zoomIn()
392 {
393 m_controller->triggerZoomIn();
394 }
395
396 void DolphinView::zoomOut()
397 {
398 m_controller->triggerZoomOut();
399 }
400
401 bool DolphinView::isZoomInPossible() const
402 {
403 return m_controller->isZoomInPossible();
404 }
405
406 bool DolphinView::isZoomOutPossible() const
407 {
408 return m_controller->isZoomOutPossible();
409 }
410
411 void DolphinView::setSorting(Sorting sorting)
412 {
413 if (sorting != this->sorting()) {
414 updateSorting(sorting);
415 }
416 }
417
418 DolphinView::Sorting DolphinView::sorting() const
419 {
420 return m_proxyModel->sorting();
421 }
422
423 void DolphinView::setSortOrder(Qt::SortOrder order)
424 {
425 if (sortOrder() != order) {
426 updateSortOrder(order);
427 }
428 }
429
430 Qt::SortOrder DolphinView::sortOrder() const
431 {
432 return m_proxyModel->sortOrder();
433 }
434
435 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
436 {
437 const KUrl viewPropsUrl = viewPropertiesUrl();
438 ViewProperties props(viewPropsUrl);
439 props.setAdditionalInfo(info);
440
441 m_controller->setAdditionalInfoCount(info.count());
442 m_fileItemDelegate->setShowInformation(info);
443
444 emit additionalInfoChanged(info);
445 loadDirectory(viewPropsUrl, true);
446 }
447
448 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
449 {
450 return m_fileItemDelegate->showInformation();
451 }
452
453 void DolphinView::reload()
454 {
455 setUrl(url());
456 loadDirectory(url(), true);
457 }
458
459 void DolphinView::refresh()
460 {
461 const bool oldActivationState = m_active;
462 m_active = true;
463
464 createView();
465 applyViewProperties(m_controller->url());
466 reload();
467
468 setActive(oldActivationState);
469 }
470
471 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
472 {
473 if (m_controller->url() == url) {
474 return;
475 }
476
477 m_controller->setUrl(url); // emits urlChanged, which we forward
478
479 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
480 applyViewProperties(rootUrl);
481 loadDirectory(rootUrl);
482 if (itemView() == m_columnView) {
483 m_columnView->setRootUrl(rootUrl);
484 m_columnView->showColumn(url);
485 }
486 } else {
487 applyViewProperties(url);
488 loadDirectory(url);
489 }
490
491 emit startedPathLoading(url);
492 }
493
494 void DolphinView::setNameFilter(const QString& nameFilter)
495 {
496 // The name filter of KDirLister does a 'hard' filtering, which
497 // means that only the items are shown where the names match
498 // exactly the filter. This is non-transparent for the user, which
499 // just wants to have a 'soft' filtering: does the name contain
500 // the filter string?
501 QString adjustedFilter(nameFilter);
502 adjustedFilter.insert(0, '*');
503 adjustedFilter.append('*');
504
505 m_dirLister->setNameFilter(adjustedFilter);
506 m_dirLister->emitChanges();
507
508 if (isColumnViewActive()) {
509 // adjusting the directory lister is not enough in the case of the
510 // column view, as each column has its own directory lister internally...
511 m_columnView->setNameFilter(nameFilter);
512 }
513 }
514
515 void DolphinView::calculateItemCount(int& fileCount, int& folderCount)
516 {
517 foreach (KFileItem item, m_dirLister->items()) {
518 if (item.isDir()) {
519 ++folderCount;
520 } else {
521 ++fileCount;
522 }
523 }
524 }
525
526 void DolphinView::setUrl(const KUrl& url)
527 {
528 updateView(url, KUrl());
529 }
530
531 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
532 {
533 QWidget::mouseReleaseEvent(event);
534 setActive(true);
535 }
536 void DolphinView::activate()
537 {
538 setActive(true);
539 }
540
541 void DolphinView::triggerItem(const KFileItem& item)
542 {
543 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
544 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
545 // items are selected by the user, hence don't trigger the
546 // item specified by 'index'
547 return;
548 }
549
550 if (item.isNull()) {
551 return;
552 }
553
554 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
555 }
556
557 void DolphinView::generatePreviews(const KFileItemList& items)
558 {
559 if (m_controller->showPreview()) {
560 KIO::PreviewJob* job = KIO::filePreview(items, 128);
561 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
562 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
563 }
564 }
565
566 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
567 {
568 Q_ASSERT(!item.isNull());
569 if (item.url().directory() != m_dirLister->url().path()) {
570 // the preview job is still working on items of an older URL, hence
571 // the item is not part of the directory model anymore
572 return;
573 }
574
575 const QModelIndex idx = m_dolphinModel->indexForItem(item);
576 if (idx.isValid() && (idx.column() == 0)) {
577 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
578 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
579 KIconEffect iconEffect;
580 const QPixmap cutPixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
581 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
582 } else {
583 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
584 }
585 }
586 }
587
588 void DolphinView::emitSelectionChangedSignal()
589 {
590 emit selectionChanged(DolphinView::selectedItems());
591 }
592
593 void DolphinView::loadDirectory(const KUrl& url, bool reload)
594 {
595 if (!url.isValid()) {
596 const QString location(url.pathOrUrl());
597 if (location.isEmpty()) {
598 emit errorMessage(i18nc("@info:status", "The location is empty."));
599 } else {
600 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
601 }
602 return;
603 }
604
605 m_cutItemsCache.clear();
606 m_loadingDirectory = true;
607
608 m_dirLister->stop();
609 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
610
611 if (isColumnViewActive()) {
612 // adjusting the directory lister is not enough in the case of the
613 // column view, as each column has its own directory lister internally...
614 if (reload) {
615 m_columnView->reload();
616 } else {
617 m_columnView->showColumn(url);
618 }
619 }
620 }
621
622 KUrl DolphinView::viewPropertiesUrl() const
623 {
624 if (isColumnViewActive()) {
625 return m_dirLister->url();
626 }
627
628 return url();
629 }
630
631 void DolphinView::applyViewProperties(const KUrl& url)
632 {
633 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
634 // The column view is active, hence don't apply the view properties
635 // of sub directories (represented by columns) to the view. The
636 // view always represents the properties of the first column.
637 return;
638 }
639
640 const ViewProperties props(url);
641
642 const Mode mode = props.viewMode();
643 if (m_mode != mode) {
644 m_mode = mode;
645 createView();
646 emit modeChanged();
647 }
648 if (itemView() == 0) {
649 createView();
650 }
651 Q_ASSERT(itemView() != 0);
652 Q_ASSERT(m_fileItemDelegate != 0);
653
654 const bool showHiddenFiles = props.showHiddenFiles();
655 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
656 m_dirLister->setShowingDotFiles(showHiddenFiles);
657 m_controller->setShowHiddenFiles(showHiddenFiles);
658 emit showHiddenFilesChanged();
659 }
660
661 m_storedCategorizedSorting = props.categorizedSorting();
662 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
663 if (categorized != m_proxyModel->isCategorizedModel()) {
664 m_proxyModel->setCategorizedModel(categorized);
665 emit categorizedSortingChanged();
666 }
667
668 const DolphinView::Sorting sorting = props.sorting();
669 if (sorting != m_proxyModel->sorting()) {
670 m_proxyModel->setSorting(sorting);
671 emit sortingChanged(sorting);
672 }
673
674 const Qt::SortOrder sortOrder = props.sortOrder();
675 if (sortOrder != m_proxyModel->sortOrder()) {
676 m_proxyModel->setSortOrder(sortOrder);
677 emit sortOrderChanged(sortOrder);
678 }
679
680 KFileItemDelegate::InformationList info = props.additionalInfo();
681 if (info != m_fileItemDelegate->showInformation()) {
682 m_controller->setAdditionalInfoCount(info.count());
683 m_fileItemDelegate->setShowInformation(info);
684 emit additionalInfoChanged(info);
685 }
686
687 const bool showPreview = props.showPreview();
688 if (showPreview != m_controller->showPreview()) {
689 m_controller->setShowPreview(showPreview);
690 emit showPreviewChanged();
691 }
692 }
693
694 void DolphinView::changeSelection(const KFileItemList& selection)
695 {
696 clearSelection();
697 if (selection.isEmpty()) {
698 return;
699 }
700 const KUrl& baseUrl = url();
701 KUrl url;
702 QItemSelection new_selection;
703 foreach(const KFileItem& item, selection) {
704 url = item.url().upUrl();
705 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
706 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
707 new_selection.select(index, index);
708 }
709 }
710 itemView()->selectionModel()->select(new_selection,
711 QItemSelectionModel::ClearAndSelect
712 | QItemSelectionModel::Current);
713 }
714
715 void DolphinView::openContextMenu(const QPoint& pos)
716 {
717 KFileItem item;
718
719 const QModelIndex index = itemView()->indexAt(pos);
720 if (isValidNameIndex(index)) {
721 item = fileItem(index);
722 }
723
724 emit requestContextMenu(item, url());
725 }
726
727 void DolphinView::dropUrls(const KUrl::List& urls,
728 const KUrl& destPath,
729 const QModelIndex& destIndex,
730 QWidget* source)
731 {
732 KFileItem directory;
733 if (isValidNameIndex(destIndex)) {
734 KFileItem item = fileItem(destIndex);
735 Q_ASSERT(!item.isNull());
736 if (item.isDir()) {
737 // the URLs are dropped above a directory
738 directory = item;
739 }
740 }
741
742 if ((directory.isNull()) && (source == itemView())) {
743 // The dropping is done into the same viewport where
744 // the dragging has been started. Just ignore this...
745 return;
746 }
747
748 const KUrl& destination = (directory.isNull()) ?
749 destPath : directory.url();
750 dropUrls(urls, destination);
751 }
752
753 void DolphinView::dropUrls(const KUrl::List& urls,
754 const KUrl& destination)
755 {
756 emit urlsDropped(urls, destination);
757 }
758
759 void DolphinView::updateSorting(DolphinView::Sorting sorting)
760 {
761 ViewProperties props(viewPropertiesUrl());
762 props.setSorting(sorting);
763
764 m_proxyModel->setSorting(sorting);
765
766 emit sortingChanged(sorting);
767 }
768
769 void DolphinView::updateSortOrder(Qt::SortOrder order)
770 {
771 ViewProperties props(viewPropertiesUrl());
772 props.setSortOrder(order);
773
774 m_proxyModel->setSortOrder(order);
775
776 emit sortOrderChanged(order);
777 }
778
779 void DolphinView::emitContentsMoved()
780 {
781 // only emit the contents moved signal if:
782 // - no directory loading is ongoing (this would reset the contents position
783 // always to (0, 0))
784 // - if the Column View is active: the column view does an automatic
785 // positioning during the loading operation, which must be remembered
786 if (!m_loadingDirectory || isColumnViewActive()) {
787 const QPoint pos(contentsPosition());
788 emit contentsMoved(pos.x(), pos.y());
789 }
790 }
791
792 void DolphinView::updateCutItems()
793 {
794 // restore the icons of all previously selected items to the
795 // original state...
796 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
797 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
798 while (it != end) {
799 const QModelIndex index = m_dolphinModel->indexForUrl((*it).url);
800 if (index.isValid()) {
801 m_dolphinModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
802 }
803 ++it;
804 }
805 m_cutItemsCache.clear();
806
807 // ... and apply an item effect to all currently cut items
808 applyCutItemEffect();
809 }
810
811 void DolphinView::showHoverInformation(const KFileItem& item)
812 {
813 if (hasSelection()) {
814 return;
815 }
816
817 emit requestItemInfo(item);
818 }
819
820 void DolphinView::clearHoverInformation()
821 {
822 emit requestItemInfo(KFileItem());
823 }
824
825
826 void DolphinView::createView()
827 {
828 KFileItemDelegate::InformationList infoList;
829 if (m_fileItemDelegate != 0) {
830 infoList = m_fileItemDelegate->showInformation();
831 }
832
833 // delete current view
834 QAbstractItemView* view = itemView();
835 if (view != 0) {
836 m_topLayout->removeWidget(view);
837 view->close();
838 view->deleteLater();
839 view = 0;
840 m_iconsView = 0;
841 m_detailsView = 0;
842 m_columnView = 0;
843 m_fileItemDelegate = 0;
844 }
845
846 Q_ASSERT(m_iconsView == 0);
847 Q_ASSERT(m_detailsView == 0);
848 Q_ASSERT(m_columnView == 0);
849
850 // ... and recreate it representing the current mode
851 switch (m_mode) {
852 case IconsView: {
853 m_iconsView = new DolphinIconsView(this, m_controller);
854 view = m_iconsView;
855 break;
856 }
857
858 case DetailsView:
859 m_detailsView = new DolphinDetailsView(this, m_controller);
860 view = m_detailsView;
861 break;
862
863 case ColumnView:
864 m_columnView = new DolphinColumnView(this, m_controller);
865 view = m_columnView;
866 break;
867 }
868
869 Q_ASSERT(view != 0);
870
871 m_fileItemDelegate = new KFileItemDelegate(view);
872 m_fileItemDelegate->setShowInformation(infoList);
873 view->setItemDelegate(m_fileItemDelegate);
874
875 view->setModel(m_proxyModel);
876 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
877
878 new KMimeTypeResolver(view, m_dolphinModel);
879 m_topLayout->insertWidget(1, view);
880
881 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
882 this, SLOT(emitSelectionChangedSignal()));
883 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
884 this, SLOT(emitContentsMoved()));
885 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
886 this, SLOT(emitContentsMoved()));
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() == DolphinModel::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 const KFileItem item = *it;
935 if (isCutItem(item)) {
936 const QModelIndex index = m_dolphinModel->indexForItem(item);
937 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
938 if (value.type() == QVariant::Icon) {
939 const QIcon icon(qvariant_cast<QIcon>(value));
940 QPixmap pixmap = icon.pixmap(128, 128);
941
942 // remember current pixmap for the item to be able
943 // to restore it when other items get cut
944 CutItem cutItem;
945 cutItem.url = item.url();
946 cutItem.pixmap = pixmap;
947 m_cutItemsCache.append(cutItem);
948
949 // apply icon effect to the cut item
950 KIconEffect iconEffect;
951 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
952 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
953 }
954 }
955 ++it;
956 }
957 }
958
959 KToggleAction* DolphinView::iconsModeAction(KActionCollection* actionCollection)
960 {
961 KToggleAction* iconsView = actionCollection->add<KToggleAction>("icons");
962 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
963 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
964 iconsView->setIcon(KIcon("fileview-icon"));
965 iconsView->setData(QVariant::fromValue(IconsView));
966 return iconsView;
967 }
968
969 KToggleAction* DolphinView::detailsModeAction(KActionCollection* actionCollection)
970 {
971 KToggleAction* detailsView = actionCollection->add<KToggleAction>("details");
972 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
973 detailsView->setShortcut(Qt::CTRL | Qt::Key_2);
974 detailsView->setIcon(KIcon("fileview-detailed"));
975 detailsView->setData(QVariant::fromValue(DetailsView));
976 return detailsView;
977 }
978
979 KToggleAction* DolphinView::columnsModeAction(KActionCollection* actionCollection)
980 {
981 KToggleAction* columnView = actionCollection->add<KToggleAction>("columns");
982 columnView->setText(i18nc("@action:inmenu View Mode", "Columns"));
983 columnView->setShortcut(Qt::CTRL | Qt::Key_3);
984 columnView->setIcon(KIcon("fileview-column"));
985 columnView->setData(QVariant::fromValue(ColumnView));
986 return columnView;
987 }
988
989 QString DolphinView::currentViewModeActionName() const
990 {
991 switch (m_mode) {
992 case DolphinView::IconsView:
993 return "icons";
994 case DolphinView::DetailsView:
995 return "details";
996 case DolphinView::ColumnView:
997 return "columns";
998 }
999 return QString(); // can't happen
1000 }
1001
1002 #include "dolphinview.moc"