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