]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
First big step to make it possible that the DolphinView can be embedded as KPart...
[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::renameSelectedItems()
271 {
272 // TODO: temporary deactivate due to DolphinView/DolphinViewController split
273
274 /*DolphinView* view = 0; //mainWindow()->activeView();
275 const KUrl::List urls = selectedUrls();
276 if (urls.count() > 1) {
277 // More than one item has been selected for renaming. Open
278 // a rename dialog and rename all items afterwards.
279 RenameDialog dialog(urls);
280 if (dialog.exec() == QDialog::Rejected) {
281 return;
282 }
283
284 const QString& newName = dialog.newName();
285 if (newName.isEmpty()) {
286 view->statusBar()->setMessage(dialog.errorString(),
287 DolphinStatusBar::Error);
288 } else {
289 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
290 // as one operation instead of n rename operations like it is done now...
291 Q_ASSERT(newName.contains('#'));
292
293 // iterate through all selected items and rename them...
294 const int replaceIndex = newName.indexOf('#');
295 Q_ASSERT(replaceIndex >= 0);
296 int index = 1;
297
298 KUrl::List::const_iterator it = urls.begin();
299 KUrl::List::const_iterator end = urls.end();
300 while (it != end) {
301 const KUrl& oldUrl = *it;
302 QString number;
303 number.setNum(index++);
304
305 QString name(newName);
306 name.replace(replaceIndex, 1, number);
307
308 if (oldUrl.fileName() != name) {
309 KUrl newUrl = oldUrl;
310 newUrl.setFileName(name);
311 m_mainWindow->rename(oldUrl, newUrl);
312 }
313 ++it;
314 }
315 }
316 } else {
317 // Only one item has been selected for renaming. Use the custom
318 // renaming mechanism from the views.
319 Q_ASSERT(urls.count() == 1);
320
321 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
322 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
323 // is a benefit for the user at all -> let's wait for some input first...
324 RenameDialog dialog(urls);
325 if (dialog.exec() == QDialog::Rejected) {
326 return;
327 }
328
329 const QString& newName = dialog.newName();
330 if (newName.isEmpty()) {
331 view->statusBar()->setMessage(dialog.errorString(),
332 DolphinStatusBar::Error);
333 } else {
334 const KUrl& oldUrl = urls.first();
335 KUrl newUrl = oldUrl;
336 newUrl.setFileName(newName);
337 m_mainWindow->rename(oldUrl, newUrl);
338 }
339 }*/
340 }
341
342 void DolphinView::selectAll()
343 {
344 selectAll(QItemSelectionModel::Select);
345 }
346
347 void DolphinView::invertSelection()
348 {
349 selectAll(QItemSelectionModel::Toggle);
350 }
351
352 int DolphinView::contentsX() const
353 {
354 return itemView()->horizontalScrollBar()->value();
355 }
356
357 int DolphinView::contentsY() const
358 {
359 return itemView()->verticalScrollBar()->value();
360 }
361
362 void DolphinView::zoomIn()
363 {
364 m_controller->triggerZoomIn();
365 }
366
367 void DolphinView::zoomOut()
368 {
369 m_controller->triggerZoomOut();
370 }
371
372 bool DolphinView::isZoomInPossible() const
373 {
374 return m_controller->isZoomInPossible();
375 }
376
377 bool DolphinView::isZoomOutPossible() const
378 {
379 return m_controller->isZoomOutPossible();
380 }
381
382 void DolphinView::setSorting(Sorting sorting)
383 {
384 if (sorting != this->sorting()) {
385 updateSorting(sorting);
386 }
387 }
388
389 DolphinView::Sorting DolphinView::sorting() const
390 {
391 return m_proxyModel->sorting();
392 }
393
394 void DolphinView::setSortOrder(Qt::SortOrder order)
395 {
396 if (sortOrder() != order) {
397 updateSortOrder(order);
398 }
399 }
400
401 Qt::SortOrder DolphinView::sortOrder() const
402 {
403 return m_proxyModel->sortOrder();
404 }
405
406 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
407 {
408 ViewProperties props(url());
409 props.setAdditionalInfo(info);
410
411 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
412 m_fileItemDelegate->setAdditionalInformation(info);
413
414 emit additionalInfoChanged(info);
415 startDirLister(url(), true);
416 }
417
418 KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
419 {
420 return m_fileItemDelegate->additionalInformation();
421 }
422
423 bool DolphinView::hasSelection() const
424 {
425 return itemView()->selectionModel()->hasSelection();
426 }
427
428 void DolphinView::clearSelection()
429 {
430 itemView()->selectionModel()->clear();
431 }
432
433 KFileItemList DolphinView::selectedItems() const
434 {
435 const QAbstractItemView* view = itemView();
436
437 // Our view has a selection, we will map them back to the DirModel
438 // and then fill the KFileItemList.
439 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
440
441 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
442 KFileItemList itemList;
443
444 const QModelIndexList indexList = selection.indexes();
445 QModelIndexList::const_iterator end = indexList.end();
446 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
447 Q_ASSERT((*it).isValid());
448
449 KFileItem* item = m_dirModel->itemForIndex(*it);
450 if (item != 0) {
451 itemList.append(item);
452 }
453 }
454
455 return itemList;
456 }
457
458 KUrl::List DolphinView::selectedUrls() const
459 {
460 KUrl::List urls;
461
462 const KFileItemList list = selectedItems();
463 KFileItemList::const_iterator it = list.begin();
464 const KFileItemList::const_iterator end = list.end();
465 while (it != end) {
466 KFileItem* item = *it;
467 urls.append(item->url());
468 ++it;
469 }
470
471 return urls;
472 }
473
474 KFileItem* DolphinView::fileItem(const QModelIndex index) const
475 {
476 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
477 return m_dirModel->itemForIndex(dirModelIndex);
478 }
479
480 void DolphinView::rename(const KUrl& source, const QString& newName)
481 {
482 bool ok = false;
483
484 if (newName.isEmpty() || (source.fileName() == newName)) {
485 return;
486 }
487
488 KUrl dest(source.upUrl());
489 dest.addPath(newName);
490
491 const bool destExists = KIO::NetAccess::exists(dest, false, this);
492 if (destExists) {
493 // the destination already exists, hence ask the user
494 // how to proceed...
495 KIO::RenameDialog renameDialog(this,
496 i18n("File Already Exists"),
497 source.path(),
498 dest.path(),
499 KIO::M_OVERWRITE);
500 switch (renameDialog.exec()) {
501 case KIO::R_OVERWRITE:
502 // the destination should be overwritten
503 ok = KIO::NetAccess::file_move(source, dest, -1, true);
504 break;
505
506 case KIO::R_RENAME: {
507 // a new name for the destination has been used
508 KUrl newDest(renameDialog.newDestUrl());
509 ok = KIO::NetAccess::file_move(source, newDest);
510 break;
511 }
512
513 default:
514 // the renaming operation has been canceled
515 return;
516 }
517 } else {
518 // no destination exists, hence just move the file to
519 // do the renaming
520 ok = KIO::NetAccess::file_move(source, dest);
521 }
522
523 const QString destFileName = dest.fileName();
524 if (ok) {
525 // XYDZ
526 //m_statusBar->setMessage(i18n("Renamed file '%1' to '%2'.", source.fileName(), destFileName),
527 // DolphinStatusBar::OperationCompleted);
528
529 KonqOperations::rename(this, source, destFileName);
530 } else {
531 // XYDZ
532 //m_statusBar->setMessage(i18n("Renaming of file '%1' to '%2' failed.", source.fileName(), destFileName),
533 // DolphinStatusBar::Error);
534 }
535 }
536
537 void DolphinView::reload()
538 {
539 setUrl(url());
540 startDirLister(url(), true);
541 }
542
543 void DolphinView::refresh()
544 {
545 createView();
546 reload();
547 }
548
549 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
550 {
551 QWidget::mouseReleaseEvent(event);
552 setActive(true);;
553 }
554 void DolphinView::activate()
555 {
556 setActive(true);
557 }
558
559 void DolphinView::triggerItem(const QModelIndex& index)
560 {
561 if (!isValidNameIndex(index)) {
562 clearSelection();
563 showHoverInformation(index);
564 return;
565 }
566
567 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
568 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
569 // items are selected by the user, hence don't trigger the
570 // item specified by 'index'
571 return;
572 }
573
574 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
575 if (item == 0) {
576 return;
577 }
578
579 // Prefer the local path over the URL. This assures that the
580 // volume space information is correct. Assuming that the URL is media:/sda1,
581 // and the local path is /windows/C: For the URL the space info is related
582 // to the root partition (and hence wrong) and for the local path the space
583 // info is related to the windows partition (-> correct).
584 const QString localPath(item->localPath());
585 KUrl url;
586 if (localPath.isEmpty()) {
587 url = item->url();
588 } else {
589 url = localPath;
590 }
591
592 if (item->isDir()) {
593 setUrl(url);
594 } else if (item->isFile()) {
595 // allow to browse through ZIP and tar files
596 KMimeType::Ptr mime = item->mimeTypePtr();
597 if (mime->is("application/zip")) {
598 url.setProtocol("zip");
599 setUrl(url);
600 } else if (mime->is("application/x-tar") ||
601 mime->is("application/x-tarz") ||
602 mime->is("application/x-bzip-compressed-tar") ||
603 mime->is("application/x-compressed-tar") ||
604 mime->is("application/x-tzo")) {
605 url.setProtocol("tar");
606 setUrl(url);
607 } else {
608 item->run();
609 }
610 } else {
611 item->run();
612 }
613 }
614
615 void DolphinView::generatePreviews(const KFileItemList& items)
616 {
617 if (m_controller->showPreview()) {
618
619 // Must turn QList<KFileItem *> to QList<KFileItem>...
620 QList<KFileItem> itemsToPreview;
621 foreach( KFileItem* it, items )
622 itemsToPreview.append( *it );
623
624 KIO::PreviewJob* job = KIO::filePreview(itemsToPreview, 128);
625 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
626 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
627 }
628 }
629
630 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
631 {
632 Q_ASSERT(!item.isNull());
633 if (item.url().directory() != m_dirLister->url().path()) {
634 // the preview job is still working on items of an older URL, hence
635 // the item is not part of the directory model anymore
636 return;
637 }
638
639 const QModelIndex idx = m_dirModel->indexForItem(item);
640 if (idx.isValid() && (idx.column() == 0)) {
641 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
642 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
643 KIconEffect iconEffect;
644 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
645 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
646 } else {
647 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
648 }
649 }
650 }
651
652 void DolphinView::restoreContentsPos()
653 {
654 m_blockContentsMovedSignal = false;
655 if (!url().isEmpty()) {
656 QAbstractItemView* view = itemView();
657 // TODO #1: view->setCurrentItem(m_urlNavigator->currentFileName());
658 // TODO #2: temporary deactivated due to DolphinView/DolphinViewController split
659 //QPoint pos = m_urlNavigator->savedPosition();
660 QPoint pos(0, 0);
661 view->horizontalScrollBar()->setValue(pos.x());
662 view->verticalScrollBar()->setValue(pos.y());
663 }
664 }
665
666 void DolphinView::emitSelectionChangedSignal()
667 {
668 emit selectionChanged(DolphinView::selectedItems());
669 }
670
671 void DolphinView::startDirLister(const KUrl& url, bool reload)
672 {
673 if (!url.isValid()) {
674 // TODO: temporary deactivated due to DolphinView/DolphinViewController split
675
676 //const QString location(url.pathOrUrl());
677 //if (location.isEmpty()) {
678 // m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
679 //} else {
680 // m_statusBar->setMessage(i18n("The location '%1' is invalid.", location),
681 // DolphinStatusBar::Error);
682 //}
683 return;
684 }
685
686 // Only show the directory loading progress if the status bar does
687 // not contain another progress information. This means that
688 // the directory loading progress information has the lowest priority.
689
690 // TODO: temporary deactivated due to DolphinView/DolphinViewController split
691 //const QString progressText(m_statusBar->progressText());
692 //m_showProgress = progressText.isEmpty() ||
693 // (progressText == i18n("Loading folder..."));
694 //if (m_showProgress) {
695 // m_statusBar->setProgressText(i18n("Loading folder..."));
696 // m_statusBar->setProgress(0);
697 //}
698
699 m_cutItemsCache.clear();
700 m_blockContentsMovedSignal = true;
701 m_dirLister->stop();
702
703 bool openDir = true;
704 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
705 m_initializeColumnView = false;
706
707 if (keepOldDirs) {
708 if (reload) {
709 keepOldDirs = false;
710
711 const KUrl& dirListerUrl = m_dirLister->url();
712 if (dirListerUrl.isValid()) {
713 const KUrl::List dirs = m_dirLister->directories();
714 KUrl url;
715 foreach(url, dirs) {
716 m_dirLister->updateDirectory(url);
717 }
718 openDir = false;
719 }
720 } else if (m_dirLister->directories().contains(url)) {
721 // The dir lister contains the directory already, so
722 // KDirLister::openUrl() may not been invoked twice.
723 m_dirLister->updateDirectory(url);
724 openDir = false;
725 } else {
726 const KUrl& dirListerUrl = m_dirLister->url();
727 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
728 // The current URL is not a child of the dir lister
729 // URL. This may happen when e. g. a bookmark has been selected
730 // and hence the view must be reset.
731 keepOldDirs = false;
732 }
733 }
734 }
735
736 if (openDir) {
737 m_dirLister->openUrl(url, keepOldDirs, reload);
738 }
739 }
740
741 void DolphinView::setUrl(const KUrl& url)
742 {
743 if (m_controller->url() == url) {
744 return;
745 }
746
747 m_controller->setUrl(url);
748
749 const ViewProperties props(url);
750
751 const Mode mode = props.viewMode();
752 bool changeMode = (m_mode != mode);
753 if (changeMode && isColumnViewActive()) {
754 // The column view is active. Only change the
755 // mode if the current URL is no child of the column view.
756 if (m_dirLister->url().isParentOf(url)) {
757 changeMode = false;
758 }
759 }
760
761 if (changeMode) {
762 m_mode = mode;
763 createView();
764 emit modeChanged();
765
766 if (m_mode == ColumnView) {
767 // The mode has been changed to the Column View. When starting the dir
768 // lister with DolphinView::startDirLister() it is important to give a
769 // hint that the dir lister may not keep the current directory
770 // although this is the default for showing a hierarchy.
771 m_initializeColumnView = true;
772 }
773 }
774
775 const bool showHiddenFiles = props.showHiddenFiles();
776 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
777 m_dirLister->setShowingDotFiles(showHiddenFiles);
778 emit showHiddenFilesChanged();
779 }
780
781 const bool categorized = props.categorizedSorting();
782 if (categorized != categorizedSorting()) {
783 if (supportsCategorizedSorting()) {
784 Q_ASSERT(m_iconsView != 0);
785 if (categorized) {
786 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
787 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
788 } else {
789 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
790 m_iconsView->setItemCategorizer(0);
791 delete categorizer;
792 }
793 }
794 emit categorizedSortingChanged();
795 }
796
797 const DolphinView::Sorting sorting = props.sorting();
798 if (sorting != m_proxyModel->sorting()) {
799 m_proxyModel->setSorting(sorting);
800 emit sortingChanged(sorting);
801 }
802
803 const Qt::SortOrder sortOrder = props.sortOrder();
804 if (sortOrder != m_proxyModel->sortOrder()) {
805 m_proxyModel->setSortOrder(sortOrder);
806 emit sortOrderChanged(sortOrder);
807 }
808
809 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
810 if (info != m_fileItemDelegate->additionalInformation()) {
811 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
812 m_fileItemDelegate->setAdditionalInformation(info);
813 emit additionalInfoChanged(info);
814 }
815
816 const bool showPreview = props.showPreview();
817 if (showPreview != m_controller->showPreview()) {
818 m_controller->setShowPreview(showPreview);
819 emit showPreviewChanged();
820 }
821
822 startDirLister(url);
823 emit urlChanged(url);
824
825 // TODO: temporary deactivated due to DolphinView/DolphinViewController split
826 //m_statusBar->clear();
827 }
828
829 void DolphinView::changeSelection(const KFileItemList& selection)
830 {
831 clearSelection();
832 if (selection.isEmpty()) {
833 return;
834 }
835 const KUrl& baseUrl = url();
836 KUrl url;
837 QItemSelection new_selection;
838 foreach(KFileItem* item, selection) {
839 url = item->url().upUrl();
840 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
841 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(*item));
842 new_selection.select(index, index);
843 }
844 }
845 itemView()->selectionModel()->select(new_selection,
846 QItemSelectionModel::ClearAndSelect
847 | QItemSelectionModel::Current);
848 }
849
850 void DolphinView::openContextMenu(const QPoint& pos)
851 {
852 KFileItem* item = 0;
853
854 const QModelIndex index = itemView()->indexAt(pos);
855 if (isValidNameIndex(index)) {
856 item = fileItem(index);
857 }
858
859 emit requestContextMenu(item, url());
860 }
861
862 void DolphinView::dropUrls(const KUrl::List& urls,
863 const QModelIndex& index,
864 QWidget* source)
865 {
866 KFileItem* directory = 0;
867 if (isValidNameIndex(index)) {
868 KFileItem* item = fileItem(index);
869 Q_ASSERT(item != 0);
870 if (item->isDir()) {
871 // the URLs are dropped above a directory
872 directory = item;
873 }
874 }
875
876 if ((directory == 0) && (source == itemView())) {
877 // The dropping is done into the same viewport where
878 // the dragging has been started. Just ignore this...
879 return;
880 }
881
882 const KUrl& destination = (directory == 0) ?
883 url() : directory->url();
884 dropUrls(urls, destination);
885 }
886
887 void DolphinView::dropUrls(const KUrl::List& urls,
888 const KUrl& destination)
889 {
890 emit urlsDropped(urls, destination);
891 }
892
893 void DolphinView::updateSorting(DolphinView::Sorting sorting)
894 {
895 ViewProperties props(url());
896 props.setSorting(sorting);
897
898 m_proxyModel->setSorting(sorting);
899
900 emit sortingChanged(sorting);
901 }
902
903 void DolphinView::updateSortOrder(Qt::SortOrder order)
904 {
905 ViewProperties props(url());
906 props.setSortOrder(order);
907
908 m_proxyModel->setSortOrder(order);
909
910 emit sortOrderChanged(order);
911 }
912
913 void DolphinView::emitContentsMoved()
914 {
915 if (!m_blockContentsMovedSignal) {
916 emit contentsMoved(contentsX(), contentsY());
917 }
918 }
919
920 void DolphinView::updateCutItems()
921 {
922 // restore the icons of all previously selected items to the
923 // original state...
924 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
925 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
926 while (it != end) {
927 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
928 if (index.isValid()) {
929 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
930 }
931 ++it;
932 }
933 m_cutItemsCache.clear();
934
935 // ... and apply an item effect to all currently cut items
936 applyCutItemEffect();
937 }
938
939 void DolphinView::showHoverInformation(const QModelIndex& index)
940 {
941 if (hasSelection()) {
942 return;
943 }
944
945 const KFileItem* item = fileItem(index);
946 if (item != 0) {
947 // TODO: temporary deactivated due to DolphinView/DolphinViewController split
948 //m_statusBar->setMessage(item->getStatusBarInfo(), DolphinStatusBar::Default);
949 emit requestItemInfo(item->url());
950 }
951 }
952
953 void DolphinView::clearHoverInformation()
954 {
955 // TODO: temporary deactivated due to DolphinView/DolphinViewController split
956 //m_statusBar->clear();
957 emit requestItemInfo(KUrl());
958 }
959
960
961 void DolphinView::createView()
962 {
963 // delete current view
964 QAbstractItemView* view = itemView();
965 if (view != 0) {
966 m_topLayout->removeWidget(view);
967 view->close();
968 if (view == m_iconsView) {
969 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
970 m_iconsView->setItemCategorizer(0);
971 delete categorizer;
972 }
973 view->deleteLater();
974 view = 0;
975 m_iconsView = 0;
976 m_detailsView = 0;
977 m_columnView = 0;
978 m_fileItemDelegate = 0;
979 }
980
981 Q_ASSERT(m_iconsView == 0);
982 Q_ASSERT(m_detailsView == 0);
983 Q_ASSERT(m_columnView == 0);
984
985 // ... and recreate it representing the current mode
986 switch (m_mode) {
987 case IconsView:
988 m_iconsView = new DolphinIconsView(this, m_controller);
989 view = m_iconsView;
990 break;
991
992 case DetailsView:
993 m_detailsView = new DolphinDetailsView(this, m_controller);
994 view = m_detailsView;
995 break;
996
997 case ColumnView:
998 m_columnView = new DolphinColumnView(this, m_controller);
999 view = m_columnView;
1000 break;
1001 }
1002
1003 Q_ASSERT(view != 0);
1004
1005 m_fileItemDelegate = new KFileItemDelegate(view);
1006 view->setItemDelegate(m_fileItemDelegate);
1007
1008 view->setModel(m_proxyModel);
1009 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1010
1011 new KMimeTypeResolver(view, m_dirModel);
1012 m_topLayout->insertWidget(1, view);
1013
1014 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1015 this, SLOT(emitSelectionChangedSignal()));
1016 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1017 this, SLOT(emitContentsMoved()));
1018 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1019 this, SLOT(emitContentsMoved()));
1020 }
1021
1022 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
1023 {
1024 QItemSelectionModel* selectionModel = itemView()->selectionModel();
1025 const QAbstractItemModel* itemModel = selectionModel->model();
1026
1027 const QModelIndex topLeft = itemModel->index(0, 0);
1028 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
1029 itemModel->columnCount() - 1);
1030
1031 QItemSelection selection(topLeft, bottomRight);
1032 selectionModel->select(selection, flags);
1033 }
1034
1035 QAbstractItemView* DolphinView::itemView() const
1036 {
1037 if (m_detailsView != 0) {
1038 return m_detailsView;
1039 } else if (m_columnView != 0) {
1040 return m_columnView;
1041 }
1042
1043 return m_iconsView;
1044 }
1045
1046 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
1047 {
1048 return index.isValid() && (index.column() == KDirModel::Name);
1049 }
1050
1051 bool DolphinView::isCutItem(const KFileItem& item) const
1052 {
1053 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1054 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1055
1056 const KUrl& itemUrl = item.url();
1057 KUrl::List::const_iterator it = cutUrls.begin();
1058 const KUrl::List::const_iterator end = cutUrls.end();
1059 while (it != end) {
1060 if (*it == itemUrl) {
1061 return true;
1062 }
1063 ++it;
1064 }
1065
1066 return false;
1067 }
1068
1069 void DolphinView::applyCutItemEffect()
1070 {
1071 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1072 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
1073 return;
1074 }
1075
1076 KFileItemList items(m_dirLister->items());
1077 KFileItemList::const_iterator it = items.begin();
1078 const KFileItemList::const_iterator end = items.end();
1079 while (it != end) {
1080 KFileItem* item = *it;
1081 if (isCutItem(*item)) {
1082 const QModelIndex index = m_dirModel->indexForItem(*item);
1083 const KFileItem* item = m_dirModel->itemForIndex(index);
1084 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
1085 if ((value.type() == QVariant::Icon) && (item != 0)) {
1086 const QIcon icon(qvariant_cast<QIcon>(value));
1087 QPixmap pixmap = icon.pixmap(128, 128);
1088
1089 // remember current pixmap for the item to be able
1090 // to restore it when other items get cut
1091 CutItem cutItem;
1092 cutItem.url = item->url();
1093 cutItem.pixmap = pixmap;
1094 m_cutItemsCache.append(cutItem);
1095
1096 // apply icon effect to the cut item
1097 KIconEffect iconEffect;
1098 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
1099 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
1100 }
1101 }
1102 ++it;
1103 }
1104 }
1105
1106 #include "dolphinview.moc"