]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.h
The filenamesearch ioslave has been moved to kio-extras, remove it from Dolphin.
[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-baloo.h>
25
26 #include "libdolphin_export.h"
27
28 #include <kparts/part.h>
29 #include <KFileItem>
30 #include <kio/fileundomanager.h>
31 #include <KIO/Job>
32 #include <KUrl>
33
34 #include <QWidget>
35
36 typedef KIO::FileUndoManager::CommandType CommandType;
37 class QVBoxLayout;
38 class DolphinItemListView;
39 class KFileItemModel;
40 class KItemListContainer;
41 class KItemModelBase;
42 class KItemSet;
43 class KUrl;
44 class ToolTipManager;
45 class VersionControlObserver;
46 class ViewProperties;
47 class QGraphicsSceneDragDropEvent;
48 class QRegExp;
49
50 /**
51 * @short Represents a view for the directory content.
52 *
53 * View modes for icons, compact and details are supported. It's
54 * possible to adjust:
55 * - sort order
56 * - sort type
57 * - show hidden files
58 * - show previews
59 * - enable grouping
60 */
61 class LIBDOLPHINPRIVATE_EXPORT DolphinView : public QWidget
62 {
63 Q_OBJECT
64
65 public:
66 /**
67 * Defines the view mode for a directory. The
68 * view mode is automatically updated if the directory itself
69 * defines a view mode (see class ViewProperties for details).
70 */
71 enum Mode
72 {
73 /**
74 * The items are shown as icons with a name-label below.
75 */
76 IconsView = 0,
77
78 /**
79 * The icon, the name and the size of the items are
80 * shown per default as a table.
81 */
82 DetailsView,
83
84 /**
85 * The items are shown as icons with the name-label aligned
86 * to the right side.
87 */
88 CompactView
89 };
90
91 /**
92 * @param url Specifies the content which should be shown.
93 * @param parent Parent widget of the view.
94 */
95 DolphinView(const KUrl& url, QWidget* parent);
96
97 virtual ~DolphinView();
98
99 /**
100 * Returns the current active URL, where all actions are applied.
101 * The URL navigator is synchronized with this URL.
102 */
103 KUrl url() const;
104
105 /**
106 * If \a active is true, the view will marked as active. The active
107 * view is defined as view where all actions are applied to.
108 */
109 void setActive(bool active);
110 bool isActive() const;
111
112 /**
113 * Changes the view mode for the current directory to \a mode.
114 * If the view properties should be remembered for each directory
115 * (GeneralSettings::globalViewProps() returns false), then the
116 * changed view mode will be stored automatically.
117 */
118 void setMode(Mode mode);
119 Mode mode() const;
120
121 /**
122 * Turns on the file preview for the all files of the current directory,
123 * if \a show is true.
124 * If the view properties should be remembered for each directory
125 * (GeneralSettings::globalViewProps() returns false), then the
126 * preview setting will be stored automatically.
127 */
128 void setPreviewsShown(bool show);
129 bool previewsShown() const;
130
131 /**
132 * Shows all hidden files of the current directory,
133 * if \a show is true.
134 * If the view properties should be remembered for each directory
135 * (GeneralSettings::globalViewProps() returns false), then the
136 * show hidden file setting will be stored automatically.
137 */
138 void setHiddenFilesShown(bool show);
139 bool hiddenFilesShown() const;
140
141 /**
142 * Turns on sorting by groups if \a enable is true.
143 */
144 void setGroupedSorting(bool grouped);
145 bool groupedSorting() const;
146
147 /**
148 * Returns the items of the view.
149 */
150 KFileItemList items() const;
151
152 /**
153 * @return The number of items. itemsCount() is faster in comparison
154 * to items().count().
155 */
156 int itemsCount() const;
157
158 /**
159 * Returns the selected items. The list is empty if no item has been
160 * selected.
161 */
162 KFileItemList selectedItems() const;
163
164 /**
165 * Returns the number of selected items (this is faster than
166 * invoking selectedItems().count()).
167 */
168 int selectedItemsCount() const;
169
170 /**
171 * Marks the items indicated by \p urls to get selected after the
172 * directory DolphinView::url() has been loaded. Note that nothing
173 * gets selected if no loading of a directory has been triggered
174 * by DolphinView::setUrl() or DolphinView::reload().
175 */
176 void markUrlsAsSelected(const QList<KUrl>& urls);
177
178 /**
179 * Marks the item indicated by \p url to be scrolled to and as the
180 * current item after directory DolphinView::url() has been loaded.
181 */
182 void markUrlAsCurrent(const KUrl& url);
183
184 /**
185 * All items that match to the pattern \a pattern will get selected
186 * if \a enabled is true and deselected if \a enabled is false.
187 */
188 void selectItems(const QRegExp& pattern, bool enabled);
189
190 /**
191 * Sets the zoom level to \a level. It is assured that the used
192 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
193 * ZoomLevelInfo::maximumLevel().
194 */
195 void setZoomLevel(int level);
196 int zoomLevel() const;
197
198 void setSortRole(const QByteArray& role);
199 QByteArray sortRole() const;
200
201 void setSortOrder(Qt::SortOrder order);
202 Qt::SortOrder sortOrder() const;
203
204 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
205 void setSortFoldersFirst(bool foldersFirst);
206 bool sortFoldersFirst() const;
207
208 /** Sets the additional information which should be shown for the items. */
209 void setVisibleRoles(const QList<QByteArray>& roles);
210
211 /** Returns the additional information which should be shown for the items. */
212 QList<QByteArray> visibleRoles() const;
213
214 void reload();
215
216 /**
217 * Refreshes the view to get synchronized with the settings (e.g. icons size,
218 * font, ...).
219 */
220 void readSettings();
221
222 /**
223 * Saves the current settings (e.g. icons size, font, ..).
224 */
225 void writeSettings();
226
227 /**
228 * Filters the currently shown items by \a nameFilter. All items
229 * which contain the given filter string will be shown.
230 */
231 void setNameFilter(const QString& nameFilter);
232 QString nameFilter() const;
233
234 /**
235 * Filters the currently shown items by \a filters. All items
236 * whose content-type matches those given by the list of filters
237 * will be shown.
238 */
239 void setMimeTypeFilters(const QStringList& filters);
240 QStringList mimeTypeFilters() const;
241
242 /**
243 * Returns a textual representation of the state of the current
244 * folder or selected items, suitable for use in the status bar.
245 */
246 QString statusBarText() const;
247
248 /**
249 * Returns the version control actions that are provided for the items \p items.
250 * Usually the actions are presented in the context menu.
251 */
252 QList<QAction*> versionControlActions(const KFileItemList& items) const;
253
254 /**
255 * Returns the state of the paste action:
256 * first is whether the action should be enabled
257 * second is the text for the action
258 */
259 QPair<bool, QString> pasteInfo() const;
260
261 /**
262 * If \a tabsForFiles is true, the signal tabRequested() will also
263 * emitted also for files. Per default tabs for files is disabled
264 * and hence the signal tabRequested() will only be emitted for
265 * directories.
266 */
267 void setTabsForFilesEnabled(bool tabsForFiles);
268 bool isTabsForFilesEnabled() const;
269
270 /**
271 * Returns true if the current view allows folders to be expanded,
272 * i.e. presents a hierarchical view to the user.
273 */
274 bool itemsExpandable() const;
275
276 /**
277 * Restores the view state (current item, contents position, details view expansion state)
278 */
279 void restoreState(QDataStream& stream);
280
281 /**
282 * Saves the view state (current item, contents position, details view expansion state)
283 */
284 void saveState(QDataStream& stream);
285
286 /**
287 * Returns the root item which represents the current URL.
288 */
289 KFileItem rootItem() const;
290
291 /**
292 * Sets a context that is used for remembering the view-properties.
293 * Per default the context is empty and the path of the currently set URL
294 * is used for remembering the view-properties. Setting a custom context
295 * makes sense if specific types of URLs (e.g. search-URLs) should
296 * share common view-properties.
297 */
298 void setViewPropertiesContext(const QString& context);
299 QString viewPropertiesContext() const;
300
301 /**
302 * Checks if the given \a item can be opened as folder (e.g. archives).
303 * This function will also adjust the \a url (e.g. change the protocol).
304 * @return a valid and adjusted url if the item can be opened as folder,
305 * otherwise return an empty url.
306 */
307 static KUrl openItemAsFolderUrl(const KFileItem& item, const bool browseThroughArchives = true);
308
309 public slots:
310 /**
311 * Changes the directory to \a url. If the current directory is equal to
312 * \a url, nothing will be done (use DolphinView::reload() instead).
313 */
314 void setUrl(const KUrl& url);
315
316 /**
317 * Selects all items.
318 * @see DolphinView::selectedItems()
319 */
320 void selectAll();
321
322 /**
323 * Inverts the current selection: selected items get unselected,
324 * unselected items get selected.
325 * @see DolphinView::selectedItems()
326 */
327 void invertSelection();
328
329 void clearSelection();
330
331 /**
332 * Triggers the renaming of the currently selected items, where
333 * the user must input a new name for the items.
334 */
335 void renameSelectedItems();
336
337 /**
338 * Moves all selected items to the trash.
339 */
340 void trashSelectedItems();
341
342 /**
343 * Deletes all selected items.
344 */
345 void deleteSelectedItems();
346
347 /**
348 * Copies all selected items to the clipboard and marks
349 * the items as cut.
350 */
351 void cutSelectedItems();
352
353 /** Copies all selected items to the clipboard. */
354 void copySelectedItems();
355
356 /** Pastes the clipboard data to this view. */
357 void paste();
358
359 /**
360 * Pastes the clipboard data into the currently selected
361 * folder. If the current selection is not exactly one folder, no
362 * paste operation is done.
363 */
364 void pasteIntoFolder();
365
366 void stopLoading();
367
368 /** Activates the view if the item list container gets focus. */
369 virtual bool eventFilter(QObject* watched, QEvent* event);
370
371 signals:
372 /**
373 * Is emitted if the view has been activated by e. g. a mouse click.
374 */
375 void activated();
376
377 /**
378 * Is emitted if the URL of the view will be changed to \a url.
379 * After the URL has been changed the signal urlChanged() will
380 * be emitted.
381 */
382 void urlAboutToBeChanged(const KUrl& url);
383
384 /** Is emitted if the URL of the view has been changed to \a url. */
385 void urlChanged(const KUrl& url);
386
387 /**
388 * Is emitted when clicking on an item with the left mouse button.
389 */
390 void itemActivated(const KFileItem& item);
391
392 /**
393 * Is emitted when multiple items have been activated by e. g.
394 * context menu open with.
395 */
396 void itemsActivated(const KFileItemList& items);
397
398 /**
399 * Is emitted if items have been added or deleted.
400 */
401 void itemCountChanged();
402
403 /**
404 * Is emitted if a new tab should be opened for the URL \a url.
405 */
406 void tabRequested(const KUrl& url);
407
408 /**
409 * Is emitted if the view mode (IconsView, DetailsView,
410 * PreviewsView) has been changed.
411 */
412 void modeChanged(DolphinView::Mode current, DolphinView::Mode previous);
413
414 /** Is emitted if the 'show preview' property has been changed. */
415 void previewsShownChanged(bool shown);
416
417 /** Is emitted if the 'show hidden files' property has been changed. */
418 void hiddenFilesShownChanged(bool shown);
419
420 /** Is emitted if the 'grouped sorting' property has been changed. */
421 void groupedSortingChanged(bool groupedSorting);
422
423 /** Is emitted if the sorting by name, size or date has been changed. */
424 void sortRoleChanged(const QByteArray& role);
425
426 /** Is emitted if the sort order (ascending or descending) has been changed. */
427 void sortOrderChanged(Qt::SortOrder order);
428
429 /**
430 * Is emitted if the sorting of files and folders (separate with folders
431 * first or mixed) has been changed.
432 */
433 void sortFoldersFirstChanged(bool foldersFirst);
434
435 /** Is emitted if the additional information shown for this view has been changed. */
436 void visibleRolesChanged(const QList<QByteArray>& current,
437 const QList<QByteArray>& previous);
438
439 /** Is emitted if the zoom level has been changed by zooming in or out. */
440 void zoomLevelChanged(int current, int previous);
441
442 /**
443 * Is emitted if information of an item is requested to be shown e. g. in the panel.
444 * If item is null, no item information request is pending.
445 */
446 void requestItemInfo(const KFileItem& item);
447
448 /**
449 * Is emitted whenever the selection has been changed.
450 */
451 void selectionChanged(const KFileItemList& selection);
452
453 /**
454 * Is emitted if a context menu is requested for the item \a item,
455 * which is part of \a url. If the item is null, the context menu
456 * for the URL should be shown and the custom actions \a customActions
457 * will be added.
458 */
459 void requestContextMenu(const QPoint& pos,
460 const KFileItem& item,
461 const KUrl& url,
462 const QList<QAction*>& customActions);
463
464 /**
465 * Is emitted if an information message with the content \a msg
466 * should be shown.
467 */
468 void infoMessage(const QString& msg);
469
470 /**
471 * Is emitted if an error message with the content \a msg
472 * should be shown.
473 */
474 void errorMessage(const QString& msg);
475
476 /**
477 * Is emitted if an "operation completed" message with the content \a msg
478 * should be shown.
479 */
480 void operationCompletedMessage(const QString& msg);
481
482 /**
483 * Is emitted after DolphinView::setUrl() has been invoked and
484 * the current directory is loaded. If this signal is emitted,
485 * it is assured that the view contains already the correct root
486 * URL and property settings.
487 */
488 void directoryLoadingStarted();
489
490 /**
491 * Is emitted after the directory triggered by DolphinView::setUrl()
492 * has been loaded.
493 */
494 void directoryLoadingCompleted();
495
496 /**
497 * Is emitted after the directory loading triggered by DolphinView::setUrl()
498 * has been canceled.
499 */
500 void directoryLoadingCanceled();
501
502 /**
503 * Is emitted after DolphinView::setUrl() has been invoked and provides
504 * the information how much percent of the current directory have been loaded.
505 */
506 void directoryLoadingProgress(int percent);
507
508 /**
509 * Is emitted if the sorting is done asynchronously and provides the
510 * progress information of the sorting.
511 */
512 void directorySortingProgress(int percent);
513
514 /**
515 * Emitted when the file-item-model emits redirection.
516 * Testcase: fish://localhost
517 */
518 void redirection(const KUrl& oldUrl, const KUrl& newUrl);
519
520 /**
521 * Is emitted when the URL set by DolphinView::setUrl() represents a file.
522 * In this case no signal errorMessage() will be emitted.
523 */
524 void urlIsFileError(const KUrl& url);
525
526 /**
527 * Is emitted when the write state of the folder has been changed. The application
528 * should disable all actions like "Create New..." that depend on the write
529 * state.
530 */
531 void writeStateChanged(bool isFolderWritable);
532
533 /**
534 * Is emitted if the URL should be changed to the previous URL of the
535 * history (e.g. because the "back"-mousebutton has been pressed).
536 */
537 void goBackRequested();
538
539 /**
540 * Is emitted if the URL should be changed to the next URL of the
541 * history (e.g. because the "next"-mousebutton has been pressed).
542 */
543 void goForwardRequested();
544
545 protected:
546 /** Changes the zoom level if Control is pressed during a wheel event. */
547 virtual void wheelEvent(QWheelEvent* event);
548
549 /** @reimp */
550 virtual void hideEvent(QHideEvent* event);
551 virtual bool event(QEvent* event);
552
553 private slots:
554 /**
555 * Marks the view as active (DolphinView:isActive() will return true)
556 * and emits the 'activated' signal if it is not already active.
557 */
558 void activate();
559
560 void slotItemActivated(int index);
561 void slotItemsActivated(const KItemSet& indexes);
562 void slotItemMiddleClicked(int index);
563 void slotItemContextMenuRequested(int index, const QPointF& pos);
564 void slotViewContextMenuRequested(const QPointF& pos);
565 void slotHeaderContextMenuRequested(const QPointF& pos);
566 void slotHeaderColumnWidthChanged(const QByteArray& role, qreal current, qreal previous);
567 void slotItemHovered(int index);
568 void slotItemUnhovered(int index);
569 void slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
570 void slotModelChanged(KItemModelBase* current, KItemModelBase* previous);
571 void slotMouseButtonPressed(int itemIndex, Qt::MouseButtons buttons);
572
573 /*
574 * Is called when new items get pasted or dropped.
575 */
576 void slotAboutToCreate(const KUrl::List& urls);
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 KItemSet& current, const KItemSet& 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 * Updates the view properties of the current URL to the
595 * sorting given by \a role.
596 */
597 void updateSortRole(const QByteArray& role);
598
599 /**
600 * Updates the view properties of the current URL to the
601 * sort order given by \a order.
602 */
603 void updateSortOrder(Qt::SortOrder order);
604
605 /**
606 * Updates the view properties of the current URL to the
607 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
608 */
609 void updateSortFoldersFirst(bool foldersFirst);
610
611 /**
612 * Indicates in the status bar that the delete operation
613 * of the job \a job has been finished.
614 */
615 void slotDeleteFileFinished(KJob* job);
616
617 /**
618 * Indicates in the status bar that the trash operation
619 * of the job \a job has been finished.
620 */
621 void slotTrashFileFinished(KJob* job);
622
623 /**
624 * Invoked when the rename job is done, for error handling.
625 */
626 void slotRenamingResult(KJob* job);
627
628 /**
629 * Invoked when the file item model has started the loading
630 * of the directory specified by DolphinView::url().
631 */
632 void slotDirectoryLoadingStarted();
633
634 /**
635 * Invoked when the file item model indicates that the loading of a directory has
636 * been completed. Assures that pasted items and renamed items get seleced.
637 */
638 void slotDirectoryLoadingCompleted();
639
640 /**
641 * Is invoked when items of KFileItemModel have been changed.
642 */
643 void slotItemsChanged();
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 * Is invoked when the visible roles have been changed by the user by dragging
659 * a header item. The view properties of the directory will get updated.
660 */
661 void slotVisibleRolesChangedByHeader(const QList<QByteArray>& current,
662 const QList<QByteArray>& previous);
663
664 void slotRoleEditingCanceled();
665 void slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value);
666
667 /**
668 * Observes the item with the URL \a url. As soon as the directory
669 * model indicates that the item is available, the item will
670 * get selected and it is assured that the item stays visible.
671 */
672 void observeCreatedItem(const KUrl& url);
673
674 /**
675 * Called when a redirection happens.
676 * Testcase: fish://localhost
677 */
678 void slotDirectoryRedirection(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 hideToolTip();
687
688 /**
689 * Calculates the number of currently shown files into
690 * \a fileCount and the number of folders into \a folderCount.
691 * The size of all files is written into \a totalFileSize.
692 * It is recommend using this method instead of asking the
693 * directory lister or the model directly, as it takes
694 * filtering and hierarchical previews into account.
695 */
696 void calculateItemCount(int& fileCount, int& folderCount, KIO::filesize_t& totalFileSize) const;
697
698 private:
699 void loadDirectory(const KUrl& url, bool reload = false);
700
701 /**
702 * Applies the view properties which are defined by the current URL
703 * to the DolphinView properties. The view properties are read from a
704 * .directory file either in the current directory, or in the
705 * share/apps/dolphin/view_properties/ subfolder of the user's .kde folder.
706 */
707 void applyViewProperties();
708
709 /**
710 * Applies the given view properties to the DolphinView.
711 */
712 void applyViewProperties(const ViewProperties& props);
713
714 /**
715 * Applies the m_mode property to the corresponding
716 * itemlayout-property of the KItemListView.
717 */
718 void applyModeToView();
719
720 /**
721 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
722 * Pastes the clipboard data into the URL \a url.
723 */
724 void pasteToUrl(const KUrl& url);
725
726 /**
727 * Returns a list of URLs for all selected items. The list is
728 * simplified, so that when the URLs are part of different tree
729 * levels, only the parent is returned.
730 */
731 KUrl::List simplifiedSelectedUrls() const;
732
733 /**
734 * Returns the MIME data for all selected items.
735 */
736 QMimeData* selectionMimeData() const;
737
738 /**
739 * Updates m_isFolderWritable dependent on whether the folder represented by
740 * the current URL is writable. If the state has changed, the signal
741 * writeableStateChanged() will be emitted.
742 */
743 void updateWritableState();
744
745 /**
746 * @return The current URL if no viewproperties-context is given (see
747 * DolphinView::viewPropertiesContext(), otherwise the context
748 * is returned.
749 */
750 KUrl viewPropertiesUrl() const;
751
752 private:
753 bool m_active;
754 bool m_tabsForFiles;
755 bool m_assureVisibleCurrentIndex;
756 bool m_isFolderWritable;
757 bool m_dragging; // True if a dragging is done. Required to be able to decide whether a
758 // tooltip may be shown when hovering an item.
759
760 KUrl m_url;
761 QString m_viewPropertiesContext;
762 Mode m_mode;
763 QList<QByteArray> m_visibleRoles;
764
765 QVBoxLayout* m_topLayout;
766
767 KFileItemModel* m_model;
768 DolphinItemListView* m_view;
769 KItemListContainer* m_container;
770
771 ToolTipManager* m_toolTipManager;
772
773 QTimer* m_selectionChangedTimer;
774
775 KUrl m_currentItemUrl; // Used for making the view to remember the current URL after F5
776 bool m_scrollToCurrentItem; // Used for marking we need to scroll to current item or not
777 QPoint m_restoredContentsPosition;
778
779 QList<KUrl> m_selectedUrls; // Used for making the view to remember selections after F5
780 bool m_clearSelectionBeforeSelectingNewItems;
781 bool m_markFirstNewlySelectedItemAsCurrent;
782
783 VersionControlObserver* m_versionControlObserver;
784
785 // For unit tests
786 friend class TestBase;
787 friend class DolphinDetailsViewTest;
788 friend class DolphinPart; // Accesses m_model
789 };
790
791 /// Allow using DolphinView::Mode in QVariant
792 Q_DECLARE_METATYPE(DolphinView::Mode)
793
794 #endif // DOLPHINVIEW_H