]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Fixed issue that when renaming a variable number of items, that only one #-character...
[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 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(infoList);
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(info);
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(info);
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::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
798 {
799 ViewProperties props(viewPropertiesUrl());
800 props.setAdditionalInfo(info);
801 props.save();
802
803 m_fileItemDelegate->setShowInformation(info);
804
805 emit additionalInfoChanged(info);
806
807 }
808
809 void DolphinView::emitContentsMoved()
810 {
811 // only emit the contents moved signal if:
812 // - no directory loading is ongoing (this would reset the contents position
813 // always to (0, 0))
814 // - if the Column View is active: the column view does an automatic
815 // positioning during the loading operation, which must be remembered
816 if (!m_loadingDirectory || isColumnViewActive()) {
817 const QPoint pos(contentsPosition());
818 emit contentsMoved(pos.x(), pos.y());
819 }
820 }
821
822 void DolphinView::updateCutItems()
823 {
824 // restore the icons of all previously selected items to the
825 // original state...
826 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
827 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
828 while (it != end) {
829 const QModelIndex index = m_dolphinModel->indexForUrl((*it).url);
830 if (index.isValid()) {
831 m_dolphinModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
832 }
833 ++it;
834 }
835 m_cutItemsCache.clear();
836
837 // ... and apply an item effect to all currently cut items
838 applyCutItemEffect();
839 }
840
841 void DolphinView::showHoverInformation(const KFileItem& item)
842 {
843 if (hasSelection() || !m_active) {
844 return;
845 }
846
847 emit requestItemInfo(item);
848 }
849
850 void DolphinView::clearHoverInformation()
851 {
852 if (m_active) {
853 emit requestItemInfo(KFileItem());
854 }
855 }
856
857
858 void DolphinView::createView()
859 {
860 deleteView();
861 Q_ASSERT(m_iconsView == 0);
862 Q_ASSERT(m_detailsView == 0);
863 Q_ASSERT(m_columnView == 0);
864
865 QAbstractItemView* view = 0;
866 switch (m_mode) {
867 case IconsView: {
868 m_iconsView = new DolphinIconsView(this, m_controller);
869 view = m_iconsView;
870 break;
871 }
872
873 case DetailsView:
874 m_detailsView = new DolphinDetailsView(this, m_controller);
875 view = m_detailsView;
876 break;
877
878 case ColumnView:
879 m_columnView = new DolphinColumnView(this, m_controller);
880 view = m_columnView;
881 break;
882 }
883
884 Q_ASSERT(view != 0);
885
886 m_fileItemDelegate = new KFileItemDelegate(view);
887 view->setItemDelegate(m_fileItemDelegate);
888
889 view->setModel(m_proxyModel);
890 if (m_selectionModel != 0) {
891 view->setSelectionModel(m_selectionModel);
892 } else {
893 m_selectionModel = view->selectionModel();
894 }
895
896 // reparent the selection model, as it should not be deleted
897 // when deleting the model
898 m_selectionModel->setParent(this);
899
900 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
901
902 new KMimeTypeResolver(view, m_dolphinModel);
903 m_topLayout->insertWidget(1, view);
904
905 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
906 this, SLOT(emitSelectionChangedSignal()));
907 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
908 this, SLOT(emitContentsMoved()));
909 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
910 this, SLOT(emitContentsMoved()));
911 }
912
913 void DolphinView::deleteView()
914 {
915 QAbstractItemView* view = itemView();
916 if (view != 0) {
917 m_topLayout->removeWidget(view);
918 view->close();
919 view->deleteLater();
920 view = 0;
921 m_iconsView = 0;
922 m_detailsView = 0;
923 m_columnView = 0;
924 m_fileItemDelegate = 0;
925 }
926 }
927
928 QAbstractItemView* DolphinView::itemView() const
929 {
930 if (m_detailsView != 0) {
931 return m_detailsView;
932 } else if (m_columnView != 0) {
933 return m_columnView;
934 }
935
936 return m_iconsView;
937 }
938
939 bool DolphinView::isCutItem(const KFileItem& item) const
940 {
941 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
942 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
943
944 const KUrl& itemUrl = item.url();
945 KUrl::List::const_iterator it = cutUrls.begin();
946 const KUrl::List::const_iterator end = cutUrls.end();
947 while (it != end) {
948 if (*it == itemUrl) {
949 return true;
950 }
951 ++it;
952 }
953
954 return false;
955 }
956
957 void DolphinView::applyCutItemEffect()
958 {
959 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
960 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
961 return;
962 }
963
964 KFileItemList items(m_dirLister->items());
965 KFileItemList::const_iterator it = items.begin();
966 const KFileItemList::const_iterator end = items.end();
967 while (it != end) {
968 const KFileItem item = *it;
969 if (isCutItem(item)) {
970 const QModelIndex index = m_dolphinModel->indexForItem(item);
971 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
972 if (value.type() == QVariant::Icon) {
973 const QIcon icon(qvariant_cast<QIcon>(value));
974 QPixmap pixmap = icon.pixmap(128, 128);
975
976 // remember current pixmap for the item to be able
977 // to restore it when other items get cut
978 CutItem cutItem;
979 cutItem.url = item.url();
980 cutItem.pixmap = pixmap;
981 m_cutItemsCache.append(cutItem);
982
983 // apply icon effect to the cut item
984 KIconEffect iconEffect;
985 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
986 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
987 }
988 }
989 ++it;
990 }
991 }
992
993 KToggleAction* DolphinView::iconsModeAction(KActionCollection* actionCollection)
994 {
995 KToggleAction* iconsView = actionCollection->add<KToggleAction>("icons");
996 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
997 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
998 iconsView->setIcon(KIcon("view-list-icons"));
999 iconsView->setData(QVariant::fromValue(IconsView));
1000 return iconsView;
1001 }
1002
1003 KToggleAction* DolphinView::detailsModeAction(KActionCollection* actionCollection)
1004 {
1005 KToggleAction* detailsView = actionCollection->add<KToggleAction>("details");
1006 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
1007 detailsView->setShortcut(Qt::CTRL | Qt::Key_2);
1008 detailsView->setIcon(KIcon("view-list-details"));
1009 detailsView->setData(QVariant::fromValue(DetailsView));
1010 return detailsView;
1011 }
1012
1013 KToggleAction* DolphinView::columnsModeAction(KActionCollection* actionCollection)
1014 {
1015 KToggleAction* columnView = actionCollection->add<KToggleAction>("columns");
1016 columnView->setText(i18nc("@action:inmenu View Mode", "Columns"));
1017 columnView->setShortcut(Qt::CTRL | Qt::Key_3);
1018 columnView->setIcon(KIcon("view-file-columns"));
1019 columnView->setData(QVariant::fromValue(ColumnView));
1020 return columnView;
1021 }
1022
1023 QString DolphinView::currentViewModeActionName() const
1024 {
1025 switch (m_mode) {
1026 case DolphinView::IconsView:
1027 return "icons";
1028 case DolphinView::DetailsView:
1029 return "details";
1030 case DolphinView::ColumnView:
1031 return "columns";
1032 }
1033 return QString(); // can't happen
1034 }
1035
1036 void DolphinView::renameSelectedItems()
1037 {
1038 const KFileItemList items = selectedItems();
1039 if (items.count() > 1) {
1040 // More than one item has been selected for renaming. Open
1041 // a rename dialog and rename all items afterwards.
1042 RenameDialog dialog(this, items);
1043 if (dialog.exec() == QDialog::Rejected) {
1044 return;
1045 }
1046
1047 const QString newName = dialog.newName();
1048 if (newName.isEmpty()) {
1049 emit errorMessage(dialog.errorString());
1050 } else {
1051 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1052 // as one operation instead of n rename operations like it is done now...
1053 Q_ASSERT(newName.contains('#'));
1054
1055 // iterate through all selected items and rename them...
1056 int index = 1;
1057 foreach (KFileItem item, items) {
1058 const KUrl& oldUrl = item.url();
1059 QString number;
1060 number.setNum(index++);
1061
1062 QString name = newName;
1063 name.replace('#', number);
1064
1065 if (oldUrl.fileName() != name) {
1066 KUrl newUrl = oldUrl;
1067 newUrl.setFileName(name);
1068 KonqOperations::rename(this, oldUrl, newUrl);
1069 emit doingOperation(KonqFileUndoManager::RENAME);
1070 }
1071 }
1072 }
1073 } else {
1074 // Only one item has been selected for renaming. Use the custom
1075 // renaming mechanism from the views.
1076 Q_ASSERT(items.count() == 1);
1077
1078 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1079 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1080 // is a benefit for the user at all -> let's wait for some input first...
1081 RenameDialog dialog(this, items);
1082 if (dialog.exec() == QDialog::Rejected) {
1083 return;
1084 }
1085
1086 const QString& newName = dialog.newName();
1087 if (newName.isEmpty()) {
1088 emit errorMessage(dialog.errorString());
1089 } else {
1090 const KUrl& oldUrl = items.first().url();
1091 KUrl newUrl = oldUrl;
1092 newUrl.setFileName(newName);
1093 KonqOperations::rename(this, oldUrl, newUrl);
1094 emit doingOperation(KonqFileUndoManager::RENAME);
1095 }
1096 }
1097 }
1098
1099 void DolphinView::trashSelectedItems()
1100 {
1101 emit doingOperation(KonqFileUndoManager::TRASH);
1102 KonqOperations::del(this, KonqOperations::TRASH, selectedUrls());
1103 }
1104
1105 void DolphinView::deleteSelectedItems()
1106 {
1107 const KUrl::List list = selectedUrls();
1108 const bool del = KonqOperations::askDeleteConfirmation(list,
1109 KonqOperations::DEL,
1110 KonqOperations::DEFAULT_CONFIRMATION,
1111 this);
1112
1113 if (del) {
1114 KIO::Job* job = KIO::del(list);
1115 connect(job, SIGNAL(result(KJob*)),
1116 this, SLOT(slotDeleteFileFinished(KJob*)));
1117 }
1118 }
1119
1120 void DolphinView::slotDeleteFileFinished(KJob* job)
1121 {
1122 if (job->error() == 0) {
1123 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1124 } else {
1125 emit errorMessage(job->errorString());
1126 }
1127 }
1128
1129 void DolphinView::slotPreviewJobFinished(KJob* job)
1130 {
1131 Q_ASSERT(job == m_previewJob);
1132 m_previewJob = 0;
1133 }
1134
1135 void DolphinView::cutSelectedItems()
1136 {
1137 QMimeData* mimeData = new QMimeData();
1138 const KUrl::List kdeUrls = selectedUrls();
1139 const KUrl::List mostLocalUrls;
1140 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
1141 QApplication::clipboard()->setMimeData(mimeData);
1142 }
1143
1144 void DolphinView::copySelectedItems()
1145 {
1146 QMimeData* mimeData = new QMimeData();
1147 const KUrl::List kdeUrls = selectedUrls();
1148 const KUrl::List mostLocalUrls;
1149 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
1150 QApplication::clipboard()->setMimeData(mimeData);
1151 }
1152
1153 void DolphinView::paste()
1154 {
1155 QClipboard* clipboard = QApplication::clipboard();
1156 const QMimeData* mimeData = clipboard->mimeData();
1157
1158 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1159
1160 // per default the pasting is done into the current Url of the view
1161 KUrl destUrl(url());
1162
1163 // check whether the pasting should be done into a selected directory
1164 const KUrl::List selectedUrls = this->selectedUrls();
1165 if (selectedUrls.count() == 1) {
1166 const KFileItem fileItem(S_IFDIR,
1167 KFileItem::Unknown,
1168 selectedUrls.first(),
1169 true);
1170 if (fileItem.isDir()) {
1171 // only one item is selected which is a directory, hence paste
1172 // into this directory
1173 destUrl = selectedUrls.first();
1174 }
1175 }
1176
1177 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
1178 KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, destUrl);
1179 emit doingOperation(KonqFileUndoManager::MOVE);
1180 clipboard->clear();
1181 } else {
1182 KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, destUrl);
1183 emit doingOperation(KonqFileUndoManager::COPY);
1184 }
1185 }
1186
1187 QPair<bool, QString> DolphinView::pasteInfo() const
1188 {
1189 QPair<bool, QString> ret;
1190 QClipboard* clipboard = QApplication::clipboard();
1191 const QMimeData* mimeData = clipboard->mimeData();
1192
1193 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1194 if (!urls.isEmpty()) {
1195 ret.first = true;
1196 ret.second = i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls.count());
1197 } else {
1198 ret.first = false;
1199 ret.second = i18nc("@action:inmenu", "Paste");
1200 }
1201
1202 if (ret.first) {
1203 const KFileItemList items = selectedItems();
1204 const uint count = items.count();
1205 if (count > 1) {
1206 // pasting should not be allowed when more than one file
1207 // is selected
1208 ret.first = false;
1209 } else if (count == 1) {
1210 // Only one file is selected. Pasting is only allowed if this
1211 // file is a directory.
1212 ret.first = items.first().isDir();
1213 }
1214 }
1215 return ret;
1216 }
1217
1218 KAction* DolphinView::createRenameAction(KActionCollection* collection)
1219 {
1220 KAction* rename = collection->addAction("rename");
1221 rename->setText(i18nc("@action:inmenu File", "Rename..."));
1222 rename->setShortcut(Qt::Key_F2);
1223 return rename;
1224 }
1225
1226 KAction* DolphinView::createMoveToTrashAction(KActionCollection* collection)
1227 {
1228 KAction* moveToTrash = collection->addAction("move_to_trash");
1229 moveToTrash->setText(i18nc("@action:inmenu File", "Move to Trash"));
1230 moveToTrash->setIcon(KIcon("user-trash"));
1231 moveToTrash->setShortcut(QKeySequence::Delete);
1232 return moveToTrash;
1233 }
1234
1235 KAction* DolphinView::createDeleteAction(KActionCollection* collection)
1236 {
1237 KAction* deleteAction = collection->addAction("delete");
1238 deleteAction->setIcon(KIcon("edit-delete"));
1239 deleteAction->setText(i18nc("@action:inmenu File", "Delete"));
1240 deleteAction->setShortcut(Qt::SHIFT | Qt::Key_Delete);
1241 return deleteAction;
1242 }
1243
1244 #include "dolphinview.moc"