Skip to content

Commit

Permalink
Merge 94de64f into c529ec9
Browse files Browse the repository at this point in the history
  • Loading branch information
LafCorentin committed Sep 2, 2020
2 parents c529ec9 + 94de64f commit f90033f
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 79 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 All @@ -115,10 +115,12 @@ CellMLZincMappingViewEditingWidget::CellMLZincMappingViewEditingWidget(const QSt
mOpenMeshFile = Core::newAction(QIcon(":/oxygen/actions/document-open.png"),
mToolBarWidget);

//TODO trash could be hidden when nothing to show
//TODO trash could be hidden when nothing selected
mClearNode = Core::newAction(QIcon(":/oxygen/actions/edit-clear.png"),
mToolBarWidget);

mOpenMappingFile = Core::newAction(QIcon(":/oxygen/actions/document-import.png"),
mToolBarWidget);
mNodeSizeWidget = new QwtWheel(mToolBarWidget);
mNodeSizeWidget->setBorderWidth(0);
mNodeSizeWidget->setFixedSize(int(0.07*availableGeometry.width()),
Expand All @@ -131,7 +133,9 @@ CellMLZincMappingViewEditingWidget::CellMLZincMappingViewEditingWidget(const QSt
mToolBarWidget->addAction(mSaveMapping);
mToolBarWidget->addAction(mOpenMeshFile);
mToolBarWidget->addAction(mClearNode);

mToolBarWidget->addWidget(mNodeSizeWidget);
mToolBarWidget->addAction(mOpenMappingFile);

mTopSeparator = Core::newLineWidget(this);

Expand Down Expand Up @@ -173,7 +177,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 All @@ -187,7 +191,9 @@ CellMLZincMappingViewEditingWidget::CellMLZincMappingViewEditingWidget(const QSt
connect(mSaveMapping, &QAction::triggered,
this, &CellMLZincMappingViewEditingWidget::saveMappingSlot);
connect(mOpenMeshFile, &QAction::triggered,
this, &CellMLZincMappingViewEditingWidget::loadMeshFile);
this, &CellMLZincMappingViewEditingWidget::openMeshFile);
connect(mOpenMappingFile, &QAction::triggered,
this, &CellMLZincMappingViewEditingWidget::openMappingFile);

//create and add informative labels

Expand Down Expand Up @@ -273,16 +279,29 @@ 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);
if (pFileNames.isEmpty()) {
return false;
}

for (QString fileName : pFileNames) {
QFileInfo check_file;
check_file.setFile(fileName);

if (!check_file.exists()) {
return false;
}
}

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

Expand Down Expand Up @@ -318,6 +337,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 @@ -341,10 +364,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 @@ -353,6 +378,10 @@ void CellMLZincMappingViewEditingWidget::dropEvent(QDropEvent *pEvent)
}
}

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

// Accept the proposed action for the event

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

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

//find a filename

Core::FileManager *fileManagerInstance = Core::FileManager::instance();
Expand All @@ -437,7 +464,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 @@ -462,6 +489,84 @@ 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 and the graph

for (int node : mMapMatch.keys()) {
mZincWidget->eraseNode(node);
}
mMapMatch.clear(); //should already be 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 @@ -537,9 +642,18 @@ void CellMLZincMappingViewEditingWidget::saveMappingSlot()
saveMapping({});
}

void CellMLZincMappingViewEditingWidget::loadMeshFile()
//==============================================================================

void CellMLZincMappingViewEditingWidget::openMappingFile()
{
openMapping(Core::getOpenFileName("Open mapping file"));
}

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

void CellMLZincMappingViewEditingWidget::openMeshFile()
{
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 @@ -101,7 +101,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 All @@ -127,6 +127,7 @@ class CellMLZincMappingViewEditingWidget : public Core::Widget
QAction *mClearNode;
QAction *mSaveMapping;
QAction *mOpenMeshFile;
QAction *mOpenMappingFile;

QFrame *mTopSeparator;
QFrame *mBottomSeparator;
Expand All @@ -146,12 +147,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 All @@ -162,7 +165,8 @@ private slots:
void emitHorizontalSplitterMoved();
void emitVerticalSplitterMoved();
void saveMappingSlot();
void loadMeshFile();
void openMeshFile();
void openMappingFile();
};

//==============================================================================
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 f90033f

Please sign in to comment.