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