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