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