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