]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Implement renaming in dolphinpart.
[dolphin.git] / src / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22 #include <ktoggleaction.h>
23 #include <kactioncollection.h>
24
25 #include <QApplication>
26 #include <QClipboard>
27 #include <QKeyEvent>
28 #include <QItemSelection>
29 #include <QBoxLayout>
30 #include <QTimer>
31 #include <QScrollBar>
32
33 #include <kcolorscheme.h>
34 #include <kdirlister.h>
35 #include <kfileitemdelegate.h>
36 #include <klocale.h>
37 #include <kiconeffect.h>
38 #include <kio/netaccess.h>
39 #include <kio/previewjob.h>
40 #include <kmimetyperesolver.h>
41 #include <konqmimedata.h>
42 #include <konq_operations.h>
43 #include <kurl.h>
44
45 #include "dolphinmodel.h"
46 #include "dolphincolumnview.h"
47 #include "dolphincontroller.h"
48 #include "dolphinsortfilterproxymodel.h"
49 #include "dolphindetailsview.h"
50 #include "dolphiniconsview.h"
51 #include "renamedialog.h"
52 #include "viewproperties.h"
53 #include "dolphinsettings.h"
54 #include "dolphin_generalsettings.h"
55
56 DolphinView::DolphinView(QWidget* parent,
57 const KUrl& url,
58 KDirLister* dirLister,
59 DolphinModel* dolphinModel,
60 DolphinSortFilterProxyModel* proxyModel) :
61 QWidget(parent),
62 m_active(true),
63 m_showPreview(false),
64 m_loadingDirectory(false),
65 m_storedCategorizedSorting(false),
66 m_mode(DolphinView::IconsView),
67 m_topLayout(0),
68 m_controller(0),
69 m_iconsView(0),
70 m_detailsView(0),
71 m_columnView(0),
72 m_fileItemDelegate(0),
73 m_dolphinModel(dolphinModel),
74 m_dirLister(dirLister),
75 m_proxyModel(proxyModel)
76 {
77 setFocusPolicy(Qt::StrongFocus);
78 m_topLayout = new QVBoxLayout(this);
79 m_topLayout->setSpacing(0);
80 m_topLayout->setMargin(0);
81
82 QClipboard* clipboard = QApplication::clipboard();
83 connect(clipboard, SIGNAL(dataChanged()),
84 this, SLOT(updateCutItems()));
85
86 connect(m_dirLister, SIGNAL(completed()),
87 this, SLOT(updateCutItems()));
88 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
89 this, SLOT(generatePreviews(const KFileItemList&)));
90
91 m_controller = new DolphinController(this);
92 m_controller->setUrl(url);
93
94 // Receiver of the DolphinView signal 'urlChanged()' don't need
95 // to care whether the internal controller changed the URL already or whether
96 // the controller just requested an URL change and will be updated later.
97 // In both cases the URL has been changed:
98 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
99 this, SIGNAL(urlChanged(const KUrl&)));
100 connect(m_controller, SIGNAL(requestUrlChange(const KUrl&)),
101 this, SIGNAL(urlChanged(const KUrl&)));
102
103 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
104 this, SLOT(openContextMenu(const QPoint&)));
105 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&, const KFileItem&)),
106 this, SLOT(dropUrls(const KUrl::List&, const KUrl&, const KFileItem&)));
107 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
108 this, SLOT(updateSorting(DolphinView::Sorting)));
109 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
110 this, SLOT(updateSortOrder(Qt::SortOrder)));
111 connect(m_controller, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
112 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&)));
113 connect(m_controller, SIGNAL(itemTriggered(const KFileItem&)),
114 this, SLOT(triggerItem(const KFileItem&)));
115 connect(m_controller, SIGNAL(activated()),
116 this, SLOT(activate()));
117 connect(m_controller, SIGNAL(itemEntered(const KFileItem&)),
118 this, SLOT(showHoverInformation(const KFileItem&)));
119 connect(m_controller, SIGNAL(viewportEntered()),
120 this, SLOT(clearHoverInformation()));
121
122 applyViewProperties(url);
123 m_topLayout->addWidget(itemView());
124 }
125
126 DolphinView::~DolphinView()
127 {
128 }
129
130 const KUrl& DolphinView::url() const
131 {
132 return m_controller->url();
133 }
134
135 KUrl DolphinView::rootUrl() const
136 {
137 return isColumnViewActive() ? m_columnView->rootUrl() : url();
138 }
139
140 void DolphinView::setActive(bool active)
141 {
142 if (active == m_active) {
143 return;
144 }
145
146 m_active = active;
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 if (isColumnViewActive()) {
187 // When changing the mode in the column view, it makes sense
188 // to go back to the root URL of the column view automatically.
189 // Otherwise there it would not be possible to turn off the column view
190 // without focusing the first column.
191 const KUrl root = rootUrl();
192 setUrl(root);
193 m_controller->setUrl(root);
194 }
195
196 deleteView();
197
198 // It is important to read the view properties _after_ deleting the view,
199 // as e. g. the detail view might adjust the additional information properties
200 // after getting closed:
201 const KUrl viewPropsUrl = viewPropertiesUrl();
202 ViewProperties props(viewPropsUrl);
203 props.setViewMode(m_mode);
204
205 createView();
206
207 // the file item delegate has been recreated, apply the current
208 // additional information manually
209 const KFileItemDelegate::InformationList infoList = props.additionalInfo();
210 m_fileItemDelegate->setShowInformation(infoList);
211 emit additionalInfoChanged(infoList);
212
213 // Not all view modes support categorized sorting. Adjust the sorting model
214 // if changing the view mode results in a change of the categorized sorting
215 // capabilities.
216 m_storedCategorizedSorting = props.categorizedSorting();
217 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
218 if (categorized != m_proxyModel->isCategorizedModel()) {
219 m_proxyModel->setCategorizedModel(categorized);
220 emit categorizedSortingChanged();
221 }
222
223 loadDirectory(viewPropsUrl);
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 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
865
866 new KMimeTypeResolver(view, m_dolphinModel);
867 m_topLayout->insertWidget(1, view);
868
869 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
870 this, SLOT(emitSelectionChangedSignal()));
871 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
872 this, SLOT(emitContentsMoved()));
873 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
874 this, SLOT(emitContentsMoved()));
875 }
876
877 void DolphinView::deleteView()
878 {
879 QAbstractItemView* view = itemView();
880 if (view != 0) {
881 m_topLayout->removeWidget(view);
882 view->close();
883 view->deleteLater();
884 view = 0;
885 m_iconsView = 0;
886 m_detailsView = 0;
887 m_columnView = 0;
888 m_fileItemDelegate = 0;
889 }
890 }
891
892 QAbstractItemView* DolphinView::itemView() const
893 {
894 if (m_detailsView != 0) {
895 return m_detailsView;
896 } else if (m_columnView != 0) {
897 return m_columnView;
898 }
899
900 return m_iconsView;
901 }
902
903 bool DolphinView::isCutItem(const KFileItem& item) const
904 {
905 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
906 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
907
908 const KUrl& itemUrl = item.url();
909 KUrl::List::const_iterator it = cutUrls.begin();
910 const KUrl::List::const_iterator end = cutUrls.end();
911 while (it != end) {
912 if (*it == itemUrl) {
913 return true;
914 }
915 ++it;
916 }
917
918 return false;
919 }
920
921 void DolphinView::applyCutItemEffect()
922 {
923 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
924 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
925 return;
926 }
927
928 KFileItemList items(m_dirLister->items());
929 KFileItemList::const_iterator it = items.begin();
930 const KFileItemList::const_iterator end = items.end();
931 while (it != end) {
932 const KFileItem item = *it;
933 if (isCutItem(item)) {
934 const QModelIndex index = m_dolphinModel->indexForItem(item);
935 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
936 if (value.type() == QVariant::Icon) {
937 const QIcon icon(qvariant_cast<QIcon>(value));
938 QPixmap pixmap = icon.pixmap(128, 128);
939
940 // remember current pixmap for the item to be able
941 // to restore it when other items get cut
942 CutItem cutItem;
943 cutItem.url = item.url();
944 cutItem.pixmap = pixmap;
945 m_cutItemsCache.append(cutItem);
946
947 // apply icon effect to the cut item
948 KIconEffect iconEffect;
949 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
950 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
951 }
952 }
953 ++it;
954 }
955 }
956
957 KToggleAction* DolphinView::iconsModeAction(KActionCollection* actionCollection)
958 {
959 KToggleAction* iconsView = actionCollection->add<KToggleAction>("icons");
960 iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
961 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
962 iconsView->setIcon(KIcon("fileview-icon"));
963 iconsView->setData(QVariant::fromValue(IconsView));
964 return iconsView;
965 }
966
967 KToggleAction* DolphinView::detailsModeAction(KActionCollection* actionCollection)
968 {
969 KToggleAction* detailsView = actionCollection->add<KToggleAction>("details");
970 detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
971 detailsView->setShortcut(Qt::CTRL | Qt::Key_2);
972 detailsView->setIcon(KIcon("fileview-detailed"));
973 detailsView->setData(QVariant::fromValue(DetailsView));
974 return detailsView;
975 }
976
977 KToggleAction* DolphinView::columnsModeAction(KActionCollection* actionCollection)
978 {
979 KToggleAction* columnView = actionCollection->add<KToggleAction>("columns");
980 columnView->setText(i18nc("@action:inmenu View Mode", "Columns"));
981 columnView->setShortcut(Qt::CTRL | Qt::Key_3);
982 columnView->setIcon(KIcon("fileview-column"));
983 columnView->setData(QVariant::fromValue(ColumnView));
984 return columnView;
985 }
986
987 QString DolphinView::currentViewModeActionName() const
988 {
989 switch (m_mode) {
990 case DolphinView::IconsView:
991 return "icons";
992 case DolphinView::DetailsView:
993 return "details";
994 case DolphinView::ColumnView:
995 return "columns";
996 }
997 return QString(); // can't happen
998 }
999
1000 void DolphinView::renameSelectedItems()
1001 {
1002 const KFileItemList items = selectedItems();
1003 if (items.count() > 1) {
1004 // More than one item has been selected for renaming. Open
1005 // a rename dialog and rename all items afterwards.
1006 RenameDialog dialog(this, items);
1007 if (dialog.exec() == QDialog::Rejected) {
1008 return;
1009 }
1010
1011 const QString newName = dialog.newName();
1012 if (newName.isEmpty()) {
1013 emit errorMessage(dialog.errorString());
1014 } else {
1015 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1016 // as one operation instead of n rename operations like it is done now...
1017 Q_ASSERT(newName.contains('#'));
1018
1019 // iterate through all selected items and rename them...
1020 const int replaceIndex = newName.indexOf('#');
1021 Q_ASSERT(replaceIndex >= 0);
1022 int index = 1;
1023
1024 KFileItemList::const_iterator it = items.begin();
1025 const KFileItemList::const_iterator end = items.end();
1026 while (it != end) {
1027 const KUrl& oldUrl = (*it).url();
1028 QString number;
1029 number.setNum(index++);
1030
1031 QString name(newName);
1032 name.replace(replaceIndex, 1, number);
1033
1034 if (oldUrl.fileName() != name) {
1035 KUrl newUrl = oldUrl;
1036 newUrl.setFileName(name);
1037 KonqOperations::rename(this, oldUrl, newUrl);
1038 emit renaming();
1039 }
1040 ++it;
1041 }
1042 }
1043 } else {
1044 // Only one item has been selected for renaming. Use the custom
1045 // renaming mechanism from the views.
1046 Q_ASSERT(items.count() == 1);
1047
1048 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1049 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1050 // is a benefit for the user at all -> let's wait for some input first...
1051 RenameDialog dialog(this, items);
1052 if (dialog.exec() == QDialog::Rejected) {
1053 return;
1054 }
1055
1056 const QString& newName = dialog.newName();
1057 if (newName.isEmpty()) {
1058 emit errorMessage(dialog.errorString());
1059 } else {
1060 const KUrl& oldUrl = items.first().url();
1061 KUrl newUrl = oldUrl;
1062 newUrl.setFileName(newName);
1063 KonqOperations::rename(this, oldUrl, newUrl);
1064 emit renaming();
1065 }
1066 }
1067 }
1068
1069 #include "dolphinview.moc"