Path: blob/devel/ElmerGUI/Application/src/materiallibrary.cpp
3203 views
/*****************************************************************************1* *2* Elmer, A Finite Element Software for Multiphysical Problems *3* *4* Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland *5* *6* This program is free software; you can redistribute it and/or *7* modify it under the terms of the GNU General Public License *8* as published by the Free Software Foundation; either version 2 *9* of the License, or (at your option) any later version. *10* *11* This program is distributed in the hope that it will be useful, *12* but WITHOUT ANY WARRANTY; without even the implied warranty of *13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *14* GNU General Public License for more details. *15* *16* You should have received a copy of the GNU General Public License *17* along with this program (in file fem/GPL-2); if not, write to the *18* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *19* Boston, MA 02110-1301, USA. *20* *21*****************************************************************************/2223/*****************************************************************************24* *25* ElmerGUI materiallibrary *26* *27*****************************************************************************28* *29* Authors: Mikko Lyly, Juha Ruokolainen and Peter R�back *30* Email: [email protected] *31* Web: http://www.csc.fi/elmer *32* Address: CSC - IT Center for Science Ltd. *33* Keilaranta 14 *34* 02101 Espoo, Finland *35* *36* Original Date: 15 Mar 2008 *37* *38*****************************************************************************/3940#include <QtGui>41#include <iostream>42#include "materiallibrary.h"4344using namespace std;4546MaterialLibrary::MaterialLibrary(QWidget *parent)47: QDialog(parent)48{49ui.setupUi(this);5051connect(ui.okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));52connect(ui.appendButton, SIGNAL(clicked()), this, SLOT(appendButtonClicked()));53connect(ui.clearButton, SIGNAL(clicked()), this, SLOT(clearButtonClicked()));54connect(ui.closeButton, SIGNAL(clicked()), this, SLOT(closeButtonClicked()));5556// Load library:57//--------------58QString elmerGuiHome;5960#ifdef __APPLE__DONTGOHERE_TODO61//QString matFileName = this->homePath + "/edf/egmaterials.xml";62QString matFileName = QDir::homePath() + "/edf/egmaterials.xml";63#else64QString matFileName = QCoreApplication::applicationDirPath()65+ "/../share/ElmerGUI/edf/egmaterials.xml"; // @TODO: fix path to share/ElmerGUI/edf6667elmerGuiHome = QString(getenv("ELMERGUI_HOME"));6869if(!elmerGuiHome.isEmpty())70matFileName = elmerGuiHome + "/edf/egmaterials.xml";71#endif7273QListWidget *list = ui.materialListWidget;74list->clear();75materialDoc.clear();76appendDocument(matFileName);7778// Enable selection by double clicking:79//--------------------------------------80connect(list, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(itemDoubleClicked(QListWidgetItem*)));8182setWindowIcon(QIcon(":/icons/Mesh3D.png"));838485appendFileToComboBox("/edf/egmaterials.xml");86addExtraMaterialLibraryFilesToComboBox();8788connect(ui.fileComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(fileChanged(int)) );8990ui.okButton->setIcon(QIcon::fromTheme("dialog-accept"));91ui.appendButton->setIcon(QIcon::fromTheme("list-add"));92ui.clearButton->setIcon(QIcon::fromTheme("edit-clear"));93ui.closeButton->setIcon(QIcon::fromTheme("dialog-error-round"));9495}9697MaterialLibrary::~MaterialLibrary()98{99}100101void MaterialLibrary::okButtonClicked()102{103QListWidget *list = ui.materialListWidget;104QListWidgetItem *item = list->currentItem();105106if(item == NULL)107return;108109// Clear all line edits:110//-----------------------111for(int i = 0; i < editor->hash.count(); i++) {112hash_entry_t value = editor->hash.values().at(i);113QDomElement elem = value.elem;114QWidget *widget = value.widget;115if(elem.attribute("Widget") == "Edit") {116QLineEdit *lineEdit = (QLineEdit*)widget;117lineEdit->setText("");118}119}120121// Update line edits with library properties:122//--------------------------------------------123QDomElement contents = materialDoc.documentElement();124QDomElement material = contents.firstChildElement("material");125for( ; !material.isNull(); material = material.nextSiblingElement()) {126QString materialName = material.attribute("name");127128if(materialName != item->text())129continue;130131editor->nameEdit->setText(materialName);132133QDomElement property = material.firstChildElement();134for( ; !property.isNull(); property = property.nextSiblingElement()) {135QString propertyName = property.attribute("name").trimmed().toLower();136QString propertyValue = property.text().trimmed();137138#if 0139cout << string(materialName.toLatin1()) << ": "140<< string(propertyName.toLatin1()) << ": "141<< string(propertyValue.toLatin1()) << endl;142#endif143144// Copy the parameter value into material editor:145//------------------------------------------------146bool match = false;147for(int i = 0; i < editor->hash.count(); i++) {148hash_entry_t value = editor->hash.values().at(i);149QDomElement elem = value.elem;150QWidget *widget = value.widget;151QString widgetName = elem.firstChildElement("Name").text().trimmed().toLower();152153if(elem.attribute("Widget") == "Edit") {154QLineEdit *lineEdit = (QLineEdit*)widget;155if(propertyName == widgetName) {156match = true;157lineEdit->setText(propertyValue);158#if 0159cout << "Material loader: found match for parameter: "160<< string(propertyName.toLatin1()) << endl;161#endif162}163}164165if(elem.attribute("Widget") == "Combo") {166QComboBox *comboBox = (QComboBox*)widget;167if(propertyName == widgetName) {168for(int n = 0; n < comboBox->count(); n++) {169QString itemText = comboBox->itemText(n).trimmed();170if(itemText.toLower() == propertyValue.toLower())171comboBox->setCurrentIndex(n);172}173match = true;174}175}176// Without this check there will be multiple instances same material parameter177// in case there are multiple Solvers where it fits!178if(match) break;179}180181#if 0182if(!match)183cout << "Material loader: no match for parameter: "184<< string(propertyName.toLatin1()) << endl;185#endif186}187}188189this->close();190editor->raise();191}192193void MaterialLibrary::itemDoubleClicked(QListWidgetItem *item)194{195QListWidget *list = ui.materialListWidget;196list->setCurrentItem(item);197okButtonClicked();198}199200void MaterialLibrary::appendButtonClicked()201{202QString matFileName = QFileDialog::getOpenFileName(this);203204if(matFileName.isEmpty())205return;206207materialDoc.clear();208appendDocument(matFileName);209}210211void MaterialLibrary::clearButtonClicked()212{213QListWidget *list = ui.materialListWidget;214list->clear();215}216217void MaterialLibrary::closeButtonClicked()218{219this->close();220editor->raise();221}222223224void MaterialLibrary::appendDocument(QString matFileName)225{226QString errStr;227int errRow;228int errCol;229QFile materialFile(matFileName);230231if(!materialFile.exists()) {232QMessageBox::information(window(), tr("Material loader"),233tr("Material library does not exist"));234return;235236} else {237238if(!materialDoc.setContent(&materialFile, true, &errStr, &errRow, &errCol)) {239QMessageBox::information(window(), tr("Material loader"),240tr("Parse error at line %1, col %2:\n%3")241.arg(errRow).arg(errCol).arg(errStr));242materialFile.close();243return;244}245}246247materialFile.close();248249if(materialDoc.documentElement().tagName() != "materiallibrary") {250QMessageBox::information(window(), tr("Material loader"),251tr("This is not a material library file"));252return;253}254255// Update list widget:256//---------------------257QListWidget *list = ui.materialListWidget;258QDomElement contents = materialDoc.documentElement();259QDomElement material = contents.firstChildElement("material");260for( ; !material.isNull(); material = material.nextSiblingElement()) {261QString materialName = material.attribute("name");262QListWidgetItem *item = new QListWidgetItem(materialName, list);263}264list->sortItems();265}266267void MaterialLibrary::fileChanged(int index)268{269ui.materialListWidget->clear();270materialDoc.clear();271appendDocument(ui.fileComboBox->itemData(index).toString());272}273274void MaterialLibrary::appendFileToComboBox(QString fileName)275{276// CAUTION: The argument fileName should be like "/edf/egmaterials.xls"277278#ifdef __APPLE__DONTGOHERE_TODO279//QString matFileName = this->homePath + fileName;280QString matFileName = QDir::homePath() + fileName;281#else282QString matFileName = QCoreApplication::applicationDirPath()283+ "/../share/ElmerGUI" + fileName; // @TODO: fix path to share/ElmerGUI/edf284285QString elmerGuiHome = QString(getenv("ELMERGUI_HOME"));286287if(!elmerGuiHome.isEmpty())288matFileName = elmerGuiHome + fileName;289#endif290291QString errStr;292int errRow;293int errCol;294QFile materialFile(matFileName);295QDomDocument doc;296297if(!materialFile.exists()) {298return;299300} else {301302if(!doc.setContent(&materialFile, true, &errStr, &errRow, &errCol)) {303materialFile.close();304return;305}306}307308materialFile.close();309310if(doc.documentElement().tagName() != "materiallibrary") {311return;312}313314ui.fileComboBox->addItem(fileName, QVariant(matFileName));315}316317318void MaterialLibrary::addExtraMaterialLibraryFilesToComboBox()319{320321#ifdef __APPLE__DONTGOHERE_TODO322//QString extraDirName = this->homePath + "/edf-extra";323QString extraDirName = QDir::homePath() + "/edf-extra";324#else325QString extraDirName = QCoreApplication::applicationDirPath()326+ "/../share/ElmerGUI/edf-extra";327328QString elmerGuiHome = QString(getenv("ELMERGUI_HOME"));329330if(!elmerGuiHome.isEmpty())331extraDirName = elmerGuiHome + "/edf-extra";332#endif333334QStringList nameFilters;335nameFilters << "*.xml";336QDir extraDir(extraDirName);337QStringList fileNameList = extraDir.entryList(nameFilters, QDir::Files | QDir::Readable);338for(int i=0; i < fileNameList.size(); i++)339{340appendFileToComboBox("/edf-extra/" + fileNameList.at(i));341}342343}344345346347