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