]> 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::mouseReleaseEvent(QMouseEvent* event)
481 {
482 QWidget::mouseReleaseEvent(event);
483 setActive(true);
484 }
485
486 void DolphinView::wheelEvent(QWheelEvent* event)
487 {
488 if (event->modifiers() & Qt::ControlModifier) {
489 const int delta = event->delta();
490 if ((delta > 0) && isZoomInPossible()) {
491 zoomIn();
492 } else if ((delta < 0) && isZoomOutPossible()) {
493 zoomOut();
494 }
495 event->accept();
496 }
497 }
498
499 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
500 {
501 if ((watched == itemView()) && (event->type() == QEvent::FocusIn)) {
502 m_controller->requestActivation();
503 }
504
505 return QWidget::eventFilter(watched, event);
506 }
507
508 void DolphinView::activate()
509 {
510 setActive(true);
511 }
512
513 void DolphinView::triggerItem(const KFileItem& item)
514 {
515 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
516 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
517 // items are selected by the user, hence don't trigger the
518 // item specified by 'index'
519 return;
520 }
521
522 if (item.isNull()) {
523 return;
524 }
525
526 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
527 }
528
529 void DolphinView::emitSelectionChangedSignal()
530 {
531 emit selectionChanged(DolphinView::selectedItems());
532 }
533
534 void DolphinView::loadDirectory(const KUrl& url, bool reload)
535 {
536 if (!url.isValid()) {
537 const QString location(url.pathOrUrl());
538 if (location.isEmpty()) {
539 emit errorMessage(i18nc("@info:status", "The location is empty."));
540 } else {
541 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
542 }
543 return;
544 }
545
546 m_loadingDirectory = true;
547
548 m_dirLister->stop();
549 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
550
551 if (isColumnViewActive()) {
552 // adjusting the directory lister is not enough in the case of the
553 // column view, as each column has its own directory lister internally...
554 if (reload) {
555 m_columnView->reload();
556 } else {
557 m_columnView->showColumn(url);
558 }
559 }
560 }
561
562 KUrl DolphinView::viewPropertiesUrl() const
563 {
564 if (isColumnViewActive()) {
565 return m_columnView->rootUrl();
566 }
567
568 return url();
569 }
570
571 void DolphinView::applyViewProperties(const KUrl& url)
572 {
573 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
574 // The column view is active, hence don't apply the view properties
575 // of sub directories (represented by columns) to the view. The
576 // view always represents the properties of the first column.
577 return;
578 }
579
580 const ViewProperties props(url);
581
582 const Mode mode = props.viewMode();
583 if (m_mode != mode) {
584 m_mode = mode;
585 createView();
586 emit modeChanged();
587 }
588 if (itemView() == 0) {
589 createView();
590 }
591 Q_ASSERT(itemView() != 0);
592 Q_ASSERT(m_fileItemDelegate != 0);
593
594 const bool showHiddenFiles = props.showHiddenFiles();
595 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
596 m_dirLister->setShowingDotFiles(showHiddenFiles);
597 emit showHiddenFilesChanged();
598 }
599
600 m_storedCategorizedSorting = props.categorizedSorting();
601 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
602 if (categorized != m_proxyModel->isCategorizedModel()) {
603 m_proxyModel->setCategorizedModel(categorized);
604 emit categorizedSortingChanged();
605 }
606
607 const DolphinView::Sorting sorting = props.sorting();
608 if (sorting != m_proxyModel->sorting()) {
609 m_proxyModel->setSorting(sorting);
610 emit sortingChanged(sorting);
611 }
612
613 const Qt::SortOrder sortOrder = props.sortOrder();
614 if (sortOrder != m_proxyModel->sortOrder()) {
615 m_proxyModel->setSortOrder(sortOrder);
616 emit sortOrderChanged(sortOrder);
617 }
618
619 KFileItemDelegate::InformationList info = props.additionalInfo();
620 if (info != m_fileItemDelegate->showInformation()) {
621 m_fileItemDelegate->setShowInformation(info);
622 emit additionalInfoChanged();
623 }
624
625 const bool showPreview = props.showPreview();
626 if (showPreview != m_showPreview) {
627 m_showPreview = showPreview;
628 m_iconManager->setShowPreview(showPreview);
629 emit showPreviewChanged();
630 }
631 }
632
633 void DolphinView::changeSelection(const KFileItemList& selection)
634 {
635 clearSelection();
636 if (selection.isEmpty()) {
637 return;
638 }
639 const KUrl& baseUrl = url();
640 KUrl url;
641 QItemSelection new_selection;
642 foreach(const KFileItem& item, selection) {
643 url = item.url().upUrl();
644 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
645 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
646 new_selection.select(index, index);
647 }
648 }
649 itemView()->selectionModel()->select(new_selection,
650 QItemSelectionModel::ClearAndSelect
651 | QItemSelectionModel::Current);
652 }
653
654 void DolphinView::openContextMenu(const QPoint& pos)
655 {
656 KFileItem item;
657
658 const QModelIndex index = itemView()->indexAt(pos);
659 if (index.isValid() && (index.column() == DolphinModel::Name)) {
660 item = fileItem(index);
661 }
662
663 emit requestContextMenu(item, url());
664 }
665
666 void DolphinView::dropUrls(const KUrl::List& urls,
667 const KUrl& destPath,
668 const KFileItem& destItem)
669 {
670 Q_ASSERT(!urls.isEmpty());
671 const KUrl& destination = !destItem.isNull() && destItem.isDir() ?
672 destItem.url() : destPath;
673 const KUrl sourceDir = KUrl(urls.first().directory());
674 if (sourceDir != destination) {
675 dropUrls(urls, destination);
676 }
677 }
678
679 void DolphinView::dropUrls(const KUrl::List& urls,
680 const KUrl& destination)
681 {
682 DolphinDropController dropController(this);
683 // forward doingOperation signal up to the mainwindow
684 connect(&dropController, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)),
685 this, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)));
686 dropController.dropUrls(urls, destination);
687 }
688
689 void DolphinView::updateSorting(DolphinView::Sorting sorting)
690 {
691 ViewProperties props(viewPropertiesUrl());
692 props.setSorting(sorting);
693
694 m_proxyModel->setSorting(sorting);
695
696 emit sortingChanged(sorting);
697 }
698
699 void DolphinView::updateSortOrder(Qt::SortOrder order)
700 {
701 ViewProperties props(viewPropertiesUrl());
702 props.setSortOrder(order);
703
704 m_proxyModel->setSortOrder(order);
705
706 emit sortOrderChanged(order);
707 }
708
709 void DolphinView::toggleSortOrder()
710 {
711 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
712 Qt::DescendingOrder :
713 Qt::AscendingOrder;
714 setSortOrder(order);
715 }
716
717 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
718 {
719 ViewProperties props(viewPropertiesUrl());
720 props.setAdditionalInfo(info);
721 props.save();
722
723 m_fileItemDelegate->setShowInformation(info);
724
725 emit additionalInfoChanged();
726 }
727
728 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
729 {
730 const bool enable = (m_mode == DolphinView::DetailsView) ||
731 (m_mode == DolphinView::IconsView);
732
733 QAction* showSizeInfo = collection->action("show_size_info");
734 QAction* showDateInfo = collection->action("show_date_info");
735 QAction* showPermissionsInfo = collection->action("show_permissions_info");
736 QAction* showOwnerInfo = collection->action("show_owner_info");
737 QAction* showGroupInfo = collection->action("show_group_info");
738 QAction* showMimeInfo = collection->action("show_mime_info");
739
740 showSizeInfo->setChecked(false);
741 showDateInfo->setChecked(false);
742 showPermissionsInfo->setChecked(false);
743 showOwnerInfo->setChecked(false);
744 showGroupInfo->setChecked(false);
745 showMimeInfo->setChecked(false);
746
747 showSizeInfo->setEnabled(enable);
748 showDateInfo->setEnabled(enable);
749 showPermissionsInfo->setEnabled(enable);
750 showOwnerInfo->setEnabled(enable);
751 showGroupInfo->setEnabled(enable);
752 showMimeInfo->setEnabled(enable);
753
754 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
755 switch (info) {
756 case KFileItemDelegate::Size:
757 showSizeInfo->setChecked(true);
758 break;
759 case KFileItemDelegate::ModificationTime:
760 showDateInfo->setChecked(true);
761 break;
762 case KFileItemDelegate::Permissions:
763 showPermissionsInfo->setChecked(true);
764 break;
765 case KFileItemDelegate::Owner:
766 showOwnerInfo->setChecked(true);
767 break;
768 case KFileItemDelegate::OwnerAndGroup:
769 showGroupInfo->setChecked(true);
770 break;
771 case KFileItemDelegate::FriendlyMimeType:
772 showMimeInfo->setChecked(true);
773 break;
774 default:
775 break;
776 }
777 }
778 }
779
780 void DolphinView::toggleAdditionalInfo(QAction* action)
781 {
782 const KFileItemDelegate::Information info =
783 static_cast<KFileItemDelegate::Information>(action->data().toInt());
784
785 KFileItemDelegate::InformationList list = additionalInfo();
786
787 const bool show = action->isChecked();
788
789 const int index = list.indexOf(info);
790 const bool containsInfo = (index >= 0);
791 if (show && !containsInfo) {
792 list.append(info);
793 setAdditionalInfo(list);
794 } else if (!show && containsInfo) {
795 list.removeAt(index);
796 setAdditionalInfo(list);
797 Q_ASSERT(list.indexOf(info) < 0);
798 }
799 }
800
801 void DolphinView::emitContentsMoved()
802 {
803 // only emit the contents moved signal if:
804 // - no directory loading is ongoing (this would reset the contents position
805 // always to (0, 0))
806 // - if the Column View is active: the column view does an automatic
807 // positioning during the loading operation, which must be remembered
808 if (!m_loadingDirectory || isColumnViewActive()) {
809 const QPoint pos(contentsPosition());
810 emit contentsMoved(pos.x(), pos.y());
811 }
812 }
813
814 void DolphinView::showHoverInformation(const KFileItem& item)
815 {
816 emit requestItemInfo(item);
817 }
818
819 void DolphinView::clearHoverInformation()
820 {
821 emit requestItemInfo(KFileItem());
822 }
823
824 void DolphinView::createView()
825 {
826 deleteView();
827 Q_ASSERT(m_iconsView == 0);
828 Q_ASSERT(m_detailsView == 0);
829 Q_ASSERT(m_columnView == 0);
830
831 QAbstractItemView* view = 0;
832 switch (m_mode) {
833 case IconsView: {
834 m_iconsView = new DolphinIconsView(this, m_controller);
835 view = m_iconsView;
836 break;
837 }
838
839 case DetailsView:
840 m_detailsView = new DolphinDetailsView(this, m_controller);
841 view = m_detailsView;
842 break;
843
844 case ColumnView:
845 m_columnView = new DolphinColumnView(this, m_controller);
846 view = m_columnView;
847 break;
848 }
849
850 Q_ASSERT(view != 0);
851 view->installEventFilter(this);
852
853 m_controller->setItemView(view);
854
855 m_fileItemDelegate = new KFileItemDelegate(view);
856 view->setItemDelegate(m_fileItemDelegate);
857
858 view->setModel(m_proxyModel);
859 if (m_selectionModel != 0) {
860 view->setSelectionModel(m_selectionModel);
861 } else {
862 m_selectionModel = view->selectionModel();
863 }
864
865 // reparent the selection model, as it should not be deleted
866 // when deleting the model
867 m_selectionModel->setParent(this);
868
869 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
870
871 new KMimeTypeResolver(view, m_dolphinModel);
872 m_iconManager = new IconManager(view, m_proxyModel);
873 m_iconManager->setShowPreview(m_showPreview);
874
875 m_topLayout->insertWidget(1, view);
876
877 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
878 this, SLOT(emitSelectionChangedSignal()));
879 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
880 this, SLOT(emitContentsMoved()));
881 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
882 this, SLOT(emitContentsMoved()));
883 }
884
885 void DolphinView::deleteView()
886 {
887 QAbstractItemView* view = itemView();
888 if (view != 0) {
889 m_topLayout->removeWidget(view);
890 view->close();
891 view->deleteLater();
892 view = 0;
893 m_iconsView = 0;
894 m_detailsView = 0;
895 m_columnView = 0;
896 m_fileItemDelegate = 0;
897 m_iconManager = 0;
898 }
899 }
900
901 QAbstractItemView* DolphinView::itemView() const
902 {
903 if (m_detailsView != 0) {
904 return m_detailsView;
905 } else if (m_columnView != 0) {
906 return m_columnView;
907 }
908
909 return m_iconsView;
910 }
911
912 bool DolphinView::isCutItem(const KFileItem& item) const
913 {
914 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
915 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
916
917 const KUrl& itemUrl = item.url();
918 KUrl::List::const_iterator it = cutUrls.begin();
919 const KUrl::List::const_iterator end = cutUrls.end();
920 while (it != end) {
921 if (*it == itemUrl) {
922 return true;
923 }
924 ++it;
925 }
926
927 return false;
928 }
929
930 void DolphinView::renameSelectedItems()
931 {
932 const KFileItemList items = selectedItems();
933 if (items.count() > 1) {
934 // More than one item has been selected for renaming. Open
935 // a rename dialog and rename all items afterwards.
936 RenameDialog dialog(this, items);
937 if (dialog.exec() == QDialog::Rejected) {
938 return;
939 }
940
941 const QString newName = dialog.newName();
942 if (newName.isEmpty()) {
943 emit errorMessage(dialog.errorString());
944 } else {
945 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
946 // as one operation instead of n rename operations like it is done now...
947 Q_ASSERT(newName.contains('#'));
948
949 // iterate through all selected items and rename them...
950 int index = 1;
951 foreach (KFileItem item, items) {
952 const KUrl& oldUrl = item.url();
953 QString number;
954 number.setNum(index++);
955
956 QString name = newName;
957 name.replace('#', number);
958
959 if (oldUrl.fileName() != name) {
960 KUrl newUrl = oldUrl;
961 newUrl.setFileName(name);
962 KonqOperations::rename(this, oldUrl, newUrl);
963 emit doingOperation(KonqFileUndoManager::RENAME);
964 }
965 }
966 }
967 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
968 Q_ASSERT(items.count() == 1);
969
970 if (isColumnViewActive()) {
971 m_columnView->editItem(items.first());
972 } else {
973 const QModelIndex dirIndex = m_dolphinModel->indexForItem(items.first());
974 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
975 itemView()->edit(proxyIndex);
976 }
977 } else {
978 Q_ASSERT(items.count() == 1);
979
980 RenameDialog dialog(this, items);
981 if (dialog.exec() == QDialog::Rejected) {
982 return;
983 }
984
985 const QString& newName = dialog.newName();
986 if (newName.isEmpty()) {
987 emit errorMessage(dialog.errorString());
988 } else {
989 const KUrl& oldUrl = items.first().url();
990 KUrl newUrl = oldUrl;
991 newUrl.setFileName(newName);
992 KonqOperations::rename(this, oldUrl, newUrl);
993 emit doingOperation(KonqFileUndoManager::RENAME);
994 }
995 }
996 }
997
998 void DolphinView::trashSelectedItems()
999 {
1000 emit doingOperation(KonqFileUndoManager::TRASH);
1001 KonqOperations::del(this, KonqOperations::TRASH, selectedUrls());
1002 }
1003
1004 void DolphinView::deleteSelectedItems()
1005 {
1006 const KUrl::List list = selectedUrls();
1007 const bool del = KonqOperations::askDeleteConfirmation(list,
1008 KonqOperations::DEL,
1009 KonqOperations::DEFAULT_CONFIRMATION,
1010 this);
1011
1012 if (del) {
1013 KIO::Job* job = KIO::del(list);
1014 connect(job, SIGNAL(result(KJob*)),
1015 this, SLOT(slotDeleteFileFinished(KJob*)));
1016 }
1017 }
1018
1019 void DolphinView::slotDeleteFileFinished(KJob* job)
1020 {
1021 if (job->error() == 0) {
1022 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1023 } else {
1024 emit errorMessage(job->errorString());
1025 }
1026 }
1027
1028 void DolphinView::cutSelectedItems()
1029 {
1030 QMimeData* mimeData = new QMimeData();
1031 const KUrl::List kdeUrls = selectedUrls();
1032 const KUrl::List mostLocalUrls;
1033 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
1034 QApplication::clipboard()->setMimeData(mimeData);
1035 }
1036
1037 void DolphinView::copySelectedItems()
1038 {
1039 QMimeData* mimeData = new QMimeData();
1040 const KUrl::List kdeUrls = selectedUrls();
1041 const KUrl::List mostLocalUrls;
1042 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
1043 QApplication::clipboard()->setMimeData(mimeData);
1044 }
1045
1046 void DolphinView::paste()
1047 {
1048 QClipboard* clipboard = QApplication::clipboard();
1049 const QMimeData* mimeData = clipboard->mimeData();
1050
1051 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1052 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
1053 KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, url());
1054 emit doingOperation(KonqFileUndoManager::MOVE);
1055 clipboard->clear();
1056 } else {
1057 KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, url());
1058 emit doingOperation(KonqFileUndoManager::COPY);
1059 }
1060 }
1061
1062 void DolphinView::setShowPreview(bool show)
1063 {
1064 if (m_showPreview == show) {
1065 return;
1066 }
1067
1068 const KUrl viewPropsUrl = viewPropertiesUrl();
1069 ViewProperties props(viewPropsUrl);
1070 props.setShowPreview(show);
1071
1072 m_showPreview = show;
1073 m_iconManager->setShowPreview(show);
1074 emit showPreviewChanged();
1075
1076 loadDirectory(viewPropsUrl);
1077 }
1078
1079 void DolphinView::setShowHiddenFiles(bool show)
1080 {
1081 if (m_dirLister->showingDotFiles() == show) {
1082 return;
1083 }
1084
1085 const KUrl viewPropsUrl = viewPropertiesUrl();
1086 ViewProperties props(viewPropsUrl);
1087 props.setShowHiddenFiles(show);
1088
1089 m_dirLister->setShowingDotFiles(show);
1090 emit showHiddenFilesChanged();
1091
1092 loadDirectory(viewPropsUrl);
1093 }
1094
1095 void DolphinView::setCategorizedSorting(bool categorized)
1096 {
1097 if (categorized == categorizedSorting()) {
1098 return;
1099 }
1100
1101 // setCategorizedSorting(true) may only get invoked
1102 // if the view supports categorized sorting
1103 Q_ASSERT(!categorized || supportsCategorizedSorting());
1104
1105 ViewProperties props(viewPropertiesUrl());
1106 props.setCategorizedSorting(categorized);
1107 props.save();
1108
1109 m_storedCategorizedSorting = categorized;
1110 m_proxyModel->setCategorizedModel(categorized);
1111
1112 emit categorizedSortingChanged();
1113 }
1114
1115 QPair<bool, QString> DolphinView::pasteInfo() const
1116 {
1117 QPair<bool, QString> ret;
1118 QClipboard* clipboard = QApplication::clipboard();
1119 const QMimeData* mimeData = clipboard->mimeData();
1120
1121 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1122 if (!urls.isEmpty()) {
1123 ret.first = true;
1124 if (urls.count() == 1) {
1125 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, urls.first(), true);
1126 ret.second = item.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
1127 i18nc("@action:inmenu", "Paste One File");
1128
1129 } else {
1130 ret.second = i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls.count());
1131 }
1132 } else {
1133 ret.first = false;
1134 ret.second = i18nc("@action:inmenu", "Paste");
1135 }
1136
1137 return ret;
1138 }
1139
1140 #include "dolphinview.moc"