summaryrefslogtreecommitdiff
path: root/src/frontend/mainindex/bookmarks/cbookmarkindex.h
blob: 90f9ab2650007adf873bbb09dfce4674e11866db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#ifndef CBOOKMARKINDEX_H
#define CBOOKMARKINDEX_H

#include "frontend/mainindex/bookmarks/btbookmarkitembase.h"

#include <QList>
#include <QTimer>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QToolTip>
#include "frontend/displaywindow/cdisplaywindow.h"
#include "util/cpointers.h"


class BTMimeData;
class CSearchDialog;
class CSwordModuleInfo;
class QAction;
class QDragLeaveEvent;
class QDragMoveEvent;
class QDropEvent;
class QMenu;
class QMouseEvent;
class QPaintEvent;
class QWidget;

/**
* The widget which manages all bookmarks.
* @author The BibleTime team
*/
class CBookmarkIndex : public QTreeWidget {
        Q_OBJECT
    public:
        CBookmarkIndex(QWidget *parent);
        virtual ~CBookmarkIndex();

        void initTree();

        /**
        * Saves the bookmarks to disk
        */
        void saveBookmarks();

    signals:
        /**
        * Is emitted when a module should be opened,
        */
        void createReadDisplayWindow( QList<CSwordModuleInfo*>, const QString& );

    public slots:

        /**
         * Indicates a need to save the bookmarks.
         * This is needed to provide a way for a bookmarkitem stored in the
         * treeWidget to inform us that it has been modified, namely its
         * description text.  It only sets a dirty-bit so we don't execute many
         * consecutive saves.
         */
        void needToSaveBookmarks();
        void needToSaveBookmarks(QTreeWidgetItem* treeItem);


    protected: // Protected methods

        /** A hack to get the modifiers. */
        virtual void mouseReleaseEvent(QMouseEvent* event);

        /** Needed to paint an drag pointer arrow. */
        virtual void paintEvent(QPaintEvent* event);

        /** Initialize the SIGNAL<->SLOT connections. */
        void initConnections();

        /** Returns the drag object for the current selection. */
        virtual QMimeData* dragObject();

        /**
        * D'n'd methods are reimplementations from QTreeWidget or its ancestors.
        * In these we handle creating, moving and copying bookmarks with d'n'd.
        */
        virtual void dragEnterEvent( QDragEnterEvent* event );
        virtual void dragMoveEvent( QDragMoveEvent* event );
        virtual void dropEvent( QDropEvent* event );
        virtual void dragLeaveEvent( QDragLeaveEvent* event );

        /** Returns the correct action object for the given type of action. */
        QAction* action( BtBookmarkItemBase::MenuAction type ) const;

        /** Reimplementation from QAbstractItemView. Takes care of movable items. */
        virtual void startDrag(Qt::DropActions supportedActions);


        /** Handle mouse moving (mag updates) */
        virtual void mouseMoveEvent(QMouseEvent* event);


    protected slots:

        /** Prevents annoying folder collapsing while dropping. */
        void expandAutoCollapsedItem(QTreeWidgetItem* i) {
            expandItem(i);
        }

        /** Is called when an item was clicked or activated. */
        void slotExecuted( QTreeWidgetItem* );

        /** Shows the context menu at the given position. */
        void contextMenu(const QPoint&);

        /** Adds a new subfolder to the current item. */
        void createNewFolder();

        /** Opens a dialog to change the current folder. */
        void changeFolder();

        /** Exports the bookmarks from the selected folder. */
        void exportBookmarks();

        /** Changes the current bookmark. */
        void changeBookmark();

        /** Helps with the extra item. */
        void slotItemEntered(QTreeWidgetItem*, int);

        /** Import bookmarks from a file and add them to the selected folder. */
        void importBookmarks();

        /** Deletes the selected entries. */
        void deleteEntries(bool confirm = true);

        /** Prints the selected bookmarks. */
        void printBookmarks();

        /** Slot for the mag update timer. */
        void magTimeout();

    private:

        /** Initializes the view. */
        void initView();

        /** Convenience function for creating a new action. */
        QAction* newQAction(const QString& text, const QString& pix, int shortcut, const QObject* receiver, const char* slot, QObject* parent);

        /**
        * Returns true if more than one entry is supported by this action type.
        * Returns false for actions which support only one entry.
        */
        bool isMultiAction( const BtBookmarkItemBase::MenuAction type ) const;

        /** A helper function for d'n'd which creates a new bookmark item when drop happens. */
        void createBookmarkFromDrop(QDropEvent* event, QTreeWidgetItem* parentItem, int indexInParent);

        /**
        * Returns a list of new items created from the selection.
        * Sets flags which indicate whether the selection was legal for dropping.
        */
        QList<QTreeWidgetItem*> addItemsToDropTree(QTreeWidgetItem* target, bool& bookmarksOnly, bool& targetIncluded, bool& moreThanOneFolder);

        struct Actions {
            QAction* newFolder;
            QAction* changeFolder;

            QAction* changeBookmark;
            QAction* importBookmarks;
            QAction* exportBookmarks;
            QAction* printBookmarks;

            QAction* deleteEntries;
        }
        m_actions;

        QMenu* m_popup;
        QTimer m_magTimer;
        int m_mouseReleaseEventModifiers;
        QTreeWidgetItem* m_previousEventItem;
        QPoint m_dragMovementPosition;
        QPoint m_dragStartPosition;
        QTreeWidgetItem* m_extraItem;

        // The following is for managing saving bookmarks.  It uses a QTimer to
        // determine whether the bookmarks should be saved.  This may seem like
        // a hassle, but it is to prevent many saves from being executed at a
        // time.

        /** Flag indicating that bookmarks have been modified. */
        bool m_bookmarksModified;
        QTimer bookmarkSaveTimer;

    private slots:
        /**
         * Saves the bookmarks.
         * It checks m_bookmarksModified and resets it at the end. It should be
         * connected to a timer that periodically calls this. */
        void considerSavingBookmarks();
};

#endif