]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
* increase the performance of item previews: each item view provides an icon size...
[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 reload();
414 }
415
416 void DolphinView::zoomOut()
417 {
418 m_controller->triggerZoomOut();
419 reload();
420 }
421
422 bool DolphinView::isZoomInPossible() const
423 {
424 return m_controller->isZoomInPossible();
425 }
426
427 bool DolphinView::isZoomOutPossible() const
428 {
429 return m_controller->isZoomOutPossible();
430 }
431
432 void DolphinView::setSorting(Sorting sorting)
433 {
434 if (sorting != this->sorting()) {
435 updateSorting(sorting);
436 }
437 }
438
439 DolphinView::Sorting DolphinView::sorting() const
440 {
441 return m_proxyModel->sorting();
442 }
443
444 void DolphinView::setSortOrder(Qt::SortOrder order)
445 {
446 if (sortOrder() != order) {
447 updateSortOrder(order);
448 }
449 }
450
451 Qt::SortOrder DolphinView::sortOrder() const
452 {
453 return m_proxyModel->sortOrder();
454 }
455
456 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
457 {
458 const KUrl viewPropsUrl = viewPropertiesUrl();
459 ViewProperties props(viewPropsUrl);
460 props.setAdditionalInfo(info);
461 m_fileItemDelegate->setShowInformation(info);
462
463 emit additionalInfoChanged();
464
465 if (itemView() != m_detailsView) {
466 // the details view requires no reloading of the directory, as it maps
467 // the file item delegate info to its columns internally
468 loadDirectory(viewPropsUrl, true);
469 }
470 }
471
472 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
473 {
474 return m_fileItemDelegate->showInformation();
475 }
476
477 void DolphinView::reload()
478 {
479 setUrl(url());
480 loadDirectory(url(), true);
481 }
482
483 void DolphinView::refresh()
484 {
485 const bool oldActivationState = m_active;
486 m_active = true;
487
488 createView();
489 applyViewProperties(m_controller->url());
490 reload();
491
492 setActive(oldActivationState);
493 }
494
495 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
496 {
497 if (m_controller->url() == url) {
498 return;
499 }
500
501 m_controller->setUrl(url); // emits urlChanged, which we forward
502
503 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
504 applyViewProperties(rootUrl);
505 loadDirectory(rootUrl);
506 if (itemView() == m_columnView) {
507 m_columnView->setRootUrl(rootUrl);
508 m_columnView->showColumn(url);
509 }
510 } else {
511 applyViewProperties(url);
512 loadDirectory(url);
513 }
514
515 emit startedPathLoading(url);
516 }
517
518 void DolphinView::setNameFilter(const QString& nameFilter)
519 {
520 m_proxyModel->setFilterRegExp(nameFilter);
521
522 if (isColumnViewActive()) {
523 // adjusting the directory lister is not enough in the case of the
524 // column view, as each column has its own directory lister internally...
525 m_columnView->setNameFilter(nameFilter);
526 }
527 }
528
529 void DolphinView::calculateItemCount(int& fileCount, int& folderCount)
530 {
531 foreach (KFileItem item, m_dirLister->items()) {
532 if (item.isDir()) {
533 ++folderCount;
534 } else {
535 ++fileCount;
536 }
537 }
538 }
539
540 void DolphinView::setUrl(const KUrl& url)
541 {
542 updateView(url, KUrl());
543 }
544
545 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
546 {
547 QWidget::mouseReleaseEvent(event);
548 setActive(true);
549 }
550 void DolphinView::activate()
551 {
552 setActive(true);
553 }
554
555 void DolphinView::triggerItem(const KFileItem& item)
556 {
557 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
558 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
559 // items are selected by the user, hence don't trigger the
560 // item specified by 'index'
561 return;
562 }
563
564 if (item.isNull()) {
565 return;
566 }
567
568 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
569 }
570
571 void DolphinView::emitSelectionChangedSignal()
572 {
573 emit selectionChanged(DolphinView::selectedItems());
574 }
575
576 void DolphinView::loadDirectory(const KUrl& url, bool reload)
577 {
578 if (!url.isValid()) {
579 const QString location(url.pathOrUrl());
580 if (location.isEmpty()) {
581 emit errorMessage(i18nc("@info:status", "The location is empty."));
582 } else {
583 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
584 }
585 return;
586 }
587
588 m_loadingDirectory = true;
589
590 m_dirLister->stop();
591 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
592
593 if (isColumnViewActive()) {
594 // adjusting the directory lister is not enough in the case of the
595 // column view, as each column has its own directory lister internally...
596 if (reload) {
597 m_columnView->reload();
598 } else {
599 m_columnView->showColumn(url);
600 }
601 }
602 }
603
604 KUrl DolphinView::viewPropertiesUrl() const
605 {
606 if (isColumnViewActive()) {
607 return m_dirLister->url();
608 }
609
610 return url();
611 }
612
613 void DolphinView::applyViewProperties(const KUrl& url)
614 {
615 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
616 // The column view is active, hence don't apply the view properties
617 // of sub directories (represented by columns) to the view. The
618 // view always represents the properties of the first column.
619 return;
620 }
621
622 const ViewProperties props(url);
623
624 const Mode mode = props.viewMode();
625 if (m_mode != mode) {
626 m_mode = mode;
627 createView();
628 emit modeChanged();
629 }
630 if (itemView() == 0) {
631 createView();
632 }
633 Q_ASSERT(itemView() != 0);
634 Q_ASSERT(m_fileItemDelegate != 0);
635
636 const bool showHiddenFiles = props.showHiddenFiles();
637 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
638 m_dirLister->setShowingDotFiles(showHiddenFiles);
639 emit showHiddenFilesChanged();
640 }
641
642 m_storedCategorizedSorting = props.categorizedSorting();
643 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
644 if (categorized != m_proxyModel->isCategorizedModel()) {
645 m_proxyModel->setCategorizedModel(categorized);
646 emit categorizedSortingChanged();
647 }
648
649 const DolphinView::Sorting sorting = props.sorting();
650 if (sorting != m_proxyModel->sorting()) {
651 m_proxyModel->setSorting(sorting);
652 emit sortingChanged(sorting);
653 }
654
655 const Qt::SortOrder sortOrder = props.sortOrder();
656 if (sortOrder != m_proxyModel->sortOrder()) {
657 m_proxyModel->setSortOrder(sortOrder);
658 emit sortOrderChanged(sortOrder);
659 }
660
661 KFileItemDelegate::InformationList info = props.additionalInfo();
662 if (info != m_fileItemDelegate->showInformation()) {
663 m_fileItemDelegate->setShowInformation(info);
664 emit additionalInfoChanged();
665 }
666
667 const bool showPreview = props.showPreview();
668 if (showPreview != m_showPreview) {
669 m_showPreview = showPreview;
670 m_iconManager->setShowPreview(showPreview);
671 emit showPreviewChanged();
672 }
673 }
674
675 void DolphinView::changeSelection(const KFileItemList& selection)
676 {
677 clearSelection();
678 if (selection.isEmpty()) {
679 return;
680 }
681 const KUrl& baseUrl = url();
682 KUrl url;
683 QItemSelection new_selection;
684 foreach(const KFileItem& item, selection) {
685 url = item.url().upUrl();
686 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
687 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
688 new_selection.select(index, index);
689 }
690 }
691 itemView()->selectionModel()->select(new_selection,
692 QItemSelectionModel::ClearAndSelect
693 | QItemSelectionModel::Current);
694 }
695
696 void DolphinView::openContextMenu(const QPoint& pos)
697 {
698 KFileItem item;
699
700 const QModelIndex index = itemView()->indexAt(pos);
701 if (index.isValid() && (index.column() == DolphinModel::Name)) {
702 item = fileItem(index);
703 }
704
705 emit requestContextMenu(item, url());
706 }
707
708 void DolphinView::dropUrls(const KUrl::List& urls,
709 const KUrl& destPath,
710 const KFileItem& destItem)
711 {
712 Q_ASSERT(!urls.isEmpty());
713 const KUrl& destination = !destItem.isNull() && destItem.isDir() ?
714 destItem.url() : destPath;
715 const KUrl sourceDir = KUrl(urls.first().directory());
716 if (sourceDir != destination) {
717 dropUrls(urls, destination);
718 }
719 }
720
721 void DolphinView::dropUrls(const KUrl::List& urls,
722 const KUrl& destination)
723 {
724 DolphinDropController dropController(this);
725 // forward doingOperation signal up to the mainwindow
726 connect(&dropController, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)),
727 this, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)));
728 dropController.dropUrls(urls, destination);
729 }
730
731 void DolphinView::updateSorting(DolphinView::Sorting sorting)
732 {
733 ViewProperties props(viewPropertiesUrl());
734 props.setSorting(sorting);
735
736 m_proxyModel->setSorting(sorting);
737
738 emit sortingChanged(sorting);
739 }
740
741 void DolphinView::updateSortOrder(Qt::SortOrder order)
742 {
743 ViewProperties props(viewPropertiesUrl());
744 props.setSortOrder(order);
745
746 m_proxyModel->setSortOrder(order);
747
748 emit sortOrderChanged(order);
749 }
750
751 void DolphinView::toggleSortOrder()
752 {
753 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
754 Qt::DescendingOrder :
755 Qt::AscendingOrder;
756 setSortOrder(order);
757 }
758
759 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
760 {
761 ViewProperties props(viewPropertiesUrl());
762 props.setAdditionalInfo(info);
763 props.save();
764
765 m_fileItemDelegate->setShowInformation(info);
766
767 emit additionalInfoChanged(); // will call updateAdditionalInfoActions just below
768 }
769
770 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
771 {
772 const bool enable = (m_mode == DolphinView::DetailsView) ||
773 (m_mode == DolphinView::IconsView);
774
775 QAction* showSizeInfo = collection->action("show_size_info");
776 QAction* showDateInfo = collection->action("show_date_info");
777 QAction* showPermissionsInfo = collection->action("show_permissions_info");
778 QAction* showOwnerInfo = collection->action("show_owner_info");
779 QAction* showGroupInfo = collection->action("show_group_info");
780 QAction* showMimeInfo = collection->action("show_mime_info");
781
782 showSizeInfo->setChecked(false);
783 showDateInfo->setChecked(false);
784 showPermissionsInfo->setChecked(false);
785 showOwnerInfo->setChecked(false);
786 showGroupInfo->setChecked(false);
787 showMimeInfo->setChecked(false);
788
789 showSizeInfo->setEnabled(enable);
790 showDateInfo->setEnabled(enable);
791 showPermissionsInfo->setEnabled(enable);
792 showOwnerInfo->setEnabled(enable);
793 showGroupInfo->setEnabled(enable);
794 showMimeInfo->setEnabled(enable);
795
796 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
797 switch (info) {
798 case KFileItemDelegate::Size:
799 showSizeInfo->setChecked(true);
800 break;
801 case KFileItemDelegate::ModificationTime:
802 showDateInfo->setChecked(true);
803 break;
804 case KFileItemDelegate::Permissions:
805 showPermissionsInfo->setChecked(true);
806 break;
807 case KFileItemDelegate::Owner:
808 showOwnerInfo->setChecked(true);
809 break;
810 case KFileItemDelegate::OwnerAndGroup:
811 showGroupInfo->setChecked(true);
812 break;
813 case KFileItemDelegate::FriendlyMimeType:
814 showMimeInfo->setChecked(true);
815 break;
816 default:
817 break;
818 }
819 }
820 }
821
822 void DolphinView::toggleAdditionalInfo(QAction* action)
823 {
824 const KFileItemDelegate::Information info =
825 static_cast<KFileItemDelegate::Information>(action->data().toInt());
826
827 KFileItemDelegate::InformationList list = additionalInfo();
828
829 const bool show = action->isChecked();
830
831 const int index = list.indexOf(info);
832 const bool containsInfo = (index >= 0);
833 if (show && !containsInfo) {
834 list.append(info);
835 setAdditionalInfo(list);
836 } else if (!show && containsInfo) {
837 list.removeAt(index);
838 setAdditionalInfo(list);
839 Q_ASSERT(list.indexOf(info) < 0);
840 }
841 }
842
843 void DolphinView::emitContentsMoved()
844 {
845 // only emit the contents moved signal if:
846 // - no directory loading is ongoing (this would reset the contents position
847 // always to (0, 0))
848 // - if the Column View is active: the column view does an automatic
849 // positioning during the loading operation, which must be remembered
850 if (!m_loadingDirectory || isColumnViewActive()) {
851 const QPoint pos(contentsPosition());
852 emit contentsMoved(pos.x(), pos.y());
853 }
854 }
855
856 void DolphinView::showHoverInformation(const KFileItem& item)
857 {
858 if (hasSelection() || !m_active) {
859 return;
860 }
861
862 emit requestItemInfo(item);
863 }
864
865 void DolphinView::clearHoverInformation()
866 {
867 if (m_active) {
868 emit requestItemInfo(KFileItem());
869 }
870 }
871
872 void DolphinView::createView()
873 {
874 deleteView();
875 Q_ASSERT(m_iconsView == 0);
876 Q_ASSERT(m_detailsView == 0);
877 Q_ASSERT(m_columnView == 0);
878
879 QAbstractItemView* view = 0;
880 switch (m_mode) {
881 case IconsView: {
882 m_iconsView = new DolphinIconsView(this, m_controller);
883 view = m_iconsView;
884 break;
885 }
886
887 case DetailsView:
888 m_detailsView = new DolphinDetailsView(this, m_controller);
889 view = m_detailsView;
890 break;
891
892 case ColumnView:
893 m_columnView = new DolphinColumnView(this, m_controller);
894 view = m_columnView;
895 break;
896 }
897
898 Q_ASSERT(view != 0);
899
900 m_fileItemDelegate = new KFileItemDelegate(view);
901 view->setItemDelegate(m_fileItemDelegate);
902
903 view->setModel(m_proxyModel);
904 if (m_selectionModel != 0) {
905 view->setSelectionModel(m_selectionModel);
906 } else {
907 m_selectionModel = view->selectionModel();
908 }
909
910 // reparent the selection model, as it should not be deleted
911 // when deleting the model
912 m_selectionModel->setParent(this);
913
914 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
915
916 new KMimeTypeResolver(view, m_dolphinModel);
917 m_iconManager = new IconManager(view, m_proxyModel);
918 m_iconManager->setShowPreview(m_showPreview);
919
920 m_topLayout->insertWidget(1, view);
921
922 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
923 this, SLOT(emitSelectionChangedSignal()));
924 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
925 this, SLOT(emitContentsMoved()));
926 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
927 this, SLOT(emitContentsMoved()));
928 }
929
930 void DolphinView::deleteView()
931 {
932 QAbstractItemView* view = itemView();
933 if (view != 0) {
934 m_topLayout->removeWidget(view);
935 view->close();
936 view->deleteLater();
937 view = 0;
938 m_iconsView = 0;
939 m_detailsView = 0;
940 m_columnView = 0;
941 m_fileItemDelegate = 0;
942 m_iconManager = 0;
943 }
944 }
945
946 QAbstractItemView* DolphinView::itemView() const
947 {
948 if (m_detailsView != 0) {
949 return m_detailsView;
950 } else if (m_columnView != 0) {
951 return m_columnView;
952 }
953
954 return m_iconsView;
955 }
956
957 bool DolphinView::isCutItem(const KFileItem& item) const
958 {
959 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
960 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
961
962 const KUrl& itemUrl = item.url();
963 KUrl::List::const_iterator it = cutUrls.begin();
964 const KUrl::List::const_iterator end = cutUrls.end();
965 while (it != end) {
966 if (*it == itemUrl) {
967 return true;
968 }
969 ++it;
970 }
971
972 return false;
973 }
974
975 KToggleAction* DolphinView::iconsModeAction(KActionCollection* actionCollection)
976 {
977 KToggleAction* iconsView = actionCollection->add<KToggleAction>("icons");
978 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
979 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
980 iconsView->setIcon(KIcon("view-list-icons"));
981 iconsView->setData(QVariant::fromValue(IconsView));
982 return iconsView;
983 }
984
985 KToggleAction* DolphinView::detailsModeAction(KActionCollection* actionCollection)
986 {
987 KToggleAction* detailsView = actionCollection->add<KToggleAction>("details");
988 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
989 detailsView->setShortcut(Qt::CTRL | Qt::Key_2);
990 detailsView->setIcon(KIcon("view-list-details"));
991 detailsView->setData(QVariant::fromValue(DetailsView));
992 return detailsView;
993 }
994
995 KToggleAction* DolphinView::columnsModeAction(KActionCollection* actionCollection)
996 {
997 KToggleAction* columnView = actionCollection->add<KToggleAction>("columns");
998 columnView->setText(i18nc("@action:inmenu View Mode", "Columns"));
999 columnView->setShortcut(Qt::CTRL | Qt::Key_3);
1000 columnView->setIcon(KIcon("view-file-columns"));
1001 columnView->setData(QVariant::fromValue(ColumnView));
1002 return columnView;
1003 }
1004
1005 QString DolphinView::currentViewModeActionName() const
1006 {
1007 switch (m_mode) {
1008 case DolphinView::IconsView:
1009 return "icons";
1010 case DolphinView::DetailsView:
1011 return "details";
1012 case DolphinView::ColumnView:
1013 return "columns";
1014 }
1015 return QString(); // can't happen
1016 }
1017
1018 void DolphinView::renameSelectedItems()
1019 {
1020 const KFileItemList items = selectedItems();
1021 if (items.count() > 1) {
1022 // More than one item has been selected for renaming. Open
1023 // a rename dialog and rename all items afterwards.
1024 RenameDialog dialog(this, items);
1025 if (dialog.exec() == QDialog::Rejected) {
1026 return;
1027 }
1028
1029 const QString newName = dialog.newName();
1030 if (newName.isEmpty()) {
1031 emit errorMessage(dialog.errorString());
1032 } else {
1033 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1034 // as one operation instead of n rename operations like it is done now...
1035 Q_ASSERT(newName.contains('#'));
1036
1037 // iterate through all selected items and rename them...
1038 int index = 1;
1039 foreach (KFileItem item, items) {
1040 const KUrl& oldUrl = item.url();
1041 QString number;
1042 number.setNum(index++);
1043
1044 QString name = newName;
1045 name.replace('#', number);
1046
1047 if (oldUrl.fileName() != name) {
1048 KUrl newUrl = oldUrl;
1049 newUrl.setFileName(name);
1050 KonqOperations::rename(this, oldUrl, newUrl);
1051 emit doingOperation(KonqFileUndoManager::RENAME);
1052 }
1053 }
1054 }
1055 } else {
1056 // Only one item has been selected for renaming. Use the custom
1057 // renaming mechanism from the views.
1058 Q_ASSERT(items.count() == 1);
1059
1060 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1061 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1062 // is a benefit for the user at all -> let's wait for some input first...
1063 RenameDialog dialog(this, items);
1064 if (dialog.exec() == QDialog::Rejected) {
1065 return;
1066 }
1067
1068 const QString& newName = dialog.newName();
1069 if (newName.isEmpty()) {
1070 emit errorMessage(dialog.errorString());
1071 } else {
1072 const KUrl& oldUrl = items.first().url();
1073 KUrl newUrl = oldUrl;
1074 newUrl.setFileName(newName);
1075 KonqOperations::rename(this, oldUrl, newUrl);
1076 emit doingOperation(KonqFileUndoManager::RENAME);
1077 }
1078 }
1079 }
1080
1081 void DolphinView::trashSelectedItems()
1082 {
1083 emit doingOperation(KonqFileUndoManager::TRASH);
1084 KonqOperations::del(this, KonqOperations::TRASH, selectedUrls());
1085 }
1086
1087 void DolphinView::deleteSelectedItems()
1088 {
1089 const KUrl::List list = selectedUrls();
1090 const bool del = KonqOperations::askDeleteConfirmation(list,
1091 KonqOperations::DEL,
1092 KonqOperations::DEFAULT_CONFIRMATION,
1093 this);
1094
1095 if (del) {
1096 KIO::Job* job = KIO::del(list);
1097 connect(job, SIGNAL(result(KJob*)),
1098 this, SLOT(slotDeleteFileFinished(KJob*)));
1099 }
1100 }
1101
1102 void DolphinView::slotDeleteFileFinished(KJob* job)
1103 {
1104 if (job->error() == 0) {
1105 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1106 } else {
1107 emit errorMessage(job->errorString());
1108 }
1109 }
1110
1111 void DolphinView::cutSelectedItems()
1112 {
1113 QMimeData* mimeData = new QMimeData();
1114 const KUrl::List kdeUrls = selectedUrls();
1115 const KUrl::List mostLocalUrls;
1116 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
1117 QApplication::clipboard()->setMimeData(mimeData);
1118 }
1119
1120 void DolphinView::copySelectedItems()
1121 {
1122 QMimeData* mimeData = new QMimeData();
1123 const KUrl::List kdeUrls = selectedUrls();
1124 const KUrl::List mostLocalUrls;
1125 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
1126 QApplication::clipboard()->setMimeData(mimeData);
1127 }
1128
1129 void DolphinView::paste()
1130 {
1131 QClipboard* clipboard = QApplication::clipboard();
1132 const QMimeData* mimeData = clipboard->mimeData();
1133
1134 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1135
1136 // per default the pasting is done into the current Url of the view
1137 KUrl destUrl(url());
1138
1139 // check whether the pasting should be done into a selected directory
1140 const KUrl::List selectedUrls = this->selectedUrls();
1141 if (selectedUrls.count() == 1) {
1142 const KFileItem fileItem(S_IFDIR,
1143 KFileItem::Unknown,
1144 selectedUrls.first(),
1145 true);
1146 if (fileItem.isDir()) {
1147 // only one item is selected which is a directory, hence paste
1148 // into this directory
1149 destUrl = selectedUrls.first();
1150 }
1151 }
1152
1153 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
1154 KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, destUrl);
1155 emit doingOperation(KonqFileUndoManager::MOVE);
1156 clipboard->clear();
1157 } else {
1158 KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, destUrl);
1159 emit doingOperation(KonqFileUndoManager::COPY);
1160 }
1161 }
1162
1163 QPair<bool, QString> DolphinView::pasteInfo() const
1164 {
1165 QPair<bool, QString> ret;
1166 QClipboard* clipboard = QApplication::clipboard();
1167 const QMimeData* mimeData = clipboard->mimeData();
1168
1169 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1170 if (!urls.isEmpty()) {
1171 ret.first = true;
1172 ret.second = i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls.count());
1173 } else {
1174 ret.first = false;
1175 ret.second = i18nc("@action:inmenu", "Paste");
1176 }
1177
1178 if (ret.first) {
1179 const KFileItemList items = selectedItems();
1180 const uint count = items.count();
1181 if (count > 1) {
1182 // pasting should not be allowed when more than one file
1183 // is selected
1184 ret.first = false;
1185 } else if (count == 1) {
1186 // Only one file is selected. Pasting is only allowed if this
1187 // file is a directory.
1188 ret.first = items.first().isDir();
1189 }
1190 }
1191 return ret;
1192 }
1193
1194 KAction* DolphinView::createRenameAction(KActionCollection* collection)
1195 {
1196 KAction* rename = collection->addAction("rename");
1197 rename->setText(i18nc("@action:inmenu File", "Rename..."));
1198 rename->setShortcut(Qt::Key_F2);
1199 return rename;
1200 }
1201
1202 KAction* DolphinView::createMoveToTrashAction(KActionCollection* collection)
1203 {
1204 KAction* moveToTrash = collection->addAction("move_to_trash");
1205 moveToTrash->setText(i18nc("@action:inmenu File", "Move to Trash"));
1206 moveToTrash->setIcon(KIcon("user-trash"));
1207 moveToTrash->setShortcut(QKeySequence::Delete);
1208 return moveToTrash;
1209 }
1210
1211 KAction* DolphinView::createDeleteAction(KActionCollection* collection)
1212 {
1213 KAction* deleteAction = collection->addAction("delete");
1214 deleteAction->setIcon(KIcon("edit-delete"));
1215 deleteAction->setText(i18nc("@action:inmenu File", "Delete"));
1216 deleteAction->setShortcut(Qt::SHIFT | Qt::Key_Delete);
1217 return deleteAction;
1218 }
1219
1220 KAction* DolphinView::createNewDirAction(KActionCollection* collection)
1221 {
1222 // This action doesn't appear in the GUI, it's for the shortcut only.
1223 // KNewMenu takes care of the GUI stuff.
1224 KAction* newDirAction = collection->addAction("create_dir");
1225 newDirAction->setText(i18n("Create Folder..."));
1226 newDirAction->setShortcut(Qt::Key_F10);
1227 return newDirAction;
1228 }
1229
1230 KAction* DolphinView::createSortDescendingAction(KActionCollection* collection)
1231 {
1232 KToggleAction* sortDescending = collection->add<KToggleAction>("descending");
1233 sortDescending->setText(i18nc("@action:inmenu Sort", "Descending"));
1234 return sortDescending;
1235 }
1236
1237 QActionGroup* DolphinView::createAdditionalInformationActionGroup(KActionCollection* collection)
1238 {
1239 QActionGroup* showInformationGroup = new QActionGroup(collection);
1240 showInformationGroup->setExclusive(false);
1241
1242 KToggleAction* showSizeInfo = collection->add<KToggleAction>("show_size_info");
1243 showSizeInfo->setText(i18nc("@action:inmenu Additional information", "Size"));
1244 showSizeInfo->setData(KFileItemDelegate::Size);
1245 showSizeInfo->setActionGroup(showInformationGroup);
1246
1247 KToggleAction* showDateInfo = collection->add<KToggleAction>("show_date_info");
1248 showDateInfo->setText(i18nc("@action:inmenu Additional information", "Date"));
1249 showDateInfo->setData(KFileItemDelegate::ModificationTime);
1250 showDateInfo->setActionGroup(showInformationGroup);
1251
1252 KToggleAction* showPermissionsInfo = collection->add<KToggleAction>("show_permissions_info");
1253 showPermissionsInfo->setText(i18nc("@action:inmenu Additional information", "Permissions"));
1254 showPermissionsInfo->setData(KFileItemDelegate::Permissions);
1255 showPermissionsInfo->setActionGroup(showInformationGroup);
1256
1257 KToggleAction* showOwnerInfo = collection->add<KToggleAction>("show_owner_info");
1258 showOwnerInfo->setText(i18nc("@action:inmenu Additional information", "Owner"));
1259 showOwnerInfo->setData(KFileItemDelegate::Owner);
1260 showOwnerInfo->setActionGroup(showInformationGroup);
1261
1262 KToggleAction* showGroupInfo = collection->add<KToggleAction>("show_group_info");
1263 showGroupInfo->setText(i18nc("@action:inmenu Additional information", "Group"));
1264 showGroupInfo->setData(KFileItemDelegate::OwnerAndGroup);
1265 showGroupInfo->setActionGroup(showInformationGroup);
1266
1267 KToggleAction* showMimeInfo = collection->add<KToggleAction>("show_mime_info");
1268 showMimeInfo->setText(i18nc("@action:inmenu Additional information", "Type"));
1269 showMimeInfo->setData(KFileItemDelegate::FriendlyMimeType);
1270 showMimeInfo->setActionGroup(showInformationGroup);
1271
1272 return showInformationGroup;
1273 }
1274
1275 #include "dolphinview.moc"