]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Simplify the DolphinController: The "show hidden files" state can be retrieved by...
[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 KFileItem&, QWidget*)),
106 this, SLOT(dropUrls(const KUrl::List&, const KUrl&, const KFileItem&, 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(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
112 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&)));
113 connect(m_controller, SIGNAL(itemTriggered(const KFileItem&)),
114 this, SLOT(triggerItem(const KFileItem&)));
115 connect(m_controller, SIGNAL(activated()),
116 this, SLOT(activate()));
117 connect(m_controller, SIGNAL(itemEntered(const KFileItem&)),
118 this, SLOT(showHoverInformation(const KFileItem&)));
119 connect(m_controller, SIGNAL(viewportEntered()),
120 this, SLOT(clearHoverInformation()));
121
122 applyViewProperties(url);
123 m_topLayout->addWidget(itemView());
124 }
125
126 DolphinView::~DolphinView()
127 {
128 }
129
130 const KUrl& DolphinView::url() const
131 {
132 return m_controller->url();
133 }
134
135 KUrl DolphinView::rootUrl() const
136 {
137 return isColumnViewActive() ? m_columnView->rootUrl() : url();
138 }
139
140 void DolphinView::setActive(bool active)
141 {
142 if (active == m_active) {
143 return;
144 }
145
146 m_active = active;
147
148 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
149 if (active) {
150 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
151 // bypasses the problem when having a split view and changing the active view to
152 // update the some URL dependent states. A nicer approach should be no big deal...
153 emit urlChanged(url());
154 emit selectionChanged(selectedItems());
155 } else {
156 color.setAlpha(150);
157 }
158
159 QWidget* viewport = itemView()->viewport();
160 QPalette palette;
161 palette.setColor(viewport->backgroundRole(), color);
162 viewport->setPalette(palette);
163
164 update();
165
166 if (active) {
167 emit activated();
168 }
169
170 m_controller->indicateActivationChange(active);
171 }
172
173 bool DolphinView::isActive() const
174 {
175 return m_active;
176 }
177
178 void DolphinView::setMode(Mode mode)
179 {
180 if (mode == m_mode) {
181 return; // the wished mode is already set
182 }
183
184 m_mode = mode;
185
186 if (isColumnViewActive()) {
187 // When changing the mode in the column view, it makes sense
188 // to go back to the root URL of the column view automatically.
189 // Otherwise there it would not be possible to turn off the column view
190 // without focusing the first column.
191 const KUrl root = rootUrl();
192 setUrl(root);
193 m_controller->setUrl(root);
194 }
195
196 const KUrl viewPropsUrl = viewPropertiesUrl();
197 ViewProperties props(viewPropsUrl);
198 props.setViewMode(m_mode);
199
200 createView();
201
202 // the file item delegate has been recreated, apply the current
203 // additional information manually
204 const KFileItemDelegate::InformationList infoList = props.additionalInfo();
205 m_fileItemDelegate->setShowInformation(infoList);
206 emit additionalInfoChanged(infoList);
207
208 // Not all view modes support categorized sorting. Adjust the sorting model
209 // if changing the view mode results in a change of the categorized sorting
210 // capabilities.
211 m_storedCategorizedSorting = props.categorizedSorting();
212 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
213 if (categorized != m_proxyModel->isCategorizedModel()) {
214 m_proxyModel->setCategorizedModel(categorized);
215 emit categorizedSortingChanged();
216 }
217
218 loadDirectory(viewPropsUrl);
219
220 emit modeChanged();
221 }
222
223 DolphinView::Mode DolphinView::mode() const
224 {
225 return m_mode;
226 }
227
228 void DolphinView::setShowPreview(bool show)
229 {
230 const KUrl viewPropsUrl = viewPropertiesUrl();
231 ViewProperties props(viewPropsUrl);
232 props.setShowPreview(show);
233
234 m_controller->setShowPreview(show);
235 emit showPreviewChanged();
236
237 loadDirectory(viewPropsUrl, true);
238 }
239
240 bool DolphinView::showPreview() const
241 {
242 return m_controller->showPreview();
243 }
244
245 void DolphinView::setShowHiddenFiles(bool show)
246 {
247 if (m_dirLister->showingDotFiles() == show) {
248 return;
249 }
250
251 const KUrl viewPropsUrl = viewPropertiesUrl();
252 ViewProperties props(viewPropsUrl);
253 props.setShowHiddenFiles(show);
254
255 m_dirLister->setShowingDotFiles(show);
256 emit showHiddenFilesChanged();
257
258 loadDirectory(viewPropsUrl, true);
259 }
260
261 bool DolphinView::showHiddenFiles() const
262 {
263 return m_dirLister->showingDotFiles();
264 }
265
266 void DolphinView::setCategorizedSorting(bool categorized)
267 {
268 if (categorized == categorizedSorting()) {
269 return;
270 }
271
272 // setCategorizedSorting(true) may only get invoked
273 // if the view supports categorized sorting
274 Q_ASSERT(!categorized || supportsCategorizedSorting());
275
276 ViewProperties props(viewPropertiesUrl());
277 props.setCategorizedSorting(categorized);
278 props.save();
279
280 m_storedCategorizedSorting = categorized;
281 m_proxyModel->setCategorizedModel(categorized);
282
283 emit categorizedSortingChanged();
284 }
285
286 bool DolphinView::categorizedSorting() const
287 {
288 // If all view modes would support categorized sorting, returning
289 // m_proxyModel->isCategorizedModel() would be the way to go. As
290 // currently only the icons view supports caterized sorting, we remember
291 // the stored view properties state in m_storedCategorizedSorting and
292 // return this state. The application takes care to disable the corresponding
293 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
294 // that this setting is not applied to the current view mode.
295 return m_storedCategorizedSorting;
296 }
297
298 bool DolphinView::supportsCategorizedSorting() const
299 {
300 return m_iconsView != 0;
301 }
302
303 void DolphinView::selectAll()
304 {
305 itemView()->selectAll();
306 }
307
308 void DolphinView::invertSelection()
309 {
310 if (isColumnViewActive()) {
311 // QAbstractItemView does not offer a virtual method invertSelection()
312 // as counterpart to QAbstractItemView::selectAll(). This makes it
313 // necessary to delegate the inverting of the selection to the
314 // column view, as only the selection of the active column should
315 // get inverted.
316 m_columnView->invertSelection();
317 } else {
318 QItemSelectionModel* selectionModel = itemView()->selectionModel();
319 const QAbstractItemModel* itemModel = selectionModel->model();
320
321 const QModelIndex topLeft = itemModel->index(0, 0);
322 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
323 itemModel->columnCount() - 1);
324
325 const QItemSelection selection(topLeft, bottomRight);
326 selectionModel->select(selection, QItemSelectionModel::Toggle);
327 }
328 }
329
330 bool DolphinView::hasSelection() const
331 {
332 return itemView()->selectionModel()->hasSelection();
333 }
334
335 void DolphinView::clearSelection()
336 {
337 itemView()->selectionModel()->clear();
338 }
339
340 KFileItemList DolphinView::selectedItems() const
341 {
342 const QAbstractItemView* view = itemView();
343
344 // Our view has a selection, we will map them back to the DolphinModel
345 // and then fill the KFileItemList.
346 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
347
348 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
349 KFileItemList itemList;
350
351 const QModelIndexList indexList = selection.indexes();
352 foreach (QModelIndex index, indexList) {
353 KFileItem item = m_dolphinModel->itemForIndex(index);
354 if (!item.isNull()) {
355 itemList.append(item);
356 }
357 }
358
359 return itemList;
360 }
361
362 KUrl::List DolphinView::selectedUrls() const
363 {
364 KUrl::List urls;
365 const KFileItemList list = selectedItems();
366 foreach (KFileItem item, list) {
367 urls.append(item.url());
368 }
369 return urls;
370 }
371
372 KFileItem DolphinView::fileItem(const QModelIndex& index) const
373 {
374 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
375 return m_dolphinModel->itemForIndex(dolphinModelIndex);
376 }
377
378 void DolphinView::setContentsPosition(int x, int y)
379 {
380 QAbstractItemView* view = itemView();
381
382 // the ColumnView takes care itself for the horizontal scrolling
383 if (!isColumnViewActive()) {
384 view->horizontalScrollBar()->setValue(x);
385 }
386 view->verticalScrollBar()->setValue(y);
387
388 m_loadingDirectory = false;
389 }
390
391 QPoint DolphinView::contentsPosition() const
392 {
393 const int x = itemView()->horizontalScrollBar()->value();
394 const int y = itemView()->verticalScrollBar()->value();
395 return QPoint(x, y);
396 }
397
398 void DolphinView::zoomIn()
399 {
400 m_controller->triggerZoomIn();
401 }
402
403 void DolphinView::zoomOut()
404 {
405 m_controller->triggerZoomOut();
406 }
407
408 bool DolphinView::isZoomInPossible() const
409 {
410 return m_controller->isZoomInPossible();
411 }
412
413 bool DolphinView::isZoomOutPossible() const
414 {
415 return m_controller->isZoomOutPossible();
416 }
417
418 void DolphinView::setSorting(Sorting sorting)
419 {
420 if (sorting != this->sorting()) {
421 updateSorting(sorting);
422 }
423 }
424
425 DolphinView::Sorting DolphinView::sorting() const
426 {
427 return m_proxyModel->sorting();
428 }
429
430 void DolphinView::setSortOrder(Qt::SortOrder order)
431 {
432 if (sortOrder() != order) {
433 updateSortOrder(order);
434 }
435 }
436
437 Qt::SortOrder DolphinView::sortOrder() const
438 {
439 return m_proxyModel->sortOrder();
440 }
441
442 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
443 {
444 const KUrl viewPropsUrl = viewPropertiesUrl();
445 ViewProperties props(viewPropsUrl);
446 props.setAdditionalInfo(info);
447 m_fileItemDelegate->setShowInformation(info);
448
449 emit additionalInfoChanged(info);
450
451 if (itemView() != m_detailsView) {
452 // the details view requires no reloading of the directory, as it maps
453 // the file item delegate info to its columns internally
454 loadDirectory(viewPropsUrl, true);
455 }
456 }
457
458 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
459 {
460 return m_fileItemDelegate->showInformation();
461 }
462
463 void DolphinView::reload()
464 {
465 setUrl(url());
466 loadDirectory(url(), true);
467 }
468
469 void DolphinView::refresh()
470 {
471 const bool oldActivationState = m_active;
472 m_active = true;
473
474 createView();
475 applyViewProperties(m_controller->url());
476 reload();
477
478 setActive(oldActivationState);
479 }
480
481 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
482 {
483 if (m_controller->url() == url) {
484 return;
485 }
486
487 m_controller->setUrl(url); // emits urlChanged, which we forward
488
489 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
490 applyViewProperties(rootUrl);
491 loadDirectory(rootUrl);
492 if (itemView() == m_columnView) {
493 m_columnView->setRootUrl(rootUrl);
494 m_columnView->showColumn(url);
495 }
496 } else {
497 applyViewProperties(url);
498 loadDirectory(url);
499 }
500
501 emit startedPathLoading(url);
502 }
503
504 void DolphinView::setNameFilter(const QString& nameFilter)
505 {
506 m_proxyModel->setFilterRegExp(nameFilter);
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 emit showHiddenFilesChanged();
658 }
659
660 m_storedCategorizedSorting = props.categorizedSorting();
661 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
662 if (categorized != m_proxyModel->isCategorizedModel()) {
663 m_proxyModel->setCategorizedModel(categorized);
664 emit categorizedSortingChanged();
665 }
666
667 const DolphinView::Sorting sorting = props.sorting();
668 if (sorting != m_proxyModel->sorting()) {
669 m_proxyModel->setSorting(sorting);
670 emit sortingChanged(sorting);
671 }
672
673 const Qt::SortOrder sortOrder = props.sortOrder();
674 if (sortOrder != m_proxyModel->sortOrder()) {
675 m_proxyModel->setSortOrder(sortOrder);
676 emit sortOrderChanged(sortOrder);
677 }
678
679 KFileItemDelegate::InformationList info = props.additionalInfo();
680 if (info != m_fileItemDelegate->showInformation()) {
681 m_fileItemDelegate->setShowInformation(info);
682 emit additionalInfoChanged(info);
683 }
684
685 const bool showPreview = props.showPreview();
686 if (showPreview != m_controller->showPreview()) {
687 m_controller->setShowPreview(showPreview);
688 emit showPreviewChanged();
689 }
690 }
691
692 void DolphinView::changeSelection(const KFileItemList& selection)
693 {
694 clearSelection();
695 if (selection.isEmpty()) {
696 return;
697 }
698 const KUrl& baseUrl = url();
699 KUrl url;
700 QItemSelection new_selection;
701 foreach(const KFileItem& item, selection) {
702 url = item.url().upUrl();
703 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
704 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
705 new_selection.select(index, index);
706 }
707 }
708 itemView()->selectionModel()->select(new_selection,
709 QItemSelectionModel::ClearAndSelect
710 | QItemSelectionModel::Current);
711 }
712
713 void DolphinView::openContextMenu(const QPoint& pos)
714 {
715 KFileItem item;
716
717 const QModelIndex index = itemView()->indexAt(pos);
718 if (index.isValid() && (index.column() == DolphinModel::Name)) {
719 item = fileItem(index);
720 }
721
722 emit requestContextMenu(item, url());
723 }
724
725 void DolphinView::dropUrls(const KUrl::List& urls,
726 const KUrl& destPath,
727 const KFileItem& destItem,
728 QWidget* source)
729 {
730 bool dropAboveDir = false;
731 if (!destItem.isNull()) {
732 dropAboveDir = destItem.isDir();
733 if (!dropAboveDir) {
734 // the dropping is done above a file
735 return;
736 }
737 } else if (source == itemView()) {
738 // the dropping is done into the same viewport where the dragging
739 // has been started
740 return;
741 }
742
743 const KUrl& destination = dropAboveDir ? destItem.url() : destPath;
744 dropUrls(urls, destination);
745 }
746
747 void DolphinView::dropUrls(const KUrl::List& urls,
748 const KUrl& destination)
749 {
750 emit urlsDropped(urls, destination);
751 }
752
753 void DolphinView::updateSorting(DolphinView::Sorting sorting)
754 {
755 ViewProperties props(viewPropertiesUrl());
756 props.setSorting(sorting);
757
758 m_proxyModel->setSorting(sorting);
759
760 emit sortingChanged(sorting);
761 }
762
763 void DolphinView::updateSortOrder(Qt::SortOrder order)
764 {
765 ViewProperties props(viewPropertiesUrl());
766 props.setSortOrder(order);
767
768 m_proxyModel->setSortOrder(order);
769
770 emit sortOrderChanged(order);
771 }
772
773 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
774 {
775 ViewProperties props(viewPropertiesUrl());
776 props.setAdditionalInfo(info);
777
778 m_fileItemDelegate->setShowInformation(info);
779
780 emit additionalInfoChanged(info);
781
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 // 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 view->setItemDelegate(m_fileItemDelegate);
873
874 view->setModel(m_proxyModel);
875 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
876
877 new KMimeTypeResolver(view, m_dolphinModel);
878 m_topLayout->insertWidget(1, view);
879
880 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
881 this, SLOT(emitSelectionChangedSignal()));
882 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
883 this, SLOT(emitContentsMoved()));
884 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
885 this, SLOT(emitContentsMoved()));
886 }
887
888 QAbstractItemView* DolphinView::itemView() const
889 {
890 if (m_detailsView != 0) {
891 return m_detailsView;
892 } else if (m_columnView != 0) {
893 return m_columnView;
894 }
895
896 return m_iconsView;
897 }
898
899 bool DolphinView::isCutItem(const KFileItem& item) const
900 {
901 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
902 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
903
904 const KUrl& itemUrl = item.url();
905 KUrl::List::const_iterator it = cutUrls.begin();
906 const KUrl::List::const_iterator end = cutUrls.end();
907 while (it != end) {
908 if (*it == itemUrl) {
909 return true;
910 }
911 ++it;
912 }
913
914 return false;
915 }
916
917 void DolphinView::applyCutItemEffect()
918 {
919 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
920 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
921 return;
922 }
923
924 KFileItemList items(m_dirLister->items());
925 KFileItemList::const_iterator it = items.begin();
926 const KFileItemList::const_iterator end = items.end();
927 while (it != end) {
928 const KFileItem item = *it;
929 if (isCutItem(item)) {
930 const QModelIndex index = m_dolphinModel->indexForItem(item);
931 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
932 if (value.type() == QVariant::Icon) {
933 const QIcon icon(qvariant_cast<QIcon>(value));
934 QPixmap pixmap = icon.pixmap(128, 128);
935
936 // remember current pixmap for the item to be able
937 // to restore it when other items get cut
938 CutItem cutItem;
939 cutItem.url = item.url();
940 cutItem.pixmap = pixmap;
941 m_cutItemsCache.append(cutItem);
942
943 // apply icon effect to the cut item
944 KIconEffect iconEffect;
945 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
946 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
947 }
948 }
949 ++it;
950 }
951 }
952
953 KToggleAction* DolphinView::iconsModeAction(KActionCollection* actionCollection)
954 {
955 KToggleAction* iconsView = actionCollection->add<KToggleAction>("icons");
956 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
957 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
958 iconsView->setIcon(KIcon("fileview-icon"));
959 iconsView->setData(QVariant::fromValue(IconsView));
960 return iconsView;
961 }
962
963 KToggleAction* DolphinView::detailsModeAction(KActionCollection* actionCollection)
964 {
965 KToggleAction* detailsView = actionCollection->add<KToggleAction>("details");
966 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
967 detailsView->setShortcut(Qt::CTRL | Qt::Key_2);
968 detailsView->setIcon(KIcon("fileview-detailed"));
969 detailsView->setData(QVariant::fromValue(DetailsView));
970 return detailsView;
971 }
972
973 KToggleAction* DolphinView::columnsModeAction(KActionCollection* actionCollection)
974 {
975 KToggleAction* columnView = actionCollection->add<KToggleAction>("columns");
976 columnView->setText(i18nc("@action:inmenu View Mode", "Columns"));
977 columnView->setShortcut(Qt::CTRL | Qt::Key_3);
978 columnView->setIcon(KIcon("fileview-column"));
979 columnView->setData(QVariant::fromValue(ColumnView));
980 return columnView;
981 }
982
983 QString DolphinView::currentViewModeActionName() const
984 {
985 switch (m_mode) {
986 case DolphinView::IconsView:
987 return "icons";
988 case DolphinView::DetailsView:
989 return "details";
990 case DolphinView::ColumnView:
991 return "columns";
992 }
993 return QString(); // can't happen
994 }
995
996 #include "dolphinview.moc"