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