Skip to content

Commit

Permalink
Merge c25a36d into 46dcd71
Browse files Browse the repository at this point in the history
  • Loading branch information
LafCorentin committed Aug 21, 2020
2 parents 46dcd71 + c25a36d commit f125dad
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ QMimeData * CellMLZincMappingViewEditingModel::mimeData(const QModelIndexList &p
//==============================================================================

CellMLZincMappingViewEditingWidget::CellMLZincMappingViewEditingWidget(const QString &pFileName,
const QString &pMeshFileName,
const QStringList &pZincMeshFileNames,
QWidget *pParent, CellMLZincMappingViewWidget *pViewWidget) :
Core::Widget(pParent),
mViewWidget(pViewWidget),
mMapMatch(),
mMeshFileName(pMeshFileName)
mZincMeshFileNames(pZincMeshFileNames)
{
mCellmlFile = CellMLSupport::CellmlFileManager::instance()->cellmlFile(pFileName);

Expand Down Expand Up @@ -173,7 +173,7 @@ CellMLZincMappingViewEditingWidget::CellMLZincMappingViewEditingWidget(const QSt

// add a Zinc widget

mZincWidget = new CellMLZincMappingViewZincWidget(this, mMeshFileName, this);
mZincWidget = new CellMLZincMappingViewZincWidget(this, mZincMeshFileNames, this);

connect(mClearNode, &QAction::triggered,
mZincWidget, &CellMLZincMappingViewZincWidget::eraseNode);
Expand Down Expand Up @@ -266,16 +266,16 @@ void CellMLZincMappingViewEditingWidget::filePermissionsChanged()

//==============================================================================

bool CellMLZincMappingViewEditingWidget::setMeshFile(const QString &pFileName, bool pShowWarning)
bool CellMLZincMappingViewEditingWidget::setMeshFiles(const QStringList &pFileNames, bool pShowWarning)
{
//TODO warnings ?
Q_UNUSED(pShowWarning)

mMeshFileName = pFileName;
mZincWidget->changeSource(pFileName);
mZincMeshFileNames = pFileNames;
mZincWidget->changeSource(pFileNames);
mMapMatch.clear();
selectNode(-1);
mViewWidget->setDefaultMeshFile(pFileName);
mViewWidget->setDefaultMeshFiles(pFileNames);
return true;
}

Expand Down Expand Up @@ -311,6 +311,10 @@ void CellMLZincMappingViewEditingWidget::dragEnterEvent(QDragEnterEvent *pEvent)
if (fileName.contains(".exelem")||fileName.contains(".exnode")||fileName.contains(".exfile")) {
acceptEvent = true;
}
if (fileName.contains(".json")) {
acceptEvent = true;
}

}

if (acceptEvent) {
Expand All @@ -334,10 +338,12 @@ void CellMLZincMappingViewEditingWidget::dragMoveEvent(QDragMoveEvent *pEvent)
void CellMLZincMappingViewEditingWidget::dropEvent(QDropEvent *pEvent)
{
// Import/open the one or several files

QStringList meshFileList;
for (const auto &fileName : Core::droppedFileNames(pEvent)) {
if (fileName.contains(".exelem")||fileName.contains(".exnode")||fileName.contains(".exfile")) {
setMeshFile(fileName);
meshFileList.append(fileName);
} else if (fileName.contains(".json")) {
openMapping(fileName);
} else if (mFileTypeInterfaces.contains(fileName)) {
//import(fileName); //?
//TODO
Expand All @@ -346,6 +352,10 @@ void CellMLZincMappingViewEditingWidget::dropEvent(QDropEvent *pEvent)
}
}

if (!meshFileList.isEmpty()) {
setMeshFiles(meshFileList);
}

// Accept the proposed action for the event

pEvent->acceptProposedAction();
Expand Down Expand Up @@ -413,8 +423,6 @@ void CellMLZincMappingViewEditingWidget::populateTree()

void CellMLZincMappingViewEditingWidget::saveMapping(const QString &pFileName)
{
Q_UNUSED(pFileName);

//find a filename

Core::FileManager *fileManagerInstance = Core::FileManager::instance();
Expand All @@ -430,7 +438,7 @@ void CellMLZincMappingViewEditingWidget::saveMapping(const QString &pFileName)
QStringList());

QJsonObject jsonContent;
jsonContent.insert("meshfile",QJsonValue::fromVariant(mMeshFileName));
jsonContent.insert("meshfiles",QJsonValue::fromVariant(mZincMeshFileNames));

//Jsonise the map
QJsonArray jsonMapArray;
Expand All @@ -455,6 +463,81 @@ void CellMLZincMappingViewEditingWidget::saveMapping(const QString &pFileName)
jsonFile.write(doc.toJson());
}

//==============================================================================

void CellMLZincMappingViewEditingWidget::openMapping(const QString &pFileName)
{

//open the file

QFile file;
file.setFileName(pFileName);

if (!file.exists()) {
return;
}

file.open(QIODevice::ReadOnly | QIODevice::Text);
QString val = file.readAll();
file.close();

//take element from json objects

QJsonDocument jsonDocument = QJsonDocument::fromJson(val.toUtf8());

QJsonObject jsonObject = jsonDocument.object();
QStringList meshFiles = jsonObject.value("meshfiles").toVariant().toStringList();

//TODO check if mesh files are good

//clear the map

mMapMatch.clear();

//insert the new mapping points in the current mapping system

int id;
QString component, variable;
QMap<QString,QSet<QString>> variables;

//first collect the variables from the cellml tree o know what exists

auto treeRoot = mVariableTreeModel->invisibleRootItem();
int numberComponents = treeRoot->rowCount();
int numberVariables;

//TODO improvable by creating the Set on the side and linking it to the map after
for (int i = 0; i < numberComponents; ++i) {
QStandardItem *componentItem = treeRoot->child(i);
component = componentItem->text();

variables.insert(component, QSet<QString>());

numberVariables = componentItem->rowCount();
for (int j = 0; j < numberVariables; ++j) {
variables.find(component)->insert(componentItem->child(j)->text());
}
}


for (auto mapPointJson : jsonObject.value("map").toArray()) {
QJsonObject mapPoint = mapPointJson.toObject();

id = mapPoint.value("node").toInt();
component = mapPoint.value("component").toString();
variable = mapPoint.value("variable").toString();

// test if the point is valid

if (mZincWidget->hasNode(id)
&& variables.contains(component)
&& variables.find(component)->contains(variable)) {

mZincWidget->setNodeMapped(id);
setNodeValue(id,component,variable);
}
}
}

//==============================================================================

Expand Down Expand Up @@ -530,9 +613,11 @@ void CellMLZincMappingViewEditingWidget::saveMappingSlot()
saveMapping({});
}

//==============================================================================

void CellMLZincMappingViewEditingWidget::loadMeshFile()
{
setMeshFile(Core::getOpenFileName("Open mesh file"));
setMeshFiles(Core::getOpenFileNames("Open mesh file"));
}

} // namespace MappingView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class CellMLZincMappingViewEditingWidget : public Core::Widget

public:
explicit CellMLZincMappingViewEditingWidget(const QString &pCellmlFileName,
const QString &pMeshFileName,
const QStringList &pMeshFileName,
QWidget *pParent,
CellMLZincMappingViewWidget *pViewWidget);

Expand All @@ -99,7 +99,7 @@ class CellMLZincMappingViewEditingWidget : public Core::Widget

void filePermissionsChanged();

bool setMeshFile(const QString &pFileName, bool pShowWarning = true);
bool setMeshFiles(const QStringList &pFileNames, bool pShowWarning = true);

void setSizes(const QIntList &pSizesHorizontal, const QIntList &pSizesVertical);

Expand Down Expand Up @@ -144,12 +144,14 @@ class CellMLZincMappingViewEditingWidget : public Core::Widget

CellMLZincMappingViewEditingModel *mVariableTreeModel;

QString mMeshFileName;
QStringList mZincMeshFileNames;

CellMLSupport::CellmlFile *mCellmlFile;

void populateTree();
void saveMapping(const QString &pFileName);
void openMapping(const QString &pFileName);

QString fileName(const QString &pFileName, const QString &pBaseFileName,
const QString &pFileExtension, const QString &pCaption,
const QStringList &pFileFilters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static const char *SettingsMeshFile = "MeshFile";

void CellMLZincMappingViewWidget::loadSettings(QSettings &pSettings)
{
static const QString DefaultMeshFileName = "/home/tuareg/Documents/OpenCOR/opencor/meshes/trilinearCube.exfile";
static const QStringList DefaultMeshFileNames = {};

static const QRect AvailableGeometry = qApp->primaryScreen()->availableGeometry();
static const int AvailableGeometryHeight = AvailableGeometry.height();
Expand All @@ -87,7 +87,7 @@ void CellMLZincMappingViewWidget::loadSettings(QSettings &pSettings)
mEditingWidgetHorizontalSizes = qVariantListToIntList(pSettings.value(SettingsEditingHoriontalSizes, DefaultEditingWidgetHorizontalSizes).toList());
mEditingWidgetVerticalSizes = qVariantListToIntList(pSettings.value(SettingsEditingVerticalSizes, DefaultEditingWidgetVerticalSizes).toList());

mMeshFileName = pSettings.value(SettingsMeshFile,DefaultMeshFileName).toString();
mZincMeshFileNames= pSettings.value(SettingsMeshFile,DefaultMeshFileNames).toStringList();
}

//==============================================================================
Expand All @@ -97,7 +97,7 @@ void CellMLZincMappingViewWidget::saveSettings(QSettings &pSettings) const
pSettings.setValue(SettingsEditingHoriontalSizes, qIntListToVariantList(mEditingWidgetHorizontalSizes));
pSettings.setValue(SettingsEditingVerticalSizes, qIntListToVariantList(mEditingWidgetVerticalSizes));

pSettings.setValue(SettingsMeshFile,mMeshFileName);
pSettings.setValue(SettingsMeshFile,mZincMeshFileNames);
}

//==============================================================================
Expand All @@ -111,7 +111,7 @@ void CellMLZincMappingViewWidget::initialize(const QString &pFileName)
if (mEditingWidget == nullptr) {
// No editing widget exists for the given file, so create one

mEditingWidget = new CellMLZincMappingViewEditingWidget(pFileName, mMeshFileName,this, this);
mEditingWidget = new CellMLZincMappingViewEditingWidget(pFileName, mZincMeshFileNames,this, this);

mEditingWidgets.insert(pFileName, mEditingWidget);

Expand Down Expand Up @@ -226,9 +226,9 @@ void CellMLZincMappingViewWidget::fileRenamed(const QString &pOldFileName, const

//==============================================================================

void CellMLZincMappingViewWidget::setDefaultMeshFile(const QString &pFileName)
void CellMLZincMappingViewWidget::setDefaultMeshFiles(const QStringList &pFileNames)
{
mMeshFileName = pFileName;
mZincMeshFileNames = pFileNames;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class CellMLZincMappingViewWidget : public Core::ViewWidget

//bool saveFile(const QString &pOldFileName, const QString &pNewFileName);

void setDefaultMeshFile(const QString &pFileName);
void setDefaultMeshFiles(const QStringList &pFileNames);

private:
QIntList mEditingWidgetHorizontalSizes;
Expand All @@ -85,7 +85,7 @@ class CellMLZincMappingViewWidget : public Core::ViewWidget
CellMLZincMappingViewEditingWidget* mEditingWidget = nullptr;
QMap<QString, CellMLZincMappingViewEditingWidget*> mEditingWidgets;

QString mMeshFileName;
QStringList mZincMeshFileNames;

private slots:
void EditingWidgetHorizontalSplitterMoved(const QIntList &pSizes);
Expand Down

0 comments on commit f125dad

Please sign in to comment.