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