main.cpp
Go to the documentation of this file.
1 #include "mainwindow.h"
2 #include <iostream>
3 #include <QApplication>
4 #include <QSplashScreen>
5 #include <QThread>
6 #include <QCommandLineParser>
7 #include <QDesktopWidget>
8 #include <QFontDatabase>
9 #include <QSettings>
10 #include <QNetworkAccessManager>
11 #include <QNetworkReply>
12 #include <QJsonDocument>
13 #include <QDir>
14 
21 
22 #include "nlohmann_parsers.h"
23 #include "new_release_dialog.h"
24 
25 #ifdef COMPILED_WITH_CATKIN
26 
27 #endif
28 #ifdef COMPILED_WITH_AMENT
29 #include <ament_index_cpp/get_package_prefix.hpp>
30 #include <ament_index_cpp/get_package_share_directory.hpp>
31 #endif
32 
33 static QString VERSION_STRING = QString("%1.%2.%3").arg(PJ_MAJOR_VERSION).arg(PJ_MINOR_VERSION).arg(PJ_PATCH_VERSION);
34 
35 inline int GetVersionNumber(QString str)
36 {
37  QStringList online_version = str.split('.');
38  if( online_version.size() != 3 )
39  {
40  return 0;
41  }
42  int major = online_version[0].toInt();
43  int minor = online_version[1].toInt();
44  int patch = online_version[2].toInt();
45  return major * 10000 + minor * 100 + patch;
46 }
47 
48 void OpenNewReleaseDialog(QNetworkReply* reply)
49 {
50  if (reply->error())
51  {
52  return;
53  }
54  QString answer = reply->readAll();
55  QJsonDocument document = QJsonDocument::fromJson(answer.toUtf8());
56  QJsonObject data = document.object();
57  QString url = data["html_url"].toString();
58  QString name = data["name"].toString();
59  QString tag_name = data["tag_name"].toString();
60  QSettings settings;
61  int online_number = GetVersionNumber(tag_name);
62  QString dont_show = settings.value("NewRelease/dontShowThisVersion", VERSION_STRING).toString();
63  int dontshow_number = GetVersionNumber(dont_show);
64  int current_number = GetVersionNumber(VERSION_STRING);
65 
66  if (online_number > current_number && online_number > dontshow_number)
67  {
68  NewReleaseDialog* dialog = new NewReleaseDialog(nullptr, tag_name, name, url);
69  dialog->show();
70  }
71 }
72 
74 {
75  QSettings settings;
76  qsrand(time(nullptr));
77 
78  auto getNum = []() {
79  const int last_image_num = 60;
80  int n = qrand() % (last_image_num + 2);
81  if (n > last_image_num)
82  {
83  n = 0;
84  }
85  return n;
86  };
87  int n = getNum();
88  int prev_n = settings.value("previousFunnySubtitle").toInt();
89  while (n == prev_n)
90  {
91  n = getNum();
92  }
93  settings.setValue("previousFunnySubtitle", n);
94  auto filename = QString("://resources/memes/meme_%1.jpg").arg(n, 2, 10, QChar('0'));
95  return QPixmap(filename);
96 }
97 
98 int main(int argc, char* argv[])
99 {
100  QApplication app(argc, argv);
101 
102  QCoreApplication::setOrganizationName("PlotJuggler");
103  QCoreApplication::setApplicationName("PlotJuggler-3");
104  QSettings::setDefaultFormat(QSettings::IniFormat);
105 
106  QSettings settings;
107 
108  if( !settings.isWritable() )
109  {
110  qDebug() << "ERROR: the file [" << settings.fileName() <<
111  "] is not writable. This may happen when you run PlotJuggler with sudo. "
112  "Change the permissions of the file (\"sudo chmod 666 <file_name>\"on linux)";
113  }
114 
115  app.setApplicationVersion(VERSION_STRING);
116 
117  QString extra_path;
118 
119  try {
120 #ifdef COMPILED_WITH_CATKIN
121  //TODO: use pluginlib instead
122  QDir ros_plugins_dir( QCoreApplication::applicationDirPath() + "_ros" );
123  if( !ros_plugins_dir.exists() || ros_plugins_dir.isEmpty() )
124  {
125  throw std::runtime_error("Missing ros plugins directory");
126  }
127  extra_path = ros_plugins_dir.path();
128 #endif
129 #ifdef COMPILED_WITH_AMENT
130  extra_path = QString::fromStdString(ament_index_cpp::get_package_prefix("plotjuggler_ros"));
131  extra_path += "/lib/plotjuggler_ros";
132 #endif
133  } catch (...) {
134 
135  QMessageBox::warning(nullptr, "Missing package [plotjuggler-ros]",
136  "If you just upgraded from PlotJuggler 2.x to 3.x , try installing this package:\n\n"
137  "sudo apt install ros-${ROS_DISTRO}-plotjuggler-ros",
138  QMessageBox::Cancel, QMessageBox::Cancel);
139  }
140 
141  //---------------------------
142  TransformFactory::registerTransform<FirstDerivative>();
143  TransformFactory::registerTransform<ScaleTransform>();
144  TransformFactory::registerTransform<MovingAverageFilter>();
145  TransformFactory::registerTransform<OutlierRemovalFilter>();
146  TransformFactory::registerTransform<IntegralTransform>();
147  //---------------------------
148 
149  QCommandLineParser parser;
150  parser.setApplicationDescription("PlotJuggler: the time series visualization tool that you deserve ");
151  parser.addVersionOption();
152  parser.addHelpOption();
153 
154  QCommandLineOption nosplash_option(QStringList() << "n"
155  << "nosplash",
156  "Don't display the splashscreen");
157  parser.addOption(nosplash_option);
158 
159  QCommandLineOption test_option(QStringList() << "t"
160  << "test",
161  "Generate test curves at startup");
162  parser.addOption(test_option);
163 
164  QCommandLineOption loadfile_option(QStringList() << "d"
165  << "datafile",
166  "Load a file containing data", "file");
167  parser.addOption(loadfile_option);
168 
169  QCommandLineOption layout_option(QStringList() << "l"
170  << "layout",
171  "Load a file containing the layout configuration", "file");
172  parser.addOption(layout_option);
173 
174  QCommandLineOption publish_option(QStringList() << "p"
175  << "publish",
176  "Automatically start publisher when loading the layout file");
177  parser.addOption(publish_option);
178 
179  QCommandLineOption folder_option(QStringList() << "extra-plugin-folders",
180  "Add semicolon-separated list of folders where you should look for plugins.");
181  if(!extra_path.isEmpty())
182  {
183  folder_option.setDefaultValue( extra_path );
184  }
185  parser.addOption(folder_option);
186 
187  QCommandLineOption buffersize_option(QStringList() << "buffer_size",
188  QCoreApplication::translate("main", "Change the maximum size of the streaming "
189  "buffer (minimum: 10 default: 60)"),
190  QCoreApplication::translate("main", "seconds"));
191  parser.addOption(buffersize_option);
192 
193  parser.process(*qApp);
194 
195  if (parser.isSet(publish_option) && !parser.isSet(layout_option))
196  {
197  std::cerr << "Option [ -p / --publish ] is invalid unless [ -l / --layout ] is used too." << std::endl;
198  return -1;
199  }
200 
201  QIcon app_icon("://resources/plotjuggler.svg");
202  QApplication::setWindowIcon(app_icon);
203 
204  QNetworkAccessManager manager;
205  QObject::connect(&manager, &QNetworkAccessManager::finished, OpenNewReleaseDialog);
206 
207  QNetworkRequest request;
208  request.setUrl(QUrl("https://api.github.com/repos/facontidavide/PlotJuggler/releases/latest"));
209  manager.get(request);
210 
211  /*
212  * You, fearless code reviewer, decided to start a journey into my source code.
213  * For your bravery, you deserve to know the truth.
214  * The splashscreen is useless; not only it is useless, it will make your start-up
215  * time slower by few seconds for absolutely no reason.
216  * But what are two seconds compared with the time that PlotJuggler will save you?
217  * The splashscreen is the connection between me and my users, the glue that keeps
218  * together our invisible relationship.
219  * Now, it is up to you to decide: you can block the splashscreen forever or not,
220  * reject a message that brings a little of happiness into your day, spent analyzing data.
221  * Please don't do it.
222  */
223 
224  if (!parser.isSet(nosplash_option) && !(parser.isSet(loadfile_option) || parser.isSet(layout_option)))
225  // if(false) // if you uncomment this line, a kitten will die somewhere in the world.
226  {
227  QPixmap main_pixmap = getFunnySplashscreen();
228  QSplashScreen splash(main_pixmap, Qt::WindowStaysOnTopHint);
229  QDesktopWidget* desktop = QApplication::desktop();
230  const int scrn = desktop->screenNumber(QCursor::pos());
231  const QPoint currentDesktopsCenter = desktop->availableGeometry(scrn).center();
232  splash.move(currentDesktopsCenter - splash.rect().center());
233 
234  splash.show();
235  app.processEvents();
236 
237  auto deadline = QDateTime::currentDateTime().addMSecs(500);
238  while (QDateTime::currentDateTime() < deadline)
239  {
240  app.processEvents();
241  }
242 
243  MainWindow w(parser);
244 
245  deadline = QDateTime::currentDateTime().addMSecs(3000);
246  while (QDateTime::currentDateTime() < deadline && !splash.isHidden())
247  {
248  app.processEvents();
249  }
250 
251  w.show();
252  splash.finish(&w);
253  return app.exec();
254  }
255  MainWindow w(parser);
256  w.show();
257  return app.exec();
258 }
void OpenNewReleaseDialog(QNetworkReply *reply)
Definition: main.cpp:48
QPixmap getFunnySplashscreen()
Definition: main.cpp:73
int finished
static QString VERSION_STRING
Definition: main.cpp:33
const char * name
float time
Definition: mqtt_test.py:17
int GetVersionNumber(QString str)
Definition: main.cpp:35
dictionary data
Definition: mqtt_test.py:22
int main(int argc, char *argv[])
Definition: main.cpp:98


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:09