forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavoritespanewidget.hh
249 lines (187 loc) · 6.89 KB
/
favoritespanewidget.hh
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* This file is (c) 2017 Abs62
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#ifndef __FAVORITIESPANEWIDGET_HH__INCLUDED__
#define __FAVORITIESPANEWIDGET_HH__INCLUDED__
#include <QWidget>
#include <QSize>
#include <QAbstractItemModel>
#include <QTreeView>
#include <QLabel>
#include <QHBoxLayout>
#include <QMenu>
#include <QDomNode>
#include <QList>
#include <config.hh>
#include "delegate.hh"
class FavoritesModel;
class FavoritesPaneWidget : public QWidget
{
Q_OBJECT
public:
FavoritesPaneWidget( QWidget * parent = 0 ): QWidget( parent ),
itemSelectionChanged( false )
, listItemDelegate( 0 )
, m_favoritesModel( 0 )
, timerId( 0 )
{}
virtual ~FavoritesPaneWidget();
virtual QSize sizeHint() const
{ return QSize( 204, 204 ); }
void setUp( Config::Class * cfg, QMenu * menu );
void addHeadword( QString const & path, QString const & headword );
// Export/import Favorites
void getDataInXml( QByteArray & dataStr );
void getDataInPlainText( QString & dataStr );
bool setDataFromXml( QString const & dataStr );
void setFocusOnTree()
{ m_favoritesTree->setFocus(); }
void setSaveInterval( unsigned interval );
signals:
void favoritesItemRequested( QString const & word, QString const & faforitesFolder );
protected:
virtual void timerEvent( QTimerEvent * ev );
private slots:
void emitFavoritesItemRequested(QModelIndex const &);
void onSelectionChanged(QItemSelection const & selection);
void onItemClicked(QModelIndex const & idx);
void showCustomMenu(QPoint const & pos);
void deleteSelectedItems();
void copySelectedItems();
void addFolder();
private:
virtual bool eventFilter( QObject *, QEvent * );
Config::Class * m_cfg ;
QTreeView * m_favoritesTree;
QMenu * m_favoritesMenu;
QAction * m_deleteSelectedAction;
QAction * m_separator;
QAction * m_copySelectedToClipboard;
QAction * m_addFolder;
QWidget favoritesPaneTitleBar;
QHBoxLayout favoritesPaneTitleBarLayout;
QLabel favoritesLabel;
/// needed to avoid multiple notifications
/// when selecting items via mouse and keyboard
bool itemSelectionChanged;
WordListItemDelegate * listItemDelegate;
FavoritesModel * m_favoritesModel;
int timerId;
};
class TreeItem
{
public:
enum Type { Word, Folder, Root };
TreeItem( const QVariant &data, TreeItem *parent = 0, Type type_ = Word );
~TreeItem();
void appendChild( TreeItem * child );
void insertChild( int row, TreeItem * item );
// Remove child from list and delete it
void deleteChild( int row );
TreeItem * child( int row ) const;
int childCount() const;
QVariant data() const;
void setData( const QVariant & newData );
int row() const;
TreeItem * parent();
Type type() const
{ return m_type; }
Qt::ItemFlags flags() const;
void setExpanded( bool expanded )
{ m_expanded = expanded; }
bool isExpanded() const
{ return m_expanded; }
// Full path from root folder
QString fullPath() const;
// Duplicate item with all childs
TreeItem * duplicateItem( TreeItem * newParent ) const;
// Check if item is ancestor of this element
bool haveAncestor( TreeItem * item );
// Check if same item already presented between childs
bool haveSameItem( TreeItem * item, bool allowSelf );
// Retrieve text from all childs
QStringList getTextFromAllChilds() const;
private:
QList< TreeItem * > childItems;
QVariant itemData;
TreeItem *parentItem;
Type m_type;
bool m_expanded;
};
class FavoritesModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit FavoritesModel( QString favoritesFilename, QObject * parent = 0 );
~FavoritesModel();
QVariant data( const QModelIndex &index, int role ) const;
Qt::ItemFlags flags( const QModelIndex &index ) const;
QVariant headerData( int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const;
QModelIndex index( int row, int column,
const QModelIndex &parent = QModelIndex() ) const;
QModelIndex parent( const QModelIndex &index ) const;
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
bool removeRows( int row, int count, const QModelIndex &parent );
bool setData( const QModelIndex &index, const QVariant &value, int role );
// Drag & drop support
Qt::DropActions supportedDropActions() const;
QStringList mimeTypes() const;
QMimeData *mimeData(const QModelIndexList &indexes) const;
bool dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &par);
// Restore nodes expanded state after data loading
void checkNodeForExpand( const TreeItem * item, const QModelIndex &parent );
void checkAllNodesForExpand();
// Retrieve text data for indexes
QStringList getTextForIndexes( QModelIndexList const & idxList ) const;
// Delete items for indexes
void removeItemsForIndexes( QModelIndexList const & idxList );
// Add new folder beside item and return its index
// or empty index if fail
QModelIndex addNewFolder( QModelIndex const & idx );
// Add new headword to given folder
// return false if it already exists there
bool addNewHeadword( QString const & path, QString const & headword );
// Return path in the tree to item
QString pathToItem( QModelIndex const & idx );
TreeItem::Type itemType( QModelIndex const & idx )
{ return getItem( idx )->type(); }
// Export/import Favorites
void getDataInXml( QByteArray & dataStr );
void getDataInPlainText( QString & dataStr );
bool setDataFromXml( QString const & dataStr );
void saveData();
public slots:
void itemCollapsed ( const QModelIndex & index );
void itemExpanded ( const QModelIndex & index );
signals:
void expandItem( const QModelIndex & index );
protected:
void readData();
void addFolder( TreeItem * parent, QDomNode & node );
void storeFolder( TreeItem * folder, QDomNode & node );
// Find item in folder
QModelIndex findItemInFolder( QString const & itemName, int itemType,
QModelIndex const & parentIdx );
// Find item by it params
QModelIndex findItem( QString const & path,
QString const & headword,
int type );
// Create items list from mime "plait/text" data
QList< QModelIndex > itemsListFromText( QString const & text );
TreeItem *getItem( const QModelIndex &index ) const;
// Find folder with given name or create it if folder not exist
QModelIndex forceFolder( QString const & name, QModelIndex const & parentIdx );
// Add headword to given folder
// return false if such headwordalready exists
bool addHeadword( QString const & word, QModelIndex const & parentIdx );
// Return tree level for item
int level( QModelIndex const & idx );
private:
QString m_favoritesFilename;
TreeItem * rootItem;
QDomDocument dom;
bool dirty;
};
#endif // __FAVORITIESPANEWIDGET_HH__INCLUDED__