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