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