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