]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.h
First big step to make it possible that the DolphinView can be embedded as KPart...
[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 <kparts/part.h>
26 #include <kfileitem.h>
27 #include <kfileitemdelegate.h>
28 #include <kio/job.h>
29
30 #include <QBoxLayout>
31 #include <QKeyEvent>
32 #include <QLinkedList>
33 #include <QListView>
34 #include <QWidget>
35
36 class DolphinController;
37 class KDirLister;
38 class KFileItemDelegate;
39 class KUrl;
40 class KDirModel;
41 class DolphinColumnView;
42 class DolphinDetailsView;
43 class DolphinIconsView;
44 class DolphinMainWindow;
45 class DolphinSortFilterProxyModel;
46 class QModelIndex;
47 class ViewProperties;
48
49 /**
50 * @short Represents a view for the directory content.
51 *
52 * View modes for icons, details and columns are supported. It's
53 * possible to adjust:
54 * - sort order
55 * - sort type
56 * - show hidden files
57 * - show previews
58 *
59 * @see DolphinIconsView
60 * @see DolphinDetailsView
61 * @see DolphinColumnView
62 */
63 class DolphinView : public QWidget
64 {
65 Q_OBJECT
66
67 public:
68 /**
69 * Defines the view mode for a directory. The view mode
70 * can be defined when constructing a DolphinView. The
71 * view mode is automatically updated if the directory itself
72 * defines a view mode (see class ViewProperties for details).
73 */
74 enum Mode
75 {
76 /**
77 * The directory items are shown as icons including an
78 * icon name. */
79 IconsView = 0,
80
81 /**
82 * The icon, the name and at least the size of the directory
83 * items are shown in a table. It is possible to add columns
84 * for date, group and permissions.
85 */
86 DetailsView = 1,
87
88 /**
89 * Each folder is shown in a separate column.
90 */
91 ColumnView = 2,
92 MaxModeEnum = ColumnView
93 };
94
95 /** Defines the sort order for the items of a directory. */
96 enum Sorting
97 {
98 SortByName = 0,
99 SortBySize,
100 SortByDate,
101 SortByPermissions,
102 SortByOwner,
103 SortByGroup,
104 SortByType,
105 MaxSortEnum = SortByType
106 };
107
108 /**
109 * @param parent Parent widget of the view.
110 * @param url Specifies the content which should be shown.
111 * @param dirLister Used directory lister. The lister is not owned
112 * by the view and won't get deleted.
113 * @param dirModel Used directory model. The model is not owned
114 * by the view and won't get deleted.
115 * @param proxyModel Used proxy model which specifies the sorting. The
116 * model is not owned by the view and won't get
117 * deleted.
118 * @param mode Used display mode (IconsView, DetailsView or ColumnsView).
119 * @param showHiddenFiles If true, hidden files will be shown in the view.
120 */
121 DolphinView(QWidget* parent,
122 const KUrl& url,
123 KDirLister* dirLister,
124 KDirModel* dirModel,
125 DolphinSortFilterProxyModel* proxyModel,
126 Mode mode = IconsView,
127 bool showHiddenFiles = false);
128
129 virtual ~DolphinView();
130
131 /**
132 * Returns the current active URL, where all actions are applied.
133 * The URL navigator is synchronized with this URL.
134 */
135 const KUrl& url() const;
136
137 /**
138 * Returns the root URL of the view, which is defined as the first
139 * visible path of DolphinView::url(). Usually the root URL is
140 * equal to DolphinView::url(), but in the case of the column view
141 * when 2 columns are shown, the root URL might be:
142 * /home/peter/Documents
143 * and DolphinView::url() might return
144 * /home/peter/Documents/Music/
145 */
146 KUrl rootUrl() const;
147
148 /**
149 * If \a active is true, the view will marked as active. The active
150 * view is defined as view where all actions are applied to.
151 */
152 void setActive(bool active);
153 bool isActive() const;
154
155 /**
156 * Changes the view mode for the current directory to \a mode.
157 * If the view properties should be remembered for each directory
158 * (GeneralSettings::globalViewProps() returns false), then the
159 * changed view mode will be be stored automatically.
160 */
161 void setMode(Mode mode);
162 Mode mode() const;
163
164 /**
165 * Turns on the file preview for the all files of the current directory,
166 * if \a show is true.
167 * If the view properties should be remembered for each directory
168 * (GeneralSettings::globalViewProps() returns false), then the
169 * preview setting will be be stored automatically.
170 */
171 void setShowPreview(bool show);
172 bool showPreview() const;
173
174 /**
175 * Shows all hidden 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 * show hidden file setting will be be stored automatically.
180 */
181 void setShowHiddenFiles(bool show);
182 bool showHiddenFiles() const;
183
184 /**
185 * Summarizes all sorted items by their category \a categorized
186 * is true.
187 * If the view properties should be remembered for each directory
188 * (GeneralSettings::globalViewProps() returns false), then the
189 * categorized sorting setting will be be stored automatically.
190 */
191 void setCategorizedSorting(bool categorized);
192 bool categorizedSorting() const;
193
194 /**
195 * Returns true, if the categorized sorting is supported by the current
196 * used mode (see DolphinView::setMode()). Currently only DolphinView::IconsView
197 * supports categorizations. To check whether the categorized
198 * sorting is set, use DolphinView::categorizedSorting().
199 */
200 bool supportsCategorizedSorting() const;
201
202 /**
203 * Triggers the renaming of the currently selected items, where
204 * the user must input a new name for the items.
205 */
206 void renameSelectedItems();
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 * Renames the filename of the source URL by the new file name.
247 * If the new file name already exists, a dialog is opened which
248 * asks the user to enter a new name.
249 */
250 void rename(const KUrl& source, const QString& newName);
251
252 /**
253 * Returns the x-position of the view content.
254 * The content of the view might be larger than the visible area
255 * and hence a scrolling must be done.
256 */
257 int contentsX() const;
258
259 /**
260 * Returns the y-position of the view content.
261 * The content of the view might be larger than the visible area
262 * and hence a scrolling must be done.
263 */
264 int contentsY() const;
265
266 /** Increases the size of the current set view mode. */
267 void zoomIn();
268
269 /** Decreases the size of the current set view mode. */
270 void zoomOut();
271
272 /**
273 * Returns true, if zooming in is possible. If false is returned,
274 * the minimal zoom size is possible.
275 */
276 bool isZoomInPossible() const;
277
278 /**
279 * Returns true, if zooming out is possible. If false is returned,
280 * the maximum zoom size is possible.
281 */
282 bool isZoomOutPossible() const;
283
284 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
285 void setSorting(Sorting sorting);
286
287 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
288 Sorting sorting() const;
289
290 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
291 void setSortOrder(Qt::SortOrder order);
292
293 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
294 Qt::SortOrder sortOrder() const;
295
296 /** Sets the additional information which should be shown for the items. */
297 void setAdditionalInfo(KFileItemDelegate::AdditionalInformation info);
298
299 /** Returns the additional information which should be shown for the items. */
300 KFileItemDelegate::AdditionalInformation additionalInfo() const;
301
302 /** Reloads the current directory. */
303 void reload();
304
305 /**
306 * Refreshs the view to get synchronized with the (updated) Dolphin settings.
307 * This method only needs to get invoked if the view settings for the Icons View,
308 * Details View or Columns View have been changed.
309 */
310 void refresh();
311
312 public slots:
313 /**
314 * Changes the directory to \a url. If the current directory is equal to
315 * \a url, nothing will be done (use DolphinView::reload() instead).
316 */
317 void setUrl(const KUrl& url);
318
319 /**
320 * Request of a selection change. The view will do its best to accommodate
321 * the request, but it is not guaranteed that all items in \a selection
322 * will actually get selected. The view will e.g. not select items which
323 * are not in the currently displayed folder.
324 */
325 void changeSelection(const KFileItemList& selection);
326
327 signals:
328 /**
329 * Is emitted if the view has been activated by e. g. a mouse click.
330 */
331 void activated();
332
333 /** Is emitted if URL of the view has been changed to \a url. */
334 void urlChanged(const KUrl& url);
335
336 /**
337 * Is emitted if the view mode (IconsView, DetailsView,
338 * PreviewsView) has been changed.
339 */
340 void modeChanged();
341
342 /** Is emitted if the 'show preview' property has been changed. */
343 void showPreviewChanged();
344
345 /** Is emitted if the 'show hidden files' property has been changed. */
346 void showHiddenFilesChanged();
347
348 /** Is emitted if the 'categorized sorting' property has been changed. */
349 void categorizedSortingChanged();
350
351 /** Is emitted if the sorting by name, size or date has been changed. */
352 void sortingChanged(DolphinView::Sorting sorting);
353
354 /** Is emitted if the sort order (ascending or descending) has been changed. */
355 void sortOrderChanged(Qt::SortOrder order);
356
357 /** Is emitted if the addtional information for an item has been changed. */
358 void additionalInfoChanged(KFileItemDelegate::AdditionalInformation info);
359
360 /**
361 * Is emitted if information of an item is requested to be shown e. g. in the sidebar.
362 * It the URL is empty, no item information request is pending.
363 */
364 void requestItemInfo(const KUrl& url);
365
366 /** Is emitted if the contents has been moved to \a x, \a y. */
367 void contentsMoved(int x, int y);
368
369 /**
370 * Is emitted whenever the selection has been changed.
371 */
372 void selectionChanged(const KFileItemList& selection);
373
374 /**
375 * Is emitted whenever the filter bar has been turned show or hidden.
376 */
377 void showFilterBarChanged(bool shown);
378
379 /**
380 * Is emitted if a context menu is requested for the item \a item,
381 * which is part of \a url. If the item is 0, the context menu
382 * for the URL should be shown.
383 */
384 void requestContextMenu(KFileItem* item, const KUrl& url);
385
386 /**
387 * Is emitted if the URLs \a are dropped to the destination URL
388 * \a destination. No operation is done within the DolphinView, the
389 * receiver of the signal has to take care about the corresponding
390 * operation.
391 */
392 void urlsDropped(const KUrl::List& urls, const KUrl& destination);
393
394 protected:
395 /** @see QWidget::mouseReleaseEvent */
396 virtual void mouseReleaseEvent(QMouseEvent* event);
397
398 private slots:
399 /**
400 * Marks the view as active (DolphinView:isActive() will return true)
401 * and emits the 'activated' signal if it is not already active.
402 */
403 void activate();
404
405 /**
406 * If the item specified by \a index is a directory, then this
407 * directory will be loaded. If the item is a file, the corresponding
408 * application will get started.
409 */
410 void triggerItem(const QModelIndex& index);
411
412 /**
413 * Generates a preview image for each file item in \a items.
414 * The current preview settings (maximum size, 'Show Preview' menu)
415 * are respected.
416 */
417 void generatePreviews(const KFileItemList& items);
418
419 /**
420 * Replaces the icon of the item \a item by the preview pixmap
421 * \a pixmap.
422 */
423 void showPreview(const KFileItem& item, const QPixmap& pixmap);
424
425 /**
426 * Restores the x- and y-position of the contents if the
427 * current view is part of the history.
428 */
429 void restoreContentsPos();
430
431 void emitSelectionChangedSignal();
432
433 /**
434 * Opens the context menu on position \a pos. The position
435 * is used to check whether the context menu is related to an
436 * item or to the viewport.
437 */
438 void openContextMenu(const QPoint& pos);
439
440 /**
441 * Drops the URLs \a urls to the index \a index. \a source
442 * indicates the widget where the dragging has been started from.
443 */
444 void dropUrls(const KUrl::List& urls,
445 const QModelIndex& index,
446 QWidget* source);
447
448 /**
449 * Drops the URLs \a urls at the
450 * destination \a destination.
451 */
452 void dropUrls(const KUrl::List& urls,
453 const KUrl& destination);
454 /**
455 * Updates the view properties of the current URL to the
456 * sorting given by \a sorting.
457 */
458 void updateSorting(DolphinView::Sorting sorting);
459
460 /**
461 * Updates the view properties of the current URL to the
462 * sort order given by \a order.
463 */
464 void updateSortOrder(Qt::SortOrder order);
465
466 /**
467 * Emits the signal contentsMoved with the current coordinates
468 * of the viewport as parameters.
469 */
470 void emitContentsMoved();
471
472 /** Applies an item effect to all cut items of the clipboard. */
473 void updateCutItems();
474
475 /**
476 * Updates the status bar to show hover information for the
477 * item with the index \a index. If currently other items are selected,
478 * no hover information is shown.
479 * @see DolphinView::clearHoverInformation()
480 */
481 void showHoverInformation(const QModelIndex& index);
482
483 /**
484 * Clears the hover information shown in the status bar.
485 * @see DolphinView::showHoverInformation().
486 */
487 void clearHoverInformation();
488
489 private:
490 void startDirLister(const KUrl& url, bool reload = false);
491
492 /**
493 * Creates a new view representing the given view mode (DolphinView::mode()).
494 * The current view will get deleted.
495 */
496 void createView();
497
498 /**
499 * Selects all items by using the selection flags \a flags. This is a helper
500 * method for the slots DolphinView::selectAll() and DolphinView::invertSelection().
501 */
502 void selectAll(QItemSelectionModel::SelectionFlags flags);
503
504 /**
505 * Returns a pointer to the currently used item view, which is either
506 * a ListView or a TreeView.
507 */
508 QAbstractItemView* itemView() const;
509
510 /**
511 * Returns true if the index is valid and represents
512 * the column KDirModel::Name.
513 */
514 bool isValidNameIndex(const QModelIndex& index) const;
515
516 /**
517 * Returns true, if the item \a item has been cut into
518 * the clipboard.
519 */
520 bool isCutItem(const KFileItem& item) const;
521
522 /** Applies an item effect to all cut items. */
523 void applyCutItemEffect();
524
525 /**
526 * Returns true, if the ColumnView is activated. As the column view
527 * requires some special handling for iterating through directories,
528 * this method has been introduced for convenience.
529 */
530 bool isColumnViewActive() const
531 {
532 return m_columnView != 0;
533 }
534
535 private:
536 /**
537 * Remembers the original pixmap for an item before
538 * the cut effect is applied.
539 */
540 struct CutItem
541 {
542 KUrl url;
543 QPixmap pixmap;
544 };
545
546 bool m_active;
547 bool m_blockContentsMovedSignal;
548 bool m_initializeColumnView;
549 Mode m_mode;
550
551 DolphinMainWindow* m_mainWindow;
552 QVBoxLayout* m_topLayout;
553
554 DolphinController* m_controller;
555 DolphinIconsView* m_iconsView;
556 DolphinDetailsView* m_detailsView;
557 DolphinColumnView* m_columnView;
558 KFileItemDelegate* m_fileItemDelegate;
559
560 KDirModel* m_dirModel;
561 KDirLister* m_dirLister;
562 DolphinSortFilterProxyModel* m_proxyModel;
563
564 QList<CutItem> m_cutItemsCache;
565 };
566
567 #endif // DOLPHINVIEW_H