]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Move the code handling the additional-info-actions to DolphinView; simplify signal...
[dolphin.git] / src / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <QApplication>
24 #include <QClipboard>
25 #include <QKeyEvent>
26 #include <QItemSelection>
27 #include <QBoxLayout>
28 #include <QTimer>
29 #include <QScrollBar>
30
31 #include <kactioncollection.h>
32 #include <kcolorscheme.h>
33 #include <kdirlister.h>
34 #include <kfileitemdelegate.h>
35 #include <klocale.h>
36 #include <kiconeffect.h>
37 #include <kio/deletejob.h>
38 #include <kio/netaccess.h>
39 #include <kio/previewjob.h>
40 #include <kjob.h>
41 #include <kmenu.h>
42 #include <kmimetyperesolver.h>
43 #include <konqmimedata.h>
44 #include <konq_operations.h>
45 #include <ktoggleaction.h>
46 #include <kurl.h>
47
48 #include "dolphindropcontroller.h"
49 #include "dolphinmodel.h"
50 #include "dolphincolumnview.h"
51 #include "dolphincontroller.h"
52 #include "dolphinsortfilterproxymodel.h"
53 #include "dolphindetailsview.h"
54 #include "dolphiniconsview.h"
55 #include "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 const KUrl viewPropsUrl = viewPropertiesUrl();
210 ViewProperties props(viewPropsUrl);
211 props.setViewMode(m_mode);
212 createView();
213
214 // the file item delegate has been recreated, apply the current
215 // additional information manually
216 const KFileItemDelegate::InformationList infoList = props.additionalInfo();
217 m_fileItemDelegate->setShowInformation(infoList);
218 emit additionalInfoChanged();
219
220 // Not all view modes support categorized sorting. Adjust the sorting model
221 // if changing the view mode results in a change of the categorized sorting
222 // capabilities.
223 m_storedCategorizedSorting = props.categorizedSorting();
224 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
225 if (categorized != m_proxyModel->isCategorizedModel()) {
226 m_proxyModel->setCategorizedModel(categorized);
227 emit categorizedSortingChanged();
228 }
229
230 emit modeChanged();
231 }
232
233 DolphinView::Mode DolphinView::mode() const
234 {
235 return m_mode;
236 }
237
238 void DolphinView::setShowPreview(bool show)
239 {
240 if (m_showPreview == show) {
241 return;
242 }
243
244 const KUrl viewPropsUrl = viewPropertiesUrl();
245 ViewProperties props(viewPropsUrl);
246 props.setShowPreview(show);
247
248 m_showPreview = show;
249
250 emit showPreviewChanged();
251
252 loadDirectory(viewPropsUrl, true);
253 }
254
255 bool DolphinView::showPreview() const
256 {
257 return m_showPreview;
258 }
259
260 void DolphinView::setShowHiddenFiles(bool show)
261 {
262 if (m_dirLister->showingDotFiles() == show) {
263 return;
264 }
265
266 const KUrl viewPropsUrl = viewPropertiesUrl();
267 ViewProperties props(viewPropsUrl);
268 props.setShowHiddenFiles(show);
269
270 m_dirLister->setShowingDotFiles(show);
271 emit showHiddenFilesChanged();
272
273 loadDirectory(viewPropsUrl, true);
274 }
275
276 bool DolphinView::showHiddenFiles() const
277 {
278 return m_dirLister->showingDotFiles();
279 }
280
281 void DolphinView::setCategorizedSorting(bool categorized)
282 {
283 if (categorized == categorizedSorting()) {
284 return;
285 }
286
287 // setCategorizedSorting(true) may only get invoked
288 // if the view supports categorized sorting
289 Q_ASSERT(!categorized || supportsCategorizedSorting());
290
291 ViewProperties props(viewPropertiesUrl());
292 props.setCategorizedSorting(categorized);
293 props.save();
294
295 m_storedCategorizedSorting = categorized;
296 m_proxyModel->setCategorizedModel(categorized);
297
298 emit categorizedSortingChanged();
299 }
300
301 bool DolphinView::categorizedSorting() const
302 {
303 // If all view modes would support categorized sorting, returning
304 // m_proxyModel->isCategorizedModel() would be the way to go. As
305 // currently only the icons view supports caterized sorting, we remember
306 // the stored view properties state in m_storedCategorizedSorting and
307 // return this state. The application takes care to disable the corresponding
308 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
309 // that this setting is not applied to the current view mode.
310 return m_storedCategorizedSorting;
311 }
312
313 bool DolphinView::supportsCategorizedSorting() const
314 {
315 return m_iconsView != 0;
316 }
317
318 void DolphinView::selectAll()
319 {
320 QAbstractItemView* view = itemView();
321 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
322 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
323 // selection instead of selecting all items. This is bypassed for KDE 4.0
324 // by invoking clearSelection() first.
325 view->clearSelection();
326 view->selectAll();
327 }
328
329 void DolphinView::invertSelection()
330 {
331 if (isColumnViewActive()) {
332 // QAbstractItemView does not offer a virtual method invertSelection()
333 // as counterpart to QAbstractItemView::selectAll(). This makes it
334 // necessary to delegate the inverting of the selection to the
335 // column view, as only the selection of the active column should
336 // get inverted.
337 m_columnView->invertSelection();
338 } else {
339 QItemSelectionModel* selectionModel = itemView()->selectionModel();
340 const QAbstractItemModel* itemModel = selectionModel->model();
341
342 const QModelIndex topLeft = itemModel->index(0, 0);
343 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
344 itemModel->columnCount() - 1);
345
346 const QItemSelection selection(topLeft, bottomRight);
347 selectionModel->select(selection, QItemSelectionModel::Toggle);
348 }
349 }
350
351 bool DolphinView::hasSelection() const
352 {
353 return itemView()->selectionModel()->hasSelection();
354 }
355
356 void DolphinView::clearSelection()
357 {
358 itemView()->selectionModel()->clear();
359 }
360
361 KFileItemList DolphinView::selectedItems() const
362 {
363 const QAbstractItemView* view = itemView();
364
365 // Our view has a selection, we will map them back to the DolphinModel
366 // and then fill the KFileItemList.
367 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
368
369 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
370 KFileItemList itemList;
371
372 const QModelIndexList indexList = selection.indexes();
373 foreach (QModelIndex index, indexList) {
374 KFileItem item = m_dolphinModel->itemForIndex(index);
375 if (!item.isNull()) {
376 itemList.append(item);
377 }
378 }
379
380 return itemList;
381 }
382
383 KUrl::List DolphinView::selectedUrls() const
384 {
385 KUrl::List urls;
386 const KFileItemList list = selectedItems();
387 foreach (KFileItem item, list) {
388 urls.append(item.url());
389 }
390 return urls;
391 }
392
393 KFileItem DolphinView::fileItem(const QModelIndex& index) const
394 {
395 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
396 return m_dolphinModel->itemForIndex(dolphinModelIndex);
397 }
398
399 void DolphinView::setContentsPosition(int x, int y)
400 {
401 QAbstractItemView* view = itemView();
402
403 // the ColumnView takes care itself for the horizontal scrolling
404 if (!isColumnViewActive()) {
405 view->horizontalScrollBar()->setValue(x);
406 }
407 view->verticalScrollBar()->setValue(y);
408
409 m_loadingDirectory = false;
410 }
411
412 QPoint DolphinView::contentsPosition() const
413 {
414 const int x = itemView()->horizontalScrollBar()->value();
415 const int y = itemView()->verticalScrollBar()->value();
416 return QPoint(x, y);
417 }
418
419 void DolphinView::zoomIn()
420 {
421 m_controller->triggerZoomIn();
422 }
423
424 void DolphinView::zoomOut()
425 {
426 m_controller->triggerZoomOut();
427 }
428
429 bool DolphinView::isZoomInPossible() const
430 {
431 return m_controller->isZoomInPossible();
432 }
433
434 bool DolphinView::isZoomOutPossible() const
435 {
436 return m_controller->isZoomOutPossible();
437 }
438
439 void DolphinView::setSorting(Sorting sorting)
440 {
441 if (sorting != this->sorting()) {
442 updateSorting(sorting);
443 }
444 }
445
446 DolphinView::Sorting DolphinView::sorting() const
447 {
448 return m_proxyModel->sorting();
449 }
450
451 void DolphinView::setSortOrder(Qt::SortOrder order)
452 {
453 if (sortOrder() != order) {
454 updateSortOrder(order);
455 }
456 }
457
458 Qt::SortOrder DolphinView::sortOrder() const
459 {
460 return m_proxyModel->sortOrder();
461 }
462
463 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
464 {
465 const KUrl viewPropsUrl = viewPropertiesUrl();
466 ViewProperties props(viewPropsUrl);
467 props.setAdditionalInfo(info);
468 m_fileItemDelegate->setShowInformation(info);
469
470 emit additionalInfoChanged();
471
472 if (itemView() != m_detailsView) {
473 // the details view requires no reloading of the directory, as it maps
474 // the file item delegate info to its columns internally
475 loadDirectory(viewPropsUrl, true);
476 }
477 }
478
479 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
480 {
481 return m_fileItemDelegate->showInformation();
482 }
483
484 void DolphinView::reload()
485 {
486 setUrl(url());
487 loadDirectory(url(), true);
488 }
489
490 void DolphinView::refresh()
491 {
492 const bool oldActivationState = m_active;
493 m_active = true;
494
495 createView();
496 applyViewProperties(m_controller->url());
497 reload();
498
499 setActive(oldActivationState);
500 }
501
502 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
503 {
504 if (m_controller->url() == url) {
505 return;
506 }
507
508 m_controller->setUrl(url); // emits urlChanged, which we forward
509
510 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
511 applyViewProperties(rootUrl);
512 loadDirectory(rootUrl);
513 if (itemView() == m_columnView) {
514 m_columnView->setRootUrl(rootUrl);
515 m_columnView->showColumn(url);
516 }
517 } else {
518 applyViewProperties(url);
519 loadDirectory(url);
520 }
521
522 emit startedPathLoading(url);
523 }
524
525 void DolphinView::setNameFilter(const QString& nameFilter)
526 {
527 m_proxyModel->setFilterRegExp(nameFilter);
528
529 if (isColumnViewActive()) {
530 // adjusting the directory lister is not enough in the case of the
531 // column view, as each column has its own directory lister internally...
532 m_columnView->setNameFilter(nameFilter);
533 }
534 }
535
536 void DolphinView::calculateItemCount(int& fileCount, int& folderCount)
537 {
538 foreach (KFileItem item, m_dirLister->items()) {
539 if (item.isDir()) {
540 ++folderCount;
541 } else {
542 ++fileCount;
543 }
544 }
545 }
546
547 void DolphinView::setUrl(const KUrl& url)
548 {
549 updateView(url, KUrl());
550 }
551
552 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
553 {
554 QWidget::mouseReleaseEvent(event);
555 setActive(true);
556 }
557 void DolphinView::activate()
558 {
559 setActive(true);
560 }
561
562 void DolphinView::triggerItem(const KFileItem& item)
563 {
564 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
565 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
566 // items are selected by the user, hence don't trigger the
567 // item specified by 'index'
568 return;
569 }
570
571 if (item.isNull()) {
572 return;
573 }
574
575 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
576 }
577
578 void DolphinView::generatePreviews(const KFileItemList& items)
579 {
580 if (m_controller->dolphinView()->showPreview()) {
581 if (m_previewJob != 0) {
582 m_previewJob->kill();
583 m_previewJob = 0;
584 }
585
586 m_previewJob = KIO::filePreview(items, 128);
587 connect(m_previewJob, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
588 this, SLOT(replaceIcon(const KFileItem&, const QPixmap&)));
589 connect(m_previewJob, SIGNAL(finished(KJob*)),
590 this, SLOT(slotPreviewJobFinished(KJob*)));
591 }
592 }
593
594 void DolphinView::replaceIcon(const KFileItem& item, const QPixmap& pixmap)
595 {
596 Q_ASSERT(!item.isNull());
597 if (!m_showPreview || (item.url().directory() != m_dirLister->url().path())) {
598 // the preview has been deactivated in the meanwhile or the preview
599 // job is still working on items of an older URL, hence
600 // the item is not part of the directory model anymore
601 return;
602 }
603
604 const QModelIndex idx = m_dolphinModel->indexForItem(item);
605 if (idx.isValid() && (idx.column() == 0)) {
606 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
607 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
608 KIconEffect iconEffect;
609 const QPixmap cutPixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
610 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
611 } else {
612 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
613 }
614 }
615 }
616
617 void DolphinView::emitSelectionChangedSignal()
618 {
619 emit selectionChanged(DolphinView::selectedItems());
620 }
621
622 void DolphinView::loadDirectory(const KUrl& url, bool reload)
623 {
624 if (!url.isValid()) {
625 const QString location(url.pathOrUrl());
626 if (location.isEmpty()) {
627 emit errorMessage(i18nc("@info:status", "The location is empty."));
628 } else {
629 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
630 }
631 return;
632 }
633
634 m_cutItemsCache.clear();
635 m_loadingDirectory = true;
636
637 m_dirLister->stop();
638 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
639
640 if (isColumnViewActive()) {
641 // adjusting the directory lister is not enough in the case of the
642 // column view, as each column has its own directory lister internally...
643 if (reload) {
644 m_columnView->reload();
645 } else {
646 m_columnView->showColumn(url);
647 }
648 }
649 }
650
651 KUrl DolphinView::viewPropertiesUrl() const
652 {
653 if (isColumnViewActive()) {
654 return m_dirLister->url();
655 }
656
657 return url();
658 }
659
660 void DolphinView::applyViewProperties(const KUrl& url)
661 {
662 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
663 // The column view is active, hence don't apply the view properties
664 // of sub directories (represented by columns) to the view. The
665 // view always represents the properties of the first column.
666 return;
667 }
668
669 const ViewProperties props(url);
670
671 const Mode mode = props.viewMode();
672 if (m_mode != mode) {
673 m_mode = mode;
674 createView();
675 emit modeChanged();
676 }
677 if (itemView() == 0) {
678 createView();
679 }
680 Q_ASSERT(itemView() != 0);
681 Q_ASSERT(m_fileItemDelegate != 0);
682
683 const bool showHiddenFiles = props.showHiddenFiles();
684 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
685 m_dirLister->setShowingDotFiles(showHiddenFiles);
686 emit showHiddenFilesChanged();
687 }
688
689 m_storedCategorizedSorting = props.categorizedSorting();
690 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
691 if (categorized != m_proxyModel->isCategorizedModel()) {
692 m_proxyModel->setCategorizedModel(categorized);
693 emit categorizedSortingChanged();
694 }
695
696 const DolphinView::Sorting sorting = props.sorting();
697 if (sorting != m_proxyModel->sorting()) {
698 m_proxyModel->setSorting(sorting);
699 emit sortingChanged(sorting);
700 }
701
702 const Qt::SortOrder sortOrder = props.sortOrder();
703 if (sortOrder != m_proxyModel->sortOrder()) {
704 m_proxyModel->setSortOrder(sortOrder);
705 emit sortOrderChanged(sortOrder);
706 }
707
708 KFileItemDelegate::InformationList info = props.additionalInfo();
709 if (info != m_fileItemDelegate->showInformation()) {
710 m_fileItemDelegate->setShowInformation(info);
711 emit additionalInfoChanged();
712 }
713
714 const bool showPreview = props.showPreview();
715 if (showPreview != m_showPreview) {
716 m_showPreview = showPreview;
717 emit showPreviewChanged();
718 }
719 }
720
721 void DolphinView::changeSelection(const KFileItemList& selection)
722 {
723 clearSelection();
724 if (selection.isEmpty()) {
725 return;
726 }
727 const KUrl& baseUrl = url();
728 KUrl url;
729 QItemSelection new_selection;
730 foreach(const KFileItem& item, selection) {
731 url = item.url().upUrl();
732 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
733 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
734 new_selection.select(index, index);
735 }
736 }
737 itemView()->selectionModel()->select(new_selection,
738 QItemSelectionModel::ClearAndSelect
739 | QItemSelectionModel::Current);
740 }
741
742 void DolphinView::openContextMenu(const QPoint& pos)
743 {
744 KFileItem item;
745
746 const QModelIndex index = itemView()->indexAt(pos);
747 if (index.isValid() && (index.column() == DolphinModel::Name)) {
748 item = fileItem(index);
749 }
750
751 emit requestContextMenu(item, url());
752 }
753
754 void DolphinView::dropUrls(const KUrl::List& urls,
755 const KUrl& destPath,
756 const KFileItem& destItem)
757 {
758 Q_ASSERT(!urls.isEmpty());
759 const KUrl& destination = !destItem.isNull() && destItem.isDir() ?
760 destItem.url() : destPath;
761 const KUrl sourceDir = KUrl(urls.first().directory());
762 if (sourceDir != destination) {
763 dropUrls(urls, destination);
764 }
765 }
766
767 void DolphinView::dropUrls(const KUrl::List& urls,
768 const KUrl& destination)
769 {
770 DolphinDropController dropController(this);
771 // forward doingOperation signal up to the mainwindow
772 connect(&dropController, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)),
773 this, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)));
774 dropController.dropUrls(urls, destination);
775 }
776
777 void DolphinView::updateSorting(DolphinView::Sorting sorting)
778 {
779 ViewProperties props(viewPropertiesUrl());
780 props.setSorting(sorting);
781
782 m_proxyModel->setSorting(sorting);
783
784 emit sortingChanged(sorting);
785 }
786
787 void DolphinView::updateSortOrder(Qt::SortOrder order)
788 {
789 ViewProperties props(viewPropertiesUrl());
790 props.setSortOrder(order);
791
792 m_proxyModel->setSortOrder(order);
793
794 emit sortOrderChanged(order);
795 }
796
797 void DolphinView::toggleSortOrder()
798 {
799 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
800 Qt::DescendingOrder :
801 Qt::AscendingOrder;
802 setSortOrder(order);
803 }
804
805 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
806 {
807 ViewProperties props(viewPropertiesUrl());
808 props.setAdditionalInfo(info);
809 props.save();
810
811 m_fileItemDelegate->setShowInformation(info);
812
813 emit additionalInfoChanged(); // will call updateAdditionalInfoActions just below
814 }
815
816 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
817 {
818 const bool enable = (m_mode == DolphinView::DetailsView) ||
819 (m_mode == DolphinView::IconsView);
820
821 QAction* showSizeInfo = collection->action("show_size_info");
822 QAction* showDateInfo = collection->action("show_date_info");
823 QAction* showPermissionsInfo = collection->action("show_permissions_info");
824 QAction* showOwnerInfo = collection->action("show_owner_info");
825 QAction* showGroupInfo = collection->action("show_group_info");
826 QAction* showMimeInfo = collection->action("show_mime_info");
827
828 showSizeInfo->setChecked(false);
829 showDateInfo->setChecked(false);
830 showPermissionsInfo->setChecked(false);
831 showOwnerInfo->setChecked(false);
832 showGroupInfo->setChecked(false);
833 showMimeInfo->setChecked(false);
834
835 showSizeInfo->setEnabled(enable);
836 showDateInfo->setEnabled(enable);
837 showPermissionsInfo->setEnabled(enable);
838 showOwnerInfo->setEnabled(enable);
839 showGroupInfo->setEnabled(enable);
840 showMimeInfo->setEnabled(enable);
841
842 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
843 switch (info) {
844 case KFileItemDelegate::Size:
845 showSizeInfo->setChecked(true);
846 break;
847 case KFileItemDelegate::ModificationTime:
848 showDateInfo->setChecked(true);
849 break;
850 case KFileItemDelegate::Permissions:
851 showPermissionsInfo->setChecked(true);
852 break;
853 case KFileItemDelegate::Owner:
854 showOwnerInfo->setChecked(true);
855 break;
856 case KFileItemDelegate::OwnerAndGroup:
857 showGroupInfo->setChecked(true);
858 break;
859 case KFileItemDelegate::FriendlyMimeType:
860 showMimeInfo->setChecked(true);
861 break;
862 default:
863 break;
864 }
865 }
866 }
867
868 void DolphinView::toggleAdditionalInfo(QAction* action)
869 {
870 const KFileItemDelegate::Information info =
871 static_cast<KFileItemDelegate::Information>(action->data().toInt());
872
873 KFileItemDelegate::InformationList list = additionalInfo();
874
875 const bool show = action->isChecked();
876
877 const int index = list.indexOf(info);
878 const bool containsInfo = (index >= 0);
879 if (show && !containsInfo) {
880 list.append(info);
881 setAdditionalInfo(list);
882 } else if (!show && containsInfo) {
883 list.removeAt(index);
884 setAdditionalInfo(list);
885 Q_ASSERT(list.indexOf(info) < 0);
886 }
887 }
888
889 void DolphinView::emitContentsMoved()
890 {
891 // only emit the contents moved signal if:
892 // - no directory loading is ongoing (this would reset the contents position
893 // always to (0, 0))
894 // - if the Column View is active: the column view does an automatic
895 // positioning during the loading operation, which must be remembered
896 if (!m_loadingDirectory || isColumnViewActive()) {
897 const QPoint pos(contentsPosition());
898 emit contentsMoved(pos.x(), pos.y());
899 }
900 }
901
902 void DolphinView::updateCutItems()
903 {
904 // restore the icons of all previously selected items to the
905 // original state...
906 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
907 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
908 while (it != end) {
909 const QModelIndex index = m_dolphinModel->indexForUrl((*it).url);
910 if (index.isValid()) {
911 m_dolphinModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
912 }
913 ++it;
914 }
915 m_cutItemsCache.clear();
916
917 // ... and apply an item effect to all currently cut items
918 applyCutItemEffect();
919 }
920
921 void DolphinView::showHoverInformation(const KFileItem& item)
922 {
923 if (hasSelection() || !m_active) {
924 return;
925 }
926
927 emit requestItemInfo(item);
928 }
929
930 void DolphinView::clearHoverInformation()
931 {
932 if (m_active) {
933 emit requestItemInfo(KFileItem());
934 }
935 }
936
937
938 void DolphinView::createView()
939 {
940 deleteView();
941 Q_ASSERT(m_iconsView == 0);
942 Q_ASSERT(m_detailsView == 0);
943 Q_ASSERT(m_columnView == 0);
944
945 QAbstractItemView* view = 0;
946 switch (m_mode) {
947 case IconsView: {
948 m_iconsView = new DolphinIconsView(this, m_controller);
949 view = m_iconsView;
950 break;
951 }
952
953 case DetailsView:
954 m_detailsView = new DolphinDetailsView(this, m_controller);
955 view = m_detailsView;
956 break;
957
958 case ColumnView:
959 m_columnView = new DolphinColumnView(this, m_controller);
960 view = m_columnView;
961 break;
962 }
963
964 Q_ASSERT(view != 0);
965
966 m_fileItemDelegate = new KFileItemDelegate(view);
967 view->setItemDelegate(m_fileItemDelegate);
968
969 view->setModel(m_proxyModel);
970 if (m_selectionModel != 0) {
971 view->setSelectionModel(m_selectionModel);
972 } else {
973 m_selectionModel = view->selectionModel();
974 }
975
976 // reparent the selection model, as it should not be deleted
977 // when deleting the model
978 m_selectionModel->setParent(this);
979
980 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
981
982 new KMimeTypeResolver(view, m_dolphinModel);
983 m_topLayout->insertWidget(1, view);
984
985 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
986 this, SLOT(emitSelectionChangedSignal()));
987 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
988 this, SLOT(emitContentsMoved()));
989 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
990 this, SLOT(emitContentsMoved()));
991 }
992
993 void DolphinView::deleteView()
994 {
995 QAbstractItemView* view = itemView();
996 if (view != 0) {
997 m_topLayout->removeWidget(view);
998 view->close();
999 view->deleteLater();
1000 view = 0;
1001 m_iconsView = 0;
1002 m_detailsView = 0;
1003 m_columnView = 0;
1004 m_fileItemDelegate = 0;
1005 }
1006 }
1007
1008 QAbstractItemView* DolphinView::itemView() const
1009 {
1010 if (m_detailsView != 0) {
1011 return m_detailsView;
1012 } else if (m_columnView != 0) {
1013 return m_columnView;
1014 }
1015
1016 return m_iconsView;
1017 }
1018
1019 bool DolphinView::isCutItem(const KFileItem& item) const
1020 {
1021 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1022 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1023
1024 const KUrl& itemUrl = item.url();
1025 KUrl::List::const_iterator it = cutUrls.begin();
1026 const KUrl::List::const_iterator end = cutUrls.end();
1027 while (it != end) {
1028 if (*it == itemUrl) {
1029 return true;
1030 }
1031 ++it;
1032 }
1033
1034 return false;
1035 }
1036
1037 void DolphinView::applyCutItemEffect()
1038 {
1039 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1040 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
1041 return;
1042 }
1043
1044 KFileItemList items(m_dirLister->items());
1045 KFileItemList::const_iterator it = items.begin();
1046 const KFileItemList::const_iterator end = items.end();
1047 while (it != end) {
1048 const KFileItem item = *it;
1049 if (isCutItem(item)) {
1050 const QModelIndex index = m_dolphinModel->indexForItem(item);
1051 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
1052 if (value.type() == QVariant::Icon) {
1053 const QIcon icon(qvariant_cast<QIcon>(value));
1054 QPixmap pixmap = icon.pixmap(128, 128);
1055
1056 // remember current pixmap for the item to be able
1057 // to restore it when other items get cut
1058 CutItem cutItem;
1059 cutItem.url = item.url();
1060 cutItem.pixmap = pixmap;
1061 m_cutItemsCache.append(cutItem);
1062
1063 // apply icon effect to the cut item
1064 KIconEffect iconEffect;
1065 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
1066 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
1067 }
1068 }
1069 ++it;
1070 }
1071 }
1072
1073 KToggleAction* DolphinView::iconsModeAction(KActionCollection* actionCollection)
1074 {
1075 KToggleAction* iconsView = actionCollection->add<KToggleAction>("icons");
1076 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
1077 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
1078 iconsView->setIcon(KIcon("view-list-icons"));
1079 iconsView->setData(QVariant::fromValue(IconsView));
1080 return iconsView;
1081 }
1082
1083 KToggleAction* DolphinView::detailsModeAction(KActionCollection* actionCollection)
1084 {
1085 KToggleAction* detailsView = actionCollection->add<KToggleAction>("details");
1086 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
1087 detailsView->setShortcut(Qt::CTRL | Qt::Key_2);
1088 detailsView->setIcon(KIcon("view-list-details"));
1089 detailsView->setData(QVariant::fromValue(DetailsView));
1090 return detailsView;
1091 }
1092
1093 KToggleAction* DolphinView::columnsModeAction(KActionCollection* actionCollection)
1094 {
1095 KToggleAction* columnView = actionCollection->add<KToggleAction>("columns");
1096 columnView->setText(i18nc("@action:inmenu View Mode", "Columns"));
1097 columnView->setShortcut(Qt::CTRL | Qt::Key_3);
1098 columnView->setIcon(KIcon("view-file-columns"));
1099 columnView->setData(QVariant::fromValue(ColumnView));
1100 return columnView;
1101 }
1102
1103 QString DolphinView::currentViewModeActionName() const
1104 {
1105 switch (m_mode) {
1106 case DolphinView::IconsView:
1107 return "icons";
1108 case DolphinView::DetailsView:
1109 return "details";
1110 case DolphinView::ColumnView:
1111 return "columns";
1112 }
1113 return QString(); // can't happen
1114 }
1115
1116 void DolphinView::renameSelectedItems()
1117 {
1118 const KFileItemList items = selectedItems();
1119 if (items.count() > 1) {
1120 // More than one item has been selected for renaming. Open
1121 // a rename dialog and rename all items afterwards.
1122 RenameDialog dialog(this, items);
1123 if (dialog.exec() == QDialog::Rejected) {
1124 return;
1125 }
1126
1127 const QString newName = dialog.newName();
1128 if (newName.isEmpty()) {
1129 emit errorMessage(dialog.errorString());
1130 } else {
1131 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1132 // as one operation instead of n rename operations like it is done now...
1133 Q_ASSERT(newName.contains('#'));
1134
1135 // iterate through all selected items and rename them...
1136 const int replaceIndex = newName.indexOf('#');
1137 Q_ASSERT(replaceIndex >= 0);
1138 int index = 1;
1139
1140 KFileItemList::const_iterator it = items.begin();
1141 const KFileItemList::const_iterator end = items.end();
1142 while (it != end) {
1143 const KUrl& oldUrl = (*it).url();
1144 QString number;
1145 number.setNum(index++);
1146
1147 QString name(newName);
1148 name.replace(replaceIndex, 1, number);
1149
1150 if (oldUrl.fileName() != name) {
1151 KUrl newUrl = oldUrl;
1152 newUrl.setFileName(name);
1153 KonqOperations::rename(this, oldUrl, newUrl);
1154 emit doingOperation(KonqFileUndoManager::RENAME);
1155 }
1156 ++it;
1157 }
1158 }
1159 } else {
1160 // Only one item has been selected for renaming. Use the custom
1161 // renaming mechanism from the views.
1162 Q_ASSERT(items.count() == 1);
1163
1164 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1165 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1166 // is a benefit for the user at all -> let's wait for some input first...
1167 RenameDialog dialog(this, items);
1168 if (dialog.exec() == QDialog::Rejected) {
1169 return;
1170 }
1171
1172 const QString& newName = dialog.newName();
1173 if (newName.isEmpty()) {
1174 emit errorMessage(dialog.errorString());
1175 } else {
1176 const KUrl& oldUrl = items.first().url();
1177 KUrl newUrl = oldUrl;
1178 newUrl.setFileName(newName);
1179 KonqOperations::rename(this, oldUrl, newUrl);
1180 emit doingOperation(KonqFileUndoManager::RENAME);
1181 }
1182 }
1183 }
1184
1185 void DolphinView::trashSelectedItems()
1186 {
1187 emit doingOperation(KonqFileUndoManager::TRASH);
1188 KonqOperations::del(this, KonqOperations::TRASH, selectedUrls());
1189 }
1190
1191 void DolphinView::deleteSelectedItems()
1192 {
1193 const KUrl::List list = selectedUrls();
1194 const bool del = KonqOperations::askDeleteConfirmation(list,
1195 KonqOperations::DEL,
1196 KonqOperations::DEFAULT_CONFIRMATION,
1197 this);
1198
1199 if (del) {
1200 KIO::Job* job = KIO::del(list);
1201 connect(job, SIGNAL(result(KJob*)),
1202 this, SLOT(slotDeleteFileFinished(KJob*)));
1203 }
1204 }
1205
1206 void DolphinView::slotDeleteFileFinished(KJob* job)
1207 {
1208 if (job->error() == 0) {
1209 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1210 } else {
1211 emit errorMessage(job->errorString());
1212 }
1213 }
1214
1215 void DolphinView::slotPreviewJobFinished(KJob* job)
1216 {
1217 Q_ASSERT(job == m_previewJob);
1218 m_previewJob = 0;
1219 }
1220
1221 void DolphinView::cutSelectedItems()
1222 {
1223 QMimeData* mimeData = new QMimeData();
1224 const KUrl::List kdeUrls = selectedUrls();
1225 const KUrl::List mostLocalUrls;
1226 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
1227 QApplication::clipboard()->setMimeData(mimeData);
1228 }
1229
1230 void DolphinView::copySelectedItems()
1231 {
1232 QMimeData* mimeData = new QMimeData();
1233 const KUrl::List kdeUrls = selectedUrls();
1234 const KUrl::List mostLocalUrls;
1235 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
1236 QApplication::clipboard()->setMimeData(mimeData);
1237 }
1238
1239 void DolphinView::paste()
1240 {
1241 QClipboard* clipboard = QApplication::clipboard();
1242 const QMimeData* mimeData = clipboard->mimeData();
1243
1244 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1245
1246 // per default the pasting is done into the current Url of the view
1247 KUrl destUrl(url());
1248
1249 // check whether the pasting should be done into a selected directory
1250 const KUrl::List selectedUrls = this->selectedUrls();
1251 if (selectedUrls.count() == 1) {
1252 const KFileItem fileItem(S_IFDIR,
1253 KFileItem::Unknown,
1254 selectedUrls.first(),
1255 true);
1256 if (fileItem.isDir()) {
1257 // only one item is selected which is a directory, hence paste
1258 // into this directory
1259 destUrl = selectedUrls.first();
1260 }
1261 }
1262
1263 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
1264 KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, destUrl);
1265 emit doingOperation(KonqFileUndoManager::MOVE);
1266 clipboard->clear();
1267 } else {
1268 KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, destUrl);
1269 emit doingOperation(KonqFileUndoManager::COPY);
1270 }
1271 }
1272
1273 QPair<bool, QString> DolphinView::pasteInfo() const
1274 {
1275 QPair<bool, QString> ret;
1276 QClipboard* clipboard = QApplication::clipboard();
1277 const QMimeData* mimeData = clipboard->mimeData();
1278
1279 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1280 if (!urls.isEmpty()) {
1281 ret.first = true;
1282 ret.second = i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls.count());
1283 } else {
1284 ret.first = false;
1285 ret.second = i18nc("@action:inmenu", "Paste");
1286 }
1287
1288 if (ret.first) {
1289 const KFileItemList items = selectedItems();
1290 const uint count = items.count();
1291 if (count > 1) {
1292 // pasting should not be allowed when more than one file
1293 // is selected
1294 ret.first = false;
1295 } else if (count == 1) {
1296 // Only one file is selected. Pasting is only allowed if this
1297 // file is a directory.
1298 ret.first = items.first().isDir();
1299 }
1300 }
1301 return ret;
1302 }
1303
1304 KAction* DolphinView::createRenameAction(KActionCollection* collection)
1305 {
1306 KAction* rename = collection->addAction("rename");
1307 rename->setText(i18nc("@action:inmenu File", "Rename..."));
1308 rename->setShortcut(Qt::Key_F2);
1309 return rename;
1310 }
1311
1312 KAction* DolphinView::createMoveToTrashAction(KActionCollection* collection)
1313 {
1314 KAction* moveToTrash = collection->addAction("move_to_trash");
1315 moveToTrash->setText(i18nc("@action:inmenu File", "Move to Trash"));
1316 moveToTrash->setIcon(KIcon("user-trash"));
1317 moveToTrash->setShortcut(QKeySequence::Delete);
1318 return moveToTrash;
1319 }
1320
1321 KAction* DolphinView::createDeleteAction(KActionCollection* collection)
1322 {
1323 KAction* deleteAction = collection->addAction("delete");
1324 deleteAction->setIcon(KIcon("edit-delete"));
1325 deleteAction->setText(i18nc("@action:inmenu File", "Delete"));
1326 deleteAction->setShortcut(Qt::SHIFT | Qt::Key_Delete);
1327 return deleteAction;
1328 }
1329
1330 KAction* DolphinView::createNewDirAction(KActionCollection* collection)
1331 {
1332 // This action doesn't appear in the GUI, it's for the shortcut only.
1333 // KNewMenu takes care of the GUI stuff.
1334 KAction* newDirAction = collection->addAction("create_dir");
1335 newDirAction->setText(i18n("Create Folder..."));
1336 newDirAction->setShortcut(Qt::Key_F10);
1337 return newDirAction;
1338 }
1339
1340 KAction* DolphinView::createSortDescendingAction(KActionCollection* collection)
1341 {
1342 KToggleAction* sortDescending = collection->add<KToggleAction>("descending");
1343 sortDescending->setText(i18nc("@action:inmenu Sort", "Descending"));
1344 return sortDescending;
1345 }
1346
1347 QActionGroup* DolphinView::createAdditionalInformationActionGroup(KActionCollection* collection)
1348 {
1349 QActionGroup* showInformationGroup = new QActionGroup(collection);
1350 showInformationGroup->setExclusive(false);
1351
1352 KToggleAction* showSizeInfo = collection->add<KToggleAction>("show_size_info");
1353 showSizeInfo->setText(i18nc("@action:inmenu Additional information", "Size"));
1354 showSizeInfo->setData(KFileItemDelegate::Size);
1355 showSizeInfo->setActionGroup(showInformationGroup);
1356
1357 KToggleAction* showDateInfo = collection->add<KToggleAction>("show_date_info");
1358 showDateInfo->setText(i18nc("@action:inmenu Additional information", "Date"));
1359 showDateInfo->setData(KFileItemDelegate::ModificationTime);
1360 showDateInfo->setActionGroup(showInformationGroup);
1361
1362 KToggleAction* showPermissionsInfo = collection->add<KToggleAction>("show_permissions_info");
1363 showPermissionsInfo->setText(i18nc("@action:inmenu Additional information", "Permissions"));
1364 showPermissionsInfo->setData(KFileItemDelegate::Permissions);
1365 showPermissionsInfo->setActionGroup(showInformationGroup);
1366
1367 KToggleAction* showOwnerInfo = collection->add<KToggleAction>("show_owner_info");
1368 showOwnerInfo->setText(i18nc("@action:inmenu Additional information", "Owner"));
1369 showOwnerInfo->setData(KFileItemDelegate::Owner);
1370 showOwnerInfo->setActionGroup(showInformationGroup);
1371
1372 KToggleAction* showGroupInfo = collection->add<KToggleAction>("show_group_info");
1373 showGroupInfo->setText(i18nc("@action:inmenu Additional information", "Group"));
1374 showGroupInfo->setData(KFileItemDelegate::OwnerAndGroup);
1375 showGroupInfo->setActionGroup(showInformationGroup);
1376
1377 KToggleAction* showMimeInfo = collection->add<KToggleAction>("show_mime_info");
1378 showMimeInfo->setText(i18nc("@action:inmenu Additional information", "Type"));
1379 showMimeInfo->setData(KFileItemDelegate::FriendlyMimeType);
1380 showMimeInfo->setActionGroup(showInformationGroup);
1381
1382 return showInformationGroup;
1383 }
1384
1385 #include "dolphinview.moc"