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