]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Initial version for a column view support (thanks a lot to Benjamin Meyer for QColumn...
[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 <QDropEvent>
26 #include <QItemSelectionModel>
27 #include <QMouseEvent>
28 #include <QVBoxLayout>
29
30 #include <kdirmodel.h>
31 #include <kfileitemdelegate.h>
32 #include <klocale.h>
33 #include <kiconeffect.h>
34 #include <kio/netaccess.h>
35 #include <kio/renamedialog.h>
36 #include <kio/previewjob.h>
37 #include <kmimetyperesolver.h>
38 #include <konqmimedata.h>
39 #include <konq_operations.h>
40 #include <kurl.h>
41
42 #include "dolphincolumnview.h"
43 #include "dolphincontroller.h"
44 #include "dolphinstatusbar.h"
45 #include "dolphinmainwindow.h"
46 #include "dolphindirlister.h"
47 #include "dolphinsortfilterproxymodel.h"
48 #include "dolphindetailsview.h"
49 #include "dolphiniconsview.h"
50 #include "dolphincontextmenu.h"
51 #include "filterbar.h"
52 #include "renamedialog.h"
53 #include "urlnavigator.h"
54 #include "viewproperties.h"
55 #include "dolphinsettings.h"
56
57 DolphinView::DolphinView(DolphinMainWindow* mainWindow,
58 QWidget* parent,
59 const KUrl& url,
60 Mode mode,
61 bool showHiddenFiles) :
62 QWidget(parent),
63 m_showProgress(false),
64 m_blockContentsMovedSignal(false),
65 m_mode(mode),
66 m_iconSize(0),
67 m_folderCount(0),
68 m_fileCount(0),
69 m_mainWindow(mainWindow),
70 m_topLayout(0),
71 m_urlNavigator(0),
72 m_controller(0),
73 m_iconsView(0),
74 m_detailsView(0),
75 m_columnView(0),
76 m_fileItemDelegate(0),
77 m_filterBar(0),
78 m_statusBar(0),
79 m_dirModel(0),
80 m_dirLister(0),
81 m_proxyModel(0)
82 {
83 hide();
84 setFocusPolicy(Qt::StrongFocus);
85 m_topLayout = new QVBoxLayout(this);
86 m_topLayout->setSpacing(0);
87 m_topLayout->setMargin(0);
88
89 connect(m_mainWindow, SIGNAL(activeViewChanged()),
90 this, SLOT(updateActivationState()));
91
92 QClipboard* clipboard = QApplication::clipboard();
93 connect(clipboard, SIGNAL(dataChanged()),
94 this, SLOT(updateCutItems()));
95
96 m_urlNavigator = new UrlNavigator(DolphinSettings::instance().bookmarkManager(), url, this);
97 m_urlNavigator->setShowHiddenFiles(showHiddenFiles);
98 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
99 this, SLOT(loadDirectory(const KUrl&)));
100 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&)),
101 this, SLOT(dropUrls(const KUrl::List&, const KUrl&)));
102 connect(m_urlNavigator, SIGNAL(activated()),
103 this, SLOT(requestActivation()));
104 connect(this, SIGNAL(contentsMoved(int, int)),
105 m_urlNavigator, SLOT(storeContentsPosition(int, int)));
106
107 m_statusBar = new DolphinStatusBar(this);
108
109 m_dirLister = new DolphinDirLister();
110 m_dirLister->setAutoUpdate(true);
111 m_dirLister->setMainWindow(this);
112 m_dirLister->setShowingDotFiles(showHiddenFiles);
113 m_dirLister->setDelayedMimeTypes(true);
114
115 connect(m_dirLister, SIGNAL(clear()),
116 this, SLOT(updateStatusBar()));
117 connect(m_dirLister, SIGNAL(percent(int)),
118 this, SLOT(updateProgress(int)));
119 connect(m_dirLister, SIGNAL(deleteItem(KFileItem*)),
120 this, SLOT(updateStatusBar()));
121 connect(m_dirLister, SIGNAL(completed()),
122 this, SLOT(updateItemCount()));
123 connect(m_dirLister, SIGNAL(completed()),
124 this, SLOT(updateCutItems()));
125 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
126 this, SLOT(generatePreviews(const KFileItemList&)));
127 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
128 this, SLOT(showInfoMessage(const QString&)));
129 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
130 this, SLOT(showErrorMessage(const QString&)));
131
132 m_dirModel = new KDirModel();
133 m_dirModel->setDirLister(m_dirLister);
134 m_dirModel->setDropsAllowed(KDirModel::DropOnDirectory);
135
136 m_proxyModel = new DolphinSortFilterProxyModel(this);
137 m_proxyModel->setSourceModel(m_dirModel);
138
139 m_controller = new DolphinController(this);
140 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
141 this, SLOT(openContextMenu(const QPoint&)));
142 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&, QWidget*)),
143 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&, QWidget*)));
144 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
145 this, SLOT(updateSorting(DolphinView::Sorting)));
146 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
147 this, SLOT(updateSortOrder(Qt::SortOrder)));
148 connect(m_controller, SIGNAL(itemTriggered(const QModelIndex&)),
149 this, SLOT(triggerItem(const QModelIndex&)));
150 connect(m_controller, SIGNAL(selectionChanged()),
151 this, SLOT(emitSelectionChangedSignal()));
152 connect(m_controller, SIGNAL(activated()),
153 this, SLOT(requestActivation()));
154
155 createView();
156
157 m_iconSize = K3Icon::SizeMedium;
158
159 m_filterBar = new FilterBar(this);
160 m_filterBar->hide();
161 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
162 this, SLOT(changeNameFilter(const QString&)));
163 connect(m_filterBar, SIGNAL(closeRequest()),
164 this, SLOT(closeFilterBar()));
165
166 m_topLayout->addWidget(m_urlNavigator);
167 m_topLayout->addWidget(itemView());
168 m_topLayout->addWidget(m_filterBar);
169 m_topLayout->addWidget(m_statusBar);
170 }
171
172 DolphinView::~DolphinView()
173 {
174 delete m_dirLister;
175 m_dirLister = 0;
176 }
177
178 void DolphinView::setUrl(const KUrl& url)
179 {
180 m_urlNavigator->setUrl(url);
181 m_controller->setUrl(url);
182 }
183
184 const KUrl& DolphinView::url() const
185 {
186 return m_urlNavigator->url();
187 }
188
189 bool DolphinView::isActive() const
190 {
191 return m_mainWindow->activeView() == this;
192 }
193
194 void DolphinView::setMode(Mode mode)
195 {
196 if (mode == m_mode) {
197 return; // the wished mode is already set
198 }
199
200 m_mode = mode;
201
202 ViewProperties props(m_urlNavigator->url());
203 props.setViewMode(m_mode);
204
205 createView();
206 startDirLister(m_urlNavigator->url());
207
208 emit modeChanged();
209 }
210
211 DolphinView::Mode DolphinView::mode() const
212 {
213 return m_mode;
214 }
215
216 void DolphinView::setShowPreview(bool show)
217 {
218 ViewProperties props(m_urlNavigator->url());
219 props.setShowPreview(show);
220
221 m_controller->setShowPreview(show);
222
223 emit showPreviewChanged();
224 reload();
225 }
226
227 bool DolphinView::showPreview() const
228 {
229 return m_controller->showPreview();
230 }
231
232 void DolphinView::setShowHiddenFiles(bool show)
233 {
234 if (m_dirLister->showingDotFiles() == show) {
235 return;
236 }
237
238 ViewProperties props(m_urlNavigator->url());
239 props.setShowHiddenFiles(show);
240 props.save();
241
242 m_dirLister->setShowingDotFiles(show);
243 m_urlNavigator->setShowHiddenFiles(show);
244
245 emit showHiddenFilesChanged();
246
247 reload();
248 }
249
250 bool DolphinView::showHiddenFiles() const
251 {
252 return m_dirLister->showingDotFiles();
253 }
254
255 void DolphinView::renameSelectedItems()
256 {
257 DolphinView* view = mainWindow()->activeView();
258 const KUrl::List urls = selectedUrls();
259 if (urls.count() > 1) {
260 // More than one item has been selected for renaming. Open
261 // a rename dialog and rename all items afterwards.
262 RenameDialog dialog(urls);
263 if (dialog.exec() == QDialog::Rejected) {
264 return;
265 }
266
267 const QString& newName = dialog.newName();
268 if (newName.isEmpty()) {
269 view->statusBar()->setMessage(dialog.errorString(),
270 DolphinStatusBar::Error);
271 }
272 else {
273 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
274 // as one operation instead of n rename operations like it is done now...
275 Q_ASSERT(newName.contains('#'));
276
277 // iterate through all selected items and rename them...
278 const int replaceIndex = newName.indexOf('#');
279 Q_ASSERT(replaceIndex >= 0);
280 int index = 1;
281
282 KUrl::List::const_iterator it = urls.begin();
283 KUrl::List::const_iterator end = urls.end();
284 while (it != end) {
285 const KUrl& oldUrl = *it;
286 QString number;
287 number.setNum(index++);
288
289 QString name(newName);
290 name.replace(replaceIndex, 1, number);
291
292 if (oldUrl.fileName() != name) {
293 KUrl newUrl(oldUrl.upUrl());
294 newUrl.addPath(name);
295 m_mainWindow->rename(oldUrl, newUrl);
296 }
297 ++it;
298 }
299 }
300 }
301 else {
302 // Only one item has been selected for renaming. Use the custom
303 // renaming mechanism from the views.
304 Q_ASSERT(urls.count() == 1);
305
306 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
307 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
308 // is a benefit for the user at all -> let's wait for some input first...
309 RenameDialog dialog(urls);
310 if (dialog.exec() == QDialog::Rejected) {
311 return;
312 }
313
314 const QString& newName = dialog.newName();
315 if (newName.isEmpty()) {
316 view->statusBar()->setMessage(dialog.errorString(),
317 DolphinStatusBar::Error);
318 }
319 else {
320 const KUrl& oldUrl = urls.first();
321 KUrl newUrl = oldUrl.upUrl();
322 newUrl.addPath(newName);
323 m_mainWindow->rename(oldUrl, newUrl);
324 }
325 }
326 }
327
328 void DolphinView::selectAll()
329 {
330 selectAll(QItemSelectionModel::Select);
331 }
332
333 void DolphinView::invertSelection()
334 {
335 selectAll(QItemSelectionModel::Toggle);
336 }
337
338 DolphinStatusBar* DolphinView::statusBar() const
339 {
340 return m_statusBar;
341 }
342
343 int DolphinView::contentsX() const
344 {
345
346 return itemView()->horizontalScrollBar()->value();
347 }
348
349 int DolphinView::contentsY() const
350 {
351 return itemView()->verticalScrollBar()->value();
352 }
353
354 void DolphinView::refreshSettings()
355 {
356 startDirLister(m_urlNavigator->url());
357 }
358
359 void DolphinView::emitRequestItemInfo(const KUrl& url)
360 {
361 emit requestItemInfo(url);
362 }
363
364 bool DolphinView::isFilterBarVisible() const
365 {
366 return m_filterBar->isVisible();
367 }
368
369 bool DolphinView::isUrlEditable() const
370 {
371 return m_urlNavigator->isUrlEditable();
372 }
373
374 void DolphinView::zoomIn()
375 {
376 m_controller->triggerZoomIn();
377 }
378
379 void DolphinView::zoomOut()
380 {
381 m_controller->triggerZoomOut();
382 }
383
384 bool DolphinView::isZoomInPossible() const
385 {
386 return m_controller->isZoomInPossible();
387 }
388
389 bool DolphinView::isZoomOutPossible() const
390 {
391 return m_controller->isZoomOutPossible();
392 }
393
394 void DolphinView::setSorting(Sorting sorting)
395 {
396 if (sorting != this->sorting()) {
397 updateSorting(sorting);
398 }
399 }
400
401 DolphinView::Sorting DolphinView::sorting() const
402 {
403 return m_proxyModel->sorting();
404 }
405
406 void DolphinView::setSortOrder(Qt::SortOrder order)
407 {
408 if (sortOrder() != order) {
409 updateSortOrder(order);
410 }
411 }
412
413 Qt::SortOrder DolphinView::sortOrder() const
414 {
415 return m_proxyModel->sortOrder();
416 }
417
418 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
419 {
420 ViewProperties props(m_urlNavigator->url());
421 props.setAdditionalInfo(info);
422
423 m_fileItemDelegate->setAdditionalInformation(info);
424
425 emit additionalInfoChanged(info);
426 reload();
427 }
428
429 KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
430 {
431 return m_fileItemDelegate->additionalInformation();
432 }
433
434 void DolphinView::goBack()
435 {
436 m_urlNavigator->goBack();
437 }
438
439 void DolphinView::goForward()
440 {
441 m_urlNavigator->goForward();
442 }
443
444 void DolphinView::goUp()
445 {
446 m_urlNavigator->goUp();
447 }
448
449 void DolphinView::goHome()
450 {
451 m_urlNavigator->goHome();
452 }
453
454 void DolphinView::setUrlEditable(bool editable)
455 {
456 m_urlNavigator->editUrl(editable);
457 }
458
459 bool DolphinView::hasSelection() const
460 {
461 return itemView()->selectionModel()->hasSelection();
462 }
463
464 void DolphinView::clearSelection()
465 {
466 itemView()->selectionModel()->clear();
467 }
468
469 KFileItemList DolphinView::selectedItems() const
470 {
471 const QAbstractItemView* view = itemView();
472
473 // Our view has a selection, we will map them back to the DirModel
474 // and then fill the KFileItemList.
475 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
476
477 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
478 KFileItemList itemList;
479
480 const QModelIndexList indexList = selection.indexes();
481 QModelIndexList::const_iterator end = indexList.end();
482 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
483 Q_ASSERT((*it).isValid());
484
485 KFileItem* item = m_dirModel->itemForIndex(*it);
486 if (item != 0) {
487 itemList.append(item);
488 }
489 }
490
491 return itemList;
492 }
493
494 KUrl::List DolphinView::selectedUrls() const
495 {
496 KUrl::List urls;
497
498 const KFileItemList list = selectedItems();
499 KFileItemList::const_iterator it = list.begin();
500 const KFileItemList::const_iterator end = list.end();
501 while (it != end) {
502 KFileItem* item = *it;
503 urls.append(item->url());
504 ++it;
505 }
506
507 return urls;
508 }
509
510 KFileItem* DolphinView::fileItem(const QModelIndex index) const
511 {
512 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
513 return m_dirModel->itemForIndex(dirModelIndex);
514 }
515
516 void DolphinView::rename(const KUrl& source, const QString& newName)
517 {
518 bool ok = false;
519
520 if (newName.isEmpty() || (source.fileName() == newName)) {
521 return;
522 }
523
524 KUrl dest(source.upUrl());
525 dest.addPath(newName);
526
527 const bool destExists = KIO::NetAccess::exists(dest,
528 false,
529 mainWindow()->activeView());
530 if (destExists) {
531 // the destination already exists, hence ask the user
532 // how to proceed...
533 KIO::RenameDialog renameDialog(this,
534 i18n("File Already Exists"),
535 source.path(),
536 dest.path(),
537 KIO::M_OVERWRITE);
538 switch (renameDialog.exec()) {
539 case KIO::R_OVERWRITE:
540 // the destination should be overwritten
541 ok = KIO::NetAccess::file_move(source, dest, -1, true);
542 break;
543
544 case KIO::R_RENAME: {
545 // a new name for the destination has been used
546 KUrl newDest(renameDialog.newDestUrl());
547 ok = KIO::NetAccess::file_move(source, newDest);
548 break;
549 }
550
551 default:
552 // the renaming operation has been canceled
553 reload();
554 return;
555 }
556 }
557 else {
558 // no destination exists, hence just move the file to
559 // do the renaming
560 ok = KIO::NetAccess::file_move(source, dest);
561 }
562
563 const QString destFileName = dest.fileName();
564 if (ok) {
565 m_statusBar->setMessage(i18n("Renamed file '%1' to '%2'.",source.fileName(), destFileName),
566 DolphinStatusBar::OperationCompleted);
567
568 KonqOperations::rename(this, source, destFileName);
569 }
570 else {
571 m_statusBar->setMessage(i18n("Renaming of file '%1' to '%2' failed.",source.fileName(), destFileName),
572 DolphinStatusBar::Error);
573 reload();
574 }
575 }
576
577 void DolphinView::reload()
578 {
579 startDirLister(m_urlNavigator->url(), true);
580 }
581
582 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
583 {
584 QWidget::mouseReleaseEvent(event);
585 mainWindow()->setActiveView(this);
586 }
587
588 DolphinMainWindow* DolphinView::mainWindow() const
589 {
590 return m_mainWindow;
591 }
592
593 void DolphinView::loadDirectory(const KUrl& url)
594 {
595 if(!isActive()) {
596 requestActivation();
597 }
598
599 const ViewProperties props(url);
600
601 const Mode mode = props.viewMode();
602 bool changeMode = (m_mode != mode);
603 if (changeMode && isColumnViewActive()) {
604 // The column view is active. Only change the
605 // mode if the current URL is no child of the column view.
606 if (m_dirLister->url().isParentOf(url)) {
607 changeMode = false;
608 }
609 }
610
611 if (changeMode) {
612 m_mode = mode;
613 createView();
614 emit modeChanged();
615 }
616
617 const bool showHiddenFiles = props.showHiddenFiles();
618 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
619 m_dirLister->setShowingDotFiles(showHiddenFiles);
620 emit showHiddenFilesChanged();
621 }
622
623 const DolphinView::Sorting sorting = props.sorting();
624 if (sorting != m_proxyModel->sorting()) {
625 m_proxyModel->setSorting(sorting);
626 emit sortingChanged(sorting);
627 }
628
629 const Qt::SortOrder sortOrder = props.sortOrder();
630 if (sortOrder != m_proxyModel->sortOrder()) {
631 m_proxyModel->setSortOrder(sortOrder);
632 emit sortOrderChanged(sortOrder);
633 }
634
635 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
636 if (info != m_fileItemDelegate->additionalInformation()) {
637 m_fileItemDelegate->setAdditionalInformation(info);
638
639 emit additionalInfoChanged(info);
640 }
641
642 const bool showPreview = props.showPreview();
643 if (showPreview != m_controller->showPreview()) {
644 m_controller->setShowPreview(showPreview);
645 emit showPreviewChanged();
646 }
647
648 startDirLister(url);
649 emit urlChanged(url);
650
651 m_statusBar->clear();
652 }
653
654 void DolphinView::triggerItem(const QModelIndex& index)
655 {
656 if (!isValidNameIndex(index)) {
657 return;
658 }
659
660 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
661 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
662 // items are selected by the user, hence don't trigger the
663 // item specified by 'index'
664 return;
665 }
666
667 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
668 if (item == 0) {
669 return;
670 }
671
672 // Prefer the local path over the URL. This assures that the
673 // volume space information is correct. Assuming that the URL is media:/sda1,
674 // and the local path is /windows/C: For the URL the space info is related
675 // to the root partition (and hence wrong) and for the local path the space
676 // info is related to the windows partition (-> correct).
677 const QString localPath(item->localPath());
678 KUrl url;
679 if (localPath.isEmpty()) {
680 url = item->url();
681 }
682 else {
683 url = localPath;
684 }
685
686 if (item->isDir()) {
687 setUrl(url);
688 }
689 else if (item->isFile()) {
690 // allow to browse through ZIP and tar files
691 KMimeType::Ptr mime = item->mimeTypePtr();
692 if (mime->is("application/zip")) {
693 url.setProtocol("zip");
694 setUrl(url);
695 }
696 else if (mime->is("application/x-tar") ||
697 mime->is("application/x-tarz") ||
698 mime->is("application/x-bzip-compressed-tar") ||
699 mime->is("application/x-compressed-tar") ||
700 mime->is("application/x-tzo")) {
701 url.setProtocol("tar");
702 setUrl(url);
703 }
704 else {
705 item->run();
706 }
707 }
708 else {
709 item->run();
710 }
711 }
712
713 void DolphinView::updateProgress(int percent)
714 {
715 if (m_showProgress) {
716 m_statusBar->setProgress(percent);
717 }
718 }
719
720 void DolphinView::updateItemCount()
721 {
722 if (m_showProgress) {
723 m_statusBar->setProgressText(QString());
724 m_statusBar->setProgress(100);
725 m_showProgress = false;
726 }
727
728 KFileItemList items(m_dirLister->items());
729 KFileItemList::const_iterator it = items.begin();
730 const KFileItemList::const_iterator end = items.end();
731
732 m_fileCount = 0;
733 m_folderCount = 0;
734
735 while (it != end) {
736 KFileItem* item = *it;
737 if (item->isDir()) {
738 ++m_folderCount;
739 }
740 else {
741 ++m_fileCount;
742 }
743 ++it;
744 }
745
746 updateStatusBar();
747
748 m_blockContentsMovedSignal = false;
749 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
750 }
751
752 void DolphinView::generatePreviews(const KFileItemList& items)
753 {
754 if (m_controller->showPreview()) {
755 KIO::PreviewJob* job = KIO::filePreview(items, 128);
756 connect(job, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)),
757 this, SLOT(showPreview(const KFileItem*, const QPixmap&)));
758 }
759 }
760
761 void DolphinView::showPreview(const KFileItem* item, const QPixmap& pixmap)
762 {
763 Q_ASSERT(item != 0);
764 const QModelIndex idx = m_dirModel->indexForItem(*item);
765 if (idx.isValid() && (idx.column() == 0)) {
766 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
767 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(*item)) {
768 KIconEffect iconEffect;
769 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
770 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
771 }
772 else {
773 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
774 }
775 }
776 }
777
778 void DolphinView::restoreContentsPos()
779 {
780 UrlNavigator::HistoryElem historyItem = m_urlNavigator->currentHistoryItem();
781 if (!historyItem.url().isEmpty()) {
782 QAbstractItemView* view = itemView();
783 // TODO: view->setCurrentItem(historyItem.currentFileName());
784 view->horizontalScrollBar()->setValue(historyItem.contentsX());
785 view->verticalScrollBar()->setValue(historyItem.contentsY());
786 }
787 }
788
789 void DolphinView::showInfoMessage(const QString& msg)
790 {
791 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
792 }
793
794 void DolphinView::showErrorMessage(const QString& msg)
795 {
796 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
797 }
798
799 void DolphinView::emitSelectionChangedSignal()
800 {
801 emit selectionChanged(DolphinView::selectedItems());
802 }
803
804 void DolphinView::closeFilterBar()
805 {
806 m_filterBar->hide();
807 emit showFilterBarChanged(false);
808 }
809
810 void DolphinView::startDirLister(const KUrl& url, bool reload)
811 {
812 if (!url.isValid()) {
813 const QString location(url.pathOrUrl());
814 if (location.isEmpty()) {
815 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
816 }
817 else {
818 m_statusBar->setMessage(i18n("The location '%1' is invalid.",location),
819 DolphinStatusBar::Error);
820 }
821 return;
822 }
823
824 // Only show the directory loading progress if the status bar does
825 // not contain another progress information. This means that
826 // the directory loading progress information has the lowest priority.
827 const QString progressText(m_statusBar->progressText());
828 m_showProgress = progressText.isEmpty() ||
829 (progressText == i18n("Loading directory..."));
830 if (m_showProgress) {
831 m_statusBar->setProgressText(i18n("Loading directory..."));
832 m_statusBar->setProgress(0);
833 }
834
835 m_cutItemsCache.clear();
836 m_blockContentsMovedSignal = true;
837 m_dirLister->stop();
838
839 bool keepOldDirs = isColumnViewActive();
840 if (keepOldDirs && !m_dirLister->url().isParentOf(url)) {
841 // The current URL is not a child of the dir lister
842 // URL. This may happen when e. g. a bookmark has been selected
843 // and hence the view must be reset.
844 keepOldDirs = false;
845 }
846 m_dirLister->openUrl(url, keepOldDirs, reload);
847 }
848
849 QString DolphinView::defaultStatusBarText() const
850 {
851 return KIO::itemsSummaryString(m_fileCount + m_folderCount,
852 m_fileCount,
853 m_folderCount,
854 0, false);
855 }
856
857 QString DolphinView::selectionStatusBarText() const
858 {
859 QString text;
860 const KFileItemList list = selectedItems();
861 if (list.isEmpty()) {
862 // when an item is triggered, it is temporary selected but selectedItems()
863 // will return an empty list
864 return QString();
865 }
866
867 int fileCount = 0;
868 int folderCount = 0;
869 KIO::filesize_t byteSize = 0;
870 KFileItemList::const_iterator it = list.begin();
871 const KFileItemList::const_iterator end = list.end();
872 while (it != end){
873 KFileItem* item = *it;
874 if (item->isDir()) {
875 ++folderCount;
876 }
877 else {
878 ++fileCount;
879 byteSize += item->size();
880 }
881 ++it;
882 }
883
884 if (folderCount > 0) {
885 text = i18np("1 Folder selected", "%1 Folders selected", folderCount);
886 if (fileCount > 0) {
887 text += ", ";
888 }
889 }
890
891 if (fileCount > 0) {
892 const QString sizeText(KIO::convertSize(byteSize));
893 text += i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount, sizeText);
894 }
895
896 return text;
897 }
898
899 void DolphinView::showFilterBar(bool show)
900 {
901 Q_ASSERT(m_filterBar != 0);
902 if (show) {
903 m_filterBar->show();
904 }
905 else {
906 m_filterBar->hide();
907 }
908 }
909
910 void DolphinView::updateStatusBar()
911 {
912 // As the item count information is less important
913 // in comparison with other messages, it should only
914 // be shown if:
915 // - the status bar is empty or
916 // - shows already the item count information or
917 // - shows only a not very important information
918 // - if any progress is given don't show the item count info at all
919 const QString msg(m_statusBar->message());
920 const bool updateStatusBarMsg = (msg.isEmpty() ||
921 (msg == m_statusBar->defaultText()) ||
922 (m_statusBar->type() == DolphinStatusBar::Information)) &&
923 (m_statusBar->progress() == 100);
924
925 const QString text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
926 m_statusBar->setDefaultText(text);
927
928 if (updateStatusBarMsg) {
929 m_statusBar->setMessage(text, DolphinStatusBar::Default);
930 }
931 }
932
933 void DolphinView::requestActivation()
934 {
935 m_mainWindow->setActiveView(this);
936 }
937
938 void DolphinView::changeSelection(const KFileItemList& selection)
939 {
940 clearSelection();
941 if (selection.isEmpty()) {
942 return;
943 }
944 KUrl baseUrl = url();
945 KUrl url;
946 QItemSelection new_selection;
947 foreach (KFileItem* item, selection) {
948 url = item->url().upUrl();
949 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
950 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(*item));
951 new_selection.select(index, index);
952 }
953 }
954 itemView()->selectionModel()->select(new_selection,
955 QItemSelectionModel::ClearAndSelect
956 | QItemSelectionModel::Current);
957 }
958
959 void DolphinView::changeNameFilter(const QString& nameFilter)
960 {
961 // The name filter of KDirLister does a 'hard' filtering, which
962 // means that only the items are shown where the names match
963 // exactly the filter. This is non-transparent for the user, which
964 // just wants to have a 'soft' filtering: does the name contain
965 // the filter string?
966 QString adjustedFilter(nameFilter);
967 adjustedFilter.insert(0, '*');
968 adjustedFilter.append('*');
969
970 // Use the ProxyModel to filter:
971 // This code is #ifdefed as setNameFilter behaves
972 // slightly different than the QSortFilterProxyModel
973 // as it will not remove directories. I will ask
974 // our beloved usability experts for input
975 // -- z.
976 #if 0
977 m_dirLister->setNameFilter(adjustedFilter);
978 m_dirLister->emitChanges();
979 #else
980 m_proxyModel->setFilterRegExp( nameFilter );
981 #endif
982 }
983
984 void DolphinView::openContextMenu(const QPoint& pos)
985 {
986 KFileItem* item = 0;
987
988 const QModelIndex index = itemView()->indexAt(pos);
989 if (isValidNameIndex(index)) {
990 item = fileItem(index);
991 }
992
993 DolphinContextMenu contextMenu(m_mainWindow, item, url());
994 contextMenu.open();
995 }
996
997 void DolphinView::dropUrls(const KUrl::List& urls,
998 const QModelIndex& index,
999 QWidget* source)
1000 {
1001 KFileItem* directory = 0;
1002 if (isValidNameIndex(index)) {
1003 KFileItem* item = fileItem(index);
1004 Q_ASSERT(item != 0);
1005 if (item->isDir()) {
1006 // the URLs are dropped above a directory
1007 directory = item;
1008 }
1009 }
1010
1011 if ((directory == 0) && (source == itemView())) {
1012 // The dropping is done into the same viewport where
1013 // the dragging has been started. Just ignore this...
1014 return;
1015 }
1016
1017 const KUrl& destination = (directory == 0) ? url() :
1018 directory->url();
1019 dropUrls(urls, destination);
1020 }
1021
1022 void DolphinView::dropUrls(const KUrl::List& urls,
1023 const KUrl& destination)
1024 {
1025 m_mainWindow->dropUrls(urls, destination);
1026 }
1027
1028 void DolphinView::updateSorting(DolphinView::Sorting sorting)
1029 {
1030 ViewProperties props(url());
1031 props.setSorting(sorting);
1032
1033 m_proxyModel->setSorting(sorting);
1034
1035 emit sortingChanged(sorting);
1036 }
1037
1038 void DolphinView::updateSortOrder(Qt::SortOrder order)
1039 {
1040 ViewProperties props(url());
1041 props.setSortOrder(order);
1042
1043 m_proxyModel->setSortOrder(order);
1044
1045 emit sortOrderChanged(order);
1046 }
1047
1048 void DolphinView::emitContentsMoved()
1049 {
1050 if (!m_blockContentsMovedSignal) {
1051 emit contentsMoved(contentsX(), contentsY());
1052 }
1053 }
1054
1055 void DolphinView::updateActivationState()
1056 {
1057 m_urlNavigator->setActive(isActive());
1058 if(isActive()) {
1059 emit urlChanged(url());
1060 emit selectionChanged(selectedItems());
1061 }
1062 }
1063
1064 void DolphinView::updateCutItems()
1065 {
1066 // restore the icons of all previously selected items to the
1067 // original state...
1068 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
1069 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
1070 while (it != end) {
1071 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
1072 if (index.isValid()) {
1073 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
1074 }
1075 ++it;
1076 }
1077 m_cutItemsCache.clear();
1078
1079 // ... and apply an item effect to all currently cut items
1080 applyCutItemEffect();
1081 }
1082
1083 void DolphinView::createView()
1084 {
1085 // delete current view
1086 QAbstractItemView* view = itemView();
1087 if (view != 0) {
1088 m_topLayout->removeWidget(view);
1089 view->close();
1090 view->deleteLater();
1091 view = 0;
1092 m_iconsView = 0;
1093 m_detailsView = 0;
1094 m_columnView = 0;
1095 m_fileItemDelegate = 0;
1096 }
1097
1098 Q_ASSERT(m_iconsView == 0);
1099 Q_ASSERT(m_detailsView == 0);
1100 Q_ASSERT(m_columnView == 0);
1101
1102 // ... and recreate it representing the current mode
1103 switch (m_mode) {
1104 case IconsView:
1105 m_iconsView = new DolphinIconsView(this, m_controller);
1106 view = m_iconsView;
1107 break;
1108
1109 case DetailsView:
1110 m_detailsView = new DolphinDetailsView(this, m_controller);
1111 view = m_detailsView;
1112 break;
1113
1114 case ColumnView:
1115 m_columnView = new DolphinColumnView(this, m_controller);
1116 view = m_columnView;
1117 break;
1118 }
1119
1120 Q_ASSERT(view != 0);
1121
1122 m_fileItemDelegate = new KFileItemDelegate(view);
1123 view->setItemDelegate(m_fileItemDelegate);
1124
1125 view->setModel(m_proxyModel);
1126 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1127
1128 new KMimeTypeResolver(view, m_dirModel);
1129 m_topLayout->insertWidget(1, view);
1130
1131 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1132 m_controller, SLOT(indicateSelectionChange()));
1133 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1134 this, SLOT(emitContentsMoved()));
1135 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1136 this, SLOT(emitContentsMoved()));
1137 }
1138
1139 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
1140 {
1141 QItemSelectionModel* selectionModel = itemView()->selectionModel();
1142 const QAbstractItemModel* itemModel = selectionModel->model();
1143
1144 const QModelIndex topLeft = itemModel->index(0, 0);
1145 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
1146 itemModel->columnCount() - 1);
1147
1148 QItemSelection selection(topLeft, bottomRight);
1149 selectionModel->select(selection, flags);
1150 }
1151
1152 QAbstractItemView* DolphinView::itemView() const
1153 {
1154 if (m_detailsView != 0) {
1155 return m_detailsView;
1156 }
1157 else if (m_columnView != 0) {
1158 return m_columnView;
1159 }
1160
1161 return m_iconsView;
1162 }
1163
1164 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
1165 {
1166 return index.isValid() && (index.column() == KDirModel::Name);
1167 }
1168
1169 bool DolphinView::isCutItem(const KFileItem& item) const
1170 {
1171 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1172 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1173
1174 const KUrl& itemUrl = item.url();
1175 KUrl::List::const_iterator it = cutUrls.begin();
1176 const KUrl::List::const_iterator end = cutUrls.end();
1177 while (it != end){
1178 if (*it == itemUrl) {
1179 return true;
1180 }
1181 ++it;
1182 }
1183
1184 return false;
1185 }
1186
1187 void DolphinView::applyCutItemEffect()
1188 {
1189 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1190 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
1191 return;
1192 }
1193
1194 KFileItemList items(m_dirLister->items());
1195 KFileItemList::const_iterator it = items.begin();
1196 const KFileItemList::const_iterator end = items.end();
1197 while (it != end) {
1198 KFileItem* item = *it;
1199 if (isCutItem(*item)) {
1200 const QModelIndex index = m_dirModel->indexForItem(*item);
1201 const KFileItem* item = m_dirModel->itemForIndex(index);
1202 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
1203 if ((value.type() == QVariant::Icon) && (item != 0)) {
1204 const QIcon icon(qvariant_cast<QIcon>(value));
1205 QPixmap pixmap = icon.pixmap(128, 128);
1206
1207 // remember current pixmap for the item to be able
1208 // to restore it when other items get cut
1209 CutItem cutItem;
1210 cutItem.url = item->url();
1211 cutItem.pixmap = pixmap;
1212 m_cutItemsCache.append(cutItem);
1213
1214 // apply icon effect to the cut item
1215 KIconEffect iconEffect;
1216 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
1217 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
1218 }
1219 }
1220 ++it;
1221 }
1222 }
1223
1224 #include "dolphinview.moc"