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