cmake_minimum_required(VERSION 3.1)

project(Qwt)

set(${PROJECT_NAME}_MAJOR_VERSION 6)
set(${PROJECT_NAME}_MINOR_VERSION 1)
set(${PROJECT_NAME}_PATCH_VERSION 0)
set(${PROJECT_NAME}_VERSION ${${PROJECT_NAME}_MAJOR_VERSION}.${${PROJECT_NAME}_MINOR_VERSION}.${${PROJECT_NAME}_PATCH_VERSION})

set(QWT_MOC_HEADERS

  # General
  qwt_dyngrid_layout.h
  qwt_magnifier.h
  qwt_panner.h
  qwt_picker.h
  qwt_text_label.h

  # QwtPlot
  qwt_abstract_legend.h
  qwt_legend.h
  qwt_legend_label.h
  qwt_plot.h
  qwt_plot_renderer.h
  qwt_plot_canvas.h
  qwt_plot_panner.h
  qwt_plot_picker.h
  qwt_plot_zoomer.h
  qwt_plot_magnifier.h
  qwt_sampling_thread.h
  qwt_scale_widget.h

  # QwtOpenGL
  qwt_plot_glcanvas.h

  # QwtWidgets
  qwt_abstract_slider.h
  qwt_abstract_scale.h
  qwt_analog_clock.h
  qwt_compass.h
  qwt_counter.h
  qwt_dial.h
  qwt_knob.h
  qwt_slider.h
  qwt_thermo.h
  qwt_wheel.h
)

set(QWT_SOURCES

  # General
  qwt_abstract_scale_draw.cpp
  qwt_clipper.cpp
  qwt_color_map.cpp
  qwt_column_symbol.cpp
  qwt_date.cpp
  qwt_date_scale_draw.cpp
  qwt_date_scale_engine.cpp
  qwt_dyngrid_layout.cpp
  qwt_event_pattern.cpp
  qwt_graphic.cpp
  qwt_interval.cpp
  qwt_interval_symbol.cpp
  qwt_math.cpp
  qwt_magnifier.cpp
  qwt_null_paintdevice.cpp
  qwt_painter.cpp
  qwt_painter_command.cpp
  qwt_panner.cpp
  qwt_picker.cpp
  qwt_picker_machine.cpp
  qwt_pixel_matrix.cpp
  qwt_point_3d.cpp
  qwt_point_polar.cpp
  qwt_round_scale_draw.cpp
  qwt_scale_div.cpp
  qwt_scale_draw.cpp
  qwt_scale_map.cpp
  qwt_spline.cpp
  qwt_scale_engine.cpp
  qwt_symbol.cpp
  qwt_system_clock.cpp
  qwt_text_engine.cpp
  qwt_text_label.cpp
  qwt_text.cpp
  qwt_transform.cpp
  qwt_widget_overlay.cpp

  # QwtPlot
  qwt_curve_fitter.cpp
  qwt_abstract_legend.cpp
  qwt_legend.cpp
  qwt_legend_data.cpp
  qwt_legend_label.cpp
  qwt_plot.cpp
  qwt_plot_renderer.cpp
  qwt_plot_xml.cpp
  qwt_plot_axis.cpp
  qwt_plot_curve.cpp
  qwt_plot_dict.cpp
  qwt_plot_directpainter.cpp
  qwt_plot_grid.cpp
  qwt_plot_histogram.cpp
  qwt_plot_item.cpp
  qwt_plot_abstract_barchart.cpp
  qwt_plot_barchart.cpp
  qwt_plot_multi_barchart.cpp
  qwt_plot_intervalcurve.cpp
  qwt_plot_zoneitem.cpp
  qwt_plot_tradingcurve.cpp
  qwt_plot_spectrogram.cpp
  qwt_plot_spectrocurve.cpp
  qwt_plot_scaleitem.cpp
  qwt_plot_legenditem.cpp
  qwt_plot_seriesitem.cpp
  qwt_plot_shapeitem.cpp
  qwt_plot_marker.cpp
  qwt_plot_textlabel.cpp
  qwt_plot_layout.cpp
  qwt_plot_canvas.cpp
  qwt_plot_panner.cpp
  qwt_plot_rasteritem.cpp
  qwt_plot_picker.cpp
  qwt_plot_zoomer.cpp
  qwt_plot_magnifier.cpp
  qwt_plot_rescaler.cpp
  qwt_point_mapper.cpp
  qwt_raster_data.cpp
  qwt_matrix_raster_data.cpp
  qwt_sampling_thread.cpp
  qwt_series_data.cpp
  qwt_point_data.cpp
  qwt_scale_widget.cpp

  # QwtSvg
  qwt_plot_svgitem.cpp

  # QwtOpenGL
  qwt_plot_glcanvas.cpp

  # QwtWidgets
  qwt_abstract_slider.cpp
  qwt_abstract_scale.cpp
  qwt_arrow_button.cpp
  qwt_analog_clock.cpp
  qwt_compass.cpp
  qwt_compass_rose.cpp
  qwt_counter.cpp
  qwt_dial.cpp
  qwt_dial_needle.cpp
  qwt_knob.cpp
  qwt_slider.cpp
  qwt_thermo.cpp
  qwt_wheel.cpp

)

set(_qwt_moc_headers )
foreach(_header ${QWT_MOC_HEADERS})
  list(APPEND _qwt_moc_headers src/${_header})
endforeach()

set(_qwt_sources )
foreach(_source ${QWT_SOURCES})
  list(APPEND _qwt_sources src/${_source})
endforeach()

if(DESIRED_QT_VERSION MATCHES 4)
  find_package(Qt4 REQUIRED QtCore QtGui QtSvg QtOpenGL QtDesigner)
  include(${QT_USE_FILE})
  qt4_wrap_cpp(_qwt_sources ${_qwt_moc_headers})

  add_library(qwt SHARED ${_qwt_sources})
  target_link_libraries(qwt PUBLIC ${QT_LIBRARIES})
elseif(DESIRED_QT_VERSION MATCHES 5)
  find_package(Qt5Svg REQUIRED)
  find_package(Qt5OpenGL REQUIRED)
  find_package(Qt5PrintSupport REQUIRED)
  find_package(Qt5Concurrent REQUIRED)

  qt5_wrap_cpp(_qwt_sources ${_qwt_moc_headers})

  add_library(qwt SHARED ${_qwt_sources})
  target_link_libraries(qwt PUBLIC Qt5::Svg Qt5::OpenGL Qt5::PrintSupport Qt5::Concurrent)
endif()

target_compile_definitions(qwt PUBLIC QWT_DLL PRIVATE QWT_MAKEDLL)
set_target_properties(qwt PROPERTIES
                      SOVERSION ${${PROJECT_NAME}_VERSION}
)


# Build the designer plug-in

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)

set(_qwt_designer_sources
  designer/qwt_designer_plotdialog.cpp
  designer/qwt_designer_plugin.cpp
)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/plugins/designer)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/plugins/designer)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/plugins/designer)

if(DESIRED_QT_VERSION MATCHES 4)
  qt4_wrap_cpp(_qwt_designer_sources
    designer/qwt_designer_plugin.h
    designer/qwt_designer_plotdialog.h
  )
  qt4_add_resources(_qwt_designer_sources designer/qwt_designer_plugin.qrc)
  add_library(qwt_designer_plugin SHARED ${_qwt_designer_sources})
  target_link_libraries(qwt_designer_plugin qwt ${QT_LIBRARIES})
elseif(DESIRED_QT_VERSION MATCHES 5)
  find_package(Qt5Designer REQUIRED)
  include_directories(${Qt5Designer_INCLUDE_DIRS})
  qt5_wrap_cpp(_qwt_designer_sources
    designer/qwt_designer_plugin.h
    designer/qwt_designer_plotdialog.h
  )
  qt5_add_resources(_qwt_designer_sources designer/qwt_designer_plugin.qrc)

  add_library(qwt_designer_plugin SHARED ${_qwt_designer_sources})
  target_link_libraries(qwt_designer_plugin qwt)
  qt5_use_modules(qwt_designer_plugin Designer)
endif()

set_target_properties(qwt_designer_plugin PROPERTIES
                      SOVERSION ${${PROJECT_NAME}_VERSION}
                      COMPILE_DEFINITIONS QWT_DLL)

set(${PROJECT_NAME}_LIBRARIES qwt)

# Install support

install(TARGETS qwt_designer_plugin
  RUNTIME DESTINATION plugins/designer
  LIBRARY DESTINATION plugins/designer
)

install(TARGETS ${${PROJECT_NAME}_LIBRARIES} EXPORT ${PROJECT_NAME}_TARGETS
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
  INCLUDES DESTINATION include/${PROJECT_NAME}
)
install(DIRECTORY src/
  DESTINATION include/${PROJECT_NAME}
  FILES_MATCHING PATTERN "*.h"
)

# Config files

configure_file(
  ${PROJECT_NAME}Config.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
  @ONLY
)
configure_file(
  ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}ConfigVersion.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
  @ONLY
)

export(EXPORT ${PROJECT_NAME}_TARGETS
  FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake
)

set(config_package_location lib/cmake/${PROJECT_NAME})
install(EXPORT ${PROJECT_NAME}_TARGETS
  FILE ${PROJECT_NAME}Targets.cmake
  DESTINATION ${config_package_location}
)
install(FILES
    "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
    "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
  DESTINATION ${config_package_location}
)
