]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
removed obsolete renaming code, which is not needed anymore due to the DolphinView...
[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 <kdirmodel.h>
32 #include <kdirlister.h>
33 #include <kfileitemdelegate.h>
34 #include <kglobalsettings.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 Mode mode,
62 bool showHiddenFiles) :
63 QWidget(parent),
64 m_active(true),
65 m_blockContentsMovedSignal(false),
66 m_initializeColumnView(false),
67 m_mode(mode),
68 m_topLayout(0),
69 m_controller(0),
70 m_iconsView(0),
71 m_detailsView(0),
72 m_columnView(0),
73 m_fileItemDelegate(0),
74 m_dirModel(dirModel),
75 m_dirLister(dirLister),
76 m_proxyModel(proxyModel)
77 {
78 setFocusPolicy(Qt::StrongFocus);
79 m_topLayout = new QVBoxLayout(this);
80 m_topLayout->setSpacing(0);
81 m_topLayout->setMargin(0);
82
83 QClipboard* clipboard = QApplication::clipboard();
84 connect(clipboard, SIGNAL(dataChanged()),
85 this, SLOT(updateCutItems()));
86
87 connect(m_dirLister, SIGNAL(completed()),
88 this, SLOT(restoreContentsPos()));
89 connect(m_dirLister, SIGNAL(completed()),
90 this, SLOT(updateCutItems()));
91 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
92 this, SLOT(generatePreviews(const KFileItemList&)));
93
94 m_controller = new DolphinController(this);
95 m_controller->setUrl(url);
96 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
97 this, SLOT(openContextMenu(const QPoint&)));
98 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&, QWidget*)),
99 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&, QWidget*)));
100 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
101 this, SLOT(updateSorting(DolphinView::Sorting)));
102 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
103 this, SLOT(updateSortOrder(Qt::SortOrder)));
104 connect(m_controller, SIGNAL(itemTriggered(const QModelIndex&)),
105 this, SLOT(triggerItem(const QModelIndex&)));
106 connect(m_controller, SIGNAL(activated()),
107 this, SLOT(activate()));
108 connect(m_controller, SIGNAL(itemEntered(const QModelIndex&)),
109 this, SLOT(showHoverInformation(const QModelIndex&)));
110 connect(m_controller, SIGNAL(viewportEntered()),
111 this, SLOT(clearHoverInformation()));
112
113 createView();
114 m_topLayout->addWidget(itemView());
115 }
116
117 DolphinView::~DolphinView()
118 {
119 }
120
121 const KUrl& DolphinView::url() const
122 {
123 return m_controller->url();
124 }
125
126 KUrl DolphinView::rootUrl() const
127 {
128 return isColumnViewActive() ? m_dirLister->url() : url();
129 }
130
131 void DolphinView::setActive(bool active)
132 {
133 if (active == m_active) {
134 return;
135 }
136
137 m_active = active;
138
139 QColor color = KGlobalSettings::baseColor();
140 if (active) {
141 emit urlChanged(url());
142 emit selectionChanged(selectedItems());
143 } else {
144 color.setAlpha(0);
145 }
146
147 QWidget* viewport = itemView()->viewport();
148 QPalette palette;
149 palette.setColor(viewport->backgroundRole(), color);
150 viewport->setPalette(palette);
151
152 update();
153
154 if (active) {
155 emit activated();
156 }
157 }
158
159 bool DolphinView::isActive() const
160 {
161 return m_active;
162 }
163
164 void DolphinView::setMode(Mode mode)
165 {
166 if (mode == m_mode) {
167 return; // the wished mode is already set
168 }
169
170 m_mode = mode;
171
172 if (isColumnViewActive()) {
173 // When changing the mode in the column view, it makes sense
174 // to go back to the root URL of the column view automatically.
175 // Otherwise there it would not be possible to turn off the column view
176 // without focusing the first column.
177 // TODO: reactivate again after DolphinView/DolphinViewController split works
178 //setUrl(m_dirLister->url());
179 //m_controller->setUrl(m_dirLister->url());
180 }
181
182 ViewProperties props(url());
183 props.setViewMode(m_mode);
184
185 createView();
186 startDirLister(url());
187
188 emit modeChanged();
189 }
190
191 DolphinView::Mode DolphinView::mode() const
192 {
193 return m_mode;
194 }
195
196 void DolphinView::setShowPreview(bool show)
197 {
198 ViewProperties props(url());
199 props.setShowPreview(show);
200
201 m_controller->setShowPreview(show);
202 emit showPreviewChanged();
203
204 startDirLister(url(), true);
205 }
206
207 bool DolphinView::showPreview() const
208 {
209 return m_controller->showPreview();
210 }
211
212 void DolphinView::setShowHiddenFiles(bool show)
213 {
214 if (m_dirLister->showingDotFiles() == show) {
215 return;
216 }
217
218 ViewProperties props(url());
219 props.setShowHiddenFiles(show);
220
221 m_dirLister->setShowingDotFiles(show);
222 emit showHiddenFilesChanged();
223
224 startDirLister(url(), 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(url());
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 int DolphinView::contentsX() const
281 {
282 return itemView()->horizontalScrollBar()->value();
283 }
284
285 int DolphinView::contentsY() const
286 {
287 return itemView()->verticalScrollBar()->value();
288 }
289
290 void DolphinView::zoomIn()
291 {
292 m_controller->triggerZoomIn();
293 }
294
295 void DolphinView::zoomOut()
296 {
297 m_controller->triggerZoomOut();
298 }
299
300 bool DolphinView::isZoomInPossible() const
301 {
302 return m_controller->isZoomInPossible();
303 }
304
305 bool DolphinView::isZoomOutPossible() const
306 {
307 return m_controller->isZoomOutPossible();
308 }
309
310 void DolphinView::setSorting(Sorting sorting)
311 {
312 if (sorting != this->sorting()) {
313 updateSorting(sorting);
314 }
315 }
316
317 DolphinView::Sorting DolphinView::sorting() const
318 {
319 return m_proxyModel->sorting();
320 }
321
322 void DolphinView::setSortOrder(Qt::SortOrder order)
323 {
324 if (sortOrder() != order) {
325 updateSortOrder(order);
326 }
327 }
328
329 Qt::SortOrder DolphinView::sortOrder() const
330 {
331 return m_proxyModel->sortOrder();
332 }
333
334 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
335 {
336 ViewProperties props(url());
337 props.setAdditionalInfo(info);
338
339 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
340 m_fileItemDelegate->setAdditionalInformation(info);
341
342 emit additionalInfoChanged(info);
343 startDirLister(url(), true);
344 }
345
346 KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
347 {
348 return m_fileItemDelegate->additionalInformation();
349 }
350
351 bool DolphinView::hasSelection() const
352 {
353 return itemView()->selectionModel()->hasSelection();
354 }
355
356 void DolphinView::clearSelection()
357 {
358 itemView()->selectionModel()->clear();
359 }
360
361 KFileItemList DolphinView::selectedItems() const
362 {
363 const QAbstractItemView* view = itemView();
364
365 // Our view has a selection, we will map them back to the DirModel
366 // and then fill the KFileItemList.
367 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
368
369 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
370 KFileItemList itemList;
371
372 const QModelIndexList indexList = selection.indexes();
373 QModelIndexList::const_iterator end = indexList.end();
374 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
375 Q_ASSERT((*it).isValid());
376
377 KFileItem* item = m_dirModel->itemForIndex(*it);
378 if (item != 0) {
379 itemList.append(item);
380 }
381 }
382
383 return itemList;
384 }
385
386 KUrl::List DolphinView::selectedUrls() const
387 {
388 KUrl::List urls;
389
390 const KFileItemList list = selectedItems();
391 KFileItemList::const_iterator it = list.begin();
392 const KFileItemList::const_iterator end = list.end();
393 while (it != end) {
394 KFileItem* item = *it;
395 urls.append(item->url());
396 ++it;
397 }
398
399 return urls;
400 }
401
402 KFileItem* DolphinView::fileItem(const QModelIndex index) const
403 {
404 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
405 return m_dirModel->itemForIndex(dirModelIndex);
406 }
407
408 void DolphinView::reload()
409 {
410 setUrl(url());
411 startDirLister(url(), true);
412 }
413
414 void DolphinView::refresh()
415 {
416 createView();
417 reload();
418 }
419
420 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
421 {
422 QWidget::mouseReleaseEvent(event);
423 setActive(true);;
424 }
425 void DolphinView::activate()
426 {
427 setActive(true);
428 }
429
430 void DolphinView::triggerItem(const QModelIndex& index)
431 {
432 if (!isValidNameIndex(index)) {
433 clearSelection();
434 showHoverInformation(index);
435 return;
436 }
437
438 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
439 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
440 // items are selected by the user, hence don't trigger the
441 // item specified by 'index'
442 return;
443 }
444
445 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
446 if (item == 0) {
447 return;
448 }
449
450 // Prefer the local path over the URL. This assures that the
451 // volume space information is correct. Assuming that the URL is media:/sda1,
452 // and the local path is /windows/C: For the URL the space info is related
453 // to the root partition (and hence wrong) and for the local path the space
454 // info is related to the windows partition (-> correct).
455 const QString localPath(item->localPath());
456 KUrl url;
457 if (localPath.isEmpty()) {
458 url = item->url();
459 } else {
460 url = localPath;
461 }
462
463 if (item->isDir()) {
464 setUrl(url);
465 } else if (item->isFile()) {
466 // allow to browse through ZIP and tar files
467 KMimeType::Ptr mime = item->mimeTypePtr();
468 if (mime->is("application/zip")) {
469 url.setProtocol("zip");
470 setUrl(url);
471 } else if (mime->is("application/x-tar") ||
472 mime->is("application/x-tarz") ||
473 mime->is("application/x-bzip-compressed-tar") ||
474 mime->is("application/x-compressed-tar") ||
475 mime->is("application/x-tzo")) {
476 url.setProtocol("tar");
477 setUrl(url);
478 } else {
479 item->run();
480 }
481 } else {
482 item->run();
483 }
484 }
485
486 void DolphinView::generatePreviews(const KFileItemList& items)
487 {
488 if (m_controller->showPreview()) {
489
490 // Must turn QList<KFileItem *> to QList<KFileItem>...
491 QList<KFileItem> itemsToPreview;
492 foreach( KFileItem* it, items )
493 itemsToPreview.append( *it );
494
495 KIO::PreviewJob* job = KIO::filePreview(itemsToPreview, 128);
496 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
497 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
498 }
499 }
500
501 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
502 {
503 Q_ASSERT(!item.isNull());
504 if (item.url().directory() != m_dirLister->url().path()) {
505 // the preview job is still working on items of an older URL, hence
506 // the item is not part of the directory model anymore
507 return;
508 }
509
510 const QModelIndex idx = m_dirModel->indexForItem(item);
511 if (idx.isValid() && (idx.column() == 0)) {
512 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
513 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
514 KIconEffect iconEffect;
515 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
516 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
517 } else {
518 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
519 }
520 }
521 }
522
523 void DolphinView::restoreContentsPos()
524 {
525 m_blockContentsMovedSignal = false;
526 if (!url().isEmpty()) {
527 QAbstractItemView* view = itemView();
528 // TODO #1: view->setCurrentItem(m_urlNavigator->currentFileName());
529 // TODO #2: temporary deactivated due to DolphinView/DolphinViewController split
530 //QPoint pos = m_urlNavigator->savedPosition();
531 QPoint pos(0, 0);
532 view->horizontalScrollBar()->setValue(pos.x());
533 view->verticalScrollBar()->setValue(pos.y());
534 }
535 }
536
537 void DolphinView::emitSelectionChangedSignal()
538 {
539 emit selectionChanged(DolphinView::selectedItems());
540 }
541
542 void DolphinView::startDirLister(const KUrl& url, bool reload)
543 {
544 if (!url.isValid()) {
545 // TODO: temporary deactivated due to DolphinView/DolphinViewController split
546
547 //const QString location(url.pathOrUrl());
548 //if (location.isEmpty()) {
549 // m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
550 //} else {
551 // m_statusBar->setMessage(i18n("The location '%1' is invalid.", location),
552 // DolphinStatusBar::Error);
553 //}
554 return;
555 }
556
557 // Only show the directory loading progress if the status bar does
558 // not contain another progress information. This means that
559 // the directory loading progress information has the lowest priority.
560
561 // TODO: temporary deactivated due to DolphinView/DolphinViewController split
562 //const QString progressText(m_statusBar->progressText());
563 //m_showProgress = progressText.isEmpty() ||
564 // (progressText == i18n("Loading folder..."));
565 //if (m_showProgress) {
566 // m_statusBar->setProgressText(i18n("Loading folder..."));
567 // m_statusBar->setProgress(0);
568 //}
569
570 m_cutItemsCache.clear();
571 m_blockContentsMovedSignal = true;
572 m_dirLister->stop();
573
574 bool openDir = true;
575 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
576 m_initializeColumnView = false;
577
578 if (keepOldDirs) {
579 if (reload) {
580 keepOldDirs = false;
581
582 const KUrl& dirListerUrl = m_dirLister->url();
583 if (dirListerUrl.isValid()) {
584 const KUrl::List dirs = m_dirLister->directories();
585 KUrl url;
586 foreach(url, dirs) {
587 m_dirLister->updateDirectory(url);
588 }
589 openDir = false;
590 }
591 } else if (m_dirLister->directories().contains(url)) {
592 // The dir lister contains the directory already, so
593 // KDirLister::openUrl() may not been invoked twice.
594 m_dirLister->updateDirectory(url);
595 openDir = false;
596 } else {
597 const KUrl& dirListerUrl = m_dirLister->url();
598 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
599 // The current URL is not a child of the dir lister
600 // URL. This may happen when e. g. a bookmark has been selected
601 // and hence the view must be reset.
602 keepOldDirs = false;
603 }
604 }
605 }
606
607 if (openDir) {
608 m_dirLister->openUrl(url, keepOldDirs, reload);
609 }
610 }
611
612 void DolphinView::setUrl(const KUrl& url)
613 {
614 if (m_controller->url() == url) {
615 return;
616 }
617
618 m_controller->setUrl(url);
619
620 const ViewProperties props(url);
621
622 const Mode mode = props.viewMode();
623 bool changeMode = (m_mode != mode);
624 if (changeMode && isColumnViewActive()) {
625 // The column view is active. Only change the
626 // mode if the current URL is no child of the column view.
627 if (m_dirLister->url().isParentOf(url)) {
628 changeMode = false;
629 }
630 }
631
632 if (changeMode) {
633 m_mode = mode;
634 createView();
635 emit modeChanged();
636
637 if (m_mode == ColumnView) {
638 // The mode has been changed to the Column View. When starting the dir
639 // lister with DolphinView::startDirLister() it is important to give a
640 // hint that the dir lister may not keep the current directory
641 // although this is the default for showing a hierarchy.
642 m_initializeColumnView = true;
643 }
644 }
645
646 const bool showHiddenFiles = props.showHiddenFiles();
647 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
648 m_dirLister->setShowingDotFiles(showHiddenFiles);
649 emit showHiddenFilesChanged();
650 }
651
652 const bool categorized = props.categorizedSorting();
653 if (categorized != categorizedSorting()) {
654 if (supportsCategorizedSorting()) {
655 Q_ASSERT(m_iconsView != 0);
656 if (categorized) {
657 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
658 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
659 } else {
660 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
661 m_iconsView->setItemCategorizer(0);
662 delete categorizer;
663 }
664 }
665 emit categorizedSortingChanged();
666 }
667
668 const DolphinView::Sorting sorting = props.sorting();
669 if (sorting != m_proxyModel->sorting()) {
670 m_proxyModel->setSorting(sorting);
671 emit sortingChanged(sorting);
672 }
673
674 const Qt::SortOrder sortOrder = props.sortOrder();
675 if (sortOrder != m_proxyModel->sortOrder()) {
676 m_proxyModel->setSortOrder(sortOrder);
677 emit sortOrderChanged(sortOrder);
678 }
679
680 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
681 if (info != m_fileItemDelegate->additionalInformation()) {
682 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
683 m_fileItemDelegate->setAdditionalInformation(info);
684 emit additionalInfoChanged(info);
685 }
686
687 const bool showPreview = props.showPreview();
688 if (showPreview != m_controller->showPreview()) {
689 m_controller->setShowPreview(showPreview);
690 emit showPreviewChanged();
691 }
692
693 startDirLister(url);
694 emit urlChanged(url);
695
696 // TODO: temporary deactivated due to DolphinView/DolphinViewController split
697 //m_statusBar->clear();
698 }
699
700 void DolphinView::changeSelection(const KFileItemList& selection)
701 {
702 clearSelection();
703 if (selection.isEmpty()) {
704 return;
705 }
706 const KUrl& baseUrl = url();
707 KUrl url;
708 QItemSelection new_selection;
709 foreach(KFileItem* item, selection) {
710 url = item->url().upUrl();
711 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
712 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(*item));
713 new_selection.select(index, index);
714 }
715 }
716 itemView()->selectionModel()->select(new_selection,
717 QItemSelectionModel::ClearAndSelect
718 | QItemSelectionModel::Current);
719 }
720
721 void DolphinView::openContextMenu(const QPoint& pos)
722 {
723 KFileItem* item = 0;
724
725 const QModelIndex index = itemView()->indexAt(pos);
726 if (isValidNameIndex(index)) {
727 item = fileItem(index);
728 }
729
730 emit requestContextMenu(item, url());
731 }
732
733 void DolphinView::dropUrls(const KUrl::List& urls,
734 const QModelIndex& index,
735 QWidget* source)
736 {
737 KFileItem* directory = 0;
738 if (isValidNameIndex(index)) {
739 KFileItem* item = fileItem(index);
740 Q_ASSERT(item != 0);
741 if (item->isDir()) {
742 // the URLs are dropped above a directory
743 directory = item;
744 }
745 }
746
747 if ((directory == 0) && (source == itemView())) {
748 // The dropping is done into the same viewport where
749 // the dragging has been started. Just ignore this...
750 return;
751 }
752
753 const KUrl& destination = (directory == 0) ?
754 url() : directory->url();
755 dropUrls(urls, destination);
756 }
757
758 void DolphinView::dropUrls(const KUrl::List& urls,
759 const KUrl& destination)
760 {
761 emit urlsDropped(urls, destination);
762 }
763
764 void DolphinView::updateSorting(DolphinView::Sorting sorting)
765 {
766 ViewProperties props(url());
767 props.setSorting(sorting);
768
769 m_proxyModel->setSorting(sorting);
770
771 emit sortingChanged(sorting);
772 }
773
774 void DolphinView::updateSortOrder(Qt::SortOrder order)
775 {
776 ViewProperties props(url());
777 props.setSortOrder(order);
778
779 m_proxyModel->setSortOrder(order);
780
781 emit sortOrderChanged(order);
782 }
783
784 void DolphinView::emitContentsMoved()
785 {
786 if (!m_blockContentsMovedSignal) {
787 emit contentsMoved(contentsX(), contentsY());
788 }
789 }
790
791 void DolphinView::updateCutItems()
792 {
793 // restore the icons of all previously selected items to the
794 // original state...
795 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
796 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
797 while (it != end) {
798 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
799 if (index.isValid()) {
800 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
801 }
802 ++it;
803 }
804 m_cutItemsCache.clear();
805
806 // ... and apply an item effect to all currently cut items
807 applyCutItemEffect();
808 }
809
810 void DolphinView::showHoverInformation(const QModelIndex& index)
811 {
812 if (hasSelection()) {
813 return;
814 }
815
816 const KFileItem* item = fileItem(index);
817 if (item != 0) {
818 // TODO: temporary deactivated due to DolphinView/DolphinViewController split
819 //m_statusBar->setMessage(item->getStatusBarInfo(), DolphinStatusBar::Default);
820 emit requestItemInfo(item->url());
821 }
822 }
823
824 void DolphinView::clearHoverInformation()
825 {
826 // TODO: temporary deactivated due to DolphinView/DolphinViewController split
827 //m_statusBar->clear();
828 emit requestItemInfo(KUrl());
829 }
830
831
832 void DolphinView::createView()
833 {
834 // delete current view
835 QAbstractItemView* view = itemView();
836 if (view != 0) {
837 m_topLayout->removeWidget(view);
838 view->close();
839 if (view == m_iconsView) {
840 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
841 m_iconsView->setItemCategorizer(0);
842 delete categorizer;
843 }
844 view->deleteLater();
845 view = 0;
846 m_iconsView = 0;
847 m_detailsView = 0;
848 m_columnView = 0;
849 m_fileItemDelegate = 0;
850 }
851
852 Q_ASSERT(m_iconsView == 0);
853 Q_ASSERT(m_detailsView == 0);
854 Q_ASSERT(m_columnView == 0);
855
856 // ... and recreate it representing the current mode
857 switch (m_mode) {
858 case IconsView:
859 m_iconsView = new DolphinIconsView(this, m_controller);
860 view = m_iconsView;
861 break;
862
863 case DetailsView:
864 m_detailsView = new DolphinDetailsView(this, m_controller);
865 view = m_detailsView;
866 break;
867
868 case ColumnView:
869 m_columnView = new DolphinColumnView(this, m_controller);
870 view = m_columnView;
871 break;
872 }
873
874 Q_ASSERT(view != 0);
875
876 m_fileItemDelegate = new KFileItemDelegate(view);
877 view->setItemDelegate(m_fileItemDelegate);
878
879 view->setModel(m_proxyModel);
880 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
881
882 new KMimeTypeResolver(view, m_dirModel);
883 m_topLayout->insertWidget(1, view);
884
885 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
886 this, SLOT(emitSelectionChangedSignal()));
887 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
888 this, SLOT(emitContentsMoved()));
889 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
890 this, SLOT(emitContentsMoved()));
891 }
892
893 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
894 {
895 QItemSelectionModel* selectionModel = itemView()->selectionModel();
896 const QAbstractItemModel* itemModel = selectionModel->model();
897
898 const QModelIndex topLeft = itemModel->index(0, 0);
899 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
900 itemModel->columnCount() - 1);
901
902 QItemSelection selection(topLeft, bottomRight);
903 selectionModel->select(selection, flags);
904 }
905
906 QAbstractItemView* DolphinView::itemView() const
907 {
908 if (m_detailsView != 0) {
909 return m_detailsView;
910 } else if (m_columnView != 0) {
911 return m_columnView;
912 }
913
914 return m_iconsView;
915 }
916
917 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
918 {
919 return index.isValid() && (index.column() == KDirModel::Name);
920 }
921
922 bool DolphinView::isCutItem(const KFileItem& item) const
923 {
924 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
925 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
926
927 const KUrl& itemUrl = item.url();
928 KUrl::List::const_iterator it = cutUrls.begin();
929 const KUrl::List::const_iterator end = cutUrls.end();
930 while (it != end) {
931 if (*it == itemUrl) {
932 return true;
933 }
934 ++it;
935 }
936
937 return false;
938 }
939
940 void DolphinView::applyCutItemEffect()
941 {
942 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
943 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
944 return;
945 }
946
947 KFileItemList items(m_dirLister->items());
948 KFileItemList::const_iterator it = items.begin();
949 const KFileItemList::const_iterator end = items.end();
950 while (it != end) {
951 KFileItem* item = *it;
952 if (isCutItem(*item)) {
953 const QModelIndex index = m_dirModel->indexForItem(*item);
954 const KFileItem* item = m_dirModel->itemForIndex(index);
955 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
956 if ((value.type() == QVariant::Icon) && (item != 0)) {
957 const QIcon icon(qvariant_cast<QIcon>(value));
958 QPixmap pixmap = icon.pixmap(128, 128);
959
960 // remember current pixmap for the item to be able
961 // to restore it when other items get cut
962 CutItem cutItem;
963 cutItem.url = item->url();
964 cutItem.pixmap = pixmap;
965 m_cutItemsCache.append(cutItem);
966
967 // apply icon effect to the cut item
968 KIconEffect iconEffect;
969 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
970 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
971 }
972 }
973 ++it;
974 }
975 }
976
977 #include "dolphinview.moc"