forked from simulationcraft/simc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sc_importWidget.cpp
225 lines (192 loc) · 6.23 KB
/
sc_importWidget.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
#include "sc_importWidget.hpp"
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
BattleNetImportWidget::BattleNetImportWidget( QWidget* parent )
: QWidget( parent ),
m_layout( new QHBoxLayout( this ) ),
m_regionLabel( new QLabel( tr( "Region" ), this ) ),
m_regionCombo( new QComboBox( this ) ),
m_realmLabel( new QLabel( tr( "Realm" ), this ) ),
m_realmCombo( new QComboBox( this ) ),
m_characterLabel( new QLabel( tr( "Character" ), this ) ),
m_character( new QLineEdit( this ) ),
m_specializationLabel( new QLabel( tr( "Specialization" ) ) ),
m_specializationCombo( new QComboBox( this ) ),
m_importButton( new QPushButton( tr( "Import" ), this ) )
{
setLayout( m_layout );
m_layout->addWidget( m_regionLabel );
m_layout->addWidget( m_regionCombo );
m_layout->addWidget( m_realmLabel );
m_layout->addWidget( m_realmCombo );
m_layout->addWidget( m_characterLabel );
m_layout->addWidget( m_character );
m_layout->addWidget( m_specializationLabel );
m_layout->addWidget( m_specializationCombo );
m_layout->addWidget( m_importButton );
// Don't enable specialization for now
m_specializationLabel->hide();
m_specializationCombo->hide();
populateRegion();
populateSpecialization();
loadRealmData();
connect( m_character, SIGNAL( editingFinished() ), this, SLOT( returnPressed() ) );
connect( m_importButton, SIGNAL( clicked() ), this, SLOT( buttonClicked() ) );
connect( m_realmCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( realmIndexChanged( int ) ) );
connect( m_regionCombo, SIGNAL( currentTextChanged( const QString& ) ), this,
SLOT( armoryRegionChangedIn( const QString& ) ) );
}
// Populate realmlists
void BattleNetImportWidget::loadRealmData()
{
static const QVector<QString> files = { "://realms/us.json", "://realms/eu.json", "://realms/tw.json",
"://realms/kr.json" };
for ( const auto& file : files )
{
QFile json_file( file );
if ( json_file.open( QIODevice::ReadOnly ) )
{
parseRealmListFile( json_file );
}
else
{
qDebug() << "Could not open" << json_file.fileName();
}
}
selectRegion( m_settings.value( "options/armory_region", "US" ).toString() ); // Populate with default data
}
void BattleNetImportWidget::parseRealmListFile( QFile& file )
{
QJsonParseError error;
auto jsdoc = QJsonDocument::fromJson( file.readAll(), &error );
if ( error.error != QJsonParseError::NoError )
{
qDebug() << error.errorString();
return;
}
if ( jsdoc.isNull() )
{
qDebug() << "No readable data in" << file.fileName();
return;
}
auto root = jsdoc.object();
if ( root.isEmpty() || !root.contains( "realms" ) )
{
qDebug() << "No root element or realms in" << file.fileName();
return;
}
auto realms = root[ "realms" ].toArray();
if ( realms.empty() )
{
qDebug() << "No realms in realm object";
return;
}
QVector<std::pair<QString, QString>> realmList;
for ( QJsonValueRef entry : realms )
{
auto obj = entry.toObject();
realmList.push_back( std::make_pair( obj[ "name" ].toString(), obj[ "slug" ].toString() ) );
}
QRegularExpression region( "([A-Za-z]+).json$" );
QString region_str;
auto region_match = region.match( file.fileName() );
if ( region_match.hasMatch() )
{
region_str = region_match.captured( 1 ).toUpper();
}
if ( region_str.size() > 0 )
{
auto model = new QStandardItemModel( this );
// model -> insertRows( 0, realmList.size() );
for ( const auto& [ name, slug ] : realmList )
{
QStandardItem* item = new QStandardItem( name );
item->setData( slug, Qt::UserRole );
model->appendRow( item );
}
m_realmModels[ region_str ] = model;
qDebug() << "Loaded realm information for" << region_str << "n_model_entries" << model->rowCount();
}
}
void BattleNetImportWidget::selectRegion( const QString& region )
{
if ( m_realmModels.find( region ) == m_realmModels.end() )
{
return;
}
m_realmCombo->setModel( m_realmModels[ region ] );
QString s = m_settings.value( "importWidget/lastUsedRealm" ).toString();
if ( s.size() > 0 )
{
auto index = m_realmCombo->findData( s, Qt::DisplayRole );
if ( index > -1 )
{
m_realmCombo->setCurrentIndex( index );
// Also set focus to the character input box
m_character->setFocus();
}
}
auto region_index = m_regionCombo->findData( region.toUpper(), Qt::DisplayRole );
if ( region_index == -1 )
{
qDebug() << "Unable to find region" << region.toUpper() << "from list of supported regions";
return;
}
m_regionCombo->setCurrentIndex( region_index );
}
bool BattleNetImportWidget::validateInput() const
{
return character().size() >= 2;
}
void BattleNetImportWidget::realmIndexChanged( int index )
{
m_settings.setValue( "importWidget/lastUsedRealm", m_realmCombo->itemText( index ) );
m_settings.sync();
}
// Note, ensure import only begins when the user presses enter, and not when focus is lost on the
// character text input
void BattleNetImportWidget::returnPressed()
{
if ( !m_character->hasFocus() )
{
return;
}
if ( !validateInput() )
{
return;
}
emit importTriggeredOut( region(), realm(), character(), specialization() );
}
void BattleNetImportWidget::buttonClicked()
{
if ( !validateInput() )
{
return;
}
emit importTriggeredOut( region(), realm(), character(), specialization() );
}
void BattleNetImportWidget::armoryRegionChangedIn( const QString& region )
{
// Remove last used realm if user changes regions
m_settings.remove( "importWidget/lastUsedRealm" );
selectRegion( region.toUpper() );
emit armoryRegionChangedOut( region.toUpper() );
}
void BattleNetImportWidget::populateRegion()
{
m_regionCombo->addItem( tr( "US" ) );
m_regionCombo->addItem( tr( "EU" ) );
m_regionCombo->addItem( tr( "KR" ) );
m_regionCombo->addItem( tr( "TW" ) );
}
void BattleNetImportWidget::populateSpecialization()
{
m_specializationCombo->addItem( tr( "Active" ) );
m_specializationCombo->addItem( tr( "First" ) );
m_specializationCombo->addItem( tr( "Second" ) );
m_specializationCombo->addItem( tr( "Third" ) );
m_specializationCombo->addItem( tr( "Fourth" ) );
}