]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
prevent flickering when zooming in or zooming out
[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);
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);
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 m_iconManager->updatePreviews();
401 }
402
403 void DolphinView::zoomOut()
404 {
405 m_controller->triggerZoomOut();
406 m_iconManager->updatePreviews();
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);
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) {
541 const int delta = event->delta();
542 if ((delta > 0) && isZoomInPossible()) {
543 zoomIn();
544 } else if ((delta < 0) && isZoomOutPossible()) {
545 zoomOut();
546 }
547 event->accept();
548 }
549 }
550
551 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
552 {
553 if ((watched == itemView()) && (event->type() == QEvent::FocusIn)) {
554 m_controller->requestActivation();
555 }
556
557 return QWidget::eventFilter(watched, event);
558 }
559
560 void DolphinView::activate()
561 {
562 setActive(true);
563 }
564
565 void DolphinView::triggerItem(const KFileItem& item)
566 {
567 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
568 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
569 // items are selected by the user, hence don't trigger the
570 // item specified by 'index'
571 return;
572 }
573
574 if (item.isNull()) {
575 return;
576 }
577
578 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
579 }
580
581 void DolphinView::emitSelectionChangedSignal()
582 {
583 emit selectionChanged(DolphinView::selectedItems());
584 }
585
586 void DolphinView::loadDirectory(const KUrl& url, bool reload)
587 {
588 if (!url.isValid()) {
589 const QString location(url.pathOrUrl());
590 if (location.isEmpty()) {
591 emit errorMessage(i18nc("@info:status", "The location is empty."));
592 } else {
593 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
594 }
595 return;
596 }
597
598 m_loadingDirectory = true;
599
600 m_dirLister->stop();
601 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
602
603 if (isColumnViewActive()) {
604 // adjusting the directory lister is not enough in the case of the
605 // column view, as each column has its own directory lister internally...
606 if (reload) {
607 m_columnView->reload();
608 } else {
609 m_columnView->showColumn(url);
610 }
611 }
612 }
613
614 KUrl DolphinView::viewPropertiesUrl() const
615 {
616 if (isColumnViewActive()) {
617 return m_columnView->rootUrl();
618 }
619
620 return url();
621 }
622
623 void DolphinView::applyViewProperties(const KUrl& url)
624 {
625 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
626 // The column view is active, hence don't apply the view properties
627 // of sub directories (represented by columns) to the view. The
628 // view always represents the properties of the first column.
629 return;
630 }
631
632 const ViewProperties props(url);
633
634 const Mode mode = props.viewMode();
635 if (m_mode != mode) {
636 m_mode = mode;
637 createView();
638 emit modeChanged();
639 }
640 if (itemView() == 0) {
641 createView();
642 }
643 Q_ASSERT(itemView() != 0);
644 Q_ASSERT(m_fileItemDelegate != 0);
645
646 const bool showHiddenFiles = props.showHiddenFiles();
647 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
648 m_dirLister->setShowingDotFiles(showHiddenFiles);
649 emit showHiddenFilesChanged();
650 }
651
652 m_storedCategorizedSorting = props.categorizedSorting();
653 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
654 if (categorized != m_proxyModel->isCategorizedModel()) {
655 m_proxyModel->setCategorizedModel(categorized);
656 emit categorizedSortingChanged();
657 }
658
659 const DolphinView::Sorting sorting = props.sorting();
660 if (sorting != m_proxyModel->sorting()) {
661 m_proxyModel->setSorting(sorting);
662 emit sortingChanged(sorting);
663 }
664
665 const Qt::SortOrder sortOrder = props.sortOrder();
666 if (sortOrder != m_proxyModel->sortOrder()) {
667 m_proxyModel->setSortOrder(sortOrder);
668 emit sortOrderChanged(sortOrder);
669 }
670
671 KFileItemDelegate::InformationList info = props.additionalInfo();
672 if (info != m_fileItemDelegate->showInformation()) {
673 m_fileItemDelegate->setShowInformation(info);
674 emit additionalInfoChanged();
675 }
676
677 const bool showPreview = props.showPreview();
678 if (showPreview != m_showPreview) {
679 m_showPreview = showPreview;
680 m_iconManager->setShowPreview(showPreview);
681 emit showPreviewChanged();
682 }
683 }
684
685 void DolphinView::changeSelection(const KFileItemList& selection)
686 {
687 clearSelection();
688 if (selection.isEmpty()) {
689 return;
690 }
691 const KUrl& baseUrl = url();
692 KUrl url;
693 QItemSelection new_selection;
694 foreach(const KFileItem& item, selection) {
695 url = item.url().upUrl();
696 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
697 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
698 new_selection.select(index, index);
699 }
700 }
701 itemView()->selectionModel()->select(new_selection,
702 QItemSelectionModel::ClearAndSelect
703 | QItemSelectionModel::Current);
704 }
705
706 void DolphinView::openContextMenu(const QPoint& pos)
707 {
708 KFileItem item;
709
710 const QModelIndex index = itemView()->indexAt(pos);
711 if (index.isValid() && (index.column() == DolphinModel::Name)) {
712 item = fileItem(index);
713 }
714
715 emit requestContextMenu(item, url());
716 }
717
718 void DolphinView::dropUrls(const KUrl::List& urls,
719 const KUrl& destPath,
720 const KFileItem& destItem)
721 {
722 Q_ASSERT(!urls.isEmpty());
723 const KUrl& destination = !destItem.isNull() && destItem.isDir() ?
724 destItem.url() : destPath;
725 const KUrl sourceDir = KUrl(urls.first().directory());
726 if (sourceDir != destination) {
727 dropUrls(urls, destination);
728 }
729 }
730
731 void DolphinView::dropUrls(const KUrl::List& urls,
732 const KUrl& destination)
733 {
734 DolphinDropController dropController(this);
735 // forward doingOperation signal up to the mainwindow
736 connect(&dropController, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)),
737 this, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)));
738 dropController.dropUrls(urls, destination);
739 }
740
741 void DolphinView::updateSorting(DolphinView::Sorting sorting)
742 {
743 ViewProperties props(viewPropertiesUrl());
744 props.setSorting(sorting);
745
746 m_proxyModel->setSorting(sorting);
747
748 emit sortingChanged(sorting);
749 }
750
751 void DolphinView::updateSortOrder(Qt::SortOrder order)
752 {
753 ViewProperties props(viewPropertiesUrl());
754 props.setSortOrder(order);
755
756 m_proxyModel->setSortOrder(order);
757
758 emit sortOrderChanged(order);
759 }
760
761 void DolphinView::toggleSortOrder()
762 {
763 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
764 Qt::DescendingOrder :
765 Qt::AscendingOrder;
766 setSortOrder(order);
767 }
768
769 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
770 {
771 ViewProperties props(viewPropertiesUrl());
772 props.setAdditionalInfo(info);
773 props.save();
774
775 m_fileItemDelegate->setShowInformation(info);
776
777 emit additionalInfoChanged();
778 }
779
780 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
781 {
782 const bool enable = (m_mode == DolphinView::DetailsView) ||
783 (m_mode == DolphinView::IconsView);
784
785 QAction* showSizeInfo = collection->action("show_size_info");
786 QAction* showDateInfo = collection->action("show_date_info");
787 QAction* showPermissionsInfo = collection->action("show_permissions_info");
788 QAction* showOwnerInfo = collection->action("show_owner_info");
789 QAction* showGroupInfo = collection->action("show_group_info");
790 QAction* showMimeInfo = collection->action("show_mime_info");
791
792 showSizeInfo->setChecked(false);
793 showDateInfo->setChecked(false);
794 showPermissionsInfo->setChecked(false);
795 showOwnerInfo->setChecked(false);
796 showGroupInfo->setChecked(false);
797 showMimeInfo->setChecked(false);
798
799 showSizeInfo->setEnabled(enable);
800 showDateInfo->setEnabled(enable);
801 showPermissionsInfo->setEnabled(enable);
802 showOwnerInfo->setEnabled(enable);
803 showGroupInfo->setEnabled(enable);
804 showMimeInfo->setEnabled(enable);
805
806 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
807 switch (info) {
808 case KFileItemDelegate::Size:
809 showSizeInfo->setChecked(true);
810 break;
811 case KFileItemDelegate::ModificationTime:
812 showDateInfo->setChecked(true);
813 break;
814 case KFileItemDelegate::Permissions:
815 showPermissionsInfo->setChecked(true);
816 break;
817 case KFileItemDelegate::Owner:
818 showOwnerInfo->setChecked(true);
819 break;
820 case KFileItemDelegate::OwnerAndGroup:
821 showGroupInfo->setChecked(true);
822 break;
823 case KFileItemDelegate::FriendlyMimeType:
824 showMimeInfo->setChecked(true);
825 break;
826 default:
827 break;
828 }
829 }
830 }
831
832 void DolphinView::toggleAdditionalInfo(QAction* action)
833 {
834 const KFileItemDelegate::Information info =
835 static_cast<KFileItemDelegate::Information>(action->data().toInt());
836
837 KFileItemDelegate::InformationList list = additionalInfo();
838
839 const bool show = action->isChecked();
840
841 const int index = list.indexOf(info);
842 const bool containsInfo = (index >= 0);
843 if (show && !containsInfo) {
844 list.append(info);
845 setAdditionalInfo(list);
846 } else if (!show && containsInfo) {
847 list.removeAt(index);
848 setAdditionalInfo(list);
849 Q_ASSERT(list.indexOf(info) < 0);
850 }
851 }
852
853 void DolphinView::emitContentsMoved()
854 {
855 // only emit the contents moved signal if:
856 // - no directory loading is ongoing (this would reset the contents position
857 // always to (0, 0))
858 // - if the Column View is active: the column view does an automatic
859 // positioning during the loading operation, which must be remembered
860 if (!m_loadingDirectory || isColumnViewActive()) {
861 const QPoint pos(contentsPosition());
862 emit contentsMoved(pos.x(), pos.y());
863 }
864 }
865
866 void DolphinView::showHoverInformation(const KFileItem& item)
867 {
868 if (hasSelection() || !m_active) {
869 return;
870 }
871
872 emit requestItemInfo(item);
873 }
874
875 void DolphinView::clearHoverInformation()
876 {
877 if (m_active) {
878 emit requestItemInfo(KFileItem());
879 }
880 }
881
882 void DolphinView::createView()
883 {
884 deleteView();
885 Q_ASSERT(m_iconsView == 0);
886 Q_ASSERT(m_detailsView == 0);
887 Q_ASSERT(m_columnView == 0);
888
889 QAbstractItemView* view = 0;
890 switch (m_mode) {
891 case IconsView: {
892 m_iconsView = new DolphinIconsView(this, m_controller);
893 view = m_iconsView;
894 break;
895 }
896
897 case DetailsView:
898 m_detailsView = new DolphinDetailsView(this, m_controller);
899 view = m_detailsView;
900 break;
901
902 case ColumnView:
903 m_columnView = new DolphinColumnView(this, m_controller);
904 view = m_columnView;
905 break;
906 }
907
908 Q_ASSERT(view != 0);
909 view->installEventFilter(this);
910
911 m_controller->setItemView(view);
912
913 m_fileItemDelegate = new KFileItemDelegate(view);
914 view->setItemDelegate(m_fileItemDelegate);
915
916 view->setModel(m_proxyModel);
917 if (m_selectionModel != 0) {
918 view->setSelectionModel(m_selectionModel);
919 } else {
920 m_selectionModel = view->selectionModel();
921 }
922
923 // reparent the selection model, as it should not be deleted
924 // when deleting the model
925 m_selectionModel->setParent(this);
926
927 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
928
929 new KMimeTypeResolver(view, m_dolphinModel);
930 m_iconManager = new IconManager(view, m_proxyModel);
931 m_iconManager->setShowPreview(m_showPreview);
932
933 m_topLayout->insertWidget(1, view);
934
935 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
936 this, SLOT(emitSelectionChangedSignal()));
937 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
938 this, SLOT(emitContentsMoved()));
939 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
940 this, SLOT(emitContentsMoved()));
941 }
942
943 void DolphinView::deleteView()
944 {
945 QAbstractItemView* view = itemView();
946 if (view != 0) {
947 m_topLayout->removeWidget(view);
948 view->close();
949 view->deleteLater();
950 view = 0;
951 m_iconsView = 0;
952 m_detailsView = 0;
953 m_columnView = 0;
954 m_fileItemDelegate = 0;
955 m_iconManager = 0;
956 }
957 }
958
959 QAbstractItemView* DolphinView::itemView() const
960 {
961 if (m_detailsView != 0) {
962 return m_detailsView;
963 } else if (m_columnView != 0) {
964 return m_columnView;
965 }
966
967 return m_iconsView;
968 }
969
970 bool DolphinView::isCutItem(const KFileItem& item) const
971 {
972 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
973 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
974
975 const KUrl& itemUrl = item.url();
976 KUrl::List::const_iterator it = cutUrls.begin();
977 const KUrl::List::const_iterator end = cutUrls.end();
978 while (it != end) {
979 if (*it == itemUrl) {
980 return true;
981 }
982 ++it;
983 }
984
985 return false;
986 }
987
988 void DolphinView::renameSelectedItems()
989 {
990 const KFileItemList items = selectedItems();
991 if (items.count() > 1) {
992 // More than one item has been selected for renaming. Open
993 // a rename dialog and rename all items afterwards.
994 RenameDialog dialog(this, items);
995 if (dialog.exec() == QDialog::Rejected) {
996 return;
997 }
998
999 const QString newName = dialog.newName();
1000 if (newName.isEmpty()) {
1001 emit errorMessage(dialog.errorString());
1002 } else {
1003 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1004 // as one operation instead of n rename operations like it is done now...
1005 Q_ASSERT(newName.contains('#'));
1006
1007 // iterate through all selected items and rename them...
1008 int index = 1;
1009 foreach (KFileItem item, items) {
1010 const KUrl& oldUrl = item.url();
1011 QString number;
1012 number.setNum(index++);
1013
1014 QString name = newName;
1015 name.replace('#', number);
1016
1017 if (oldUrl.fileName() != name) {
1018 KUrl newUrl = oldUrl;
1019 newUrl.setFileName(name);
1020 KonqOperations::rename(this, oldUrl, newUrl);
1021 emit doingOperation(KonqFileUndoManager::RENAME);
1022 }
1023 }
1024 }
1025 } else {
1026 // Only one item has been selected for renaming. Use the custom
1027 // renaming mechanism from the views.
1028 Q_ASSERT(items.count() == 1);
1029
1030 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1031 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1032 // is a benefit for the user at all -> let's wait for some input first...
1033 RenameDialog dialog(this, items);
1034 if (dialog.exec() == QDialog::Rejected) {
1035 return;
1036 }
1037
1038 const QString& newName = dialog.newName();
1039 if (newName.isEmpty()) {
1040 emit errorMessage(dialog.errorString());
1041 } else {
1042 const KUrl& oldUrl = items.first().url();
1043 KUrl newUrl = oldUrl;
1044 newUrl.setFileName(newName);
1045 KonqOperations::rename(this, oldUrl, newUrl);
1046 emit doingOperation(KonqFileUndoManager::RENAME);
1047 }
1048 }
1049 }
1050
1051 void DolphinView::trashSelectedItems()
1052 {
1053 emit doingOperation(KonqFileUndoManager::TRASH);
1054 KonqOperations::del(this, KonqOperations::TRASH, selectedUrls());
1055 }
1056
1057 void DolphinView::deleteSelectedItems()
1058 {
1059 const KUrl::List list = selectedUrls();
1060 const bool del = KonqOperations::askDeleteConfirmation(list,
1061 KonqOperations::DEL,
1062 KonqOperations::DEFAULT_CONFIRMATION,
1063 this);
1064
1065 if (del) {
1066 KIO::Job* job = KIO::del(list);
1067 connect(job, SIGNAL(result(KJob*)),
1068 this, SLOT(slotDeleteFileFinished(KJob*)));
1069 }
1070 }
1071
1072 void DolphinView::slotDeleteFileFinished(KJob* job)
1073 {
1074 if (job->error() == 0) {
1075 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1076 } else {
1077 emit errorMessage(job->errorString());
1078 }
1079 }
1080
1081 void DolphinView::cutSelectedItems()
1082 {
1083 QMimeData* mimeData = new QMimeData();
1084 const KUrl::List kdeUrls = selectedUrls();
1085 const KUrl::List mostLocalUrls;
1086 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
1087 QApplication::clipboard()->setMimeData(mimeData);
1088 }
1089
1090 void DolphinView::copySelectedItems()
1091 {
1092 QMimeData* mimeData = new QMimeData();
1093 const KUrl::List kdeUrls = selectedUrls();
1094 const KUrl::List mostLocalUrls;
1095 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
1096 QApplication::clipboard()->setMimeData(mimeData);
1097 }
1098
1099 void DolphinView::paste()
1100 {
1101 QClipboard* clipboard = QApplication::clipboard();
1102 const QMimeData* mimeData = clipboard->mimeData();
1103
1104 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1105
1106 // per default the pasting is done into the current Url of the view
1107 KUrl destUrl(url());
1108
1109 // check whether the pasting should be done into a selected directory
1110 const KUrl::List selectedUrls = this->selectedUrls();
1111 if (selectedUrls.count() == 1) {
1112 const KFileItem fileItem(S_IFDIR,
1113 KFileItem::Unknown,
1114 selectedUrls.first(),
1115 true);
1116 if (fileItem.isDir()) {
1117 // only one item is selected which is a directory, hence paste
1118 // into this directory
1119 destUrl = selectedUrls.first();
1120 }
1121 }
1122
1123 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
1124 KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, destUrl);
1125 emit doingOperation(KonqFileUndoManager::MOVE);
1126 clipboard->clear();
1127 } else {
1128 KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, destUrl);
1129 emit doingOperation(KonqFileUndoManager::COPY);
1130 }
1131 }
1132
1133 QPair<bool, QString> DolphinView::pasteInfo() const
1134 {
1135 QPair<bool, QString> ret;
1136 QClipboard* clipboard = QApplication::clipboard();
1137 const QMimeData* mimeData = clipboard->mimeData();
1138
1139 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1140 if (!urls.isEmpty()) {
1141 ret.first = true;
1142 ret.second = i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls.count());
1143 } else {
1144 ret.first = false;
1145 ret.second = i18nc("@action:inmenu", "Paste");
1146 }
1147
1148 if (ret.first) {
1149 const KFileItemList items = selectedItems();
1150 const uint count = items.count();
1151 if (count > 1) {
1152 // pasting should not be allowed when more than one file
1153 // is selected
1154 ret.first = false;
1155 } else if (count == 1) {
1156 // Only one file is selected. Pasting is only allowed if this
1157 // file is a directory.
1158 ret.first = items.first().isDir();
1159 }
1160 }
1161 return ret;
1162 }
1163
1164 #include "dolphinview.moc"