]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
KDirLister openUrl parameters are now flags
[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 KFileItemList 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 KFileItemList 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 KFileItemList list = selectedItems();
333 foreach (KFileItem item, list) {
334 urls.append(item.url());
335 }
336 return urls;
337 }
338
339 KFileItem DolphinView::fileItem(const QModelIndex& index) const
340 {
341 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
342 return m_dolphinModel->itemForIndex(dolphinModelIndex);
343 }
344
345 void DolphinView::setContentsPosition(int x, int y)
346 {
347 QAbstractItemView* view = itemView();
348
349 // the ColumnView takes care itself for the horizontal scrolling
350 if (!isColumnViewActive()) {
351 view->horizontalScrollBar()->setValue(x);
352 }
353 view->verticalScrollBar()->setValue(y);
354
355 m_loadingDirectory = false;
356 }
357
358 QPoint DolphinView::contentsPosition() const
359 {
360 const int x = itemView()->horizontalScrollBar()->value();
361 const int y = itemView()->verticalScrollBar()->value();
362 return QPoint(x, y);
363 }
364
365 void DolphinView::zoomIn()
366 {
367 m_controller->triggerZoomIn();
368 }
369
370 void DolphinView::zoomOut()
371 {
372 m_controller->triggerZoomOut();
373 }
374
375 bool DolphinView::isZoomInPossible() const
376 {
377 return m_controller->isZoomInPossible();
378 }
379
380 bool DolphinView::isZoomOutPossible() const
381 {
382 return m_controller->isZoomOutPossible();
383 }
384
385 void DolphinView::setSorting(Sorting sorting)
386 {
387 if (sorting != this->sorting()) {
388 updateSorting(sorting);
389 }
390 }
391
392 DolphinView::Sorting DolphinView::sorting() const
393 {
394 return m_proxyModel->sorting();
395 }
396
397 void DolphinView::setSortOrder(Qt::SortOrder order)
398 {
399 if (sortOrder() != order) {
400 updateSortOrder(order);
401 }
402 }
403
404 Qt::SortOrder DolphinView::sortOrder() const
405 {
406 return m_proxyModel->sortOrder();
407 }
408
409 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
410 {
411 const KUrl viewPropsUrl = viewPropertiesUrl();
412 ViewProperties props(viewPropsUrl);
413 props.setAdditionalInfo(info);
414
415 m_controller->setAdditionalInfoCount(info.count());
416 m_fileItemDelegate->setShowInformation(info);
417
418 emit additionalInfoChanged(info);
419 startDirLister(viewPropsUrl, true);
420 }
421
422 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
423 {
424 return m_fileItemDelegate->showInformation();
425 }
426
427 void DolphinView::reload()
428 {
429 setUrl(url());
430 startDirLister(url(), true);
431 }
432
433 void DolphinView::refresh()
434 {
435 createView();
436 applyViewProperties(m_controller->url());
437 reload();
438 updateViewportColor();
439 }
440
441 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
442 {
443 if (m_controller->url() == url) {
444 return;
445 }
446
447 const bool restoreColumnView = !rootUrl.isEmpty()
448 && !rootUrl.equals(url, KUrl::CompareWithoutTrailingSlash)
449 && rootUrl.isParentOf(url);
450
451 m_controller->setUrl(url); // emits urlChanged, which we forward
452
453 if (restoreColumnView) {
454 applyViewProperties(rootUrl);
455 Q_ASSERT(itemView() == m_columnView);
456 startDirLister(rootUrl);
457 m_columnView->showColumn(url);
458 } else {
459 applyViewProperties(url);
460 startDirLister(url);
461 }
462
463 itemView()->setFocus();
464
465 emit startedPathLoading(url);
466 }
467
468 void DolphinView::setUrl(const KUrl& url)
469 {
470 updateView(url, KUrl());
471 }
472
473 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
474 {
475 QWidget::mouseReleaseEvent(event);
476 setActive(true);
477 }
478 void DolphinView::activate()
479 {
480 setActive(true);
481 }
482
483 void DolphinView::triggerItem(const QModelIndex& index)
484 {
485 Q_ASSERT(index.isValid());
486
487 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
488 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
489 // items are selected by the user, hence don't trigger the
490 // item specified by 'index'
491 return;
492 }
493
494 const KFileItem item = m_dolphinModel->itemForIndex(m_proxyModel->mapToSource(index));
495
496 if (item.isNull()) {
497 return;
498 }
499
500 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
501 }
502
503 void DolphinView::generatePreviews(const KFileItemList& items)
504 {
505 if (m_controller->showPreview()) {
506 KIO::PreviewJob* job = KIO::filePreview(items, 128);
507 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
508 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
509 }
510 }
511
512 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
513 {
514 Q_ASSERT(!item.isNull());
515 if (item.url().directory() != m_dirLister->url().path()) {
516 // the preview job is still working on items of an older URL, hence
517 // the item is not part of the directory model anymore
518 return;
519 }
520
521 const QModelIndex idx = m_dolphinModel->indexForItem(item);
522 if (idx.isValid() && (idx.column() == 0)) {
523 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
524 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
525 KIconEffect iconEffect;
526 const QPixmap cutPixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
527 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
528 } else {
529 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
530 }
531 }
532 }
533
534 void DolphinView::emitSelectionChangedSignal()
535 {
536 emit selectionChanged(DolphinView::selectedItems());
537 }
538
539 void DolphinView::startDirLister(const KUrl& url, bool reload)
540 {
541 if (!url.isValid()) {
542 const QString location(url.pathOrUrl());
543 if (location.isEmpty()) {
544 emit errorMessage(i18nc("@info:status", "The location is empty."));
545 } else {
546 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
547 }
548 return;
549 }
550
551 m_cutItemsCache.clear();
552 m_loadingDirectory = true;
553
554 m_dirLister->stop();
555
556 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
557 m_initializeColumnView = false;
558
559 if (keepOldDirs) {
560 // keeping old directories is only necessary for hierarchical views
561 // like the column view
562 if (reload) {
563 // for the column view it is not enough to reload the directory lister,
564 // so this task is delegated to the column view directly
565 m_columnView->reload();
566 } else if (m_dirLister->directories().contains(url)) {
567 // The dir lister contains the directory already, so
568 // KDirLister::openUrl() may not get invoked twice.
569 m_dirLister->updateDirectory(url);
570 } else {
571 const KUrl& dirListerUrl = m_dirLister->url();
572 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
573 // The current URL is not a child of the dir lister
574 // URL. This may happen when e. g. a place has been selected
575 // and hence the view must be reset.
576 m_dirLister->openUrl(url, KDirLister::NoFlags);
577 }
578 }
579 } else {
580 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
581 }
582 }
583
584 KUrl DolphinView::viewPropertiesUrl() const
585 {
586 if (isColumnViewActive()) {
587 return m_dirLister->url();
588 }
589
590 return url();
591 }
592
593 void DolphinView::applyViewProperties(const KUrl& url)
594 {
595 if (isColumnViewActive() && m_dirLister->url().isParentOf(url)) {
596 // The column view is active, hence don't apply the view properties
597 // of sub directories (represented by columns) to the view. The
598 // view always represents the properties of the first column.
599 return;
600 }
601
602 const ViewProperties props(url);
603
604 const Mode mode = props.viewMode();
605 if (m_mode != mode) {
606 m_mode = mode;
607 createView();
608 emit modeChanged();
609
610 if (m_mode == ColumnView) {
611 // The mode has been changed to the Column View. When starting the dir
612 // lister with DolphinView::startDirLister() it is important to give a
613 // hint that the dir lister may not keep the current directory
614 // although this is the default for showing a hierarchy.
615 m_initializeColumnView = true;
616 }
617 }
618 if (itemView() == 0) {
619 createView();
620 }
621 Q_ASSERT(itemView() != 0);
622 Q_ASSERT(m_fileItemDelegate != 0);
623
624 const bool showHiddenFiles = props.showHiddenFiles();
625 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
626 m_dirLister->setShowingDotFiles(showHiddenFiles);
627 emit showHiddenFilesChanged();
628 }
629
630 m_storedCategorizedSorting = props.categorizedSorting();
631 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
632 if (categorized != m_proxyModel->isCategorizedModel()) {
633 m_proxyModel->setCategorizedModel(categorized);
634 m_proxyModel->sort(m_proxyModel->sortColumn(), m_proxyModel->sortOrder());
635 emit categorizedSortingChanged();
636 }
637
638 const DolphinView::Sorting sorting = props.sorting();
639 if (sorting != m_proxyModel->sorting()) {
640 m_proxyModel->setSorting(sorting);
641 emit sortingChanged(sorting);
642 }
643
644 const Qt::SortOrder sortOrder = props.sortOrder();
645 if (sortOrder != m_proxyModel->sortOrder()) {
646 m_proxyModel->setSortOrder(sortOrder);
647 emit sortOrderChanged(sortOrder);
648 }
649
650 KFileItemDelegate::InformationList info = props.additionalInfo();
651 if (info != m_fileItemDelegate->showInformation()) {
652 m_controller->setAdditionalInfoCount(info.count());
653 m_fileItemDelegate->setShowInformation(info);
654 emit additionalInfoChanged(info);
655 }
656
657 const bool showPreview = props.showPreview();
658 if (showPreview != m_controller->showPreview()) {
659 m_controller->setShowPreview(showPreview);
660 emit showPreviewChanged();
661 }
662 }
663
664 void DolphinView::changeSelection(const KFileItemList& selection)
665 {
666 clearSelection();
667 if (selection.isEmpty()) {
668 return;
669 }
670 const KUrl& baseUrl = url();
671 KUrl url;
672 QItemSelection new_selection;
673 foreach(const KFileItem& item, selection) {
674 url = item.url().upUrl();
675 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
676 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
677 new_selection.select(index, index);
678 }
679 }
680 itemView()->selectionModel()->select(new_selection,
681 QItemSelectionModel::ClearAndSelect
682 | QItemSelectionModel::Current);
683 }
684
685 void DolphinView::openContextMenu(const QPoint& pos)
686 {
687 KFileItem item;
688
689 const QModelIndex index = itemView()->indexAt(pos);
690 if (isValidNameIndex(index)) {
691 item = fileItem(index);
692 }
693
694 emit requestContextMenu(item, url());
695 }
696
697 void DolphinView::dropUrls(const KUrl::List& urls,
698 const KUrl& destPath,
699 const QModelIndex& destIndex,
700 QWidget* source)
701 {
702 KFileItem directory;
703 if (isValidNameIndex(destIndex)) {
704 KFileItem item = fileItem(destIndex);
705 Q_ASSERT(!item.isNull());
706 if (item.isDir()) {
707 // the URLs are dropped above a directory
708 directory = item;
709 }
710 }
711
712 if ((directory.isNull()) && (source == itemView())) {
713 // The dropping is done into the same viewport where
714 // the dragging has been started. Just ignore this...
715 return;
716 }
717
718 const KUrl& destination = (directory.isNull()) ?
719 destPath : directory.url();
720 dropUrls(urls, destination);
721 }
722
723 void DolphinView::dropUrls(const KUrl::List& urls,
724 const KUrl& destination)
725 {
726 emit urlsDropped(urls, destination);
727 }
728
729 void DolphinView::updateSorting(DolphinView::Sorting sorting)
730 {
731 ViewProperties props(viewPropertiesUrl());
732 props.setSorting(sorting);
733
734 m_proxyModel->setSorting(sorting);
735
736 emit sortingChanged(sorting);
737 }
738
739 void DolphinView::updateSortOrder(Qt::SortOrder order)
740 {
741 ViewProperties props(viewPropertiesUrl());
742 props.setSortOrder(order);
743
744 m_proxyModel->setSortOrder(order);
745
746 emit sortOrderChanged(order);
747 }
748
749 void DolphinView::emitContentsMoved()
750 {
751 // only emit the contents moved signal if:
752 // - no directory loading is ongoing (this would reset the contents position
753 // always to (0, 0))
754 // - if the Column View is active: the column view does an automatic
755 // positioning during the loading operation, which must be remembered
756 if (!m_loadingDirectory || isColumnViewActive()) {
757 const QPoint pos(contentsPosition());
758 emit contentsMoved(pos.x(), pos.y());
759 }
760 }
761
762 void DolphinView::updateCutItems()
763 {
764 // restore the icons of all previously selected items to the
765 // original state...
766 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
767 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
768 while (it != end) {
769 const QModelIndex index = m_dolphinModel->indexForUrl((*it).url);
770 if (index.isValid()) {
771 m_dolphinModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
772 }
773 ++it;
774 }
775 m_cutItemsCache.clear();
776
777 // ... and apply an item effect to all currently cut items
778 applyCutItemEffect();
779 }
780
781 void DolphinView::showHoverInformation(const QModelIndex& index)
782 {
783 if (hasSelection()) {
784 return;
785 }
786
787 const KFileItem item = fileItem(index);
788 if (!item.isNull()) {
789 emit requestItemInfo(item);
790 }
791 }
792
793 void DolphinView::clearHoverInformation()
794 {
795 emit requestItemInfo(KFileItem());
796 }
797
798
799 void DolphinView::createView()
800 {
801 KFileItemDelegate::InformationList infoList;
802 if (m_fileItemDelegate != 0) {
803 infoList = m_fileItemDelegate->showInformation();
804 }
805
806 // delete current view
807 QAbstractItemView* view = itemView();
808 if (view != 0) {
809 m_topLayout->removeWidget(view);
810 view->close();
811 view->deleteLater();
812 view = 0;
813 m_iconsView = 0;
814 m_detailsView = 0;
815 m_columnView = 0;
816 m_fileItemDelegate = 0;
817 }
818
819 Q_ASSERT(m_iconsView == 0);
820 Q_ASSERT(m_detailsView == 0);
821 Q_ASSERT(m_columnView == 0);
822
823 // ... and recreate it representing the current mode
824 switch (m_mode) {
825 case IconsView: {
826 m_iconsView = new DolphinIconsView(this, m_controller);
827 view = m_iconsView;
828 break;
829 }
830
831 case DetailsView:
832 m_detailsView = new DolphinDetailsView(this, m_controller);
833 view = m_detailsView;
834 break;
835
836 case ColumnView:
837 m_columnView = new DolphinColumnView(this, m_controller);
838 view = m_columnView;
839 break;
840 }
841
842 Q_ASSERT(view != 0);
843
844 m_fileItemDelegate = new KFileItemDelegate(view);
845 m_fileItemDelegate->setShowInformation(infoList);
846 view->setItemDelegate(m_fileItemDelegate);
847
848 view->setModel(m_proxyModel);
849 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
850
851 new KMimeTypeResolver(view, m_dolphinModel);
852 m_topLayout->insertWidget(1, view);
853
854 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
855 this, SLOT(emitSelectionChangedSignal()));
856 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
857 this, SLOT(emitContentsMoved()));
858 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
859 this, SLOT(emitContentsMoved()));
860 view->setFocus();
861 }
862
863 QAbstractItemView* DolphinView::itemView() const
864 {
865 if (m_detailsView != 0) {
866 return m_detailsView;
867 } else if (m_columnView != 0) {
868 return m_columnView;
869 }
870
871 return m_iconsView;
872 }
873
874 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
875 {
876 return index.isValid() && (index.column() == DolphinModel::Name);
877 }
878
879 bool DolphinView::isCutItem(const KFileItem& item) const
880 {
881 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
882 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
883
884 const KUrl& itemUrl = item.url();
885 KUrl::List::const_iterator it = cutUrls.begin();
886 const KUrl::List::const_iterator end = cutUrls.end();
887 while (it != end) {
888 if (*it == itemUrl) {
889 return true;
890 }
891 ++it;
892 }
893
894 return false;
895 }
896
897 void DolphinView::applyCutItemEffect()
898 {
899 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
900 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
901 return;
902 }
903
904 KFileItemList items(m_dirLister->items());
905 KFileItemList::const_iterator it = items.begin();
906 const KFileItemList::const_iterator end = items.end();
907 while (it != end) {
908 const KFileItem item = *it;
909 if (isCutItem(item)) {
910 const QModelIndex index = m_dolphinModel->indexForItem(item);
911 // Huh? the item is already known
912 //const KFileItem item = m_dolphinModel->itemForIndex(index);
913 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
914 if (value.type() == QVariant::Icon) {
915 const QIcon icon(qvariant_cast<QIcon>(value));
916 QPixmap pixmap = icon.pixmap(128, 128);
917
918 // remember current pixmap for the item to be able
919 // to restore it when other items get cut
920 CutItem cutItem;
921 cutItem.url = item.url();
922 cutItem.pixmap = pixmap;
923 m_cutItemsCache.append(cutItem);
924
925 // apply icon effect to the cut item
926 KIconEffect iconEffect;
927 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
928 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
929 }
930 }
931 ++it;
932 }
933 }
934
935 void DolphinView::updateViewportColor()
936 {
937 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
938 if (m_active) {
939 emit urlChanged(url()); // Hmm, this is a hack; the url hasn't really changed.
940 emit selectionChanged(selectedItems());
941 } else {
942 color.setAlpha(0);
943 }
944
945 QWidget* viewport = itemView()->viewport();
946 QPalette palette;
947 palette.setColor(viewport->backgroundRole(), color);
948 viewport->setPalette(palette);
949 }
950
951 #include "dolphinview.moc"