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