]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.h
Fix regression concerning Control+mouse wheel zooming
[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 * Marks the item indicated by \p url as the current item after the
191 * directory DolphinView::url() has been loaded.
192 */
193 void markUrlAsCurrent(const KUrl& url);
194
195 /**
196 * All items that match to the pattern \a pattern will get selected
197 * if \a enabled is true and deselected if \a enabled is false.
198 */
199 void setItemSelectionEnabled(const QRegExp& pattern, bool enabled);
200
201 /**
202 * Sets the zoom level to \a level. It is assured that the used
203 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
204 * ZoomLevelInfo::maximumLevel().
205 */
206 void setZoomLevel(int level);
207 int zoomLevel() const;
208
209 /**
210 * Returns true, if zooming in is possible. If false is returned,
211 * the maximum zooming level has been reached.
212 */
213 bool isZoomInPossible() const;
214
215 /**
216 * Returns true, if zooming out is possible. If false is returned,
217 * the minimum zooming level has been reached.
218 */
219 bool isZoomOutPossible() const;
220
221 /** Sets the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
222 void setSorting(Sorting sorting);
223
224 /** Returns the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
225 Sorting sorting() const;
226
227 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
228 void setSortOrder(Qt::SortOrder order);
229
230 /** Returns the currently used sort order (Qt::Ascending or Qt::Descending). */
231 Qt::SortOrder sortOrder() const;
232
233 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
234 void setSortFoldersFirst(bool foldersFirst);
235
236 /** Returns if files and folders are sorted separately or not. */
237 bool sortFoldersFirst() const;
238
239 /** Sets the additional information which should be shown for the items. */
240 void setAdditionalInfoList(const QList<AdditionalInfo>& info);
241
242 /** Returns the additional information which should be shown for the items. */
243 QList<AdditionalInfo> additionalInfoList() const;
244
245 /** Reloads the current directory. */
246 void reload();
247
248 void stopLoading();
249
250 /**
251 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
252 * This method only needs to get invoked if the view settings for the Icons View,
253 * Details View or Columns View have been changed.
254 */
255 void refresh();
256
257 /**
258 * Filters the currently shown items by \a nameFilter. All items
259 * which contain the given filter string will be shown.
260 */
261 void setNameFilter(const QString& nameFilter);
262 QString nameFilter() const;
263
264 /**
265 * Calculates the number of currently shown files into
266 * \a fileCount and the number of folders into \a folderCount.
267 * The size of all files is written into \a totalFileSize.
268 * It is recommend using this method instead of asking the
269 * directory lister or the model directly, as it takes
270 * filtering and hierarchical previews into account.
271 */
272 void calculateItemCount(int& fileCount, int& folderCount, KIO::filesize_t& totalFileSize) const;
273
274 /**
275 * Returns a textual representation of the state of the current
276 * folder or selected items, suitable for use in the status bar.
277 */
278 QString statusBarText() const;
279
280 /**
281 * Returns the version control actions that are provided for the items \p items.
282 * Usually the actions are presented in the context menu.
283 */
284 QList<QAction*> versionControlActions(const KFileItemList& items) const;
285
286 /**
287 * Returns the state of the paste action:
288 * first is whether the action should be enabled
289 * second is the text for the action
290 */
291 QPair<bool, QString> pasteInfo() const;
292
293 /**
294 * If \a tabsForFiles is true, the signal tabRequested() will also
295 * emitted also for files. Per default tabs for files is disabled
296 * and hence the signal tabRequested() will only be emitted for
297 * directories.
298 */
299 void setTabsForFilesEnabled(bool tabsForFiles);
300 bool isTabsForFilesEnabled() const;
301
302 /**
303 * Returns true if the current view allows folders to be expanded,
304 * i.e. presents a hierarchical view to the user.
305 */
306 bool itemsExpandable() const;
307
308 /**
309 * Restores the view state (current item, contents position, details view expansion state)
310 */
311 void restoreState(QDataStream& stream);
312
313 /**
314 * Saves the view state (current item, contents position, details view expansion state)
315 */
316 void saveState(QDataStream& stream);
317
318 /** Returns true, if at least one item is selected. */
319 bool hasSelection() const;
320
321 /**
322 * Returns the root item which represents the current URL. Note that the returned
323 * item can be null (KFileItem::isNull() will return true) in case that the directory
324 * has not been loaded.
325 */
326 KFileItem rootItem() const;
327
328 public slots:
329 /**
330 * Changes the directory to \a url. If the current directory is equal to
331 * \a url, nothing will be done (use DolphinView::reload() instead).
332 */
333 void setUrl(const KUrl& url);
334
335 /**
336 * Selects all items.
337 * @see DolphinView::selectedItems()
338 */
339 void selectAll();
340
341 /**
342 * Inverts the current selection: selected items get unselected,
343 * unselected items get selected.
344 * @see DolphinView::selectedItems()
345 */
346 void invertSelection();
347
348 void clearSelection();
349
350 /**
351 * Triggers the renaming of the currently selected items, where
352 * the user must input a new name for the items.
353 */
354 void renameSelectedItems();
355
356 /**
357 * Moves all selected items to the trash.
358 */
359 void trashSelectedItems();
360
361 /**
362 * Deletes all selected items.
363 */
364 void deleteSelectedItems();
365
366 /**
367 * Copies all selected items to the clipboard and marks
368 * the items as cut.
369 */
370 void cutSelectedItems();
371
372 /** Copies all selected items to the clipboard. */
373 void copySelectedItems();
374
375 /** Pastes the clipboard data to this view. */
376 void paste();
377
378 /**
379 * Pastes the clipboard data into the currently selected
380 * folder. If the current selection is not exactly one folder, no
381 * paste operation is done.
382 */
383 void pasteIntoFolder();
384
385 /**
386 * Turns on the file preview for the all files of the current directory,
387 * if \a show is true.
388 * If the view properties should be remembered for each directory
389 * (GeneralSettings::globalViewProps() returns false), then the
390 * preview setting will be stored automatically.
391 */
392 void setPreviewsShown(bool show);
393
394 /**
395 * Shows all hidden files of the current directory,
396 * if \a show is true.
397 * If the view properties should be remembered for each directory
398 * (GeneralSettings::globalViewProps() returns false), then the
399 * show hidden file setting will be stored automatically.
400 */
401 void setHiddenFilesShown(bool show);
402
403 /**
404 * Summarizes all sorted items by their category \a categorized
405 * is true.
406 * If the view properties should be remembered for each directory
407 * (GeneralSettings::globalViewProps() returns false), then the
408 * categorized sorting setting will be stored automatically.
409 */
410 void setCategorizedSorting(bool categorized);
411
412 /** Activates the view if the item list container gets focus. */
413 virtual bool eventFilter(QObject* watched, QEvent* event);
414
415 signals:
416 /**
417 * Is emitted if the view has been activated by e. g. a mouse click.
418 */
419 void activated();
420
421 /**
422 * Is emitted if the URL of the view will be changed to \a url.
423 * After the URL has been changed the signal urlChanged() will
424 * be emitted.
425 */
426 void urlAboutToBeChanged(const KUrl& url);
427
428 /** Is emitted if the URL of the view has been changed to \a url. */
429 void urlChanged(const KUrl& url);
430
431 /**
432 * Is emitted when clicking on an item with the left mouse button.
433 */
434 void itemActivated(const KFileItem& item);
435
436 /**
437 * Is emitted if items have been added or deleted.
438 */
439 void itemCountChanged();
440
441 /**
442 * Is emitted if a new tab should be opened for the URL \a url.
443 */
444 void tabRequested(const KUrl& url);
445
446 /**
447 * Is emitted if the view mode (IconsView, DetailsView,
448 * PreviewsView) has been changed.
449 */
450 void modeChanged(DolphinView::Mode current, DolphinView::Mode previous);
451
452 /** Is emitted if the 'show preview' property has been changed. */
453 void previewsShownChanged(bool shown);
454
455 /** Is emitted if the 'show hidden files' property has been changed. */
456 void hiddenFilesShownChanged(bool shown);
457
458 /** Is emitted if the 'categorized sorting' property has been changed. */
459 void categorizedSortingChanged(bool sortCategorized);
460
461 /** Is emitted if the sorting by name, size or date has been changed. */
462 void sortingChanged(DolphinView::Sorting sorting);
463
464 /** Is emitted if the sort order (ascending or descending) has been changed. */
465 void sortOrderChanged(Qt::SortOrder order);
466
467 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
468 void sortFoldersFirstChanged(bool foldersFirst);
469
470 /** Is emitted if the additional information shown for this view has been changed. */
471 void additionalInfoListChanged(const QList<DolphinView::AdditionalInfo>& current,
472 const QList<DolphinView::AdditionalInfo>& previous);
473
474 /** Is emitted if the zoom level has been changed by zooming in or out. */
475 void zoomLevelChanged(int current, int previous);
476
477 /**
478 * Is emitted if information of an item is requested to be shown e. g. in the panel.
479 * If item is null, no item information request is pending.
480 */
481 void requestItemInfo(const KFileItem& item);
482
483 /**
484 * Is emitted whenever the selection has been changed.
485 */
486 void selectionChanged(const KFileItemList& selection);
487
488 /**
489 * Is emitted if a context menu is requested for the item \a item,
490 * which is part of \a url. If the item is null, the context menu
491 * for the URL should be shown and the custom actions \a customActions
492 * will be added.
493 */
494 void requestContextMenu(const QPoint& pos,
495 const KFileItem& item,
496 const KUrl& url,
497 const QList<QAction*>& customActions);
498
499 /**
500 * Is emitted if an information message with the content \a msg
501 * should be shown.
502 */
503 void infoMessage(const QString& msg);
504
505 /**
506 * Is emitted if an error message with the content \a msg
507 * should be shown.
508 */
509 void errorMessage(const QString& msg);
510
511 /**
512 * Is emitted if an "operation completed" message with the content \a msg
513 * should be shown.
514 */
515 void operationCompletedMessage(const QString& msg);
516
517 /**
518 * Is emitted after DolphinView::setUrl() has been invoked and
519 * the path \a url is currently loaded. If this signal is emitted,
520 * it is assured that the view contains already the correct root
521 * URL and property settings.
522 */
523 void startedPathLoading(const KUrl& url);
524
525 /**
526 * Is emitted after the path triggered by DolphinView::setUrl()
527 * has been loaded.
528 */
529 void finishedPathLoading(const KUrl& url);
530
531 /**
532 * Is emitted after DolphinView::setUrl() has been invoked and provides
533 * the information how much percent of the current path have been loaded.
534 */
535 void pathLoadingProgress(int percent);
536
537 /**
538 * Is emitted if the DolphinView::setUrl() is invoked but the URL is not
539 * a directory.
540 */
541 void urlIsFileError(const KUrl& file);
542
543 /**
544 * Emitted when KDirLister emits redirection.
545 * Testcase: fish://localhost
546 */
547 void redirection(const KUrl& oldUrl, const KUrl& newUrl);
548
549 /**
550 * Is emitted when the write state of the folder has been changed. The application
551 * should disable all actions like "Create New..." that depend on the write
552 * state.
553 */
554 void writeStateChanged(bool isFolderWritable);
555
556 protected:
557 /** Changes the zoom level if Control is pressed during a wheel event. */
558 virtual void wheelEvent(QWheelEvent* event);
559
560 private slots:
561 /**
562 * Marks the view as active (DolphinView:isActive() will return true)
563 * and emits the 'activated' signal if it is not already active.
564 */
565 void activate();
566
567 void slotItemActivated(int index);
568 void slotItemMiddleClicked(int index);
569 void slotItemContextMenuRequested(int index, const QPointF& pos);
570 void slotViewContextMenuRequested(const QPointF& pos);
571 void slotHeaderContextMenuRequested(const QPointF& pos);
572 void slotItemExpansionToggleClicked(int index);
573 void slotItemHovered(int index);
574 void slotItemUnhovered(int index);
575 void slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
576 void slotModelChanged(KItemModelBase* current, KItemModelBase* previous);
577
578 /**
579 * Emits the signal \a selectionChanged() with a small delay. This is
580 * because getting all file items for the selection can be an expensive
581 * operation. Fast selection changes are collected in this case and
582 * the signal is emitted only after no selection change has been done
583 * within a small delay.
584 */
585 void slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous);
586
587 /**
588 * Is called by emitDelayedSelectionChangedSignal() and emits the
589 * signal \a selectionChanged() with all selected file items as parameter.
590 */
591 void emitSelectionChangedSignal();
592
593 /**
594 * Drops dragged URLs to the destination path \a destPath. If
595 * the URLs are dropped above an item inside the destination path,
596 * the item is indicated by \a destItem.
597 */
598 void dropUrls(const KFileItem& destItem,
599 const KUrl& destPath,
600 QDropEvent* event);
601
602 /**
603 * Updates the view properties of the current URL to the
604 * sorting given by \a sorting.
605 */
606 void updateSorting(DolphinView::Sorting sorting);
607
608 /**
609 * Updates the view properties of the current URL to the
610 * sort order given by \a order.
611 */
612 void updateSortOrder(Qt::SortOrder order);
613
614 /**
615 * Updates the view properties of the current URL to the
616 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
617 */
618 void updateSortFoldersFirst(bool foldersFirst);
619
620 /**
621 * Updates the status bar to show hover information for the
622 * item \a item. If currently other items are selected,
623 * no hover information is shown.
624 * @see DolphinView::clearHoverInformation()
625 */
626 void showHoverInformation(const KFileItem& item);
627
628 /**
629 * Clears the hover information shown in the status bar.
630 * @see DolphinView::showHoverInformation().
631 */
632 void clearHoverInformation();
633
634 /**
635 * Indicates in the status bar that the delete operation
636 * of the job \a job has been finished.
637 */
638 void slotDeleteFileFinished(KJob* job);
639
640 /**
641 * Invoked when the directory lister has been started the
642 * loading of \a url.
643 */
644 void slotDirListerStarted(const KUrl& url);
645
646 /**
647 * Invoked when the file item model indicates that the directory lister has completed the loading
648 * of items, and that expanded folders have been restored (if the view mode is 'Details', and the
649 * view state is restored after navigating back or forward in history). Assures that pasted items
650 * and renamed items get seleced.
651 */
652 void slotLoadingCompleted();
653
654 /**
655 * Is invoked when the KDirLister indicates refreshed items.
656 */
657 void slotRefreshItems();
658
659 /**
660 * Observes the item with the URL \a url. As soon as the directory
661 * model indicates that the item is available, the item will
662 * get selected and it is assured that the item stays visible.
663 *
664 * @see selectAndScrollToCreatedItem()
665 */
666 void observeCreatedItem(const KUrl& url);
667
668 /**
669 * Selects and scrolls to the item that got observed
670 * by observeCreatedItem().
671 */
672 void selectAndScrollToCreatedItem();
673
674 /**
675 * Called when a redirection happens.
676 * Testcase: fish://localhost
677 */
678 void slotRedirection(const KUrl& oldUrl, const KUrl& newUrl);
679
680 /**
681 * Applies the state that has been restored by restoreViewState()
682 * to the view.
683 */
684 void updateViewState();
685
686 //void slotUrlChangeRequested(const KUrl& url);
687
688 private:
689 KFileItemModel* fileItemModel() const;
690
691 void loadDirectory(const KUrl& url, bool reload = false);
692
693 /**
694 * Applies the view properties which are defined by the current URL
695 * to the DolphinView properties.
696 */
697 void applyViewProperties();
698
699 void applyAdditionalInfoListToView();
700
701 /**
702 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
703 * Pastes the clipboard data into the URL \a url.
704 */
705 void pasteToUrl(const KUrl& url);
706
707 /**
708 * Checks whether the current item view has the same zoom level
709 * as \a oldZoomLevel. If this is not the case, the zoom level
710 * of the controller is updated and a zoomLevelChanged() signal
711 * is emitted.
712 */
713 void updateZoomLevel(int oldZoomLevel);
714
715 /**
716 * Returns a list of URLs for all selected items. The list is
717 * simplified, so that when the URLs are part of different tree
718 * levels, only the parent is returned.
719 */
720 KUrl::List simplifiedSelectedUrls() const;
721
722 /**
723 * Returns the MIME data for all selected items.
724 */
725 QMimeData* selectionMimeData() const;
726
727 /**
728 * Is invoked after a paste operation or a drag & drop
729 * operation and URLs from \a mimeData as selected.
730 * This allows to select all newly pasted
731 * items in restoreViewState().
732 */
733 void markPastedUrlsAsSelected(const QMimeData* mimeData);
734
735 /**
736 * Updates m_isFolderWritable dependent on whether the folder represented by
737 * the current URL is writable. If the state has changed, the signal
738 * writeableStateChanged() will be emitted.
739 */
740 void updateWritableState();
741
742 QByteArray sortRoleForSorting(Sorting sorting) const;
743 Sorting sortingForSortRole(const QByteArray& sortRole) const;
744
745 private:
746 bool m_active : 1;
747 bool m_tabsForFiles : 1;
748 bool m_assureVisibleCurrentIndex : 1;
749 bool m_isFolderWritable : 1;
750
751 KUrl m_url;
752 Mode m_mode;
753 QList<AdditionalInfo> m_additionalInfoList;
754
755 QVBoxLayout* m_topLayout;
756
757 DolphinDirLister* m_dirLister;
758 DolphinItemListContainer* m_container;
759
760 ToolTipManager* m_toolTipManager;
761
762 QTimer* m_selectionChangedTimer;
763
764 KUrl m_currentItemUrl;
765 QPoint m_restoredContentsPosition;
766 KUrl m_createdItemUrl; // URL for a new item that got created by the "Create New..." menu
767 QList<KUrl> m_selectedUrls; // this is used for making the View to remember selections after F5
768
769 VersionControlObserver* m_versionControlObserver;
770
771 // For unit tests
772 friend class TestBase;
773 friend class DolphinDetailsViewTest;
774 };
775
776 /// Allow using DolphinView::Mode in QVariant
777 Q_DECLARE_METATYPE(DolphinView::Mode)
778
779 #endif // DOLPHINVIEW_H