Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/Application/src/materiallibrary.cpp
3203 views
1
/*****************************************************************************
2
* *
3
* Elmer, A Finite Element Software for Multiphysical Problems *
4
* *
5
* Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland *
6
* *
7
* This program is free software; you can redistribute it and/or *
8
* modify it under the terms of the GNU General Public License *
9
* as published by the Free Software Foundation; either version 2 *
10
* of the License, or (at your option) any later version. *
11
* *
12
* This program is distributed in the hope that it will be useful, *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
* GNU General Public License for more details. *
16
* *
17
* You should have received a copy of the GNU General Public License *
18
* along with this program (in file fem/GPL-2); if not, write to the *
19
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
20
* Boston, MA 02110-1301, USA. *
21
* *
22
*****************************************************************************/
23
24
/*****************************************************************************
25
* *
26
* ElmerGUI materiallibrary *
27
* *
28
*****************************************************************************
29
* *
30
* Authors: Mikko Lyly, Juha Ruokolainen and Peter R�back *
31
* Email: [email protected] *
32
* Web: http://www.csc.fi/elmer *
33
* Address: CSC - IT Center for Science Ltd. *
34
* Keilaranta 14 *
35
* 02101 Espoo, Finland *
36
* *
37
* Original Date: 15 Mar 2008 *
38
* *
39
*****************************************************************************/
40
41
#include <QtGui>
42
#include <iostream>
43
#include "materiallibrary.h"
44
45
using namespace std;
46
47
MaterialLibrary::MaterialLibrary(QWidget *parent)
48
: QDialog(parent)
49
{
50
ui.setupUi(this);
51
52
connect(ui.okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
53
connect(ui.appendButton, SIGNAL(clicked()), this, SLOT(appendButtonClicked()));
54
connect(ui.clearButton, SIGNAL(clicked()), this, SLOT(clearButtonClicked()));
55
connect(ui.closeButton, SIGNAL(clicked()), this, SLOT(closeButtonClicked()));
56
57
// Load library:
58
//--------------
59
QString elmerGuiHome;
60
61
#ifdef __APPLE__DONTGOHERE_TODO
62
//QString matFileName = this->homePath + "/edf/egmaterials.xml";
63
QString matFileName = QDir::homePath() + "/edf/egmaterials.xml";
64
#else
65
QString matFileName = QCoreApplication::applicationDirPath()
66
+ "/../share/ElmerGUI/edf/egmaterials.xml"; // @TODO: fix path to share/ElmerGUI/edf
67
68
elmerGuiHome = QString(getenv("ELMERGUI_HOME"));
69
70
if(!elmerGuiHome.isEmpty())
71
matFileName = elmerGuiHome + "/edf/egmaterials.xml";
72
#endif
73
74
QListWidget *list = ui.materialListWidget;
75
list->clear();
76
materialDoc.clear();
77
appendDocument(matFileName);
78
79
// Enable selection by double clicking:
80
//--------------------------------------
81
connect(list, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(itemDoubleClicked(QListWidgetItem*)));
82
83
setWindowIcon(QIcon(":/icons/Mesh3D.png"));
84
85
86
appendFileToComboBox("/edf/egmaterials.xml");
87
addExtraMaterialLibraryFilesToComboBox();
88
89
connect(ui.fileComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(fileChanged(int)) );
90
91
ui.okButton->setIcon(QIcon::fromTheme("dialog-accept"));
92
ui.appendButton->setIcon(QIcon::fromTheme("list-add"));
93
ui.clearButton->setIcon(QIcon::fromTheme("edit-clear"));
94
ui.closeButton->setIcon(QIcon::fromTheme("dialog-error-round"));
95
96
}
97
98
MaterialLibrary::~MaterialLibrary()
99
{
100
}
101
102
void MaterialLibrary::okButtonClicked()
103
{
104
QListWidget *list = ui.materialListWidget;
105
QListWidgetItem *item = list->currentItem();
106
107
if(item == NULL)
108
return;
109
110
// Clear all line edits:
111
//-----------------------
112
for(int i = 0; i < editor->hash.count(); i++) {
113
hash_entry_t value = editor->hash.values().at(i);
114
QDomElement elem = value.elem;
115
QWidget *widget = value.widget;
116
if(elem.attribute("Widget") == "Edit") {
117
QLineEdit *lineEdit = (QLineEdit*)widget;
118
lineEdit->setText("");
119
}
120
}
121
122
// Update line edits with library properties:
123
//--------------------------------------------
124
QDomElement contents = materialDoc.documentElement();
125
QDomElement material = contents.firstChildElement("material");
126
for( ; !material.isNull(); material = material.nextSiblingElement()) {
127
QString materialName = material.attribute("name");
128
129
if(materialName != item->text())
130
continue;
131
132
editor->nameEdit->setText(materialName);
133
134
QDomElement property = material.firstChildElement();
135
for( ; !property.isNull(); property = property.nextSiblingElement()) {
136
QString propertyName = property.attribute("name").trimmed().toLower();
137
QString propertyValue = property.text().trimmed();
138
139
#if 0
140
cout << string(materialName.toLatin1()) << ": "
141
<< string(propertyName.toLatin1()) << ": "
142
<< string(propertyValue.toLatin1()) << endl;
143
#endif
144
145
// Copy the parameter value into material editor:
146
//------------------------------------------------
147
bool match = false;
148
for(int i = 0; i < editor->hash.count(); i++) {
149
hash_entry_t value = editor->hash.values().at(i);
150
QDomElement elem = value.elem;
151
QWidget *widget = value.widget;
152
QString widgetName = elem.firstChildElement("Name").text().trimmed().toLower();
153
154
if(elem.attribute("Widget") == "Edit") {
155
QLineEdit *lineEdit = (QLineEdit*)widget;
156
if(propertyName == widgetName) {
157
match = true;
158
lineEdit->setText(propertyValue);
159
#if 0
160
cout << "Material loader: found match for parameter: "
161
<< string(propertyName.toLatin1()) << endl;
162
#endif
163
}
164
}
165
166
if(elem.attribute("Widget") == "Combo") {
167
QComboBox *comboBox = (QComboBox*)widget;
168
if(propertyName == widgetName) {
169
for(int n = 0; n < comboBox->count(); n++) {
170
QString itemText = comboBox->itemText(n).trimmed();
171
if(itemText.toLower() == propertyValue.toLower())
172
comboBox->setCurrentIndex(n);
173
}
174
match = true;
175
}
176
}
177
// Without this check there will be multiple instances same material parameter
178
// in case there are multiple Solvers where it fits!
179
if(match) break;
180
}
181
182
#if 0
183
if(!match)
184
cout << "Material loader: no match for parameter: "
185
<< string(propertyName.toLatin1()) << endl;
186
#endif
187
}
188
}
189
190
this->close();
191
editor->raise();
192
}
193
194
void MaterialLibrary::itemDoubleClicked(QListWidgetItem *item)
195
{
196
QListWidget *list = ui.materialListWidget;
197
list->setCurrentItem(item);
198
okButtonClicked();
199
}
200
201
void MaterialLibrary::appendButtonClicked()
202
{
203
QString matFileName = QFileDialog::getOpenFileName(this);
204
205
if(matFileName.isEmpty())
206
return;
207
208
materialDoc.clear();
209
appendDocument(matFileName);
210
}
211
212
void MaterialLibrary::clearButtonClicked()
213
{
214
QListWidget *list = ui.materialListWidget;
215
list->clear();
216
}
217
218
void MaterialLibrary::closeButtonClicked()
219
{
220
this->close();
221
editor->raise();
222
}
223
224
225
void MaterialLibrary::appendDocument(QString matFileName)
226
{
227
QString errStr;
228
int errRow;
229
int errCol;
230
QFile materialFile(matFileName);
231
232
if(!materialFile.exists()) {
233
QMessageBox::information(window(), tr("Material loader"),
234
tr("Material library does not exist"));
235
return;
236
237
} else {
238
239
if(!materialDoc.setContent(&materialFile, true, &errStr, &errRow, &errCol)) {
240
QMessageBox::information(window(), tr("Material loader"),
241
tr("Parse error at line %1, col %2:\n%3")
242
.arg(errRow).arg(errCol).arg(errStr));
243
materialFile.close();
244
return;
245
}
246
}
247
248
materialFile.close();
249
250
if(materialDoc.documentElement().tagName() != "materiallibrary") {
251
QMessageBox::information(window(), tr("Material loader"),
252
tr("This is not a material library file"));
253
return;
254
}
255
256
// Update list widget:
257
//---------------------
258
QListWidget *list = ui.materialListWidget;
259
QDomElement contents = materialDoc.documentElement();
260
QDomElement material = contents.firstChildElement("material");
261
for( ; !material.isNull(); material = material.nextSiblingElement()) {
262
QString materialName = material.attribute("name");
263
QListWidgetItem *item = new QListWidgetItem(materialName, list);
264
}
265
list->sortItems();
266
}
267
268
void MaterialLibrary::fileChanged(int index)
269
{
270
ui.materialListWidget->clear();
271
materialDoc.clear();
272
appendDocument(ui.fileComboBox->itemData(index).toString());
273
}
274
275
void MaterialLibrary::appendFileToComboBox(QString fileName)
276
{
277
// CAUTION: The argument fileName should be like "/edf/egmaterials.xls"
278
279
#ifdef __APPLE__DONTGOHERE_TODO
280
//QString matFileName = this->homePath + fileName;
281
QString matFileName = QDir::homePath() + fileName;
282
#else
283
QString matFileName = QCoreApplication::applicationDirPath()
284
+ "/../share/ElmerGUI" + fileName; // @TODO: fix path to share/ElmerGUI/edf
285
286
QString elmerGuiHome = QString(getenv("ELMERGUI_HOME"));
287
288
if(!elmerGuiHome.isEmpty())
289
matFileName = elmerGuiHome + fileName;
290
#endif
291
292
QString errStr;
293
int errRow;
294
int errCol;
295
QFile materialFile(matFileName);
296
QDomDocument doc;
297
298
if(!materialFile.exists()) {
299
return;
300
301
} else {
302
303
if(!doc.setContent(&materialFile, true, &errStr, &errRow, &errCol)) {
304
materialFile.close();
305
return;
306
}
307
}
308
309
materialFile.close();
310
311
if(doc.documentElement().tagName() != "materiallibrary") {
312
return;
313
}
314
315
ui.fileComboBox->addItem(fileName, QVariant(matFileName));
316
}
317
318
319
void MaterialLibrary::addExtraMaterialLibraryFilesToComboBox()
320
{
321
322
#ifdef __APPLE__DONTGOHERE_TODO
323
//QString extraDirName = this->homePath + "/edf-extra";
324
QString extraDirName = QDir::homePath() + "/edf-extra";
325
#else
326
QString extraDirName = QCoreApplication::applicationDirPath()
327
+ "/../share/ElmerGUI/edf-extra";
328
329
QString elmerGuiHome = QString(getenv("ELMERGUI_HOME"));
330
331
if(!elmerGuiHome.isEmpty())
332
extraDirName = elmerGuiHome + "/edf-extra";
333
#endif
334
335
QStringList nameFilters;
336
nameFilters << "*.xml";
337
QDir extraDir(extraDirName);
338
QStringList fileNameList = extraDir.entryList(nameFilters, QDir::Files | QDir::Readable);
339
for(int i=0; i < fileNameList.size(); i++)
340
{
341
appendFileToComboBox("/edf-extra/" + fileNameList.at(i));
342
}
343
344
}
345
346
347