Skip to content

Commit

Permalink
Merge pull request NIRALUser#38 from jcfr/do-not-use-gpl-widget-in-sl…
Browse files Browse the repository at this point in the history
…icer

Do not use gpl widget in slicer
  • Loading branch information
juanprietob authored Nov 16, 2018
2 parents 8568528 + bf3ac57 commit 6de7a04
Show file tree
Hide file tree
Showing 12 changed files with 597 additions and 219 deletions.
10 changes: 9 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ set(CXX_FILES
gradientArrow.cxx
gradientWidgetQT.cxx
ShapePopulationBase.cxx
ShapePopulationColorMapIO.cxx
ShapePopulationData.cxx
ShapePopulationGradientWidgetQT.cxx
ShapePopulationMainWindowQT.cxx
ShapePopulationQT.cxx
vtkPVPostFilter.cxx
Expand All @@ -47,6 +49,7 @@ set(QT_WRAP
CSVloaderQT.h
customizeColorMapByDirectionDialogQT.h
gradientWidgetQT.h
ShapePopulationGradientWidgetQT.h
ShapePopulationMainWindowQT.h
ShapePopulationQT.h
)
Expand All @@ -58,7 +61,11 @@ set(QT_QRC_FILES
# --- LIBRARIES ---------------------------------------------------------------------------
if(ShapePopulationViewer_QT_VERSION VERSION_EQUAL "4")
QT4_WRAP_UI(UISrcs ${UI_FILES})
QT4_WRAP_CPP(MOCSrcs ${QT_WRAP})
set(_moc_options)
if(ShapePopulationViewer_BUILD_SLICER_EXTENSION)
set(_moc_options OPTIONS -DShapePopulationViewer_BUILD_SLICER_EXTENSION)
endif()
QT4_WRAP_CPP(MOCSrcs ${QT_WRAP} ${_moc_options})
QT4_ADD_RESOURCES(QRCrcs ${QT_QRC_FILES})
else()
QT5_WRAP_UI(UISrcs ${UI_FILES})
Expand All @@ -74,6 +81,7 @@ add_library(${LOCAL_PROJECT_NAME}Widget STATIC
target_link_libraries(${LOCAL_PROJECT_NAME}Widget PUBLIC
${QT_LIBRARIES}
${VTK_LIBRARIES}
$<$<BOOL:${ShapePopulationViewer_BUILD_SLICER_EXTENSION}>:CTKVisualizationVTKWidgets>
)

if(ShapePopulationViewer_BUILD_SLICER_EXTENSION)
Expand Down
208 changes: 208 additions & 0 deletions src/ShapePopulationColorMapIO.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@

#include "ShapePopulationColorMapIO.h"

// Qt includes
#include <QFile>
#include <QFileInfo>
#include <QMessageBox>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QWidget>

// STD includes
#include <sstream>

namespace ShapePopulationColorMapIO
{

bool getXmlReaderValue(QXmlStreamReader * a_xmlReader, double * a_colorPointValue)
{
a_xmlReader->readNext();
if(a_xmlReader->tokenType() != QXmlStreamReader::Characters) return false;

double value = a_xmlReader->text().toString().toDouble();
if(value < 0.0 || value > 1.0)
{
std::ostringstream strs;
std::string str;
strs << "Error with value column " << a_xmlReader->columnNumber() << std::endl
<< "Value = " << value << " while should be between 0 and 1";
str = strs.str();
QMessageBox::critical(0,"XML Reader error",QString(str.c_str()),QMessageBox::Ok);
return false;
}

*a_colorPointValue = value;
return true;
}

void loadColorPointList(QString a_filePath, std::vector<colorPointStruct> * a_colorPointList, QWidget* parent)
{
QFile file(a_filePath);

if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
/* If we can't open it, let's show an error message. */
QMessageBox::critical(parent,"XML Reader error","Couldn't open .spvcm",QMessageBox::Ok);
return;
}

/* QXmlStreamReader takes any QIODevice. */
QXmlStreamReader xmlReader(&file);
std::vector<colorPointStruct> colorPointList;

/* We'll parse the XML until we reach end of it.*/
while(!xmlReader.atEnd() && !xmlReader.hasError())
{
/* Read next element.*/
QXmlStreamReader::TokenType token = xmlReader.readNext();

/* If token is just StartDocument, we'll go to next.*/
if(token == QXmlStreamReader::StartDocument) continue;

/* If token is StartElement, we'll see if we can read it.*/
if(token == QXmlStreamReader::StartElement)
{
/* If it's named colormap, we'll go to the next.*/
if(xmlReader.name() == "colormap") continue;

/* If it's named colorpoint, we'll dig the information from there.*/
if(xmlReader.name() == "colorpoint")
{
colorPointStruct ColorPoint;
xmlReader.readNext();

while(!(xmlReader.tokenType() == QXmlStreamReader::EndElement && xmlReader.name() == "colorpoint"))
{
if(xmlReader.tokenType() == QXmlStreamReader::StartElement)
{
/* We've found position. */
if(xmlReader.name() == "position")
{
getXmlReaderValue(&xmlReader,&ColorPoint.pos);
}
/* We've found Red value. */
if(xmlReader.name() == "R")
{
getXmlReaderValue(&xmlReader,&ColorPoint.r);
}
/* We've found Green value. */
if(xmlReader.name() == "G")
{
getXmlReaderValue(&xmlReader,&ColorPoint.g);
}
/* We've found Blue value. */
if(xmlReader.name() == "B")
{
getXmlReaderValue(&xmlReader,&ColorPoint.b);
}
}
/* ...and next... */
xmlReader.readNext();
}
/* Add the point to the colorMap */
colorPointList.push_back(ColorPoint);
}
}
}

/* Error handling. */
if(xmlReader.hasError())
{
QMessageBox::critical(parent,"XML Reader error",xmlReader.errorString(), QMessageBox::Ok);
return;
}

/* Removes any device() or data from the reader and resets its internal state to the initial state. */
xmlReader.clear();

/* Load the colormap */
*a_colorPointList = colorPointList;
}

void saveColorPointList(QString a_filePath, const std::vector<colorPointStruct>& colorPointList, QWidget* parent)
{
//write XML File
QFile file(a_filePath);
QFileInfo fileInfo(a_filePath);
if (!file.open(QIODevice::WriteOnly))
{
/* show error message if not able to open file */
QMessageBox::warning(parent, "Read only", "The file is in read only mode");
}
else
{
std::ostringstream strs;
std::string str;

QXmlStreamWriter* xmlWriter = new QXmlStreamWriter();
xmlWriter->setAutoFormatting(true);
xmlWriter->setDevice(&file);

xmlWriter->writeStartDocument();

/* SPVColorMap */
xmlWriter->writeStartElement("SPVColorMap");
xmlWriter->writeAttribute("description", "ShapePopulationViewer Color Map configuration");
xmlWriter->writeAttribute("version", "1.0");

/* ColorMap */
xmlWriter->writeStartElement("colormap");
xmlWriter->writeAttribute("name", fileInfo.baseName());
strs.str(""); strs.clear();
strs << colorPointList.size() ;
str = strs.str();
xmlWriter->writeAttribute("points", QString(str.c_str()));

for(unsigned int i = 0 ; i < colorPointList.size() ; i++ )
{
/* ColorPoint */
xmlWriter->writeStartElement("colorpoint");
strs.str(""); strs.clear();
strs << i ;
str = strs.str();
xmlWriter->writeAttribute("index", QString(str.c_str()));

/* Position */
xmlWriter->writeStartElement("position");
strs.str(""); strs.clear();
strs << colorPointList[i].pos ;
str = strs.str();
xmlWriter->writeCharacters(QString(str.c_str()));
xmlWriter->writeEndElement();

/* Red */
xmlWriter->writeStartElement("R");
strs.str(""); strs.clear();
strs << colorPointList[i].r ;
str = strs.str();
xmlWriter->writeCharacters(QString(str.c_str()));
xmlWriter->writeEndElement();

/* Green */
xmlWriter->writeStartElement("G");
strs.str(""); strs.clear();
strs << colorPointList[i].g ;
str = strs.str();
xmlWriter->writeCharacters(QString(str.c_str()));
xmlWriter->writeEndElement();

/* Blue */
xmlWriter->writeStartElement("B");
strs.str(""); strs.clear();
strs << colorPointList[i].b ;
str = strs.str();
xmlWriter->writeCharacters(QString(str.c_str()));
xmlWriter->writeEndElement();

xmlWriter->writeEndElement();
}
xmlWriter->writeEndElement();
xmlWriter->writeEndElement();

xmlWriter->writeEndDocument();
delete xmlWriter;
}
}

}
22 changes: 22 additions & 0 deletions src/ShapePopulationColorMapIO.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef ShapePopulationColorMapIO_h
#define ShapePopulationColorMapIO_h

// ShapePopulationViewer includes
#include "ShapePopulationStruct.h"

// Qt includes
#include <QString>
class QWidget;

// STD includes
#include <vector>

namespace ShapePopulationColorMapIO
{
void loadColorPointList(QString a_filePath, std::vector<colorPointStruct> * a_colorPointList, QWidget* parent=0);

void saveColorPointList(QString a_filePath, const std::vector<colorPointStruct>& colorPointList, QWidget* parent=0);
}


#endif
Loading

0 comments on commit 6de7a04

Please sign in to comment.