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