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