]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
- allow to configure that the filter bar should be shown after starting Dolphin ...
[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 void DolphinView::emitRequestItemInfo(const KUrl& url)
413 {
414 emit requestItemInfo(url);
415 }
416
417 bool DolphinView::isFilterBarVisible() const
418 {
419 return m_filterBar->isVisible();
420 }
421
422 bool DolphinView::isUrlEditable() const
423 {
424 return m_urlNavigator->isUrlEditable();
425 }
426
427 void DolphinView::zoomIn()
428 {
429 m_controller->triggerZoomIn();
430 }
431
432 void DolphinView::zoomOut()
433 {
434 m_controller->triggerZoomOut();
435 }
436
437 bool DolphinView::isZoomInPossible() const
438 {
439 return m_controller->isZoomInPossible();
440 }
441
442 bool DolphinView::isZoomOutPossible() const
443 {
444 return m_controller->isZoomOutPossible();
445 }
446
447 void DolphinView::setSorting(Sorting sorting)
448 {
449 if (sorting != this->sorting()) {
450 updateSorting(sorting);
451 }
452 }
453
454 DolphinView::Sorting DolphinView::sorting() const
455 {
456 return m_proxyModel->sorting();
457 }
458
459 void DolphinView::setSortOrder(Qt::SortOrder order)
460 {
461 if (sortOrder() != order) {
462 updateSortOrder(order);
463 }
464 }
465
466 Qt::SortOrder DolphinView::sortOrder() const
467 {
468 return m_proxyModel->sortOrder();
469 }
470
471 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
472 {
473 ViewProperties props(m_urlNavigator->url());
474 props.setAdditionalInfo(info);
475
476 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
477 m_fileItemDelegate->setAdditionalInformation(info);
478
479 emit additionalInfoChanged(info);
480 startDirLister(m_urlNavigator->url(), true);
481 }
482
483 KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
484 {
485 return m_fileItemDelegate->additionalInformation();
486 }
487
488 void DolphinView::goBack()
489 {
490 m_urlNavigator->goBack();
491 }
492
493 void DolphinView::goForward()
494 {
495 m_urlNavigator->goForward();
496 }
497
498 void DolphinView::goUp()
499 {
500 m_urlNavigator->goUp();
501 }
502
503 void DolphinView::goHome()
504 {
505 m_urlNavigator->goHome();
506 }
507
508 void DolphinView::setUrlEditable(bool editable)
509 {
510 m_urlNavigator->setUrlEditable(editable);
511 }
512
513 bool DolphinView::hasSelection() const
514 {
515 return itemView()->selectionModel()->hasSelection();
516 }
517
518 void DolphinView::clearSelection()
519 {
520 itemView()->selectionModel()->clear();
521 }
522
523 KFileItemList DolphinView::selectedItems() const
524 {
525 const QAbstractItemView* view = itemView();
526
527 // Our view has a selection, we will map them back to the DirModel
528 // and then fill the KFileItemList.
529 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
530
531 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
532 KFileItemList itemList;
533
534 const QModelIndexList indexList = selection.indexes();
535 QModelIndexList::const_iterator end = indexList.end();
536 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
537 Q_ASSERT((*it).isValid());
538
539 KFileItem* item = m_dirModel->itemForIndex(*it);
540 if (item != 0) {
541 itemList.append(item);
542 }
543 }
544
545 return itemList;
546 }
547
548 KUrl::List DolphinView::selectedUrls() const
549 {
550 KUrl::List urls;
551
552 const KFileItemList list = selectedItems();
553 KFileItemList::const_iterator it = list.begin();
554 const KFileItemList::const_iterator end = list.end();
555 while (it != end) {
556 KFileItem* item = *it;
557 urls.append(item->url());
558 ++it;
559 }
560
561 return urls;
562 }
563
564 KFileItem* DolphinView::fileItem(const QModelIndex index) const
565 {
566 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
567 return m_dirModel->itemForIndex(dirModelIndex);
568 }
569
570 void DolphinView::rename(const KUrl& source, const QString& newName)
571 {
572 bool ok = false;
573
574 if (newName.isEmpty() || (source.fileName() == newName)) {
575 return;
576 }
577
578 KUrl dest(source.upUrl());
579 dest.addPath(newName);
580
581 const bool destExists = KIO::NetAccess::exists(dest,
582 false,
583 mainWindow()->activeView());
584 if (destExists) {
585 // the destination already exists, hence ask the user
586 // how to proceed...
587 KIO::RenameDialog renameDialog(this,
588 i18n("File Already Exists"),
589 source.path(),
590 dest.path(),
591 KIO::M_OVERWRITE);
592 switch (renameDialog.exec()) {
593 case KIO::R_OVERWRITE:
594 // the destination should be overwritten
595 ok = KIO::NetAccess::file_move(source, dest, -1, true);
596 break;
597
598 case KIO::R_RENAME: {
599 // a new name for the destination has been used
600 KUrl newDest(renameDialog.newDestUrl());
601 ok = KIO::NetAccess::file_move(source, newDest);
602 break;
603 }
604
605 default:
606 // the renaming operation has been canceled
607 return;
608 }
609 } else {
610 // no destination exists, hence just move the file to
611 // do the renaming
612 ok = KIO::NetAccess::file_move(source, dest);
613 }
614
615 const QString destFileName = dest.fileName();
616 if (ok) {
617 m_statusBar->setMessage(i18n("Renamed file '%1' to '%2'.", source.fileName(), destFileName),
618 DolphinStatusBar::OperationCompleted);
619
620 KonqOperations::rename(this, source, destFileName);
621 } else {
622 m_statusBar->setMessage(i18n("Renaming of file '%1' to '%2' failed.", source.fileName(), destFileName),
623 DolphinStatusBar::Error);
624 }
625 }
626
627 void DolphinView::reload()
628 {
629 const KUrl& url = m_urlNavigator->url();
630 changeDirectory(url);
631 startDirLister(url, true);
632 }
633
634 void DolphinView::refresh()
635 {
636 createView();
637 reload();
638 }
639
640 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
641 {
642 QWidget::mouseReleaseEvent(event);
643 mainWindow()->setActiveView(this);
644 }
645
646 DolphinMainWindow* DolphinView::mainWindow() const
647 {
648 return m_mainWindow;
649 }
650
651 void DolphinView::changeDirectory(const KUrl& url)
652 {
653 if (!isActive()) {
654 requestActivation();
655 }
656
657 const ViewProperties props(url);
658
659 const Mode mode = props.viewMode();
660 bool changeMode = (m_mode != mode);
661 if (changeMode && isColumnViewActive()) {
662 // The column view is active. Only change the
663 // mode if the current URL is no child of the column view.
664 if (m_dirLister->url().isParentOf(url)) {
665 changeMode = false;
666 }
667 }
668
669 if (changeMode) {
670 m_mode = mode;
671 createView();
672 emit modeChanged();
673
674 if (m_mode == ColumnView) {
675 // The mode has been changed to the Column View. When starting the dir
676 // lister with DolphinView::startDirLister() it is important to give a
677 // hint that the dir lister may not keep the current directory
678 // although this is the default for showing a hierarchy.
679 m_initializeColumnView = true;
680 }
681 }
682
683 const bool showHiddenFiles = props.showHiddenFiles();
684 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
685 m_dirLister->setShowingDotFiles(showHiddenFiles);
686 emit showHiddenFilesChanged();
687 }
688
689 const bool categorized = props.categorizedSorting();
690 if (categorized != categorizedSorting()) {
691 if (supportsCategorizedSorting()) {
692 Q_ASSERT(m_iconsView != 0);
693 if (categorized) {
694 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
695 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
696 } else {
697 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
698 m_iconsView->setItemCategorizer(0);
699 delete categorizer;
700 }
701 }
702 emit categorizedSortingChanged();
703 }
704
705 const DolphinView::Sorting sorting = props.sorting();
706 if (sorting != m_proxyModel->sorting()) {
707 m_proxyModel->setSorting(sorting);
708 emit sortingChanged(sorting);
709 }
710
711 const Qt::SortOrder sortOrder = props.sortOrder();
712 if (sortOrder != m_proxyModel->sortOrder()) {
713 m_proxyModel->setSortOrder(sortOrder);
714 emit sortOrderChanged(sortOrder);
715 }
716
717 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
718 if (info != m_fileItemDelegate->additionalInformation()) {
719 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
720 m_fileItemDelegate->setAdditionalInformation(info);
721 emit additionalInfoChanged(info);
722 }
723
724 const bool showPreview = props.showPreview();
725 if (showPreview != m_controller->showPreview()) {
726 m_controller->setShowPreview(showPreview);
727 emit showPreviewChanged();
728 }
729
730 startDirLister(url);
731 emit urlChanged(url);
732
733 m_statusBar->clear();
734 }
735
736 void DolphinView::triggerItem(const QModelIndex& index)
737 {
738 if (!isValidNameIndex(index)) {
739 return;
740 }
741
742 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
743 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
744 // items are selected by the user, hence don't trigger the
745 // item specified by 'index'
746 return;
747 }
748
749 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
750 if (item == 0) {
751 return;
752 }
753
754 // Prefer the local path over the URL. This assures that the
755 // volume space information is correct. Assuming that the URL is media:/sda1,
756 // and the local path is /windows/C: For the URL the space info is related
757 // to the root partition (and hence wrong) and for the local path the space
758 // info is related to the windows partition (-> correct).
759 const QString localPath(item->localPath());
760 KUrl url;
761 if (localPath.isEmpty()) {
762 url = item->url();
763 } else {
764 url = localPath;
765 }
766
767 if (item->isDir()) {
768 setUrl(url);
769 } else if (item->isFile()) {
770 // allow to browse through ZIP and tar files
771 KMimeType::Ptr mime = item->mimeTypePtr();
772 if (mime->is("application/zip")) {
773 url.setProtocol("zip");
774 setUrl(url);
775 } else if (mime->is("application/x-tar") ||
776 mime->is("application/x-tarz") ||
777 mime->is("application/x-bzip-compressed-tar") ||
778 mime->is("application/x-compressed-tar") ||
779 mime->is("application/x-tzo")) {
780 url.setProtocol("tar");
781 setUrl(url);
782 } else {
783 item->run();
784 }
785 } else {
786 item->run();
787 }
788 }
789
790 void DolphinView::updateProgress(int percent)
791 {
792 if (m_showProgress) {
793 m_statusBar->setProgress(percent);
794 }
795 }
796
797 void DolphinView::updateItemCount()
798 {
799 if (m_showProgress) {
800 m_statusBar->setProgressText(QString());
801 m_statusBar->setProgress(100);
802 m_showProgress = false;
803 }
804
805 KFileItemList items(m_dirLister->items());
806 KFileItemList::const_iterator it = items.begin();
807 const KFileItemList::const_iterator end = items.end();
808
809 m_fileCount = 0;
810 m_folderCount = 0;
811
812 while (it != end) {
813 KFileItem* item = *it;
814 if (item->isDir()) {
815 ++m_folderCount;
816 } else {
817 ++m_fileCount;
818 }
819 ++it;
820 }
821
822 updateStatusBar();
823
824 m_blockContentsMovedSignal = false;
825 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
826 }
827
828 void DolphinView::generatePreviews(const KFileItemList& items)
829 {
830 if (m_controller->showPreview()) {
831
832 // Must turn QList<KFileItem *> to QList<KFileItem>...
833 QList<KFileItem> itemsToPreview;
834 foreach( KFileItem* it, items )
835 itemsToPreview.append( *it );
836
837 KIO::PreviewJob* job = KIO::filePreview(itemsToPreview, 128);
838 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
839 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
840 }
841 }
842
843 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
844 {
845 Q_ASSERT(!item.isNull());
846 if (item.url().directory() != m_dirLister->url().path()) {
847 // the preview job is still working on items of an older URL, hence
848 // the item is not part of the directory model anymore
849 return;
850 }
851
852 const QModelIndex idx = m_dirModel->indexForItem(item);
853 if (idx.isValid() && (idx.column() == 0)) {
854 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
855 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
856 KIconEffect iconEffect;
857 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
858 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
859 } else {
860 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
861 }
862 }
863 }
864
865 void DolphinView::restoreContentsPos()
866 {
867 KUrl currentUrl = m_urlNavigator->url();
868 if (!currentUrl.isEmpty()) {
869 QAbstractItemView* view = itemView();
870 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
871 QPoint pos = m_urlNavigator->savedPosition();
872 view->horizontalScrollBar()->setValue(pos.x());
873 view->verticalScrollBar()->setValue(pos.y());
874 }
875 }
876
877 void DolphinView::showInfoMessage(const QString& msg)
878 {
879 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
880 }
881
882 void DolphinView::showErrorMessage(const QString& msg)
883 {
884 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
885 }
886
887 void DolphinView::emitSelectionChangedSignal()
888 {
889 emit selectionChanged(DolphinView::selectedItems());
890 }
891
892 void DolphinView::closeFilterBar()
893 {
894 m_filterBar->hide();
895 emit showFilterBarChanged(false);
896 }
897
898 void DolphinView::startDirLister(const KUrl& url, bool reload)
899 {
900 if (!url.isValid()) {
901 const QString location(url.pathOrUrl());
902 if (location.isEmpty()) {
903 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
904 } else {
905 m_statusBar->setMessage(i18n("The location '%1' is invalid.", location),
906 DolphinStatusBar::Error);
907 }
908 return;
909 }
910
911 // Only show the directory loading progress if the status bar does
912 // not contain another progress information. This means that
913 // the directory loading progress information has the lowest priority.
914 const QString progressText(m_statusBar->progressText());
915 m_showProgress = progressText.isEmpty() ||
916 (progressText == i18n("Loading folder..."));
917 if (m_showProgress) {
918 m_statusBar->setProgressText(i18n("Loading folder..."));
919 m_statusBar->setProgress(0);
920 }
921
922 m_cutItemsCache.clear();
923 m_blockContentsMovedSignal = true;
924 m_dirLister->stop();
925
926 bool openDir = true;
927 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
928 m_initializeColumnView = false;
929
930 if (keepOldDirs) {
931 if (reload) {
932 keepOldDirs = false;
933
934 const KUrl& dirListerUrl = m_dirLister->url();
935 if (dirListerUrl.isValid()) {
936 const KUrl::List dirs = m_dirLister->directories();
937 KUrl url;
938 foreach(url, dirs) {
939 m_dirLister->updateDirectory(url);
940 }
941 openDir = false;
942 }
943 } else if (m_dirLister->directories().contains(url)) {
944 // The dir lister contains the directory already, so
945 // KDirLister::openUrl() may not been invoked twice.
946 m_dirLister->updateDirectory(url);
947 openDir = false;
948 } else {
949 const KUrl& dirListerUrl = m_dirLister->url();
950 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
951 // The current URL is not a child of the dir lister
952 // URL. This may happen when e. g. a bookmark has been selected
953 // and hence the view must be reset.
954 keepOldDirs = false;
955 }
956 }
957 }
958
959 if (openDir) {
960 m_dirLister->openUrl(url, keepOldDirs, reload);
961 }
962 }
963
964 QString DolphinView::defaultStatusBarText() const
965 {
966 return KIO::itemsSummaryString(m_fileCount + m_folderCount,
967 m_fileCount,
968 m_folderCount,
969 0, false);
970 }
971
972 QString DolphinView::selectionStatusBarText() const
973 {
974 QString text;
975 const KFileItemList list = selectedItems();
976 if (list.isEmpty()) {
977 // when an item is triggered, it is temporary selected but selectedItems()
978 // will return an empty list
979 return QString();
980 }
981
982 int fileCount = 0;
983 int folderCount = 0;
984 KIO::filesize_t byteSize = 0;
985 KFileItemList::const_iterator it = list.begin();
986 const KFileItemList::const_iterator end = list.end();
987 while (it != end) {
988 KFileItem* item = *it;
989 if (item->isDir()) {
990 ++folderCount;
991 } else {
992 ++fileCount;
993 byteSize += item->size();
994 }
995 ++it;
996 }
997
998 if (folderCount > 0) {
999 text = i18np("1 Folder selected", "%1 Folders selected", folderCount);
1000 if (fileCount > 0) {
1001 text += ", ";
1002 }
1003 }
1004
1005 if (fileCount > 0) {
1006 const QString sizeText(KIO::convertSize(byteSize));
1007 text += i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount, sizeText);
1008 }
1009
1010 return text;
1011 }
1012
1013 void DolphinView::showFilterBar(bool show)
1014 {
1015 Q_ASSERT(m_filterBar != 0);
1016 if (show) {
1017 m_filterBar->show();
1018 } else {
1019 m_filterBar->hide();
1020 }
1021 }
1022
1023 void DolphinView::updateStatusBar()
1024 {
1025 // As the item count information is less important
1026 // in comparison with other messages, it should only
1027 // be shown if:
1028 // - the status bar is empty or
1029 // - shows already the item count information or
1030 // - shows only a not very important information
1031 // - if any progress is given don't show the item count info at all
1032 const QString msg(m_statusBar->message());
1033 const bool updateStatusBarMsg = (msg.isEmpty() ||
1034 (msg == m_statusBar->defaultText()) ||
1035 (m_statusBar->type() == DolphinStatusBar::Information)) &&
1036 (m_statusBar->progress() == 100);
1037
1038 const QString text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
1039 m_statusBar->setDefaultText(text);
1040
1041 if (updateStatusBarMsg) {
1042 m_statusBar->setMessage(text, DolphinStatusBar::Default);
1043 }
1044 }
1045
1046 void DolphinView::requestActivation()
1047 {
1048 m_mainWindow->setActiveView(this);
1049 }
1050
1051 void DolphinView::changeSelection(const KFileItemList& selection)
1052 {
1053 clearSelection();
1054 if (selection.isEmpty()) {
1055 return;
1056 }
1057 KUrl baseUrl = url();
1058 KUrl url;
1059 QItemSelection new_selection;
1060 foreach(KFileItem* item, selection) {
1061 url = item->url().upUrl();
1062 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
1063 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(*item));
1064 new_selection.select(index, index);
1065 }
1066 }
1067 itemView()->selectionModel()->select(new_selection,
1068 QItemSelectionModel::ClearAndSelect
1069 | QItemSelectionModel::Current);
1070 }
1071
1072 void DolphinView::changeNameFilter(const QString& nameFilter)
1073 {
1074 // The name filter of KDirLister does a 'hard' filtering, which
1075 // means that only the items are shown where the names match
1076 // exactly the filter. This is non-transparent for the user, which
1077 // just wants to have a 'soft' filtering: does the name contain
1078 // the filter string?
1079 QString adjustedFilter(nameFilter);
1080 adjustedFilter.insert(0, '*');
1081 adjustedFilter.append('*');
1082
1083 // Use the ProxyModel to filter:
1084 // This code is #ifdefed as setNameFilter behaves
1085 // slightly different than the QSortFilterProxyModel
1086 // as it will not remove directories. I will ask
1087 // our beloved usability experts for input
1088 // -- z.
1089 #if 0
1090 m_dirLister->setNameFilter(adjustedFilter);
1091 m_dirLister->emitChanges();
1092 #else
1093 m_proxyModel->setFilterRegExp(nameFilter);
1094 #endif
1095 }
1096
1097 void DolphinView::openContextMenu(const QPoint& pos)
1098 {
1099 KFileItem* item = 0;
1100
1101 const QModelIndex index = itemView()->indexAt(pos);
1102 if (isValidNameIndex(index)) {
1103 item = fileItem(index);
1104 }
1105
1106 DolphinContextMenu contextMenu(m_mainWindow, item, url());
1107 contextMenu.open();
1108 }
1109
1110 void DolphinView::dropUrls(const KUrl::List& urls,
1111 const QModelIndex& index,
1112 QWidget* source)
1113 {
1114 KFileItem* directory = 0;
1115 if (isValidNameIndex(index)) {
1116 KFileItem* item = fileItem(index);
1117 Q_ASSERT(item != 0);
1118 if (item->isDir()) {
1119 // the URLs are dropped above a directory
1120 directory = item;
1121 }
1122 }
1123
1124 if ((directory == 0) && (source == itemView())) {
1125 // The dropping is done into the same viewport where
1126 // the dragging has been started. Just ignore this...
1127 return;
1128 }
1129
1130 const KUrl& destination = (directory == 0) ? url() :
1131 directory->url();
1132 dropUrls(urls, destination);
1133 }
1134
1135 void DolphinView::dropUrls(const KUrl::List& urls,
1136 const KUrl& destination)
1137 {
1138 m_mainWindow->dropUrls(urls, destination);
1139 }
1140
1141 void DolphinView::updateSorting(DolphinView::Sorting sorting)
1142 {
1143 ViewProperties props(url());
1144 props.setSorting(sorting);
1145
1146 m_proxyModel->setSorting(sorting);
1147
1148 emit sortingChanged(sorting);
1149 }
1150
1151 void DolphinView::updateSortOrder(Qt::SortOrder order)
1152 {
1153 ViewProperties props(url());
1154 props.setSortOrder(order);
1155
1156 m_proxyModel->setSortOrder(order);
1157
1158 emit sortOrderChanged(order);
1159 }
1160
1161 void DolphinView::emitContentsMoved()
1162 {
1163 if (!m_blockContentsMovedSignal) {
1164 emit contentsMoved(contentsX(), contentsY());
1165 }
1166 }
1167
1168 void DolphinView::updateActivationState()
1169 {
1170 m_urlNavigator->setActive(isActive());
1171
1172 QColor color = KGlobalSettings::baseColor();
1173 if (isActive()) {
1174 emit urlChanged(url());
1175 emit selectionChanged(selectedItems());
1176 } else {
1177 // darken the background if the view is inactive
1178 // TODO: does not work for a black background
1179 color = color.darker(105);
1180 }
1181
1182 QWidget* viewport = itemView()->viewport();
1183 QPalette palette;
1184 palette.setColor(viewport->backgroundRole(), color);
1185 viewport->setPalette(palette);
1186
1187 update();
1188 }
1189
1190 void DolphinView::updateCutItems()
1191 {
1192 // restore the icons of all previously selected items to the
1193 // original state...
1194 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
1195 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
1196 while (it != end) {
1197 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
1198 if (index.isValid()) {
1199 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
1200 }
1201 ++it;
1202 }
1203 m_cutItemsCache.clear();
1204
1205 // ... and apply an item effect to all currently cut items
1206 applyCutItemEffect();
1207 }
1208
1209 void DolphinView::showHoverInformation(const QModelIndex& index)
1210 {
1211 if (hasSelection()) {
1212 return;
1213 }
1214
1215 const KFileItem* item = fileItem(index);
1216 if (item != 0) {
1217 m_statusBar->setMessage(item->getStatusBarInfo(), DolphinStatusBar::Default);
1218 emit requestItemInfo(item->url());
1219 }
1220 }
1221
1222 void DolphinView::clearHoverInformation()
1223 {
1224 m_statusBar->clear();
1225 }
1226
1227
1228 void DolphinView::createView()
1229 {
1230 // delete current view
1231 QAbstractItemView* view = itemView();
1232 if (view != 0) {
1233 m_topLayout->removeWidget(view);
1234 view->close();
1235 if (view == m_iconsView) {
1236 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
1237 m_iconsView->setItemCategorizer(0);
1238 delete categorizer;
1239 }
1240 view->deleteLater();
1241 view = 0;
1242 m_iconsView = 0;
1243 m_detailsView = 0;
1244 m_columnView = 0;
1245 m_fileItemDelegate = 0;
1246 }
1247
1248 Q_ASSERT(m_iconsView == 0);
1249 Q_ASSERT(m_detailsView == 0);
1250 Q_ASSERT(m_columnView == 0);
1251
1252 // ... and recreate it representing the current mode
1253 switch (m_mode) {
1254 case IconsView:
1255 m_iconsView = new DolphinIconsView(this, m_controller);
1256 view = m_iconsView;
1257 break;
1258
1259 case DetailsView:
1260 m_detailsView = new DolphinDetailsView(this, m_controller);
1261 view = m_detailsView;
1262 break;
1263
1264 case ColumnView:
1265 m_columnView = new DolphinColumnView(this, m_controller);
1266 view = m_columnView;
1267 break;
1268 }
1269
1270 Q_ASSERT(view != 0);
1271
1272 m_fileItemDelegate = new KFileItemDelegate(view);
1273 view->setItemDelegate(m_fileItemDelegate);
1274
1275 view->setModel(m_proxyModel);
1276 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1277
1278 new KMimeTypeResolver(view, m_dirModel);
1279 m_topLayout->insertWidget(1, view);
1280
1281 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1282 m_controller, SLOT(indicateSelectionChange()));
1283 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1284 this, SLOT(emitContentsMoved()));
1285 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1286 this, SLOT(emitContentsMoved()));
1287 }
1288
1289 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
1290 {
1291 QItemSelectionModel* selectionModel = itemView()->selectionModel();
1292 const QAbstractItemModel* itemModel = selectionModel->model();
1293
1294 const QModelIndex topLeft = itemModel->index(0, 0);
1295 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
1296 itemModel->columnCount() - 1);
1297
1298 QItemSelection selection(topLeft, bottomRight);
1299 selectionModel->select(selection, flags);
1300 }
1301
1302 QAbstractItemView* DolphinView::itemView() const
1303 {
1304 if (m_detailsView != 0) {
1305 return m_detailsView;
1306 } else if (m_columnView != 0) {
1307 return m_columnView;
1308 }
1309
1310 return m_iconsView;
1311 }
1312
1313 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
1314 {
1315 return index.isValid() && (index.column() == KDirModel::Name);
1316 }
1317
1318 bool DolphinView::isCutItem(const KFileItem& item) const
1319 {
1320 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1321 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1322
1323 const KUrl& itemUrl = item.url();
1324 KUrl::List::const_iterator it = cutUrls.begin();
1325 const KUrl::List::const_iterator end = cutUrls.end();
1326 while (it != end) {
1327 if (*it == itemUrl) {
1328 return true;
1329 }
1330 ++it;
1331 }
1332
1333 return false;
1334 }
1335
1336 void DolphinView::applyCutItemEffect()
1337 {
1338 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1339 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
1340 return;
1341 }
1342
1343 KFileItemList items(m_dirLister->items());
1344 KFileItemList::const_iterator it = items.begin();
1345 const KFileItemList::const_iterator end = items.end();
1346 while (it != end) {
1347 KFileItem* item = *it;
1348 if (isCutItem(*item)) {
1349 const QModelIndex index = m_dirModel->indexForItem(*item);
1350 const KFileItem* item = m_dirModel->itemForIndex(index);
1351 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
1352 if ((value.type() == QVariant::Icon) && (item != 0)) {
1353 const QIcon icon(qvariant_cast<QIcon>(value));
1354 QPixmap pixmap = icon.pixmap(128, 128);
1355
1356 // remember current pixmap for the item to be able
1357 // to restore it when other items get cut
1358 CutItem cutItem;
1359 cutItem.url = item->url();
1360 cutItem.pixmap = pixmap;
1361 m_cutItemsCache.append(cutItem);
1362
1363 // apply icon effect to the cut item
1364 KIconEffect iconEffect;
1365 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
1366 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
1367 }
1368 }
1369 ++it;
1370 }
1371 }
1372
1373 #include "dolphinview.moc"