]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.h
Implement restoring expanded folders in Details View
[dolphin.git] / src / views / dolphinview.h
1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
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 #ifndef DOLPHINVIEW_H
22 #define DOLPHINVIEW_H
23
24 #include <config-nepomuk.h>
25
26 #include "libdolphin_export.h"
27
28 #include <kparts/part.h>
29 #include <KFileItem>
30 #include <KFileItemDelegate>
31 #include <kio/fileundomanager.h>
32 #include <KIO/Job>
33
34 #include <QBoxLayout>
35 #include <QKeyEvent>
36 #include <QLinkedList>
37 #include <QSet>
38 #include <QWidget>
39
40 typedef KIO::FileUndoManager::CommandType CommandType;
41
42 class DolphinDirLister;
43 class DolphinItemListContainer;
44 class KAction;
45 class KActionCollection;
46 class KFileItemModel;
47 class KItemModelBase;
48 class KUrl;
49 class ToolTipManager;
50 class VersionControlObserver;
51 class ViewProperties;
52 class QGraphicsSceneDragDropEvent;
53 class QRegExp;
54
55 /**
56 * @short Represents a view for the directory content.
57 *
58 * View modes for icons, compact and details are supported. It's
59 * possible to adjust:
60 * - sort order
61 * - sort type
62 * - show hidden files
63 * - show previews
64 * - enable grouping
65 */
66 class LIBDOLPHINPRIVATE_EXPORT DolphinView : public QWidget
67 {
68 Q_OBJECT
69
70 public:
71 /**
72 * Defines the view mode for a directory. The
73 * view mode is automatically updated if the directory itself
74 * defines a view mode (see class ViewProperties for details).
75 */
76 enum Mode
77 {
78 /**
79 * The items are shown as icons with a name-label below.
80 */
81 IconsView = 0,
82
83 /**
84 * The icon, the name and the size of the items are
85 * shown per default as a table.
86 */
87 DetailsView,
88
89 /**
90 * The items are shown as icons with the name-label aligned
91 * to the right side.
92 */
93 CompactView
94 };
95
96 /** Defines the sort order for the items of a directory. */
97 enum Sorting
98 {
99 SortByName = 0,
100 SortBySize,
101 SortByDate,
102 SortByPermissions,
103 SortByOwner,
104 SortByGroup,
105 SortByType,
106 SortByDestination,
107 SortByPath
108 };
109
110 /** Defines the additional information shown for the items of a directory. */
111 enum AdditionalInfo
112 {
113 NoInfo = 0,
114 NameInfo,
115 SizeInfo,
116 DateInfo,
117 PermissionsInfo,
118 OwnerInfo,
119 GroupInfo,
120 TypeInfo,
121 DestinationInfo,
122 PathInfo
123 };
124
125 /**
126 * @param url Specifies the content which should be shown.
127 * @param parent Parent widget of the view.
128 */
129 DolphinView(const KUrl& url, QWidget* parent);
130
131 virtual ~DolphinView();
132
133 /**
134 * Returns the current active URL, where all actions are applied.
135 * The URL navigator is synchronized with this URL.
136 */
137 KUrl url() const;
138
139 /**
140 * If \a active is true, the view will marked as active. The active
141 * view is defined as view where all actions are applied to.
142 */
143 void setActive(bool active);
144 bool isActive() const;
145
146 /**
147 * Changes the view mode for the current directory to \a mode.
148 * If the view properties should be remembered for each directory
149 * (GeneralSettings::globalViewProps() returns false), then the
150 * changed view mode will be stored automatically.
151 */
152 void setMode(Mode mode);
153 Mode mode() const;
154
155 /** See setPreviewsShown */
156 bool previewsShown() const;
157
158 /** See setShowHiddenFiles */
159 bool hiddenFilesShown() const;
160
161 /** See setCategorizedSorting */
162 bool categorizedSorting() const;
163
164 /**
165 * Returns the items of the view.
166 */
167 KFileItemList items() const;
168
169 /**
170 * Returns the selected items. The list is empty if no item has been
171 * selected.
172 */
173 KFileItemList selectedItems() const;
174
175 /**
176 * Returns the number of selected items (this is faster than
177 * invoking selectedItems().count()).
178 */
179 int selectedItemsCount() const;
180
181 /**
182 * Marks the items indicated by \p urls to get selected after the
183 * directory DolphinView::url() has been loaded. Note that nothing
184 * gets selected if no loading of a directory has been triggered
185 * by DolphinView::setUrl() or DolphinView::reload().
186 */
187 void markUrlsAsSelected(const QList<KUrl>& urls);
188
189 /**
190 * All items that match to the pattern \a pattern will get selected
191 * if \a enabled is true and deselected if \a enabled is false.
192 */
193 void setItemSelectionEnabled(const QRegExp& pattern, bool enabled);
194
195 /**
196 * Sets the zoom level to \a level. It is assured that the used
197 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
198 * ZoomLevelInfo::maximumLevel().
199 */
200 void setZoomLevel(int level);
201 int zoomLevel() const;
202
203 /**
204 * Returns true, if zooming in is possible. If false is returned,
205 * the maximum zooming level has been reached.
206 */
207 bool isZoomInPossible() const;
208
209 /**
210 * Returns true, if zooming out is possible. If false is returned,
211 * the minimum zooming level has been reached.
212 */
213 bool isZoomOutPossible() const;
214
215 /** Sets the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
216 void setSorting(Sorting sorting);
217
218 /** Returns the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
219 Sorting sorting() const;
220
221 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
222 void setSortOrder(Qt::SortOrder order);
223
224 /** Returns the currently used sort order (Qt::Ascending or Qt::Descending). */
225 Qt::SortOrder sortOrder() const;
226
227 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
228 void setSortFoldersFirst(bool foldersFirst);
229
230 /** Returns if files and folders are sorted separately or not. */
231 bool sortFoldersFirst() const;
232
233 /** Sets the additional information which should be shown for the items. */
234 void setAdditionalInfoList(const QList<AdditionalInfo>& info);
235
236 /** Returns the additional information which should be shown for the items. */
237 QList<AdditionalInfo> additionalInfoList() const;
238
239 /** Reloads the current directory. */
240 void reload();
241
242 void stopLoading();
243
244 /**
245 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
246 * This method only needs to get invoked if the view settings for the Icons View,
247 * Details View or Columns View have been changed.
248 */
249 void refresh();
250
251 /**
252 * Filters the currently shown items by \a nameFilter. All items
253 * which contain the given filter string will be shown.
254 */
255 void setNameFilter(const QString& nameFilter);
256 QString nameFilter() const;
257
258 /**
259 * Calculates the number of currently shown files into
260 * \a fileCount and the number of folders into \a folderCount.
261 * The size of all files is written into \a totalFileSize.
262 * It is recommend using this method instead of asking the
263 * directory lister or the model directly, as it takes
264 * filtering and hierarchical previews into account.
265 */
266 void calculateItemCount(int& fileCount, int& folderCount, KIO::filesize_t& totalFileSize) const;
267
268 /**
269 * Returns a textual representation of the state of the current
270 * folder or selected items, suitable for use in the status bar.
271 */
272 QString statusBarText() const;
273
274 /**
275 * Returns the version control actions that are provided for the items \p items.
276 * Usually the actions are presented in the context menu.
277 */
278 QList<QAction*> versionControlActions(const KFileItemList& items) const;
279
280 /**
281 * Returns the state of the paste action:
282 * first is whether the action should be enabled
283 * second is the text for the action
284 */
285 QPair<bool, QString> pasteInfo() const;
286
287 /**
288 * If \a tabsForFiles is true, the signal tabRequested() will also
289 * emitted also for files. Per default tabs for files is disabled
290 * and hence the signal tabRequested() will only be emitted for
291 * directories.
292 */
293 void setTabsForFilesEnabled(bool tabsForFiles);
294 bool isTabsForFilesEnabled() const;
295
296 /**
297 * Returns true if the current view allows folders to be expanded,
298 * i.e. presents a hierarchical view to the user.
299 */
300 bool itemsExpandable() const;
301
302 /**
303 * Restores the view state (current item, contents position, details view expansion state)
304 */
305 void restoreState(QDataStream& stream);
306
307 /**
308 * Saves the view state (current item, contents position, details view expansion state)
309 */
310 void saveState(QDataStream& stream);
311
312 /** Returns true, if at least one item is selected. */
313 bool hasSelection() const;
314
315 /**
316 * Returns the root item which represents the current URL. Note that the returned
317 * item can be null (KFileItem::isNull() will return true) in case that the directory
318 * has not been loaded.
319 */
320 KFileItem rootItem() const;
321
322 public slots:
323 /**
324 * Changes the directory to \a url. If the current directory is equal to
325 * \a url, nothing will be done (use DolphinView::reload() instead).
326 */
327 void setUrl(const KUrl& url);
328
329 /**
330 * Selects all items.
331 * @see DolphinView::selectedItems()
332 */
333 void selectAll();
334
335 /**
336 * Inverts the current selection: selected items get unselected,
337 * unselected items get selected.
338 * @see DolphinView::selectedItems()
339 */
340 void invertSelection();
341
342 void clearSelection();
343
344 /**
345 * Triggers the renaming of the currently selected items, where
346 * the user must input a new name for the items.
347 */
348 void renameSelectedItems();
349
350 /**
351 * Moves all selected items to the trash.
352 */
353 void trashSelectedItems();
354
355 /**
356 * Deletes all selected items.
357 */
358 void deleteSelectedItems();
359
360 /**
361 * Copies all selected items to the clipboard and marks
362 * the items as cut.
363 */
364 void cutSelectedItems();
365
366 /** Copies all selected items to the clipboard. */
367 void copySelectedItems();
368
369 /** Pastes the clipboard data to this view. */
370 void paste();
371
372 /**
373 * Pastes the clipboard data into the currently selected
374 * folder. If the current selection is not exactly one folder, no
375 * paste operation is done.
376 */
377 void pasteIntoFolder();
378
379 /**
380 * Turns on the file preview for the all files of the current directory,
381 * if \a show is true.
382 * If the view properties should be remembered for each directory
383 * (GeneralSettings::globalViewProps() returns false), then the
384 * preview setting will be stored automatically.
385 */
386 void setPreviewsShown(bool show);
387
388 /**
389 * Shows all hidden files of the current directory,
390 * if \a show is true.
391 * If the view properties should be remembered for each directory
392 * (GeneralSettings::globalViewProps() returns false), then the
393 * show hidden file setting will be stored automatically.
394 */
395 void setHiddenFilesShown(bool show);
396
397 /**
398 * Summarizes all sorted items by their category \a categorized
399 * is true.
400 * If the view properties should be remembered for each directory
401 * (GeneralSettings::globalViewProps() returns false), then the
402 * categorized sorting setting will be stored automatically.
403 */
404 void setCategorizedSorting(bool categorized);
405
406 signals:
407 /**
408 * Is emitted if the view has been activated by e. g. a mouse click.
409 */
410 void activated();
411
412 /**
413 * Is emitted if the URL of the view will be changed to \a url.
414 * After the URL has been changed the signal urlChanged() will
415 * be emitted.
416 */
417 void urlAboutToBeChanged(const KUrl& url);
418
419 /** Is emitted if the URL of the view has been changed to \a url. */
420 void urlChanged(const KUrl& url);
421
422 /**
423 * Is emitted when clicking on an item with the left mouse button.
424 */
425 void itemActivated(const KFileItem& item);
426
427 /**
428 * Is emitted if items have been added or deleted.
429 */
430 void itemCountChanged();
431
432 /**
433 * Is emitted if a new tab should be opened for the URL \a url.
434 */
435 void tabRequested(const KUrl& url);
436
437 /**
438 * Is emitted if the view mode (IconsView, DetailsView,
439 * PreviewsView) has been changed.
440 */
441 void modeChanged(DolphinView::Mode current, DolphinView::Mode previous);
442
443 /** Is emitted if the 'show preview' property has been changed. */
444 void previewsShownChanged(bool shown);
445
446 /** Is emitted if the 'show hidden files' property has been changed. */
447 void hiddenFilesShownChanged(bool shown);
448
449 /** Is emitted if the 'categorized sorting' property has been changed. */
450 void categorizedSortingChanged(bool sortCategorized);
451
452 /** Is emitted if the sorting by name, size or date has been changed. */
453 void sortingChanged(DolphinView::Sorting sorting);
454
455 /** Is emitted if the sort order (ascending or descending) has been changed. */
456 void sortOrderChanged(Qt::SortOrder order);
457
458 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
459 void sortFoldersFirstChanged(bool foldersFirst);
460
461 /** Is emitted if the additional information shown for this view has been changed. */
462 void additionalInfoListChanged(const QList<DolphinView::AdditionalInfo>& current,
463 const QList<DolphinView::AdditionalInfo>& previous);
464
465 /** Is emitted if the zoom level has been changed by zooming in or out. */
466 void zoomLevelChanged(int current, int previous);
467
468 /**
469 * Is emitted if information of an item is requested to be shown e. g. in the panel.
470 * If item is null, no item information request is pending.
471 */
472 void requestItemInfo(const KFileItem& item);
473
474 /**
475 * Is emitted whenever the selection has been changed.
476 */
477 void selectionChanged(const KFileItemList& selection);
478
479 /**
480 * Is emitted if a context menu is requested for the item \a item,
481 * which is part of \a url. If the item is null, the context menu
482 * for the URL should be shown and the custom actions \a customActions
483 * will be added.
484 */
485 void requestContextMenu(const KFileItem& item,
486 const KUrl& url,
487 const QList<QAction*>& customActions);
488
489 /**
490 * Is emitted if an information message with the content \a msg
491 * should be shown.
492 */
493 void infoMessage(const QString& msg);
494
495 /**
496 * Is emitted if an error message with the content \a msg
497 * should be shown.
498 */
499 void errorMessage(const QString& msg);
500
501 /**
502 * Is emitted if an "operation completed" message with the content \a msg
503 * should be shown.
504 */
505 void operationCompletedMessage(const QString& msg);
506
507 /**
508 * Is emitted after DolphinView::setUrl() has been invoked and
509 * the path \a url is currently loaded. If this signal is emitted,
510 * it is assured that the view contains already the correct root
511 * URL and property settings.
512 */
513 void startedPathLoading(const KUrl& url);
514
515 /**
516 * Is emitted after the path triggered by DolphinView::setUrl()
517 * has been loaded.
518 */
519 void finishedPathLoading(const KUrl& url);
520
521 /**
522 * Is emitted after DolphinView::setUrl() has been invoked and provides
523 * the information how much percent of the current path have been loaded.
524 */
525 void pathLoadingProgress(int percent);
526
527 /**
528 * Is emitted if the DolphinView::setUrl() is invoked but the URL is not
529 * a directory.
530 */
531 void urlIsFileError(const KUrl& file);
532
533 /**
534 * Emitted when KDirLister emits redirection.
535 * Testcase: fish://localhost
536 */
537 void redirection(const KUrl& oldUrl, const KUrl& newUrl);
538
539 /**
540 * Is emitted when the write state of the folder has been changed. The application
541 * should disable all actions like "Create New..." that depend on the write
542 * state.
543 */
544 void writeStateChanged(bool isFolderWritable);
545
546 protected:
547 virtual void mouseReleaseEvent(QMouseEvent* event);
548 virtual void contextMenuEvent(QContextMenuEvent* event);
549 virtual void wheelEvent(QWheelEvent* event);
550
551 private slots:
552 /**
553 * Marks the view as active (DolphinView:isActive() will return true)
554 * and emits the 'activated' signal if it is not already active.
555 */
556 void activate();
557
558 void slotItemActivated(int index);
559 void slotItemMiddleClicked(int index);
560 void slotContextMenuRequested(int index, const QPointF& pos);
561 void slotItemExpansionToggleClicked(int index);
562 void slotItemHovered(int index);
563 void slotItemUnhovered(int index);
564 void slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
565 void slotModelChanged(KItemModelBase* current, KItemModelBase* previous);
566
567 /**
568 * Emits the signal \a selectionChanged() with a small delay. This is
569 * because getting all file items for the selection can be an expensive
570 * operation. Fast selection changes are collected in this case and
571 * the signal is emitted only after no selection change has been done
572 * within a small delay.
573 */
574 void slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous);
575
576 /**
577 * Is called by emitDelayedSelectionChangedSignal() and emits the
578 * signal \a selectionChanged() with all selected file items as parameter.
579 */
580 void emitSelectionChangedSignal();
581
582 /**
583 * Opens the context menu on position \a pos. The position
584 * is used to check whether the context menu is related to an
585 * item or to the viewport.
586 */
587 void openContextMenu(const QPoint& pos, const QList<QAction*>& customActions);
588
589 /**
590 * Drops dragged URLs to the destination path \a destPath. If
591 * the URLs are dropped above an item inside the destination path,
592 * the item is indicated by \a destItem.
593 */
594 void dropUrls(const KFileItem& destItem,
595 const KUrl& destPath,
596 QDropEvent* event);
597
598 /**
599 * Updates the view properties of the current URL to the
600 * sorting given by \a sorting.
601 */
602 void updateSorting(DolphinView::Sorting sorting);
603
604 /**
605 * Updates the view properties of the current URL to the
606 * sort order given by \a order.
607 */
608 void updateSortOrder(Qt::SortOrder order);
609
610 /**
611 * Updates the view properties of the current URL to the
612 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
613 */
614 void updateSortFoldersFirst(bool foldersFirst);
615
616 /**
617 * Updates the status bar to show hover information for the
618 * item \a item. If currently other items are selected,
619 * no hover information is shown.
620 * @see DolphinView::clearHoverInformation()
621 */
622 void showHoverInformation(const KFileItem& item);
623
624 /**
625 * Clears the hover information shown in the status bar.
626 * @see DolphinView::showHoverInformation().
627 */
628 void clearHoverInformation();
629
630 /**
631 * Indicates in the status bar that the delete operation
632 * of the job \a job has been finished.
633 */
634 void slotDeleteFileFinished(KJob* job);
635
636 /**
637 * Invoked when the directory lister has been started the
638 * loading of \a url.
639 */
640 void slotDirListerStarted(const KUrl& url);
641
642 /**
643 * Invoked when the file item model indicates that the directory lister has completed the loading
644 * of items, and that expanded folders have been restored (if the view mode is 'Details', and the
645 * view state is restored after navigating back or forward in history). Assures that pasted items
646 * and renamed items get seleced.
647 */
648 void slotLoadingCompleted();
649
650 /**
651 * Is invoked when the KDirLister indicates refreshed items.
652 */
653 void slotRefreshItems();
654
655 /**
656 * Observes the item with the URL \a url. As soon as the directory
657 * model indicates that the item is available, the item will
658 * get selected and it is assured that the item stays visible.
659 *
660 * @see selectAndScrollToCreatedItem()
661 */
662 void observeCreatedItem(const KUrl& url);
663
664 /**
665 * Selects and scrolls to the item that got observed
666 * by observeCreatedItem().
667 */
668 void selectAndScrollToCreatedItem();
669
670 /**
671 * Called when a redirection happens.
672 * Testcase: fish://localhost
673 */
674 void slotRedirection(const KUrl& oldUrl, const KUrl& newUrl);
675
676 /**
677 * Applies the state that has been restored by restoreViewState()
678 * to the view.
679 */
680 void updateViewState();
681
682 //void slotUrlChangeRequested(const KUrl& url);
683
684 private:
685 KFileItemModel* fileItemModel() const;
686
687 void loadDirectory(const KUrl& url, bool reload = false);
688
689 /**
690 * Applies the view properties which are defined by the current URL
691 * to the DolphinView properties.
692 */
693 void applyViewProperties();
694
695 void applyAdditionalInfoListToView();
696
697 /**
698 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
699 * Pastes the clipboard data into the URL \a url.
700 */
701 void pasteToUrl(const KUrl& url);
702
703 /**
704 * Checks whether the current item view has the same zoom level
705 * as \a oldZoomLevel. If this is not the case, the zoom level
706 * of the controller is updated and a zoomLevelChanged() signal
707 * is emitted.
708 */
709 void updateZoomLevel(int oldZoomLevel);
710
711 /**
712 * Returns a list of URLs for all selected items. The list is
713 * simplified, so that when the URLs are part of different tree
714 * levels, only the parent is returned.
715 */
716 KUrl::List simplifiedSelectedUrls() const;
717
718 /**
719 * Returns the MIME data for all selected items.
720 */
721 QMimeData* selectionMimeData() const;
722
723 /**
724 * Is invoked after a paste operation or a drag & drop
725 * operation and URLs from \a mimeData as selected.
726 * This allows to select all newly pasted
727 * items in restoreViewState().
728 */
729 void markPastedUrlsAsSelected(const QMimeData* mimeData);
730
731 /**
732 * Updates m_isFolderWritable dependent on whether the folder represented by
733 * the current URL is writable. If the state has changed, the signal
734 * writeableStateChanged() will be emitted.
735 */
736 void updateWritableState();
737
738 QByteArray sortRoleForSorting(Sorting sorting) const;
739
740 private:
741 bool m_active : 1;
742 bool m_tabsForFiles : 1;
743 bool m_assureVisibleCurrentIndex : 1;
744 bool m_isFolderWritable : 1;
745
746 KUrl m_url;
747 Mode m_mode;
748 QList<AdditionalInfo> m_additionalInfoList;
749
750 QVBoxLayout* m_topLayout;
751
752 DolphinDirLister* m_dirLister;
753 DolphinItemListContainer* m_container;
754
755 ToolTipManager* m_toolTipManager;
756
757 QTimer* m_selectionChangedTimer;
758
759 int m_currentItemIndex;
760 QPoint m_restoredContentsPosition;
761 KUrl m_createdItemUrl; // URL for a new item that got created by the "Create New..." menu
762 KFileItemList m_selectedItems; // this is used for making the View to remember selections after F5
763
764 VersionControlObserver* m_versionControlObserver;
765
766 // For unit tests
767 friend class TestBase;
768 friend class DolphinDetailsViewTest;
769 };
770
771 /// Allow using DolphinView::Mode in QVariant
772 Q_DECLARE_METATYPE(DolphinView::Mode)
773
774 #endif // DOLPHINVIEW_H