-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictmanager.cpp
307 lines (267 loc) · 9.81 KB
/
dictmanager.cpp
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/***************************************************************************
* Copyright (C) 2011-2012 by Marcin Nawrocki <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
#include "dictmanager.h"
#include "configmanager.h"
#include "dictinstance.h"
#include "ydptypes.h"
#include "dict.h"
#include <QStringListModel>
#include <QStandardItemModel>
typedef QMap<int, IDictionary*> VersionToDictMap;
class DictManager : public IDictManager
{
public:
static DictManager& instance();
QAbstractItemModel* entriesModel() const { return _entriesModel; }
QAbstractItemModel* dictionaryModel() const { return _dictionaryModel; }
QAbstractItemModel* historyModel() const { return _historyModel; }
bool isTypeEqual(int type) { return ((_version + _direction) == type ? true : false); }
int currentDictionaryIndex() const;
bool isCurrentDictValid() const;
void setVersion(int currentDictionaryIndex);
void invertDirection();
bool setType(int historyItemIndex);
bool isDirectionForeign() const;
void initHistory();
void insertHistoryItem(QString text);
void initDictionary();
void clearEntries();
void clearDictionary();
void clearHistory();
void initEntries();
void init();
const QStringList& entries() const;
QString description(int index) const;
int entryToIndex(QString entry) const;
QString entriesText(int index) const;
QString foreignIconName() const;
QString tooltip() const;
QString wavFormat() const;
QString samplesPath() const;
DictManager();
~DictManager();
private:
VersionToDictMap _dictionaries;
int _version;
int _direction;
QStringList _entriesList;
QStringListModel* _entriesModel;
QStandardItemModel* _dictionaryModel;
QStandardItemModel* _historyModel;
int findValidDictionaryVersion();
};
DictManager::DictManager() :
_version(YdpTypes::InvalidVersion),
_direction(YdpTypes::Foreign)
{
IConfigManager& confMan = IConfigManager::instance();
foreach (int version, DictInstance::versionList())
{
IDictionary* dict = IDictionary::createInstance(version, confMan.getConfig().getDictionaryPath(version));
_dictionaries[version] = dict;
QObject::connect(&confMan, SIGNAL(dictionaryDirectoryChanged(int, QString)), dict, SLOT(initialize(int, QString)));
}
init();
_dictionaryModel = new QStandardItemModel();
_historyModel = new QStandardItemModel();
_entriesModel = new QStringListModel();
_entriesModel->setStringList(QStringList());
}
DictManager::~DictManager()
{
foreach (IDictionary* dictionary, _dictionaries)
dictionary->release();
clearDictionary();
clearHistory();
delete _dictionaryModel;
delete _historyModel;
delete _entriesModel;
}
IDictManager& IDictManager::instance()
{
static DictManager manager;
return manager;
}
int DictManager::findValidDictionaryVersion()
{
foreach (int version, _dictionaries.keys())
if (_dictionaries[version]->isValid())
return version;
return YdpTypes::InvalidVersion;
}
void DictManager::init()
{
IConfigManager& confMan = IConfigManager::instance();
if (confMan.getConfig().dictionaryToOpen() == Recent)
{
_version = YdpTypes::typeToVersion(confMan.getConfig().recentDictionary());
_direction = YdpTypes::typeToDirection(confMan.getConfig().recentDictionary());
}
else
{
_version = YdpTypes::typeToVersion(confMan.getConfig().selectedDictionary());
_direction = YdpTypes::typeToDirection(confMan.getConfig().selectedDictionary());
}
if (YdpTypes::InvalidVersion == _version || !_dictionaries[_version]->isValid())
_version = findValidDictionaryVersion();
}
bool DictManager::isCurrentDictValid() const
{
return _version != YdpTypes::InvalidVersion;
}
void DictManager::initHistory()
{
IConfigManager& confMan = IConfigManager::instance();
foreach (HistoryItem historyItem, confMan.getConfig().history())
{
YdpTypes::YdpDirection direction = YdpTypes::typeToDirection(historyItem.getType());
YdpTypes::YdpVersion version = YdpTypes::typeToVersion(historyItem.getType());
IDictionary* dict = _dictionaries[version];
if (!dict->isValid())
continue;
if (_historyModel->rowCount() == confMan.getConfig().maxNumberOfHistoryItems())
break;
QStandardItem *item = new QStandardItem();
item->setText(historyItem.getText());
item->setIcon(QIcon(DictInstance::dictInst(version).getIconName(direction)));
item->setData(QVariant(historyItem.getType()), Qt::UserRole);
_historyModel->appendRow(item);
}
}
void DictManager::insertHistoryItem(QString text)
{
QStandardItem *item = new QStandardItem();
item->setText(text);
item->setIcon(QIcon(DictInstance::dictInst(_version).getIconName(_direction)));
item->setData(QVariant(_version + _direction), Qt::UserRole);
_historyModel->insertRow(0, item);
const int rowCount = _historyModel->rowCount();
if (rowCount > IConfigManager::instance().getConfig().maxNumberOfHistoryItems())
{
delete _historyModel->item(rowCount-1);
_historyModel->removeRow(rowCount-1);
}
IConfigManager::instance().getConfig().insertHistoryItem(text, _version + _direction);
}
void DictManager::initDictionary()
{
foreach (int version, _dictionaries.keys())
{
if (_dictionaries[version]->isValid())
{
QStandardItem *item = new QStandardItem();
DictInst dictInstance = DictInstance::dictInst(version);
item->setText(dictInstance.getDescription());
item->setIcon(QIcon(dictInstance.getIconName(YdpTypes::Foreign)));
item->setData(QVariant(version), Qt::UserRole);
_dictionaryModel->appendRow(item);
}
}
}
void DictManager::initEntries()
{
_entriesModel->setStringList(entries());
emit dictionaryChanged(_version + _direction);
}
void DictManager::clearEntries()
{
_entriesModel->setStringList(QStringList());
}
void DictManager::clearDictionary()
{
const int itemCount = _dictionaryModel->rowCount();
for (int index = 0; index < itemCount; index++)
delete _dictionaryModel->item(index);
_dictionaryModel->removeRows(0, itemCount);
}
void DictManager::clearHistory()
{
const int itemCount = _historyModel->rowCount();
for (int index = 0; index < itemCount; index++)
delete _historyModel->item(index);
_historyModel->removeRows(0, itemCount);
}
const QStringList& DictManager::entries() const
{
return _dictionaries[_version]->entries(_direction);
}
QString DictManager::description(int index) const
{
return _dictionaries[_version]->description(_direction, index);
}
int DictManager::entryToIndex(QString entry) const
{
return _dictionaries[_version]->entryToIndex(_direction, entry);
}
QString DictManager::entriesText(int index) const
{
return _entriesModel->data(_entriesModel->index(index), Qt::EditRole).toString();
}
QString DictManager::foreignIconName() const
{
return DictInstance::dictInst(_version).getForeignIconName();
}
QString DictManager::tooltip() const
{
return DictInstance::dictInst(_version).getToolTip();
}
QString DictManager::wavFormat() const
{
return DictInstance::dictInst(_version).getWavFormat();
}
void DictManager::invertDirection()
{
_direction = YdpTypes::getOppositeDirection(_direction);
initEntries();
}
bool DictManager::isDirectionForeign() const
{
return YdpTypes::Foreign == _direction;
}
QString DictManager::samplesPath() const
{
return IConfigManager::instance().getConfig().samplesPath(_version);
}
void DictManager::setVersion(int dictionaryIndex)
{
_version = _dictionaryModel->itemData(_dictionaryModel->index(dictionaryIndex, 0))[Qt::UserRole].toInt();
initEntries();
}
bool DictManager::setType(int historyItemIndex)
{
const int type = _historyModel->itemData(_historyModel->index(historyItemIndex, 0))[Qt::UserRole].toInt();
const int version = YdpTypes::typeToVersion(type);
const int direction = YdpTypes::typeToDirection(type);
const bool versionChange = (_version != version);
const bool directionChange = (_direction != direction);
_version = version;
_direction = direction;
if (versionChange || directionChange)
initEntries();
return versionChange;
}
int DictManager::currentDictionaryIndex() const
{
const int count = _dictionaryModel->rowCount();
int index;
for (index = 0; index < count; index++)
if (_version == _dictionaryModel->itemData(_dictionaryModel->index(index, 0))[Qt::UserRole].toInt())
break;
return index;
}