]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.h
d9a241759ca9e39b914c0264de12fff7128d9cc2
[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 <urlnavigator.h>
31
32 #include <QDropEvent>
33 #include <QLinkedList>
34 #include <QListView>
35 #include <QMouseEvent>
36 #include <QVBoxLayout>
37 #include <QWidget>
38
39 class DolphinController;
40 class FilterBar;
41 class KFileItemDelegate;
42 class KUrl;
43 class KDirModel;
44 class UrlNavigator;
45 class DolphinColumnView;
46 class DolphinDetailsView;
47 class DolphinDirLister;
48 class DolphinIconsView;
49 class DolphinMainWindow;
50 class DolphinSortFilterProxyModel;
51 class DolphinStatusBar;
52 class QModelIndex;
53 class QPainter;
54 class QTimer;
55 class ViewProperties;
56
57 /**
58 * @short Represents a view for the directory content
59 * including the navigation bar, filter bar and status bar.
60 *
61 * View modes for icons, details and columns are supported. Currently
62 * Dolphin allows to have up to two views inside the main window.
63 *
64 * @see DolphinIconsView
65 * @see DolphinDetailsView
66 * @see DolphinColumnView
67 * @see UrlNavigator
68 * @see DolphinStatusBar
69 */
70 class DolphinView : public QWidget
71 {
72 Q_OBJECT
73
74 public:
75 /**
76 * Defines the view mode for a directory. The view mode
77 * can be defined when constructing a DolphinView. The
78 * view mode is automatically updated if the directory itself
79 * defines a view mode (see class ViewProperties for details).
80 */
81 enum Mode
82 {
83 /**
84 * The directory items are shown as icons including an
85 * icon name. */
86 IconsView = 0,
87
88 /**
89 * The icon, the name and at least the size of the directory
90 * items are shown in a table. It is possible to add columns
91 * for date, group and permissions.
92 */
93 DetailsView = 1,
94
95 /**
96 * Each folder is shown in a separate column.
97 */
98 ColumnView = 2,
99 MaxModeEnum = ColumnView
100 };
101
102 /** Defines the sort order for the items of a directory. */
103 enum Sorting
104 {
105 SortByName = 0,
106 SortBySize,
107 SortByDate,
108 SortByPermissions,
109 SortByOwner,
110 SortByGroup,
111 MaxSortEnum = SortByGroup
112 };
113
114 DolphinView(DolphinMainWindow* mainwindow,
115 QWidget *parent,
116 const KUrl& url,
117 Mode mode = IconsView,
118 bool showHiddenFiles = false);
119
120 virtual ~DolphinView();
121
122 /**
123 * Sets the current active URL.
124 * The signals UrlNavigator::urlChanged() and UrlNavigator::historyChanged()
125 * are emitted.
126 */
127 void setUrl(const KUrl& url);
128
129 /** Returns the current active URL. */
130 const KUrl& url() const;
131
132 /**
133 * Returns true if the view is active and hence all actions are
134 * applied to this view.
135 */
136 bool isActive() const;
137
138 /**
139 * Changes the view mode for the current directory to \a mode.
140 * If the view properties should be remembered for each directory
141 * (GeneralSettings::globalViewProps() returns false), then the
142 * changed view mode will be be stored automatically.
143 */
144 void setMode(Mode mode);
145 Mode mode() const;
146
147 /**
148 * Turns on the file preview for the all files of the current directory,
149 * if \a show is true.
150 * If the view properties should be remembered for each directory
151 * (GeneralSettings::globalViewProps() returns false), then the
152 * preview setting will be be stored automatically.
153 */
154 void setShowPreview(bool show);
155 bool showPreview() const;
156
157 /**
158 * Shows all hidden files of the current directory,
159 * if \a show is true.
160 * If the view properties should be remembered for each directory
161 * (GeneralSettings::globalViewProps() returns false), then the
162 * show hidden file setting will be be stored automatically.
163 */
164 void setShowHiddenFiles(bool show);
165 bool showHiddenFiles() const;
166
167 /**
168 * Triggers the renaming of the currently selected items, where
169 * the user must input a new name for the items.
170 */
171 void renameSelectedItems();
172
173 /**
174 * Selects all items.
175 * @see DolphinView::selectedItems()
176 */
177 void selectAll();
178
179 /**
180 * Inverts the current selection: selected items get unselected,
181 * unselected items get selected.
182 * @see DolphinView::selectedItems()
183 */
184 void invertSelection();
185
186 /**
187 * Goes back one step in the URL history. The signals
188 * UrlNavigator::urlChanged() and UrlNavigator::historyChanged()
189 * are submitted.
190 */
191 void goBack();
192
193 /**
194 * Goes forward one step in the Url history. The signals
195 * UrlNavigator::urlChanged() and UrlNavigator::historyChanged()
196 * are submitted.
197 */
198 void goForward();
199
200 /**
201 * Goes up one step of the Url path. The signals
202 * UrlNavigator::urlChanged() and UrlNavigator::historyChanged()
203 * are submitted.
204 */
205 void goUp();
206
207 /**
208 * Goes to the home URL. The signals UrlNavigator::urlChanged()
209 * and UrlNavigator::historyChanged() are submitted.
210 */
211 void goHome();
212
213 /**
214 * Sets the URL of the navigation bar to an editable state
215 * if \a editable is true. If \a editable is false, each part of
216 * the location is presented by a button for a fast navigation.
217 */
218 void setUrlEditable(bool editable);
219
220 /** Returns true, if at least one item is selected. */
221 bool hasSelection() const;
222
223 void clearSelection();
224
225 /**
226 * Returns the selected items. The list is empty if no item has been
227 * selected.
228 * @see DolphinView::selectedUrls()
229 */
230 KFileItemList selectedItems() const;
231
232 /**
233 * Returns a list of URLs for all selected items. An empty list
234 * is returned, if no item is selected.
235 * @see DolphinView::selectedItems()
236 */
237 KUrl::List selectedUrls() const;
238
239 /**
240 * Returns the file item for the given model index \a index.
241 */
242 KFileItem* fileItem(const QModelIndex index) const;
243
244 /**
245 * Renames the filename of the source URL by the new file name.
246 * If the new file name already exists, a dialog is opened which
247 * asks the user to enter a new name.
248 */
249 void rename(const KUrl& source, const QString& newName);
250
251 DolphinStatusBar* statusBar() const;
252
253 /**
254 * Returns the x-position of the view content.
255 * The content of the view might be larger than the visible area
256 * and hence a scrolling must be done.
257 */
258 int contentsX() const;
259
260 /**
261 * Returns the y-position of the view content.
262 * The content of the view might be larger than the visible area
263 * and hence a scrolling must be done.
264 */
265 int contentsY() const;
266
267 /**
268 * Returns true, if the URL shown by the navigation bar is editable.
269 * @see UrlNavigator
270 */
271 bool isUrlEditable() const;
272
273 /** Increases the size of the current set view mode. */
274 void zoomIn();
275
276 /** Decreases the size of the current set view mode. */
277 void zoomOut();
278
279 /**
280 * Returns true, if zooming in is possible. If false is returned,
281 * the minimal zoom size is possible.
282 */
283 bool isZoomInPossible() const;
284
285 /**
286 * Returns true, if zooming out is possible. If false is returned,
287 * the maximum zoom size is possible.
288 */
289 bool isZoomOutPossible() const;
290
291 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
292 void setSorting(Sorting sorting);
293
294 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
295 Sorting sorting() const;
296
297 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
298 void setSortOrder(Qt::SortOrder order);
299
300 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
301 Qt::SortOrder sortOrder() const;
302
303 /** Sets the additional information which should be shown for the items. */
304 void setAdditionalInfo(KFileItemDelegate::AdditionalInformation info);
305
306 /** Returns the additional information which should be shown for the items. */
307 KFileItemDelegate::AdditionalInformation additionalInfo() const;
308
309 /** Returns the UrlNavigator of the view for read access. */
310 const UrlNavigator* urlNavigator() const { return m_urlNavigator; }
311
312 /**
313 * Triggers to request user information for the item given
314 * by the URL \a url. The signal requestItemInfo is emitted,
315 * which provides a way for widgets to get an indication to update
316 * the item information.
317 */
318 void emitRequestItemInfo(const KUrl& url);
319
320 /** Returns true, if the filter bar is visible. */
321 bool isFilterBarVisible() const;
322
323 /**
324 * Return the DolphinMainWindow this View belongs to. It is guranteed
325 * that we have one.
326 */
327 DolphinMainWindow* mainWindow() const ;
328
329 /** Reloads the current directory. */
330 void reload();
331
332 public slots:
333 /**
334 * Popups the filter bar above the status bar if \a show is true.
335 */
336 void showFilterBar(bool show);
337
338 /**
339 * Updates the number of items (= number of files + number of
340 * directories) in the statusbar. If files are selected, the number
341 * of selected files and the sum of the filesize is shown.
342 */
343 void updateStatusBar();
344
345 /**
346 * Requests the main window to set this view as active view, which
347 * means that all actions are applied to this view.
348 */
349 void requestActivation();
350
351 /**
352 * Request of a selection change. The view will do its best to accomodate
353 * the request, but it is not guaranteed that all items in \a selection
354 * will actually get selected. The view will e.g. not select items which
355 * are not in the currently displayed folder.
356 */
357 void changeSelection(const KFileItemList& selection);
358
359 signals:
360 /** Is emitted if URL of the view has been changed to \a url. */
361 void urlChanged(const KUrl& url);
362
363 /**
364 * Is emitted if the view mode (IconsView, DetailsView,
365 * PreviewsView) has been changed.
366 */
367 void modeChanged();
368
369 /** Is emitted if the 'show preview' property has been changed. */
370 void showPreviewChanged();
371
372 /** Is emitted if the 'show hidden files' property has been changed. */
373 void showHiddenFilesChanged();
374
375 /** Is emitted if the sorting by name, size or date has been changed. */
376 void sortingChanged(DolphinView::Sorting sorting);
377
378 /** Is emitted if the sort order (ascending or descending) has been changed. */
379 void sortOrderChanged(Qt::SortOrder order);
380
381 /** Is emitted if the addtional information for an item has been changed. */
382 void additionalInfoChanged(KFileItemDelegate::AdditionalInformation info);
383
384 /**
385 * Is emitted if information of an item is requested to be shown e. g. in the sidebar.
386 * It the URL is empty, no item information request is pending.
387 */
388 void requestItemInfo(const KUrl& url);
389
390 /** Is emitted if the contents has been moved to \a x, \a y. */
391 void contentsMoved(int x, int y);
392
393 /**
394 * Is emitted whenever the selection has been changed.
395 */
396 void selectionChanged(const KFileItemList& selection);
397
398 /**
399 * Is emitted whenever the filter bar has been turned show or hidden.
400 */
401 void showFilterBarChanged(bool shown);
402
403 protected:
404 /** @see QWidget::mouseReleaseEvent */
405 virtual void mouseReleaseEvent(QMouseEvent* event);
406
407 private slots:
408 void loadDirectory(const KUrl& kurl);
409 void triggerItem(const QModelIndex& index);
410 void updateProgress(int percent);
411
412 /**
413 * Updates the number of items (= number of directories + number of files)
414 * and shows this information in the statusbar.
415 */
416 void updateItemCount();
417
418 /**
419 * Generates a preview image for each file item in \a items.
420 * The current preview settings (maximum size, 'Show Preview' menu)
421 * are respected.
422 */
423 void generatePreviews(const KFileItemList& items);
424
425 /**
426 * Replaces the icon of the item \a item by the preview pixmap
427 * \a pixmap.
428 */
429 void showPreview(const KFileItem* item, const QPixmap& pixmap);
430
431 /**
432 * Restores the x- and y-position of the contents if the
433 * current view is part of the history.
434 */
435 void restoreContentsPos();
436
437 /** Shows the information \a msg inside the statusbar. */
438 void showInfoMessage(const QString& msg);
439
440 /** Shows the error message \a msg inside the statusbar. */
441 void showErrorMessage(const QString& msg);
442
443 void emitSelectionChangedSignal();
444 void closeFilterBar();
445
446 /**
447 * Filters the currently shown items by \a nameFilter. All items
448 * which contain the given filter string will be shown.
449 */
450 void changeNameFilter(const QString& nameFilter);
451
452 /**
453 * Opens the context menu on position \a pos. The position
454 * is used to check whether the context menu is related to an
455 * item or to the viewport.
456 */
457 void openContextMenu(const QPoint& pos);
458
459 /**
460 * Drops the URLs \a urls to the index \a index. \a source
461 * indicates the widget where the dragging has been started from.
462 */
463 void dropUrls(const KUrl::List& urls,
464 const QModelIndex& index,
465 QWidget* source);
466
467 /**
468 * Drops the URLs \a urls at the
469 * destination \a destination.
470 */
471 void dropUrls(const KUrl::List& urls,
472 const KUrl& destination);
473 /**
474 * Updates the view properties of the current URL to the
475 * sorting given by \a sorting.
476 */
477 void updateSorting(DolphinView::Sorting sorting);
478
479 /**
480 * Updates the view properties of the current URL to the
481 * sort order given by \a order.
482 */
483 void updateSortOrder(Qt::SortOrder order);
484
485 /**
486 * Emits the signal contentsMoved with the current coordinates
487 * of the viewport as parameters.
488 */
489 void emitContentsMoved();
490
491 /**
492 * Updates the activation state of the view by checking whether
493 * the currently active view is this view.
494 */
495 void updateActivationState();
496
497 /** Applies an item effect to all cut items of the clipboard. */
498 void updateCutItems();
499
500 private:
501 void startDirLister(const KUrl& url, bool reload = false);
502
503 /**
504 * Returns the default text of the status bar, if no item is
505 * selected.
506 */
507 QString defaultStatusBarText() const;
508
509 /**
510 * Returns the text for the status bar, if at least one item
511 * is selected.
512 */
513 QString selectionStatusBarText() const;
514
515 /**
516 * Creates a new view representing the given view mode (DolphinView::mode()).
517 * The current view will get deleted.
518 */
519 void createView();
520
521 /**
522 * Selects all items by using the selection flags \a flags. This is a helper
523 * method for the slots DolphinView::selectAll() and DolphinView::invertSelection().
524 */
525 void selectAll(QItemSelectionModel::SelectionFlags flags);
526
527 /**
528 * Returns a pointer to the currently used item view, which is either
529 * a ListView or a TreeView.
530 */
531 QAbstractItemView* itemView() const;
532
533 /**
534 * Returns true if the index is valid and represents
535 * the column KDirModel::Name.
536 */
537 bool isValidNameIndex(const QModelIndex& index) const;
538
539 /**
540 * Returns true, if the item \a item has been cut into
541 * the clipboard.
542 */
543 bool isCutItem(const KFileItem& item) const;
544
545 /** Applies an item effect to all cut items. */
546 void applyCutItemEffect();
547
548 /**
549 * Returns true, if the ColumnView is activated. As the column view
550 * requires some special handling for iterating through directories,
551 * this method has been introduced for convenience.
552 */
553 bool isColumnViewActive() const { return m_columnView != 0; }
554
555 private:
556 /**
557 * Remembers the original pixmap for an item before
558 * the cut effect is applied.
559 */
560 struct CutItem {
561 KUrl url;
562 QPixmap pixmap;
563 };
564
565 bool m_showProgress;
566 bool m_blockContentsMovedSignal;
567 Mode m_mode;
568
569 int m_iconSize;
570 int m_folderCount;
571 int m_fileCount;
572
573 DolphinMainWindow* m_mainWindow;
574 QVBoxLayout* m_topLayout;
575 UrlNavigator* m_urlNavigator;
576
577 DolphinController* m_controller;
578 DolphinIconsView* m_iconsView;
579 DolphinDetailsView* m_detailsView;
580 DolphinColumnView* m_columnView;
581 KFileItemDelegate* m_fileItemDelegate;
582
583 FilterBar* m_filterBar;
584 DolphinStatusBar* m_statusBar;
585
586 KDirModel* m_dirModel;
587 DolphinDirLister* m_dirLister;
588 DolphinSortFilterProxyModel* m_proxyModel;
589
590 QList<CutItem> m_cutItemsCache;
591 };
592
593 #endif // _DOLPHINVIEW_H_