]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Assure that the view properties of a column view are always stored in the folder...
[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 <kdirmodel.h>
33 #include <kdirlister.h>
34 #include <kfileitemdelegate.h>
35 #include <klocale.h>
36 #include <kiconeffect.h>
37 #include <kio/netaccess.h>
38 #include <kio/renamedialog.h>
39 #include <kio/previewjob.h>
40 #include <kmimetyperesolver.h>
41 #include <konqmimedata.h>
42 #include <konq_operations.h>
43 #include <kurl.h>
44
45 #include "dolphincolumnview.h"
46 #include "dolphincontroller.h"
47 #include "dolphinsortfilterproxymodel.h"
48 #include "dolphindetailsview.h"
49 #include "dolphiniconsview.h"
50 #include "dolphinitemcategorizer.h"
51 #include "renamedialog.h"
52 #include "viewproperties.h"
53 #include "dolphinsettings.h"
54 #include "dolphin_generalsettings.h"
55
56 DolphinView::DolphinView(QWidget* parent,
57 const KUrl& url,
58 KDirLister* dirLister,
59 KDirModel* dirModel,
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_dirModel(dirModel),
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 QModelIndex&, QWidget*)),
97 this, SLOT(dropUrls(const KUrl::List&, 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 QColor color = KColorScheme(KColorScheme::View).background();
138 if (active) {
139 emit urlChanged(url());
140 emit selectionChanged(selectedItems());
141 } else {
142 color.setAlpha(0);
143 }
144
145 QWidget* viewport = itemView()->viewport();
146 QPalette palette;
147 palette.setColor(viewport->backgroundRole(), color);
148 viewport->setPalette(palette);
149
150 update();
151
152 if (active) {
153 emit activated();
154 }
155 }
156
157 bool DolphinView::isActive() const
158 {
159 return m_active;
160 }
161
162 void DolphinView::setMode(Mode mode)
163 {
164 if (mode == m_mode) {
165 return; // the wished mode is already set
166 }
167
168 m_mode = mode;
169
170 if (isColumnViewActive()) {
171 // When changing the mode in the column view, it makes sense
172 // to go back to the root URL of the column view automatically.
173 // Otherwise there it would not be possible to turn off the column view
174 // without focusing the first column.
175 setUrl(m_dirLister->url());
176 m_controller->setUrl(m_dirLister->url());
177 }
178
179 const KUrl viewPropsUrl = viewPropertiesUrl();
180 ViewProperties props(viewPropsUrl);
181 props.setViewMode(m_mode);
182
183 createView();
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 (!supportsCategorizedSorting() || (categorized == categorizedSorting())) {
235 return;
236 }
237
238 Q_ASSERT(m_iconsView != 0);
239 if (categorized) {
240 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
241 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
242 } else {
243 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
244 m_iconsView->setItemCategorizer(0);
245 delete categorizer;
246 }
247
248 ViewProperties props(viewPropertiesUrl());
249 props.setCategorizedSorting(categorized);
250 props.save();
251
252 emit categorizedSortingChanged();
253 }
254
255 bool DolphinView::categorizedSorting() const
256 {
257 if (!supportsCategorizedSorting()) {
258 return false;
259 }
260
261 Q_ASSERT(m_iconsView != 0);
262 return m_iconsView->itemCategorizer() != 0;
263 }
264
265 bool DolphinView::supportsCategorizedSorting() const
266 {
267 return m_iconsView != 0;
268 }
269
270 void DolphinView::selectAll()
271 {
272 selectAll(QItemSelectionModel::Select);
273 }
274
275 void DolphinView::invertSelection()
276 {
277 selectAll(QItemSelectionModel::Toggle);
278 }
279
280 bool DolphinView::hasSelection() const
281 {
282 return itemView()->selectionModel()->hasSelection();
283 }
284
285 void DolphinView::clearSelection()
286 {
287 itemView()->selectionModel()->clear();
288 }
289
290 KFileItemList DolphinView::selectedItems() const
291 {
292 const QAbstractItemView* view = itemView();
293
294 // Our view has a selection, we will map them back to the DirModel
295 // and then fill the KFileItemList.
296 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
297
298 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
299 KFileItemList itemList;
300
301 const QModelIndexList indexList = selection.indexes();
302 QModelIndexList::const_iterator end = indexList.end();
303 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
304 Q_ASSERT((*it).isValid());
305
306 KFileItem* item = m_dirModel->itemForIndex(*it);
307 if (item != 0) {
308 itemList.append(item);
309 }
310 }
311
312 return itemList;
313 }
314
315 KUrl::List DolphinView::selectedUrls() const
316 {
317 KUrl::List urls;
318
319 const KFileItemList list = selectedItems();
320 KFileItemList::const_iterator it = list.begin();
321 const KFileItemList::const_iterator end = list.end();
322 while (it != end) {
323 KFileItem* item = *it;
324 urls.append(item->url());
325 ++it;
326 }
327
328 return urls;
329 }
330
331 KFileItem* DolphinView::fileItem(const QModelIndex index) const
332 {
333 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
334 return m_dirModel->itemForIndex(dirModelIndex);
335 }
336
337 void DolphinView::setContentsPosition(int x, int y)
338 {
339 QAbstractItemView* view = itemView();
340 view->horizontalScrollBar()->setValue(x);
341 view->verticalScrollBar()->setValue(y);
342
343 m_loadingDirectory = false;
344 }
345
346 QPoint DolphinView::contentsPosition() const
347 {
348 const int x = itemView()->horizontalScrollBar()->value();
349 const int y = itemView()->verticalScrollBar()->value();
350 return QPoint(x, y);
351 }
352
353 void DolphinView::zoomIn()
354 {
355 m_controller->triggerZoomIn();
356 }
357
358 void DolphinView::zoomOut()
359 {
360 m_controller->triggerZoomOut();
361 }
362
363 bool DolphinView::isZoomInPossible() const
364 {
365 return m_controller->isZoomInPossible();
366 }
367
368 bool DolphinView::isZoomOutPossible() const
369 {
370 return m_controller->isZoomOutPossible();
371 }
372
373 void DolphinView::setSorting(Sorting sorting)
374 {
375 if (sorting != this->sorting()) {
376 updateSorting(sorting);
377 }
378 }
379
380 DolphinView::Sorting DolphinView::sorting() const
381 {
382 return m_proxyModel->sorting();
383 }
384
385 void DolphinView::setSortOrder(Qt::SortOrder order)
386 {
387 if (sortOrder() != order) {
388 updateSortOrder(order);
389 }
390 }
391
392 Qt::SortOrder DolphinView::sortOrder() const
393 {
394 return m_proxyModel->sortOrder();
395 }
396
397 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
398 {
399 const KUrl viewPropsUrl = viewPropertiesUrl();
400 ViewProperties props(viewPropsUrl);
401 props.setAdditionalInfo(info);
402
403 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
404 m_fileItemDelegate->setAdditionalInformation(info);
405
406 emit additionalInfoChanged(info);
407 startDirLister(viewPropsUrl, true);
408 }
409
410 KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
411 {
412 return m_fileItemDelegate->additionalInformation();
413 }
414
415 void DolphinView::reload()
416 {
417 setUrl(url());
418 startDirLister(url(), true);
419 }
420
421 void DolphinView::refresh()
422 {
423 createView();
424 applyViewProperties(m_controller->url());
425 reload();
426 }
427
428 void DolphinView::setUrl(const KUrl& url)
429 {
430 if (m_controller->url() == url) {
431 return;
432 }
433
434 m_controller->setUrl(url);
435
436 applyViewProperties(url);
437
438 startDirLister(url);
439 emit urlChanged(url);
440 }
441
442 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
443 {
444 QWidget::mouseReleaseEvent(event);
445 setActive(true);;
446 }
447 void DolphinView::activate()
448 {
449 setActive(true);
450 }
451
452 void DolphinView::triggerItem(const QModelIndex& index)
453 {
454 if (!isValidNameIndex(index)) {
455 clearSelection();
456 showHoverInformation(index);
457 return;
458 }
459
460 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
461 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
462 // items are selected by the user, hence don't trigger the
463 // item specified by 'index'
464 return;
465 }
466
467 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
468 if (item == 0) {
469 return;
470 }
471
472 // The stuff below should be moved to ViewContainer and be just a signal?
473
474 // Prefer the local path over the URL.
475 bool isLocal;
476 KUrl url = item->mostLocalUrl(isLocal);
477
478 if (item->isDir()) {
479 setUrl(url);
480 } else if (item->isFile()) {
481 // allow to browse through ZIP and tar files
482 KMimeType::Ptr mime = item->mimeTypePtr();
483 if (mime->is("application/zip")) {
484 url.setProtocol("zip");
485 setUrl(url);
486 } else if (mime->is("application/x-tar") ||
487 mime->is("application/x-tarz") ||
488 mime->is("application/x-bzip-compressed-tar") ||
489 mime->is("application/x-compressed-tar") ||
490 mime->is("application/x-tzo")) {
491 url.setProtocol("tar");
492 setUrl(url);
493 } else {
494 item->run();
495 }
496 } else {
497 item->run();
498 }
499 }
500
501 void DolphinView::generatePreviews(const KFileItemList& items)
502 {
503 if (m_controller->showPreview()) {
504
505 // Must turn QList<KFileItem *> to QList<KFileItem>...
506 QList<KFileItem> itemsToPreview;
507 foreach( KFileItem* it, items )
508 itemsToPreview.append( *it );
509
510 KIO::PreviewJob* job = KIO::filePreview(itemsToPreview, 128);
511 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
512 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
513 }
514 }
515
516 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
517 {
518 Q_ASSERT(!item.isNull());
519 if (item.url().directory() != m_dirLister->url().path()) {
520 // the preview job is still working on items of an older URL, hence
521 // the item is not part of the directory model anymore
522 return;
523 }
524
525 const QModelIndex idx = m_dirModel->indexForItem(item);
526 if (idx.isValid() && (idx.column() == 0)) {
527 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
528 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
529 KIconEffect iconEffect;
530 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
531 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
532 } else {
533 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
534 }
535 }
536 }
537
538 void DolphinView::emitSelectionChangedSignal()
539 {
540 emit selectionChanged(DolphinView::selectedItems());
541 }
542
543 void DolphinView::startDirLister(const KUrl& url, bool reload)
544 {
545 if (!url.isValid()) {
546 const QString location(url.pathOrUrl());
547 if (location.isEmpty()) {
548 emit errorMessage(i18nc("@info:status", "The location is empty."));
549 } else {
550 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
551 }
552 return;
553 }
554
555 m_cutItemsCache.clear();
556 m_loadingDirectory = true;
557
558 m_dirLister->stop();
559
560 bool openDir = true;
561 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
562 m_initializeColumnView = false;
563
564 if (keepOldDirs) {
565 if (reload) {
566 keepOldDirs = false;
567
568 const KUrl& dirListerUrl = m_dirLister->url();
569 if (dirListerUrl.isValid()) {
570 const KUrl::List dirs = m_dirLister->directories();
571 KUrl url;
572 foreach(url, dirs) {
573 m_dirLister->updateDirectory(url);
574 }
575 openDir = false;
576 }
577 } else if (m_dirLister->directories().contains(url)) {
578 // The dir lister contains the directory already, so
579 // KDirLister::openUrl() may not been invoked twice.
580 m_dirLister->updateDirectory(url);
581 openDir = false;
582 } else {
583 const KUrl& dirListerUrl = m_dirLister->url();
584 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
585 // The current URL is not a child of the dir lister
586 // URL. This may happen when e. g. a bookmark has been selected
587 // and hence the view must be reset.
588 keepOldDirs = false;
589 }
590 }
591 }
592
593 if (openDir) {
594 m_dirLister->openUrl(url, keepOldDirs, reload);
595 }
596 }
597
598 KUrl DolphinView::viewPropertiesUrl() const
599 {
600 if (isColumnViewActive()) {
601 return m_dirLister->url();
602 }
603
604 return url();
605 }
606
607 void DolphinView::applyViewProperties(const KUrl& url)
608 {
609 if (isColumnViewActive() && m_dirLister->url().isParentOf(url)) {
610 // The column view is active, hence don't apply the view properties
611 // of sub directories (represented by columns) to the view. The
612 // view always represents the properties of the first column.
613 return;
614 }
615
616 const ViewProperties props(url);
617
618 const Mode mode = props.viewMode();
619 if (m_mode != mode) {
620 m_mode = mode;
621 createView();
622 emit modeChanged();
623
624 if (m_mode == ColumnView) {
625 // The mode has been changed to the Column View. When starting the dir
626 // lister with DolphinView::startDirLister() it is important to give a
627 // hint that the dir lister may not keep the current directory
628 // although this is the default for showing a hierarchy.
629 m_initializeColumnView = true;
630 }
631 }
632 if (itemView() == 0) {
633 createView();
634 }
635 Q_ASSERT(itemView() != 0);
636 Q_ASSERT(m_fileItemDelegate != 0);
637
638 const bool showHiddenFiles = props.showHiddenFiles();
639 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
640 m_dirLister->setShowingDotFiles(showHiddenFiles);
641 emit showHiddenFilesChanged();
642 }
643
644 const bool categorized = props.categorizedSorting();
645 if (categorized != categorizedSorting()) {
646 if (supportsCategorizedSorting()) {
647 Q_ASSERT(m_iconsView != 0);
648 if (categorized) {
649 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
650 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
651 } else {
652 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
653 m_iconsView->setItemCategorizer(0);
654 delete categorizer;
655 }
656 }
657 emit categorizedSortingChanged();
658 }
659
660 const DolphinView::Sorting sorting = props.sorting();
661 if (sorting != m_proxyModel->sorting()) {
662 m_proxyModel->setSorting(sorting);
663 emit sortingChanged(sorting);
664 }
665
666 const Qt::SortOrder sortOrder = props.sortOrder();
667 if (sortOrder != m_proxyModel->sortOrder()) {
668 m_proxyModel->setSortOrder(sortOrder);
669 emit sortOrderChanged(sortOrder);
670 }
671
672 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
673 if (info != m_fileItemDelegate->additionalInformation()) {
674 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
675 m_fileItemDelegate->setAdditionalInformation(info);
676 emit additionalInfoChanged(info);
677 }
678
679 const bool showPreview = props.showPreview();
680 if (showPreview != m_controller->showPreview()) {
681 m_controller->setShowPreview(showPreview);
682 emit showPreviewChanged();
683 }
684 }
685
686 void DolphinView::changeSelection(const KFileItemList& selection)
687 {
688 clearSelection();
689 if (selection.isEmpty()) {
690 return;
691 }
692 const KUrl& baseUrl = url();
693 KUrl url;
694 QItemSelection new_selection;
695 foreach(KFileItem* item, selection) {
696 url = item->url().upUrl();
697 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
698 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(*item));
699 new_selection.select(index, index);
700 }
701 }
702 itemView()->selectionModel()->select(new_selection,
703 QItemSelectionModel::ClearAndSelect
704 | QItemSelectionModel::Current);
705 }
706
707 void DolphinView::openContextMenu(const QPoint& pos)
708 {
709 KFileItem* item = 0;
710
711 const QModelIndex index = itemView()->indexAt(pos);
712 if (isValidNameIndex(index)) {
713 item = fileItem(index);
714 }
715
716 emit requestContextMenu(item, url());
717 }
718
719 void DolphinView::dropUrls(const KUrl::List& urls,
720 const QModelIndex& index,
721 QWidget* source)
722 {
723 KFileItem* directory = 0;
724 if (isValidNameIndex(index)) {
725 KFileItem* item = fileItem(index);
726 Q_ASSERT(item != 0);
727 if (item->isDir()) {
728 // the URLs are dropped above a directory
729 directory = item;
730 }
731 }
732
733 if ((directory == 0) && (source == itemView())) {
734 // The dropping is done into the same viewport where
735 // the dragging has been started. Just ignore this...
736 return;
737 }
738
739 const KUrl& destination = (directory == 0) ?
740 url() : directory->url();
741 dropUrls(urls, destination);
742 }
743
744 void DolphinView::dropUrls(const KUrl::List& urls,
745 const KUrl& destination)
746 {
747 emit urlsDropped(urls, destination);
748 }
749
750 void DolphinView::updateSorting(DolphinView::Sorting sorting)
751 {
752 ViewProperties props(viewPropertiesUrl());
753 props.setSorting(sorting);
754
755 m_proxyModel->setSorting(sorting);
756
757 emit sortingChanged(sorting);
758 }
759
760 void DolphinView::updateSortOrder(Qt::SortOrder order)
761 {
762 ViewProperties props(viewPropertiesUrl());
763 props.setSortOrder(order);
764
765 m_proxyModel->setSortOrder(order);
766
767 emit sortOrderChanged(order);
768 }
769
770 void DolphinView::emitContentsMoved()
771 {
772 // only emit the contents moved signal if:
773 // - no directory loading is ongoing (this would reset the contents position
774 // always to (0, 0))
775 // - if the Column View is active: the column view does an automatic
776 // positioning during the loading operation, which must be remembered
777 if (!m_loadingDirectory || isColumnViewActive()) {
778 const QPoint pos(contentsPosition());
779 emit contentsMoved(pos.x(), pos.y());
780 }
781 }
782
783 void DolphinView::updateCutItems()
784 {
785 // restore the icons of all previously selected items to the
786 // original state...
787 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
788 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
789 while (it != end) {
790 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
791 if (index.isValid()) {
792 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
793 }
794 ++it;
795 }
796 m_cutItemsCache.clear();
797
798 // ... and apply an item effect to all currently cut items
799 applyCutItemEffect();
800 }
801
802 void DolphinView::showHoverInformation(const QModelIndex& index)
803 {
804 if (hasSelection()) {
805 return;
806 }
807
808 const KFileItem* item = fileItem(index);
809 if (item != 0) {
810 emit requestItemInfo(*item);
811 }
812 }
813
814 void DolphinView::clearHoverInformation()
815 {
816 emit requestItemInfo(KFileItem());
817 }
818
819
820 void DolphinView::createView()
821 {
822 // delete current view
823 QAbstractItemView* view = itemView();
824 if (view != 0) {
825 m_topLayout->removeWidget(view);
826 view->close();
827 if (view == m_iconsView) {
828 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
829 m_iconsView->setItemCategorizer(0);
830 delete categorizer;
831 }
832 view->deleteLater();
833 view = 0;
834 m_iconsView = 0;
835 m_detailsView = 0;
836 m_columnView = 0;
837 m_fileItemDelegate = 0;
838 }
839
840 Q_ASSERT(m_iconsView == 0);
841 Q_ASSERT(m_detailsView == 0);
842 Q_ASSERT(m_columnView == 0);
843
844 // ... and recreate it representing the current mode
845 switch (m_mode) {
846 case IconsView:
847 m_iconsView = new DolphinIconsView(this, m_controller);
848 view = m_iconsView;
849 break;
850
851 case DetailsView:
852 m_detailsView = new DolphinDetailsView(this, m_controller);
853 view = m_detailsView;
854 break;
855
856 case ColumnView:
857 m_columnView = new DolphinColumnView(this, m_controller);
858 view = m_columnView;
859 break;
860 }
861
862 Q_ASSERT(view != 0);
863
864 m_fileItemDelegate = new KFileItemDelegate(view);
865 view->setItemDelegate(m_fileItemDelegate);
866
867 view->setModel(m_proxyModel);
868 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
869
870 new KMimeTypeResolver(view, m_dirModel);
871 m_topLayout->insertWidget(1, view);
872
873 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
874 this, SLOT(emitSelectionChangedSignal()));
875 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
876 this, SLOT(emitContentsMoved()));
877 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
878 this, SLOT(emitContentsMoved()));
879 }
880
881 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
882 {
883 QItemSelectionModel* selectionModel = itemView()->selectionModel();
884 const QAbstractItemModel* itemModel = selectionModel->model();
885
886 const QModelIndex topLeft = itemModel->index(0, 0);
887 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
888 itemModel->columnCount() - 1);
889
890 QItemSelection selection(topLeft, bottomRight);
891 selectionModel->select(selection, flags);
892 }
893
894 QAbstractItemView* DolphinView::itemView() const
895 {
896 if (m_detailsView != 0) {
897 return m_detailsView;
898 } else if (m_columnView != 0) {
899 return m_columnView;
900 }
901
902 return m_iconsView;
903 }
904
905 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
906 {
907 return index.isValid() && (index.column() == KDirModel::Name);
908 }
909
910 bool DolphinView::isCutItem(const KFileItem& item) const
911 {
912 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
913 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
914
915 const KUrl& itemUrl = item.url();
916 KUrl::List::const_iterator it = cutUrls.begin();
917 const KUrl::List::const_iterator end = cutUrls.end();
918 while (it != end) {
919 if (*it == itemUrl) {
920 return true;
921 }
922 ++it;
923 }
924
925 return false;
926 }
927
928 void DolphinView::applyCutItemEffect()
929 {
930 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
931 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
932 return;
933 }
934
935 KFileItemList items(m_dirLister->items());
936 KFileItemList::const_iterator it = items.begin();
937 const KFileItemList::const_iterator end = items.end();
938 while (it != end) {
939 KFileItem* item = *it;
940 if (isCutItem(*item)) {
941 const QModelIndex index = m_dirModel->indexForItem(*item);
942 const KFileItem* item = m_dirModel->itemForIndex(index);
943 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
944 if ((value.type() == QVariant::Icon) && (item != 0)) {
945 const QIcon icon(qvariant_cast<QIcon>(value));
946 QPixmap pixmap = icon.pixmap(128, 128);
947
948 // remember current pixmap for the item to be able
949 // to restore it when other items get cut
950 CutItem cutItem;
951 cutItem.url = item->url();
952 cutItem.pixmap = pixmap;
953 m_cutItemsCache.append(cutItem);
954
955 // apply icon effect to the cut item
956 KIconEffect iconEffect;
957 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
958 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
959 }
960 }
961 ++it;
962 }
963 }
964
965 #include "dolphinview.moc"