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