]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
allow to enable the categorization feature for sorting
[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 <QDropEvent>
26 #include <QItemSelectionModel>
27 #include <QMouseEvent>
28 #include <QVBoxLayout>
29 #include <QTimer>
30 #include <QScrollBar>
31
32 #include <kdirmodel.h>
33 #include <kfileitemdelegate.h>
34 #include <kfileplacesmodel.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 "dolphinstatusbar.h"
48 #include "dolphinmainwindow.h"
49 #include "dolphindirlister.h"
50 #include "dolphinsortfilterproxymodel.h"
51 #include "dolphindetailsview.h"
52 #include "dolphiniconsview.h"
53 #include "dolphincontextmenu.h"
54 #include "dolphinitemcategorizer.h"
55 #include "filterbar.h"
56 #include "renamedialog.h"
57 #include "kurlnavigator.h"
58 #include "viewproperties.h"
59 #include "dolphinsettings.h"
60 #include "dolphin_generalsettings.h"
61
62 DolphinView::DolphinView(DolphinMainWindow* mainWindow,
63 QWidget* parent,
64 const KUrl& url,
65 Mode mode,
66 bool showHiddenFiles) :
67 QWidget(parent),
68 m_showProgress(false),
69 m_blockContentsMovedSignal(false),
70 m_mode(mode),
71 m_iconSize(0),
72 m_folderCount(0),
73 m_fileCount(0),
74 m_mainWindow(mainWindow),
75 m_topLayout(0),
76 m_urlNavigator(0),
77 m_controller(0),
78 m_iconsView(0),
79 m_detailsView(0),
80 m_columnView(0),
81 m_fileItemDelegate(0),
82 m_filterBar(0),
83 m_statusBar(0),
84 m_dirModel(0),
85 m_dirLister(0),
86 m_proxyModel(0)
87 {
88 hide();
89 setFocusPolicy(Qt::StrongFocus);
90 m_topLayout = new QVBoxLayout(this);
91 m_topLayout->setSpacing(0);
92 m_topLayout->setMargin(0);
93
94 connect(m_mainWindow, SIGNAL(activeViewChanged()),
95 this, SLOT(updateActivationState()));
96
97 QClipboard* clipboard = QApplication::clipboard();
98 connect(clipboard, SIGNAL(dataChanged()),
99 this, SLOT(updateCutItems()));
100
101 m_urlNavigator = new KUrlNavigator(DolphinSettings::instance().placesModel(), url, this);
102 m_urlNavigator->setUrlEditable(DolphinSettings::instance().generalSettings()->editableUrl());
103 m_urlNavigator->setHomeUrl(DolphinSettings::instance().generalSettings()->homeUrl());
104 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
105 this, SLOT(loadDirectory(const KUrl&)));
106 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&)),
107 this, SLOT(dropUrls(const KUrl::List&, const KUrl&)));
108 connect(m_urlNavigator, SIGNAL(activated()),
109 this, SLOT(requestActivation()));
110 connect(this, SIGNAL(contentsMoved(int, int)),
111 m_urlNavigator, SLOT(savePosition(int, int)));
112
113 m_statusBar = new DolphinStatusBar(this);
114
115 m_dirLister = new DolphinDirLister();
116 m_dirLister->setAutoUpdate(true);
117 m_dirLister->setMainWindow(this);
118 m_dirLister->setShowingDotFiles(showHiddenFiles);
119 m_dirLister->setDelayedMimeTypes(true);
120
121 connect(m_dirLister, SIGNAL(clear()),
122 this, SLOT(updateStatusBar()));
123 connect(m_dirLister, SIGNAL(percent(int)),
124 this, SLOT(updateProgress(int)));
125 connect(m_dirLister, SIGNAL(deleteItem(KFileItem*)),
126 this, SLOT(updateStatusBar()));
127 connect(m_dirLister, SIGNAL(completed()),
128 this, SLOT(updateItemCount()));
129 connect(m_dirLister, SIGNAL(completed()),
130 this, SLOT(updateCutItems()));
131 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
132 this, SLOT(generatePreviews(const KFileItemList&)));
133 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
134 this, SLOT(showInfoMessage(const QString&)));
135 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
136 this, SLOT(showErrorMessage(const QString&)));
137
138 m_dirModel = new KDirModel();
139 m_dirModel->setDirLister(m_dirLister);
140 m_dirModel->setDropsAllowed(KDirModel::DropOnDirectory);
141
142 m_proxyModel = new DolphinSortFilterProxyModel(this);
143 m_proxyModel->setSourceModel(m_dirModel);
144
145 m_controller = new DolphinController(this);
146 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
147 this, SLOT(openContextMenu(const QPoint&)));
148 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&, QWidget*)),
149 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&, QWidget*)));
150 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
151 this, SLOT(updateSorting(DolphinView::Sorting)));
152 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
153 this, SLOT(updateSortOrder(Qt::SortOrder)));
154 connect(m_controller, SIGNAL(itemTriggered(const QModelIndex&)),
155 this, SLOT(triggerItem(const QModelIndex&)));
156 connect(m_controller, SIGNAL(selectionChanged()),
157 this, SLOT(emitSelectionChangedSignal()));
158 connect(m_controller, SIGNAL(activated()),
159 this, SLOT(requestActivation()));
160
161 createView();
162
163 m_iconSize = K3Icon::SizeMedium;
164
165 m_filterBar = new FilterBar(this);
166 m_filterBar->hide();
167 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
168 this, SLOT(changeNameFilter(const QString&)));
169 connect(m_filterBar, SIGNAL(closeRequest()),
170 this, SLOT(closeFilterBar()));
171
172 m_topLayout->addWidget(m_urlNavigator);
173 m_topLayout->addWidget(itemView());
174 m_topLayout->addWidget(m_filterBar);
175 m_topLayout->addWidget(m_statusBar);
176 }
177
178 DolphinView::~DolphinView()
179 {
180 delete m_dirLister;
181 m_dirLister = 0;
182 }
183
184 void DolphinView::setUrl(const KUrl& url)
185 {
186 m_urlNavigator->setUrl(url);
187 m_controller->setUrl(url);
188 }
189
190 const KUrl& DolphinView::url() const
191 {
192 return m_urlNavigator->url();
193 }
194
195 bool DolphinView::isActive() const
196 {
197 return m_mainWindow->activeView() == this;
198 }
199
200 void DolphinView::setMode(Mode mode)
201 {
202 if (mode == m_mode) {
203 return; // the wished mode is already set
204 }
205
206 m_mode = mode;
207
208 ViewProperties props(m_urlNavigator->url());
209 props.setViewMode(m_mode);
210
211 createView();
212 startDirLister(m_urlNavigator->url());
213
214 emit modeChanged();
215 }
216
217 DolphinView::Mode DolphinView::mode() const
218 {
219 return m_mode;
220 }
221
222 void DolphinView::setShowPreview(bool show)
223 {
224 ViewProperties props(m_urlNavigator->url());
225 props.setShowPreview(show);
226
227 m_controller->setShowPreview(show);
228
229 emit showPreviewChanged();
230 reload();
231 }
232
233 bool DolphinView::showPreview() const
234 {
235 return m_controller->showPreview();
236 }
237
238 void DolphinView::setShowHiddenFiles(bool show)
239 {
240 if (m_dirLister->showingDotFiles() == show) {
241 return;
242 }
243
244 ViewProperties props(m_urlNavigator->url());
245 props.setShowHiddenFiles(show);
246 props.save();
247
248 m_dirLister->setShowingDotFiles(show);
249 emit showHiddenFilesChanged();
250
251 reload();
252 }
253
254 bool DolphinView::showHiddenFiles() const
255 {
256 return m_dirLister->showingDotFiles();
257 }
258
259 void DolphinView::setCategorizedSorting(bool categorized)
260 {
261 if (!supportsCategorizedSorting() || (categorized == categorizedSorting())) {
262 return;
263 }
264
265 Q_ASSERT(m_iconsView != 0);
266 if (categorized) {
267 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
268 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
269 } else {
270 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
271 m_iconsView->setItemCategorizer(0);
272 delete categorizer;
273 }
274
275 ViewProperties props(m_urlNavigator->url());
276 props.setCategorizedSorting(categorized);
277 props.save();
278
279 emit categorizedSortingChanged();
280 }
281
282 bool DolphinView::categorizedSorting() const
283 {
284 if (!supportsCategorizedSorting()) {
285 return false;
286 }
287
288 Q_ASSERT(m_iconsView != 0);
289 return m_iconsView->itemCategorizer() != 0;
290 }
291
292 bool DolphinView::supportsCategorizedSorting() const
293 {
294 return m_iconsView != 0;
295 }
296
297 void DolphinView::renameSelectedItems()
298 {
299 DolphinView* view = mainWindow()->activeView();
300 const KUrl::List urls = selectedUrls();
301 if (urls.count() > 1) {
302 // More than one item has been selected for renaming. Open
303 // a rename dialog and rename all items afterwards.
304 RenameDialog dialog(urls);
305 if (dialog.exec() == QDialog::Rejected) {
306 return;
307 }
308
309 const QString& newName = dialog.newName();
310 if (newName.isEmpty()) {
311 view->statusBar()->setMessage(dialog.errorString(),
312 DolphinStatusBar::Error);
313 } else {
314 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
315 // as one operation instead of n rename operations like it is done now...
316 Q_ASSERT(newName.contains('#'));
317
318 // iterate through all selected items and rename them...
319 const int replaceIndex = newName.indexOf('#');
320 Q_ASSERT(replaceIndex >= 0);
321 int index = 1;
322
323 KUrl::List::const_iterator it = urls.begin();
324 KUrl::List::const_iterator end = urls.end();
325 while (it != end) {
326 const KUrl& oldUrl = *it;
327 QString number;
328 number.setNum(index++);
329
330 QString name(newName);
331 name.replace(replaceIndex, 1, number);
332
333 if (oldUrl.fileName() != name) {
334 KUrl newUrl = oldUrl;
335 newUrl.setFileName(name);
336 m_mainWindow->rename(oldUrl, newUrl);
337 }
338 ++it;
339 }
340 }
341 } else {
342 // Only one item has been selected for renaming. Use the custom
343 // renaming mechanism from the views.
344 Q_ASSERT(urls.count() == 1);
345
346 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
347 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
348 // is a benefit for the user at all -> let's wait for some input first...
349 RenameDialog dialog(urls);
350 if (dialog.exec() == QDialog::Rejected) {
351 return;
352 }
353
354 const QString& newName = dialog.newName();
355 if (newName.isEmpty()) {
356 view->statusBar()->setMessage(dialog.errorString(),
357 DolphinStatusBar::Error);
358 } else {
359 const KUrl& oldUrl = urls.first();
360 KUrl newUrl = oldUrl;
361 newUrl.setFileName(newName);
362 m_mainWindow->rename(oldUrl, newUrl);
363 }
364 }
365 }
366
367 void DolphinView::selectAll()
368 {
369 selectAll(QItemSelectionModel::Select);
370 }
371
372 void DolphinView::invertSelection()
373 {
374 selectAll(QItemSelectionModel::Toggle);
375 }
376
377 DolphinStatusBar* DolphinView::statusBar() const
378 {
379 return m_statusBar;
380 }
381
382 int DolphinView::contentsX() const
383 {
384 return itemView()->horizontalScrollBar()->value();
385 }
386
387 int DolphinView::contentsY() const
388 {
389 return itemView()->verticalScrollBar()->value();
390 }
391
392 void DolphinView::emitRequestItemInfo(const KUrl& url)
393 {
394 emit requestItemInfo(url);
395 }
396
397 bool DolphinView::isFilterBarVisible() const
398 {
399 return m_filterBar->isVisible();
400 }
401
402 bool DolphinView::isUrlEditable() const
403 {
404 return m_urlNavigator->isUrlEditable();
405 }
406
407 void DolphinView::zoomIn()
408 {
409 m_controller->triggerZoomIn();
410 }
411
412 void DolphinView::zoomOut()
413 {
414 m_controller->triggerZoomOut();
415 }
416
417 bool DolphinView::isZoomInPossible() const
418 {
419 return m_controller->isZoomInPossible();
420 }
421
422 bool DolphinView::isZoomOutPossible() const
423 {
424 return m_controller->isZoomOutPossible();
425 }
426
427 void DolphinView::setSorting(Sorting sorting)
428 {
429 if (sorting != this->sorting()) {
430 updateSorting(sorting);
431 }
432 }
433
434 DolphinView::Sorting DolphinView::sorting() const
435 {
436 return m_proxyModel->sorting();
437 }
438
439 void DolphinView::setSortOrder(Qt::SortOrder order)
440 {
441 if (sortOrder() != order) {
442 updateSortOrder(order);
443 }
444 }
445
446 Qt::SortOrder DolphinView::sortOrder() const
447 {
448 return m_proxyModel->sortOrder();
449 }
450
451 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
452 {
453 ViewProperties props(m_urlNavigator->url());
454 props.setAdditionalInfo(info);
455
456 m_fileItemDelegate->setAdditionalInformation(info);
457
458 emit additionalInfoChanged(info);
459 reload();
460 }
461
462 KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
463 {
464 return m_fileItemDelegate->additionalInformation();
465 }
466
467 void DolphinView::goBack()
468 {
469 m_urlNavigator->goBack();
470 }
471
472 void DolphinView::goForward()
473 {
474 m_urlNavigator->goForward();
475 }
476
477 void DolphinView::goUp()
478 {
479 m_urlNavigator->goUp();
480 }
481
482 void DolphinView::goHome()
483 {
484 m_urlNavigator->goHome();
485 }
486
487 void DolphinView::setUrlEditable(bool editable)
488 {
489 m_urlNavigator->setUrlEditable(editable);
490 }
491
492 bool DolphinView::hasSelection() const
493 {
494 return itemView()->selectionModel()->hasSelection();
495 }
496
497 void DolphinView::clearSelection()
498 {
499 itemView()->selectionModel()->clear();
500 }
501
502 KFileItemList DolphinView::selectedItems() const
503 {
504 const QAbstractItemView* view = itemView();
505
506 // Our view has a selection, we will map them back to the DirModel
507 // and then fill the KFileItemList.
508 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
509
510 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
511 KFileItemList itemList;
512
513 const QModelIndexList indexList = selection.indexes();
514 QModelIndexList::const_iterator end = indexList.end();
515 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
516 Q_ASSERT((*it).isValid());
517
518 KFileItem* item = m_dirModel->itemForIndex(*it);
519 if (item != 0) {
520 itemList.append(item);
521 }
522 }
523
524 return itemList;
525 }
526
527 KUrl::List DolphinView::selectedUrls() const
528 {
529 KUrl::List urls;
530
531 const KFileItemList list = selectedItems();
532 KFileItemList::const_iterator it = list.begin();
533 const KFileItemList::const_iterator end = list.end();
534 while (it != end) {
535 KFileItem* item = *it;
536 urls.append(item->url());
537 ++it;
538 }
539
540 return urls;
541 }
542
543 KFileItem* DolphinView::fileItem(const QModelIndex index) const
544 {
545 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
546 return m_dirModel->itemForIndex(dirModelIndex);
547 }
548
549 void DolphinView::rename(const KUrl& source, const QString& newName)
550 {
551 bool ok = false;
552
553 if (newName.isEmpty() || (source.fileName() == newName)) {
554 return;
555 }
556
557 KUrl dest(source.upUrl());
558 dest.addPath(newName);
559
560 const bool destExists = KIO::NetAccess::exists(dest,
561 false,
562 mainWindow()->activeView());
563 if (destExists) {
564 // the destination already exists, hence ask the user
565 // how to proceed...
566 KIO::RenameDialog renameDialog(this,
567 i18n("File Already Exists"),
568 source.path(),
569 dest.path(),
570 KIO::M_OVERWRITE);
571 switch (renameDialog.exec()) {
572 case KIO::R_OVERWRITE:
573 // the destination should be overwritten
574 ok = KIO::NetAccess::file_move(source, dest, -1, true);
575 break;
576
577 case KIO::R_RENAME: {
578 // a new name for the destination has been used
579 KUrl newDest(renameDialog.newDestUrl());
580 ok = KIO::NetAccess::file_move(source, newDest);
581 break;
582 }
583
584 default:
585 // the renaming operation has been canceled
586 reload();
587 return;
588 }
589 } else {
590 // no destination exists, hence just move the file to
591 // do the renaming
592 ok = KIO::NetAccess::file_move(source, dest);
593 }
594
595 const QString destFileName = dest.fileName();
596 if (ok) {
597 m_statusBar->setMessage(i18n("Renamed file '%1' to '%2'.", source.fileName(), destFileName),
598 DolphinStatusBar::OperationCompleted);
599
600 KonqOperations::rename(this, source, destFileName);
601 } else {
602 m_statusBar->setMessage(i18n("Renaming of file '%1' to '%2' failed.", source.fileName(), destFileName),
603 DolphinStatusBar::Error);
604 reload();
605 }
606 }
607
608 void DolphinView::reload()
609 {
610 startDirLister(m_urlNavigator->url(), true);
611 }
612
613 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
614 {
615 QWidget::mouseReleaseEvent(event);
616 mainWindow()->setActiveView(this);
617 }
618
619 DolphinMainWindow* DolphinView::mainWindow() const
620 {
621 return m_mainWindow;
622 }
623
624 void DolphinView::loadDirectory(const KUrl& url)
625 {
626 if (!isActive()) {
627 requestActivation();
628 }
629
630 const ViewProperties props(url);
631
632 const Mode mode = props.viewMode();
633 bool changeMode = (m_mode != mode);
634 if (changeMode && isColumnViewActive()) {
635 // The column view is active. Only change the
636 // mode if the current URL is no child of the column view.
637 if (m_dirLister->url().isParentOf(url)) {
638 changeMode = false;
639 }
640 }
641
642 if (changeMode) {
643 m_mode = mode;
644 createView();
645 emit modeChanged();
646 }
647
648 const bool showHiddenFiles = props.showHiddenFiles();
649 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
650 m_dirLister->setShowingDotFiles(showHiddenFiles);
651 emit showHiddenFilesChanged();
652 }
653
654 const bool categorized = props.categorizedSorting();
655 if (categorized != categorizedSorting()) {
656 if (supportsCategorizedSorting()) {
657 Q_ASSERT(m_iconsView != 0);
658 if (categorized) {
659 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
660 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
661 } else {
662 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
663 m_iconsView->setItemCategorizer(0);
664 delete categorizer;
665 }
666 }
667 emit categorizedSortingChanged();
668 }
669
670 const DolphinView::Sorting sorting = props.sorting();
671 if (sorting != m_proxyModel->sorting()) {
672 m_proxyModel->setSorting(sorting);
673 emit sortingChanged(sorting);
674 }
675
676 const Qt::SortOrder sortOrder = props.sortOrder();
677 if (sortOrder != m_proxyModel->sortOrder()) {
678 m_proxyModel->setSortOrder(sortOrder);
679 emit sortOrderChanged(sortOrder);
680 }
681
682 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
683 if (info != m_fileItemDelegate->additionalInformation()) {
684 m_fileItemDelegate->setAdditionalInformation(info);
685
686 emit additionalInfoChanged(info);
687 }
688
689 const bool showPreview = props.showPreview();
690 if (showPreview != m_controller->showPreview()) {
691 m_controller->setShowPreview(showPreview);
692 emit showPreviewChanged();
693 }
694
695 startDirLister(url);
696 emit urlChanged(url);
697
698 m_statusBar->clear();
699 }
700
701 void DolphinView::triggerItem(const QModelIndex& index)
702 {
703 if (!isValidNameIndex(index)) {
704 return;
705 }
706
707 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
708 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
709 // items are selected by the user, hence don't trigger the
710 // item specified by 'index'
711 return;
712 }
713
714 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
715 if (item == 0) {
716 return;
717 }
718
719 // Prefer the local path over the URL. This assures that the
720 // volume space information is correct. Assuming that the URL is media:/sda1,
721 // and the local path is /windows/C: For the URL the space info is related
722 // to the root partition (and hence wrong) and for the local path the space
723 // info is related to the windows partition (-> correct).
724 const QString localPath(item->localPath());
725 KUrl url;
726 if (localPath.isEmpty()) {
727 url = item->url();
728 } else {
729 url = localPath;
730 }
731
732 if (item->isDir()) {
733 setUrl(url);
734 } else if (item->isFile()) {
735 // allow to browse through ZIP and tar files
736 KMimeType::Ptr mime = item->mimeTypePtr();
737 if (mime->is("application/zip")) {
738 url.setProtocol("zip");
739 setUrl(url);
740 } else if (mime->is("application/x-tar") ||
741 mime->is("application/x-tarz") ||
742 mime->is("application/x-bzip-compressed-tar") ||
743 mime->is("application/x-compressed-tar") ||
744 mime->is("application/x-tzo")) {
745 url.setProtocol("tar");
746 setUrl(url);
747 } else {
748 item->run();
749 }
750 } else {
751 item->run();
752 }
753 }
754
755 void DolphinView::updateProgress(int percent)
756 {
757 if (m_showProgress) {
758 m_statusBar->setProgress(percent);
759 }
760 }
761
762 void DolphinView::updateItemCount()
763 {
764 if (m_showProgress) {
765 m_statusBar->setProgressText(QString());
766 m_statusBar->setProgress(100);
767 m_showProgress = false;
768 }
769
770 KFileItemList items(m_dirLister->items());
771 KFileItemList::const_iterator it = items.begin();
772 const KFileItemList::const_iterator end = items.end();
773
774 m_fileCount = 0;
775 m_folderCount = 0;
776
777 while (it != end) {
778 KFileItem* item = *it;
779 if (item->isDir()) {
780 ++m_folderCount;
781 } else {
782 ++m_fileCount;
783 }
784 ++it;
785 }
786
787 updateStatusBar();
788
789 m_blockContentsMovedSignal = false;
790 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
791 }
792
793 void DolphinView::generatePreviews(const KFileItemList& items)
794 {
795 if (m_controller->showPreview()) {
796 KIO::PreviewJob* job = KIO::filePreview(items, 128);
797 connect(job, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)),
798 this, SLOT(showPreview(const KFileItem*, const QPixmap&)));
799 }
800 }
801
802 void DolphinView::showPreview(const KFileItem* item, const QPixmap& pixmap)
803 {
804 Q_ASSERT(item != 0);
805 const QModelIndex idx = m_dirModel->indexForItem(*item);
806 if (idx.isValid() && (idx.column() == 0)) {
807 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
808 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(*item)) {
809 KIconEffect iconEffect;
810 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
811 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
812 } else {
813 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
814 }
815 }
816 }
817
818 void DolphinView::restoreContentsPos()
819 {
820 KUrl currentUrl = m_urlNavigator->url();
821 if (!currentUrl.isEmpty()) {
822 QAbstractItemView* view = itemView();
823 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
824 QPoint pos = m_urlNavigator->savedPosition();
825 view->horizontalScrollBar()->setValue(pos.x());
826 view->verticalScrollBar()->setValue(pos.y());
827 }
828 }
829
830 void DolphinView::showInfoMessage(const QString& msg)
831 {
832 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
833 }
834
835 void DolphinView::showErrorMessage(const QString& msg)
836 {
837 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
838 }
839
840 void DolphinView::emitSelectionChangedSignal()
841 {
842 emit selectionChanged(DolphinView::selectedItems());
843 }
844
845 void DolphinView::closeFilterBar()
846 {
847 m_filterBar->hide();
848 emit showFilterBarChanged(false);
849 }
850
851 void DolphinView::startDirLister(const KUrl& url, bool reload)
852 {
853 if (!url.isValid()) {
854 const QString location(url.pathOrUrl());
855 if (location.isEmpty()) {
856 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
857 } else {
858 m_statusBar->setMessage(i18n("The location '%1' is invalid.", location),
859 DolphinStatusBar::Error);
860 }
861 return;
862 }
863
864 // Only show the directory loading progress if the status bar does
865 // not contain another progress information. This means that
866 // the directory loading progress information has the lowest priority.
867 const QString progressText(m_statusBar->progressText());
868 m_showProgress = progressText.isEmpty() ||
869 (progressText == i18n("Loading directory..."));
870 if (m_showProgress) {
871 m_statusBar->setProgressText(i18n("Loading directory..."));
872 m_statusBar->setProgress(0);
873 }
874
875 m_cutItemsCache.clear();
876 m_blockContentsMovedSignal = true;
877 m_dirLister->stop();
878
879 bool openDir = true;
880 bool keepOldDirs = isColumnViewActive();
881 if (keepOldDirs) {
882 if (reload) {
883 keepOldDirs = false;
884
885 const KUrl& dirListerUrl = m_dirLister->url();
886 if (dirListerUrl.isValid()) {
887 const KUrl::List dirs = m_dirLister->directories();
888 KUrl url;
889 foreach(url, dirs) {
890 m_dirLister->updateDirectory(url);
891 }
892 openDir = false;
893 }
894 } else if (m_dirLister->directories().contains(url)) {
895 // The dir lister contains the directory already, so
896 // KDirLister::openUrl() may not been invoked twice.
897 m_dirLister->updateDirectory(url);
898 openDir = false;
899 } else {
900 const KUrl& dirListerUrl = m_dirLister->url();
901 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
902 // The current URL is not a child of the dir lister
903 // URL. This may happen when e. g. a bookmark has been selected
904 // and hence the view must be reset.
905 keepOldDirs = false;
906 }
907 }
908 }
909
910 if (openDir) {
911 m_dirLister->openUrl(url, keepOldDirs, reload);
912 }
913 }
914
915 QString DolphinView::defaultStatusBarText() const
916 {
917 return KIO::itemsSummaryString(m_fileCount + m_folderCount,
918 m_fileCount,
919 m_folderCount,
920 0, false);
921 }
922
923 QString DolphinView::selectionStatusBarText() const
924 {
925 QString text;
926 const KFileItemList list = selectedItems();
927 if (list.isEmpty()) {
928 // when an item is triggered, it is temporary selected but selectedItems()
929 // will return an empty list
930 return QString();
931 }
932
933 int fileCount = 0;
934 int folderCount = 0;
935 KIO::filesize_t byteSize = 0;
936 KFileItemList::const_iterator it = list.begin();
937 const KFileItemList::const_iterator end = list.end();
938 while (it != end) {
939 KFileItem* item = *it;
940 if (item->isDir()) {
941 ++folderCount;
942 } else {
943 ++fileCount;
944 byteSize += item->size();
945 }
946 ++it;
947 }
948
949 if (folderCount > 0) {
950 text = i18np("1 Folder selected", "%1 Folders selected", folderCount);
951 if (fileCount > 0) {
952 text += ", ";
953 }
954 }
955
956 if (fileCount > 0) {
957 const QString sizeText(KIO::convertSize(byteSize));
958 text += i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount, sizeText);
959 }
960
961 return text;
962 }
963
964 void DolphinView::showFilterBar(bool show)
965 {
966 Q_ASSERT(m_filterBar != 0);
967 if (show) {
968 m_filterBar->show();
969 } else {
970 m_filterBar->hide();
971 }
972 }
973
974 void DolphinView::updateStatusBar()
975 {
976 // As the item count information is less important
977 // in comparison with other messages, it should only
978 // be shown if:
979 // - the status bar is empty or
980 // - shows already the item count information or
981 // - shows only a not very important information
982 // - if any progress is given don't show the item count info at all
983 const QString msg(m_statusBar->message());
984 const bool updateStatusBarMsg = (msg.isEmpty() ||
985 (msg == m_statusBar->defaultText()) ||
986 (m_statusBar->type() == DolphinStatusBar::Information)) &&
987 (m_statusBar->progress() == 100);
988
989 const QString text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
990 m_statusBar->setDefaultText(text);
991
992 if (updateStatusBarMsg) {
993 m_statusBar->setMessage(text, DolphinStatusBar::Default);
994 }
995 }
996
997 void DolphinView::requestActivation()
998 {
999 m_mainWindow->setActiveView(this);
1000 }
1001
1002 void DolphinView::changeSelection(const KFileItemList& selection)
1003 {
1004 clearSelection();
1005 if (selection.isEmpty()) {
1006 return;
1007 }
1008 KUrl baseUrl = url();
1009 KUrl url;
1010 QItemSelection new_selection;
1011 foreach(KFileItem* item, selection) {
1012 url = item->url().upUrl();
1013 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
1014 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(*item));
1015 new_selection.select(index, index);
1016 }
1017 }
1018 itemView()->selectionModel()->select(new_selection,
1019 QItemSelectionModel::ClearAndSelect
1020 | QItemSelectionModel::Current);
1021 }
1022
1023 void DolphinView::changeNameFilter(const QString& nameFilter)
1024 {
1025 // The name filter of KDirLister does a 'hard' filtering, which
1026 // means that only the items are shown where the names match
1027 // exactly the filter. This is non-transparent for the user, which
1028 // just wants to have a 'soft' filtering: does the name contain
1029 // the filter string?
1030 QString adjustedFilter(nameFilter);
1031 adjustedFilter.insert(0, '*');
1032 adjustedFilter.append('*');
1033
1034 // Use the ProxyModel to filter:
1035 // This code is #ifdefed as setNameFilter behaves
1036 // slightly different than the QSortFilterProxyModel
1037 // as it will not remove directories. I will ask
1038 // our beloved usability experts for input
1039 // -- z.
1040 #if 0
1041 m_dirLister->setNameFilter(adjustedFilter);
1042 m_dirLister->emitChanges();
1043 #else
1044 m_proxyModel->setFilterRegExp(nameFilter);
1045 #endif
1046 }
1047
1048 void DolphinView::openContextMenu(const QPoint& pos)
1049 {
1050 KFileItem* item = 0;
1051
1052 const QModelIndex index = itemView()->indexAt(pos);
1053 if (isValidNameIndex(index)) {
1054 item = fileItem(index);
1055 }
1056
1057 DolphinContextMenu contextMenu(m_mainWindow, item, url());
1058 contextMenu.open();
1059 }
1060
1061 void DolphinView::dropUrls(const KUrl::List& urls,
1062 const QModelIndex& index,
1063 QWidget* source)
1064 {
1065 KFileItem* directory = 0;
1066 if (isValidNameIndex(index)) {
1067 KFileItem* item = fileItem(index);
1068 Q_ASSERT(item != 0);
1069 if (item->isDir()) {
1070 // the URLs are dropped above a directory
1071 directory = item;
1072 }
1073 }
1074
1075 if ((directory == 0) && (source == itemView())) {
1076 // The dropping is done into the same viewport where
1077 // the dragging has been started. Just ignore this...
1078 return;
1079 }
1080
1081 const KUrl& destination = (directory == 0) ? url() :
1082 directory->url();
1083 dropUrls(urls, destination);
1084 }
1085
1086 void DolphinView::dropUrls(const KUrl::List& urls,
1087 const KUrl& destination)
1088 {
1089 m_mainWindow->dropUrls(urls, destination);
1090 }
1091
1092 void DolphinView::updateSorting(DolphinView::Sorting sorting)
1093 {
1094 ViewProperties props(url());
1095 props.setSorting(sorting);
1096
1097 m_proxyModel->setSorting(sorting);
1098
1099 emit sortingChanged(sorting);
1100 }
1101
1102 void DolphinView::updateSortOrder(Qt::SortOrder order)
1103 {
1104 ViewProperties props(url());
1105 props.setSortOrder(order);
1106
1107 m_proxyModel->setSortOrder(order);
1108
1109 emit sortOrderChanged(order);
1110 }
1111
1112 void DolphinView::emitContentsMoved()
1113 {
1114 if (!m_blockContentsMovedSignal) {
1115 emit contentsMoved(contentsX(), contentsY());
1116 }
1117 }
1118
1119 void DolphinView::updateActivationState()
1120 {
1121 m_urlNavigator->setActive(isActive());
1122 if (isActive()) {
1123 emit urlChanged(url());
1124 emit selectionChanged(selectedItems());
1125 }
1126 }
1127
1128 void DolphinView::updateCutItems()
1129 {
1130 // restore the icons of all previously selected items to the
1131 // original state...
1132 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
1133 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
1134 while (it != end) {
1135 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
1136 if (index.isValid()) {
1137 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
1138 }
1139 ++it;
1140 }
1141 m_cutItemsCache.clear();
1142
1143 // ... and apply an item effect to all currently cut items
1144 applyCutItemEffect();
1145 }
1146
1147 void DolphinView::createView()
1148 {
1149 // delete current view
1150 QAbstractItemView* view = itemView();
1151 if (view != 0) {
1152 m_topLayout->removeWidget(view);
1153 view->close();
1154 if (view == m_iconsView) {
1155 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
1156 m_iconsView->setItemCategorizer(0);
1157 delete categorizer;
1158 }
1159 view->deleteLater();
1160 view = 0;
1161 m_iconsView = 0;
1162 m_detailsView = 0;
1163 m_columnView = 0;
1164 m_fileItemDelegate = 0;
1165 }
1166
1167 Q_ASSERT(m_iconsView == 0);
1168 Q_ASSERT(m_detailsView == 0);
1169 Q_ASSERT(m_columnView == 0);
1170
1171 // ... and recreate it representing the current mode
1172 switch (m_mode) {
1173 case IconsView:
1174 m_iconsView = new DolphinIconsView(this, m_controller);
1175 view = m_iconsView;
1176 break;
1177
1178 case DetailsView:
1179 m_detailsView = new DolphinDetailsView(this, m_controller);
1180 view = m_detailsView;
1181 break;
1182
1183 case ColumnView:
1184 m_columnView = new DolphinColumnView(this, m_controller);
1185 view = m_columnView;
1186 break;
1187 }
1188
1189 Q_ASSERT(view != 0);
1190
1191 m_fileItemDelegate = new KFileItemDelegate(view);
1192 view->setItemDelegate(m_fileItemDelegate);
1193
1194 view->setModel(m_proxyModel);
1195 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1196
1197 new KMimeTypeResolver(view, m_dirModel);
1198 m_topLayout->insertWidget(1, view);
1199
1200 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1201 m_controller, SLOT(indicateSelectionChange()));
1202 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1203 this, SLOT(emitContentsMoved()));
1204 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1205 this, SLOT(emitContentsMoved()));
1206 }
1207
1208 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
1209 {
1210 QItemSelectionModel* selectionModel = itemView()->selectionModel();
1211 const QAbstractItemModel* itemModel = selectionModel->model();
1212
1213 const QModelIndex topLeft = itemModel->index(0, 0);
1214 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
1215 itemModel->columnCount() - 1);
1216
1217 QItemSelection selection(topLeft, bottomRight);
1218 selectionModel->select(selection, flags);
1219 }
1220
1221 QAbstractItemView* DolphinView::itemView() const
1222 {
1223 if (m_detailsView != 0) {
1224 return m_detailsView;
1225 } else if (m_columnView != 0) {
1226 return m_columnView;
1227 }
1228
1229 return m_iconsView;
1230 }
1231
1232 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
1233 {
1234 return index.isValid() && (index.column() == KDirModel::Name);
1235 }
1236
1237 bool DolphinView::isCutItem(const KFileItem& item) const
1238 {
1239 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1240 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1241
1242 const KUrl& itemUrl = item.url();
1243 KUrl::List::const_iterator it = cutUrls.begin();
1244 const KUrl::List::const_iterator end = cutUrls.end();
1245 while (it != end) {
1246 if (*it == itemUrl) {
1247 return true;
1248 }
1249 ++it;
1250 }
1251
1252 return false;
1253 }
1254
1255 void DolphinView::applyCutItemEffect()
1256 {
1257 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1258 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
1259 return;
1260 }
1261
1262 KFileItemList items(m_dirLister->items());
1263 KFileItemList::const_iterator it = items.begin();
1264 const KFileItemList::const_iterator end = items.end();
1265 while (it != end) {
1266 KFileItem* item = *it;
1267 if (isCutItem(*item)) {
1268 const QModelIndex index = m_dirModel->indexForItem(*item);
1269 const KFileItem* item = m_dirModel->itemForIndex(index);
1270 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
1271 if ((value.type() == QVariant::Icon) && (item != 0)) {
1272 const QIcon icon(qvariant_cast<QIcon>(value));
1273 QPixmap pixmap = icon.pixmap(128, 128);
1274
1275 // remember current pixmap for the item to be able
1276 // to restore it when other items get cut
1277 CutItem cutItem;
1278 cutItem.url = item->url();
1279 cutItem.pixmap = pixmap;
1280 m_cutItemsCache.append(cutItem);
1281
1282 // apply icon effect to the cut item
1283 KIconEffect iconEffect;
1284 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
1285 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
1286 }
1287 }
1288 ++it;
1289 }
1290 }
1291
1292 #include "dolphinview.moc"