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