]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Use hover effect from KFileItemDelegate also for the details view and assure that...
[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 if (!isValidNameIndex(index)) {
626 return;
627 }
628
629 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
630 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
631 // items are selected by the user, hence don't trigger the
632 // item specified by 'index'
633 return;
634 }
635
636 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
637 if (item == 0) {
638 return;
639 }
640
641 if (item->isDir()) {
642 // Prefer the local path over the URL. This assures that the
643 // volume space information is correct. Assuming that the URL is media:/sda1,
644 // and the local path is /windows/C: For the URL the space info is related
645 // to the root partition (and hence wrong) and for the local path the space
646 // info is related to the windows partition (-> correct).
647 const QString localPath(item->localPath());
648 if (localPath.isEmpty()) {
649 setUrl(item->url());
650 }
651 else {
652 setUrl(KUrl(localPath));
653 }
654 }
655 else {
656 item->run();
657 }
658 }
659
660 void DolphinView::updateProgress(int percent)
661 {
662 if (m_showProgress) {
663 m_statusBar->setProgress(percent);
664 }
665 }
666
667 void DolphinView::updateItemCount()
668 {
669 if (m_showProgress) {
670 m_statusBar->setProgressText(QString());
671 m_statusBar->setProgress(100);
672 m_showProgress = false;
673 }
674
675 KFileItemList items(m_dirLister->items());
676 KFileItemList::const_iterator it = items.begin();
677 const KFileItemList::const_iterator end = items.end();
678
679 m_fileCount = 0;
680 m_folderCount = 0;
681
682 while (it != end) {
683 KFileItem* item = *it;
684 if (item->isDir()) {
685 ++m_folderCount;
686 }
687 else {
688 ++m_fileCount;
689 }
690 ++it;
691 }
692
693 updateStatusBar();
694
695 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
696 }
697
698 void DolphinView::generatePreviews(const KFileItemList& items)
699 {
700 if (m_controller->showPreview()) {
701 KIO::PreviewJob* job = KIO::filePreview(items, 128);
702 connect(job, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)),
703 this, SLOT(showPreview(const KFileItem*, const QPixmap&)));
704 }
705 }
706
707 void DolphinView::showPreview(const KFileItem* item, const QPixmap& pixmap)
708 {
709 Q_ASSERT(item != 0);
710 const QModelIndex idx = m_dirModel->indexForItem(*item);
711 if (idx.isValid() && (idx.column() == 0)) {
712 m_dirModel->setData(idx, pixmap, Qt::DecorationRole);
713 }
714 }
715
716 void DolphinView::restoreContentsPos()
717 {
718 int index = 0;
719 const QLinkedList<UrlNavigator::HistoryElem> history = urlHistory(index);
720 if (!history.isEmpty()) {
721 QAbstractItemView* view = itemView();
722 // TODO: view->setCurrentItem(history[index].currentFileName());
723
724 QLinkedList<UrlNavigator::HistoryElem>::const_iterator it = history.begin();
725 it += index;
726 view->horizontalScrollBar()->setValue((*it).contentsX());
727 view->verticalScrollBar()->setValue((*it).contentsY());
728 }
729 }
730
731 void DolphinView::showInfoMessage(const QString& msg)
732 {
733 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
734 }
735
736 void DolphinView::showErrorMessage(const QString& msg)
737 {
738 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
739 }
740
741 void DolphinView::emitSelectionChangedSignal()
742 {
743 emit selectionChanged();
744 }
745
746 void DolphinView::closeFilterBar()
747 {
748 m_filterBar->hide();
749 emit showFilterBarChanged(false);
750 }
751
752 void DolphinView::startDirLister(const KUrl& url, bool reload)
753 {
754 if (!url.isValid()) {
755 const QString location(url.pathOrUrl());
756 if (location.isEmpty()) {
757 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
758 }
759 else {
760 m_statusBar->setMessage(i18n("The location '%1' is invalid.",location),
761 DolphinStatusBar::Error);
762 }
763 return;
764 }
765
766 // Only show the directory loading progress if the status bar does
767 // not contain another progress information. This means that
768 // the directory loading progress information has the lowest priority.
769 const QString progressText(m_statusBar->progressText());
770 m_showProgress = progressText.isEmpty() ||
771 (progressText == i18n("Loading directory..."));
772 if (m_showProgress) {
773 m_statusBar->setProgressText(i18n("Loading directory..."));
774 m_statusBar->setProgress(0);
775 }
776
777 m_dirLister->stop();
778 m_dirLister->openUrl(url, false, reload);
779 }
780
781 QString DolphinView::defaultStatusBarText() const
782 {
783 return KIO::itemsSummaryString(m_fileCount + m_folderCount,
784 m_fileCount,
785 m_folderCount,
786 0, false);
787 }
788
789 QString DolphinView::selectionStatusBarText() const
790 {
791 QString text;
792 const KFileItemList list = selectedItems();
793 if (list.isEmpty()) {
794 // when an item is triggered, it is temporary selected but selectedItems()
795 // will return an empty list
796 return QString();
797 }
798
799 int fileCount = 0;
800 int folderCount = 0;
801 KIO::filesize_t byteSize = 0;
802 KFileItemList::const_iterator it = list.begin();
803 const KFileItemList::const_iterator end = list.end();
804 while (it != end){
805 KFileItem* item = *it;
806 if (item->isDir()) {
807 ++folderCount;
808 }
809 else {
810 ++fileCount;
811 byteSize += item->size();
812 }
813 ++it;
814 }
815
816 if (folderCount > 0) {
817 text = i18np("1 Folder selected", "%1 Folders selected", folderCount);
818 if (fileCount > 0) {
819 text += ", ";
820 }
821 }
822
823 if (fileCount > 0) {
824 const QString sizeText(KIO::convertSize(byteSize));
825 text += i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount, sizeText);
826 }
827
828 return text;
829 }
830
831 void DolphinView::showFilterBar(bool show)
832 {
833 assert(m_filterBar != 0);
834 if (show) {
835 m_filterBar->show();
836 }
837 else {
838 m_filterBar->hide();
839 }
840 }
841
842 void DolphinView::updateStatusBar()
843 {
844 // As the item count information is less important
845 // in comparison with other messages, it should only
846 // be shown if:
847 // - the status bar is empty or
848 // - shows already the item count information or
849 // - shows only a not very important information
850 // - if any progress is given don't show the item count info at all
851 const QString msg(m_statusBar->message());
852 const bool updateStatusBarMsg = (msg.isEmpty() ||
853 (msg == m_statusBar->defaultText()) ||
854 (m_statusBar->type() == DolphinStatusBar::Information)) &&
855 (m_statusBar->progress() == 100);
856
857 const QString text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
858 m_statusBar->setDefaultText(text);
859
860 if (updateStatusBarMsg) {
861 m_statusBar->setMessage(text, DolphinStatusBar::Default);
862 }
863 }
864
865 void DolphinView::requestActivation()
866 {
867 m_mainWindow->setActiveView(this);
868 }
869
870 void DolphinView::changeNameFilter(const QString& nameFilter)
871 {
872 // The name filter of KDirLister does a 'hard' filtering, which
873 // means that only the items are shown where the names match
874 // exactly the filter. This is non-transparent for the user, which
875 // just wants to have a 'soft' filtering: does the name contain
876 // the filter string?
877 QString adjustedFilter(nameFilter);
878 adjustedFilter.insert(0, '*');
879 adjustedFilter.append('*');
880
881 // Use the ProxyModel to filter:
882 // This code is #ifdefed as setNameFilter behaves
883 // slightly different than the QSortFilterProxyModel
884 // as it will not remove directories. I will ask
885 // our beloved usability experts for input
886 // -- z.
887 #if 0
888 m_dirLister->setNameFilter(adjustedFilter);
889 m_dirLister->emitChanges();
890 #else
891 m_proxyModel->setFilterRegExp( nameFilter );
892 #endif
893 }
894
895 void DolphinView::openContextMenu(const QPoint& pos)
896 {
897 KFileItem* item = 0;
898
899 const QModelIndex index = itemView()->indexAt(pos);
900 if (isValidNameIndex(index)) {
901 item = fileItem(index);
902 }
903
904 DolphinContextMenu contextMenu(this, item);
905 contextMenu.open();
906 }
907
908 void DolphinView::dropUrls(const KUrl::List& urls,
909 const QPoint& pos)
910 {
911 KFileItem* directory = 0;
912 const QModelIndex index = itemView()->indexAt(pos);
913 if (isValidNameIndex(index)) {
914 KFileItem* item = fileItem(index);
915 assert(item != 0);
916 if (item->isDir()) {
917 // the URLs are dropped above a directory
918 directory = item;
919 }
920 }
921
922 const KUrl& destination = (directory == 0) ? url() :
923 directory->url();
924 dropUrls(urls, destination);
925 }
926
927 void DolphinView::dropUrls(const KUrl::List& urls,
928 const KUrl& destination)
929 {
930 m_mainWindow->dropUrls(urls, destination);
931 }
932
933
934 void DolphinView::updateSorting(DolphinView::Sorting sorting)
935 {
936 ViewProperties props(url());
937 props.setSorting(sorting);
938
939 m_proxyModel->setSorting(sorting);
940
941 emit sortingChanged(sorting);
942 }
943
944 void DolphinView::updateSortOrder(Qt::SortOrder order)
945 {
946 ViewProperties props(url());
947 props.setSortOrder(order);
948
949 m_proxyModel->setSortOrder(order);
950
951 emit sortOrderChanged(order);
952 }
953
954 void DolphinView::emitContentsMoved()
955 {
956 emit contentsMoved(contentsX(), contentsY());
957 }
958
959 void DolphinView::updateActivationState()
960 {
961 m_urlNavigator->setActive(isActive());
962 }
963
964 void DolphinView::createView()
965 {
966 // delete current view
967 QAbstractItemView* view = itemView();
968 if (view != 0) {
969 m_topLayout->removeWidget(view);
970 view->close();
971 view->deleteLater();
972 m_iconsView = 0;
973 m_detailsView = 0;
974 }
975
976 assert(m_iconsView == 0);
977 assert(m_detailsView == 0);
978
979 // ... and recreate it representing the current mode
980 switch (m_mode) {
981 case IconsView:
982 m_iconsView = new DolphinIconsView(this, m_controller);
983 view = m_iconsView;
984 break;
985
986 case DetailsView:
987 m_detailsView = new DolphinDetailsView(this, m_controller);
988 view = m_detailsView;
989 break;
990 }
991
992 view->setModel(m_proxyModel);
993 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
994
995 new KMimeTypeResolver(view, m_dirModel);
996 m_topLayout->insertWidget(1, view);
997
998 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
999 m_controller, SLOT(indicateSelectionChange()));
1000 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1001 this, SLOT(emitContentsMoved()));
1002 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1003 this, SLOT(emitContentsMoved()));
1004 }
1005
1006 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
1007 {
1008 QItemSelectionModel* selectionModel = itemView()->selectionModel();
1009 const QAbstractItemModel* itemModel = selectionModel->model();
1010
1011 const QModelIndex topLeft = itemModel->index(0, 0);
1012 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
1013 itemModel->columnCount() - 1);
1014
1015 QItemSelection selection(topLeft, bottomRight);
1016 selectionModel->select(selection, flags);
1017 }
1018
1019 QAbstractItemView* DolphinView::itemView() const
1020 {
1021 Q_ASSERT((m_iconsView == 0) || (m_detailsView == 0));
1022 if (m_detailsView != 0) {
1023 return m_detailsView;
1024 }
1025 return m_iconsView;
1026 }
1027
1028 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
1029 {
1030 return index.isValid() && (index.column() == KDirModel::Name);
1031 }
1032
1033 #include "dolphinview.moc"