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