File size: 6,967 Bytes
b6bd94d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
 * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */

#pragma once

/// Various custom widgets used in the Groups dialog
#include <vector>
#include <QListWidget>
#include <QSortFilterProxyModel>
#include "config.hh"
#include "dict/dictionary.hh"

/// A model to be projected into the view, according to Qt's MVC model
class DictListModel: public QAbstractListModel
{
  Q_OBJECT

public:

  DictListModel( QWidget * parent ):
    QAbstractListModel( parent ),
    isSource( false ),
    allDicts( 0 )
  {
  }

  /// Populates the current model with the given dictionaries. This is
  /// ought to be part of construction process.
  void populate( const std::vector< sptr< Dictionary::Class > > & active,
                 const std::vector< sptr< Dictionary::Class > > & available );
  void populate( const std::vector< sptr< Dictionary::Class > > & active );

  /// Marks that this model is used as an immutable dictionary source
  void setAsSource();
  bool sourceModel() const
  {
    return isSource;
  }

  /// Returns the dictionaries the model currently has listed
  const std::vector< sptr< Dictionary::Class > > & getCurrentDictionaries() const;

  void removeSelectedRows( QItemSelectionModel * source );
  void addSelectedUniqueFromModel( QItemSelectionModel * source );

  Qt::ItemFlags flags( const QModelIndex & index ) const override;
  int rowCount( const QModelIndex & parent ) const override;
  QVariant data( const QModelIndex & index, int role ) const override;
  bool insertRows( int row, int count, const QModelIndex & parent ) override;
  bool removeRows( int row, int count, const QModelIndex & parent ) override;
  bool setData( const QModelIndex & index, const QVariant & value, int role ) override;

  void addRow( const QModelIndex & parent, sptr< Dictionary::Class > dict );

  Qt::DropActions supportedDropActions() const override;

  void filterDuplicates();

private:

  bool isSource;
  std::vector< sptr< Dictionary::Class > > dictionaries;
  const std::vector< sptr< Dictionary::Class > > * allDicts;

signals:
  void contentChanged();
};

/// This widget is for dictionaries' lists, it handles drag-n-drop operations
/// with them etc.
class DictListWidget: public QListView
{
  Q_OBJECT

public:
  DictListWidget( QWidget * parent );
  ~DictListWidget() override = default;

  /// Populates the current list with the given dictionaries.
  void populate( const std::vector< sptr< Dictionary::Class > > & active,
                 const std::vector< sptr< Dictionary::Class > > & available );
  void populate( const std::vector< sptr< Dictionary::Class > > & active );

  /// Marks that this widget is used as an immutable dictionary source
  void setAsSource();

  /// Returns the dictionaries the widget currently has listed
  const std::vector< sptr< Dictionary::Class > > & getCurrentDictionaries() const;

  DictListModel * getModel()
  {
    return &model;
  }

signals:
  void gotFocus();

protected:
  void dropEvent( QDropEvent * event ) override;
  void focusInEvent( QFocusEvent * ) override;

  void rowsAboutToBeRemoved( const QModelIndex & parent, int start, int end ) override;

private:
  DictListModel model;
};

#include "ui_edit_group_tab.h"

/// A widget that is placed into each tab in the Groups dialog.
class DictGroupWidget: public QWidget
{
  Q_OBJECT

public:
  DictGroupWidget( QWidget * parent, const std::vector< sptr< Dictionary::Class > > &, const Config::Group & );

  Config::Group makeGroup() const;

  DictListModel * getModel() const
  {
    return ui.dictionaries->getModel();
  }

  QItemSelectionModel * getSelectionModel() const
  {
    return ui.dictionaries->selectionModel();
  }

  QString name()
  {
    return groupName;
  }

  void setName( const QString & name )
  {
    groupName = name;
  }

private slots:

  void groupIconActivated( int );
  void showDictInfo( const QPoint & pos );
  void removeCurrentItem( const QModelIndex & );

private:
  Ui::DictGroupWidget ui;
  unsigned groupId;
  QString groupName;

signals:
  void showDictionaryInfo( const QString & id );
};

/// A tab widget with groups inside
class DictGroupsWidget: public QTabWidget
{
  Q_OBJECT

public:

  DictGroupsWidget( QWidget * parent );

  /// Creates all the tabs with the groups
  void populate( const Config::Groups &,
                 const std::vector< sptr< Dictionary::Class > > & allDicts,
                 const std::vector< sptr< Dictionary::Class > > & activeDicts );

  /// Creates new empty group with the given name
  int addNewGroup( const QString & );

  /// Creates new empty group with the given name if no such group
  /// and return it index
  int addUniqueGroup( const QString & name );

  void addAutoGroups();

  /// auto grouping by containning folder
  void addAutoGroupsByFolders();
  void addGroupBasedOnMap( const QMultiMap< QString, sptr< Dictionary::Class > > & groupToDicts );

  void groupsByMetadata();

  /// Returns currently chosen group's name
  QString getCurrentGroupName() const;

  /// Changes the name of the currently chosen group, if any, to the given one
  void renameCurrentGroup( const QString & );

  /// Removes the currently chosen group, if any
  void removeCurrentGroup();

  /// Removes all the groups
  void removeAllGroups();

  /// Creates groups from what is currently set up
  Config::Groups makeGroups() const;

  DictListModel * getCurrentModel() const;

  DictListModel * getModelAt( int current ) const;
  int getDictionaryCountAt( int current ) const;
  std::vector< sptr< Dictionary::Class > > getDictionaryAt( int current ) const;

  QItemSelectionModel * getCurrentSelectionModel() const;

protected:
  void tabInserted( int index ) override;
  void tabRemoved( int index ) override;

private:

  /// Add source group to target group
  void combineGroups( int source, int target );

  unsigned nextId;
  const std::vector< sptr< Dictionary::Class > > * allDicts;
  const std::vector< sptr< Dictionary::Class > > * activeDicts;

private slots:
  void contextMenu( const QPoint & );
  void tabDataChanged();
  void removeTabRequested( int index );
  void addNewTab();


signals:
  void showDictionaryInfo( const QString & id );
  void countChanged();
  void newTabRequested();
};

class QuickFilterLine: public QLineEdit
{
  Q_OBJECT

public:

  QuickFilterLine( QWidget * parent );
  ~QuickFilterLine() override;

  /// Sets the source view to filter
  void applyTo( QAbstractItemView * source );

  QAction * getFocusAction()
  {
    return &m_focusAction;
  }

  QModelIndex mapToSource( const QModelIndex & idx );

protected:
  void keyPressEvent( QKeyEvent * event ) override;

private:
  QSortFilterProxyModel m_proxyModel;
  QAction m_focusAction;
  QAbstractItemView * m_source;

private slots:
  void filterChangedInternal();
  void emitFilterChanged();
  void focusFilterLine();

signals:
  void filterChanged( const QString & filter );
};