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