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