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