├── src ├── icon.rc ├── pattern-ipc.h ├── pattern-color.h ├── pattern-export.h ├── pattern-misc.h ├── pattern-json.h ├── mingw.h ├── pattern-plot.h ├── pattern-ui-plot.h ├── pattern-ui-dialogs.h ├── pattern-import.h ├── CMakeLists.txt ├── pattern-ui.h ├── version.h ├── pattern-data.h ├── pattern-color.c ├── pattern-signal.h ├── pattern-ui-window.h ├── pattern.h ├── mingw.c ├── main.c ├── pattern-export.c ├── pattern-misc.c ├── pattern-data.c ├── pattern-ui-plot.c ├── pattern-ipc.c ├── pattern-import.c ├── pattern-signal.c ├── pattern.c ├── pattern-ui-dialogs.c ├── pattern-json.c ├── pattern-ui-window.c └── pattern-plot.c ├── antpatt.png ├── icons ├── antpatt.ico ├── 16x16 │ └── apps │ │ └── antpatt.png ├── 22x22 │ └── apps │ │ └── antpatt.png ├── 24x24 │ └── apps │ │ └── antpatt.png ├── 32x32 │ └── apps │ │ └── antpatt.png ├── 48x48 │ └── apps │ │ └── antpatt.png ├── icons.xml └── scalable │ └── apps │ └── antpatt.svg ├── examples ├── 2x5el-1016.antp.gz ├── 2x5el-1019.antp.gz ├── 2x5el-1033.antp.gz ├── 2x5el-1063.antp.gz ├── 2x5el-1071.antp.gz ├── 2x5el-1078.antp.gz ├── 2x5el-877.antp.gz ├── 2x5el-879.antp.gz ├── 2x5el-881.antp.gz ├── 2x5el-922.antp.gz ├── 2x5el-939.antp.gz ├── 2x5el-943.antp.gz ├── 2x5el-961.antp.gz ├── 2x5el-973.antp.gz ├── 2x5el-981.antp.gz ├── 2x5el-989.antp.gz ├── 2x5el-997.antp.gz └── mmanagal-export.png ├── antpatt.desktop ├── CMakeLists.txt ├── README.md └── LICENSE /src/icon.rc: -------------------------------------------------------------------------------- 1 | 1 ICON "..\\icons\\antpatt.ico" 2 | -------------------------------------------------------------------------------- /antpatt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/antpatt.png -------------------------------------------------------------------------------- /icons/antpatt.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/icons/antpatt.ico -------------------------------------------------------------------------------- /examples/2x5el-1016.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-1016.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-1019.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-1019.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-1033.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-1033.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-1063.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-1063.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-1071.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-1071.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-1078.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-1078.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-877.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-877.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-879.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-879.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-881.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-881.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-922.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-922.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-939.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-939.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-943.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-943.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-961.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-961.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-973.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-973.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-981.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-981.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-989.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-989.antp.gz -------------------------------------------------------------------------------- /examples/2x5el-997.antp.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/2x5el-997.antp.gz -------------------------------------------------------------------------------- /examples/mmanagal-export.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/examples/mmanagal-export.png -------------------------------------------------------------------------------- /icons/16x16/apps/antpatt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/icons/16x16/apps/antpatt.png -------------------------------------------------------------------------------- /icons/22x22/apps/antpatt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/icons/22x22/apps/antpatt.png -------------------------------------------------------------------------------- /icons/24x24/apps/antpatt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/icons/24x24/apps/antpatt.png -------------------------------------------------------------------------------- /icons/32x32/apps/antpatt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/icons/32x32/apps/antpatt.png -------------------------------------------------------------------------------- /icons/48x48/apps/antpatt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kkonradpl/antpatt/HEAD/icons/48x48/apps/antpatt.png -------------------------------------------------------------------------------- /antpatt.desktop: -------------------------------------------------------------------------------- 1 | [Desktop Entry] 2 | Version=1.0 3 | Type=Application 4 | Name=antpatt 5 | Comment=Antenna pattern plotting and analysis software 6 | Icon=antpatt 7 | Exec=antpatt 8 | Terminal=false 9 | Categories=Science; 10 | StartupNotify=false 11 | -------------------------------------------------------------------------------- /icons/icons.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | icons/16x16/apps/antpatt.png 5 | icons/22x22/apps/antpatt.png 6 | icons/24x24/apps/antpatt.png 7 | icons/32x32/apps/antpatt.png 8 | icons/48x48/apps/antpatt.png 9 | icons/scalable/apps/antpatt.svg 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/pattern-ipc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2022 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_IPC_H_ 17 | #define ANTPATT_PATTERN_IPC_H_ 18 | 19 | void pattern_ipc_init(pattern_t*); 20 | void pattern_ipc_cleanup(void); 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /src/pattern-color.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_COLOR_H_ 17 | #define ANTPATT_PATTERN_COLOR_H_ 18 | 19 | GdkRGBA pattern_color_next(void); 20 | gchar* pattern_color_to_string(const GdkRGBA*); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /src/pattern-export.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2022 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_EXPORT_H_ 17 | #define ANTPATT_PATTERN_EXPORT_H_ 18 | #include "pattern-data.h" 19 | 20 | gboolean pattern_export(pattern_data_t*, const gchar*); 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /src/pattern-misc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2022 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_MISC_H_ 17 | #define ANTPATT_PATTERN_MISC_H_ 18 | 19 | gchar* pattern_misc_format_frequency(gint); 20 | gchar* pattern_misc_info_all(pattern_t*, gdouble); 21 | 22 | #endif -------------------------------------------------------------------------------- /src/pattern-json.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2022 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_JSON_H_ 17 | #define ANTPATT_PATTERN_JSON_H_ 18 | 19 | gboolean pattern_json_load(pattern_t*, const gchar*, gchar**); 20 | gboolean pattern_json_save(pattern_t*, const gchar*, gboolean); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /src/mingw.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2022 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_MINGW_H_ 17 | #define ANTPATT_MINGW_H_ 18 | 19 | void mingw_init(void); 20 | void mingw_cleanup(void); 21 | 22 | gchar* strsep(gchar**, const gchar*); 23 | 24 | gboolean mingw_uri_signal(GtkWidget*, gchar*, gpointer); 25 | void mingw_realize(GtkWidget*, gpointer); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /src/pattern-plot.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_PLOT_H_ 17 | #define ANTPATT_PATTERN_PLOT_H_ 18 | 19 | #define PATTERN_PLOT_BASE_SIZE 500.0 20 | #define PATTERN_PLOT_OFFSET 32.0 21 | #define PATTERN_PLOT_BORDER_WIDTH 1.0 22 | 23 | void pattern_plot(cairo_t*, pattern_t*); 24 | gboolean pattern_plot_to_file(const gchar*, pattern_t*); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /src/pattern-ui-plot.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_UI_PLOT_H_ 17 | #define ANTPATT_PATTERN_UI_PLOT_H_ 18 | 19 | gboolean pattern_ui_plot(GtkWidget*, cairo_t*, pattern_ui_t*); 20 | 21 | gboolean pattern_ui_plot_motion(GtkWidget*, GdkEventMotion*, pattern_ui_t*); 22 | gboolean pattern_ui_plot_click(GtkWidget*, GdkEventButton*, pattern_ui_t*); 23 | gboolean pattern_ui_plot_leave(GtkWidget*, GdkEvent*, pattern_ui_t*); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/pattern-ui-dialogs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2022 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_UI_DIALOGS_H_ 17 | #define ANTPATT_PATTERN_UI_DIALOGS_H_ 18 | 19 | void pattern_ui_dialog(GtkWindow*, GtkMessageType, gchar*, gchar*, ...); 20 | gboolean pattern_ui_dialog_yesno(GtkWindow*, const gchar*, const gchar*); 21 | gint pattern_ui_dialog_ask_unsaved(GtkWindow*); 22 | 23 | gchar* pattern_ui_dialog_open(GtkWindow*); 24 | gchar* pattern_ui_dialog_save(GtkWindow*); 25 | GSList* pattern_ui_dialog_import(GtkWindow*); 26 | gchar* pattern_ui_dialog_render(GtkWindow*); 27 | gchar* pattern_ui_dialog_export(GtkWindow*); 28 | void pattern_ui_dialog_about(GtkWindow*); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/pattern-import.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2022 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_IMPORT_H_ 17 | #define ANTPATT_PATTERN_IMPORT_H_ 18 | #include "pattern-signal.h" 19 | 20 | enum 21 | { 22 | PATTERN_IMPORT_OK = 0, 23 | PATTERN_IMPORT_ERROR, 24 | PATTERN_IMPORT_INVALID_FORMAT, 25 | PATTERN_IMPORT_EMPTY_FILE 26 | }; 27 | 28 | typedef struct pattern_import pattern_import_t; 29 | 30 | pattern_import_t* pattern_import_new(void); 31 | void pattern_import_free(pattern_import_t*, gboolean); 32 | gint pattern_import(pattern_import_t*, const gchar*); 33 | 34 | pattern_signal_t* pattern_import_get_signal(pattern_import_t*); 35 | const gchar* pattern_import_get_name(pattern_import_t*); 36 | gint pattern_import_get_freq(pattern_import_t*); 37 | 38 | #endif 39 | 40 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.6) 2 | 3 | set(SOURCE_FILES 4 | main.c 5 | pattern.c 6 | pattern.h 7 | pattern-color.c 8 | pattern-color.h 9 | pattern-data.c 10 | pattern-data.h 11 | pattern-export.c 12 | pattern-export.h 13 | pattern-import.c 14 | pattern-import.h 15 | pattern-ipc.c 16 | pattern-ipc.h 17 | pattern-json.c 18 | pattern-json.h 19 | pattern-misc.c 20 | pattern-misc.h 21 | pattern-plot.c 22 | pattern-plot.h 23 | pattern-signal.c 24 | pattern-signal.h 25 | pattern-ui-dialogs.c 26 | pattern-ui-dialogs.h 27 | pattern-ui-plot.c 28 | pattern-ui-plot.h 29 | pattern-ui-window.c 30 | pattern-ui-window.h 31 | pattern-ui.c 32 | pattern-ui.h 33 | version.h 34 | ${CMAKE_BINARY_DIR}/resources.c) 35 | 36 | set(SOURCE_FILES_MINGW 37 | icon.rc 38 | mingw.c 39 | mingw.h) 40 | 41 | set(LIBRARIES 42 | ${GTK_LIBRARIES} 43 | ${GSL_LIBRARIES} 44 | ${JSON-C_LIBRARIES} 45 | ${ZLIB_LIBRARIES}) 46 | 47 | if(MINGW) 48 | IF(NOT (CMAKE_BUILD_TYPE MATCHES Debug)) 49 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mwindows") 50 | ENDIF() 51 | set(CMAKE_RC_COMPILER_INIT windres) 52 | ENABLE_LANGUAGE(RC) 53 | SET(CMAKE_RC_COMPILE_OBJECT " -i -o ") 54 | add_executable(antpatt ${SOURCE_FILES} ${SOURCE_FILES_MINGW}) 55 | ELSE() 56 | add_executable(antpatt ${SOURCE_FILES}) 57 | ENDIF() 58 | 59 | target_include_directories(antpatt PRIVATE ${CMAKE_BINARY_DIR}) 60 | target_link_libraries(antpatt ${LIBRARIES}) 61 | -------------------------------------------------------------------------------- /src/pattern-ui.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_UI_H_ 17 | #define ANTPATT_PATTERN_UI_H_ 18 | 19 | typedef struct pattern pattern_t; 20 | typedef struct pattern_ui pattern_ui_t; 21 | 22 | pattern_ui_t* pattern_ui(pattern_t*); 23 | 24 | void pattern_ui_sync_name(pattern_ui_t*, gboolean); 25 | void pattern_ui_sync_freq(pattern_ui_t*, gboolean); 26 | void pattern_ui_sync_avg(pattern_ui_t*, gboolean); 27 | void pattern_ui_sync_color(pattern_ui_t*, gboolean); 28 | void pattern_ui_sync_hide(pattern_ui_t*, gboolean); 29 | void pattern_ui_sync_fill(pattern_ui_t*, gboolean); 30 | void pattern_ui_sync_rev(pattern_ui_t*, gboolean); 31 | void pattern_ui_sync_data(pattern_ui_t*); 32 | 33 | pattern_t* pattern_ui_get_pattern(pattern_ui_t*); 34 | GtkWindow* pattern_ui_get_plot_window(pattern_ui_t*); 35 | 36 | void pattern_ui_set_focus_idx(pattern_ui_t*, gint); 37 | gint pattern_ui_get_focus_idx(const pattern_ui_t*); 38 | void pattern_ui_set_rotating_idx(pattern_ui_t*, gint); 39 | gint pattern_ui_get_rotating_idx(const pattern_ui_t*); 40 | 41 | void pattern_ui_interactive(pattern_ui_t*, gboolean); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.6) 2 | project(antpatt) 3 | 4 | find_package(PkgConfig REQUIRED) 5 | 6 | pkg_check_modules(GTK REQUIRED gtk+-3.0) 7 | include_directories(${GTK_INCLUDE_DIRS}) 8 | link_directories(${GTK_LIBRARY_DIRS}) 9 | add_definitions(${GTK_CFLAGS_OTHER}) 10 | 11 | pkg_check_modules(GSL REQUIRED gsl) 12 | include_directories(${GSL_INCLUDE_DIRS}) 13 | link_directories(${GSL_LIBRARY_DIRS}) 14 | add_definitions(${GSL_CFLAGS_OTHER}) 15 | 16 | pkg_check_modules(JSON-C REQUIRED json-c) 17 | include_directories(${JSON-C_INCLUDE_DIRS}) 18 | link_directories(${JSON-C_LIBRARY_DIRS}) 19 | add_definitions(${JSON-C_CFLAGS_OTHER}) 20 | 21 | pkg_check_modules(ZLIB REQUIRED zlib) 22 | include_directories(${ZLIB_INCLUDE_DIRS}) 23 | link_directories(${ZLIB_LIBRARY_DIRS}) 24 | add_definitions(${ZLIB_CFLAGS_OTHER}) 25 | 26 | find_program(GLIB_COMPILE_RESOURCES NAMES glib-compile-resources REQUIRED) 27 | execute_process(COMMAND ${GLIB_COMPILE_RESOURCES} --generate-source --sourcedir=${CMAKE_SOURCE_DIR} --target=${CMAKE_BINARY_DIR}/resources.c ${CMAKE_SOURCE_DIR}/icons/icons.xml) 28 | execute_process(COMMAND ${GLIB_COMPILE_RESOURCES} --generate-header --sourcedir=${CMAKE_SOURCE_DIR} --target=${CMAKE_BINARY_DIR}/resources.h ${CMAKE_SOURCE_DIR}/icons/icons.xml) 29 | 30 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wno-overlength-strings") 31 | 32 | add_subdirectory(src) 33 | 34 | if(NOT MINGW) 35 | install(TARGETS antpatt DESTINATION bin) 36 | install(FILES antpatt.desktop DESTINATION share/applications) 37 | install(DIRECTORY icons/ DESTINATION share/icons/hicolor 38 | FILES_MATCHING 39 | PATTERN "antpatt\.png" 40 | PATTERN "antpatt\.svg") 41 | install(CODE "execute_process(COMMAND gtk-update-icon-cache /usr/share/icons/hicolor)") 42 | endif() 43 | -------------------------------------------------------------------------------- /src/version.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2024 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_VERSION_H_ 17 | #define ANTPATT_VERSION_H_ 18 | 19 | #define APP_TITLE "Antenna pattern" 20 | #define APP_TITLE_PLOT "Plot" 21 | #define APP_NAME "antpatt" 22 | #define APP_ICON "antpatt" 23 | #define APP_VERSION "1.1.1" 24 | #define APP_COPYRIGHT "2017-2024" 25 | #define APP_FILE_EXT ".antp" 26 | #define APP_FILE_COMPRESS ".gz" 27 | 28 | #define APP_LICENCE \ 29 | "This program is free software; you can redistribute it and/or\n" \ 30 | "modify it under the terms of the GNU General Public License\n" \ 31 | "as published by the Free Software Foundation; either version 2\n" \ 32 | "of the License, or (at your option) any later version.\n" \ 33 | "\n" \ 34 | "This program is distributed in the hope that it will be useful,\n" \ 35 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \ 36 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" \ 37 | "GNU General Public License for more details.\n" \ 38 | "\n" \ 39 | "You should have received a copy of the GNU General Public License\n" \ 40 | "along with this program. If not, see http://www.gnu.org/licenses/\n" 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /src/pattern-data.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2022 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_DATA_H_ 17 | #define ANTPATT_PATTERN_DATA_H_ 18 | #include "pattern-signal.h" 19 | 20 | #define PATTERN_DATA_MIN_FREQ 0 21 | #define PATTERN_DATA_MAX_FREQ 99999999 22 | 23 | typedef struct pattern_data pattern_data_t; 24 | 25 | pattern_data_t* pattern_data_new(pattern_signal_t*); 26 | void pattern_data_free(pattern_data_t*); 27 | 28 | gboolean pattern_data_changed(const pattern_data_t*); 29 | void pattern_data_unchanged(pattern_data_t*); 30 | 31 | pattern_signal_t* pattern_data_get_signal(pattern_data_t*); 32 | const gchar* pattern_data_get_name(const pattern_data_t*); 33 | void pattern_data_set_name(pattern_data_t*, const gchar*); 34 | gint pattern_data_get_freq(const pattern_data_t*); 35 | void pattern_data_set_freq(pattern_data_t*, gint); 36 | const GdkRGBA* pattern_data_get_color(const pattern_data_t*); 37 | void pattern_data_set_color(pattern_data_t*, const GdkRGBA*); 38 | gboolean pattern_data_get_hide(const pattern_data_t*); 39 | void pattern_data_set_hide(pattern_data_t*, gboolean); 40 | gboolean pattern_data_get_fill(const pattern_data_t*); 41 | void pattern_data_set_fill(pattern_data_t*, gboolean); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /src/pattern-color.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | #define HUE_INITIAL 0.558 20 | #define HUE_STEP 0.618 21 | 22 | static void hsv2rgb(gdouble, gdouble, gdouble, GdkRGBA*); 23 | 24 | GdkRGBA 25 | pattern_color_next() 26 | { 27 | static gdouble h = HUE_INITIAL; 28 | GdkRGBA color; 29 | 30 | h -= HUE_STEP; 31 | if (h < 0.0) 32 | h += 1.0; 33 | 34 | hsv2rgb(h, 1.0, 1.0, &color); 35 | return color; 36 | } 37 | 38 | gchar* 39 | pattern_color_to_string(const GdkRGBA *color) 40 | { 41 | g_assert(color != NULL); 42 | return g_strdup_printf("#%02X%02X%02X", 43 | (uint8_t)round(color->red*255), 44 | (uint8_t)round(color->green*255), 45 | (uint8_t)round(color->blue*255)); 46 | } 47 | 48 | static void 49 | hsv2rgb(gdouble h, 50 | gdouble s, 51 | gdouble v, 52 | GdkRGBA *rgb) 53 | { 54 | gint i = (gint)(h * 6.0); 55 | gdouble f = h * 6.0 - i; 56 | gdouble p = v * (1.0 - s); 57 | gdouble q = v * (1.0 - f * s); 58 | gdouble t = v * (1.0 - (1.0 - f) * s); 59 | 60 | switch (i % 6) 61 | { 62 | case 0: 63 | rgb->red = v; 64 | rgb->green = t; 65 | rgb->blue = p; 66 | break; 67 | case 1: 68 | rgb->red = q; 69 | rgb->green = v; 70 | rgb->blue = p; 71 | break; 72 | case 2: 73 | rgb->red = p; 74 | rgb->green = v; 75 | rgb->blue = t; 76 | break; 77 | case 3: 78 | rgb->red = p; 79 | rgb->green = q; 80 | rgb->blue = v; 81 | break; 82 | case 4: 83 | rgb->red = t; 84 | rgb->green = p; 85 | rgb->blue = v; 86 | break; 87 | case 5: 88 | rgb->red = v; 89 | rgb->green = p; 90 | rgb->blue = q; 91 | break; 92 | } 93 | 94 | rgb->alpha = 1.0; 95 | } 96 | -------------------------------------------------------------------------------- /src/pattern-signal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2022 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_SIGNAL_H_ 17 | #define ANTPATT_PATTERN_SIGNAL_H_ 18 | 19 | #define PATTERN_SIGNAL_MIN_AVG 0 20 | #define PATTERN_SIGNAL_MAX_AVG 10 21 | 22 | enum 23 | { 24 | PATTERN_INTERP_LINEAR = 0, 25 | PATTERN_INTERP_AKIMA, 26 | PATTERN_INTERP_AKIMA_CLIPPED, 27 | PATTERN_INTERP_N 28 | }; 29 | 30 | typedef struct pattern_signal pattern_signal_t; 31 | 32 | pattern_signal_t* pattern_signal_new(void); 33 | void pattern_signal_free(pattern_signal_t *s); 34 | 35 | gboolean pattern_signal_changed(const pattern_signal_t*); 36 | void pattern_signal_unchanged(pattern_signal_t*); 37 | 38 | gint pattern_signal_count(const pattern_signal_t*); 39 | gint pattern_signal_interp(const pattern_signal_t*); 40 | void pattern_signal_push(pattern_signal_t*, gdouble); 41 | 42 | gdouble pattern_signal_get_sample(const pattern_signal_t*, gint); 43 | gdouble pattern_signal_get_sample_raw(const pattern_signal_t*, gint); 44 | gdouble pattern_signal_get_sample_interp(pattern_signal_t*, gint, gdouble); 45 | 46 | gdouble pattern_signal_get_min(const pattern_signal_t*); 47 | gdouble pattern_signal_get_peak(const pattern_signal_t*); 48 | void pattern_signal_set_peak(pattern_signal_t*, gdouble); 49 | 50 | gboolean pattern_signal_get_rev(const pattern_signal_t*); 51 | void pattern_signal_set_rev(pattern_signal_t*, gboolean); 52 | 53 | gint pattern_signal_get_avg(const pattern_signal_t*); 54 | void pattern_signal_set_avg(pattern_signal_t*, gint); 55 | 56 | gint pattern_signal_get_interp(const pattern_signal_t*); 57 | void pattern_signal_set_interp(pattern_signal_t*, gint); 58 | 59 | gboolean pattern_signal_get_finished(const pattern_signal_t*); 60 | void pattern_signal_set_finished(pattern_signal_t*); 61 | 62 | gint pattern_signal_get_rotate(const pattern_signal_t*); 63 | void pattern_signal_set_rotate(pattern_signal_t*, gint); 64 | void pattern_signal_rotate(pattern_signal_t*, gint); 65 | void pattern_signal_rotate_0(pattern_signal_t*); 66 | void pattern_signal_rotate_reset(pattern_signal_t*); 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /src/pattern-ui-window.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_UI_WINDOW_H_ 17 | #define ANTPATT_PATTERN_UI_WINDOW_H_ 18 | 19 | enum 20 | { 21 | PATTERN_UI_SCALE_ARRL = 0, 22 | PATTERN_UI_SCALE_LINEAR_20, 23 | PATTERN_UI_SCALE_LINEAR_30, 24 | PATTERN_UI_SCALE_LINEAR_40, 25 | PATTERN_UI_SCALE_LINEAR_50, 26 | PATTERN_UI_SCALE_LINEAR_60 27 | }; 28 | 29 | struct pattern_ui_window 30 | { 31 | GtkWidget *window; 32 | GtkWidget *window_plot; 33 | GtkWidget *box; 34 | 35 | GtkWidget *box_buttons; 36 | GtkWidget *box_buttons_main; 37 | GtkWidget *b_new; 38 | GtkWidget *b_load; 39 | GtkWidget *b_save; 40 | GtkWidget *b_save_as; 41 | GtkWidget *b_render; 42 | GtkWidget *b_detach; 43 | GtkWidget *b_about; 44 | 45 | GtkWidget *box_header1; 46 | GtkWidget *l_title, *e_title; 47 | GtkWidget *l_size, *s_size; 48 | GtkWidget *c_scale; 49 | GtkWidget *l_line, *s_line; 50 | 51 | GtkWidget *box_header2; 52 | GtkWidget *l_interp, *c_interp; 53 | GtkWidget *b_full_angle; 54 | GtkWidget *b_black; 55 | GtkWidget *b_normalize; 56 | GtkWidget *b_legend; 57 | 58 | GtkWidget *box_plot; 59 | GtkWidget *plot; 60 | GtkWidget *separator; 61 | 62 | GtkWidget *box_select; 63 | GtkWidget *b_add; 64 | GtkWidget *b_down; 65 | GtkWidget *b_up; 66 | GtkCellRenderer *r_select; 67 | GtkWidget *c_select; 68 | GtkWidget *b_export; 69 | GtkWidget *b_remove; 70 | GtkWidget *b_clear; 71 | 72 | GtkWidget *box_edit1; 73 | GtkWidget *l_name, *e_name; 74 | GtkWidget *l_freq, *s_freq; 75 | GtkWidget *l_avg, *s_avg; 76 | GtkWidget *b_color; 77 | GtkWidget *b_color_next; 78 | 79 | GtkWidget *box_edit2; 80 | GtkWidget *b_rotate_reset; 81 | GtkWidget *b_rotate_ccw_fast; 82 | GtkWidget *b_rotate_ccw; 83 | GtkWidget *b_rotate_peak; 84 | GtkWidget *b_rotate_cw; 85 | GtkWidget *b_rotate_cw_fast; 86 | GtkWidget *b_fill; 87 | GtkWidget *b_rev; 88 | GtkWidget *b_hide; 89 | }; 90 | 91 | struct pattern_ui_window* pattern_ui_window_new(void); 92 | void pattern_ui_window_set_title(struct pattern_ui_window*, const gchar*); 93 | 94 | #endif 95 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | antpatt 2 | ======= 3 | 4 | Antenna pattern plotting and analysis software. 5 | 6 | ![Screenshot](/antpatt.png?raw=true) 7 | 8 | Copyright (C) 2017-2024 Konrad Kosmatka 9 | 10 | This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 11 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 | 13 | # Build 14 | In order to build antpatt you will need: 15 | - CMake 16 | - C compiler 17 | 18 | You will also need several dependencies: 19 | - GTK+ 3 & dependencies 20 | - GSL 21 | - JSON-C 22 | - zlib 23 | 24 | Once you have all the necessary dependencies, you can use scripts available in the `build` directory. 25 | 26 | For Windows builds (binaries), see the [Releases](https://github.com/kkonradpl/antpatt/releases) page. To enable dark theme on Windows, run `antpatt.exe` with `-d` command line parameter.  27 | 28 | # Installing 29 | 30 | After a successful build, just use: 31 | ```sh 32 | $ sudo make install 33 | ``` 34 | in the `build` directory. This will install both the executable file `antpatt` and icons. 35 | 36 | # Supported data formats 37 | 38 | - Radiomobile – `ANT` 39 | - MMANA-GAL – `CSV` 40 | - Planet – `MSI` 41 | - XDR-GTK (legacy) – `XDRP` 42 | 43 | The whole project can be saved as `.antp.gz` file (compressed `.antp`) which is simply a JSON file with all settings included and data samples embedded. See `examples` directory. 44 | 45 | # Data from MMANA-GAL 46 | 47 | MMANA-GAL can export CSV files that antpatt accepts. Use the following settings in MMANA-GAL: File → Table of Angle/Gain (*.csv) dialog to export the CSV: 48 | 49 | ![Screenshot](/examples/mmanagal-export.png?raw=true) 50 | 51 | # Interactive console mode 52 | 53 | Interactive console mode (`-i` command line option) can be used for data streaming from another application for real-time antenna radiation pattern plotting. Commands consist of a single word and are case insensitive: 54 | 55 | - `START` – create new measurement 56 | - `STOP` – close current measurement 57 | - `PUSH ` – add signal level sample 58 | - `NAME ` – set plot name 59 | - `FREQ ` – set plot frequency [kHz] 60 | - `COLOR ` – set plot color (#XXXXXX) 61 | - `AVG ` – set plot moving-average 62 | - `FILL ` – set plot fill 63 | - `REV ` – set plot reverse mode 64 | 65 | Antpatt will send the following responses: 66 | 67 | - `READY` – after application startup 68 | - `BYE` – before application exit 69 | - `OK` – after a successful command 70 | - `ERROR` – after an incorrect command -------------------------------------------------------------------------------- /src/pattern.h: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #ifndef ANTPATT_PATTERN_H_ 17 | #define ANTPATT_PATTERN_H_ 18 | #include "version.h" 19 | #include "pattern-data.h" 20 | 21 | #define PATTERN_MIN_SIZE 350 22 | #define PATTERN_MAX_SIZE 3000 23 | 24 | #define PATTERN_MIN_LINE 0.1 25 | #define PATTERN_MAX_LINE 2.0 26 | 27 | enum 28 | { 29 | PATTERN_COL_DATA = 0, 30 | PATTERN_COLS 31 | }; 32 | 33 | typedef struct pattern pattern_t; 34 | typedef struct pattern_ui pattern_ui_t; 35 | 36 | pattern_t* pattern_new(void); 37 | void pattern_free(pattern_t*); 38 | 39 | gboolean pattern_changed(const pattern_t*); 40 | void pattern_unchanged(pattern_t*); 41 | void pattern_reset(pattern_t*); 42 | 43 | GtkListStore* pattern_get_model(pattern_t*); 44 | void pattern_set_ui(pattern_t*, pattern_ui_t*); 45 | pattern_ui_t* pattern_get_ui(pattern_t*); 46 | 47 | void pattern_add(pattern_t*, pattern_data_t*); 48 | void pattern_remove(pattern_t*, GtkTreeIter*); 49 | void pattern_clear(pattern_t*); 50 | 51 | void pattern_set_current(pattern_t*, pattern_data_t*); 52 | pattern_data_t* pattern_get_current(pattern_t*); 53 | 54 | void pattern_set_size(pattern_t*, gint); 55 | gint pattern_get_size(const pattern_t*); 56 | void pattern_set_title(pattern_t*, const gchar*); 57 | const gchar* pattern_get_title(const pattern_t*); 58 | void pattern_set_scale(pattern_t*, gint); 59 | gint pattern_get_scale(const pattern_t*); 60 | void pattern_set_line(pattern_t*, gdouble); 61 | gdouble pattern_get_line(const pattern_t*); 62 | void pattern_set_interp(pattern_t*, gint); 63 | gint pattern_get_interp(const pattern_t*); 64 | void pattern_set_full_angle(pattern_t*, gboolean); 65 | gboolean pattern_get_full_angle(const pattern_t*); 66 | void pattern_set_black(pattern_t*, gboolean); 67 | gboolean pattern_get_black(const pattern_t*); 68 | void pattern_set_normalize(pattern_t*, gboolean); 69 | gboolean pattern_get_normalize(const pattern_t*); 70 | void pattern_set_legend(pattern_t*, gboolean); 71 | gboolean pattern_get_legend(const pattern_t*); 72 | 73 | void pattern_set_filename(pattern_t*, const gchar*); 74 | const gchar* pattern_get_filename(const pattern_t*); 75 | 76 | gint pattern_get_visible_count(const pattern_t*); 77 | gdouble pattern_get_peak(const pattern_t*); 78 | 79 | void pattern_hide(pattern_t*, pattern_data_t*, gboolean); 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /src/mingw.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #define _WIN32_WINNT 0x0500 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #define MINGW_FONT_FILE ".\\share\\fonts\\TTF\\DejaVuSansMono.ttf" 23 | 24 | static gint mingw_font = 0; 25 | static const char css_string[] = 26 | "* {\n" 27 | " font-family: Sans;\n" 28 | " font-size: 10pt;\n" 29 | "}\n"; 30 | 31 | static void mingw_dark_titlebar(GtkWidget*); 32 | 33 | 34 | void 35 | mingw_init(void) 36 | { 37 | mingw_font = AddFontResourceEx(MINGW_FONT_FILE, FR_PRIVATE, NULL); 38 | 39 | GtkCssProvider *provider = gtk_css_provider_new(); 40 | gtk_css_provider_load_from_data(provider, css_string, -1, NULL); 41 | GdkScreen *screen = gdk_display_get_default_screen(gdk_display_get_default()); 42 | gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); 43 | } 44 | 45 | void 46 | mingw_cleanup(void) 47 | { 48 | if (mingw_font) 49 | RemoveFontResourceEx(MINGW_FONT_FILE, FR_PRIVATE, NULL); 50 | } 51 | 52 | gchar* 53 | strsep(gchar **string, 54 | const gchar *del) 55 | { 56 | gchar *start = *string; 57 | gchar *p = (start ? strpbrk(start, del) : NULL); 58 | 59 | if (p == NULL) 60 | { 61 | *string = NULL; 62 | } 63 | else 64 | { 65 | *p = '\0'; 66 | *string = p + 1; 67 | } 68 | return start; 69 | } 70 | 71 | gboolean 72 | mingw_uri_signal(GtkWidget *label, 73 | gchar *uri, 74 | gpointer data) 75 | { 76 | ShellExecute(0, "open", uri, NULL, NULL, 1); 77 | return TRUE; 78 | } 79 | 80 | void 81 | mingw_realize(GtkWidget *widget, 82 | gpointer user_data) 83 | { 84 | gboolean dark_theme = FALSE; 85 | 86 | g_object_get(gtk_settings_get_default(), 87 | "gtk-application-prefer-dark-theme", 88 | &dark_theme, NULL); 89 | 90 | if (dark_theme) 91 | mingw_dark_titlebar(widget); 92 | } 93 | 94 | static void 95 | mingw_dark_titlebar(GtkWidget *widget) 96 | { 97 | const DWORD dark_mode = 20; 98 | const DWORD dark_mode_pre20h1 = 19; 99 | const BOOL value = TRUE; 100 | GdkWindow *window = gtk_widget_get_window(widget); 101 | 102 | if (window == NULL) 103 | return; 104 | 105 | HWND handle = GDK_WINDOW_HWND(window); 106 | if (!SUCCEEDED(DwmSetWindowAttribute(handle, dark_mode, &value, sizeof(value)))) 107 | DwmSetWindowAttribute(handle, dark_mode_pre20h1, &value, sizeof(value)); 108 | } 109 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include 18 | #include "pattern.h" 19 | #include "pattern-ui.h" 20 | #include "pattern-ipc.h" 21 | #include "pattern-json.h" 22 | #include "resources.h" 23 | #ifdef G_OS_WIN32 24 | #include "mingw.h" 25 | #endif 26 | 27 | typedef struct antpatt_arg 28 | { 29 | gboolean interactive; 30 | const char *project; 31 | gboolean dark_theme; 32 | } antpatt_arg_t; 33 | 34 | static antpatt_arg_t args = 35 | { 36 | .interactive = FALSE, 37 | .project = NULL, 38 | .dark_theme = FALSE 39 | }; 40 | 41 | static void 42 | antpatt_usage(void) 43 | { 44 | printf("antpatt " APP_VERSION " - antenna pattern plotting and analysis software\n"); 45 | printf("usage: antpatt [-i] [-d] project\n"); 46 | printf("options:\n"); 47 | printf(" -i interactive console mode\n"); 48 | printf(" -d prefer dark theme\n"); 49 | } 50 | 51 | static void 52 | parse_args(gint argc, 53 | gchar *argv[]) 54 | { 55 | gint c; 56 | while ((c = getopt(argc, argv, "hid")) != -1) 57 | { 58 | switch (c) 59 | { 60 | case 'h': 61 | antpatt_usage(); 62 | exit(0); 63 | 64 | case 'i': 65 | args.interactive = TRUE; 66 | break; 67 | 68 | case 'd': 69 | args.dark_theme = TRUE; 70 | 71 | default: 72 | break; 73 | } 74 | } 75 | 76 | if (optind == argc - 1) 77 | args.project = argv[optind]; 78 | } 79 | 80 | gint 81 | main(gint argc, 82 | gchar *argv[]) 83 | { 84 | pattern_t *p = pattern_new(); 85 | gchar *error = NULL; 86 | 87 | gtk_disable_setlocale(); 88 | gtk_init(&argc, &argv); 89 | parse_args(argc, argv); 90 | 91 | g_resources_register(icons_get_resource()); 92 | gtk_icon_theme_add_resource_path(gtk_icon_theme_get_default(), "/org/antpatt/icons"); 93 | 94 | #ifdef G_OS_WIN32 95 | mingw_init(); 96 | #endif 97 | 98 | if (args.dark_theme) 99 | g_object_set(gtk_settings_get_default(), "gtk-application-prefer-dark-theme", TRUE, NULL); 100 | 101 | if (args.project) 102 | { 103 | if (!pattern_json_load(p, args.project, &error)) 104 | { 105 | fprintf(stderr, "Error: %s\n", error); 106 | g_free(error); 107 | } 108 | } 109 | 110 | pattern_ui(p); 111 | 112 | if (args.interactive) 113 | pattern_ipc_init(p); 114 | 115 | gtk_main(); 116 | 117 | #ifdef G_OS_WIN32 118 | mingw_cleanup(); 119 | #endif 120 | 121 | if (args.interactive) 122 | pattern_ipc_cleanup(); 123 | 124 | pattern_free(p); 125 | return 0; 126 | } 127 | -------------------------------------------------------------------------------- /src/pattern-export.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2022-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include 18 | #include "pattern-export.h" 19 | 20 | static gboolean pattern_export_xdrp(pattern_data_t*, FILE*); 21 | static gboolean pattern_export_ant(pattern_data_t*, FILE*); 22 | 23 | static gboolean write_to_file(const char*, FILE*); 24 | 25 | 26 | gboolean 27 | pattern_export(pattern_data_t *data, 28 | const gchar *filename) 29 | { 30 | gboolean ret; 31 | 32 | g_assert(data != NULL); 33 | g_assert(filename != NULL); 34 | 35 | FILE *fp = g_fopen(filename, "w"); 36 | if (fp == NULL) 37 | return FALSE; 38 | 39 | gchar *ext = strrchr(filename, '.'); 40 | if (ext && g_ascii_strcasecmp(ext, ".ant") == 0) 41 | { 42 | /* ANT: Radio Mobile file */ 43 | ret = pattern_export_ant(data, fp); 44 | } 45 | else 46 | { 47 | /* Other: XDR-GTK pattern file */ 48 | ret = pattern_export_xdrp(data, fp); 49 | } 50 | 51 | if (fclose(fp)) 52 | return FALSE; 53 | 54 | return ret; 55 | } 56 | 57 | static gboolean 58 | pattern_export_xdrp(pattern_data_t *data, 59 | FILE *fp) 60 | { 61 | pattern_signal_t *s = pattern_data_get_signal(data); 62 | gint count = pattern_signal_count(s); 63 | gchar buff[1024]; 64 | gint i; 65 | 66 | snprintf(buff, sizeof(buff), 67 | "%d\n", 68 | pattern_data_get_freq(data)); 69 | if (!write_to_file(buff, fp)) 70 | return FALSE; 71 | 72 | snprintf(buff, sizeof(buff), 73 | "%s\n", 74 | pattern_data_get_name(data)); 75 | if (!write_to_file(buff, fp)) 76 | return FALSE; 77 | 78 | for (i = 0; i < count; i++) 79 | { 80 | snprintf(buff, sizeof(buff), 81 | "%.2f\n", 82 | pattern_signal_get_sample(s, i)); 83 | if (!write_to_file(buff, fp)) 84 | return FALSE; 85 | } 86 | 87 | return TRUE; 88 | } 89 | 90 | static gboolean 91 | pattern_export_ant(pattern_data_t *data, 92 | FILE *fp) 93 | { 94 | pattern_signal_t *s = pattern_data_get_signal(data); 95 | gint count = pattern_signal_count(s); 96 | gchar buff[128]; 97 | gint i; 98 | 99 | for (i = 0; i < 360; i++) 100 | { 101 | gdouble x = i / 360.0 * count; 102 | snprintf(buff, sizeof(buff), 103 | "%.2f\r\n", 104 | pattern_signal_get_sample_interp(s, (gint) x, x - (gint) x)); 105 | if (!write_to_file(buff, fp)) 106 | return FALSE; 107 | } 108 | 109 | return TRUE; 110 | } 111 | 112 | static gboolean 113 | write_to_file(const char *string, 114 | FILE *fp) 115 | { 116 | size_t len = strlen(string); 117 | size_t out = fwrite(string, sizeof(gchar), len, fp); 118 | return (out == len); 119 | } -------------------------------------------------------------------------------- /src/pattern-misc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include 18 | #include "pattern.h" 19 | #include "pattern-color.h" 20 | 21 | gchar* 22 | pattern_misc_format_frequency(gint freq) 23 | { 24 | gchar buff[10]; 25 | const char *unit; 26 | size_t i; 27 | 28 | if (freq < 1000) 29 | { 30 | return g_strdup_printf("%d kHz", freq); 31 | } 32 | 33 | if (freq < 1000000) 34 | { 35 | g_snprintf(buff, sizeof(buff), "%.3f", freq / 1000.0); 36 | unit = "MHz"; 37 | } 38 | else 39 | { 40 | g_snprintf(buff, sizeof(buff), "%.6f", freq / 1000000.0); 41 | unit = "GHz"; 42 | } 43 | 44 | for (i = strlen(buff) - 1; i > 0 && buff[i] == '0'; i--); 45 | 46 | if (buff[i] == '.') 47 | i++; 48 | buff[i + 1] = '\0'; 49 | 50 | return g_strdup_printf("%s %s", buff, unit); 51 | } 52 | 53 | gchar* 54 | pattern_misc_info_all(pattern_t *p, 55 | gdouble angle) 56 | { 57 | GtkTreeIter iter; 58 | pattern_data_t *data; 59 | pattern_signal_t *s; 60 | gint count; 61 | gdouble angle_displ; 62 | gdouble x; 63 | gchar *color; 64 | gdouble peak; 65 | GString *str; 66 | gchar *cstr; 67 | 68 | str = g_string_new(NULL); 69 | 70 | angle_displ = (!pattern_get_full_angle(p) && angle > 180.0) ? angle - 360.0 : angle; 71 | g_string_append_printf(str, 72 | "Angle: %.2f°\n\n", 73 | angle_displ); 74 | 75 | if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(pattern_get_model(p)), &iter)) 76 | { 77 | do 78 | { 79 | gtk_tree_model_get(GTK_TREE_MODEL(pattern_get_model(p)), &iter, PATTERN_COL_DATA, &data, -1); 80 | if (pattern_data_get_hide(data)) 81 | continue; 82 | 83 | s = pattern_data_get_signal(data); 84 | count = pattern_signal_count(s); 85 | if (!count) 86 | continue; 87 | 88 | x = angle/360.0 * count; 89 | peak = (pattern_get_normalize(p) ? pattern_signal_get_peak(pattern_data_get_signal(data)) : pattern_get_peak(p)), 90 | color = pattern_color_to_string(pattern_data_get_color(data)); 91 | 92 | g_string_append_printf(str, 93 | "%s\n" 94 | "%.2f (%.2f) dB\n\n", 95 | (pattern_get_black(p) ? "black" : "white"), 96 | color, 97 | pattern_data_get_name(data), 98 | pattern_signal_get_sample_interp(s, (gint)x, x - (gint)x), 99 | pattern_signal_get_sample_interp(s, (gint)x, x - (gint)x) - peak); 100 | 101 | g_free(color); 102 | } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(pattern_get_model(p)), &iter)); 103 | } 104 | 105 | cstr = g_string_free(str, FALSE); 106 | cstr[strlen(cstr) - 2] = '\0'; // remove last two line feeds 107 | return cstr; 108 | } 109 | -------------------------------------------------------------------------------- /src/pattern-data.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2022 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include 18 | #include "pattern-data.h" 19 | 20 | typedef struct pattern_data 21 | { 22 | pattern_signal_t *s; 23 | gchar *name; 24 | gint freq; 25 | GdkRGBA color; 26 | gboolean hide; 27 | gboolean fill; 28 | gboolean changed; 29 | } pattern_data_t; 30 | 31 | 32 | pattern_data_t* 33 | pattern_data_new(pattern_signal_t *s) 34 | { 35 | pattern_data_t *data; 36 | g_assert(s != NULL); 37 | data = g_malloc0(sizeof(pattern_data_t)); 38 | data->s = s; 39 | return data; 40 | } 41 | 42 | void 43 | pattern_data_free(pattern_data_t *data) 44 | { 45 | pattern_signal_free(data->s); 46 | g_free(data->name); 47 | g_free(data); 48 | } 49 | 50 | gboolean 51 | pattern_data_changed(const pattern_data_t *data) 52 | { 53 | return data->changed || pattern_signal_changed(data->s); 54 | } 55 | 56 | void 57 | pattern_data_unchanged(pattern_data_t *data) 58 | { 59 | pattern_signal_unchanged(data->s); 60 | data->changed = FALSE; 61 | } 62 | 63 | pattern_signal_t* 64 | pattern_data_get_signal(pattern_data_t *data) 65 | { 66 | g_assert(data != NULL); 67 | return data->s; 68 | } 69 | 70 | const gchar* 71 | pattern_data_get_name(const pattern_data_t *data) 72 | { 73 | static const gchar *default_name = ""; 74 | g_assert(data != NULL); 75 | return (data->name ? data->name : default_name); 76 | } 77 | 78 | void 79 | pattern_data_set_name(pattern_data_t *data, 80 | const gchar *value) 81 | { 82 | g_assert(data != NULL); 83 | if (g_strcmp0(value, data->name) != 0) 84 | { 85 | g_free(data->name); 86 | data->name = (value ? g_strdup(value) : NULL); 87 | data->changed = TRUE; 88 | } 89 | } 90 | 91 | gint 92 | pattern_data_get_freq(const pattern_data_t *data) 93 | { 94 | g_assert(data != NULL); 95 | return data->freq; 96 | } 97 | 98 | void 99 | pattern_data_set_freq(pattern_data_t *data, 100 | gint value) 101 | { 102 | g_assert(data != NULL); 103 | value = MIN(PATTERN_DATA_MAX_FREQ, value); 104 | value = MAX(PATTERN_DATA_MIN_FREQ, value); 105 | if (value != data->freq) 106 | { 107 | data->freq = value; 108 | data->changed = TRUE; 109 | } 110 | } 111 | 112 | const GdkRGBA* 113 | pattern_data_get_color(const pattern_data_t *data) 114 | { 115 | g_assert(data != NULL); 116 | return &data->color; 117 | } 118 | 119 | void 120 | pattern_data_set_color(pattern_data_t *data, 121 | const GdkRGBA *value) 122 | { 123 | g_assert(data != NULL); 124 | if (!gdk_rgba_equal(value, &data->color)) 125 | { 126 | data->color = *value; 127 | data->changed = TRUE; 128 | } 129 | 130 | } 131 | 132 | gboolean 133 | pattern_data_get_hide(const pattern_data_t *data) 134 | { 135 | g_assert(data != NULL); 136 | return data->hide; 137 | } 138 | 139 | void 140 | pattern_data_set_hide(pattern_data_t *data, 141 | gboolean value) 142 | { 143 | g_assert(data != NULL); 144 | if (value != data->hide) 145 | { 146 | data->hide = value; 147 | data->changed = TRUE; 148 | } 149 | } 150 | 151 | gboolean 152 | pattern_data_get_fill(const pattern_data_t *data) 153 | { 154 | g_assert(data != NULL); 155 | return data->fill; 156 | } 157 | 158 | void 159 | pattern_data_set_fill(pattern_data_t *data, 160 | gboolean value) 161 | { 162 | g_assert(data != NULL); 163 | if (value != data->fill) 164 | { 165 | data->fill = value; 166 | data->changed = TRUE; 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /src/pattern-ui-plot.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include 18 | #include "pattern.h" 19 | #include "pattern-ui.h" 20 | #include "pattern-ui-dialogs.h" 21 | #include "pattern-plot.h" 22 | #include "pattern-misc.h" 23 | 24 | #define RAD2DEG(RAD) ((RAD) * 180.0 / M_PI) 25 | 26 | 27 | gboolean 28 | pattern_ui_plot(GtkWidget *widget, 29 | cairo_t *cr, 30 | pattern_ui_t *ui) 31 | { 32 | pattern_t *p = pattern_ui_get_pattern(ui); 33 | pattern_plot(cr, p); 34 | return FALSE; 35 | } 36 | 37 | gboolean 38 | pattern_ui_plot_motion(GtkWidget *widget, 39 | GdkEventMotion *event, 40 | pattern_ui_t *ui) 41 | { 42 | pattern_t *p = pattern_ui_get_pattern(ui); 43 | pattern_data_t *data; 44 | gint width; 45 | gdouble offset; 46 | gdouble line_width; 47 | gdouble radius; 48 | gint count; 49 | gdouble x, y; 50 | gdouble angle; 51 | gdouble step; 52 | gint i; 53 | gint rotating; 54 | gboolean redraw = FALSE; 55 | 56 | data = pattern_get_current(p); 57 | if (data == NULL || pattern_data_get_hide(data)) 58 | return TRUE; 59 | 60 | width = pattern_get_size(p); 61 | offset = width / (PATTERN_PLOT_BASE_SIZE / PATTERN_PLOT_OFFSET); 62 | line_width = width / (PATTERN_PLOT_BASE_SIZE / PATTERN_PLOT_BORDER_WIDTH); 63 | radius = width / 2.0 - offset + line_width; 64 | 65 | count = pattern_signal_count(pattern_data_get_signal(data)); 66 | if (!count) 67 | return TRUE; 68 | 69 | x = event->x - (offset + radius); 70 | y = event->y - (offset + radius); 71 | angle = RAD2DEG(atan2(y, x) + M_PI / 2.0); 72 | if (angle < 0.0) 73 | angle += 360.0; 74 | 75 | step = 360.0 / count; 76 | i = (gint)lround(angle / step) % count; 77 | rotating = pattern_ui_get_rotating_idx(ui); 78 | if (rotating != -1 && 79 | i != rotating) 80 | { 81 | pattern_signal_rotate(pattern_data_get_signal(data), 82 | pattern_ui_get_rotating_idx(ui) - i); 83 | pattern_ui_set_rotating_idx(ui, i); 84 | redraw = TRUE; 85 | } 86 | 87 | if ((event->x - width / 2.0) * (event->x - width / 2.0) + (event->y - width / 2.0) * (event->y - width / 2.0) > radius * radius) 88 | { 89 | if (pattern_ui_get_focus_idx(ui) != -1) 90 | { 91 | pattern_ui_set_focus_idx(ui, -1); 92 | redraw = TRUE; 93 | } 94 | } 95 | else 96 | { 97 | if (pattern_ui_get_focus_idx(ui) != i) 98 | { 99 | pattern_ui_set_focus_idx(ui, i); 100 | redraw = TRUE; 101 | } 102 | } 103 | 104 | if (redraw) 105 | gtk_widget_queue_draw(widget); 106 | 107 | return TRUE; 108 | } 109 | 110 | gboolean 111 | pattern_ui_plot_click(GtkWidget *widget, 112 | GdkEventButton *event, 113 | pattern_ui_t *ui) 114 | { 115 | pattern_t *p = pattern_ui_get_pattern(ui); 116 | pattern_data_t *data; 117 | gint width; 118 | gdouble offset; 119 | gdouble line_width; 120 | gdouble radius; 121 | gdouble angle; 122 | gchar *string; 123 | 124 | data = pattern_get_current(p); 125 | if (data == NULL || pattern_data_get_hide(data)) 126 | return FALSE; 127 | 128 | width = pattern_get_size(p); 129 | offset = width / (PATTERN_PLOT_BASE_SIZE / PATTERN_PLOT_OFFSET); 130 | line_width = width / (PATTERN_PLOT_BASE_SIZE / PATTERN_PLOT_BORDER_WIDTH); 131 | radius = width / 2.0 - offset + line_width; 132 | 133 | if (event->type == GDK_BUTTON_RELEASE && 134 | event->button == 1) 135 | { 136 | /* Left button release */ 137 | if (pattern_ui_get_rotating_idx(ui) != -1) 138 | pattern_ui_set_rotating_idx(ui, -1); 139 | return FALSE; 140 | } 141 | 142 | if ((event->x - width / 2.0) * (event->x - width / 2.0) + (event->y - width / 2.0) * (event->y - width / 2.0) > radius * radius) 143 | { 144 | /* Out of the plot */ 145 | return FALSE; 146 | } 147 | 148 | if (event->type == GDK_BUTTON_PRESS && 149 | event->button == 1) 150 | { 151 | /* Left button press */ 152 | pattern_ui_set_rotating_idx(ui, pattern_ui_get_focus_idx(ui)); 153 | return FALSE; 154 | } 155 | 156 | if (event->type == GDK_BUTTON_PRESS && 157 | event->button == 3) 158 | { 159 | /* Right button press */ 160 | angle = RAD2DEG(atan2(event->y - (offset + radius), event->x - (offset + radius)) + M_PI / 2.0); 161 | if (angle < 0.0) 162 | angle += 360.0; 163 | string = pattern_misc_info_all(p, angle); 164 | pattern_ui_dialog(pattern_ui_get_plot_window(ui), 165 | GTK_MESSAGE_INFO, 166 | "Interpolation", 167 | string); 168 | g_free(string); 169 | } 170 | 171 | return FALSE; 172 | } 173 | 174 | gboolean 175 | pattern_ui_plot_leave(GtkWidget *widget, 176 | GdkEvent *event, 177 | pattern_ui_t *ui) 178 | { 179 | if (pattern_ui_get_rotating_idx(ui) != -1) 180 | pattern_ui_set_rotating_idx(ui, -1); 181 | 182 | if (pattern_ui_get_focus_idx(ui) != -1) 183 | { 184 | pattern_ui_set_focus_idx(ui, -1); 185 | gtk_widget_queue_draw(widget); 186 | } 187 | 188 | return TRUE; 189 | } 190 | -------------------------------------------------------------------------------- /icons/scalable/apps/antpatt.svg: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 17 | 18 | -------------------------------------------------------------------------------- /src/pattern-ipc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2022-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include "pattern.h" 18 | #include "pattern-ui.h" 19 | #include "pattern-color.h" 20 | #ifdef G_OS_WIN32 21 | #include "mingw.h" 22 | #endif 23 | 24 | struct pattern_ipc 25 | { 26 | GIOChannel *channel; 27 | pattern_t *p; 28 | }; 29 | 30 | static struct pattern_ipc ipc = 31 | { 32 | .channel = NULL, 33 | .p = NULL 34 | }; 35 | 36 | static const gchar response_ready[] = "READY"; 37 | static const gchar response_bye[] = "BYE"; 38 | static const gchar response_ok[] = "OK"; 39 | static const gchar response_error[] = "ERROR"; 40 | 41 | static const gchar command_start[] = "START"; 42 | static const gchar command_stop[] = "STOP"; 43 | static const gchar command_push[] = "PUSH"; 44 | static const gchar command_name[] = "NAME"; 45 | static const gchar command_freq[] = "FREQ"; 46 | static const gchar command_color[] = "COLOR"; 47 | static const gchar command_avg[] = "AVG"; 48 | static const gchar command_fill[] = "FILL"; 49 | static const gchar command_rev[] = "REV"; 50 | 51 | static gboolean handle_channel(GIOChannel*, GIOCondition, gpointer); 52 | static void parse_command(pattern_t*, gchar*); 53 | static void send_response(const gchar*); 54 | static gboolean set_running(pattern_t*, gboolean); 55 | 56 | 57 | void 58 | pattern_ipc_init(pattern_t *p) 59 | { 60 | #ifdef G_OS_WIN32 61 | ipc.channel = g_io_channel_win32_new_fd(fileno(stdin)); 62 | #else 63 | ipc.channel = g_io_channel_unix_new(fileno(stdin)); 64 | #endif 65 | if (ipc.channel) 66 | { 67 | ipc.p = p; 68 | g_io_channel_set_close_on_unref(ipc.channel, TRUE); 69 | g_io_add_watch(ipc.channel, G_IO_IN | G_IO_ERR | G_IO_HUP, (GIOFunc)handle_channel, NULL); 70 | } 71 | 72 | send_response(ipc.channel ? response_ready : response_error); 73 | } 74 | 75 | void 76 | pattern_ipc_cleanup() 77 | { 78 | if (ipc.channel) 79 | { 80 | send_response(response_bye); 81 | g_io_channel_unref(ipc.channel); 82 | ipc.channel = NULL; 83 | } 84 | } 85 | 86 | static gboolean 87 | handle_channel(GIOChannel *source, 88 | GIOCondition cond, 89 | gpointer user_data) 90 | { 91 | g_autofree gchar *buff = NULL; 92 | GIOStatus status; 93 | 94 | status = g_io_channel_read_line(source, &buff, NULL, NULL, NULL); 95 | 96 | if (ipc.p == NULL) 97 | { 98 | send_response(response_error); 99 | return G_SOURCE_CONTINUE; 100 | } 101 | 102 | switch (status) 103 | { 104 | case G_IO_STATUS_AGAIN: 105 | return G_SOURCE_CONTINUE; 106 | case G_IO_STATUS_NORMAL: 107 | parse_command(ipc.p, buff); 108 | return G_SOURCE_CONTINUE; 109 | default: 110 | parse_command(ipc.p, NULL); 111 | return G_SOURCE_REMOVE; 112 | } 113 | } 114 | 115 | static void 116 | parse_command(pattern_t *p, 117 | gchar *command) 118 | { 119 | pattern_data_t *data = pattern_get_current(p); 120 | pattern_ui_t *ui = pattern_get_ui(p); 121 | static gboolean running = FALSE; 122 | gchar *value = command; 123 | gboolean ack = FALSE; 124 | 125 | if (command == NULL) 126 | { 127 | if (running) 128 | running = set_running(p, FALSE); 129 | return; 130 | } 131 | 132 | g_strchomp(command); 133 | strsep(&value, " "); 134 | 135 | if (g_ascii_strcasecmp(command, command_start) == 0) 136 | { 137 | if (running) 138 | running = set_running(p, FALSE); 139 | 140 | data = pattern_data_new(pattern_signal_new()); 141 | GdkRGBA color = pattern_color_next(); 142 | pattern_data_set_name(data, "Measurement"); 143 | pattern_data_set_color(data, &color); 144 | pattern_add(p, data); 145 | 146 | running = set_running(p, TRUE); 147 | ack = TRUE; 148 | } 149 | else if (running && data) 150 | { 151 | if (g_ascii_strcasecmp(command, command_stop) == 0) 152 | { 153 | running = set_running(p, FALSE); 154 | ack = TRUE; 155 | } 156 | else if (g_ascii_strcasecmp(command, command_name) == 0) 157 | { 158 | pattern_data_set_name(data, value); 159 | if (ui) 160 | pattern_ui_sync_name(ui, TRUE); 161 | ack = TRUE; 162 | } 163 | else if (g_ascii_strcasecmp(command, command_freq) == 0) 164 | { 165 | gint freq; 166 | if (value && sscanf(value, "%d", &freq)) 167 | { 168 | pattern_data_set_freq(data, freq); 169 | if (ui) 170 | pattern_ui_sync_freq(ui, TRUE); 171 | ack = TRUE; 172 | } 173 | } 174 | else if (g_ascii_strcasecmp(command, command_color) == 0) 175 | { 176 | GdkRGBA color; 177 | if (value && gdk_rgba_parse(&color, value)) 178 | { 179 | pattern_data_set_color(data, &color); 180 | if (ui) 181 | pattern_ui_sync_color(ui, TRUE); 182 | ack = TRUE; 183 | } 184 | } 185 | else if (g_ascii_strcasecmp(command, command_avg) == 0) 186 | { 187 | gint avg; 188 | if (value) 189 | { 190 | avg = g_ascii_strtoll(value, NULL, 10); 191 | pattern_signal_set_avg(pattern_data_get_signal(data), avg); 192 | if (ui) 193 | pattern_ui_sync_avg(ui, TRUE); 194 | ack = TRUE; 195 | } 196 | } 197 | else if (g_ascii_strcasecmp(command, command_fill) == 0) 198 | { 199 | gint fill; 200 | if (value) 201 | { 202 | fill = (g_ascii_strtoll(value, NULL, 10) != 0); 203 | pattern_data_set_fill(data, fill); 204 | if (ui) 205 | pattern_ui_sync_fill(ui, TRUE); 206 | ack = TRUE; 207 | } 208 | } 209 | else if (g_ascii_strcasecmp(command, command_rev) == 0) 210 | { 211 | gint rev; 212 | if (value) 213 | { 214 | rev = (g_ascii_strtoll(value, NULL, 10) != 0); 215 | pattern_signal_set_rev(pattern_data_get_signal(data), rev); 216 | if (ui) 217 | pattern_ui_sync_rev(ui, TRUE); 218 | ack = TRUE; 219 | } 220 | } 221 | else if (g_ascii_strcasecmp(command, command_push) == 0) 222 | { 223 | gdouble sample; 224 | if (value && sscanf(value, "%lf", &sample)) 225 | { 226 | pattern_signal_push(pattern_data_get_signal(data), sample); 227 | if (ui) 228 | pattern_ui_sync_data(ui); 229 | ack = TRUE; 230 | } 231 | } 232 | } 233 | 234 | send_response(ack ? response_ok : response_error); 235 | } 236 | 237 | static void 238 | send_response(const gchar *response) 239 | { 240 | fprintf(stdout, "%s\n", response); 241 | fflush(stdout); 242 | } 243 | 244 | static gboolean 245 | set_running(pattern_t *p, 246 | gboolean running) 247 | { 248 | pattern_data_t *data = pattern_get_current(p); 249 | pattern_ui_t *ui = pattern_get_ui(p); 250 | 251 | if (!running && 252 | data) 253 | { 254 | pattern_signal_set_finished(pattern_data_get_signal(data)); 255 | } 256 | 257 | if (ui) 258 | { 259 | pattern_ui_interactive(ui, running); 260 | } 261 | 262 | return running; 263 | } -------------------------------------------------------------------------------- /src/pattern-import.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include "pattern-import.h" 21 | #ifdef G_OS_WIN32 22 | #include "mingw.h" 23 | #endif 24 | 25 | typedef struct pattern_import 26 | { 27 | pattern_signal_t *samples; 28 | gchar *name; 29 | gint freq; 30 | } pattern_import_t; 31 | 32 | static gint pattern_import_xdrp(pattern_import_t*, FILE*); 33 | static gint pattern_import_mmanagal(pattern_import_t*, FILE*, const gchar*); 34 | static gint pattern_import_ant(pattern_import_t*, FILE*); 35 | static gint pattern_import_msi(pattern_import_t*, FILE*); 36 | 37 | static void replace_comma_with_dot(gchar*); 38 | 39 | 40 | pattern_import_t* 41 | pattern_import_new() 42 | { 43 | pattern_import_t *im; 44 | im = g_malloc(sizeof(pattern_import_t)); 45 | im->samples = pattern_signal_new(); 46 | im->name = NULL; 47 | im->freq = 0; 48 | return im; 49 | } 50 | 51 | void 52 | pattern_import_free(pattern_import_t *im, 53 | gboolean s) 54 | { 55 | if (im != NULL) 56 | { 57 | if (s) 58 | pattern_signal_free(im->samples); 59 | g_free(im->name); 60 | g_free(im); 61 | } 62 | } 63 | 64 | gint 65 | pattern_import(pattern_import_t *im, 66 | const gchar *filename) 67 | { 68 | const gchar *ext; 69 | FILE *fp; 70 | gint ret; 71 | 72 | g_assert(im != NULL); 73 | g_assert(filename != NULL); 74 | 75 | fp = g_fopen(filename, "r"); 76 | if (fp == NULL) 77 | return PATTERN_IMPORT_ERROR; 78 | 79 | ext = strrchr(filename, '.'); 80 | /* CSV: MMANA-GAL file */ 81 | if (ext && g_ascii_strcasecmp(ext, ".csv") == 0) 82 | ret = pattern_import_mmanagal(im, fp, "total"); 83 | /* ANT: Radio Mobile file */ 84 | else if (ext && g_ascii_strcasecmp(ext, ".ant") == 0) 85 | ret = pattern_import_ant(im, fp); 86 | /* MSI: Planet antenna file */ 87 | else if (ext && g_ascii_strcasecmp(ext, ".msi") == 0) 88 | ret = pattern_import_msi(im, fp); 89 | /* Other: XDR-GTK pattern file */ 90 | else 91 | ret = pattern_import_xdrp(im, fp); 92 | 93 | fclose(fp); 94 | 95 | if (im->name == NULL) 96 | im->name = g_path_get_basename(filename); 97 | pattern_signal_set_finished(im->samples); 98 | return ret; 99 | } 100 | 101 | static gint 102 | pattern_import_xdrp(pattern_import_t *im, 103 | FILE *fp) 104 | { 105 | gchar buff[256]; 106 | gdouble sample; 107 | 108 | /* First line: frequency [kHz] */ 109 | fgets(buff, sizeof(buff), fp); 110 | if (!sscanf(buff, "%d", &im->freq)) 111 | return PATTERN_IMPORT_INVALID_FORMAT; 112 | 113 | /* Second line: name */ 114 | fgets(buff, sizeof(buff), fp); 115 | if (strlen(buff) > 1) 116 | { 117 | buff[strcspn(buff, "\r\n")] = 0; 118 | im->name = g_strdup(buff); 119 | } 120 | 121 | /* Next lines: signal samples */ 122 | while (!feof(fp) && fscanf(fp, "%lf", &sample)) 123 | pattern_signal_push(im->samples, sample); 124 | 125 | if (!pattern_signal_count(im->samples)) 126 | return PATTERN_IMPORT_EMPTY_FILE; 127 | 128 | return PATTERN_IMPORT_OK; 129 | } 130 | 131 | static gint 132 | pattern_import_mmanagal(pattern_import_t *im, 133 | FILE *fp, 134 | const gchar *column_name) 135 | { 136 | gchar buff[256], *ptr, *token; 137 | gdouble sample; 138 | gint i, column = -1; 139 | 140 | /* First line: CSV header */ 141 | fgets(buff, sizeof(buff), fp); 142 | ptr = buff; 143 | for (i = 0; (token = strsep(&ptr, ",")); i++) 144 | { 145 | if (!g_ascii_strncasecmp(column_name, token, strlen(column_name))) 146 | column = i; 147 | } 148 | 149 | if (column == -1) 150 | return PATTERN_IMPORT_INVALID_FORMAT; 151 | 152 | /* Next lines: signal samples */ 153 | while (!feof(fp) && fgets(buff, sizeof(buff), fp)) 154 | { 155 | ptr = buff; 156 | for (i = 0; (token = strsep(&ptr, ",")); i++) 157 | { 158 | if (i == column) 159 | { 160 | if (sscanf(token, "%lf", &sample)) 161 | pattern_signal_push(im->samples, sample); 162 | else 163 | return PATTERN_IMPORT_INVALID_FORMAT; 164 | break; 165 | } 166 | } 167 | } 168 | 169 | if (!pattern_signal_count(im->samples)) 170 | return PATTERN_IMPORT_EMPTY_FILE; 171 | 172 | return PATTERN_IMPORT_OK; 173 | } 174 | 175 | static gint 176 | pattern_import_ant(pattern_import_t *im, 177 | FILE *fp) 178 | { 179 | gchar buff[256]; 180 | gdouble sample; 181 | gint i; 182 | 183 | for (i = 0; i < 360 && !feof(fp); i++) 184 | { 185 | if (!fgets(buff, sizeof(buff), fp)) 186 | break; 187 | if (sscanf(buff, "%lf", &sample)) 188 | pattern_signal_push(im->samples, sample); 189 | } 190 | 191 | if (pattern_signal_count(im->samples) != 360) 192 | return PATTERN_IMPORT_INVALID_FORMAT; 193 | 194 | return PATTERN_IMPORT_OK; 195 | } 196 | 197 | static gint 198 | pattern_import_msi(pattern_import_t *im, 199 | FILE *fp) 200 | { 201 | static const gchar *name_str = "NAME "; 202 | static const gchar *freq_str = "FREQUENCY "; 203 | static const gchar *gain_str = "GAIN "; 204 | static const gchar *data_str = "HORIZONTAL "; 205 | gchar buff[256]; 206 | gdouble sample; 207 | gdouble gain = NAN; 208 | gboolean data = FALSE; 209 | gint count = 0; 210 | gint i = 0; 211 | gint current; 212 | 213 | while (!feof(fp) && fgets(buff, sizeof(buff), fp) && (!data || (i != count))) 214 | { 215 | if (!im->name && !g_ascii_strncasecmp(name_str, buff, strlen(name_str))) 216 | { 217 | buff[strcspn(buff, "\r\n")] = 0; 218 | im->name = g_strdup(buff+strlen(name_str)); 219 | } 220 | else if (!im->freq && !g_ascii_strncasecmp(freq_str, buff, strlen(freq_str))) 221 | { 222 | sscanf(buff+strlen(freq_str), "%d", &im->freq); 223 | im->freq *= 1000; 224 | } 225 | else if (isnan(gain) && !g_ascii_strncasecmp(gain_str, buff, strlen(gain_str))) 226 | { 227 | sscanf(buff+strlen(gain_str), "%lf", &gain); 228 | } 229 | else if (!data && !g_ascii_strncasecmp(data_str, buff, strlen(data_str))) 230 | { 231 | if (sscanf(buff+strlen(data_str), "%d", &count)) 232 | data = TRUE; 233 | } 234 | else if (data) 235 | { 236 | replace_comma_with_dot(buff); 237 | if (sscanf(buff, "%d %lf", ¤t, &sample) == 2 && current == i) 238 | pattern_signal_push(im->samples, -sample); 239 | else 240 | break; 241 | i++; 242 | } 243 | } 244 | 245 | if (!count || pattern_signal_count(im->samples) != count) 246 | return PATTERN_IMPORT_INVALID_FORMAT; 247 | 248 | if (!isnan(gain)) 249 | pattern_signal_set_peak(im->samples, gain); 250 | 251 | return PATTERN_IMPORT_OK; 252 | } 253 | 254 | static void 255 | replace_comma_with_dot(gchar *buff) 256 | { 257 | size_t length = strlen(buff); 258 | gint i; 259 | for (i = 0; i < length; i++) 260 | if (buff[i] == ',') 261 | buff[i] = '.'; 262 | } 263 | 264 | pattern_signal_t* 265 | pattern_import_get_signal(pattern_import_t *r) 266 | { 267 | g_assert(r != NULL); 268 | g_assert(r->samples != NULL); 269 | return r->samples; 270 | } 271 | 272 | const gchar* 273 | pattern_import_get_name(pattern_import_t *r) 274 | { 275 | g_assert(r != NULL); 276 | return r->name; 277 | } 278 | 279 | gint 280 | pattern_import_get_freq(pattern_import_t *r) 281 | { 282 | g_assert(r != NULL); 283 | return r->freq; 284 | } 285 | -------------------------------------------------------------------------------- /src/pattern-signal.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include "pattern-signal.h" 20 | 21 | typedef struct pattern_signal 22 | { 23 | GArray *arr; 24 | gint count; 25 | gboolean finished; 26 | gdouble min; 27 | gdouble peak; 28 | gboolean rev; 29 | gint rotate; 30 | gint avg; 31 | gint interp; 32 | gsl_interp_accel *acc; 33 | gsl_spline *spline; 34 | gboolean changed; 35 | } pattern_signal_t; 36 | 37 | static gint pattern_signal_idx(const pattern_signal_t*, gint); 38 | static void pattern_signal_interp_init(pattern_signal_t*); 39 | static void pattern_signal_interp_invalidate(pattern_signal_t*); 40 | 41 | pattern_signal_t* 42 | pattern_signal_new() 43 | { 44 | pattern_signal_t *s = g_malloc(sizeof(pattern_signal_t)); 45 | s->arr = g_array_sized_new(FALSE, FALSE, sizeof(gdouble), 360); 46 | s->count = 0; 47 | s->finished = FALSE; 48 | s->min = NAN; 49 | s->peak = NAN; 50 | s->rev = FALSE; 51 | s->rotate = 0; 52 | s->avg = 0; 53 | s->interp = PATTERN_INTERP_LINEAR; 54 | s->acc = NULL; 55 | s->spline = NULL; 56 | s->changed = FALSE; 57 | return s; 58 | } 59 | 60 | void 61 | pattern_signal_free(pattern_signal_t *s) 62 | { 63 | if (s != NULL) 64 | { 65 | g_array_free(s->arr, TRUE); 66 | 67 | if (s->acc != NULL) 68 | gsl_interp_accel_free(s->acc); 69 | 70 | if (s->spline != NULL) 71 | gsl_spline_free(s->spline); 72 | 73 | g_free(s); 74 | } 75 | } 76 | 77 | gboolean 78 | pattern_signal_changed(const pattern_signal_t *s) 79 | { 80 | g_assert(s != NULL); 81 | return s->changed; 82 | } 83 | 84 | void 85 | pattern_signal_unchanged(pattern_signal_t *s) 86 | { 87 | g_assert(s != NULL); 88 | s->changed = FALSE; 89 | } 90 | 91 | gint 92 | pattern_signal_count(const pattern_signal_t *s) 93 | { 94 | g_assert(s != NULL); 95 | return s->count; 96 | } 97 | 98 | gint 99 | pattern_signal_interp(const pattern_signal_t *s) 100 | { 101 | g_assert(s != NULL); 102 | gint count = pattern_signal_count(s); 103 | gint value = (count ? 1024 / count : 1); 104 | return value ? value : 1; 105 | } 106 | 107 | void 108 | pattern_signal_push(pattern_signal_t *s, 109 | gdouble val) 110 | { 111 | g_assert(s != NULL); 112 | 113 | g_array_append_val(s->arr, val); 114 | s->count++; 115 | s->changed = TRUE; 116 | 117 | if (isnan(s->min) || 118 | s->min > val) 119 | { 120 | s->min = val; 121 | } 122 | 123 | if (isnan(s->peak) || 124 | s->peak < val) 125 | { 126 | s->peak = val; 127 | } 128 | 129 | pattern_signal_interp_invalidate(s); 130 | } 131 | 132 | gdouble 133 | pattern_signal_get_sample(const pattern_signal_t *s, 134 | gint idx) 135 | { 136 | g_assert(s != NULL); 137 | g_assert(s->count != 0); 138 | 139 | gdouble val; 140 | gint i; 141 | 142 | if (s->rev) 143 | idx = s->count - idx; 144 | 145 | idx += s->rotate; 146 | val = pattern_signal_get_sample_raw(s, idx); 147 | if (s->avg > 0) 148 | { 149 | for (i = 1; i <= s->avg; i++) 150 | { 151 | val += pattern_signal_get_sample_raw(s, idx-i); 152 | val += pattern_signal_get_sample_raw(s, idx+i); 153 | } 154 | val /= s->avg * 2.0 + 1.0; 155 | } 156 | return val; 157 | } 158 | 159 | static gint 160 | pattern_signal_idx(const pattern_signal_t *s, 161 | gint idx) 162 | { 163 | if (idx < 0) 164 | return s->count - 1 + ((idx + 1) % s->count); 165 | else 166 | return idx % s->count; 167 | } 168 | 169 | 170 | gdouble 171 | pattern_signal_get_sample_raw(const pattern_signal_t *s, 172 | gint idx) 173 | { 174 | g_assert(s != NULL); 175 | idx = pattern_signal_idx(s, idx); 176 | return g_array_index(s->arr, gdouble, idx); 177 | } 178 | 179 | gdouble 180 | pattern_signal_get_sample_interp(pattern_signal_t *s, 181 | gint idx, 182 | gdouble frac) 183 | { 184 | gdouble val, current, next; 185 | gint idx_new; 186 | 187 | g_assert(s != NULL); 188 | g_assert(s->count != 0); 189 | 190 | if (!s->acc && !s->spline) 191 | pattern_signal_interp_init(s); 192 | 193 | if (s->rev) 194 | { 195 | idx_new = s->count - 1 - idx; 196 | if (frac == 0.0) 197 | idx_new++; 198 | else 199 | frac = 1.0 - frac; 200 | } 201 | else 202 | { 203 | idx_new = idx; 204 | } 205 | 206 | idx_new = pattern_signal_idx(s, idx_new+s->rotate); 207 | val = gsl_spline_eval(s->spline, idx_new+frac, s->acc); 208 | 209 | if (s->interp == PATTERN_INTERP_AKIMA_CLIPPED) 210 | { 211 | current = pattern_signal_get_sample(s, idx); 212 | next = pattern_signal_get_sample(s, idx+1); 213 | if (val < current && val < next) 214 | val = MIN(current, next); 215 | if (val > current && val > next) 216 | val = MAX(current, next); 217 | } 218 | return val; 219 | } 220 | 221 | gdouble 222 | pattern_signal_get_min(const pattern_signal_t *s) 223 | { 224 | g_assert(s != NULL); 225 | return s->min; 226 | } 227 | 228 | gdouble 229 | pattern_signal_get_peak(const pattern_signal_t *s) 230 | { 231 | g_assert(s != NULL); 232 | return s->peak; 233 | } 234 | 235 | void 236 | pattern_signal_set_peak(pattern_signal_t *s, 237 | gdouble peak) 238 | { 239 | gdouble offset; 240 | gint i; 241 | 242 | g_assert(s != NULL); 243 | 244 | offset = peak - s->peak; 245 | s->peak = peak; 246 | s->min = offset + s->min; 247 | 248 | for (i = 0; i < s->count; i++) 249 | g_array_index(s->arr, gdouble, i) = g_array_index(s->arr, gdouble, i) + offset; 250 | } 251 | 252 | gboolean 253 | pattern_signal_get_rev(const pattern_signal_t *s) 254 | { 255 | g_assert(s != NULL); 256 | return s->rev; 257 | } 258 | 259 | void 260 | pattern_signal_set_rev(pattern_signal_t *s, 261 | gboolean rev) 262 | { 263 | g_assert(s != NULL); 264 | if (rev != s->rev) 265 | { 266 | s->rev = rev; 267 | s->changed = TRUE; 268 | } 269 | } 270 | 271 | gint 272 | pattern_signal_get_avg(const pattern_signal_t *s) 273 | { 274 | g_assert(s != NULL); 275 | return s->avg; 276 | } 277 | 278 | void 279 | pattern_signal_set_avg(pattern_signal_t *s, 280 | gint avg) 281 | { 282 | g_assert(s != NULL); 283 | avg = MIN(PATTERN_SIGNAL_MAX_AVG, avg); 284 | avg = MAX(PATTERN_SIGNAL_MIN_AVG, avg); 285 | if (avg != s->avg) 286 | { 287 | s->avg = avg; 288 | s->changed = TRUE; 289 | pattern_signal_interp_invalidate(s); 290 | } 291 | } 292 | 293 | gint 294 | pattern_signal_get_interp(const pattern_signal_t *s) 295 | { 296 | g_assert(s != NULL); 297 | return s->interp; 298 | } 299 | 300 | void 301 | pattern_signal_set_interp(pattern_signal_t *s, 302 | gint interp) 303 | { 304 | g_assert(s != NULL); 305 | g_assert(interp < PATTERN_INTERP_N); 306 | 307 | if ((s->interp == PATTERN_INTERP_LINEAR && interp != PATTERN_INTERP_LINEAR) || 308 | (s->interp != PATTERN_INTERP_LINEAR && interp == PATTERN_INTERP_LINEAR)) 309 | { 310 | pattern_signal_interp_invalidate(s); 311 | } 312 | 313 | if (interp != s->interp) 314 | { 315 | s->interp = interp; 316 | s->changed = TRUE; 317 | } 318 | } 319 | 320 | gboolean 321 | pattern_signal_get_finished(const pattern_signal_t *s) 322 | { 323 | g_assert(s != NULL); 324 | return s->finished; 325 | } 326 | 327 | void 328 | pattern_signal_set_finished(pattern_signal_t *s) 329 | { 330 | g_assert(s != NULL); 331 | if (!s->finished) 332 | { 333 | s->finished = TRUE; 334 | s->changed = TRUE; 335 | } 336 | } 337 | 338 | gint 339 | pattern_signal_get_rotate(const pattern_signal_t *s) 340 | { 341 | g_assert(s != NULL); 342 | return s->rotate; 343 | } 344 | 345 | void 346 | pattern_signal_set_rotate(pattern_signal_t *s, 347 | gint n) 348 | { 349 | g_assert(s != NULL); 350 | if (n != s->rotate) 351 | { 352 | s->rotate = n; 353 | s->changed = TRUE; 354 | } 355 | } 356 | 357 | void 358 | pattern_signal_rotate(pattern_signal_t *s, 359 | gint n) 360 | { 361 | g_assert(s != NULL); 362 | if (n) 363 | { 364 | s->rotate += (s->rev ? -n : n); 365 | s->changed = TRUE; 366 | } 367 | } 368 | 369 | void 370 | pattern_signal_rotate_0(pattern_signal_t *s) 371 | { 372 | gdouble max = NAN; 373 | gdouble val; 374 | gint idx, i; 375 | gint rotate = 0; 376 | gint mainlobe = 0; 377 | 378 | g_assert(s != NULL); 379 | if (s->count == 0) 380 | return; 381 | 382 | /* count samples with signal over -3dB */ 383 | for (idx = 0; idx < s->count; idx++) 384 | { 385 | val = pattern_signal_get_sample_raw(s, idx); 386 | if (s->peak - val <= 3.0) 387 | mainlobe++; 388 | } 389 | 390 | mainlobe /= 3; 391 | 392 | for (idx = 0; idx < s->count; idx++) 393 | { 394 | val = pattern_signal_get_sample_raw(s, idx); 395 | 396 | for (i = 1; i <= mainlobe; i++) 397 | { 398 | val += pattern_signal_get_sample_raw(s, idx-i); 399 | val += pattern_signal_get_sample_raw(s, idx+i); 400 | } 401 | val /= mainlobe * 2.0 + 1.0; 402 | 403 | if (isnan(max) || max < val) 404 | { 405 | max = val; 406 | rotate = idx; 407 | } 408 | } 409 | 410 | if (rotate != s->rotate) 411 | { 412 | s->rotate = rotate; 413 | s->changed = TRUE; 414 | } 415 | } 416 | 417 | void 418 | pattern_signal_rotate_reset(pattern_signal_t *s) 419 | { 420 | g_assert(s != NULL); 421 | if (s->rotate != 0) 422 | { 423 | s->rotate = 0; 424 | s->changed = TRUE; 425 | } 426 | } 427 | 428 | static void 429 | pattern_signal_interp_init(pattern_signal_t *s) 430 | { 431 | gdouble *x; 432 | gdouble *y; 433 | gint idx, i; 434 | size_t count; 435 | 436 | count = (size_t)s->count+1; 437 | 438 | /* Akima interpolation requires at least 5 samples */ 439 | if (s->interp != PATTERN_INTERP_LINEAR && 440 | count < gsl_interp_type_min_size(gsl_interp_akima_periodic)) 441 | { 442 | s->interp = PATTERN_INTERP_LINEAR; 443 | } 444 | 445 | x = g_malloc(count * sizeof(gdouble)); 446 | y = g_malloc(count * sizeof(gdouble)); 447 | 448 | /* count+1, loop one more time at the end */ 449 | for (idx = 0; idx <= s->count; idx++) 450 | { 451 | x[idx] = idx; 452 | y[idx] = pattern_signal_get_sample_raw(s, idx); 453 | 454 | if (s->avg > 0) 455 | { 456 | for (i = 1; i <= s->avg; i++) 457 | { 458 | y[idx] += pattern_signal_get_sample_raw(s, idx - i); 459 | y[idx] += pattern_signal_get_sample_raw(s, idx + i); 460 | } 461 | y[idx] /= s->avg * 2.0 + 1.0; 462 | } 463 | } 464 | 465 | s->acc = gsl_interp_accel_alloc(); 466 | s->spline = gsl_spline_alloc((s->interp != PATTERN_INTERP_LINEAR ? gsl_interp_akima_periodic : gsl_interp_linear), count); 467 | gsl_spline_init(s->spline, x, y, count); 468 | 469 | g_free(x); 470 | g_free(y); 471 | } 472 | 473 | static void 474 | pattern_signal_interp_invalidate(pattern_signal_t *s) 475 | { 476 | if (s->acc != NULL) 477 | { 478 | gsl_interp_accel_free(s->acc); 479 | s->acc = NULL; 480 | } 481 | 482 | if (s->spline != NULL) 483 | { 484 | gsl_spline_free(s->spline); 485 | s->spline = NULL; 486 | } 487 | } 488 | -------------------------------------------------------------------------------- /src/pattern.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include 18 | #include "pattern.h" 19 | 20 | /* Default settings */ 21 | #define PATTERN_DEFAULT_SIZE 600 22 | #define PATTERN_DEFAULT_TITLE "" 23 | #define PATTERN_DEFAULT_SCALE 0 24 | #define PATTERN_DEFAULT_LINE 1.0 25 | #define PATTERN_DEFAULT_INTERP PATTERN_INTERP_LINEAR 26 | #define PATTERN_DEFAULT_FULL_ANGLE TRUE 27 | #define PATTERN_DEFAULT_BLACK TRUE 28 | #define PATTERN_DEFAULT_NORMALIZE TRUE 29 | #define PATTERN_DEFAULT_LEGEND TRUE 30 | 31 | typedef struct pattern 32 | { 33 | pattern_ui_t *ui; 34 | GtkListStore *model; 35 | pattern_data_t *current; 36 | 37 | gint size; 38 | gchar *title; 39 | gint scale; 40 | gdouble line; 41 | gint interp; 42 | gboolean full_angle; 43 | gboolean black; 44 | gboolean normalize; 45 | gboolean legend; 46 | 47 | gchar *filename; 48 | gint visible; 49 | gboolean changed; 50 | } pattern_t; 51 | 52 | static gboolean pattern_set_interp_foreach(GtkTreeModel*, GtkTreePath*, GtkTreeIter*, gpointer); 53 | static void model_changed(pattern_t*); 54 | 55 | 56 | pattern_t* 57 | pattern_new() 58 | { 59 | pattern_t *p = g_malloc0(sizeof(pattern_t)); 60 | p->model = gtk_list_store_new(PATTERN_COLS, G_TYPE_POINTER); 61 | 62 | pattern_reset(p); 63 | 64 | g_signal_connect_swapped(p->model, "row-changed", G_CALLBACK(model_changed), p); 65 | g_signal_connect_swapped(p->model, "row-inserted", G_CALLBACK(model_changed), p); 66 | g_signal_connect_swapped(p->model, "row-deleted", G_CALLBACK(model_changed), p); 67 | g_signal_connect_swapped(p->model, "rows-reordered", G_CALLBACK(model_changed), p); 68 | 69 | return p; 70 | } 71 | 72 | void 73 | pattern_free(pattern_t *p) 74 | { 75 | if (p) 76 | { 77 | pattern_clear(p); 78 | g_free(p->filename); 79 | g_free(p->title); 80 | g_free(p->ui); 81 | g_free(p); 82 | } 83 | } 84 | 85 | void 86 | pattern_reset(pattern_t *p) 87 | { 88 | pattern_set_size(p, PATTERN_DEFAULT_SIZE); 89 | pattern_set_title(p, PATTERN_DEFAULT_TITLE); 90 | pattern_set_scale(p, PATTERN_DEFAULT_SCALE); 91 | pattern_set_line(p, PATTERN_DEFAULT_LINE); 92 | pattern_set_interp(p, PATTERN_DEFAULT_INTERP); 93 | pattern_set_full_angle(p, PATTERN_DEFAULT_FULL_ANGLE); 94 | pattern_set_black(p, PATTERN_DEFAULT_BLACK); 95 | pattern_set_normalize(p, PATTERN_DEFAULT_NORMALIZE); 96 | pattern_set_legend(p, PATTERN_DEFAULT_LEGEND); 97 | 98 | pattern_clear(p); 99 | 100 | pattern_set_filename(p, NULL); 101 | p->changed = FALSE; 102 | } 103 | 104 | gboolean 105 | pattern_changed(const pattern_t *p) 106 | { 107 | g_assert(p != NULL); 108 | GtkTreeIter iter; 109 | pattern_data_t *data; 110 | 111 | if (p->changed) 112 | return TRUE; 113 | 114 | if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(p->model), &iter)) 115 | return FALSE; 116 | 117 | do 118 | { 119 | gtk_tree_model_get(GTK_TREE_MODEL(p->model), &iter, 120 | PATTERN_COL_DATA, &data, -1); 121 | if (pattern_data_changed(data)) 122 | return TRUE; 123 | } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(p->model), &iter)); 124 | 125 | return FALSE; 126 | } 127 | 128 | void 129 | pattern_unchanged(pattern_t *p) 130 | { 131 | g_assert(p != NULL); 132 | GtkTreeIter iter; 133 | pattern_data_t *data; 134 | 135 | p->changed = FALSE; 136 | 137 | if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(p->model), &iter)) 138 | return; 139 | 140 | do 141 | { 142 | gtk_tree_model_get(GTK_TREE_MODEL(p->model), &iter, 143 | PATTERN_COL_DATA, &data, -1); 144 | pattern_data_unchanged(data); 145 | } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(p->model), &iter)); 146 | } 147 | 148 | GtkListStore* 149 | pattern_get_model(pattern_t *p) 150 | { 151 | g_assert(p != NULL); 152 | return p->model; 153 | } 154 | 155 | pattern_ui_t* 156 | pattern_get_ui(pattern_t *p) 157 | { 158 | g_assert(p != NULL); 159 | return p->ui; 160 | } 161 | 162 | void 163 | pattern_set_ui(pattern_t *p, 164 | pattern_ui_t *ui) 165 | { 166 | g_assert(p != NULL); 167 | p->ui = ui; 168 | } 169 | 170 | void 171 | pattern_add(pattern_t *p, 172 | pattern_data_t *data) 173 | { 174 | g_assert(p != NULL); 175 | g_assert(data != NULL); 176 | gtk_list_store_insert_with_values(p->model, NULL, -1, PATTERN_COL_DATA, data, -1); 177 | 178 | if (!pattern_data_get_hide(data)) 179 | p->visible++; 180 | 181 | pattern_signal_set_interp(pattern_data_get_signal(data), p->interp); 182 | 183 | /* No need to set pattern_changed explicitly */ 184 | } 185 | 186 | void 187 | pattern_remove(pattern_t *p, 188 | GtkTreeIter *remove) 189 | { 190 | g_assert(p != NULL); 191 | pattern_data_t *data; 192 | gboolean visible; 193 | 194 | gtk_tree_model_get(GTK_TREE_MODEL(p->model), remove, 195 | PATTERN_COL_DATA, &data, 196 | -1); 197 | visible = !pattern_data_get_hide(data); 198 | pattern_data_free(data); 199 | gtk_list_store_remove(p->model, remove); 200 | 201 | if (p->current == data) 202 | pattern_set_current(p, NULL); 203 | 204 | if (visible) 205 | p->visible--; 206 | 207 | /* No need to set pattern_changed explicitly */ 208 | } 209 | 210 | void 211 | pattern_clear(pattern_t *p) 212 | { 213 | g_assert(p != NULL); 214 | pattern_data_t *data; 215 | GtkTreeIter iter; 216 | 217 | pattern_set_current(p, NULL); 218 | p->visible = 0; 219 | 220 | if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(p->model), &iter)) 221 | return; 222 | 223 | do 224 | { 225 | gtk_tree_model_get(GTK_TREE_MODEL(p->model), &iter, 226 | PATTERN_COL_DATA, &data, -1); 227 | pattern_data_free(data); 228 | } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(p->model), &iter)); 229 | 230 | gtk_list_store_clear(p->model); 231 | 232 | /* No need to call pattern_changed explicitly */ 233 | } 234 | 235 | void 236 | pattern_set_current(pattern_t *p, 237 | pattern_data_t *data) 238 | { 239 | g_assert(p != NULL); 240 | p->current = data; 241 | } 242 | 243 | pattern_data_t* 244 | pattern_get_current(pattern_t *p) 245 | { 246 | g_assert(p != NULL); 247 | return p->current; 248 | } 249 | 250 | void 251 | pattern_set_size(pattern_t *p, 252 | gint value) 253 | { 254 | g_assert(p != NULL); 255 | value = MIN(PATTERN_MAX_SIZE, value); 256 | value = MAX(PATTERN_MIN_SIZE, value); 257 | if (value != p->size) 258 | { 259 | p->size = value; 260 | p->changed = TRUE; 261 | } 262 | } 263 | 264 | gint 265 | pattern_get_size(const pattern_t *p) 266 | { 267 | g_assert(p != NULL); 268 | return p->size; 269 | } 270 | 271 | void 272 | pattern_set_title(pattern_t *p, 273 | const gchar *value) 274 | { 275 | g_assert(p != NULL); 276 | g_assert(value != NULL); 277 | if (g_strcmp0(value, p->title) != 0) 278 | { 279 | g_free(p->title); 280 | p->title = g_strdup(value); 281 | p->changed = TRUE; 282 | } 283 | } 284 | 285 | const gchar* 286 | pattern_get_title(const pattern_t *p) 287 | { 288 | g_assert(p != NULL); 289 | return p->title; 290 | } 291 | 292 | void 293 | pattern_set_scale(pattern_t *p, 294 | gint value) 295 | { 296 | g_assert(p != NULL); 297 | if (value != p->scale) 298 | { 299 | p->scale = value; 300 | p->changed = TRUE; 301 | } 302 | } 303 | 304 | gint 305 | pattern_get_scale(const pattern_t *p) 306 | { 307 | g_assert(p != NULL); 308 | return p->scale; 309 | } 310 | 311 | void 312 | pattern_set_line(pattern_t *p, 313 | gdouble value) 314 | { 315 | g_assert(p != NULL); 316 | value = MIN(PATTERN_MAX_LINE, value); 317 | value = MAX(PATTERN_MIN_LINE, value); 318 | if (value != p->line) 319 | { 320 | p->line = value; 321 | p->changed = TRUE; 322 | } 323 | } 324 | 325 | gdouble 326 | pattern_get_line(const pattern_t *p) 327 | { 328 | g_assert(p != NULL); 329 | return p->line; 330 | } 331 | 332 | void 333 | pattern_set_interp(pattern_t *p, 334 | gint value) 335 | { 336 | g_assert(p != NULL); 337 | if (value != p->interp) 338 | { 339 | p->interp = value; 340 | p->changed = TRUE; 341 | gtk_tree_model_foreach(GTK_TREE_MODEL(p->model), pattern_set_interp_foreach, p); 342 | } 343 | } 344 | 345 | static gboolean 346 | pattern_set_interp_foreach(GtkTreeModel *model, 347 | GtkTreePath *path, 348 | GtkTreeIter *iter, 349 | gpointer user_data) 350 | { 351 | pattern_t *p = (pattern_t*)user_data; 352 | pattern_data_t *data; 353 | gtk_tree_model_get(model, iter, PATTERN_COL_DATA, &data, -1); 354 | pattern_signal_set_interp(pattern_data_get_signal(data), p->interp); 355 | return FALSE; 356 | } 357 | 358 | gint 359 | pattern_get_interp(const pattern_t *p) 360 | { 361 | g_assert(p != NULL); 362 | return p->interp; 363 | } 364 | 365 | void 366 | pattern_set_full_angle(pattern_t *p, 367 | gboolean value) 368 | { 369 | g_assert(p != NULL); 370 | if (value != p->full_angle) 371 | { 372 | p->full_angle = value; 373 | p->changed = TRUE; 374 | } 375 | } 376 | 377 | gboolean 378 | pattern_get_full_angle(const pattern_t *p) 379 | { 380 | g_assert(p != NULL); 381 | return p->full_angle; 382 | } 383 | 384 | void 385 | pattern_set_black(pattern_t *p, 386 | gboolean value) 387 | { 388 | g_assert(p != NULL); 389 | if (value != p->black) 390 | { 391 | p->black = value; 392 | p->changed = TRUE; 393 | } 394 | } 395 | 396 | gboolean 397 | pattern_get_black(const pattern_t *p) 398 | { 399 | g_assert(p != NULL); 400 | return p->black; 401 | } 402 | 403 | void 404 | pattern_set_normalize(pattern_t *p, 405 | gboolean value) 406 | { 407 | g_assert(p != NULL); 408 | if (value != p->normalize) 409 | { 410 | p->normalize = value; 411 | p->changed = TRUE; 412 | } 413 | } 414 | 415 | gboolean 416 | pattern_get_normalize(const pattern_t *p) 417 | { 418 | g_assert(p != NULL); 419 | return p->normalize; 420 | } 421 | 422 | void 423 | pattern_set_legend(pattern_t *p, 424 | gboolean value) 425 | { 426 | g_assert(p != NULL); 427 | if (value != p->legend) 428 | { 429 | p->legend = value; 430 | p->changed = TRUE; 431 | } 432 | } 433 | 434 | gboolean 435 | pattern_get_legend(const pattern_t *p) 436 | { 437 | g_assert(p != NULL); 438 | return p->legend; 439 | } 440 | 441 | void 442 | pattern_set_filename(pattern_t *p, 443 | const gchar *value) 444 | { 445 | g_assert(p != NULL); 446 | if (g_strcmp0(value, p->filename) != 0) 447 | { 448 | g_free(p->filename); 449 | p->filename = g_strdup(value); 450 | p->changed = TRUE; 451 | } 452 | } 453 | 454 | const gchar* 455 | pattern_get_filename(const pattern_t *p) 456 | { 457 | g_assert(p != NULL); 458 | return p->filename; 459 | } 460 | 461 | gint 462 | pattern_get_visible_count(const pattern_t *p) 463 | { 464 | g_assert(p != NULL); 465 | return p->visible; 466 | } 467 | 468 | gdouble 469 | pattern_get_peak(const pattern_t *p) 470 | { 471 | g_assert(p != NULL); 472 | pattern_data_t *data; 473 | GtkTreeIter iter; 474 | gdouble peak = NAN; 475 | 476 | if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(p->model), &iter)) 477 | return peak; 478 | 479 | do 480 | { 481 | gtk_tree_model_get(GTK_TREE_MODEL(p->model), &iter, 482 | PATTERN_COL_DATA, &data, -1); 483 | 484 | if (isnan(peak) || peak < pattern_signal_get_peak(pattern_data_get_signal(data))) 485 | peak = pattern_signal_get_peak(pattern_data_get_signal(data)); 486 | 487 | } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(p->model), &iter)); 488 | 489 | return peak; 490 | } 491 | 492 | void 493 | pattern_hide(pattern_t *p, 494 | pattern_data_t *data, 495 | gboolean hide) 496 | { 497 | g_assert(p != NULL); 498 | g_assert(data != NULL); 499 | if (pattern_data_get_hide(data) != hide) 500 | { 501 | p->visible += (hide ? -1 : 1); 502 | pattern_data_set_hide(data, hide); 503 | p->changed = TRUE; 504 | } 505 | } 506 | 507 | static void 508 | model_changed(pattern_t *p) 509 | { 510 | p->changed = TRUE; 511 | } 512 | -------------------------------------------------------------------------------- /src/pattern-ui-dialogs.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include 18 | #include "pattern-ui-dialogs.h" 19 | #include "version.h" 20 | #ifdef G_OS_WIN32 21 | #include "mingw.h" 22 | #endif 23 | 24 | static void file_chooser_response(GtkWidget*, gint, gpointer); 25 | static gboolean str_has_suffix(const gchar*, const gchar*); 26 | 27 | 28 | void 29 | pattern_ui_dialog(GtkWindow *window, 30 | GtkMessageType icon, 31 | gchar *title, 32 | gchar *format, 33 | ...) 34 | { 35 | GtkWidget *dialog; 36 | va_list args; 37 | gchar *msg; 38 | 39 | va_start(args, format); 40 | msg = g_markup_vprintf_escaped(format, args); 41 | va_end(args); 42 | dialog = gtk_message_dialog_new(window, 43 | GTK_DIALOG_MODAL, 44 | icon, 45 | GTK_BUTTONS_CLOSE, 46 | NULL); 47 | #ifdef G_OS_WIN32 48 | g_signal_connect(dialog, "realize", G_CALLBACK(mingw_realize), NULL); 49 | #endif 50 | gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(dialog), msg); 51 | gtk_window_set_title(GTK_WINDOW(dialog), title); 52 | if (window == NULL) 53 | gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER); 54 | gtk_dialog_run(GTK_DIALOG(dialog)); 55 | gtk_widget_destroy(dialog); 56 | g_free(msg); 57 | } 58 | 59 | gboolean 60 | pattern_ui_dialog_yesno(GtkWindow *parent, 61 | const gchar *title, 62 | const gchar *text) 63 | { 64 | GtkWidget *dialog; 65 | gint response; 66 | 67 | dialog = gtk_message_dialog_new(parent, 68 | GTK_DIALOG_MODAL, 69 | GTK_MESSAGE_QUESTION, 70 | GTK_BUTTONS_YES_NO, 71 | NULL); 72 | #ifdef G_OS_WIN32 73 | g_signal_connect(dialog, "realize", G_CALLBACK(mingw_realize), NULL); 74 | #endif 75 | gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(dialog), text); 76 | gtk_window_set_title(GTK_WINDOW(dialog), title); 77 | response = gtk_dialog_run(GTK_DIALOG(dialog)); 78 | gtk_widget_destroy(dialog); 79 | return response == GTK_RESPONSE_YES; 80 | } 81 | 82 | gint 83 | pattern_ui_dialog_ask_unsaved(GtkWindow *parent) 84 | { 85 | GtkWidget *dialog; 86 | gint response; 87 | 88 | dialog = gtk_message_dialog_new(parent, 89 | GTK_DIALOG_MODAL, 90 | GTK_MESSAGE_QUESTION, 91 | GTK_BUTTONS_NONE, 92 | "There are some unsaved changes.\nDo you want to save them?"); 93 | #ifdef G_OS_WIN32 94 | g_signal_connect(dialog, "realize", G_CALLBACK(mingw_realize), NULL); 95 | #endif 96 | gtk_dialog_add_buttons(GTK_DIALOG(dialog), 97 | "_Cancel", GTK_RESPONSE_CANCEL, 98 | "_Save", GTK_RESPONSE_YES, 99 | "_Discard", GTK_RESPONSE_NO, 100 | NULL); 101 | 102 | gtk_window_set_title(GTK_WINDOW(dialog), "Project"); 103 | response = gtk_dialog_run(GTK_DIALOG(dialog)); 104 | gtk_widget_destroy(dialog); 105 | return response; 106 | } 107 | 108 | gchar* 109 | pattern_ui_dialog_open(GtkWindow *window) 110 | { 111 | GtkWidget *dialog; 112 | GtkFileFilter *filter; 113 | gchar *filename = NULL; 114 | 115 | dialog = gtk_file_chooser_dialog_new("Open project", 116 | window, 117 | GTK_FILE_CHOOSER_ACTION_OPEN, 118 | "_Cancel", GTK_RESPONSE_CANCEL, 119 | "_Open", GTK_RESPONSE_ACCEPT, 120 | NULL); 121 | #ifdef G_OS_WIN32 122 | g_signal_connect(dialog, "realize", G_CALLBACK(mingw_realize), NULL); 123 | #endif 124 | 125 | filter = gtk_file_filter_new(); 126 | gtk_file_filter_set_name(filter, "Antenna pattern project (*.antp.gz, *.antp)"); 127 | gtk_file_filter_add_pattern(filter, "*" APP_FILE_EXT); 128 | gtk_file_filter_add_pattern(filter, "*" APP_FILE_EXT APP_FILE_COMPRESS); 129 | gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); 130 | 131 | if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) 132 | filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); 133 | gtk_widget_destroy(dialog); 134 | 135 | return filename; 136 | } 137 | 138 | gchar* 139 | pattern_ui_dialog_save(GtkWindow *window) 140 | { 141 | GtkWidget *dialog; 142 | GtkWidget *box; 143 | GtkFileFilter *filter; 144 | gchar *ret = NULL; 145 | 146 | dialog = gtk_file_chooser_dialog_new("Save project", 147 | window, 148 | GTK_FILE_CHOOSER_ACTION_SAVE, 149 | "_Cancel", GTK_RESPONSE_CANCEL, 150 | "_Save", GTK_RESPONSE_ACCEPT, 151 | NULL); 152 | #ifdef G_OS_WIN32 153 | g_signal_connect(dialog, "realize", G_CALLBACK(mingw_realize), NULL); 154 | #endif 155 | gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog), TRUE); 156 | gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE); 157 | 158 | box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 12); 159 | 160 | gtk_widget_show_all(box); 161 | gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog), box); 162 | 163 | filter = gtk_file_filter_new(); 164 | gtk_file_filter_set_name(filter, "Antenna pattern project (*.antp.gz)"); 165 | gtk_file_filter_add_pattern(filter, "*" APP_FILE_EXT APP_FILE_COMPRESS); 166 | g_object_set_data_full(G_OBJECT(filter), "antpatt-ext", g_strdup(APP_FILE_EXT APP_FILE_COMPRESS), g_free); 167 | gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); 168 | 169 | filter = gtk_file_filter_new(); 170 | gtk_file_filter_set_name(filter, "Antenna pattern project (*.antp)"); 171 | gtk_file_filter_add_pattern(filter, "*" APP_FILE_EXT); 172 | g_object_set_data_full(G_OBJECT(filter), "antpatt-ext", g_strdup(APP_FILE_EXT), g_free); 173 | gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); 174 | 175 | g_signal_connect(dialog, "response", G_CALLBACK(file_chooser_response), &ret); 176 | while (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_NONE); 177 | 178 | return ret; 179 | } 180 | 181 | GSList* 182 | pattern_ui_dialog_import(GtkWindow *window) 183 | { 184 | GtkWidget *dialog; 185 | GSList *list = NULL; 186 | 187 | dialog = gtk_file_chooser_dialog_new("Import files", 188 | window, 189 | GTK_FILE_CHOOSER_ACTION_OPEN, 190 | "_Cancel", GTK_RESPONSE_CANCEL, 191 | "_Open", GTK_RESPONSE_ACCEPT, 192 | NULL); 193 | #ifdef G_OS_WIN32 194 | g_signal_connect(dialog, "realize", G_CALLBACK(mingw_realize), NULL); 195 | #endif 196 | gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); 197 | if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) 198 | list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog)); 199 | gtk_widget_destroy(dialog); 200 | 201 | return list; 202 | } 203 | 204 | gchar* 205 | pattern_ui_dialog_render(GtkWindow *window) 206 | { 207 | GtkWidget *dialog; 208 | GtkFileFilter *filter; 209 | gchar *filename = NULL; 210 | 211 | dialog = gtk_file_chooser_dialog_new("Render pattern plot", 212 | window, 213 | GTK_FILE_CHOOSER_ACTION_SAVE, 214 | "_Cancel", GTK_RESPONSE_CANCEL, 215 | "_Save", GTK_RESPONSE_ACCEPT, 216 | NULL); 217 | #ifdef G_OS_WIN32 218 | g_signal_connect(dialog, "realize", G_CALLBACK(mingw_realize), NULL); 219 | #endif 220 | gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog), TRUE); 221 | gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE); 222 | 223 | filter = gtk_file_filter_new(); 224 | gtk_file_filter_set_name(filter, "PNG image"); 225 | gtk_file_filter_add_pattern(filter, "*.png"); 226 | g_object_set_data_full(G_OBJECT(filter), "antpatt-ext", g_strdup(".png"), g_free); 227 | gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); 228 | 229 | filter = gtk_file_filter_new(); 230 | gtk_file_filter_set_name(filter, "SVG image"); 231 | gtk_file_filter_add_pattern(filter, "*.svg"); 232 | g_object_set_data_full(G_OBJECT(filter), "antpatt-ext", g_strdup(".svg"), g_free); 233 | gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); 234 | 235 | g_signal_connect(dialog, "response", G_CALLBACK(file_chooser_response), &filename); 236 | while (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_NONE); 237 | 238 | return filename; 239 | } 240 | 241 | gchar* 242 | pattern_ui_dialog_export(GtkWindow *window) 243 | { 244 | GtkWidget *dialog; 245 | GtkFileFilter *filter; 246 | gchar *filename = NULL; 247 | 248 | dialog = gtk_file_chooser_dialog_new("Export pattern data", 249 | window, 250 | GTK_FILE_CHOOSER_ACTION_SAVE, 251 | "_Cancel", GTK_RESPONSE_CANCEL, 252 | "_Save", GTK_RESPONSE_ACCEPT, 253 | NULL); 254 | #ifdef G_OS_WIN32 255 | g_signal_connect(dialog, "realize", G_CALLBACK(mingw_realize), NULL); 256 | #endif 257 | gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog), TRUE); 258 | gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE); 259 | 260 | filter = gtk_file_filter_new(); 261 | gtk_file_filter_set_name(filter, "XDR-GTK file (*.xdrp)"); 262 | gtk_file_filter_add_pattern(filter, "*.xdrp"); 263 | g_object_set_data_full(G_OBJECT(filter), "antpatt-ext", g_strdup(".xdrp"), g_free); 264 | gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); 265 | 266 | filter = gtk_file_filter_new(); 267 | gtk_file_filter_set_name(filter, "Radiomobile file (*.ant)"); 268 | gtk_file_filter_add_pattern(filter, "*.ant"); 269 | g_object_set_data_full(G_OBJECT(filter), "antpatt-ext", g_strdup(".ant"), g_free); 270 | gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); 271 | 272 | g_signal_connect(dialog, "response", G_CALLBACK(file_chooser_response), &filename); 273 | while (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_NONE); 274 | 275 | return filename; 276 | } 277 | 278 | static void 279 | file_chooser_response(GtkWidget *dialog, 280 | gint response_id, 281 | gpointer user_data) 282 | { 283 | gchar **ret = (gchar**)user_data; 284 | gchar *filename; 285 | GtkFileFilter *filter; 286 | 287 | if (response_id != GTK_RESPONSE_ACCEPT) 288 | { 289 | gtk_widget_destroy(dialog); 290 | return; 291 | } 292 | 293 | if (!(filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)))) 294 | { 295 | pattern_ui_dialog(GTK_WINDOW(dialog), 296 | GTK_MESSAGE_ERROR, 297 | "Error", 298 | "No file selected."); 299 | return; 300 | } 301 | 302 | filter = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog)); 303 | const gchar *ext = g_object_get_data(G_OBJECT(filter), "antpatt-ext"); 304 | if (!str_has_suffix(filename, ext)) 305 | { 306 | filename = (gchar*)g_realloc(filename, strlen(filename) + strlen(ext) + 1); 307 | strcat(filename, ext); 308 | 309 | /* After adding the suffix, the GTK should check whether we may overwrite something. */ 310 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), filename); 311 | gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); 312 | g_free(filename); 313 | return; 314 | } 315 | 316 | *ret = filename; 317 | gtk_widget_destroy(dialog); 318 | } 319 | 320 | static gboolean 321 | str_has_suffix(const gchar *string, 322 | const gchar *suffix) 323 | { 324 | size_t string_len = strlen(string); 325 | size_t suffix_len = strlen(suffix); 326 | 327 | if (string_len < suffix_len) 328 | return FALSE; 329 | 330 | return g_ascii_strncasecmp(string + string_len - suffix_len, suffix, suffix_len) == 0; 331 | } 332 | 333 | void 334 | pattern_ui_dialog_about(GtkWindow *window) 335 | { 336 | GtkWidget *dialog = gtk_about_dialog_new(); 337 | #ifdef G_OS_WIN32 338 | g_signal_connect(dialog, "realize", G_CALLBACK(mingw_realize), NULL); 339 | #endif 340 | gtk_window_set_icon_name(GTK_WINDOW(dialog), "gtk-about"); 341 | gtk_window_set_transient_for(GTK_WINDOW(dialog), window); 342 | gtk_about_dialog_set_program_name(GTK_ABOUT_DIALOG(dialog), APP_NAME); 343 | gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(dialog), APP_VERSION); 344 | gtk_about_dialog_set_logo_icon_name(GTK_ABOUT_DIALOG(dialog), APP_ICON); 345 | gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(dialog), "Copyright © " APP_COPYRIGHT " Konrad Kosmatka"); 346 | gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(dialog), "Antenna pattern plotting and analysis software"); 347 | gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(dialog), "https://fmdx.pl/antpatt"); 348 | gtk_about_dialog_set_license(GTK_ABOUT_DIALOG(dialog), APP_LICENCE); 349 | #ifdef G_OS_WIN32 350 | g_signal_connect(dialog, "activate-link", G_CALLBACK(mingw_uri_signal), NULL); 351 | #endif 352 | gtk_dialog_run(GTK_DIALOG(dialog)); 353 | gtk_widget_destroy(dialog); 354 | } 355 | -------------------------------------------------------------------------------- /src/pattern-json.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include "pattern.h" 22 | #include "pattern-color.h" 23 | 24 | #define PATTERN_JSON_VERSION 1 25 | 26 | #define READ_BUFFER (50*1024) 27 | 28 | #define KEY_VERSION APP_NAME 29 | #define KEY_SIZE "size" 30 | #define KEY_TITLE "title" 31 | #define KEY_SCALE "scale" 32 | #define KEY_LINE "line" 33 | #define KEY_INTERP "interp" 34 | #define KEY_FULL_ANGLE "full_angle" 35 | #define KEY_BLACK "black" 36 | #define KEY_NORMALIZE "normalize" 37 | #define KEY_LEGEND "legend" 38 | #define KEY_DATA "data" 39 | #define KEY_NAME "name" 40 | #define KEY_FREQ "freq" 41 | #define KEY_COLOR "color" 42 | #define KEY_HIDE "hide" 43 | #define KEY_FILL "fill" 44 | #define KEY_REV "rev" 45 | #define KEY_AVG "avg" 46 | #define KEY_ROTATE "rotate" 47 | #define KEY_SAMPLES "samples" 48 | 49 | static gboolean pattern_json_check(json_object*, gchar**); 50 | static void pattern_json_parse(json_object*, pattern_t*); 51 | static pattern_data_t* pattern_json_parse_data(json_object*); 52 | static json_object* pattern_json_build(pattern_t*, gboolean); 53 | static gboolean pattern_json_build_foreach(GtkTreeModel*, GtkTreePath*, GtkTreeIter*, gpointer); 54 | static const gchar* pattern_json_format_double(gdouble); 55 | 56 | gboolean 57 | pattern_json_load(pattern_t *p, 58 | const gchar *filename, 59 | gchar **error) 60 | { 61 | gzFile gzfp; 62 | gchar buffer[READ_BUFFER]; 63 | json_tokener *json; 64 | json_object *root; 65 | enum json_tokener_error jerr; 66 | gint n, gerr; 67 | const gchar *err_string; 68 | gboolean ret; 69 | 70 | gzfp = gzopen(filename, "r"); 71 | if (gzfp == NULL) 72 | { 73 | *error = g_strdup_printf("Failed to open a file:\n%s", filename); 74 | return FALSE; 75 | } 76 | 77 | json = json_tokener_new(); 78 | do 79 | { 80 | n = gzread(gzfp, buffer, READ_BUFFER); 81 | root = json_tokener_parse_ex(json, buffer, n); 82 | 83 | if (gzeof(gzfp)) 84 | { 85 | jerr = json_tokener_get_error(json); 86 | break; 87 | } 88 | else if (n < READ_BUFFER) 89 | { 90 | err_string = gzerror(gzfp, &gerr); 91 | if (gerr) 92 | { 93 | *error = g_strdup_printf("Failed to read a file:\n%s\n%s", filename, err_string); 94 | gzclose(gzfp); 95 | json_tokener_free(json); 96 | return FALSE; 97 | } 98 | } 99 | } while ((jerr = json_tokener_get_error(json)) == json_tokener_continue); 100 | 101 | if (jerr != json_tokener_success) 102 | { 103 | *error = g_strdup_printf("Failed to parse a file:\n%s\n%s", filename, json_tokener_error_desc(jerr)); 104 | if (root != NULL) 105 | json_object_put(root); 106 | json_tokener_free(json); 107 | return FALSE; 108 | } 109 | gzclose(gzfp); 110 | 111 | ret = pattern_json_check(root, error); 112 | if (ret && 113 | p != NULL) 114 | { 115 | pattern_json_parse(root, p); 116 | pattern_set_filename(p, filename); 117 | pattern_unchanged(p); 118 | } 119 | 120 | json_object_put(root); 121 | json_tokener_free(json); 122 | return ret; 123 | } 124 | 125 | static gboolean 126 | pattern_json_check(json_object *root, 127 | gchar **error) 128 | { 129 | json_object *object; 130 | 131 | if (!json_object_object_get_ex(root, KEY_VERSION, &object)) 132 | { 133 | *error = g_strdup("Invalid file format"); 134 | return FALSE; 135 | } 136 | 137 | gint version = json_object_get_int(object); 138 | if (version != PATTERN_JSON_VERSION) 139 | { 140 | *error = g_strdup("Invalid file format version"); 141 | return FALSE; 142 | } 143 | 144 | return TRUE; 145 | } 146 | 147 | static void 148 | pattern_json_parse(json_object *root, 149 | pattern_t *p) 150 | { 151 | json_object *object; 152 | json_object *array; 153 | pattern_data_t *data; 154 | size_t len, i; 155 | 156 | /* KEY_SIZE (int) */ 157 | if (json_object_object_get_ex(root, KEY_SIZE, &object) && 158 | json_object_is_type(object, json_type_int)) 159 | { 160 | pattern_set_size(p, json_object_get_int(object)); 161 | } 162 | 163 | /* KEY_TITLE (string) */ 164 | if (json_object_object_get_ex(root, KEY_TITLE, &object) && 165 | json_object_is_type(object, json_type_string)) 166 | { 167 | pattern_set_title(p, json_object_get_string(object)); 168 | } 169 | 170 | /* KEY_SCALE (int) */ 171 | if (json_object_object_get_ex(root, KEY_SCALE, &object) && 172 | json_object_is_type(object, json_type_int)) 173 | { 174 | pattern_set_scale(p, json_object_get_int(object)); 175 | } 176 | 177 | /* KEY_LINE (double) */ 178 | if (json_object_object_get_ex(root, KEY_LINE, &object)) 179 | { 180 | if (json_object_is_type(object, json_type_double)) 181 | pattern_set_line(p, json_object_get_double(object)); 182 | else if (json_object_is_type(object, json_type_int)) 183 | pattern_set_line(p, (gdouble)json_object_get_int(object)); 184 | } 185 | 186 | /* KEY_INTERP (int) */ 187 | if (json_object_object_get_ex(root, KEY_INTERP, &object) && 188 | json_object_is_type(object, json_type_int)) 189 | { 190 | pattern_set_interp(p, json_object_get_int(object)); 191 | } 192 | 193 | /* KEY_FULL_ANGLE (boolean) */ 194 | if (json_object_object_get_ex(root, KEY_FULL_ANGLE, &object) && 195 | json_object_is_type(object, json_type_boolean)) 196 | { 197 | pattern_set_full_angle(p, json_object_get_boolean(object)); 198 | } 199 | 200 | /* KEY_BLACK (boolean) */ 201 | if (json_object_object_get_ex(root, KEY_BLACK, &object) && 202 | json_object_is_type(object, json_type_boolean)) 203 | { 204 | pattern_set_black(p, json_object_get_boolean(object)); 205 | } 206 | 207 | /* KEY_NORMALIZE (boolean) */ 208 | if (json_object_object_get_ex(root, KEY_NORMALIZE, &object) && 209 | json_object_is_type(object, json_type_boolean)) 210 | { 211 | pattern_set_normalize(p, json_object_get_boolean(object)); 212 | } 213 | 214 | /* KEY_LEGEND (boolean) */ 215 | if (json_object_object_get_ex(root, KEY_LEGEND, &object) && 216 | json_object_is_type(object, json_type_boolean)) 217 | { 218 | pattern_set_legend(p, json_object_get_boolean(object)); 219 | } 220 | 221 | /* KEY_DATA (array) */ 222 | if (json_object_object_get_ex(root, KEY_DATA, &array) && 223 | json_object_is_type(array, json_type_array)) 224 | { 225 | len = json_object_array_length(array); 226 | for (i = 0; i < len; i++) 227 | { 228 | object = json_object_array_get_idx(array, i); 229 | data = pattern_json_parse_data(object); 230 | if (data != NULL) 231 | pattern_add(p, data); 232 | } 233 | } 234 | } 235 | 236 | static pattern_data_t* 237 | pattern_json_parse_data(json_object *root) 238 | { 239 | json_object *object, *array; 240 | pattern_data_t *data; 241 | pattern_signal_t *s; 242 | size_t len, i; 243 | GdkRGBA color; 244 | 245 | if (!json_object_object_get_ex(root, KEY_SAMPLES, &array) || 246 | !json_object_is_type(array, json_type_array) || 247 | !(len = json_object_array_length(array))) 248 | { 249 | /* No signal samples */ 250 | return NULL; 251 | } 252 | 253 | s = pattern_signal_new(); 254 | for (i = 0; i < len; i++) 255 | { 256 | object = json_object_array_get_idx(array, i); 257 | if (json_object_is_type(object, json_type_double)) 258 | pattern_signal_push(s, json_object_get_double(object)); 259 | else if (json_object_is_type(object, json_type_int)) 260 | pattern_signal_push(s, json_object_get_int(object)); 261 | } 262 | 263 | if (!pattern_signal_count(s)) 264 | { 265 | /* No valid signal samples */ 266 | pattern_signal_free(s); 267 | return NULL; 268 | } 269 | 270 | pattern_signal_set_finished(s); 271 | data = pattern_data_new(s); 272 | 273 | /* KEY_NAME (string) */ 274 | if (json_object_object_get_ex(root, KEY_NAME, &object) && 275 | json_object_is_type(object, json_type_string)) 276 | { 277 | pattern_data_set_name(data, json_object_get_string(object)); 278 | } 279 | 280 | /* KEY_FREQ (int) */ 281 | if (json_object_object_get_ex(root, KEY_FREQ, &object) && 282 | json_object_is_type(object, json_type_int)) 283 | { 284 | pattern_data_set_freq(data, json_object_get_int(object)); 285 | } 286 | 287 | /* KEY_COLOR (string) */ 288 | if (json_object_object_get_ex(root, KEY_COLOR, &object) && 289 | json_object_is_type(object, json_type_string)) 290 | { 291 | if (gdk_rgba_parse(&color, json_object_get_string(object))) 292 | pattern_data_set_color(data, &color); 293 | } 294 | 295 | /* KEY_HIDE (boolean) */ 296 | if (json_object_object_get_ex(root, KEY_HIDE, &object) && 297 | json_object_is_type(object, json_type_boolean)) 298 | { 299 | pattern_data_set_hide(data, json_object_get_boolean(object)); 300 | } 301 | 302 | /* KEY_FILL (boolean) */ 303 | if (json_object_object_get_ex(root, KEY_FILL, &object) && 304 | json_object_is_type(object, json_type_boolean)) 305 | { 306 | pattern_data_set_fill(data, json_object_get_boolean(object)); 307 | } 308 | 309 | /* KEY_REV (boolean) */ 310 | if (json_object_object_get_ex(root, KEY_REV, &object) && 311 | json_object_is_type(object, json_type_boolean)) 312 | { 313 | pattern_signal_set_rev(s, json_object_get_boolean(object)); 314 | } 315 | 316 | /* KEY_AVG (int) */ 317 | if (json_object_object_get_ex(root, KEY_AVG, &object) && 318 | json_object_is_type(object, json_type_int)) 319 | { 320 | pattern_signal_set_avg(s, json_object_get_int(object)); 321 | } 322 | 323 | /* KEY_ROTATE (boolean) */ 324 | if (json_object_object_get_ex(root, KEY_ROTATE, &object) && 325 | json_object_is_type(object, json_type_int)) 326 | { 327 | pattern_signal_set_rotate(s, json_object_get_int(object)); 328 | } 329 | 330 | return data; 331 | } 332 | 333 | gboolean 334 | pattern_json_save(pattern_t *p, 335 | const gchar *filename, 336 | gboolean config) 337 | { 338 | gzFile gzfp = NULL; 339 | FILE *fp = NULL; 340 | const gchar *ext; 341 | json_object *json; 342 | const gchar *json_string; 343 | size_t json_length; 344 | size_t wrote; 345 | gint wrote_gz; 346 | 347 | ext = strrchr(filename, '.'); 348 | if (ext && !g_ascii_strcasecmp(ext, ".gz")) 349 | gzfp = gzopen(filename, "wb"); 350 | else 351 | fp = g_fopen(filename, "w"); 352 | 353 | if (!gzfp && !fp) 354 | return FALSE; 355 | 356 | json = pattern_json_build(p, config); 357 | json_string = json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED); 358 | json_length = strlen(json_string); 359 | 360 | if (gzfp) 361 | { 362 | wrote_gz = gzwrite(gzfp, json_string, (guint)json_length); 363 | wrote = wrote_gz >= 0 ? (size_t)wrote_gz : 0; 364 | gzclose(gzfp); 365 | } 366 | else 367 | { 368 | wrote = fwrite(json_string, sizeof(gchar), json_length, fp); 369 | fclose(fp); 370 | } 371 | 372 | json_object_put(json); 373 | return (json_length == wrote); 374 | } 375 | 376 | static json_object* 377 | pattern_json_build(pattern_t *p, 378 | gboolean config) 379 | { 380 | json_object *root = json_object_new_object(); 381 | json_object *array; 382 | 383 | json_object_object_add(root, KEY_VERSION, json_object_new_int(PATTERN_JSON_VERSION)); 384 | json_object_object_add(root, KEY_SIZE, json_object_new_int(pattern_get_size(p))); 385 | json_object_object_add(root, KEY_TITLE, json_object_new_string(pattern_get_title(p))); 386 | json_object_object_add(root, KEY_SCALE, json_object_new_int(pattern_get_scale(p))); 387 | json_object_object_add(root, KEY_LINE, json_object_new_double_s(pattern_get_line(p), pattern_json_format_double(pattern_get_line(p)))); 388 | json_object_object_add(root, KEY_INTERP, json_object_new_int(pattern_get_interp(p))); 389 | json_object_object_add(root, KEY_FULL_ANGLE, json_object_new_boolean(pattern_get_full_angle(p))); 390 | json_object_object_add(root, KEY_BLACK, json_object_new_boolean(pattern_get_black(p))); 391 | json_object_object_add(root, KEY_NORMALIZE, json_object_new_boolean(pattern_get_normalize(p))); 392 | json_object_object_add(root, KEY_LEGEND, json_object_new_boolean(pattern_get_legend(p))); 393 | 394 | if (!config) 395 | { 396 | array = json_object_new_array(); 397 | gtk_tree_model_foreach(GTK_TREE_MODEL(pattern_get_model(p)), pattern_json_build_foreach, array); 398 | json_object_object_add(root, KEY_DATA, array); 399 | } 400 | 401 | return root; 402 | } 403 | 404 | static gboolean 405 | pattern_json_build_foreach(GtkTreeModel *model, 406 | GtkTreePath *path, 407 | GtkTreeIter *iter, 408 | gpointer user_data) 409 | { 410 | json_object *parent = (json_object*)user_data; 411 | json_object *child = json_object_new_object(); 412 | json_object *array; 413 | pattern_data_t *data; 414 | pattern_signal_t *signal; 415 | gint n, i; 416 | gdouble sample; 417 | const gchar *format; 418 | gchar *color; 419 | 420 | gtk_tree_model_get(model, iter, PATTERN_COL_DATA, &data, -1); 421 | signal = pattern_data_get_signal(data); 422 | 423 | json_object_object_add(child, KEY_NAME, json_object_new_string(pattern_data_get_name(data))); 424 | json_object_object_add(child, KEY_FREQ, json_object_new_int(pattern_data_get_freq(data))); 425 | 426 | color = pattern_color_to_string(pattern_data_get_color(data)); 427 | json_object_object_add(child, KEY_COLOR, json_object_new_string(color)); 428 | g_free(color); 429 | 430 | json_object_object_add(child, KEY_HIDE, json_object_new_boolean(pattern_data_get_hide(data))); 431 | json_object_object_add(child, KEY_FILL, json_object_new_boolean(pattern_data_get_fill(data))); 432 | json_object_object_add(child, KEY_REV, json_object_new_boolean(pattern_signal_get_rev(signal))); 433 | json_object_object_add(child, KEY_AVG, json_object_new_int(pattern_signal_get_avg(signal))); 434 | json_object_object_add(child, KEY_ROTATE, json_object_new_int(pattern_signal_get_rotate(signal))); 435 | 436 | n = pattern_signal_count(signal); 437 | if (n) 438 | { 439 | array = json_object_new_array(); 440 | for (i = 0; i < n; i++) 441 | { 442 | sample = pattern_signal_get_sample_raw(signal, i); 443 | format = pattern_json_format_double(sample); 444 | json_object_array_add(array, json_object_new_double_s(sample, format)); 445 | } 446 | json_object_object_add(child, KEY_SAMPLES, array); 447 | } 448 | 449 | json_object_array_add(parent, child); 450 | return FALSE; 451 | } 452 | 453 | static const gchar* 454 | pattern_json_format_double(gdouble value) 455 | { 456 | static gchar output[G_ASCII_DTOSTR_BUF_SIZE]; 457 | g_ascii_formatd(output, sizeof(output), "%.10g", value); 458 | return output; 459 | } 460 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /src/pattern-ui-window.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include "version.h" 18 | #include "pattern-ui-window.h" 19 | #include "pattern-data.h" 20 | #include "pattern-signal.h" 21 | #include "pattern.h" 22 | #ifdef G_OS_WIN32 23 | #include "mingw.h" 24 | #endif 25 | 26 | static gboolean pattern_ui_window_delete(struct pattern_ui_window*); 27 | static gboolean pattern_ui_window_attach(struct pattern_ui_window*); 28 | static void pattern_ui_window_detach(struct pattern_ui_window*); 29 | 30 | struct pattern_ui_window* 31 | pattern_ui_window_new() 32 | { 33 | struct pattern_ui_window *window = g_malloc0(sizeof(struct pattern_ui_window)); 34 | 35 | window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 36 | #ifdef G_OS_WIN32 37 | g_signal_connect(window->window, "realize", G_CALLBACK(mingw_realize), NULL); 38 | #endif 39 | gtk_window_set_title(GTK_WINDOW(window->window), APP_TITLE); 40 | gtk_window_set_icon_name(GTK_WINDOW(window->window), APP_ICON); 41 | gtk_window_set_resizable(GTK_WINDOW(window->window), FALSE); 42 | gtk_window_set_position(GTK_WINDOW(window->window), GTK_WIN_POS_CENTER); 43 | gtk_container_set_border_width(GTK_CONTAINER(window->window), 5); 44 | 45 | window->window_plot = gtk_window_new(GTK_WINDOW_TOPLEVEL); 46 | #ifdef G_OS_WIN32 47 | g_signal_connect(window->window_plot, "realize", G_CALLBACK(mingw_realize), NULL); 48 | #endif 49 | gtk_window_set_title(GTK_WINDOW(window->window_plot), APP_TITLE_PLOT); 50 | gtk_window_set_icon_name(GTK_WINDOW(window->window_plot), APP_ICON); 51 | gtk_window_set_resizable(GTK_WINDOW(window->window_plot), FALSE); 52 | gtk_container_set_border_width(GTK_CONTAINER(window->window_plot), 0); 53 | 54 | window->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4); 55 | gtk_container_add(GTK_CONTAINER(window->window), window->box); 56 | 57 | window->box_buttons = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4); 58 | gtk_container_add(GTK_CONTAINER(window->box), window->box_buttons); 59 | 60 | window->box_buttons_main = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4); 61 | gtk_box_set_homogeneous(GTK_BOX(window->box_buttons_main), TRUE); 62 | gtk_box_pack_start(GTK_BOX(window->box_buttons), window->box_buttons_main, TRUE, TRUE, 0); 63 | 64 | window->b_new = gtk_button_new_with_label("New"); 65 | gtk_button_set_image(GTK_BUTTON(window->b_new), gtk_image_new_from_icon_name("document-new", GTK_ICON_SIZE_LARGE_TOOLBAR)); 66 | gtk_button_set_always_show_image(GTK_BUTTON(window->b_new), TRUE); 67 | gtk_box_pack_start(GTK_BOX(window->box_buttons_main), window->b_new, TRUE, TRUE, 0); 68 | 69 | window->b_load = gtk_button_new_with_label("Load"); 70 | gtk_button_set_image(GTK_BUTTON(window->b_load), gtk_image_new_from_icon_name("document-open", GTK_ICON_SIZE_LARGE_TOOLBAR)); 71 | gtk_button_set_always_show_image(GTK_BUTTON(window->b_load), TRUE); 72 | gtk_box_pack_start(GTK_BOX(window->box_buttons_main), window->b_load, TRUE, TRUE, 0); 73 | 74 | window->b_save = gtk_button_new_with_label("Save"); 75 | gtk_button_set_image(GTK_BUTTON(window->b_save), gtk_image_new_from_icon_name("document-save", GTK_ICON_SIZE_LARGE_TOOLBAR)); 76 | gtk_button_set_always_show_image(GTK_BUTTON(window->b_save), TRUE); 77 | gtk_box_pack_start(GTK_BOX(window->box_buttons_main), window->b_save, TRUE, TRUE, 0); 78 | 79 | window->b_save_as = gtk_button_new_with_label("Save as"); 80 | gtk_button_set_image(GTK_BUTTON(window->b_save_as), gtk_image_new_from_icon_name("document-save-as", GTK_ICON_SIZE_LARGE_TOOLBAR)); 81 | gtk_button_set_always_show_image(GTK_BUTTON(window->b_save_as), TRUE); 82 | gtk_box_pack_start(GTK_BOX(window->box_buttons_main), window->b_save_as, TRUE, TRUE, 0); 83 | 84 | window->b_render = gtk_button_new_with_label("Render"); 85 | gtk_button_set_image(GTK_BUTTON(window->b_render), gtk_image_new_from_icon_name("document-save-as", GTK_ICON_SIZE_LARGE_TOOLBAR)); 86 | gtk_button_set_always_show_image(GTK_BUTTON(window->b_render), TRUE); 87 | gtk_box_pack_start(GTK_BOX(window->box_buttons_main), window->b_render, TRUE, TRUE, 0); 88 | 89 | window->b_detach = gtk_button_new(); 90 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_detach), "Detach"); 91 | gtk_button_set_image(GTK_BUTTON(window->b_detach), gtk_image_new_from_icon_name("view-restore", GTK_ICON_SIZE_LARGE_TOOLBAR)); 92 | gtk_box_pack_start(GTK_BOX(window->box_buttons), window->b_detach, FALSE, FALSE, 0); 93 | 94 | window->b_about = gtk_button_new(); 95 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_about), "About..."); 96 | gtk_button_set_image(GTK_BUTTON(window->b_about), gtk_image_new_from_icon_name("help-about-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR)); 97 | gtk_box_pack_start(GTK_BOX(window->box_buttons), window->b_about, FALSE, FALSE, 0); 98 | 99 | window->box_header1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4); 100 | gtk_container_add(GTK_CONTAINER(window->box), window->box_header1); 101 | 102 | window->l_size = gtk_label_new("Size:"); 103 | gtk_box_pack_start(GTK_BOX(window->box_header1), window->l_size, FALSE, FALSE, 0); 104 | 105 | window->s_size = gtk_spin_button_new(GTK_ADJUSTMENT(gtk_adjustment_new(0.0, PATTERN_MIN_SIZE, PATTERN_MAX_SIZE, 10.0, 25.0, 0.0)), 0, 0); 106 | gtk_box_pack_start(GTK_BOX(window->box_header1), window->s_size, FALSE, FALSE, 0); 107 | 108 | window->l_title = gtk_label_new("Title:"); 109 | gtk_box_pack_start(GTK_BOX(window->box_header1), window->l_title, FALSE, FALSE, 0); 110 | 111 | window->e_title = gtk_entry_new(); 112 | gtk_entry_set_max_length(GTK_ENTRY(window->e_title), 100); 113 | gtk_box_pack_start(GTK_BOX(window->box_header1), window->e_title, TRUE, TRUE, 0); 114 | 115 | window->c_scale = gtk_combo_box_text_new(); 116 | gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->c_scale), "ARRL"); 117 | gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->c_scale), "20 dB"); 118 | gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->c_scale), "30 dB"); 119 | gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->c_scale), "40 dB"); 120 | gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->c_scale), "50 dB"); 121 | gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->c_scale), "60 dB"); 122 | gtk_box_pack_start(GTK_BOX(window->box_header1), window->c_scale, FALSE, FALSE, 0); 123 | 124 | window->l_line = gtk_label_new("Line:"); 125 | gtk_box_pack_start(GTK_BOX(window->box_header1), window->l_line, FALSE, FALSE, 0); 126 | 127 | window->s_line = gtk_spin_button_new(GTK_ADJUSTMENT(gtk_adjustment_new(0.0, PATTERN_MIN_LINE, PATTERN_MAX_LINE, 0.1, 0.2, 0.0)), 0, 1); 128 | gtk_box_pack_start(GTK_BOX(window->box_header1), window->s_line, FALSE, FALSE, 0); 129 | 130 | window->box_header2 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4); 131 | gtk_container_add(GTK_CONTAINER(window->box), window->box_header2); 132 | 133 | window->l_interp = gtk_label_new("Interpolation:"); 134 | gtk_box_pack_start(GTK_BOX(window->box_header2), window->l_interp, FALSE, FALSE, 0); 135 | 136 | window->c_interp = gtk_combo_box_text_new(); 137 | gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->c_interp), "Linear"); 138 | gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->c_interp), "Akima"); 139 | gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->c_interp), "Akima'"); 140 | gtk_box_pack_start(GTK_BOX(window->box_header2), window->c_interp, TRUE, TRUE, 0); 141 | 142 | window->b_full_angle = gtk_check_button_new_with_label("360°"); 143 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->b_full_angle), TRUE); 144 | gtk_box_pack_start(GTK_BOX(window->box_header2), window->b_full_angle, FALSE, FALSE, 0); 145 | 146 | window->b_black = gtk_check_button_new_with_label("Black"); 147 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->b_black), TRUE); 148 | gtk_box_pack_start(GTK_BOX(window->box_header2), window->b_black, FALSE, FALSE, 0); 149 | 150 | window->b_normalize = gtk_check_button_new_with_label("Normalize"); 151 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->b_normalize), TRUE); 152 | gtk_box_pack_start(GTK_BOX(window->box_header2), window->b_normalize, FALSE, FALSE, 0); 153 | 154 | window->b_legend = gtk_check_button_new_with_label("Legend"); 155 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->b_legend), TRUE); 156 | gtk_box_pack_start(GTK_BOX(window->box_header2), window->b_legend, FALSE, FALSE, 0); 157 | 158 | window->box_plot = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); 159 | gtk_container_add(GTK_CONTAINER(window->box), window->box_plot); 160 | 161 | window->separator = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL); 162 | gtk_box_pack_start(GTK_BOX(window->box_plot), window->separator, FALSE, FALSE, 0); 163 | 164 | window->plot = gtk_drawing_area_new(); 165 | gtk_widget_add_events(window->plot, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK); 166 | gtk_box_set_center_widget(GTK_BOX(window->box_plot), window->plot); 167 | 168 | window->box_select = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4); 169 | gtk_container_add(GTK_CONTAINER(window->box), window->box_select); 170 | 171 | window->b_add = gtk_button_new_with_label("Add"); 172 | gtk_button_set_image(GTK_BUTTON(window->b_add), gtk_image_new_from_icon_name("list-add", GTK_ICON_SIZE_LARGE_TOOLBAR)); 173 | gtk_button_set_always_show_image(GTK_BUTTON(window->b_add), TRUE); 174 | gtk_box_pack_start(GTK_BOX(window->box_select), window->b_add, FALSE, FALSE, 0); 175 | 176 | window->b_down = gtk_button_new(); 177 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_down), "Move down"); 178 | gtk_button_set_image(GTK_BUTTON(window->b_down), gtk_image_new_from_icon_name("go-down", GTK_ICON_SIZE_LARGE_TOOLBAR)); 179 | gtk_box_pack_start(GTK_BOX(window->box_select), window->b_down, FALSE, FALSE, 0); 180 | 181 | window->b_up = gtk_button_new(); 182 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_up), "Move up"); 183 | gtk_button_set_image(GTK_BUTTON(window->b_up), gtk_image_new_from_icon_name("go-up", GTK_ICON_SIZE_LARGE_TOOLBAR)); 184 | gtk_box_pack_start(GTK_BOX(window->box_select), window->b_up, FALSE, FALSE, 0); 185 | 186 | window->c_select = gtk_combo_box_new(); 187 | window->r_select = gtk_cell_renderer_text_new(); 188 | g_object_set(window->r_select, 189 | "ellipsize", PANGO_ELLIPSIZE_END, 190 | "ellipsize-set", TRUE, 191 | NULL); 192 | gtk_cell_renderer_text_set_fixed_height_from_font(GTK_CELL_RENDERER_TEXT(window->r_select), 1); 193 | gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(window->c_select), window->r_select, TRUE); 194 | gtk_box_pack_start(GTK_BOX(window->box_select), window->c_select, TRUE, TRUE, 0); 195 | 196 | window->b_export = gtk_button_new(); 197 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_export), "Export"); 198 | gtk_button_set_image(GTK_BUTTON(window->b_export), gtk_image_new_from_icon_name("document-save", GTK_ICON_SIZE_LARGE_TOOLBAR)); 199 | gtk_box_pack_start(GTK_BOX(window->box_select), window->b_export, FALSE, FALSE, 0); 200 | 201 | window->b_remove = gtk_button_new(); 202 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_remove), "Remove"); 203 | gtk_button_set_image(GTK_BUTTON(window->b_remove), gtk_image_new_from_icon_name("list-remove", GTK_ICON_SIZE_LARGE_TOOLBAR)); 204 | gtk_box_pack_start(GTK_BOX(window->box_select), window->b_remove, FALSE, FALSE, 0); 205 | 206 | window->b_clear = gtk_button_new(); 207 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_clear), "Remove all"); 208 | gtk_button_set_image(GTK_BUTTON(window->b_clear), gtk_image_new_from_icon_name("edit-clear", GTK_ICON_SIZE_LARGE_TOOLBAR)); 209 | gtk_box_pack_start(GTK_BOX(window->box_select), window->b_clear, FALSE, FALSE, 0); 210 | 211 | window->box_edit1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4); 212 | gtk_container_add(GTK_CONTAINER(window->box), window->box_edit1); 213 | 214 | window->l_name = gtk_label_new("Name:"); 215 | gtk_box_pack_start(GTK_BOX(window->box_edit1), window->l_name, FALSE, FALSE, 0); 216 | 217 | window->e_name = gtk_entry_new(); 218 | gtk_entry_set_width_chars(GTK_ENTRY(window->e_name), 12); 219 | gtk_entry_set_max_length(GTK_ENTRY(window->e_name), 50); 220 | gtk_box_pack_start(GTK_BOX(window->box_edit1), window->e_name, TRUE, TRUE, 0); 221 | 222 | window->l_freq = gtk_label_new("Freq:"); 223 | gtk_box_pack_start(GTK_BOX(window->box_edit1), window->l_freq, FALSE, FALSE, 0); 224 | 225 | window->s_freq = gtk_spin_button_new(GTK_ADJUSTMENT(gtk_adjustment_new(0.0, PATTERN_DATA_MIN_FREQ, PATTERN_DATA_MAX_FREQ, 1000.0, 10000.0, 0.0)), 0, 0); 226 | gtk_box_pack_start(GTK_BOX(window->box_edit1), window->s_freq, FALSE, FALSE, 0); 227 | 228 | window->l_avg = gtk_label_new("Avg:"); 229 | gtk_box_pack_start(GTK_BOX(window->box_edit1), window->l_avg, FALSE, FALSE, 0); 230 | 231 | window->s_avg = gtk_spin_button_new(GTK_ADJUSTMENT(gtk_adjustment_new(0.0, PATTERN_SIGNAL_MIN_AVG, PATTERN_SIGNAL_MAX_AVG, 1.0, 2.0, 0.0)), 0, 0); 232 | gtk_box_pack_start(GTK_BOX(window->box_edit1), window->s_avg, FALSE, FALSE, 0); 233 | 234 | window->b_color = gtk_color_button_new(); 235 | gtk_box_pack_start(GTK_BOX(window->box_edit1), window->b_color, FALSE, FALSE, 0); 236 | 237 | window->b_color_next = gtk_button_new(); 238 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_color_next), "Pick another color"); 239 | gtk_button_set_image(GTK_BUTTON(window->b_color_next), gtk_image_new_from_icon_name("gtk-select-color", GTK_ICON_SIZE_LARGE_TOOLBAR)); 240 | gtk_box_pack_start(GTK_BOX(window->box_edit1), window->b_color_next, FALSE, FALSE, 0); 241 | 242 | window->box_edit2 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4); 243 | gtk_container_add(GTK_CONTAINER(window->box), window->box_edit2); 244 | 245 | window->b_rotate_reset = gtk_button_new(); 246 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_rotate_reset), "Reset rotation"); 247 | gtk_button_set_image(GTK_BUTTON(window->b_rotate_reset), gtk_image_new_from_icon_name("go-home", GTK_ICON_SIZE_LARGE_TOOLBAR)); 248 | gtk_box_pack_start(GTK_BOX(window->box_edit2), window->b_rotate_reset, TRUE, TRUE, 0); 249 | 250 | window->b_rotate_ccw_fast = gtk_button_new(); 251 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_rotate_ccw_fast), "Rotate CCW fast"); 252 | gtk_button_set_image(GTK_BUTTON(window->b_rotate_ccw_fast), gtk_image_new_from_icon_name("media-seek-backward", GTK_ICON_SIZE_LARGE_TOOLBAR)); 253 | gtk_box_pack_start(GTK_BOX(window->box_edit2), window->b_rotate_ccw_fast, TRUE, TRUE, 0); 254 | 255 | window->b_rotate_ccw = gtk_button_new(); 256 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_rotate_ccw), "Rotate CCW"); 257 | gtk_button_set_image(GTK_BUTTON(window->b_rotate_ccw), gtk_image_new_from_icon_name("go-previous", GTK_ICON_SIZE_LARGE_TOOLBAR)); 258 | gtk_box_pack_start(GTK_BOX(window->box_edit2), window->b_rotate_ccw, TRUE, TRUE, 0); 259 | 260 | window->b_rotate_peak = gtk_button_new(); 261 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_rotate_peak), "Find peak"); 262 | gtk_button_set_image(GTK_BUTTON(window->b_rotate_peak), gtk_image_new_from_icon_name("go-top", GTK_ICON_SIZE_LARGE_TOOLBAR)); 263 | gtk_box_pack_start(GTK_BOX(window->box_edit2), window->b_rotate_peak, TRUE, TRUE, 0); 264 | 265 | window->b_rotate_cw = gtk_button_new(); 266 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_rotate_cw), "Rotate CW"); 267 | gtk_button_set_image(GTK_BUTTON(window->b_rotate_cw), gtk_image_new_from_icon_name("go-next", GTK_ICON_SIZE_LARGE_TOOLBAR)); 268 | gtk_box_pack_start(GTK_BOX(window->box_edit2), window->b_rotate_cw, TRUE, TRUE, 0); 269 | 270 | window->b_rotate_cw_fast = gtk_button_new(); 271 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_rotate_cw_fast), "Rotate CW fast"); 272 | gtk_button_set_image(GTK_BUTTON(window->b_rotate_cw_fast), gtk_image_new_from_icon_name("media-seek-forward", GTK_ICON_SIZE_LARGE_TOOLBAR)); 273 | gtk_box_pack_start(GTK_BOX(window->box_edit2), window->b_rotate_cw_fast, TRUE, TRUE, 0); 274 | 275 | window->b_fill = gtk_check_button_new_with_label("Fill"); 276 | gtk_box_pack_start(GTK_BOX(window->box_edit2), window->b_fill, FALSE, FALSE, 0); 277 | 278 | window->b_rev = gtk_check_button_new_with_label("Rev"); 279 | gtk_box_pack_start(GTK_BOX(window->box_edit2), window->b_rev, FALSE, FALSE, 0); 280 | 281 | window->b_hide = gtk_check_button_new_with_label("Hide"); 282 | gtk_box_pack_start(GTK_BOX(window->box_edit2), window->b_hide, FALSE, FALSE, 0); 283 | 284 | g_signal_connect_data(window->window, "delete-event", G_CALLBACK(pattern_ui_window_delete), window, NULL, G_CONNECT_AFTER | G_CONNECT_SWAPPED); 285 | g_signal_connect_swapped(window->window_plot, "delete-event", G_CALLBACK(pattern_ui_window_attach), window); 286 | g_signal_connect_swapped(window->b_detach, "clicked", G_CALLBACK(pattern_ui_window_detach), window); 287 | return window; 288 | } 289 | 290 | static gboolean 291 | pattern_ui_window_delete(struct pattern_ui_window *window) 292 | { 293 | /* Destroy the plot window first */ 294 | gtk_widget_destroy(GTK_WIDGET(window->window_plot)); 295 | 296 | return FALSE; 297 | } 298 | 299 | static gboolean 300 | pattern_ui_window_attach(struct pattern_ui_window *window) 301 | { 302 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_detach), "Detach"); 303 | g_signal_handlers_disconnect_by_func(window->b_detach, G_CALLBACK(pattern_ui_window_attach), (gpointer)window); 304 | g_signal_connect_swapped(window->b_detach, "clicked", G_CALLBACK(pattern_ui_window_detach), (gpointer)window); 305 | 306 | gtk_widget_hide(window->window_plot); 307 | gtk_widget_hide(window->separator); 308 | g_object_ref(window->plot); 309 | gtk_container_remove(GTK_CONTAINER(window->window_plot), window->plot); 310 | gtk_box_set_center_widget(GTK_BOX(window->box_plot), window->plot); 311 | g_object_unref(window->plot); 312 | 313 | return TRUE; 314 | } 315 | 316 | static void 317 | pattern_ui_window_detach(struct pattern_ui_window *window) 318 | { 319 | gtk_widget_set_tooltip_text(GTK_WIDGET(window->b_detach), "Attach"); 320 | g_signal_handlers_disconnect_by_func(window->b_detach, G_CALLBACK(pattern_ui_window_detach), (gpointer)window); 321 | g_signal_connect_swapped(window->b_detach, "clicked", G_CALLBACK(pattern_ui_window_attach), (gpointer)window); 322 | 323 | g_object_ref(window->plot); 324 | gtk_container_remove(GTK_CONTAINER(window->box_plot), window->plot); 325 | gtk_container_add(GTK_CONTAINER(window->window_plot), window->plot); 326 | g_object_unref(window->plot); 327 | gtk_widget_show(window->separator); 328 | gtk_window_set_transient_for(GTK_WINDOW(window->window_plot), GTK_WINDOW(window->window)); 329 | gtk_widget_show_all(window->window_plot); 330 | gtk_window_set_transient_for(GTK_WINDOW(window->window_plot), NULL); 331 | } 332 | 333 | void 334 | pattern_ui_window_set_title(struct pattern_ui_window *window, 335 | const gchar *filename) 336 | { 337 | gchar *title; 338 | gchar *plot_title; 339 | gchar *name; 340 | gchar *ext; 341 | 342 | if (filename) 343 | { 344 | name = g_path_get_basename(filename); 345 | ext = strrchr(name, '.'); 346 | if (ext && !g_ascii_strcasecmp(ext, APP_FILE_COMPRESS)) 347 | { 348 | *ext = '\0'; 349 | ext = strrchr(name, '.'); 350 | } 351 | if (ext && !g_ascii_strcasecmp(ext, APP_FILE_EXT)) 352 | *ext = '\0'; 353 | 354 | title = g_strdup_printf("%s [%s]", APP_TITLE, name); 355 | plot_title = g_strdup_printf("%s [%s]", APP_TITLE_PLOT, name); 356 | g_free(name); 357 | } 358 | else 359 | { 360 | title = g_strdup_printf("%s", APP_TITLE); 361 | plot_title = g_strdup_printf("%s", APP_TITLE_PLOT); 362 | } 363 | 364 | gtk_window_set_title(GTK_WINDOW(window->window), title); 365 | gtk_window_set_title(GTK_WINDOW(window->window_plot), plot_title); 366 | g_free(title); 367 | g_free(plot_title); 368 | } 369 | -------------------------------------------------------------------------------- /src/pattern-plot.c: -------------------------------------------------------------------------------- 1 | /* 2 | * antpatt - antenna pattern plotting and analysis software 3 | * Copyright (c) 2017-2023 Konrad Kosmatka 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include "pattern.h" 20 | #include "pattern-plot.h" 21 | #include "pattern-misc.h" 22 | #include "pattern-ui.h" 23 | 24 | #define PATTERN_FONT_SIZE_TITLE 16.0 25 | #define PATTERN_FONT_SIZE_SCALE 11.0 26 | #define PATTERN_FONT_SIZE_LEGEND 13.0 27 | #define PATTERN_FONT_SIZE_FREQ 15.0 28 | #define PATTERN_LEGEND_SPACING 4.0 29 | #define PATTERN_ANGLE_LABEL_OFF 15.0 30 | #define PATTERN_PLOT_LINE_WIDTH 1.25 31 | #define PATTERN_PLOT_LEGEND_WIDTH 1.5 32 | #define PATTERN_PLOT_FG_ALPHA 0.90 33 | #define PATTERN_PLOT_BG_ALPHA 0.18 34 | #define PATTERN_FONT "DejaVu Sans Mono" 35 | 36 | #define DEG2RAD(DEG) ((DEG) * M_PI / 180.0) 37 | 38 | typedef struct 39 | { 40 | gint width; 41 | gdouble line; 42 | const gchar *title; 43 | gint scale; 44 | gboolean full_angle; 45 | gboolean black; 46 | gboolean norm; 47 | gboolean legend; 48 | gdouble offset; 49 | gdouble radius; 50 | gdouble peak; 51 | } pattern_plot_t; 52 | 53 | static void pattern_plot_title(cairo_t*, pattern_plot_t*); 54 | static void pattern_plot_coords(cairo_t*, pattern_plot_t*); 55 | static void pattern_plot_grid(cairo_t*, pattern_plot_t*); 56 | static void pattern_plot_radiation(cairo_t*, pattern_plot_t*, pattern_t*); 57 | static void pattern_plot_radiation_data(cairo_t*, pattern_plot_t*, pattern_data_t*, gdouble, gdouble); 58 | static void pattern_plot_legend(cairo_t*, pattern_plot_t*, pattern_data_t*, gint, gint); 59 | static void pattern_plot_frequency(cairo_t*, pattern_plot_t*, gint); 60 | static void pattern_plot_focus(cairo_t*, pattern_plot_t*, pattern_t*); 61 | static void pattern_plot_pointer(cairo_t*, pattern_plot_t*, pattern_t*, pattern_data_t*); 62 | static void pattern_plot_info(cairo_t*, pattern_plot_t*, pattern_t*, pattern_data_t*); 63 | static void pattern_plot_stats(cairo_t*, pattern_plot_t*, pattern_t*, pattern_data_t*); 64 | 65 | static gdouble pattern_plot_signal(gint, gdouble); 66 | 67 | 68 | void 69 | pattern_plot(cairo_t *cr, 70 | pattern_t *p) 71 | { 72 | pattern_plot_t plot; 73 | 74 | plot.width = pattern_get_size(p); 75 | plot.line = pattern_get_line(p); 76 | plot.title = pattern_get_title(p); 77 | plot.scale = pattern_get_scale(p); 78 | plot.full_angle = pattern_get_full_angle(p); 79 | plot.black = pattern_get_black(p); 80 | plot.norm = pattern_get_normalize(p); 81 | plot.legend = pattern_get_legend(p); 82 | 83 | plot.offset = plot.width / (PATTERN_PLOT_BASE_SIZE / PATTERN_PLOT_OFFSET); 84 | plot.radius = plot.width / 2.0 - plot.offset; 85 | plot.peak = plot.norm ? NAN : pattern_get_peak(p); 86 | 87 | /* clear the canvas */ 88 | cairo_set_source_rgb(cr, (plot.black ? 0.0 : 1.0), (plot.black ? 0.0 : 1.0), (plot.black ? 0.0 : 1.0)); 89 | cairo_paint(cr); 90 | 91 | /* set default font face */ 92 | cairo_select_font_face(cr, PATTERN_FONT, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); 93 | 94 | /* display title label */ 95 | pattern_plot_title(cr, &plot); 96 | 97 | /* draw the polar coordinates */ 98 | pattern_plot_coords(cr, &plot); 99 | 100 | /* draw the grid */ 101 | pattern_plot_grid(cr, &plot); 102 | 103 | /* plot all patterns */ 104 | pattern_plot_radiation(cr, &plot, p); 105 | 106 | /* mark focused data point */ 107 | pattern_plot_focus(cr, &plot, p); 108 | } 109 | 110 | static void 111 | pattern_plot_title(cairo_t *cr, 112 | pattern_plot_t *plot) 113 | { 114 | cairo_text_extents_t extents; 115 | gdouble font_height = plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_FONT_SIZE_TITLE); 116 | gdouble x, y; 117 | 118 | cairo_set_source_rgb(cr, (plot->black ? 1.0 : 0.0), (plot->black ? 1.0 : 0.0), (plot->black ? 1.0 : 0.0)); 119 | cairo_set_font_size(cr, font_height); 120 | cairo_text_extents(cr, plot->title, &extents); 121 | 122 | x = (plot->width - extents.width) / 2.0; 123 | y = font_height+(plot->width / (PATTERN_PLOT_BASE_SIZE / 2.0)); 124 | cairo_move_to(cr, round(x), round(y)); 125 | cairo_show_text(cr, plot->title); 126 | cairo_stroke(cr); 127 | } 128 | 129 | static void 130 | pattern_plot_coords(cairo_t *cr, 131 | pattern_plot_t *plot) 132 | { 133 | gdouble line_width = plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_PLOT_BORDER_WIDTH); 134 | cairo_set_source_rgb(cr, (plot->black ? 0.6 : 0.4), (plot->black ? 0.6 : 0.4), (plot->black ? 0.6 : 0.4)); 135 | cairo_set_line_width(cr, line_width); 136 | cairo_set_font_size(cr, plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_FONT_SIZE_SCALE)); 137 | cairo_arc(cr, plot->radius + plot->offset, plot->radius + plot->offset, plot->radius, 0, 2 * M_PI); 138 | cairo_stroke(cr); 139 | } 140 | 141 | static void 142 | pattern_plot_grid(cairo_t *cr, 143 | pattern_plot_t *plot) 144 | { 145 | static const gint scales[] = {-3, -10, -20, -30, -40, -50, 0}; 146 | static const gdouble dash[] = {1.0, 2.0}; 147 | static const gint dash_len = sizeof(dash) / sizeof(dash[0]); 148 | cairo_text_extents_t extents; 149 | gdouble l, x, y; 150 | gint i, j, k, n; 151 | gint font_height = (gint)(plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_FONT_SIZE_SCALE)); 152 | gchar text[20]; 153 | gboolean draw; 154 | gint limit; 155 | gint fill; 156 | 157 | if (plot->scale) 158 | { 159 | limit = plot->scale + 10; 160 | fill = plot->scale; 161 | } 162 | else /* ARRL */ 163 | { 164 | limit = -40; 165 | fill = -30; 166 | } 167 | 168 | cairo_set_line_width(cr, plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_PLOT_BORDER_WIDTH)); 169 | cairo_set_font_size(cr, font_height); 170 | cairo_set_dash(cr, dash, dash_len, 0); 171 | for (i = 0; scales[i]; i++) 172 | { 173 | if (scales[i] < limit) 174 | break; 175 | 176 | cairo_arc(cr, 177 | plot->radius + plot->offset, 178 | plot->radius + plot->offset, 179 | plot->radius*pattern_plot_signal(plot->scale, scales[i]), 180 | -0.5 * M_PI, 181 | 1.5 * M_PI); 182 | cairo_stroke_preserve(cr); 183 | g_snprintf(text, sizeof(text), "%d", scales[i]); 184 | cairo_text_extents(cr, text, &extents); 185 | cairo_get_current_point(cr, &x, &y); 186 | x += -(extents.width / 2.0 + extents.x_bearing) - 0.5; 187 | y += font_height; 188 | cairo_move_to(cr, round(x), round(y)); 189 | cairo_show_text(cr, text); 190 | cairo_stroke(cr); 191 | } 192 | cairo_set_dash(cr, dash, 0, 0); 193 | 194 | cairo_set_line_width(cr, plot->width/(PATTERN_PLOT_BASE_SIZE / PATTERN_PLOT_BORDER_WIDTH)); 195 | k = 180; 196 | for (i = 0; i < 360; i += 10) 197 | { 198 | for (j = -1; j > fill; j -= 1) 199 | { 200 | if (i % 30 && j % 2) 201 | continue; 202 | 203 | draw = TRUE; 204 | for (n = 0; scales[n]; n++) 205 | if (scales[n] == j) 206 | draw = FALSE; 207 | 208 | /* dots */ 209 | if (draw) 210 | { 211 | l = plot->radius * pattern_plot_signal(plot->scale, j); 212 | x = plot->offset + plot->radius + sin(DEG2RAD(i)) * l; 213 | y = plot->offset + plot->radius + cos(DEG2RAD(i)) * l; 214 | cairo_set_line_width(cr, plot->width / (PATTERN_PLOT_BASE_SIZE / (PATTERN_PLOT_BORDER_WIDTH / 2.0))); 215 | cairo_arc(cr, x, y, 0.5, 0, 2*M_PI); 216 | cairo_stroke_preserve(cr); 217 | cairo_fill(cr); 218 | } 219 | } 220 | 221 | /* angle labels */ 222 | if (k && (k % 30) == 0) 223 | { 224 | if (plot->full_angle && k < 0) 225 | k = 360 + k; 226 | 227 | l = plot->radius + plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_ANGLE_LABEL_OFF); 228 | x = plot->offset + plot->radius + sin(DEG2RAD(i)) * l; 229 | y = plot->offset + plot->radius + cos(DEG2RAD(i)) * l; 230 | 231 | if (!plot->full_angle && k == 180) 232 | g_snprintf(text, sizeof(text), "±%d°", k); 233 | else 234 | g_snprintf(text, sizeof(text), "%d°", k); 235 | 236 | cairo_text_extents(cr, text, &extents); 237 | x -= extents.width / 2.0 + extents.x_bearing; 238 | y -= extents.height / 2.0 + extents.y_bearing; 239 | 240 | cairo_set_source_rgb(cr, (plot->black ? 0.75 : 0.25), (plot->black ? 0.75 : 0.25), (plot->black ? 0.75 : 0.25)); 241 | cairo_move_to(cr, round(x), round(y)); 242 | cairo_show_text(cr, text); 243 | cairo_stroke(cr); 244 | } 245 | k -= 10; 246 | } 247 | } 248 | 249 | static void 250 | pattern_plot_radiation(cairo_t *cr, 251 | pattern_plot_t *plot, 252 | pattern_t *p) 253 | { 254 | pattern_data_t *data; 255 | pattern_signal_t *s; 256 | GtkTreeIter iter; 257 | gint show_frequency = FALSE; 258 | gint frequency = 0; 259 | gint new_frequency; 260 | gint i = 0; 261 | gdouble line_width = plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_PLOT_LINE_WIDTH) * plot->line; 262 | 263 | if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(pattern_get_model(p)), &iter)) 264 | { 265 | do 266 | { 267 | gtk_tree_model_get(GTK_TREE_MODEL(pattern_get_model(p)), &iter, PATTERN_COL_DATA, &data, -1); 268 | 269 | if (pattern_data_get_hide(data)) 270 | continue; 271 | 272 | if (plot->legend) 273 | pattern_plot_legend(cr, plot, data, pattern_get_visible_count(p), i); 274 | 275 | if (i == 0) 276 | { 277 | frequency = pattern_data_get_freq(data); 278 | show_frequency = TRUE; 279 | } 280 | else if (show_frequency) 281 | { 282 | new_frequency = pattern_data_get_freq(data); 283 | if (new_frequency) 284 | { 285 | if (frequency == 0) 286 | frequency = new_frequency; 287 | else 288 | show_frequency = (new_frequency == frequency); 289 | } 290 | } 291 | 292 | pattern_plot_radiation_data(cr, 293 | plot, 294 | data, 295 | (plot->norm ? pattern_signal_get_peak(pattern_data_get_signal(data)) : plot->peak), 296 | line_width); 297 | 298 | i++; 299 | } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(pattern_get_model(p)), &iter)); 300 | 301 | /* draw frequency label */ 302 | if (show_frequency && frequency) 303 | pattern_plot_frequency(cr, plot, frequency); 304 | 305 | cairo_stroke(cr); 306 | } 307 | } 308 | 309 | static void 310 | pattern_plot_radiation_data(cairo_t *cr, 311 | pattern_plot_t *plot, 312 | pattern_data_t *data, 313 | gdouble peak, 314 | gdouble line_width) 315 | { 316 | gdouble x, y, len, ang; 317 | gint i, j; 318 | gdouble sample; 319 | gboolean finished; 320 | pattern_signal_t *s = pattern_data_get_signal(data); 321 | const GdkRGBA *color = pattern_data_get_color(data); 322 | gint interp = pattern_signal_interp(s); 323 | gint count = pattern_signal_count(s); 324 | 325 | if (!count) 326 | return; 327 | 328 | cairo_set_line_width(cr, line_width); 329 | cairo_set_source_rgba(cr, 330 | color->red, 331 | color->green, 332 | color->blue, 333 | PATTERN_PLOT_FG_ALPHA); 334 | 335 | finished = pattern_signal_get_finished(s); 336 | 337 | for (i = 0; i < count; i++) 338 | { 339 | for (j = 0; j < interp; j++) 340 | { 341 | /* We want to start from the first sample */ 342 | gint idx = i - pattern_signal_get_rotate(s); 343 | 344 | sample = pattern_signal_get_sample_interp(s, idx, j / (gdouble)interp); 345 | len = plot->radius * pattern_plot_signal(plot->scale, sample - peak); 346 | ang = M_PI - (idx * interp + j) / (count * (gdouble)interp) * 2.0 * M_PI; 347 | x = plot->offset + plot->radius + sin(ang) * len; 348 | y = plot->offset + plot->radius + cos(ang) * len; 349 | cairo_line_to(cr, x, y); 350 | 351 | if (i == count-1 && !finished) 352 | break; 353 | } 354 | } 355 | 356 | if (finished) 357 | cairo_close_path(cr); 358 | 359 | if (pattern_data_get_fill(data)) 360 | { 361 | cairo_stroke_preserve(cr); 362 | cairo_set_source_rgba(cr, 363 | color->red, 364 | color->green, 365 | color->blue, 366 | PATTERN_PLOT_BG_ALPHA); 367 | cairo_fill(cr); 368 | } 369 | else 370 | { 371 | cairo_stroke(cr); 372 | } 373 | } 374 | 375 | static void 376 | pattern_plot_legend(cairo_t *cr, 377 | pattern_plot_t *plot, 378 | pattern_data_t *data, 379 | gint visible, 380 | gint index) 381 | { 382 | gint offset = (gint)(plot->width / (PATTERN_PLOT_BASE_SIZE / (PATTERN_PLOT_OFFSET / 4.0))); 383 | const GdkRGBA *color = pattern_data_get_color(data); 384 | 385 | gint line_height = (gint)(plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_PLOT_LEGEND_WIDTH)); 386 | gint font_height = (gint)(plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_FONT_SIZE_LEGEND)); 387 | gint spacing = (gint)(plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_LEGEND_SPACING)); 388 | gint x, y; 389 | 390 | cairo_set_line_width(cr, line_height); 391 | cairo_set_source_rgba(cr, 392 | color->red, 393 | color->green, 394 | color->blue, 395 | PATTERN_PLOT_FG_ALPHA); 396 | x = offset; 397 | y = plot->width - (visible - index) * (font_height + spacing + line_height) + spacing - offset; 398 | 399 | cairo_rectangle(cr, x + 0.5, y + 0.5, font_height, font_height); 400 | cairo_stroke_preserve(cr); 401 | cairo_set_source_rgba(cr, 402 | color->red, 403 | color->green, 404 | color->blue, 405 | PATTERN_PLOT_BG_ALPHA); 406 | cairo_fill(cr); 407 | 408 | cairo_set_source_rgba(cr, 409 | color->red, 410 | color->green, 411 | color->blue, 412 | 1.0); 413 | 414 | x += font_height + spacing + line_height; 415 | y += font_height - line_height; 416 | 417 | cairo_set_font_size(cr, font_height); 418 | cairo_move_to(cr, round(x), round(y)); 419 | cairo_show_text(cr, pattern_data_get_name(data)); 420 | cairo_stroke(cr); 421 | } 422 | 423 | static void 424 | pattern_plot_frequency(cairo_t *cr, 425 | pattern_plot_t *plot, 426 | gint freq) 427 | { 428 | gdouble offset = plot->width / (PATTERN_PLOT_BASE_SIZE / (PATTERN_PLOT_OFFSET / 4.0)); 429 | cairo_text_extents_t extents; 430 | gchar *text = pattern_misc_format_frequency(freq); 431 | gdouble x, y; 432 | 433 | cairo_set_source_rgb(cr, (plot->black ? 1.0 : 0.0), (plot->black ? 1.0 : 0.0), (plot->black ? 1.0 : 0.0)); 434 | cairo_set_font_size(cr, plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_FONT_SIZE_FREQ)); 435 | cairo_text_extents(cr, text, &extents); 436 | 437 | x = plot->width - extents.width - offset; 438 | y = plot->width - offset; 439 | cairo_move_to(cr, round(x), round(y)); 440 | cairo_show_text(cr, text); 441 | cairo_stroke(cr); 442 | 443 | g_free(text); 444 | } 445 | 446 | static void 447 | pattern_plot_focus(cairo_t *cr, 448 | pattern_plot_t *plot, 449 | pattern_t *p) 450 | { 451 | pattern_data_t *data = pattern_get_current(p); 452 | pattern_ui_t *ui = pattern_get_ui(p); 453 | 454 | if (data == NULL) 455 | return; 456 | 457 | if (ui == NULL) 458 | return; 459 | 460 | if (pattern_ui_get_focus_idx(ui) == -1) 461 | return; 462 | 463 | pattern_plot_pointer(cr, plot, p, data); 464 | pattern_plot_info(cr, plot, p, data); 465 | pattern_plot_stats(cr, plot, p, data); 466 | } 467 | 468 | 469 | static void 470 | pattern_plot_pointer(cairo_t *cr, 471 | pattern_plot_t *plot, 472 | pattern_t *p, 473 | pattern_data_t *data) 474 | { 475 | pattern_ui_t *ui = pattern_get_ui(p); 476 | pattern_signal_t *s = pattern_data_get_signal(data); 477 | gint count = pattern_signal_count(s); 478 | gdouble peak = (plot->norm ? pattern_signal_get_peak(s) : plot->peak); 479 | gdouble value = pattern_signal_get_sample(s, pattern_ui_get_focus_idx(ui)); 480 | gdouble line_width = plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_PLOT_LINE_WIDTH); 481 | gdouble len = plot->radius * pattern_plot_signal(plot->scale, value - peak); 482 | gdouble ang = M_PI - pattern_ui_get_focus_idx(ui) / (gdouble) count * 2.0 * M_PI; 483 | gdouble x = plot->offset + plot->radius + sin(ang) * len; 484 | gdouble y = plot->offset + plot->radius + cos(ang) * len; 485 | 486 | cairo_set_source_rgb(cr, (plot->black ? 0.75 : 0.25), (plot->black ? 0.75 : 0.25), (plot->black ? 0.75 : 0.25)); 487 | cairo_set_line_width(cr, line_width); 488 | cairo_arc(cr, x, y, line_width * 3.0, 0, 2 * M_PI); 489 | cairo_stroke(cr); 490 | } 491 | 492 | static void 493 | pattern_plot_info(cairo_t *cr, 494 | pattern_plot_t *plot, 495 | pattern_t *p, 496 | pattern_data_t *data) 497 | { 498 | pattern_ui_t *ui = pattern_get_ui(p); 499 | pattern_signal_t *s = pattern_data_get_signal(data); 500 | gint count = pattern_signal_count(s); 501 | gint idx = pattern_ui_get_focus_idx(ui); 502 | gdouble angle = idx / (gdouble)count * 360.0; 503 | gdouble peak = (plot->norm ? pattern_signal_get_peak(pattern_data_get_signal(data)) : plot->peak); 504 | const GdkRGBA *color = pattern_data_get_color(data); 505 | gint offset = (gint)(plot->width / (PATTERN_PLOT_BASE_SIZE / (PATTERN_PLOT_OFFSET / 4.0))); 506 | gint font_height = (gint)(plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_FONT_SIZE_LEGEND)); 507 | gint spacing = (gint)(plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_LEGEND_SPACING)); 508 | gchar buff[50]; 509 | gint x, y; 510 | 511 | if (!pattern_get_full_angle(p) && angle > 180.0) 512 | angle -= 360.0; 513 | 514 | cairo_set_source_rgba(cr, 515 | color->red, 516 | color->green, 517 | color->blue, 518 | 1.0); 519 | cairo_set_font_size(cr, font_height); 520 | x = offset; 521 | 522 | y = (gint)plot->offset + spacing; 523 | cairo_move_to(cr, round(x), round(y)); 524 | g_snprintf(buff, sizeof(buff), "Angle: %.1f°", angle); 525 | cairo_show_text(cr, buff); 526 | cairo_stroke(cr); 527 | 528 | y += font_height + spacing; 529 | cairo_move_to(cr, round(x), round(y)); 530 | g_snprintf(buff, sizeof(buff), "Val: %.1f dB", pattern_signal_get_sample(s, idx)); 531 | cairo_show_text(cr, buff); 532 | cairo_stroke(cr); 533 | 534 | y += font_height + spacing; 535 | cairo_move_to(cr, round(x), round(y)); 536 | g_snprintf(buff, sizeof(buff), "Att: %.1f dB", pattern_signal_get_sample(s, idx) - peak); 537 | cairo_show_text(cr, buff); 538 | cairo_stroke(cr); 539 | } 540 | 541 | static void 542 | pattern_plot_stats(cairo_t *cr, 543 | pattern_plot_t *plot, 544 | pattern_t *p, 545 | pattern_data_t *data) 546 | { 547 | const pattern_signal_t *s = pattern_data_get_signal(data); 548 | gdouble max = pattern_signal_get_peak(s); 549 | gdouble min = pattern_signal_get_min(s); 550 | gdouble delta = max - min; 551 | 552 | const GdkRGBA *color = pattern_data_get_color(data); 553 | cairo_set_source_rgba(cr, 554 | color->red, 555 | color->green, 556 | color->blue, 557 | 1.0); 558 | 559 | 560 | gint offset = (gint)(plot->width / (PATTERN_PLOT_BASE_SIZE / (PATTERN_PLOT_OFFSET / 4.0))); 561 | gint font_height = (gint)(plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_FONT_SIZE_LEGEND)); 562 | gint spacing = (gint)(plot->width / (PATTERN_PLOT_BASE_SIZE / PATTERN_LEGEND_SPACING)); 563 | gchar text[50]; 564 | gint x, y; 565 | 566 | cairo_text_extents_t extents; 567 | cairo_set_font_size(cr, font_height); 568 | 569 | g_snprintf(text, sizeof(text), "Max: %.1f dB", max); 570 | cairo_text_extents(cr, text, &extents); 571 | x = plot->width - extents.width - offset; 572 | y = (gint)plot->offset + spacing; 573 | cairo_move_to(cr, round(x), round(y)); 574 | cairo_show_text(cr, text); 575 | cairo_stroke(cr); 576 | 577 | g_snprintf(text, sizeof(text), "Min: %.1f dB", min); 578 | cairo_text_extents(cr, text, &extents); 579 | x = plot->width - extents.width - offset; 580 | y += font_height + spacing; 581 | cairo_move_to(cr, round(x), round(y)); 582 | cairo_show_text(cr, text); 583 | cairo_stroke(cr); 584 | 585 | g_snprintf(text, sizeof(text), "\u0394: %.1f dB", delta); 586 | cairo_text_extents(cr, text, &extents); 587 | x = plot->width - extents.width - offset; 588 | y += font_height + spacing; 589 | cairo_move_to(cr, round(x), round(y)); 590 | cairo_show_text(cr, text); 591 | cairo_stroke(cr); 592 | } 593 | 594 | gboolean 595 | pattern_plot_to_file(const gchar *filename, 596 | pattern_t *p) 597 | { 598 | cairo_surface_t *surface; 599 | cairo_t *cr; 600 | gboolean ret = FALSE; 601 | gint size = pattern_get_size(p); 602 | gchar *ext = strrchr(filename, '.'); 603 | gboolean png; 604 | 605 | if (ext && g_ascii_strcasecmp(ext, ".svg") == 0) 606 | { 607 | /* SVG vector file */ 608 | surface = cairo_svg_surface_create(filename, size, size); 609 | png = FALSE; 610 | } 611 | else 612 | { 613 | /* Other: PNG file */ 614 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size); 615 | png = TRUE; 616 | } 617 | 618 | if (surface == NULL) 619 | return FALSE; 620 | 621 | if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) 622 | { 623 | cairo_surface_destroy(surface); 624 | return FALSE; 625 | } 626 | 627 | cr = cairo_create(surface); 628 | if (cr) 629 | { 630 | pattern_plot(cr, p); 631 | cairo_destroy(cr); 632 | 633 | if (png) 634 | ret = cairo_surface_write_to_png(surface, filename) == CAIRO_STATUS_SUCCESS; 635 | else 636 | ret = cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS; 637 | } 638 | 639 | cairo_surface_destroy(surface); 640 | return ret; 641 | } 642 | 643 | static gdouble 644 | pattern_plot_signal(gint scale, 645 | gdouble x) 646 | { 647 | if (scale) /* Linear */ 648 | return (x > scale ? -x / scale + 1.0 : 0.0); 649 | else /* ARRL */ 650 | return pow(0.89, (-0.5 * x)); 651 | } 652 | --------------------------------------------------------------------------------