File Add-support-for-OpenBabel-3.x.patch of Package kalzium4

From 2b9ebd854d85fa1e092c766e149e16ac18019496 Mon Sep 17 00:00:00 2001
From: Antonio Rojas <arojas@archlinux.org>
Date: Wed, 6 Jan 2021 09:41:41 +0000
Subject: [PATCH] Add support for OpenBabel 3.x

---
 CMakeLists.txt                        | 24 ++++++++++++++++++++----
 compoundviewer/CMakeLists.txt         |  4 ++--
 compoundviewer/widgets/CMakeLists.txt |  2 +-
 config-kalzium.h.cmake                |  6 ++++++
 src/CMakeLists.txt                    | 16 ++++++++--------
 src/kalzium.cpp                       | 12 ++++++------
 src/kalziumschemetype.cpp             | 14 ++++++++++++--
 src/kalziumschemetype.h               |  2 +-
 src/main.cpp                          |  2 +-
 9 files changed, 57 insertions(+), 25 deletions(-)

diff --git a/KalziumConfigureChecks.cmake b/KalziumConfigureChecks.cmake
index 889bcfbc..96162801 100644
--- a/KalziumConfigureChecks.cmake
+++ b/KalziumConfigureChecks.cmake
@@ -3,7 +3,10 @@ include(CheckFunctionExists)
 
 macro_optional_find_package(OCaml)
 macro_optional_find_package(Libfacile)
-macro_optional_find_package(OpenBabel2)
+macro_optional_find_package(OpenBabel3 3.1)
+if(NOT ${OpenBabel3_FOUND})
+    macro_optional_find_package(OpenBabel2)
+endif()
 macro_optional_find_package(Eigen3)
 macro_optional_find_package(Avogadro 1.0.0 NO_MODULE)
 
@@ -13,10 +16,22 @@ check_include_files(ieeefp.h HAVE_IEEEFP_H)
 check_include_files(ieeefp.h HAVE_IEEEFP_H)
 
 macro_bool_to_01(LIBFACILE_FOUND HAVE_FACILE)
+macro_bool_to_01(OpenBabel3_FOUND HAVE_OPENBABEL3)
 macro_bool_to_01(OPENBABEL2_FOUND HAVE_OPENBABEL2)
+if(HAVE_OPENBABEL2 OR HAVE_OPENBABEL3)
+    set(HAVE_OPENBABEL 1)
+endif()
 macro_bool_to_01(EIGEN3_FOUND HAVE_EIGEN)
 macro_bool_to_01(Avogadro_FOUND HAVE_AVOGADRO)
 
+if(HAVE_OPENBABEL3)
+    set(OPENBABEL_INCLUDE_DIR ${OpenBabel3_INCLUDE_DIRS})
+    set(OPENBABEL_LIBRARIES ${OpenBabel3_LIBRARIES})
+elseif(HAVE_OPENBABEL2)
+    set(OPENBABEL_INCLUDE_DIR ${OPENBABEL2_INCLUDE_DIR})
+    set(OPENBABEL_LIBRARIES ${OPENBABEL2_LIBRARIES})
+endif()
+
 # at the end, output the configuration
 configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/config-kalzium.h.cmake
@@ -25,7 +28,7 @@ configure_file(
 
 macro_log_feature(OCAML_FOUND "OCaml" "OCaml is needed by Kalzium for the Equation Solver" "http://caml.inria.fr/" FALSE "" "")
 macro_log_feature(LIBFACILE_FOUND "LibFacile" "libfacile is needed by Kalzium for the Equation Solver." "http://www.recherche.enac.fr/log/facile/" FALSE "" "")
-macro_log_feature(OPENBABEL2_FOUND "OpenBabel2" "OpenBabel is needed by Kalzium for the 3D-molecule viewer" "http://openbabel.sourceforge.net/" FALSE "2.2" "")
+macro_log_feature(HAVE_OPENBABEL "OpenBabel" "OpenBabel is needed by Kalzium for the 3D-molecule viewer" "http://openbabel.sourceforge.net/" FALSE "2.2" "")
 macro_log_feature(EIGEN3_FOUND "Eigen3" "A generic C++ template library for dense and sparse matrices" "http://eigen.tuxfamily.org" FALSE "3.0.0" "")
 macro_log_feature(Avogadro_FOUND "Avogadro" "Advanced molecular editor" "http://avogadro.openmolecules.net" FALSE "1.0" "")
 macro_log_feature(CHEMICAL_MIME_DATA_FOUND "Chemical mime data" "Needed to open CML molecules from the file manager" "http://chemical-mime.sourceforge.net/" FALSE "" "")
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 889bcfbc..96162801 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -35,7 +35,7 @@ kde4_no_enable_final(kalzium)
 
 include(KalziumConfigureChecks.cmake)
 
-if (OPENBABEL2_FOUND AND Avogadro_FOUND AND EIGEN3_FOUND)
+if (HAVE_OPENBABEL AND Avogadro_FOUND AND EIGEN3_FOUND)
   # avoid compilerwarnings about redefinitions
   # todo: use check_function_exits() ?
   message(STATUS "Kalzium molecular editor enabled")
@@ -43,9 +43,9 @@ if (OPENBABEL2_FOUND AND AvogadroLibs_FOUND AND EIGEN3_FOUND)
     add_definitions(-DHAVE_SNPRINTF -DHAVE_STRCASECMP -DHAVE_STRNCASECMP)
   endif (WIN32)
   add_subdirectory(compoundviewer)
-else (OPENBABEL2_FOUND AND Avogadro_FOUND AND EIGEN3_FOUND)
+else (HAVE_OPENBABEL AND Avogadro_FOUND AND EIGEN3_FOUND)
   message(STATUS "Kalzium molecular editor disabled")
-endif (OPENBABEL2_FOUND AND Avogadro_FOUND AND EIGEN3_FOUND)
+endif (HAVE_OPENBABEL AND Avogadro_FOUND AND EIGEN3_FOUND)
 
 add_subdirectory(doc)
 add_subdirectory(src)
diff --git a/compoundviewer/CMakeLists.txt b/compoundviewer/CMakeLists.txt
index db367a7d..3fd2c42b 100644
--- a/compoundviewer/CMakeLists.txt
+++ b/compoundviewer/CMakeLists.txt
@@ -3,7 +3,7 @@
 include_directories(
   ${CMAKE_CURRENT_BINARY_DIR}/..
   ${EIGEN3_INCLUDE_DIR}
-  ${OPENBABEL2_INCLUDE_DIR}
+  ${OPENBABEL_INCLUDE_DIR}
   )
 
 set( compoundviewer_SRCS
@@ -16,7 +16,7 @@ target_link_libraries(compoundviewer
 kde4_add_library(compoundviewer SHARED ${compoundviewer_SRCS})
 
 target_link_libraries(compoundviewer
-   ${OPENBABEL2_LIBRARIES}
+   ${OPENBABEL_LIBRARIES}
    ${QT_QTOPENGL_LIBRARY}
    ${KDE4_KDEUI_LIBS}
    avogadro
diff --git a/compoundviewer/widgets/CMakeLists.txt b/compoundviewer/widgets/CMakeLists.txt
index 7658056c..a1e54204 100644
--- a/compoundviewer/widgets/CMakeLists.txt
+++ b/compoundviewer/widgets/CMakeLists.txt
@@ -11,6 +11,6 @@ kde4_add_widget_files(kalziumui_PART_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/kalziumui.
 
 kde4_add_plugin(kalziumuiwidgets ${kalziumui_PART_SRCS})
 
-target_link_libraries(kalziumuiwidgets ${OPENBABEL2_LIBRARIES} ${QT_QTDESIGNER_LIBRARY} ${KDE4_KDECORE_LIBS})
+target_link_libraries(kalziumuiwidgets ${OPENBABEL_LIBRARIES} ${QT_QTDESIGNER_LIBRARY} ${KDE4_KDECORE_LIBS})
 
 install(TARGETS kalziumuiwidgets DESTINATION ${PLUGIN_INSTALL_DIR}/plugins/designer)
diff --git a/config-kalzium.h.cmake b/config-kalzium.h.cmake
index f834d57d..99862809 100644
--- a/config-kalzium.h.cmake
+++ b/config-kalzium.h.cmake
@@ -4,6 +4,12 @@
 /* Define to 1 if we have OpenBabel2 */
 #cmakedefine HAVE_OPENBABEL2 1
 
+/* Define to 1 if we have OpenBabel3 */
+#cmakedefine HAVE_OPENBABEL3 1
+
+/* Define to 1 if we have OpenBabel */
+#cmakedefine HAVE_OPENBABEL 1
+
 /* Define 1 if we have Eigen */
 #cmakedefine HAVE_EIGEN 1
 
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 30c34133..a4cd86df 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -10,10 +10,10 @@ include_directories(
 
 ########### next target ###############
 
-# The tools can only be built when OpenBabel2 is found.
-# The moleculeviewer needs not only OpenBabel2 but also Eigen2 and Avogadro,
+# The tools can only be built when OpenBabel is found.
+# The moleculeviewer needs not only OpenBabel but also Eigen and Avogadro,
 # so there is a nested if-check.
-if (OPENBABEL2_FOUND)
+if (HAVE_OPENBABEL)
    set(kalziumtools_SRCS
       tools/obconverter.cpp
       )
@@ -25,7 +25,7 @@ if (OPENBABEL2_FOUND)
    kde4_add_ui_files(kalziumtools_SRCS
       tools/obconverterwidget.ui
       )
-   include_directories(${OPENBABEL2_INCLUDE_DIR})
+   include_directories(${OPENBABEL_INCLUDE_DIR})
 
    if (EIGEN3_FOUND AND Avogadro_FOUND)
 
@@ -37,7 +37,7 @@ if (OPENBABEL2_FOUND)
          )
       include_directories(${EIGEN3_INCLUDE_DIR})
    endif (EIGEN3_FOUND AND Avogadro_FOUND)
-endif (OPENBABEL2_FOUND)
+endif (HAVE_OPENBABEL)
 
 ## Kalzium calculator files
 set(kalziumtools_SRCS
@@ -135,12 +135,12 @@ target_link_libraries(kalzium
 
 target_link_libraries(kalzium ${KDE4_KHTML_LIBRARY} ${KDE4_KNEWSTUFF3_LIBS} ${KDE4_KUNITCONVERSION_LIBS} ${QT_QTSCRIPT_LIBRARY} ${QT_QTDECLARATIVE_LIBRARY} science)
 
-if (OPENBABEL2_FOUND)
-   target_link_libraries(kalzium ${OPENBABEL2_LIBRARIES})
+if (HAVE_OPENBABEL)
+   target_link_libraries(kalzium ${OPENBABEL_LIBRARIES})
    if (EIGEN3_FOUND AND Avogadro_FOUND)
       target_link_libraries(kalzium ${QT_QTOPENGL_LIBRARY} compoundviewer avogadro)
    endif (EIGEN3_FOUND AND Avogadro_FOUND)
-endif (OPENBABEL2_FOUND)
+endif (HAVE_OPENBABEL)
 
 if (LIBFACILE_FOUND)
    link_directories(${OCAMLC_DIR})
diff --git a/src/kalzium.cpp b/src/kalzium.cpp
index 065780ea..4de4c8fa 100644
--- a/src/kalzium.cpp
+++ b/src/kalzium.cpp
@@ -39,7 +39,7 @@
 #include "eqchemview.h"
 #endif
 
-#ifdef HAVE_OPENBABEL2
+#ifdef HAVE_OPENBABEL
 #if defined(HAVE_EIGEN) && defined(HAVE_AVOGADRO)
 #include "tools/moleculeview.h"
 #endif
@@ -203,7 +203,7 @@ void Kalzium::setupActions()
     m_pOBConverterAction->setIcon(KIcon("edit-copy"));
     m_pOBConverterAction->setWhatsThis(i18nc("WhatsThis Help", "With this tool, you can convert files containing chemical data between various file formats."));
     connect(m_pOBConverterAction, SIGNAL(triggered()), this, SLOT(slotOBConverter()));
-#ifndef HAVE_OPENBABEL2
+#ifndef HAVE_OPENBABEL
     m_pOBConverterAction->setEnabled(false);
 #endif
 
@@ -212,7 +212,7 @@ void Kalzium::setupActions()
     m_pMoleculesviewer->setIcon(KIcon("kalzium_molviewer"));
     m_pMoleculesviewer->setWhatsThis(i18nc("WhatsThis Help", "This tool allows you to view and edit 3D molecular structures."));
     connect(m_pMoleculesviewer, SIGNAL(triggered()), this, SLOT(slotMoleculeviewer()));
-#if !defined(HAVE_OPENBABEL2) || !defined(HAVE_EIGEN) || !defined(HAVE_AVOGADRO)
+#if !defined(HAVE_OPENBABEL) || !defined(HAVE_EIGEN) || !defined(HAVE_AVOGADRO)
     m_pMoleculesviewer->setEnabled(false);
 #endif
 
@@ -367,7 +367,7 @@ void Kalzium::slotRS()
 
 void Kalzium::slotOBConverter()
 {
-#ifdef HAVE_OPENBABEL2
+#ifdef HAVE_OPENBABEL
     KOpenBabel * d = new KOpenBabel(this);
     d->setAttribute(Qt::WA_DeleteOnClose);
     d->show();
@@ -376,7 +376,7 @@ void Kalzium::slotOBConverter()
 
 MoleculeDialog *Kalzium::slotMoleculeviewer()
 {
-#if defined(HAVE_OPENBABEL2) && defined(HAVE_EIGEN) && defined(HAVE_AVOGADRO)
+#if defined(HAVE_OPENBABEL) && defined(HAVE_EIGEN) && defined(HAVE_AVOGADRO)
 
     if (!QGLFormat::hasOpenGL()) {
         KMessageBox::error(0, i18n("This system does not support OpenGL."), i18n("Kalzium Error"));
@@ -638,7 +638,7 @@ Kalzium::~Kalzium()
 
 void Kalzium::loadMolecule(const QString &moleculeFile)
 {
-#if defined(HAVE_OPENBABEL2) && defined(HAVE_EIGEN) && defined(HAVE_AVOGADRO)
+#if defined(HAVE_OPENBABEL) && defined(HAVE_EIGEN) && defined(HAVE_AVOGADRO)
     MoleculeDialog *d = slotMoleculeviewer();
     if (d) {
         d->loadMolecule(moleculeFile);
diff --git a/src/kalziumschemetype.cpp b/src/kalziumschemetype.cpp
index bf516bc7..acb39acf 100644
--- a/src/kalziumschemetype.cpp
+++ b/src/kalziumschemetype.cpp
@@ -25,6 +25,9 @@
 #ifdef HAVE_OPENBABEL2
 #include <openbabel/mol.h>
 #endif
+#ifdef HAVE_OPENBABEL3
+#include <openbabel/elements.h>
+#endif
 
 #include <QBrush>
 
@@ -40,7 +43,7 @@ KalziumSchemeTypeFactory::KalziumSchemeTypeFactory()
     m_schemes << KalziumIconicSchemeType::instance();
     m_schemes << KalziumFamilySchemeType::instance();
     m_schemes << KalziumGroupsSchemeType::instance();
-    #ifdef HAVE_OPENBABEL2
+    #ifdef HAVE_OPENBABEL
     m_schemes << KalziumColorSchemeType::instance();
     #endif
 }
@@ -380,7 +383,7 @@ QList<legendPair> KalziumGroupsSchemeType::legendItems() const
     return ll;
 }
 
-#ifdef HAVE_OPENBABEL2
+#ifdef HAVE_OPENBABEL
 ///OpenBabel Color///
 KalziumColorSchemeType::KalziumColorSchemeType()
         : KalziumSchemeType()
@@ -407,8 +410,15 @@ QBrush KalziumColorSchemeType::elementBrush(int el) const
 {
     QColor c;
 
+#ifdef HAVE_OPENBABEL2
     std::vector<double> color = OpenBabel::etab.GetRGB(el);
     c.setRgbF(color[0], color[1], color[2]);
+#endif
+#ifdef HAVE_OPENBABEL3
+    double red, green, blue;
+    OpenBabel::OBElements::GetRGB(el, &red, &green, &blue);
+    c.setRgbF(red, green, blue);
+#endif
 
     return QBrush(c);
 }
diff --git a/src/kalziumschemetype.h b/src/kalziumschemetype.h
index c4bb4085..e1a91e10 100644
--- a/src/kalziumschemetype.h
+++ b/src/kalziumschemetype.h
@@ -230,7 +230,7 @@ private:
     KalziumGroupsSchemeType();
 };
 
-#ifdef HAVE_OPENBABEL2
+#ifdef HAVE_OPENBABEL
 /**
  * The scheme for color
  *
diff --git a/src/main.cpp b/src/main.cpp
index c1bfd59c..e0686716 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -75,7 +75,7 @@ int main(int argc, char **argv)
     KCmdLineArgs::init(argc, argv, &about);
 
     KCmdLineOptions options;
-#if defined(HAVE_OPENBABEL2) && defined(HAVE_EIGEN) && defined(HAVE_AVOGADRO)
+#if defined(HAVE_OPENBABEL) && defined(HAVE_EIGEN) && defined(HAVE_AVOGADRO)
     options.add("molecule <file>", ki18n("Open the given molecule file"));
 #endif
     KCmdLineArgs::addCmdLineOptions(options);
-- 
GitLab

openSUSE Build Service is sponsored by