]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
svn merge -r764536:HEAD from 4.0 branch: refactor "additional info" actions and provi...
[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 <kmimetyperesolver.h>
43 #include <konq_operations.h>
44 #include <konqmimedata.h>
45 #include <ktoggleaction.h>
46 #include <kurl.h>
47
48 #include "dolphindropcontroller.h"
49 #include "dolphinmodel.h"
50 #include "dolphincolumnview.h"
51 #include "dolphincontroller.h"
52 #include "dolphinsortfilterproxymodel.h"
53 #include "dolphindetailsview.h"
54 #include "dolphiniconsview.h"
55 #include "dolphinsettings.h"
56 #include "dolphin_generalsettings.h"
57 #include "iconmanager.h"
58 #include "renamedialog.h"
59 #include "viewproperties.h"
60
61 DolphinView::DolphinView(QWidget* parent,
62 const KUrl& url,
63 KDirLister* dirLister,
64 DolphinModel* dolphinModel,
65 DolphinSortFilterProxyModel* proxyModel) :
66 QWidget(parent),
67 m_active(true),
68 m_showPreview(false),
69 m_loadingDirectory(false),
70 m_storedCategorizedSorting(false),
71 m_mode(DolphinView::IconsView),
72 m_topLayout(0),
73 m_controller(0),
74 m_iconsView(0),
75 m_detailsView(0),
76 m_columnView(0),
77 m_fileItemDelegate(0),
78 m_selectionModel(0),
79 m_dolphinModel(dolphinModel),
80 m_dirLister(dirLister),
81 m_proxyModel(proxyModel),
82 m_iconManager(0)
83 {
84 setFocusPolicy(Qt::StrongFocus);
85 m_topLayout = new QVBoxLayout(this);
86 m_topLayout->setSpacing(0);
87 m_topLayout->setMargin(0);
88
89 connect(m_dirLister, SIGNAL(completed()),
90 this, SLOT(updateCutItems()));
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 if (isColumnViewActive()) {
189 // When changing the mode in the column view, it makes sense
190 // to go back to the root URL of the column view automatically.
191 // Otherwise there it would not be possible to turn off the column view
192 // without focusing the first column.
193 const KUrl root = rootUrl();
194 setUrl(root);
195 m_controller->setUrl(root);
196 }
197
198 deleteView();
199
200 const KUrl viewPropsUrl = viewPropertiesUrl();
201 ViewProperties props(viewPropsUrl);
202 props.setViewMode(m_mode);
203 createView();
204
205 // the file item delegate has been recreated, apply the current
206 // additional information manually
207 const KFileItemDelegate::InformationList infoList = props.additionalInfo();
208 m_fileItemDelegate->setShowInformation(infoList);
209 emit additionalInfoChanged();
210
211 // Not all view modes support categorized sorting. Adjust the sorting model
212 // if changing the view mode results in a change of the categorized sorting
213 // capabilities.
214 m_storedCategorizedSorting = props.categorizedSorting();
215 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
216 if (categorized != m_proxyModel->isCategorizedModel()) {
217 m_proxyModel->setCategorizedModel(categorized);
218 emit categorizedSortingChanged();
219 }
220
221 emit modeChanged();
222 }
223
224 DolphinView::Mode DolphinView::mode() const
225 {
226 return m_mode;
227 }
228
229 void DolphinView::setShowPreview(bool show)
230 {
231 if (m_showPreview == show) {
232 return;
233 }
234
235 const KUrl viewPropsUrl = viewPropertiesUrl();
236 ViewProperties props(viewPropsUrl);
237 props.setShowPreview(show);
238
239 m_showPreview = show;
240 m_iconManager->setShowPreview(show);
241 emit showPreviewChanged();
242
243 loadDirectory(viewPropsUrl, true);
244 }
245
246 bool DolphinView::showPreview() const
247 {
248 return m_showPreview;
249 }
250
251 void DolphinView::setShowHiddenFiles(bool show)
252 {
253 if (m_dirLister->showingDotFiles() == show) {
254 return;
255 }
256
257 const KUrl viewPropsUrl = viewPropertiesUrl();
258 ViewProperties props(viewPropsUrl);
259 props.setShowHiddenFiles(show);
260
261 m_dirLister->setShowingDotFiles(show);
262 emit showHiddenFilesChanged();
263
264 loadDirectory(viewPropsUrl, true);
265 }
266
267 bool DolphinView::showHiddenFiles() const
268 {
269 return m_dirLister->showingDotFiles();
270 }
271
272 void DolphinView::setCategorizedSorting(bool categorized)
273 {
274 if (categorized == categorizedSorting()) {
275 return;
276 }
277
278 // setCategorizedSorting(true) may only get invoked
279 // if the view supports categorized sorting
280 Q_ASSERT(!categorized || supportsCategorizedSorting());
281
282 ViewProperties props(viewPropertiesUrl());
283 props.setCategorizedSorting(categorized);
284 props.save();
285
286 m_storedCategorizedSorting = categorized;
287 m_proxyModel->setCategorizedModel(categorized);
288
289 emit categorizedSortingChanged();
290 }
291
292 bool DolphinView::categorizedSorting() const
293 {
294 // If all view modes would support categorized sorting, returning
295 // m_proxyModel->isCategorizedModel() would be the way to go. As
296 // currently only the icons view supports caterized sorting, we remember
297 // the stored view properties state in m_storedCategorizedSorting and
298 // return this state. The application takes care to disable the corresponding
299 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
300 // that this setting is not applied to the current view mode.
301 return m_storedCategorizedSorting;
302 }
303
304 bool DolphinView::supportsCategorizedSorting() const
305 {
306 return m_iconsView != 0;
307 }
308
309 void DolphinView::selectAll()
310 {
311 QAbstractItemView* view = itemView();
312 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
313 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
314 // selection instead of selecting all items. This is bypassed for KDE 4.0
315 // by invoking clearSelection() first.
316 view->clearSelection();
317 view->selectAll();
318 }
319
320 void DolphinView::invertSelection()
321 {
322 if (isColumnViewActive()) {
323 // QAbstractItemView does not offer a virtual method invertSelection()
324 // as counterpart to QAbstractItemView::selectAll(). This makes it
325 // necessary to delegate the inverting of the selection to the
326 // column view, as only the selection of the active column should
327 // get inverted.
328 m_columnView->invertSelection();
329 } else {
330 QItemSelectionModel* selectionModel = itemView()->selectionModel();
331 const QAbstractItemModel* itemModel = selectionModel->model();
332
333 const QModelIndex topLeft = itemModel->index(0, 0);
334 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
335 itemModel->columnCount() - 1);
336
337 const QItemSelection selection(topLeft, bottomRight);
338 selectionModel->select(selection, QItemSelectionModel::Toggle);
339 }
340 }
341
342 bool DolphinView::hasSelection() const
343 {
344 return itemView()->selectionModel()->hasSelection();
345 }
346
347 void DolphinView::clearSelection()
348 {
349 itemView()->selectionModel()->clear();
350 }
351
352 KFileItemList DolphinView::selectedItems() const
353 {
354 const QAbstractItemView* view = itemView();
355
356 // Our view has a selection, we will map them back to the DolphinModel
357 // and then fill the KFileItemList.
358 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
359
360 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
361 KFileItemList itemList;
362
363 const QModelIndexList indexList = selection.indexes();
364 foreach (QModelIndex index, indexList) {
365 KFileItem item = m_dolphinModel->itemForIndex(index);
366 if (!item.isNull()) {
367 itemList.append(item);
368 }
369 }
370
371 return itemList;
372 }
373
374 KUrl::List DolphinView::selectedUrls() const
375 {
376 KUrl::List urls;
377 const KFileItemList list = selectedItems();
378 foreach (KFileItem item, list) {
379 urls.append(item.url());
380 }
381 return urls;
382 }
383
384 KFileItem DolphinView::fileItem(const QModelIndex& index) const
385 {
386 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
387 return m_dolphinModel->itemForIndex(dolphinModelIndex);
388 }
389
390 void DolphinView::setContentsPosition(int x, int y)
391 {
392 QAbstractItemView* view = itemView();
393
394 // the ColumnView takes care itself for the horizontal scrolling
395 if (!isColumnViewActive()) {
396 view->horizontalScrollBar()->setValue(x);
397 }
398 view->verticalScrollBar()->setValue(y);
399
400 m_loadingDirectory = false;
401 }
402
403 QPoint DolphinView::contentsPosition() const
404 {
405 const int x = itemView()->horizontalScrollBar()->value();
406 const int y = itemView()->verticalScrollBar()->value();
407 return QPoint(x, y);
408 }
409
410 void DolphinView::zoomIn()
411 {
412 m_controller->triggerZoomIn();
413 }
414
415 void DolphinView::zoomOut()
416 {
417 m_controller->triggerZoomOut();
418 }
419
420 bool DolphinView::isZoomInPossible() const
421 {
422 return m_controller->isZoomInPossible();
423 }
424
425 bool DolphinView::isZoomOutPossible() const
426 {
427 return m_controller->isZoomOutPossible();
428 }
429
430 void DolphinView::setSorting(Sorting sorting)
431 {
432 if (sorting != this->sorting()) {
433 updateSorting(sorting);
434 }
435 }
436
437 DolphinView::Sorting DolphinView::sorting() const
438 {
439 return m_proxyModel->sorting();
440 }
441
442 void DolphinView::setSortOrder(Qt::SortOrder order)
443 {
444 if (sortOrder() != order) {
445 updateSortOrder(order);
446 }
447 }
448
449 Qt::SortOrder DolphinView::sortOrder() const
450 {
451 return m_proxyModel->sortOrder();
452 }
453
454 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
455 {
456 const KUrl viewPropsUrl = viewPropertiesUrl();
457 ViewProperties props(viewPropsUrl);
458 props.setAdditionalInfo(info);
459 m_fileItemDelegate->setShowInformation(info);
460
461 emit additionalInfoChanged();
462
463 if (itemView() != m_detailsView) {
464 // the details view requires no reloading of the directory, as it maps
465 // the file item delegate info to its columns internally
466 loadDirectory(viewPropsUrl, true);
467 }
468 }
469
470 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
471 {
472 return m_fileItemDelegate->showInformation();
473 }
474
475 void DolphinView::reload()
476 {
477 setUrl(url());
478 loadDirectory(url(), true);
479 }
480
481 void DolphinView::refresh()
482 {
483 const bool oldActivationState = m_active;
484 m_active = true;
485
486 createView();
487 applyViewProperties(m_controller->url());
488 reload();
489
490 setActive(oldActivationState);
491 }
492
493 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
494 {
495 if (m_controller->url() == url) {
496 return;
497 }
498
499 m_controller->setUrl(url); // emits urlChanged, which we forward
500
501 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
502 applyViewProperties(rootUrl);
503 loadDirectory(rootUrl);
504 if (itemView() == m_columnView) {
505 m_columnView->setRootUrl(rootUrl);
506 m_columnView->showColumn(url);
507 }
508 } else {
509 applyViewProperties(url);
510 loadDirectory(url);
511 }
512
513 emit startedPathLoading(url);
514 }
515
516 void DolphinView::setNameFilter(const QString& nameFilter)
517 {
518 m_proxyModel->setFilterRegExp(nameFilter);
519
520 if (isColumnViewActive()) {
521 // adjusting the directory lister is not enough in the case of the
522 // column view, as each column has its own directory lister internally...
523 m_columnView->setNameFilter(nameFilter);
524 }
525 }
526
527 void DolphinView::calculateItemCount(int& fileCount, int& folderCount)
528 {
529 foreach (KFileItem item, m_dirLister->items()) {
530 if (item.isDir()) {
531 ++folderCount;
532 } else {
533 ++fileCount;
534 }
535 }
536 }
537
538 void DolphinView::setUrl(const KUrl& url)
539 {
540 updateView(url, KUrl());
541 }
542
543 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
544 {
545 QWidget::mouseReleaseEvent(event);
546 setActive(true);
547 }
548 void DolphinView::activate()
549 {
550 setActive(true);
551 }
552
553 void DolphinView::triggerItem(const KFileItem& item)
554 {
555 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
556 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
557 // items are selected by the user, hence don't trigger the
558 // item specified by 'index'
559 return;
560 }
561
562 if (item.isNull()) {
563 return;
564 }
565
566 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
567 }
568
569 void DolphinView::emitSelectionChangedSignal()
570 {
571 emit selectionChanged(DolphinView::selectedItems());
572 }
573
574 void DolphinView::loadDirectory(const KUrl& url, bool reload)
575 {
576 if (!url.isValid()) {
577 const QString location(url.pathOrUrl());
578 if (location.isEmpty()) {
579 emit errorMessage(i18nc("@info:status", "The location is empty."));
580 } else {
581 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
582 }
583 return;
584 }
585
586 m_loadingDirectory = true;
587
588 m_dirLister->stop();
589 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
590
591 if (isColumnViewActive()) {
592 // adjusting the directory lister is not enough in the case of the
593 // column view, as each column has its own directory lister internally...
594 if (reload) {
595 m_columnView->reload();
596 } else {
597 m_columnView->showColumn(url);
598 }
599 }
600 }
601
602 KUrl DolphinView::viewPropertiesUrl() const
603 {
604 if (isColumnViewActive()) {
605 return m_dirLister->url();
606 }
607
608 return url();
609 }
610
611 void DolphinView::applyViewProperties(const KUrl& url)
612 {
613 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
614 // The column view is active, hence don't apply the view properties
615 // of sub directories (represented by columns) to the view. The
616 // view always represents the properties of the first column.
617 return;
618 }
619
620 const ViewProperties props(url);
621
622 const Mode mode = props.viewMode();
623 if (m_mode != mode) {
624 m_mode = mode;
625 createView();
626 emit modeChanged();
627 }
628 if (itemView() == 0) {
629 createView();
630 }
631 Q_ASSERT(itemView() != 0);
632 Q_ASSERT(m_fileItemDelegate != 0);
633
634 const bool showHiddenFiles = props.showHiddenFiles();
635 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
636 m_dirLister->setShowingDotFiles(showHiddenFiles);
637 emit showHiddenFilesChanged();
638 }
639
640 m_storedCategorizedSorting = props.categorizedSorting();
641 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
642 if (categorized != m_proxyModel->isCategorizedModel()) {
643 m_proxyModel->setCategorizedModel(categorized);
644 emit categorizedSortingChanged();
645 }
646
647 const DolphinView::Sorting sorting = props.sorting();
648 if (sorting != m_proxyModel->sorting()) {
649 m_proxyModel->setSorting(sorting);
650 emit sortingChanged(sorting);
651 }
652
653 const Qt::SortOrder sortOrder = props.sortOrder();
654 if (sortOrder != m_proxyModel->sortOrder()) {
655 m_proxyModel->setSortOrder(sortOrder);
656 emit sortOrderChanged(sortOrder);
657 }
658
659 KFileItemDelegate::InformationList info = props.additionalInfo();
660 if (info != m_fileItemDelegate->showInformation()) {
661 m_fileItemDelegate->setShowInformation(info);
662 emit additionalInfoChanged();
663 }
664
665 const bool showPreview = props.showPreview();
666 if (showPreview != m_showPreview) {
667 m_showPreview = showPreview;
668 m_iconManager->setShowPreview(showPreview);
669 emit showPreviewChanged();
670 }
671 }
672
673 void DolphinView::changeSelection(const KFileItemList& selection)
674 {
675 clearSelection();
676 if (selection.isEmpty()) {
677 return;
678 }
679 const KUrl& baseUrl = url();
680 KUrl url;
681 QItemSelection new_selection;
682 foreach(const KFileItem& item, selection) {
683 url = item.url().upUrl();
684 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
685 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
686 new_selection.select(index, index);
687 }
688 }
689 itemView()->selectionModel()->select(new_selection,
690 QItemSelectionModel::ClearAndSelect
691 | QItemSelectionModel::Current);
692 }
693
694 void DolphinView::openContextMenu(const QPoint& pos)
695 {
696 KFileItem item;
697
698 const QModelIndex index = itemView()->indexAt(pos);
699 if (index.isValid() && (index.column() == DolphinModel::Name)) {
700 item = fileItem(index);
701 }
702
703 emit requestContextMenu(item, url());
704 }
705
706 void DolphinView::dropUrls(const KUrl::List& urls,
707 const KUrl& destPath,
708 const KFileItem& destItem)
709 {
710 Q_ASSERT(!urls.isEmpty());
711 const KUrl& destination = !destItem.isNull() && destItem.isDir() ?
712 destItem.url() : destPath;
713 const KUrl sourceDir = KUrl(urls.first().directory());
714 if (sourceDir != destination) {
715 dropUrls(urls, destination);
716 }
717 }
718
719 void DolphinView::dropUrls(const KUrl::List& urls,
720 const KUrl& destination)
721 {
722 DolphinDropController dropController(this);
723 // forward doingOperation signal up to the mainwindow
724 connect(&dropController, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)),
725 this, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)));
726 dropController.dropUrls(urls, destination);
727 }
728
729 void DolphinView::updateSorting(DolphinView::Sorting sorting)
730 {
731 ViewProperties props(viewPropertiesUrl());
732 props.setSorting(sorting);
733
734 m_proxyModel->setSorting(sorting);
735
736 emit sortingChanged(sorting);
737 }
738
739 void DolphinView::updateSortOrder(Qt::SortOrder order)
740 {
741 ViewProperties props(viewPropertiesUrl());
742 props.setSortOrder(order);
743
744 m_proxyModel->setSortOrder(order);
745
746 emit sortOrderChanged(order);
747 }
748
749 void DolphinView::toggleSortOrder()
750 {
751 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
752 Qt::DescendingOrder :
753 Qt::AscendingOrder;
754 setSortOrder(order);
755 }
756
757 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
758 {
759 ViewProperties props(viewPropertiesUrl());
760 props.setAdditionalInfo(info);
761 props.save();
762
763 m_fileItemDelegate->setShowInformation(info);
764
765 emit additionalInfoChanged(); // will call updateAdditionalInfoActions just below
766 }
767
768 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
769 {
770 const bool enable = (m_mode == DolphinView::DetailsView) ||
771 (m_mode == DolphinView::IconsView);
772
773 QAction* showSizeInfo = collection->action("show_size_info");
774 QAction* showDateInfo = collection->action("show_date_info");
775 QAction* showPermissionsInfo = collection->action("show_permissions_info");
776 QAction* showOwnerInfo = collection->action("show_owner_info");
777 QAction* showGroupInfo = collection->action("show_group_info");
778 QAction* showMimeInfo = collection->action("show_mime_info");
779
780 showSizeInfo->setChecked(false);
781 showDateInfo->setChecked(false);
782 showPermissionsInfo->setChecked(false);
783 showOwnerInfo->setChecked(false);
784 showGroupInfo->setChecked(false);
785 showMimeInfo->setChecked(false);
786
787 showSizeInfo->setEnabled(enable);
788 showDateInfo->setEnabled(enable);
789 showPermissionsInfo->setEnabled(enable);
790 showOwnerInfo->setEnabled(enable);
791 showGroupInfo->setEnabled(enable);
792 showMimeInfo->setEnabled(enable);
793
794 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
795 switch (info) {
796 case KFileItemDelegate::Size:
797 showSizeInfo->setChecked(true);
798 break;
799 case KFileItemDelegate::ModificationTime:
800 showDateInfo->setChecked(true);
801 break;
802 case KFileItemDelegate::Permissions:
803 showPermissionsInfo->setChecked(true);
804 break;
805 case KFileItemDelegate::Owner:
806 showOwnerInfo->setChecked(true);
807 break;
808 case KFileItemDelegate::OwnerAndGroup:
809 showGroupInfo->setChecked(true);
810 break;
811 case KFileItemDelegate::FriendlyMimeType:
812 showMimeInfo->setChecked(true);
813 break;
814 default:
815 break;
816 }
817 }
818 }
819
820 void DolphinView::toggleAdditionalInfo(QAction* action)
821 {
822 const KFileItemDelegate::Information info =
823 static_cast<KFileItemDelegate::Information>(action->data().toInt());
824
825 KFileItemDelegate::InformationList list = additionalInfo();
826
827 const bool show = action->isChecked();
828
829 const int index = list.indexOf(info);
830 const bool containsInfo = (index >= 0);
831 if (show && !containsInfo) {
832 list.append(info);
833 setAdditionalInfo(list);
834 } else if (!show && containsInfo) {
835 list.removeAt(index);
836 setAdditionalInfo(list);
837 Q_ASSERT(list.indexOf(info) < 0);
838 }
839 }
840
841 void DolphinView::emitContentsMoved()
842 {
843 // only emit the contents moved signal if:
844 // - no directory loading is ongoing (this would reset the contents position
845 // always to (0, 0))
846 // - if the Column View is active: the column view does an automatic
847 // positioning during the loading operation, which must be remembered
848 if (!m_loadingDirectory || isColumnViewActive()) {
849 const QPoint pos(contentsPosition());
850 emit contentsMoved(pos.x(), pos.y());
851 }
852 }
853
854 void DolphinView::showHoverInformation(const KFileItem& item)
855 {
856 if (hasSelection() || !m_active) {
857 return;
858 }
859
860 emit requestItemInfo(item);
861 }
862
863 void DolphinView::clearHoverInformation()
864 {
865 if (m_active) {
866 emit requestItemInfo(KFileItem());
867 }
868 }
869
870 void DolphinView::createView()
871 {
872 deleteView();
873 Q_ASSERT(m_iconsView == 0);
874 Q_ASSERT(m_detailsView == 0);
875 Q_ASSERT(m_columnView == 0);
876
877 QAbstractItemView* view = 0;
878 switch (m_mode) {
879 case IconsView: {
880 m_iconsView = new DolphinIconsView(this, m_controller);
881 view = m_iconsView;
882 break;
883 }
884
885 case DetailsView:
886 m_detailsView = new DolphinDetailsView(this, m_controller);
887 view = m_detailsView;
888 break;
889
890 case ColumnView:
891 m_columnView = new DolphinColumnView(this, m_controller);
892 view = m_columnView;
893 break;
894 }
895
896 Q_ASSERT(view != 0);
897
898 m_fileItemDelegate = new KFileItemDelegate(view);
899 view->setItemDelegate(m_fileItemDelegate);
900
901 view->setModel(m_proxyModel);
902 if (m_selectionModel != 0) {
903 view->setSelectionModel(m_selectionModel);
904 } else {
905 m_selectionModel = view->selectionModel();
906 }
907
908 // reparent the selection model, as it should not be deleted
909 // when deleting the model
910 m_selectionModel->setParent(this);
911
912 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
913
914 new KMimeTypeResolver(view, m_dolphinModel);
915 m_iconManager = new IconManager(view, m_proxyModel);
916 m_iconManager->setShowPreview(m_showPreview);
917
918 m_topLayout->insertWidget(1, view);
919
920 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
921 this, SLOT(emitSelectionChangedSignal()));
922 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
923 this, SLOT(emitContentsMoved()));
924 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
925 this, SLOT(emitContentsMoved()));
926 }
927
928 void DolphinView::deleteView()
929 {
930 QAbstractItemView* view = itemView();
931 if (view != 0) {
932 m_topLayout->removeWidget(view);
933 view->close();
934 view->deleteLater();
935 view = 0;
936 m_iconsView = 0;
937 m_detailsView = 0;
938 m_columnView = 0;
939 m_fileItemDelegate = 0;
940 m_iconManager = 0;
941 }
942 }
943
944 QAbstractItemView* DolphinView::itemView() const
945 {
946 if (m_detailsView != 0) {
947 return m_detailsView;
948 } else if (m_columnView != 0) {
949 return m_columnView;
950 }
951
952 return m_iconsView;
953 }
954
955 bool DolphinView::isCutItem(const KFileItem& item) const
956 {
957 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
958 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
959
960 const KUrl& itemUrl = item.url();
961 KUrl::List::const_iterator it = cutUrls.begin();
962 const KUrl::List::const_iterator end = cutUrls.end();
963 while (it != end) {
964 if (*it == itemUrl) {
965 return true;
966 }
967 ++it;
968 }
969
970 return false;
971 }
972
973 KToggleAction* DolphinView::iconsModeAction(KActionCollection* actionCollection)
974 {
975 KToggleAction* iconsView = actionCollection->add<KToggleAction>("icons");
976 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
977 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
978 iconsView->setIcon(KIcon("view-list-icons"));
979 iconsView->setData(QVariant::fromValue(IconsView));
980 return iconsView;
981 }
982
983 KToggleAction* DolphinView::detailsModeAction(KActionCollection* actionCollection)
984 {
985 KToggleAction* detailsView = actionCollection->add<KToggleAction>("details");
986 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
987 detailsView->setShortcut(Qt::CTRL | Qt::Key_2);
988 detailsView->setIcon(KIcon("view-list-details"));
989 detailsView->setData(QVariant::fromValue(DetailsView));
990 return detailsView;
991 }
992
993 KToggleAction* DolphinView::columnsModeAction(KActionCollection* actionCollection)
994 {
995 KToggleAction* columnView = actionCollection->add<KToggleAction>("columns");
996 columnView->setText(i18nc("@action:inmenu View Mode", "Columns"));
997 columnView->setShortcut(Qt::CTRL | Qt::Key_3);
998 columnView->setIcon(KIcon("view-file-columns"));
999 columnView->setData(QVariant::fromValue(ColumnView));
1000 return columnView;
1001 }
1002
1003 QString DolphinView::currentViewModeActionName() const
1004 {
1005 switch (m_mode) {
1006 case DolphinView::IconsView:
1007 return "icons";
1008 case DolphinView::DetailsView:
1009 return "details";
1010 case DolphinView::ColumnView:
1011 return "columns";
1012 }
1013 return QString(); // can't happen
1014 }
1015
1016 void DolphinView::renameSelectedItems()
1017 {
1018 const KFileItemList items = selectedItems();
1019 if (items.count() > 1) {
1020 // More than one item has been selected for renaming. Open
1021 // a rename dialog and rename all items afterwards.
1022 RenameDialog dialog(this, items);
1023 if (dialog.exec() == QDialog::Rejected) {
1024 return;
1025 }
1026
1027 const QString newName = dialog.newName();
1028 if (newName.isEmpty()) {
1029 emit errorMessage(dialog.errorString());
1030 } else {
1031 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1032 // as one operation instead of n rename operations like it is done now...
1033 Q_ASSERT(newName.contains('#'));
1034
1035 // iterate through all selected items and rename them...
1036 int index = 1;
1037 foreach (KFileItem item, items) {
1038 const KUrl& oldUrl = item.url();
1039 QString number;
1040 number.setNum(index++);
1041
1042 QString name = newName;
1043 name.replace('#', number);
1044
1045 if (oldUrl.fileName() != name) {
1046 KUrl newUrl = oldUrl;
1047 newUrl.setFileName(name);
1048 KonqOperations::rename(this, oldUrl, newUrl);
1049 emit doingOperation(KonqFileUndoManager::RENAME);
1050 }
1051 }
1052 }
1053 } else {
1054 // Only one item has been selected for renaming. Use the custom
1055 // renaming mechanism from the views.
1056 Q_ASSERT(items.count() == 1);
1057
1058 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1059 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1060 // is a benefit for the user at all -> let's wait for some input first...
1061 RenameDialog dialog(this, items);
1062 if (dialog.exec() == QDialog::Rejected) {
1063 return;
1064 }
1065
1066 const QString& newName = dialog.newName();
1067 if (newName.isEmpty()) {
1068 emit errorMessage(dialog.errorString());
1069 } else {
1070 const KUrl& oldUrl = items.first().url();
1071 KUrl newUrl = oldUrl;
1072 newUrl.setFileName(newName);
1073 KonqOperations::rename(this, oldUrl, newUrl);
1074 emit doingOperation(KonqFileUndoManager::RENAME);
1075 }
1076 }
1077 }
1078
1079 void DolphinView::trashSelectedItems()
1080 {
1081 emit doingOperation(KonqFileUndoManager::TRASH);
1082 KonqOperations::del(this, KonqOperations::TRASH, selectedUrls());
1083 }
1084
1085 void DolphinView::deleteSelectedItems()
1086 {
1087 const KUrl::List list = selectedUrls();
1088 const bool del = KonqOperations::askDeleteConfirmation(list,
1089 KonqOperations::DEL,
1090 KonqOperations::DEFAULT_CONFIRMATION,
1091 this);
1092
1093 if (del) {
1094 KIO::Job* job = KIO::del(list);
1095 connect(job, SIGNAL(result(KJob*)),
1096 this, SLOT(slotDeleteFileFinished(KJob*)));
1097 }
1098 }
1099
1100 void DolphinView::slotDeleteFileFinished(KJob* job)
1101 {
1102 if (job->error() == 0) {
1103 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1104 } else {
1105 emit errorMessage(job->errorString());
1106 }
1107 }
1108
1109 void DolphinView::cutSelectedItems()
1110 {
1111 QMimeData* mimeData = new QMimeData();
1112 const KUrl::List kdeUrls = selectedUrls();
1113 const KUrl::List mostLocalUrls;
1114 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
1115 QApplication::clipboard()->setMimeData(mimeData);
1116 }
1117
1118 void DolphinView::copySelectedItems()
1119 {
1120 QMimeData* mimeData = new QMimeData();
1121 const KUrl::List kdeUrls = selectedUrls();
1122 const KUrl::List mostLocalUrls;
1123 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
1124 QApplication::clipboard()->setMimeData(mimeData);
1125 }
1126
1127 void DolphinView::paste()
1128 {
1129 QClipboard* clipboard = QApplication::clipboard();
1130 const QMimeData* mimeData = clipboard->mimeData();
1131
1132 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1133
1134 // per default the pasting is done into the current Url of the view
1135 KUrl destUrl(url());
1136
1137 // check whether the pasting should be done into a selected directory
1138 const KUrl::List selectedUrls = this->selectedUrls();
1139 if (selectedUrls.count() == 1) {
1140 const KFileItem fileItem(S_IFDIR,
1141 KFileItem::Unknown,
1142 selectedUrls.first(),
1143 true);
1144 if (fileItem.isDir()) {
1145 // only one item is selected which is a directory, hence paste
1146 // into this directory
1147 destUrl = selectedUrls.first();
1148 }
1149 }
1150
1151 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
1152 KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, destUrl);
1153 emit doingOperation(KonqFileUndoManager::MOVE);
1154 clipboard->clear();
1155 } else {
1156 KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, destUrl);
1157 emit doingOperation(KonqFileUndoManager::COPY);
1158 }
1159 }
1160
1161 QPair<bool, QString> DolphinView::pasteInfo() const
1162 {
1163 QPair<bool, QString> ret;
1164 QClipboard* clipboard = QApplication::clipboard();
1165 const QMimeData* mimeData = clipboard->mimeData();
1166
1167 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1168 if (!urls.isEmpty()) {
1169 ret.first = true;
1170 ret.second = i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls.count());
1171 } else {
1172 ret.first = false;
1173 ret.second = i18nc("@action:inmenu", "Paste");
1174 }
1175
1176 if (ret.first) {
1177 const KFileItemList items = selectedItems();
1178 const uint count = items.count();
1179 if (count > 1) {
1180 // pasting should not be allowed when more than one file
1181 // is selected
1182 ret.first = false;
1183 } else if (count == 1) {
1184 // Only one file is selected. Pasting is only allowed if this
1185 // file is a directory.
1186 ret.first = items.first().isDir();
1187 }
1188 }
1189 return ret;
1190 }
1191
1192 KAction* DolphinView::createRenameAction(KActionCollection* collection)
1193 {
1194 KAction* rename = collection->addAction("rename");
1195 rename->setText(i18nc("@action:inmenu File", "Rename..."));
1196 rename->setShortcut(Qt::Key_F2);
1197 return rename;
1198 }
1199
1200 KAction* DolphinView::createMoveToTrashAction(KActionCollection* collection)
1201 {
1202 KAction* moveToTrash = collection->addAction("move_to_trash");
1203 moveToTrash->setText(i18nc("@action:inmenu File", "Move to Trash"));
1204 moveToTrash->setIcon(KIcon("user-trash"));
1205 moveToTrash->setShortcut(QKeySequence::Delete);
1206 return moveToTrash;
1207 }
1208
1209 KAction* DolphinView::createDeleteAction(KActionCollection* collection)
1210 {
1211 KAction* deleteAction = collection->addAction("delete");
1212 deleteAction->setIcon(KIcon("edit-delete"));
1213 deleteAction->setText(i18nc("@action:inmenu File", "Delete"));
1214 deleteAction->setShortcut(Qt::SHIFT | Qt::Key_Delete);
1215 return deleteAction;
1216 }
1217
1218 KAction* DolphinView::createNewDirAction(KActionCollection* collection)
1219 {
1220 // This action doesn't appear in the GUI, it's for the shortcut only.
1221 // KNewMenu takes care of the GUI stuff.
1222 KAction* newDirAction = collection->addAction("create_dir");
1223 newDirAction->setText(i18n("Create Folder..."));
1224 newDirAction->setShortcut(Qt::Key_F10);
1225 return newDirAction;
1226 }
1227
1228 KAction* DolphinView::createSortDescendingAction(KActionCollection* collection)
1229 {
1230 KToggleAction* sortDescending = collection->add<KToggleAction>("descending");
1231 sortDescending->setText(i18nc("@action:inmenu Sort", "Descending"));
1232 return sortDescending;
1233 }
1234
1235 QActionGroup* DolphinView::createAdditionalInformationActionGroup(KActionCollection* collection)
1236 {
1237 QActionGroup* showInformationGroup = new QActionGroup(collection);
1238 showInformationGroup->setExclusive(false);
1239
1240 KToggleAction* showSizeInfo = collection->add<KToggleAction>("show_size_info");
1241 showSizeInfo->setText(i18nc("@action:inmenu Additional information", "Size"));
1242 showSizeInfo->setData(KFileItemDelegate::Size);
1243 showSizeInfo->setActionGroup(showInformationGroup);
1244
1245 KToggleAction* showDateInfo = collection->add<KToggleAction>("show_date_info");
1246 showDateInfo->setText(i18nc("@action:inmenu Additional information", "Date"));
1247 showDateInfo->setData(KFileItemDelegate::ModificationTime);
1248 showDateInfo->setActionGroup(showInformationGroup);
1249
1250 KToggleAction* showPermissionsInfo = collection->add<KToggleAction>("show_permissions_info");
1251 showPermissionsInfo->setText(i18nc("@action:inmenu Additional information", "Permissions"));
1252 showPermissionsInfo->setData(KFileItemDelegate::Permissions);
1253 showPermissionsInfo->setActionGroup(showInformationGroup);
1254
1255 KToggleAction* showOwnerInfo = collection->add<KToggleAction>("show_owner_info");
1256 showOwnerInfo->setText(i18nc("@action:inmenu Additional information", "Owner"));
1257 showOwnerInfo->setData(KFileItemDelegate::Owner);
1258 showOwnerInfo->setActionGroup(showInformationGroup);
1259
1260 KToggleAction* showGroupInfo = collection->add<KToggleAction>("show_group_info");
1261 showGroupInfo->setText(i18nc("@action:inmenu Additional information", "Group"));
1262 showGroupInfo->setData(KFileItemDelegate::OwnerAndGroup);
1263 showGroupInfo->setActionGroup(showInformationGroup);
1264
1265 KToggleAction* showMimeInfo = collection->add<KToggleAction>("show_mime_info");
1266 showMimeInfo->setText(i18nc("@action:inmenu Additional information", "Type"));
1267 showMimeInfo->setData(KFileItemDelegate::FriendlyMimeType);
1268 showMimeInfo->setActionGroup(showInformationGroup);
1269
1270 return showInformationGroup;
1271 }
1272
1273 #include "dolphinview.moc"