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