]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Bypassed a Qt-issue where enabling the menu-animation for QApplication emits a clicke...
[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 "viewproperties.h"
61
62 DolphinView::DolphinView(QWidget* parent,
63 const KUrl& url,
64 KDirLister* dirLister,
65 DolphinModel* dolphinModel,
66 DolphinSortFilterProxyModel* proxyModel) :
67 QWidget(parent),
68 m_active(true),
69 m_showPreview(false),
70 m_loadingDirectory(false),
71 m_storedCategorizedSorting(false),
72 m_isContextMenuOpen(false),
73 m_mode(DolphinView::IconsView),
74 m_topLayout(0),
75 m_controller(0),
76 m_iconsView(0),
77 m_detailsView(0),
78 m_columnView(0),
79 m_fileItemDelegate(0),
80 m_selectionModel(0),
81 m_dolphinModel(dolphinModel),
82 m_dirLister(dirLister),
83 m_proxyModel(proxyModel),
84 m_iconManager(0)
85 {
86 setFocusPolicy(Qt::StrongFocus);
87 m_topLayout = new QVBoxLayout(this);
88 m_topLayout->setSpacing(0);
89 m_topLayout->setMargin(0);
90
91 m_controller = new DolphinController(this);
92 m_controller->setUrl(url);
93
94 // Receiver of the DolphinView signal 'urlChanged()' don't need
95 // to care whether the internal controller changed the URL already or whether
96 // the controller just requested an URL change and will be updated later.
97 // In both cases the URL has been changed:
98 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
99 this, SIGNAL(urlChanged(const KUrl&)));
100 connect(m_controller, SIGNAL(requestUrlChange(const KUrl&)),
101 this, SIGNAL(urlChanged(const KUrl&)));
102
103 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
104 this, SLOT(openContextMenu(const QPoint&)));
105 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&, const KFileItem&)),
106 this, SLOT(dropUrls(const KUrl::List&, const KUrl&, const KFileItem&)));
107 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
108 this, SLOT(updateSorting(DolphinView::Sorting)));
109 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
110 this, SLOT(updateSortOrder(Qt::SortOrder)));
111 connect(m_controller, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
112 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&)));
113 connect(m_controller, SIGNAL(itemTriggered(const KFileItem&)),
114 this, SLOT(triggerItem(const KFileItem&)));
115 connect(m_controller, SIGNAL(activated()),
116 this, SLOT(activate()));
117 connect(m_controller, SIGNAL(itemEntered(const KFileItem&)),
118 this, SLOT(showHoverInformation(const KFileItem&)));
119 connect(m_controller, SIGNAL(viewportEntered()),
120 this, SLOT(clearHoverInformation()));
121
122 applyViewProperties(url);
123 m_topLayout->addWidget(itemView());
124
125 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
126 // TODO: instantiate ToolTipManager here...
127 }
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 (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 (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 (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 KonqFileUndoManager/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 (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(KonqFileUndoManager::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(KonqFileUndoManager::RENAME);
570 }
571 }
572 }
573
574 void DolphinView::trashSelectedItems()
575 {
576 emit doingOperation(KonqFileUndoManager::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 xxxxxx
752 if (item.isNull() || m_isContextMenuOpen) {
753 return;
754 }
755
756 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
757 }
758
759 void DolphinView::emitSelectionChangedSignal()
760 {
761 emit selectionChanged(DolphinView::selectedItems());
762 }
763
764 void DolphinView::openContextMenu(const QPoint& pos)
765 {
766 KFileItem item;
767
768 const QModelIndex index = itemView()->indexAt(pos);
769 if (index.isValid() && (index.column() == DolphinModel::Name)) {
770 item = fileItem(index);
771 }
772
773 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue xxxxxx
774 emit requestContextMenu(item, url());
775 m_isContextMenuOpen = false;
776 }
777
778 void DolphinView::dropUrls(const KUrl::List& urls,
779 const KUrl& destPath,
780 const KFileItem& destItem)
781 {
782 Q_ASSERT(!urls.isEmpty());
783 const KUrl& destination = !destItem.isNull() && destItem.isDir() ?
784 destItem.url() : destPath;
785 const KUrl sourceDir = KUrl(urls.first().directory());
786 if (sourceDir != destination) {
787 dropUrls(urls, destination);
788 }
789 }
790
791 void DolphinView::dropUrls(const KUrl::List& urls,
792 const KUrl& destination)
793 {
794 DolphinDropController dropController(this);
795 // forward doingOperation signal up to the mainwindow
796 connect(&dropController, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)),
797 this, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)));
798 dropController.dropUrls(urls, destination);
799 }
800
801 void DolphinView::updateSorting(DolphinView::Sorting sorting)
802 {
803 ViewProperties props(viewPropertiesUrl());
804 props.setSorting(sorting);
805
806 m_proxyModel->setSorting(sorting);
807
808 emit sortingChanged(sorting);
809 }
810
811 void DolphinView::updateSortOrder(Qt::SortOrder order)
812 {
813 ViewProperties props(viewPropertiesUrl());
814 props.setSortOrder(order);
815
816 m_proxyModel->setSortOrder(order);
817
818 emit sortOrderChanged(order);
819 }
820
821 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
822 {
823 ViewProperties props(viewPropertiesUrl());
824 props.setAdditionalInfo(info);
825 props.save();
826
827 m_fileItemDelegate->setShowInformation(info);
828
829 emit additionalInfoChanged();
830 }
831
832 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
833 {
834 const bool enable = (m_mode == DolphinView::DetailsView) ||
835 (m_mode == DolphinView::IconsView);
836
837 QAction* showSizeInfo = collection->action("show_size_info");
838 QAction* showDateInfo = collection->action("show_date_info");
839 QAction* showPermissionsInfo = collection->action("show_permissions_info");
840 QAction* showOwnerInfo = collection->action("show_owner_info");
841 QAction* showGroupInfo = collection->action("show_group_info");
842 QAction* showMimeInfo = collection->action("show_mime_info");
843
844 showSizeInfo->setChecked(false);
845 showDateInfo->setChecked(false);
846 showPermissionsInfo->setChecked(false);
847 showOwnerInfo->setChecked(false);
848 showGroupInfo->setChecked(false);
849 showMimeInfo->setChecked(false);
850
851 showSizeInfo->setEnabled(enable);
852 showDateInfo->setEnabled(enable);
853 showPermissionsInfo->setEnabled(enable);
854 showOwnerInfo->setEnabled(enable);
855 showGroupInfo->setEnabled(enable);
856 showMimeInfo->setEnabled(enable);
857
858 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
859 switch (info) {
860 case KFileItemDelegate::Size:
861 showSizeInfo->setChecked(true);
862 break;
863 case KFileItemDelegate::ModificationTime:
864 showDateInfo->setChecked(true);
865 break;
866 case KFileItemDelegate::Permissions:
867 showPermissionsInfo->setChecked(true);
868 break;
869 case KFileItemDelegate::Owner:
870 showOwnerInfo->setChecked(true);
871 break;
872 case KFileItemDelegate::OwnerAndGroup:
873 showGroupInfo->setChecked(true);
874 break;
875 case KFileItemDelegate::FriendlyMimeType:
876 showMimeInfo->setChecked(true);
877 break;
878 default:
879 break;
880 }
881 }
882 }
883
884 QPair<bool, QString> DolphinView::pasteInfo() const
885 {
886 QPair<bool, QString> ret;
887 QClipboard* clipboard = QApplication::clipboard();
888 const QMimeData* mimeData = clipboard->mimeData();
889
890 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
891 if (!urls.isEmpty()) {
892 ret.first = true;
893 if (urls.count() == 1) {
894 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, urls.first(), true);
895 ret.second = item.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
896 i18nc("@action:inmenu", "Paste One File");
897
898 } else {
899 ret.second = i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls.count());
900 }
901 } else {
902 ret.first = false;
903 ret.second = i18nc("@action:inmenu", "Paste");
904 }
905
906 return ret;
907 }
908
909 void DolphinView::emitContentsMoved()
910 {
911 // only emit the contents moved signal if:
912 // - no directory loading is ongoing (this would reset the contents position
913 // always to (0, 0))
914 // - if the Column View is active: the column view does an automatic
915 // positioning during the loading operation, which must be remembered
916 if (!m_loadingDirectory || isColumnViewActive()) {
917 const QPoint pos(contentsPosition());
918 emit contentsMoved(pos.x(), pos.y());
919 }
920 }
921
922 void DolphinView::showHoverInformation(const KFileItem& item)
923 {
924 emit requestItemInfo(item);
925 }
926
927 void DolphinView::clearHoverInformation()
928 {
929 emit requestItemInfo(KFileItem());
930 }
931
932 void DolphinView::slotDeleteFileFinished(KJob* job)
933 {
934 if (job->error() == 0) {
935 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
936 } else {
937 emit errorMessage(job->errorString());
938 }
939 }
940
941 void DolphinView::loadDirectory(const KUrl& url, bool reload)
942 {
943 if (!url.isValid()) {
944 const QString location(url.pathOrUrl());
945 if (location.isEmpty()) {
946 emit errorMessage(i18nc("@info:status", "The location is empty."));
947 } else {
948 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
949 }
950 return;
951 }
952
953 m_loadingDirectory = true;
954
955 m_dirLister->stop();
956 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
957
958 if (isColumnViewActive()) {
959 // adjusting the directory lister is not enough in the case of the
960 // column view, as each column has its own directory lister internally...
961 if (reload) {
962 m_columnView->reload();
963 } else {
964 m_columnView->showColumn(url);
965 }
966 }
967 }
968
969 KUrl DolphinView::viewPropertiesUrl() const
970 {
971 if (isColumnViewActive()) {
972 return m_columnView->rootUrl();
973 }
974
975 return url();
976 }
977
978 void DolphinView::applyViewProperties(const KUrl& url)
979 {
980 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
981 // The column view is active, hence don't apply the view properties
982 // of sub directories (represented by columns) to the view. The
983 // view always represents the properties of the first column.
984 return;
985 }
986
987 const ViewProperties props(url);
988
989 const Mode mode = props.viewMode();
990 if (m_mode != mode) {
991 m_mode = mode;
992 createView();
993 emit modeChanged();
994 }
995 if (itemView() == 0) {
996 createView();
997 }
998 Q_ASSERT(itemView() != 0);
999 Q_ASSERT(m_fileItemDelegate != 0);
1000
1001 const bool showHiddenFiles = props.showHiddenFiles();
1002 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
1003 m_dirLister->setShowingDotFiles(showHiddenFiles);
1004 emit showHiddenFilesChanged();
1005 }
1006
1007 m_storedCategorizedSorting = props.categorizedSorting();
1008 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1009 if (categorized != m_proxyModel->isCategorizedModel()) {
1010 m_proxyModel->setCategorizedModel(categorized);
1011 emit categorizedSortingChanged();
1012 }
1013
1014 const DolphinView::Sorting sorting = props.sorting();
1015 if (sorting != m_proxyModel->sorting()) {
1016 m_proxyModel->setSorting(sorting);
1017 emit sortingChanged(sorting);
1018 }
1019
1020 const Qt::SortOrder sortOrder = props.sortOrder();
1021 if (sortOrder != m_proxyModel->sortOrder()) {
1022 m_proxyModel->setSortOrder(sortOrder);
1023 emit sortOrderChanged(sortOrder);
1024 }
1025
1026 KFileItemDelegate::InformationList info = props.additionalInfo();
1027 if (info != m_fileItemDelegate->showInformation()) {
1028 m_fileItemDelegate->setShowInformation(info);
1029 emit additionalInfoChanged();
1030 }
1031
1032 const bool showPreview = props.showPreview();
1033 if (showPreview != m_showPreview) {
1034 m_showPreview = showPreview;
1035 m_iconManager->setShowPreview(showPreview);
1036 emit showPreviewChanged();
1037 }
1038 }
1039
1040 void DolphinView::createView()
1041 {
1042 deleteView();
1043 Q_ASSERT(m_iconsView == 0);
1044 Q_ASSERT(m_detailsView == 0);
1045 Q_ASSERT(m_columnView == 0);
1046
1047 QAbstractItemView* view = 0;
1048 switch (m_mode) {
1049 case IconsView: {
1050 m_iconsView = new DolphinIconsView(this, m_controller);
1051 view = m_iconsView;
1052 break;
1053 }
1054
1055 case DetailsView:
1056 m_detailsView = new DolphinDetailsView(this, m_controller);
1057 view = m_detailsView;
1058 break;
1059
1060 case ColumnView:
1061 m_columnView = new DolphinColumnView(this, m_controller);
1062 view = m_columnView;
1063 break;
1064 }
1065
1066 Q_ASSERT(view != 0);
1067 view->installEventFilter(this);
1068
1069 m_controller->setItemView(view);
1070
1071 m_fileItemDelegate = new KFileItemDelegate(view);
1072 view->setItemDelegate(m_fileItemDelegate);
1073
1074 view->setModel(m_proxyModel);
1075 if (m_selectionModel != 0) {
1076 view->setSelectionModel(m_selectionModel);
1077 } else {
1078 m_selectionModel = view->selectionModel();
1079 }
1080
1081 // reparent the selection model, as it should not be deleted
1082 // when deleting the model
1083 m_selectionModel->setParent(this);
1084
1085 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1086
1087 new KMimeTypeResolver(view, m_dolphinModel);
1088 m_iconManager = new IconManager(view, m_proxyModel);
1089 m_iconManager->setShowPreview(m_showPreview);
1090
1091 m_topLayout->insertWidget(1, view);
1092
1093 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1094 this, SLOT(emitSelectionChangedSignal()));
1095 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1096 this, SLOT(emitContentsMoved()));
1097 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1098 this, SLOT(emitContentsMoved()));
1099 }
1100
1101 void DolphinView::deleteView()
1102 {
1103 QAbstractItemView* view = itemView();
1104 if (view != 0) {
1105 m_topLayout->removeWidget(view);
1106 view->close();
1107 view->deleteLater();
1108 view = 0;
1109 m_iconsView = 0;
1110 m_detailsView = 0;
1111 m_columnView = 0;
1112 m_fileItemDelegate = 0;
1113 m_iconManager = 0;
1114 }
1115 }
1116
1117 QAbstractItemView* DolphinView::itemView() const
1118 {
1119 if (m_detailsView != 0) {
1120 return m_detailsView;
1121 } else if (m_columnView != 0) {
1122 return m_columnView;
1123 }
1124
1125 return m_iconsView;
1126 }
1127
1128 bool DolphinView::isCutItem(const KFileItem& item) const
1129 {
1130 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1131 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1132
1133 const KUrl& itemUrl = item.url();
1134 KUrl::List::const_iterator it = cutUrls.begin();
1135 const KUrl::List::const_iterator end = cutUrls.end();
1136 while (it != end) {
1137 if (*it == itemUrl) {
1138 return true;
1139 }
1140 ++it;
1141 }
1142
1143 return false;
1144 }
1145
1146 void DolphinView::pasteToUrl(const KUrl& url)
1147 {
1148 QClipboard* clipboard = QApplication::clipboard();
1149 const QMimeData* mimeData = clipboard->mimeData();
1150
1151 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1152 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
1153 KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, url);
1154 emit doingOperation(KonqFileUndoManager::MOVE);
1155 clipboard->clear();
1156 } else {
1157 KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, url);
1158 emit doingOperation(KonqFileUndoManager::COPY);
1159 }
1160 }
1161
1162 #include "dolphinview.moc"