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