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