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