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