]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Assure that the item view gets the keyboard focus when it was activated. This fixes...
[dolphin.git] / src / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <QApplication>
24 #include <QClipboard>
25 #include <QKeyEvent>
26 #include <QItemSelection>
27 #include <QBoxLayout>
28 #include <QTimer>
29 #include <QScrollBar>
30
31 #include <kactioncollection.h>
32 #include <kcolorscheme.h>
33 #include <kdirlister.h>
34 #include <kfileitemdelegate.h>
35 #include <kiconeffect.h>
36 #include <klocale.h>
37 #include <kio/deletejob.h>
38 #include <kio/netaccess.h>
39 #include <kio/previewjob.h>
40 #include <kjob.h>
41 #include <kmenu.h>
42 #include <kmessagebox.h>
43 #include <kmimetyperesolver.h>
44 #include <konq_operations.h>
45 #include <konqmimedata.h>
46 #include <ktoggleaction.h>
47 #include <kurl.h>
48
49 #include "dolphindropcontroller.h"
50 #include "dolphinmodel.h"
51 #include "dolphincolumnview.h"
52 #include "dolphincontroller.h"
53 #include "dolphinsortfilterproxymodel.h"
54 #include "dolphindetailsview.h"
55 #include "dolphiniconsview.h"
56 #include "dolphinsettings.h"
57 #include "dolphin_generalsettings.h"
58 #include "iconmanager.h"
59 #include "renamedialog.h"
60 #include "tooltipmanager.h"
61 #include "viewproperties.h"
62
63 DolphinView::DolphinView(QWidget* parent,
64 const KUrl& url,
65 KDirLister* dirLister,
66 DolphinModel* dolphinModel,
67 DolphinSortFilterProxyModel* proxyModel) :
68 QWidget(parent),
69 m_active(true),
70 m_showPreview(false),
71 m_loadingDirectory(false),
72 m_storedCategorizedSorting(false),
73 m_tabsForFiles(false),
74 m_isContextMenuOpen(false),
75 m_mode(DolphinView::IconsView),
76 m_topLayout(0),
77 m_controller(0),
78 m_iconsView(0),
79 m_detailsView(0),
80 m_columnView(0),
81 m_fileItemDelegate(0),
82 m_selectionModel(0),
83 m_dolphinModel(dolphinModel),
84 m_dirLister(dirLister),
85 m_proxyModel(proxyModel),
86 m_iconManager(0),
87 m_toolTipManager(0),
88 m_rootUrl(),
89 m_currentItemUrl()
90 {
91 m_topLayout = new QVBoxLayout(this);
92 m_topLayout->setSpacing(0);
93 m_topLayout->setMargin(0);
94
95 m_controller = new DolphinController(this);
96 m_controller->setUrl(url);
97
98 // Receiver of the DolphinView signal 'urlChanged()' don't need
99 // to care whether the internal controller changed the URL already or whether
100 // the controller just requested an URL change and will be updated later.
101 // In both cases the URL has been changed:
102 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
103 this, SIGNAL(urlChanged(const KUrl&)));
104 connect(m_controller, SIGNAL(requestUrlChange(const KUrl&)),
105 this, SLOT(slotRequestUrlChange(const KUrl&)));
106
107 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
108 this, SLOT(openContextMenu(const QPoint&)));
109 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&, const KFileItem&)),
110 this, SLOT(dropUrls(const KUrl::List&, const KUrl&, const KFileItem&)));
111 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
112 this, SLOT(updateSorting(DolphinView::Sorting)));
113 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
114 this, SLOT(updateSortOrder(Qt::SortOrder)));
115 connect(m_controller, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
116 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&)));
117 connect(m_controller, SIGNAL(itemTriggered(const KFileItem&)),
118 this, SLOT(triggerItem(const KFileItem&)));
119 connect(m_controller, SIGNAL(tabRequested(const KUrl&)),
120 this, SIGNAL(tabRequested(const KUrl&)));
121 connect(m_controller, SIGNAL(activated()),
122 this, SLOT(activate()));
123 connect(m_controller, SIGNAL(itemEntered(const KFileItem&)),
124 this, SLOT(showHoverInformation(const KFileItem&)));
125 connect(m_controller, SIGNAL(viewportEntered()),
126 this, SLOT(clearHoverInformation()));
127
128 connect(m_dirLister, SIGNAL(redirection(KUrl, KUrl)),
129 this, SLOT(slotRedirection(KUrl, KUrl)));
130 connect(m_dirLister, SIGNAL(completed()),
131 this, SLOT(restoreCurrentItem()));
132
133 applyViewProperties(url);
134 m_topLayout->addWidget(itemView());
135 }
136
137 DolphinView::~DolphinView()
138 {
139 }
140
141 const KUrl& DolphinView::url() const
142 {
143 return m_controller->url();
144 }
145
146 KUrl DolphinView::rootUrl() const
147 {
148 return isColumnViewActive() ? m_columnView->rootUrl() : url();
149 }
150
151 void DolphinView::setActive(bool active)
152 {
153 if (active == m_active) {
154 return;
155 }
156
157 m_active = active;
158 m_selectionModel->clearSelection();
159
160 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
161 if (active) {
162 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
163 // bypasses the problem when having a split view and changing the active view to
164 // update the some URL dependent states. A nicer approach should be no big deal...
165 emit urlChanged(url());
166 emit selectionChanged(selectedItems());
167 } else {
168 color.setAlpha(150);
169 }
170
171 QWidget* viewport = itemView()->viewport();
172 QPalette palette;
173 palette.setColor(viewport->backgroundRole(), color);
174 viewport->setPalette(palette);
175
176 update();
177
178 if (active) {
179 itemView()->setFocus();
180 emit activated();
181 }
182
183 m_controller->indicateActivationChange(active);
184 }
185
186 bool DolphinView::isActive() const
187 {
188 return m_active;
189 }
190
191 void DolphinView::setMode(Mode mode)
192 {
193 if (mode == m_mode) {
194 return; // the wished mode is already set
195 }
196
197 m_mode = mode;
198
199 deleteView();
200
201 const KUrl viewPropsUrl = viewPropertiesUrl();
202 ViewProperties props(viewPropsUrl);
203 props.setViewMode(m_mode);
204 createView();
205
206 // the file item delegate has been recreated, apply the current
207 // additional information manually
208 const KFileItemDelegate::InformationList infoList = props.additionalInfo();
209 m_fileItemDelegate->setShowInformation(infoList);
210 emit additionalInfoChanged();
211
212 // Not all view modes support categorized sorting. Adjust the sorting model
213 // if changing the view mode results in a change of the categorized sorting
214 // capabilities.
215 m_storedCategorizedSorting = props.categorizedSorting();
216 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
217 if (categorized != m_proxyModel->isCategorizedModel()) {
218 m_proxyModel->setCategorizedModel(categorized);
219 emit categorizedSortingChanged();
220 }
221
222 emit modeChanged();
223 }
224
225 DolphinView::Mode DolphinView::mode() const
226 {
227 return m_mode;
228 }
229
230 bool DolphinView::showPreview() const
231 {
232 return m_showPreview;
233 }
234
235 bool DolphinView::showHiddenFiles() const
236 {
237 return m_dirLister->showingDotFiles();
238 }
239
240 bool DolphinView::categorizedSorting() const
241 {
242 // If all view modes would support categorized sorting, returning
243 // m_proxyModel->isCategorizedModel() would be the way to go. As
244 // currently only the icons view supports caterized sorting, we remember
245 // the stored view properties state in m_storedCategorizedSorting and
246 // return this state. The application takes care to disable the corresponding
247 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
248 // that this setting is not applied to the current view mode.
249 return m_storedCategorizedSorting;
250 }
251
252 bool DolphinView::supportsCategorizedSorting() const
253 {
254 return m_iconsView != 0;
255 }
256
257 void DolphinView::selectAll()
258 {
259 QAbstractItemView* view = itemView();
260 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
261 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
262 // selection instead of selecting all items. This is bypassed for KDE 4.0
263 // by invoking clearSelection() first.
264 view->clearSelection();
265 view->selectAll();
266 }
267
268 void DolphinView::invertSelection()
269 {
270 if (isColumnViewActive()) {
271 // QAbstractItemView does not offer a virtual method invertSelection()
272 // as counterpart to QAbstractItemView::selectAll(). This makes it
273 // necessary to delegate the inverting of the selection to the
274 // column view, as only the selection of the active column should
275 // get inverted.
276 m_columnView->invertSelection();
277 } else {
278 QItemSelectionModel* selectionModel = itemView()->selectionModel();
279 const QAbstractItemModel* itemModel = selectionModel->model();
280
281 const QModelIndex topLeft = itemModel->index(0, 0);
282 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
283 itemModel->columnCount() - 1);
284
285 const QItemSelection selection(topLeft, bottomRight);
286 selectionModel->select(selection, QItemSelectionModel::Toggle);
287 }
288 }
289
290 bool DolphinView::hasSelection() const
291 {
292 return itemView()->selectionModel()->hasSelection();
293 }
294
295 void DolphinView::clearSelection()
296 {
297 itemView()->selectionModel()->clear();
298 }
299
300 KFileItemList DolphinView::selectedItems() const
301 {
302 const QAbstractItemView* view = itemView();
303
304 // Our view has a selection, we will map them back to the DolphinModel
305 // and then fill the KFileItemList.
306 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
307
308 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
309 KFileItemList itemList;
310
311 const QModelIndexList indexList = selection.indexes();
312 foreach (const QModelIndex &index, indexList) {
313 KFileItem item = m_dolphinModel->itemForIndex(index);
314 if (!item.isNull()) {
315 itemList.append(item);
316 }
317 }
318
319 return itemList;
320 }
321
322 KUrl::List DolphinView::selectedUrls() const
323 {
324 KUrl::List urls;
325 const KFileItemList list = selectedItems();
326 foreach (const KFileItem &item, list) {
327 urls.append(item.url());
328 }
329 return urls;
330 }
331
332 KFileItem DolphinView::fileItem(const QModelIndex& index) const
333 {
334 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
335 return m_dolphinModel->itemForIndex(dolphinModelIndex);
336 }
337
338 void DolphinView::setContentsPosition(int x, int y)
339 {
340 QAbstractItemView* view = itemView();
341
342 // the ColumnView takes care itself for the horizontal scrolling
343 if (!isColumnViewActive()) {
344 view->horizontalScrollBar()->setValue(x);
345 }
346 view->verticalScrollBar()->setValue(y);
347
348 m_loadingDirectory = false;
349 }
350
351 QPoint DolphinView::contentsPosition() const
352 {
353 const int x = itemView()->horizontalScrollBar()->value();
354 const int y = itemView()->verticalScrollBar()->value();
355 return QPoint(x, y);
356 }
357
358 void DolphinView::zoomIn()
359 {
360 m_controller->triggerZoomIn();
361 m_iconManager->updatePreviews();
362 }
363
364 void DolphinView::zoomOut()
365 {
366 m_controller->triggerZoomOut();
367 m_iconManager->updatePreviews();
368 }
369
370 bool DolphinView::isZoomInPossible() const
371 {
372 return m_controller->isZoomInPossible();
373 }
374
375 bool DolphinView::isZoomOutPossible() const
376 {
377 return m_controller->isZoomOutPossible();
378 }
379
380 void DolphinView::setSorting(Sorting sorting)
381 {
382 if (sorting != this->sorting()) {
383 updateSorting(sorting);
384 }
385 }
386
387 DolphinView::Sorting DolphinView::sorting() const
388 {
389 return m_proxyModel->sorting();
390 }
391
392 void DolphinView::setSortOrder(Qt::SortOrder order)
393 {
394 if (sortOrder() != order) {
395 updateSortOrder(order);
396 }
397 }
398
399 Qt::SortOrder DolphinView::sortOrder() const
400 {
401 return m_proxyModel->sortOrder();
402 }
403
404 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
405 {
406 const KUrl viewPropsUrl = viewPropertiesUrl();
407 ViewProperties props(viewPropsUrl);
408 props.setAdditionalInfo(info);
409 m_fileItemDelegate->setShowInformation(info);
410
411 emit additionalInfoChanged();
412
413 if (itemView() != m_detailsView) {
414 // the details view requires no reloading of the directory, as it maps
415 // the file item delegate info to its columns internally
416 loadDirectory(viewPropsUrl);
417 }
418 }
419
420 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
421 {
422 return m_fileItemDelegate->showInformation();
423 }
424
425 void DolphinView::reload()
426 {
427 setUrl(url());
428 loadDirectory(url(), true);
429 }
430
431 void DolphinView::refresh()
432 {
433 const bool oldActivationState = m_active;
434 m_active = true;
435
436 createView();
437 applyViewProperties(m_controller->url());
438 reload();
439
440 setActive(oldActivationState);
441 }
442
443 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
444 {
445 if (m_controller->url() == url) {
446 return;
447 }
448
449 m_controller->setUrl(url); // emits urlChanged, which we forward
450
451 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
452 applyViewProperties(rootUrl);
453 loadDirectory(rootUrl);
454 if (itemView() == m_columnView) {
455 m_columnView->setRootUrl(rootUrl);
456 m_columnView->showColumn(url);
457 }
458 } else {
459 applyViewProperties(url);
460 loadDirectory(url);
461 }
462
463 emit startedPathLoading(url);
464 }
465
466 void DolphinView::setNameFilter(const QString& nameFilter)
467 {
468 m_proxyModel->setFilterRegExp(nameFilter);
469
470 if (isColumnViewActive()) {
471 // adjusting the directory lister is not enough in the case of the
472 // column view, as each column has its own directory lister internally...
473 m_columnView->setNameFilter(nameFilter);
474 }
475 }
476
477 void DolphinView::calculateItemCount(int& fileCount, int& folderCount)
478 {
479 foreach (const KFileItem &item, m_dirLister->items()) {
480 if (item.isDir()) {
481 ++folderCount;
482 } else {
483 ++fileCount;
484 }
485 }
486 }
487
488 void DolphinView::setUrl(const KUrl& url)
489 {
490 // remember current item candidate (see restoreCurrentItem())
491 m_currentItemUrl = url;
492 updateView(url, KUrl());
493 }
494
495 void DolphinView::changeSelection(const KFileItemList& selection)
496 {
497 clearSelection();
498 if (selection.isEmpty()) {
499 return;
500 }
501 const KUrl& baseUrl = url();
502 KUrl url;
503 QItemSelection new_selection;
504 foreach(const KFileItem& item, selection) {
505 url = item.url().upUrl();
506 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
507 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
508 new_selection.select(index, index);
509 }
510 }
511 itemView()->selectionModel()->select(new_selection,
512 QItemSelectionModel::ClearAndSelect
513 | QItemSelectionModel::Current);
514 }
515
516 void DolphinView::renameSelectedItems()
517 {
518 const KFileItemList items = selectedItems();
519 if (items.count() > 1) {
520 // More than one item has been selected for renaming. Open
521 // a rename dialog and rename all items afterwards.
522 RenameDialog dialog(this, items);
523 if (dialog.exec() == QDialog::Rejected) {
524 return;
525 }
526
527 const QString newName = dialog.newName();
528 if (newName.isEmpty()) {
529 emit errorMessage(dialog.errorString());
530 } else {
531 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
532 // as one operation instead of n rename operations like it is done now...
533 Q_ASSERT(newName.contains('#'));
534
535 // iterate through all selected items and rename them...
536 int index = 1;
537 foreach (const KFileItem &item, items) {
538 const KUrl& oldUrl = item.url();
539 QString number;
540 number.setNum(index++);
541
542 QString name = newName;
543 name.replace('#', number);
544
545 if (oldUrl.fileName() != name) {
546 KUrl newUrl = oldUrl;
547 newUrl.setFileName(name);
548 KonqOperations::rename(this, oldUrl, newUrl);
549 emit doingOperation(KIO::FileUndoManager::Rename);
550 }
551 }
552 }
553 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
554 Q_ASSERT(items.count() == 1);
555
556 if (isColumnViewActive()) {
557 m_columnView->editItem(items.first());
558 } else {
559 const QModelIndex dirIndex = m_dolphinModel->indexForItem(items.first());
560 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
561 itemView()->edit(proxyIndex);
562 }
563 } else {
564 Q_ASSERT(items.count() == 1);
565
566 RenameDialog dialog(this, items);
567 if (dialog.exec() == QDialog::Rejected) {
568 return;
569 }
570
571 const QString& newName = dialog.newName();
572 if (newName.isEmpty()) {
573 emit errorMessage(dialog.errorString());
574 } else {
575 const KUrl& oldUrl = items.first().url();
576 KUrl newUrl = oldUrl;
577 newUrl.setFileName(newName);
578 KonqOperations::rename(this, oldUrl, newUrl);
579 emit doingOperation(KIO::FileUndoManager::Rename);
580 }
581 }
582 }
583
584 void DolphinView::trashSelectedItems()
585 {
586 emit doingOperation(KIO::FileUndoManager::Trash);
587 KonqOperations::del(this, KonqOperations::TRASH, selectedUrls());
588 }
589
590 void DolphinView::deleteSelectedItems()
591 {
592 const KUrl::List list = selectedUrls();
593 const bool del = KonqOperations::askDeleteConfirmation(list,
594 KonqOperations::DEL,
595 KonqOperations::DEFAULT_CONFIRMATION,
596 this);
597
598 if (del) {
599 KIO::Job* job = KIO::del(list);
600 connect(job, SIGNAL(result(KJob*)),
601 this, SLOT(slotDeleteFileFinished(KJob*)));
602 }
603 }
604
605 void DolphinView::cutSelectedItems()
606 {
607 QMimeData* mimeData = new QMimeData();
608 const KUrl::List kdeUrls = selectedUrls();
609 const KUrl::List mostLocalUrls;
610 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
611 QApplication::clipboard()->setMimeData(mimeData);
612 }
613
614 void DolphinView::copySelectedItems()
615 {
616 QMimeData* mimeData = new QMimeData();
617 const KUrl::List kdeUrls = selectedUrls();
618 const KUrl::List mostLocalUrls;
619 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
620 QApplication::clipboard()->setMimeData(mimeData);
621 }
622
623 void DolphinView::paste()
624 {
625 pasteToUrl(url());
626 }
627
628 void DolphinView::pasteIntoFolder()
629 {
630 const KFileItemList items = selectedItems();
631 if ((items.count() == 1) && items.first().isDir()) {
632 pasteToUrl(items.first().url());
633 }
634 }
635
636 void DolphinView::setShowPreview(bool show)
637 {
638 if (m_showPreview == show) {
639 return;
640 }
641
642 const KUrl viewPropsUrl = viewPropertiesUrl();
643 ViewProperties props(viewPropsUrl);
644 props.setShowPreview(show);
645
646 m_showPreview = show;
647 m_iconManager->setShowPreview(show);
648 emit showPreviewChanged();
649
650 loadDirectory(viewPropsUrl);
651 }
652
653 void DolphinView::setShowHiddenFiles(bool show)
654 {
655 if (m_dirLister->showingDotFiles() == show) {
656 return;
657 }
658
659 const KUrl viewPropsUrl = viewPropertiesUrl();
660 ViewProperties props(viewPropsUrl);
661 props.setShowHiddenFiles(show);
662
663 m_dirLister->setShowingDotFiles(show);
664 emit showHiddenFilesChanged();
665
666 loadDirectory(viewPropsUrl);
667 }
668
669 void DolphinView::setCategorizedSorting(bool categorized)
670 {
671 if (categorized == categorizedSorting()) {
672 return;
673 }
674
675 // setCategorizedSorting(true) may only get invoked
676 // if the view supports categorized sorting
677 Q_ASSERT(!categorized || supportsCategorizedSorting());
678
679 ViewProperties props(viewPropertiesUrl());
680 props.setCategorizedSorting(categorized);
681 props.save();
682
683 m_storedCategorizedSorting = categorized;
684 m_proxyModel->setCategorizedModel(categorized);
685
686 emit categorizedSortingChanged();
687 }
688
689 void DolphinView::toggleSortOrder()
690 {
691 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
692 Qt::DescendingOrder :
693 Qt::AscendingOrder;
694 setSortOrder(order);
695 }
696
697 void DolphinView::toggleAdditionalInfo(QAction* action)
698 {
699 const KFileItemDelegate::Information info =
700 static_cast<KFileItemDelegate::Information>(action->data().toInt());
701
702 KFileItemDelegate::InformationList list = additionalInfo();
703
704 const bool show = action->isChecked();
705
706 const int index = list.indexOf(info);
707 const bool containsInfo = (index >= 0);
708 if (show && !containsInfo) {
709 list.append(info);
710 setAdditionalInfo(list);
711 } else if (!show && containsInfo) {
712 list.removeAt(index);
713 setAdditionalInfo(list);
714 Q_ASSERT(list.indexOf(info) < 0);
715 }
716 }
717
718
719 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
720 {
721 QWidget::mouseReleaseEvent(event);
722 setActive(true);
723 }
724
725 void DolphinView::wheelEvent(QWheelEvent* event)
726 {
727 if (event->modifiers() & Qt::ControlModifier) {
728 const int delta = event->delta();
729 if ((delta > 0) && isZoomInPossible()) {
730 zoomIn();
731 } else if ((delta < 0) && isZoomOutPossible()) {
732 zoomOut();
733 }
734 event->accept();
735 }
736 }
737
738 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
739 {
740 if ((watched == itemView()) && (event->type() == QEvent::FocusIn)) {
741 m_controller->requestActivation();
742 }
743
744 return QWidget::eventFilter(watched, event);
745 }
746
747 void DolphinView::activate()
748 {
749 setActive(true);
750 }
751
752 void DolphinView::triggerItem(const KFileItem& item)
753 {
754 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
755 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
756 // items are selected by the user, hence don't trigger the
757 // item specified by 'index'
758 return;
759 }
760
761 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
762 if (item.isNull() || m_isContextMenuOpen) {
763 return;
764 }
765
766 if (m_toolTipManager != 0) {
767 m_toolTipManager->hideTip();
768 }
769 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
770 }
771
772 void DolphinView::emitSelectionChangedSignal()
773 {
774 emit selectionChanged(DolphinView::selectedItems());
775 }
776
777 void DolphinView::openContextMenu(const QPoint& pos)
778 {
779 KFileItem item;
780
781 const QModelIndex index = itemView()->indexAt(pos);
782 if (index.isValid() && (index.column() == DolphinModel::Name)) {
783 item = fileItem(index);
784 }
785
786 if (m_toolTipManager != 0) {
787 m_toolTipManager->hideTip();
788 }
789
790 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
791 emit requestContextMenu(item, url());
792 m_isContextMenuOpen = false;
793 }
794
795 void DolphinView::dropUrls(const KUrl::List& urls,
796 const KUrl& destPath,
797 const KFileItem& destItem)
798 {
799 Q_ASSERT(!urls.isEmpty());
800 const KUrl& destination = !destItem.isNull() && destItem.isDir() ?
801 destItem.url() : destPath;
802 const KUrl sourceDir = KUrl(urls.first().directory());
803 if (sourceDir != destination) {
804 dropUrls(urls, destination);
805 }
806 }
807
808 void DolphinView::dropUrls(const KUrl::List& urls,
809 const KUrl& destination)
810 {
811 DolphinDropController dropController(this);
812 // forward doingOperation signal up to the mainwindow
813 connect(&dropController, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType)),
814 this, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType)));
815 dropController.dropUrls(urls, destination);
816 }
817
818 void DolphinView::updateSorting(DolphinView::Sorting sorting)
819 {
820 ViewProperties props(viewPropertiesUrl());
821 props.setSorting(sorting);
822
823 m_proxyModel->setSorting(sorting);
824
825 emit sortingChanged(sorting);
826 }
827
828 void DolphinView::updateSortOrder(Qt::SortOrder order)
829 {
830 ViewProperties props(viewPropertiesUrl());
831 props.setSortOrder(order);
832
833 m_proxyModel->setSortOrder(order);
834
835 emit sortOrderChanged(order);
836 }
837
838 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
839 {
840 ViewProperties props(viewPropertiesUrl());
841 props.setAdditionalInfo(info);
842 props.save();
843
844 m_fileItemDelegate->setShowInformation(info);
845
846 emit additionalInfoChanged();
847 }
848
849 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
850 {
851 const bool enable = (m_mode == DolphinView::DetailsView) ||
852 (m_mode == DolphinView::IconsView);
853
854 QAction* showSizeInfo = collection->action("show_size_info");
855 QAction* showDateInfo = collection->action("show_date_info");
856 QAction* showPermissionsInfo = collection->action("show_permissions_info");
857 QAction* showOwnerInfo = collection->action("show_owner_info");
858 QAction* showGroupInfo = collection->action("show_group_info");
859 QAction* showMimeInfo = collection->action("show_mime_info");
860
861 showSizeInfo->setChecked(false);
862 showDateInfo->setChecked(false);
863 showPermissionsInfo->setChecked(false);
864 showOwnerInfo->setChecked(false);
865 showGroupInfo->setChecked(false);
866 showMimeInfo->setChecked(false);
867
868 showSizeInfo->setEnabled(enable);
869 showDateInfo->setEnabled(enable);
870 showPermissionsInfo->setEnabled(enable);
871 showOwnerInfo->setEnabled(enable);
872 showGroupInfo->setEnabled(enable);
873 showMimeInfo->setEnabled(enable);
874
875 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
876 switch (info) {
877 case KFileItemDelegate::Size:
878 showSizeInfo->setChecked(true);
879 break;
880 case KFileItemDelegate::ModificationTime:
881 showDateInfo->setChecked(true);
882 break;
883 case KFileItemDelegate::Permissions:
884 showPermissionsInfo->setChecked(true);
885 break;
886 case KFileItemDelegate::Owner:
887 showOwnerInfo->setChecked(true);
888 break;
889 case KFileItemDelegate::OwnerAndGroup:
890 showGroupInfo->setChecked(true);
891 break;
892 case KFileItemDelegate::FriendlyMimeType:
893 showMimeInfo->setChecked(true);
894 break;
895 default:
896 break;
897 }
898 }
899 }
900
901 QPair<bool, QString> DolphinView::pasteInfo() const
902 {
903 QPair<bool, QString> ret;
904 QClipboard* clipboard = QApplication::clipboard();
905 const QMimeData* mimeData = clipboard->mimeData();
906
907 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
908 if (!urls.isEmpty()) {
909 ret.first = true;
910 if (urls.count() == 1) {
911 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, urls.first(), true);
912 ret.second = item.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
913 i18nc("@action:inmenu", "Paste One File");
914
915 } else {
916 ret.second = i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls.count());
917 }
918 } else {
919 ret.first = false;
920 ret.second = i18nc("@action:inmenu", "Paste");
921 }
922
923 return ret;
924 }
925
926 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
927 {
928 m_tabsForFiles = tabsForFiles;
929 }
930
931 bool DolphinView::isTabsForFilesEnabled() const
932 {
933 return m_tabsForFiles;
934 }
935
936 void DolphinView::emitContentsMoved()
937 {
938 // only emit the contents moved signal if:
939 // - no directory loading is ongoing (this would reset the contents position
940 // always to (0, 0))
941 // - if the Column View is active: the column view does an automatic
942 // positioning during the loading operation, which must be remembered
943 if (!m_loadingDirectory || isColumnViewActive()) {
944 const QPoint pos(contentsPosition());
945 emit contentsMoved(pos.x(), pos.y());
946 }
947 }
948
949 void DolphinView::showHoverInformation(const KFileItem& item)
950 {
951 emit requestItemInfo(item);
952 }
953
954 void DolphinView::clearHoverInformation()
955 {
956 emit requestItemInfo(KFileItem());
957 }
958
959 void DolphinView::slotDeleteFileFinished(KJob* job)
960 {
961 if (job->error() == 0) {
962 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
963 } else {
964 emit errorMessage(job->errorString());
965 }
966 }
967
968
969 void DolphinView::restoreCurrentItem()
970 {
971 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_currentItemUrl);
972 if (dirIndex.isValid()) {
973 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
974 QAbstractItemView* view = itemView();
975 const bool clearSelection = !hasSelection();
976 view->setCurrentIndex(proxyIndex);
977 if (clearSelection) {
978 view->clearSelection();
979 }
980 }
981 }
982
983 void DolphinView::loadDirectory(const KUrl& url, bool reload)
984 {
985 if (!url.isValid()) {
986 const QString location(url.pathOrUrl());
987 if (location.isEmpty()) {
988 emit errorMessage(i18nc("@info:status", "The location is empty."));
989 } else {
990 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
991 }
992 return;
993 }
994
995 m_loadingDirectory = true;
996
997 m_dirLister->stop();
998 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
999
1000 if (isColumnViewActive()) {
1001 // adjusting the directory lister is not enough in the case of the
1002 // column view, as each column has its own directory lister internally...
1003 if (reload) {
1004 m_columnView->reload();
1005 } else {
1006 m_columnView->showColumn(url);
1007 }
1008 }
1009 }
1010
1011 KUrl DolphinView::viewPropertiesUrl() const
1012 {
1013 if (isColumnViewActive()) {
1014 return m_columnView->rootUrl();
1015 }
1016
1017 return url();
1018 }
1019
1020 void DolphinView::applyViewProperties(const KUrl& url)
1021 {
1022 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
1023 // The column view is active, hence don't apply the view properties
1024 // of sub directories (represented by columns) to the view. The
1025 // view always represents the properties of the first column.
1026 return;
1027 }
1028
1029 const ViewProperties props(url);
1030
1031 const Mode mode = props.viewMode();
1032 if (m_mode != mode) {
1033 m_mode = mode;
1034 createView();
1035 emit modeChanged();
1036 }
1037 if (itemView() == 0) {
1038 createView();
1039 }
1040 Q_ASSERT(itemView() != 0);
1041 Q_ASSERT(m_fileItemDelegate != 0);
1042
1043 const bool showHiddenFiles = props.showHiddenFiles();
1044 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
1045 m_dirLister->setShowingDotFiles(showHiddenFiles);
1046 emit showHiddenFilesChanged();
1047 }
1048
1049 m_storedCategorizedSorting = props.categorizedSorting();
1050 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1051 if (categorized != m_proxyModel->isCategorizedModel()) {
1052 m_proxyModel->setCategorizedModel(categorized);
1053 emit categorizedSortingChanged();
1054 }
1055
1056 const DolphinView::Sorting sorting = props.sorting();
1057 if (sorting != m_proxyModel->sorting()) {
1058 m_proxyModel->setSorting(sorting);
1059 emit sortingChanged(sorting);
1060 }
1061
1062 const Qt::SortOrder sortOrder = props.sortOrder();
1063 if (sortOrder != m_proxyModel->sortOrder()) {
1064 m_proxyModel->setSortOrder(sortOrder);
1065 emit sortOrderChanged(sortOrder);
1066 }
1067
1068 KFileItemDelegate::InformationList info = props.additionalInfo();
1069 if (info != m_fileItemDelegate->showInformation()) {
1070 m_fileItemDelegate->setShowInformation(info);
1071 emit additionalInfoChanged();
1072 }
1073
1074 const bool showPreview = props.showPreview();
1075 if (showPreview != m_showPreview) {
1076 m_showPreview = showPreview;
1077 m_iconManager->setShowPreview(showPreview);
1078 emit showPreviewChanged();
1079 }
1080 }
1081
1082 void DolphinView::createView()
1083 {
1084 deleteView();
1085 Q_ASSERT(m_iconsView == 0);
1086 Q_ASSERT(m_detailsView == 0);
1087 Q_ASSERT(m_columnView == 0);
1088
1089 QAbstractItemView* view = 0;
1090 switch (m_mode) {
1091 case IconsView: {
1092 m_iconsView = new DolphinIconsView(this, m_controller);
1093 view = m_iconsView;
1094 break;
1095 }
1096
1097 case DetailsView:
1098 m_detailsView = new DolphinDetailsView(this, m_controller);
1099 view = m_detailsView;
1100 break;
1101
1102 case ColumnView:
1103 m_columnView = new DolphinColumnView(this, m_controller);
1104 view = m_columnView;
1105 break;
1106 }
1107
1108 Q_ASSERT(view != 0);
1109 view->installEventFilter(this);
1110
1111 m_controller->setItemView(view);
1112
1113 m_fileItemDelegate = new KFileItemDelegate(view);
1114 view->setItemDelegate(m_fileItemDelegate);
1115
1116 view->setModel(m_proxyModel);
1117 if (m_selectionModel != 0) {
1118 view->setSelectionModel(m_selectionModel);
1119 } else {
1120 m_selectionModel = view->selectionModel();
1121 }
1122
1123 // reparent the selection model, as it should not be deleted
1124 // when deleting the model
1125 m_selectionModel->setParent(this);
1126
1127 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1128
1129 new KMimeTypeResolver(view, m_dolphinModel);
1130 m_iconManager = new IconManager(view, m_proxyModel);
1131 m_iconManager->setShowPreview(m_showPreview);
1132
1133 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1134 m_toolTipManager = new ToolTipManager(view, m_proxyModel);
1135 }
1136
1137 m_topLayout->insertWidget(1, view);
1138
1139 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1140 this, SLOT(emitSelectionChangedSignal()));
1141 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1142 this, SLOT(emitContentsMoved()));
1143 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1144 this, SLOT(emitContentsMoved()));
1145 }
1146
1147 void DolphinView::deleteView()
1148 {
1149 QAbstractItemView* view = itemView();
1150 if (view != 0) {
1151 m_topLayout->removeWidget(view);
1152 view->close();
1153 view->deleteLater();
1154 view = 0;
1155 m_iconsView = 0;
1156 m_detailsView = 0;
1157 m_columnView = 0;
1158 m_fileItemDelegate = 0;
1159 m_iconManager = 0;
1160 m_toolTipManager = 0;
1161 }
1162 }
1163
1164 QAbstractItemView* DolphinView::itemView() const
1165 {
1166 if (m_detailsView != 0) {
1167 return m_detailsView;
1168 } else if (m_columnView != 0) {
1169 return m_columnView;
1170 }
1171
1172 return m_iconsView;
1173 }
1174
1175 bool DolphinView::isCutItem(const KFileItem& item) const
1176 {
1177 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1178 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1179
1180 const KUrl& itemUrl = item.url();
1181 KUrl::List::const_iterator it = cutUrls.begin();
1182 const KUrl::List::const_iterator end = cutUrls.end();
1183 while (it != end) {
1184 if (*it == itemUrl) {
1185 return true;
1186 }
1187 ++it;
1188 }
1189
1190 return false;
1191 }
1192
1193 void DolphinView::pasteToUrl(const KUrl& url)
1194 {
1195 QClipboard* clipboard = QApplication::clipboard();
1196 const QMimeData* mimeData = clipboard->mimeData();
1197
1198 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1199 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
1200 KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, url);
1201 emit doingOperation(KIO::FileUndoManager::Move);
1202 clipboard->clear();
1203 } else {
1204 KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, url);
1205 emit doingOperation(KIO::FileUndoManager::Copy);
1206 }
1207 }
1208
1209 void DolphinView::slotRequestUrlChange(const KUrl& url)
1210 {
1211 emit requestUrlChange(url);
1212 m_controller->setUrl(url);
1213 }
1214
1215 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
1216 {
1217 if (oldUrl == m_controller->url()) {
1218 m_controller->setUrl(newUrl);
1219 }
1220 }
1221
1222 #include "dolphinview.moc"