]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.h
Move the code handling the additional-info-actions to DolphinView; simplify signal...
[dolphin.git] / src / dolphinview.h
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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.h>
31 #include <kfileitemdelegate.h>
32 #include <konq_fileundomanager.h>
33 #include <kio/job.h>
34
35 #include <QBoxLayout>
36 #include <QKeyEvent>
37 #include <QLinkedList>
38 #include <QListView>
39 #include <QWidget>
40
41 class QActionGroup;
42 class KAction;
43 class KToggleAction;
44 class DolphinController;
45 class KDirLister;
46 class KFileItemDelegate;
47 class KUrl;
48 class DolphinModel;
49 class DolphinColumnView;
50 class DolphinDetailsView;
51 class DolphinIconsView;
52 class DolphinMainWindow;
53 class DolphinSortFilterProxyModel;
54 class QModelIndex;
55 class ViewProperties;
56 namespace KIO
57 {
58 class PreviewJob;
59 }
60
61 /**
62 * @short Represents a view for the directory content.
63 *
64 * View modes for icons, details and columns are supported. It's
65 * possible to adjust:
66 * - sort order
67 * - sort type
68 * - show hidden files
69 * - show previews
70 *
71 * @see DolphinIconsView
72 * @see DolphinDetailsView
73 * @see DolphinColumnView
74 */
75 class LIBDOLPHINPRIVATE_EXPORT DolphinView : public QWidget
76 {
77 Q_OBJECT
78
79 public:
80 /**
81 * Defines the view mode for a directory. The view mode
82 * can be defined when constructing a DolphinView. The
83 * view mode is automatically updated if the directory itself
84 * defines a view mode (see class ViewProperties for details).
85 */
86 enum Mode
87 {
88 /**
89 * The directory items are shown as icons including an
90 * icon name. */
91 IconsView = 0,
92
93 /**
94 * The icon, the name and at least the size of the directory
95 * items are shown in a table. It is possible to add columns
96 * for date, group and permissions.
97 */
98 DetailsView = 1,
99
100 /**
101 * Each folder is shown in a separate column.
102 */
103 ColumnView = 2,
104 MaxModeEnum = ColumnView
105 };
106
107 /** Defines the sort order for the items of a directory. */
108 enum Sorting
109 {
110 SortByName = 0,
111 SortBySize,
112 SortByDate,
113 SortByPermissions,
114 SortByOwner,
115 SortByGroup,
116 SortByType,
117 SortByRating,
118 SortByTags,
119 MaxSortEnum = SortByTags
120 };
121
122 /**
123 * @param parent Parent widget of the view.
124 * @param url Specifies the content which should be shown.
125 * @param dirLister Used directory lister. The lister is not owned
126 * by the view and won't get deleted.
127 * @param dolphinModel Used directory model. The model is not owned
128 * by the view and won't get deleted.
129 * @param proxyModel Used proxy model which specifies the sorting. The
130 * model is not owned by the view and won't get
131 * deleted.
132 */
133 DolphinView(QWidget* parent,
134 const KUrl& url,
135 KDirLister* dirLister,
136 DolphinModel* dolphinModel,
137 DolphinSortFilterProxyModel* proxyModel);
138
139 virtual ~DolphinView();
140
141 /**
142 * Returns the current active URL, where all actions are applied.
143 * The URL navigator is synchronized with this URL.
144 */
145 const KUrl& url() const;
146
147 /**
148 * Returns the root URL of the view, which is defined as the first
149 * visible path of DolphinView::url(). Usually the root URL is
150 * equal to DolphinView::url(), but in the case of the column view
151 * when 2 columns are shown, the root URL might be:
152 * /home/peter/Documents
153 * and DolphinView::url() might return
154 * /home/peter/Documents/Music/
155 */
156 KUrl rootUrl() const;
157
158 /**
159 * If \a active is true, the view will marked as active. The active
160 * view is defined as view where all actions are applied to.
161 */
162 void setActive(bool active);
163 bool isActive() const;
164
165 /**
166 * Changes the view mode for the current directory to \a mode.
167 * If the view properties should be remembered for each directory
168 * (GeneralSettings::globalViewProps() returns false), then the
169 * changed view mode will be be stored automatically.
170 */
171 void setMode(Mode mode);
172 Mode mode() const;
173
174 /**
175 * Turns on the file preview for the all files of the current directory,
176 * if \a show is true.
177 * If the view properties should be remembered for each directory
178 * (GeneralSettings::globalViewProps() returns false), then the
179 * preview setting will be be stored automatically.
180 */
181 void setShowPreview(bool show);
182 bool showPreview() const;
183
184 /**
185 * Shows all hidden files of the current directory,
186 * if \a show is true.
187 * If the view properties should be remembered for each directory
188 * (GeneralSettings::globalViewProps() returns false), then the
189 * show hidden file setting will be be stored automatically.
190 */
191 void setShowHiddenFiles(bool show);
192 bool showHiddenFiles() const;
193
194 /**
195 * Summarizes all sorted items by their category \a categorized
196 * is true.
197 * If the view properties should be remembered for each directory
198 * (GeneralSettings::globalViewProps() returns false), then the
199 * categorized sorting setting will be be stored automatically.
200 */
201 void setCategorizedSorting(bool categorized);
202 bool categorizedSorting() const;
203
204 /**
205 * Returns true, if the categorized sorting is supported by the current
206 * used mode (see DolphinView::setMode()). Currently only DolphinView::IconsView
207 * supports categorizations. To check whether the categorized
208 * sorting is set, use DolphinView::categorizedSorting().
209 */
210 bool supportsCategorizedSorting() const;
211
212 /**
213 * Selects all items.
214 * @see DolphinView::selectedItems()
215 */
216 void selectAll();
217
218 /**
219 * Inverts the current selection: selected items get unselected,
220 * unselected items get selected.
221 * @see DolphinView::selectedItems()
222 */
223 void invertSelection();
224
225 /** Returns true, if at least one item is selected. */
226 bool hasSelection() const;
227
228 void clearSelection();
229
230 /**
231 * Returns the selected items. The list is empty if no item has been
232 * selected.
233 * @see DolphinView::selectedUrls()
234 */
235 KFileItemList selectedItems() const;
236
237 /**
238 * Returns a list of URLs for all selected items. An empty list
239 * is returned, if no item is selected.
240 * @see DolphinView::selectedItems()
241 */
242 KUrl::List selectedUrls() const;
243
244 /**
245 * Returns the file item for the given model index \a index.
246 */
247 KFileItem fileItem(const QModelIndex& index) const;
248
249 /**
250 * Sets the upper left position of the view content
251 * to (x,y). The content of the view might be larger than the visible area
252 * and hence a scrolling must be done.
253 */
254 void setContentsPosition(int x, int y);
255
256 /** Returns the upper left position of the view content. */
257 QPoint contentsPosition() const;
258
259 /** Increases the size of the current set view mode. */
260 void zoomIn();
261
262 /** Decreases the size of the current set view mode. */
263 void zoomOut();
264
265 /**
266 * Returns true, if zooming in is possible. If false is returned,
267 * the minimal zoom size is possible.
268 */
269 bool isZoomInPossible() const;
270
271 /**
272 * Returns true, if zooming out is possible. If false is returned,
273 * the maximum zoom size is possible.
274 */
275 bool isZoomOutPossible() const;
276
277 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
278 void setSorting(Sorting sorting);
279
280 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
281 Sorting sorting() const;
282
283 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
284 void setSortOrder(Qt::SortOrder order);
285
286 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
287 Qt::SortOrder sortOrder() const;
288
289 /** Sets the additional information which should be shown for the items. */
290 void setAdditionalInfo(KFileItemDelegate::InformationList info);
291
292 /** Returns the additional information which should be shown for the items. */
293 KFileItemDelegate::InformationList additionalInfo() const;
294
295 /** Reloads the current directory. */
296 void reload();
297
298 /**
299 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
300 * This method only needs to get invoked if the view settings for the Icons View,
301 * Details View or Columns View have been changed.
302 */
303 void refresh();
304
305 /**
306 * Changes the directory of the view to \a url. If \a rootUrl is empty, the view
307 * properties from \a url are used for adjusting the view mode and the other properties.
308 * If \a rootUrl is not empty, the view properties from the root URL are considered
309 * instead. Specifying a root URL is only required if a view having a different root URL
310 * (e. g. the column view) should be restored. Usually using DolphinView::setUrl()
311 * is enough for changing the current URL.
312 */
313 void updateView(const KUrl& url, const KUrl& rootUrl);
314
315 /**
316 * Filters the currently shown items by \a nameFilter. All items
317 * which contain the given filter string will be shown.
318 */
319 void setNameFilter(const QString& nameFilter);
320
321 /**
322 * Calculates the number of currently shown files into
323 * \a fileCount and the number of folders into \a folderCount.
324 * It is recommend using this method instead of asking the
325 * directory lister or the model directly, as it takes
326 * filtering and hierarchical previews into account.
327 */
328 void calculateItemCount(int& fileCount, int& folderCount);
329
330 /**
331 * Returns the "switch to icons mode" action.
332 * This code is here to share it between the mainwindow and the part
333 */
334 static KToggleAction* iconsModeAction(KActionCollection* collection);
335
336 /**
337 * Returns the "switch to details mode" action.
338 * This code is here to share it between the mainwindow and the part
339 */
340 static KToggleAction* detailsModeAction(KActionCollection* collection);
341
342 /**
343 * Returns the "switch to columns mode" action.
344 * This code is here to share it between the mainwindow and the part
345 */
346 static KToggleAction* columnsModeAction(KActionCollection* collection);
347
348 /**
349 * Creates the rename action.
350 * This code is here to share it between the mainwindow and the part
351 */
352 static KAction* createRenameAction(KActionCollection* collection);
353
354 /**
355 * Creates the "move to trash" action.
356 * This code is here to share it between the mainwindow and the part
357 */
358 static KAction* createMoveToTrashAction(KActionCollection* collection);
359
360 /**
361 * Creates the delete action.
362 * This code is here to share it between the mainwindow and the part
363 */
364 static KAction* createDeleteAction(KActionCollection* collection);
365
366 /**
367 * Creates the "new directory" action.
368 * This code is here to share it between the mainwindow and the part
369 */
370 static KAction* createNewDirAction(KActionCollection* collection);
371
372 /**
373 * Creates the "sort descending" action.
374 * This code is here to share it between the mainwindow and the part
375 */
376 static KAction* createSortDescendingAction(KActionCollection* collection);
377
378 /**
379 * Creates an action group with all the "show additional information" actions in it.
380 * This code is here to share it between the mainwindow and the part
381 */
382 static QActionGroup* createAdditionalInformationActionGroup(KActionCollection* collection);
383
384 /**
385 * Updates the state of the 'Additional Information' actions in \a collection.
386 */
387 void updateAdditionalInfoActions(KActionCollection* collection);
388
389 /**
390 * Returns the action name corresponding to the current view mode
391 */
392 QString currentViewModeActionName() const;
393
394 /**
395 * Returns the state of the paste action:
396 * first is whether the action should be enabled
397 * second is the text for the action
398 */
399 QPair<bool, QString> pasteInfo() const;
400
401 public slots:
402 /**
403 * Changes the directory to \a url. If the current directory is equal to
404 * \a url, nothing will be done (use DolphinView::reload() instead).
405 */
406 void setUrl(const KUrl& url);
407
408 /**
409 * Request of a selection change. The view will do its best to accommodate
410 * the request, but it is not guaranteed that all items in \a selection
411 * will actually get selected. The view will e.g. not select items which
412 * are not in the currently displayed folder.
413 */
414 void changeSelection(const KFileItemList& selection);
415
416 /**
417 * Triggers the renaming of the currently selected items, where
418 * the user must input a new name for the items.
419 */
420 void renameSelectedItems();
421
422 /**
423 * Moves all selected items to the trash.
424 */
425 void trashSelectedItems();
426
427 /**
428 * Deletes all selected items.
429 */
430 void deleteSelectedItems();
431
432 /**
433 * Copies all selected items to the clipboard and marks
434 * the items as cutted.
435 */
436 void cutSelectedItems();
437
438 /** Copies all selected items to the clipboard. */
439 void copySelectedItems();
440
441 /** Pastes the clipboard data to this view. */
442 void paste();
443
444 /** Switches between an ascending and descending sorting order. */
445 void toggleSortOrder();
446
447 /**
448 * Switches on or off the displaying of additional information
449 * as specified by \a action.
450 */
451 void toggleAdditionalInfo(QAction* action);
452
453 signals:
454 /**
455 * Is emitted if the view has been activated by e. g. a mouse click.
456 */
457 void activated();
458
459 /** Is emitted if URL of the view has been changed to \a url. */
460 void urlChanged(const KUrl& url);
461
462 /**
463 * Is emitted when clicking on an item
464 */
465 void itemTriggered(const KFileItem& item);
466
467 /**
468 * Is emitted if the view mode (IconsView, DetailsView,
469 * PreviewsView) has been changed.
470 */
471 void modeChanged();
472
473 /** Is emitted if the 'show preview' property has been changed. */
474 void showPreviewChanged();
475
476 /** Is emitted if the 'show hidden files' property has been changed. */
477 void showHiddenFilesChanged();
478
479 /** Is emitted if the 'categorized sorting' property has been changed. */
480 void categorizedSortingChanged();
481
482 /** Is emitted if the sorting by name, size or date has been changed. */
483 void sortingChanged(DolphinView::Sorting sorting);
484
485 /** Is emitted if the sort order (ascending or descending) has been changed. */
486 void sortOrderChanged(Qt::SortOrder order);
487
488 /** Is emitted if the additional information shown for this view has been changed. */
489 void additionalInfoChanged();
490
491 /**
492 * Is emitted if information of an item is requested to be shown e. g. in the sidebar.
493 * If item is null, no item information request is pending.
494 */
495 void requestItemInfo(const KFileItem& item);
496
497 /** Is emitted if the contents has been moved to \a x, \a y. */
498 void contentsMoved(int x, int y);
499
500 /**
501 * Is emitted whenever the selection has been changed.
502 */
503 void selectionChanged(const KFileItemList& selection);
504
505 /**
506 * Is emitted if a context menu is requested for the item \a item,
507 * which is part of \a url. If the item is 0, the context menu
508 * for the URL should be shown.
509 */
510 void requestContextMenu(const KFileItem& item, const KUrl& url);
511
512 /**
513 * Is emitted if an information message with the content \a msg
514 * should be shown.
515 */
516 void infoMessage(const QString& msg);
517
518 /**
519 * Is emitted if an error message with the content \a msg
520 * should be shown.
521 */
522 void errorMessage(const QString& msg);
523
524 /**
525 * Is emitted if an "operation completed" message with the content \a msg
526 * should be shown.
527 */
528 void operationCompletedMessage(const QString& msg);
529
530 /**
531 * Is emitted after DolphinView::setUrl() has been invoked and
532 * the path \a url is currently loaded. If this signal is emitted,
533 * it is assured that the view contains already the correct root
534 * URL and property settings.
535 */
536 void startedPathLoading(const KUrl& url);
537
538 /**
539 * Is emitted when renaming, copying, moving, linking etc.
540 * Used for feedback in the mainwindow.
541 */
542 void doingOperation(KonqFileUndoManager::CommandType type);
543
544 protected:
545 /** @see QWidget::mouseReleaseEvent */
546 virtual void mouseReleaseEvent(QMouseEvent* event);
547
548 private slots:
549 /**
550 * Marks the view as active (DolphinView:isActive() will return true)
551 * and emits the 'activated' signal if it is not already active.
552 */
553 void activate();
554
555 /**
556 * If the item \a item is a directory, then this
557 * directory will be loaded. If the item is a file, the corresponding
558 * application will get started.
559 */
560 void triggerItem(const KFileItem& index);
561
562 /**
563 * Generates a preview image for each file item in \a items.
564 * The current preview settings (maximum size, 'Show Preview' menu)
565 * are respected.
566 */
567 void generatePreviews(const KFileItemList& items);
568
569 /**
570 * Replaces the icon of the item \a item by the preview pixmap
571 * \a pixmap.
572 */
573 void replaceIcon(const KFileItem& item, const QPixmap& pixmap);
574
575 void emitSelectionChangedSignal();
576
577 /**
578 * Opens the context menu on position \a pos. The position
579 * is used to check whether the context menu is related to an
580 * item or to the viewport.
581 */
582 void openContextMenu(const QPoint& pos);
583
584 /**
585 * Drops the URLs \a urls to the destination path \a destPath. If
586 * the URLs are dropped above an item inside the destination path,
587 * the item is indicated by \a destItem.
588 */
589 void dropUrls(const KUrl::List& urls,
590 const KUrl& destPath,
591 const KFileItem& destItem);
592
593 /**
594 * Handles the dropping of URLs to the given destination.
595 * @see DolphinDropController
596 */
597 void dropUrls(const KUrl::List& urls,
598 const KUrl& destination);
599 /**
600 * Updates the view properties of the current URL to the
601 * sorting given by \a sorting.
602 */
603 void updateSorting(DolphinView::Sorting sorting);
604
605 /**
606 * Updates the view properties of the current URL to the
607 * sort order given by \a order.
608 */
609 void updateSortOrder(Qt::SortOrder order);
610
611 /**
612 * Updates the view properties of the current URL to the
613 * additional information given by \a info.
614 */
615 void updateAdditionalInfo(const KFileItemDelegate::InformationList& info);
616
617 /**
618 * Emits the signal contentsMoved with the current coordinates
619 * of the viewport as parameters.
620 */
621 void emitContentsMoved();
622
623 /** Applies an item effect to all cut items of the clipboard. */
624 void updateCutItems();
625
626 /**
627 * Updates the status bar to show hover information for the
628 * item \a item. If currently other items are selected,
629 * no hover information is shown.
630 * @see DolphinView::clearHoverInformation()
631 */
632 void showHoverInformation(const KFileItem& item);
633
634 /**
635 * Clears the hover information shown in the status bar.
636 * @see DolphinView::showHoverInformation().
637 */
638 void clearHoverInformation();
639
640 /**
641 * Indicates in the status bar that the delete operation
642 * of the job \a job has been finished.
643 */
644 void slotDeleteFileFinished(KJob* job);
645
646 /**
647 * Is invoked when the preview job has been finished and
648 * set m_previewJob to 0.
649 */
650 void slotPreviewJobFinished(KJob* job);
651
652 private:
653 void loadDirectory(const KUrl& url, bool reload = false);
654
655 /**
656 * Returns the URL where the view properties should be stored. Usually
657 * DolphinView::url() is returned, but in the case of a Column View the
658 * view properties are always stored in the directory represented by the
659 * first column. It is recommendend whenever using the ViewProperties class
660 * to use DolphinView::viewPropertiesUrl() as URL.
661 */
662 KUrl viewPropertiesUrl() const;
663
664 /**
665 * Applies the view properties which are defined by the current URL
666 * m_url to the DolphinView properties.
667 */
668 void applyViewProperties(const KUrl& url);
669
670 /**
671 * Creates a new view representing the given view mode (DolphinView::mode()).
672 * The current view will get deleted.
673 */
674 void createView();
675
676 void deleteView();
677
678 /**
679 * Returns a pointer to the currently used item view, which is either
680 * a ListView or a TreeView.
681 */
682 QAbstractItemView* itemView() const;
683
684 /**
685 * Returns true, if the item \a item has been cut into
686 * the clipboard.
687 */
688 bool isCutItem(const KFileItem& item) const;
689
690 /** Applies an item effect to all cut items. */
691 void applyCutItemEffect();
692
693 /**
694 * Returns true, if the ColumnView is activated. As the column view
695 * requires some special handling for iterating through directories,
696 * this method has been introduced for convenience.
697 */
698 bool isColumnViewActive() const
699 {
700 return m_columnView != 0;
701 }
702
703 private:
704 /**
705 * Remembers the original pixmap for an item before
706 * the cut effect is applied.
707 */
708 struct CutItem
709 {
710 KUrl url;
711 QPixmap pixmap;
712 };
713
714 bool m_active;
715 bool m_showPreview;
716 bool m_loadingDirectory;
717 bool m_storedCategorizedSorting;
718 Mode m_mode;
719
720 DolphinMainWindow* m_mainWindow;
721 QVBoxLayout* m_topLayout;
722
723 DolphinController* m_controller;
724 DolphinIconsView* m_iconsView;
725 DolphinDetailsView* m_detailsView;
726 DolphinColumnView* m_columnView;
727 KFileItemDelegate* m_fileItemDelegate;
728 QItemSelectionModel* m_selectionModel;
729
730 DolphinModel* m_dolphinModel;
731 KDirLister* m_dirLister;
732 DolphinSortFilterProxyModel* m_proxyModel;
733
734 KIO::PreviewJob* m_previewJob;
735
736 QList<CutItem> m_cutItemsCache;
737
738 KUrl m_rootUrl;
739 };
740
741 /// Allow using DolphinView::Mode in QVariant
742 Q_DECLARE_METATYPE(DolphinView::Mode)
743
744 #endif // DOLPHINVIEW_H