BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
appoptions.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/main/appoptions.cpp
6 //! @brief Implements class ProgramOptions.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "GUI/main/appoptions.h"
17 #include <QSize>
18 #include <boost/program_options/config.hpp>
19 #include <boost/program_options/parsers.hpp>
20 #include <fstream>
21 #include <iostream>
22 
23 namespace {
24 const char* geometry = "geometry";
25 const char* nohighdpi = "nohighdpi";
26 
27 //! Converts string "1600x1000" to QSize(1600, 1000)
28 QSize windowSize(const QString& size_string)
29 {
30  auto list = size_string.split("x");
31 
32  if (list.size() != 2)
33  return QSize();
34 
35  return QSize(list[0].toInt(), list[1].toInt());
36 }
37 
38 //! Returns true if windows size makes sence.
39 bool isValid(const QSize& win_size)
40 {
41  if (win_size.width() > 640 && win_size.height() > 480)
42  return true;
43 
44  return false;
45 }
46 
47 } // namespace
48 
49 ApplicationOptions::ApplicationOptions(int argc, char** argv) : m_options_is_consistent(false)
50 {
51  m_options.add_options()("help,h", "print help message");
52  m_options.add_options()("with-debug", "run application with debug printout");
53  m_options.add_options()("no-splash", "do not show splash screen");
54  m_options.add_options()(geometry, bpo::value<std::string>(),
55  "Main window geometry, e.g. 1600x1000");
56  m_options.add_options()(nohighdpi, "Run without high-dpi support");
57 
58  parseCommandLine(argc, argv);
59 
61 }
62 
63 //! access variables
64 
65 const bpo::variable_value& ApplicationOptions::operator[](const std::string& s) const
66 {
67  return m_variables_map[s.c_str()];
68 }
69 
70 bool ApplicationOptions::find(std::string name) const
71 {
72  return (m_variables_map.count(name.c_str()));
73 }
74 
76 {
78 }
79 
80 //! parse command line arguments
81 
82 void ApplicationOptions::parseCommandLine(int argc, char** argv)
83 {
85  // parsing command line arguments
86  try {
87  // if positional option description is empty, no command line arguments
88  // without '--' or '-' will be accepted
89  // 'store' populates the variable map
90  bpo::store(bpo::command_line_parser(argc, argv)
91  .options(m_options)
92  .positional(m_positional_options)
93  .run(),
95  // bpo::store(bpo::parse_command_line(argc, argv, m_options), m_variables_map);
96 
97  // 'notify' raises any errors encountered
98  bpo::notify(m_variables_map);
99 
101  } catch (std::exception& e) {
102  // we get here if there is unrecognized options
103  std::cout << "main() -> " << e.what() << std::endl;
104  std::cout << m_options << std::endl;
105  }
106 }
107 
108 boost::program_options::variables_map& ApplicationOptions::getVariables()
109 {
110  return m_variables_map;
111 }
112 
113 boost::program_options::options_description& ApplicationOptions::getOptions()
114 {
115  return m_options;
116 }
117 
119 {
120  if (m_variables_map.count("help")) {
122  m_options_is_consistent = false;
123  }
124 
125  else if (m_variables_map.count("version")) {
126  std::cout << "BornAgain-" << GUIHelpers::getBornAgainVersionString().toStdString()
127  << std::endl;
128  m_options_is_consistent = false;
129  }
130 
131  else if (m_variables_map.count(geometry)) {
132  if (!isValid(mainWindowSize())) {
133  std::cout << "Wrong window size, try --geometry=1600x900\n";
134  m_options_is_consistent = false;
135  }
136  }
137 }
138 
140 {
141  std::cout << "BornAgain Graphical User Interface" << std::endl;
142  std::cout << m_options << std::endl;
143 }
144 
146 {
147  QString size_str = QString::fromStdString(m_variables_map[geometry].as<std::string>());
148  return windowSize(size_str);
149 }
150 
152 {
153  return find(nohighdpi);
154 }
Defines class GUIHelpers functions.
Collection of utilities to parse command line options.
bool find(std::string name) const
Returns true if option with given name has been set.
Definition: appoptions.cpp:70
bpo::variables_map & getVariables()
Returns reference to the variables container.
Definition: appoptions.cpp:108
void parseCommandLine(int argc, char **argv)
Parses command line arguments.
Definition: appoptions.cpp:82
bpo::options_description & getOptions()
Returns reference to the options description.
Definition: appoptions.cpp:113
bool m_options_is_consistent
true if options are consistent (no conflicts, no –help request)
Definition: appoptions.h:64
bpo::options_description m_options
options description, to be filled with add() from different program modules
Definition: appoptions.h:66
ApplicationOptions(int argc=0, char **argv=0)
Definition: appoptions.cpp:49
bool disableHighDPISupport()
Definition: appoptions.cpp:151
bpo::variables_map m_variables_map
Definition: appoptions.h:69
bool isConsistent() const
Returns true if options are consistent (no conflicts, no –help request)
Definition: appoptions.cpp:75
QSize mainWindowSize() const
Definition: appoptions.cpp:145
void printHelpMessage() const
Definition: appoptions.cpp:139
bpo::positional_options_description m_positional_options
positional options description, to be filled with addPositional() from main module
Definition: appoptions.h:68
const bpo::variable_value & operator[](const std::string &s) const
access to variable with given name defined in variables container
Definition: appoptions.cpp:65
QString getBornAgainVersionString()
Definition: GUIHelpers.cpp:130
QString const & name(EShape k)
Definition: particles.cpp:21