Quellcode durchsuchen

reworking serialization

Nicolas Winkler vor 4 Jahren
Ursprung
Commit
591629f9cb

+ 21 - 21
libalmond/CMakeLists.txt

@@ -38,27 +38,27 @@ target_link_libraries(libalmond PUBLIC ${FFMPEG_LIBRARIES})
 if (PNG_FOUND AND LIBALMOND_LIBPNG)
     target_link_libraries(libalmond PUBLIC PNG::PNG)
     target_compile_definitions(libalmond PUBLIC WITH_LIBPNG)
-elseif(LIBALMOND_LIBPNG)
-    set(PNG_BUILD_ZLIB ON CACHE BOOL "build zlib ourselves")
-    add_subdirectory(zlib-1.2.11)
-    set(ZLIB_LIBRARY zlibstatic)
-    foreach(header ${ZLIB_PUBLIC_HDRS})
-        get_filename_component(the_incluude ${header} DIRECTORY)
-        list(APPEND ZLIB_PUB_INCLUDE ${the_incluude})
-    endforeach()
-    set(ZLIB_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/zlib-1.2.11 ${ZLIB_PUB_INCLUDE} )
-    
-    set(SKIP_INSTALL_ALL ON)
-    add_subdirectory(lpng1637)
-    foreach(header ${libpng_public_hdrs})
-        get_filename_component(the_incluude ${header} DIRECTORY)
-        list(APPEND PNG_PUB_INCLUDE ${the_incluude})
-    endforeach()
-    #target_link_libraries(png_static PRIVATE zlibstatic)
-    target_include_directories(libalmond PRIVATE ${PNG_PUB_INCLUDE})
-    target_include_directories(libalmond PRIVATE ${ZLIB_INCLUDE_DIR})
-    target_link_libraries(libalmond PRIVATE png_static)
-    target_compile_definitions(libalmond PUBLIC WITH_LIBPNG)
+#elseif(LIBALMOND_LIBPNG)
+#    set(PNG_BUILD_ZLIB ON CACHE BOOL "build zlib ourselves")
+#    add_subdirectory(zlib-1.2.11)
+#    set(ZLIB_LIBRARY zlibstatic)
+#    foreach(header ${ZLIB_PUBLIC_HDRS})
+#        get_filename_component(the_incluude ${header} DIRECTORY)
+#        list(APPEND ZLIB_PUB_INCLUDE ${the_incluude})
+#    endforeach()
+#    set(ZLIB_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/zlib-1.2.11 ${ZLIB_PUB_INCLUDE} )
+#    
+#    set(SKIP_INSTALL_ALL ON)
+#    add_subdirectory(lpng1637)
+#    foreach(header ${libpng_public_hdrs})
+#        get_filename_component(the_incluude ${header} DIRECTORY)
+#        list(APPEND PNG_PUB_INCLUDE ${the_incluude})
+#    endforeach()
+#    #target_link_libraries(png_static PRIVATE zlibstatic)
+#    target_include_directories(libalmond PRIVATE ${PNG_PUB_INCLUDE})
+#    target_include_directories(libalmond PRIVATE ${ZLIB_INCLUDE_DIR})
+#    target_link_libraries(libalmond PRIVATE png_static)
+#    target_compile_definitions(libalmond PUBLIC WITH_LIBPNG)
 endif()
 
 if (JPEG_FOUND AND LIBALMOND_LIBJPEG)

+ 23 - 9
libalmond/include/Serialize.h

@@ -9,24 +9,38 @@
 
 namespace tinyxml2
 {
-    struct XMLElement;
+    class XMLElement;
+    class XMLDocument;
 }
 
+
 namespace alm
 {
     struct ImageView;
 
-    Gradient deserializeGradient(tinyxml2::XMLElement* xml);
-    std::unique_ptr<tinyxml2::XMLElement> serializeGradient(const Gradient& g);
 
-    Gradient loadGradient(const std::string& xml);
-    std::string saveGradient(const Gradient& g);
+    template<typename T>
+    tinyxml2::XMLElement* serialize(tinyxml2::XMLDocument& doc, const T&);
+
+    template<typename T>
+    std::string toXml(const T&);
+
+    template<typename T>
+    T deserialize(tinyxml2::XMLElement* xml);
+
+    template<typename T>
+    T fromXml(const std::string& xml);
 
-    ImageView deserializeImageView(tinyxml2::XMLElement* xml);
-    std::unique_ptr<tinyxml2::XMLElement> serializeImageView(const ImageView& iv);
 
-    ImageView loadImageView(const std::string& xml);
-    std::string saveImageView(const ImageView& iv);
+    // specializations
+    template<>
+    tinyxml2::XMLElement* serialize<Gradient>(tinyxml2::XMLDocument& doc, const Gradient&);
+    template<>
+    std::string toXml<Gradient>(const Gradient&);
+    template<>
+    Gradient deserialize<Gradient>(tinyxml2::XMLElement* xml);
+    template<>
+    Gradient fromXml<Gradient>(const std::string& xml);
 }
 
 

+ 15 - 7
libalmond/src/Serialize.cpp

@@ -15,7 +15,8 @@ using tinyxml2::XMLNode;
 using tinyxml2::XML_SUCCESS;
 
 
-alm::Gradient alm::deserializeGradient(XMLElement* elem)
+template<>
+alm::Gradient alm::deserialize<alm::Gradient>(XMLElement* elem)
 {
     if (elem == nullptr)
         throw alm::XmlException{ "invalid root node" };
@@ -51,17 +52,27 @@ alm::Gradient alm::deserializeGradient(XMLElement* elem)
 }
 
 
-alm::Gradient alm::loadGradient(const std::string& xml)
+
+template<>
+tinyxml2::XMLElement* alm::serialize<alm::Gradient>(tinyxml2::XMLDocument& doc, const alm::Gradient&)
+{
+    return nullptr;
+}
+
+
+template<>
+alm::Gradient alm::fromXml<alm::Gradient>(const std::string& xml)
 {
     XMLDocument xmlDoc;
     XMLError err = xmlDoc.Parse(xml.c_str());
     if (err != XML_SUCCESS)
         throw alm::XmlException{ "error parsing gradient xml" };
-    return deserializeGradient(xmlDoc.RootElement());
+    return deserialize<Gradient>(xmlDoc.RootElement());
 }
 
 
-std::string alm::saveGradient(const Gradient& g)
+template<>
+std::string alm::toXml<alm::Gradient>(const alm::Gradient& g)
 {
     std::stringstream buf;
 
@@ -79,6 +90,3 @@ std::string alm::saveGradient(const Gradient& g)
     return buf.str();
 }
 
-
-ImageView deserializeImageView(tinyxml2::XMLElement* xml);
-std::unique_ptr<tinyxml2::XMLElement> serializeImageView(const ImageView& iv);

+ 0 - 2
libalmond/tinyxml2/tinyxml2.cpp

@@ -971,8 +971,6 @@ XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
 }
 
 
-
-
 const XMLElement* XMLNode::FirstChildElement( const char* name ) const
 {
     for( const XMLNode* node = _firstChild; node; node = node->_next ) {

+ 1 - 0
resources/Almond.qrc

@@ -14,6 +14,7 @@
         <file alias="grayscale">gradients/grayscale.xml</file>
         <file alias="peach">gradients/peach.xml</file>
         <file alias="blue gold">gradients/blue_gold.xml</file>
+        <file alias="element">gradients/element.xml</file>
     </qresource>
     <qresource prefix="/about">
         <file alias="about_en">about_en.html</file>

+ 22 - 0
resources/gradients/element.xml

@@ -0,0 +1,22 @@
+<gradient max="350" repeat="false" version="1.0.0" >
+    <color r="49" g="49" b="49" p="0" />
+    <color r="128" g="132" b="95" p="11.054" />
+    <color r="243" g="255" b="0" p="21.4653" />
+    <color r="255" g="0" b="4" p="38.0463" />
+    <color r="129" g="41" b="0" p="53.599" />
+    <color r="36" g="18" b="0" p="64.653" />
+    <color r="0" g="45" b="0" p="97.2393" />
+    <color r="66" g="97" b="66" p="108.896" />
+    <color r="255" g="230" b="0" p="122.086" />
+    <color r="99" g="0" b="1" p="142.434" />
+    <color r="255" g="174" b="176" p="161.8" />
+    <color r="254" g="233" b="234" p="175.44" />
+    <color r="97" g="0" b="0" p="190.491" />
+    <color r="255" g="178" b="83" p="204.601" />
+    <color r="5" g="0" b="72" p="233.483" />
+    <color r="185" g="224" b="233" p="255.077" />
+    <color r="255" g="107" b="2" p="287.018" />
+    <color r="75" g="0" b="0" p="302.314" />
+    <color r="154" g="72" b="6" p="326.157" />
+    <color r="49" g="49" b="49" p="350" />
+</gradient>

+ 5 - 0
src/FractalWidget.cpp

@@ -108,8 +108,13 @@ void FractalWidget::mouseReleaseEvent(QMouseEvent* me)
 void FractalWidget::wheelEvent(QWheelEvent* we)
 {
     QOpenGLWidget::wheelEvent(we);
+#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
     float x = float(we->position().x()) / this->width();
     float y = float(we->position().y()) / this->height();
+#else
+    float x = float(we->x()) / this->width();
+    float y = float(we->y()) / this->height();
+#endif
     float scale = ::powf(0.9975f, we->angleDelta().y());
     //mandelInfo.view.zoom(scale, x, y);
     zoom(scale, x, y);

+ 3 - 2
src/GradientMenu.cpp

@@ -15,6 +15,7 @@ const QString GradientMenu::presetNames[] = {
     "blue gold",
     "clouds",
     "oldschool",
+    "element",
     "grayscale",
     "peach",
     "rainbow"
@@ -72,7 +73,7 @@ void GradientMenu::loadGradient(QFile& file)
     if (file.isOpen() || file.open(QFile::ReadOnly)) {
         QString xml = QString::fromUtf8(file.readAll());
         try {
-            ui->gradientWidget->setGradient(alm::loadGradient(xml.toStdString()));
+            ui->gradientWidget->setGradient(alm::fromXml<alm::Gradient>(xml.toStdString()));
         } catch (alm::XmlException& xmlex) {
             QMessageBox::critical(this, tr("Error Loading Gradient"), tr("Error loading gradient: ") + xmlex.what());
         } catch (...) {
@@ -102,7 +103,7 @@ void GradientMenu::on_presetCmb_currentIndexChanged(int index)
 
 void GradientMenu::on_saveBtn_clicked()
 {
-    std::string xml = alm::saveGradient(ui->gradientWidget->getGradient());
+    std::string xml = alm::toXml(ui->gradientWidget->getGradient());
     QString filename =
             QFileDialog::getSaveFileName(this, tr("Save Gradient"), "", "Gradient XML Files (*.xml)");
     if (!filename.isNull()) {