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