demos/browser/bookmarks.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the demonstration applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef BOOKMARKS_H
       
    43 #define BOOKMARKS_H
       
    44 
       
    45 #include <QtCore/QObject>
       
    46 #include <QtCore/QAbstractItemModel>
       
    47 
       
    48 #include <QtGui/QUndoCommand>
       
    49 
       
    50 /*!
       
    51     Bookmark manager, owner of the bookmarks, loads, saves and basic tasks
       
    52   */
       
    53 class AutoSaver;
       
    54 class BookmarkNode;
       
    55 class BookmarksModel;
       
    56 class BookmarksManager : public QObject
       
    57 {
       
    58     Q_OBJECT
       
    59 
       
    60 signals:
       
    61     void entryAdded(BookmarkNode *item);
       
    62     void entryRemoved(BookmarkNode *parent, int row, BookmarkNode *item);
       
    63     void entryChanged(BookmarkNode *item);
       
    64 
       
    65 public:
       
    66     BookmarksManager(QObject *parent = 0);
       
    67     ~BookmarksManager();
       
    68 
       
    69     void addBookmark(BookmarkNode *parent, BookmarkNode *node, int row = -1);
       
    70     void removeBookmark(BookmarkNode *node);
       
    71     void setTitle(BookmarkNode *node, const QString &newTitle);
       
    72     void setUrl(BookmarkNode *node, const QString &newUrl);
       
    73     void changeExpanded();
       
    74 
       
    75     BookmarkNode *bookmarks();
       
    76     BookmarkNode *menu();
       
    77     BookmarkNode *toolbar();
       
    78 
       
    79     BookmarksModel *bookmarksModel();
       
    80     QUndoStack *undoRedoStack() { return &m_commands; };
       
    81 
       
    82 public slots:
       
    83     void importBookmarks();
       
    84     void exportBookmarks();
       
    85 
       
    86 private slots:
       
    87     void save() const;
       
    88 
       
    89 private:
       
    90     void load();
       
    91 
       
    92     bool m_loaded;
       
    93     AutoSaver *m_saveTimer;
       
    94     BookmarkNode *m_bookmarkRootNode;
       
    95     BookmarksModel *m_bookmarkModel;
       
    96     QUndoStack m_commands;
       
    97 
       
    98     friend class RemoveBookmarksCommand;
       
    99     friend class ChangeBookmarkCommand;
       
   100 };
       
   101 
       
   102 class RemoveBookmarksCommand : public QUndoCommand
       
   103 {
       
   104 
       
   105 public:
       
   106     RemoveBookmarksCommand(BookmarksManager *m_bookmarkManagaer, BookmarkNode *parent, int row);
       
   107     ~RemoveBookmarksCommand();
       
   108     void undo();
       
   109     void redo();
       
   110 
       
   111 protected:
       
   112     int m_row;
       
   113     BookmarksManager *m_bookmarkManagaer;
       
   114     BookmarkNode *m_node;
       
   115     BookmarkNode *m_parent;
       
   116     bool m_done;
       
   117 };
       
   118 
       
   119 class InsertBookmarksCommand : public RemoveBookmarksCommand
       
   120 {
       
   121 
       
   122 public:
       
   123     InsertBookmarksCommand(BookmarksManager *m_bookmarkManagaer,
       
   124         BookmarkNode *parent, BookmarkNode *node, int row);
       
   125     void undo() { RemoveBookmarksCommand::redo(); }
       
   126     void redo() { RemoveBookmarksCommand::undo(); }
       
   127 
       
   128 };
       
   129 
       
   130 class ChangeBookmarkCommand : public QUndoCommand
       
   131 {
       
   132 
       
   133 public:
       
   134     ChangeBookmarkCommand(BookmarksManager *m_bookmarkManagaer,
       
   135         BookmarkNode *node, const QString &newValue, bool title);
       
   136     void undo();
       
   137     void redo();
       
   138 
       
   139 private:
       
   140     BookmarksManager *m_bookmarkManagaer;
       
   141     bool m_title;
       
   142     QString m_oldValue;
       
   143     QString m_newValue;
       
   144     BookmarkNode *m_node;
       
   145 };
       
   146 
       
   147 /*!
       
   148     BookmarksModel is a QAbstractItemModel wrapper around the BookmarkManager
       
   149   */
       
   150 #include <QtGui/QIcon>
       
   151 class BookmarksModel : public QAbstractItemModel
       
   152 {
       
   153     Q_OBJECT
       
   154 
       
   155 public slots:
       
   156     void entryAdded(BookmarkNode *item);
       
   157     void entryRemoved(BookmarkNode *parent, int row, BookmarkNode *item);
       
   158     void entryChanged(BookmarkNode *item);
       
   159 
       
   160 public:
       
   161     enum Roles {
       
   162         TypeRole = Qt::UserRole + 1,
       
   163         UrlRole = Qt::UserRole + 2,
       
   164         UrlStringRole = Qt::UserRole + 3,
       
   165         SeparatorRole = Qt::UserRole + 4
       
   166     };
       
   167 
       
   168     BookmarksModel(BookmarksManager *bookmarkManager, QObject *parent = 0);
       
   169     inline BookmarksManager *bookmarksManager() const { return m_bookmarksManager; }
       
   170 
       
   171     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
       
   172     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
       
   173     int columnCount(const QModelIndex &parent = QModelIndex()) const;
       
   174     int rowCount(const QModelIndex &parent = QModelIndex()) const;
       
   175     QModelIndex index(int, int, const QModelIndex& = QModelIndex()) const;
       
   176     QModelIndex parent(const QModelIndex& index= QModelIndex()) const;
       
   177     Qt::ItemFlags flags(const QModelIndex &index) const;
       
   178     Qt::DropActions supportedDropActions () const;
       
   179     bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
       
   180     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
       
   181     QMimeData *mimeData(const QModelIndexList &indexes) const;
       
   182     QStringList mimeTypes() const;
       
   183     bool dropMimeData(const QMimeData *data,
       
   184         Qt::DropAction action, int row, int column, const QModelIndex &parent);
       
   185     bool hasChildren(const QModelIndex &parent = QModelIndex()) const;
       
   186 
       
   187     BookmarkNode *node(const QModelIndex &index) const;
       
   188     QModelIndex index(BookmarkNode *node) const;
       
   189 
       
   190 private:
       
   191 
       
   192     bool m_endMacro;
       
   193     BookmarksManager *m_bookmarksManager;
       
   194 };
       
   195 
       
   196 // Menu that is dynamically populated from the bookmarks
       
   197 #include "modelmenu.h"
       
   198 class BookmarksMenu : public ModelMenu
       
   199 {
       
   200     Q_OBJECT
       
   201 
       
   202 signals:
       
   203     void openUrl(const QUrl &url);
       
   204 
       
   205 public:
       
   206      BookmarksMenu(QWidget *parent = 0);
       
   207      void setInitialActions(QList<QAction*> actions);
       
   208 
       
   209 protected:
       
   210     bool prePopulated();
       
   211 
       
   212 private slots:
       
   213     void activated(const QModelIndex &index);
       
   214 
       
   215 private:
       
   216     BookmarksManager *m_bookmarksManager;
       
   217     QList<QAction*> m_initialActions;
       
   218 };
       
   219 
       
   220 /*
       
   221     Proxy model that filters out the bookmarks so only the folders
       
   222     are left behind.  Used in the add bookmark dialog combobox.
       
   223  */
       
   224 #include <QtGui/QSortFilterProxyModel>
       
   225 class AddBookmarkProxyModel : public QSortFilterProxyModel
       
   226 {
       
   227     Q_OBJECT
       
   228 public:
       
   229     AddBookmarkProxyModel(QObject * parent = 0);
       
   230     int columnCount(const QModelIndex & parent = QModelIndex()) const;
       
   231 
       
   232 protected:
       
   233     bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
       
   234 };
       
   235 
       
   236 /*!
       
   237     Add bookmark dialog
       
   238  */
       
   239 #include "ui_addbookmarkdialog.h"
       
   240 class AddBookmarkDialog : public QDialog, public Ui_AddBookmarkDialog
       
   241 {
       
   242     Q_OBJECT
       
   243 
       
   244 public:
       
   245     AddBookmarkDialog(const QString &url, const QString &title, QWidget *parent = 0, BookmarksManager *bookmarkManager = 0);
       
   246 
       
   247 private slots:
       
   248     void accept();
       
   249 
       
   250 private:
       
   251     QString m_url;
       
   252     BookmarksManager *m_bookmarksManager;
       
   253     AddBookmarkProxyModel *m_proxyModel;
       
   254 };
       
   255 
       
   256 #include "ui_bookmarks.h"
       
   257 class TreeProxyModel;
       
   258 class BookmarksDialog : public QDialog, public Ui_BookmarksDialog
       
   259 {
       
   260     Q_OBJECT
       
   261 
       
   262 signals:
       
   263     void openUrl(const QUrl &url);
       
   264 
       
   265 public:
       
   266     BookmarksDialog(QWidget *parent = 0, BookmarksManager *manager = 0);
       
   267     ~BookmarksDialog();
       
   268 
       
   269 private slots:
       
   270     void customContextMenuRequested(const QPoint &pos);
       
   271     void open();
       
   272     void newFolder();
       
   273 
       
   274 private:
       
   275     void expandNodes(BookmarkNode *node);
       
   276     bool saveExpandedNodes(const QModelIndex &parent);
       
   277 
       
   278     BookmarksManager *m_bookmarksManager;
       
   279     BookmarksModel *m_bookmarksModel;
       
   280     TreeProxyModel *m_proxyModel;
       
   281 };
       
   282 
       
   283 #include <QtGui/QToolBar>
       
   284 class BookmarksToolBar : public QToolBar
       
   285 {
       
   286     Q_OBJECT
       
   287 
       
   288 signals:
       
   289     void openUrl(const QUrl &url);
       
   290 
       
   291 public:
       
   292     BookmarksToolBar(BookmarksModel *model, QWidget *parent = 0);
       
   293     void setRootIndex(const QModelIndex &index);
       
   294     QModelIndex rootIndex() const;
       
   295 
       
   296 protected:
       
   297     void dragEnterEvent(QDragEnterEvent *event);
       
   298     void dropEvent(QDropEvent *event);
       
   299 
       
   300 private slots:
       
   301     void triggered(QAction *action);
       
   302     void activated(const QModelIndex &index);
       
   303     void build();
       
   304 
       
   305 private:
       
   306     BookmarksModel *m_bookmarksModel;
       
   307     QPersistentModelIndex m_root;
       
   308 };
       
   309 
       
   310 #endif // BOOKMARKS_H