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