]> sigrok.org Git - pulseview.git/blame - pv/application.cpp
Fix #1549 by notifying user of wrong input parameter names
[pulseview.git] / pv / application.cpp
CommitLineData
dac1bb97
ML
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
dac1bb97
ML
18 */
19
49719858 20#include <iostream>
c1a688de
SA
21#include <typeinfo>
22
23#include <QDebug>
380f4ee6 24#include <QDir>
4353c902 25#include <QLibraryInfo>
380f4ee6
SA
26#include <QMessageBox>
27#include <QWidget>
c1a688de 28
49719858
SA
29#include <boost/version.hpp>
30
c1a688de
SA
31#ifdef ENABLE_STACKTRACE
32#include <boost/stacktrace.hpp>
33#endif
dac1bb97 34
49719858
SA
35#ifdef ENABLE_DECODE
36#include <libsigrokdecode/libsigrokdecode.h>
37#endif
38
0466001b
SA
39#include "application.hpp"
40#include "config.h"
41#include "globalsettings.hpp"
42
49719858
SA
43using std::cout;
44using std::endl;
6f925ba9 45using std::exception;
49719858
SA
46using std::shared_ptr;
47
48#ifdef ENABLE_DECODE
49static gint sort_pds(gconstpointer a, gconstpointer b)
50{
51 const struct srd_decoder *sda, *sdb;
52
53 sda = (const struct srd_decoder *)a;
54 sdb = (const struct srd_decoder *)b;
55 return strcmp(sda->id, sdb->id);
56}
57#endif
6f925ba9 58
dac1bb97
ML
59Application::Application(int &argc, char* argv[]) :
60 QApplication(argc, argv)
61{
62 setApplicationVersion(PV_VERSION_STRING);
63 setApplicationName("PulseView");
39eb0d45 64 setOrganizationName("sigrok");
dac1bb97
ML
65 setOrganizationDomain("sigrok.org");
66}
67
380f4ee6
SA
68QStringList Application::get_languages()
69{
70 QStringList files = QDir(":/l10n/").entryList(QStringList("*.qm"), QDir::Files);
71
72 QStringList result;
73 result << "en"; // Add default language to the set
74
75 // Remove file extensions
49fee853 76 for (const QString& file : files)
380f4ee6
SA
77 result << file.split(".").front();
78
79 result.sort(Qt::CaseInsensitive);
80
81 return result;
82}
83
0466001b
SA
84void Application::switch_language(const QString& language)
85{
86 removeTranslator(&app_translator_);
87 removeTranslator(&qt_translator_);
4353c902 88 removeTranslator(&qtbase_translator_);
0466001b
SA
89
90 if ((language != "C") && (language != "en")) {
91 // Application translations
92 QString resource = ":/l10n/" + language +".qm";
93 if (app_translator_.load(resource))
94 installTranslator(&app_translator_);
95 else
96 qWarning() << "Translation resource" << resource << "not found";
97
98 // Qt translations
4353c902
SA
99 QString tr_path(QLibraryInfo::location(QLibraryInfo::TranslationsPath));
100
101 if (qt_translator_.load("qt_" + language, tr_path))
0466001b
SA
102 installTranslator(&qt_translator_);
103 else
4353c902
SA
104 qWarning() << "QT translations for" << language << "not found at" <<
105 tr_path << ", Qt translations package is probably missing";
106
107 // Qt base translations
108 if (qtbase_translator_.load("qtbase_" + language, tr_path))
109 installTranslator(&qtbase_translator_);
110 else
111 qWarning() << "QT base translations for" << language << "not found at" <<
112 tr_path << ", Qt translations package is probably missing";
0466001b 113 }
380f4ee6
SA
114
115 if (!topLevelWidgets().empty()) {
116 // Force all windows to update
117 for (QWidget *widget : topLevelWidgets())
118 widget->update();
119
120 QMessageBox msg(topLevelWidgets().front());
121 msg.setText(tr("Some parts of the application may still " \
122 "use the previous language. Re-opening the affected windows or " \
123 "restarting the application will remedy this."));
124 msg.setStandardButtons(QMessageBox::Ok);
125 msg.setIcon(QMessageBox::Information);
126 msg.exec();
127 }
0466001b
SA
128}
129
130void Application::on_setting_changed(const QString &key, const QVariant &value)
131{
132 if (key == pv::GlobalSettings::Key_General_Language)
133 switch_language(value.toString());
134}
135
49719858
SA
136void Application::collect_version_info(shared_ptr<sigrok::Context> context)
137{
138 // Library versions and features
139 version_info_.emplace_back(applicationName(), applicationVersion());
140 version_info_.emplace_back("Qt", qVersion());
141 version_info_.emplace_back("glibmm", PV_GLIBMM_VERSION);
142 version_info_.emplace_back("Boost", BOOST_LIB_VERSION);
143
144 version_info_.emplace_back("libsigrok", QString("%1/%2 (rt: %3/%4)")
145 .arg(SR_PACKAGE_VERSION_STRING, SR_LIB_VERSION_STRING,
146 sr_package_version_string_get(), sr_lib_version_string_get()));
147
148 GSList *l_orig = sr_buildinfo_libs_get();
149 for (GSList *l = l_orig; l; l = l->next) {
150 GSList *m = (GSList *)l->data;
151 const char *lib = (const char *)m->data;
152 const char *version = (const char *)m->next->data;
153 version_info_.emplace_back(QString(" - %1").arg(QString(lib)), QString(version));
154 g_slist_free_full(m, g_free);
155 }
156 g_slist_free(l_orig);
157
158 char *host = sr_buildinfo_host_get();
159 version_info_.emplace_back(" - Host", QString(host));
160 g_free(host);
161
162 char *scpi_backends = sr_buildinfo_scpi_backends_get();
163 version_info_.emplace_back(" - SCPI backends", QString(scpi_backends));
164 g_free(scpi_backends);
165
166#ifdef ENABLE_DECODE
167 struct srd_decoder *dec;
168
169 version_info_.emplace_back("libsigrokdecode", QString("%1/%2 (rt: %3/%4)")
170 .arg(SRD_PACKAGE_VERSION_STRING, SRD_LIB_VERSION_STRING,
171 srd_package_version_string_get(), srd_lib_version_string_get()));
172
173 l_orig = srd_buildinfo_libs_get();
174 for (GSList *l = l_orig; l; l = l->next) {
175 GSList *m = (GSList *)l->data;
176 const char *lib = (const char *)m->data;
177 const char *version = (const char *)m->next->data;
178 version_info_.emplace_back(QString(" - %1").arg(QString(lib)), QString(version));
179 g_slist_free_full(m, g_free);
180 }
181 g_slist_free(l_orig);
182
183 host = srd_buildinfo_host_get();
184 version_info_.emplace_back(" - Host", QString(host));
185 g_free(host);
186#endif
187
188 // Firmware paths
189 l_orig = sr_resourcepaths_get(SR_RESOURCE_FIRMWARE);
190 for (GSList *l = l_orig; l; l = l->next)
d2d0056d 191 fw_path_list_.emplace_back((char*)l->data);
49719858
SA
192 g_slist_free_full(l_orig, g_free);
193
194 // PD paths
195#ifdef ENABLE_DECODE
196 l_orig = srd_searchpaths_get();
197 for (GSList *l = l_orig; l; l = l->next)
d2d0056d 198 pd_path_list_.emplace_back((char*)l->data);
49719858
SA
199 g_slist_free_full(l_orig, g_free);
200#endif
201
202 // Device drivers
f4ab4b5c 203 for (auto& entry : context->drivers())
49719858
SA
204 driver_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
205 QString::fromUtf8(entry.second->long_name().c_str()));
206
207 // Input formats
f4ab4b5c 208 for (auto& entry : context->input_formats())
49719858
SA
209 input_format_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
210 QString::fromUtf8(entry.second->description().c_str()));
211
212 // Output formats
f4ab4b5c 213 for (auto& entry : context->output_formats())
49719858
SA
214 output_format_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
215 QString::fromUtf8(entry.second->description().c_str()));
216
217 // Protocol decoders
218#ifdef ENABLE_DECODE
219 GSList *sl = g_slist_copy((GSList *)srd_decoder_list());
220 sl = g_slist_sort(sl, sort_pds);
221 for (const GSList *l = sl; l; l = l->next) {
222 dec = (struct srd_decoder *)l->data;
223 pd_list_.emplace_back(QString::fromUtf8(dec->id),
224 QString::fromUtf8(dec->longname));
225 }
226 g_slist_free(sl);
227#endif
228}
229
230void Application::print_version_info()
231{
2e2ddbe9 232 cout << PV_TITLE << " " << PV_VERSION_STRING << endl;
49719858
SA
233
234 cout << endl << "Libraries and features:" << endl;
f4ab4b5c 235 for (pair<QString, QString>& entry : version_info_)
49719858
SA
236 cout << " " << entry.first.toStdString() << " " << entry.second.toStdString() << endl;
237
238 cout << endl << "Firmware search paths:" << endl;
f4ab4b5c 239 for (QString& entry : fw_path_list_)
49719858
SA
240 cout << " " << entry.toStdString() << endl;
241
242 cout << endl << "Protocol decoder search paths:" << endl;
f4ab4b5c 243 for (QString& entry : pd_path_list_)
49719858
SA
244 cout << " " << entry.toStdString() << endl;
245
246 cout << endl << "Supported hardware drivers:" << endl;
f4ab4b5c 247 for (pair<QString, QString>& entry : driver_list_)
49719858
SA
248 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
249 entry.second.toStdString() << endl;
250
251 cout << endl << "Supported input formats:" << endl;
f4ab4b5c 252 for (pair<QString, QString>& entry : input_format_list_)
49719858
SA
253 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
254 entry.second.toStdString() << endl;
255
256 cout << endl << "Supported output formats:" << endl;
f4ab4b5c 257 for (pair<QString, QString>& entry : output_format_list_)
49719858
SA
258 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
259 entry.second.toStdString() << endl;
260
261#ifdef ENABLE_DECODE
262 cout << endl << "Supported protocol decoders:" << endl;
f4ab4b5c 263 for (pair<QString, QString>& entry : pd_list_)
49719858
SA
264 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
265 entry.second.toStdString() << endl;
266#endif
49719858
SA
267}
268
269vector< pair<QString, QString> > Application::get_version_info() const
270{
271 return version_info_;
272}
273
274vector<QString> Application::get_fw_path_list() const
275{
276 return fw_path_list_;
277}
278
279vector<QString> Application::get_pd_path_list() const
280{
281 return pd_path_list_;
282}
283
284vector< pair<QString, QString> > Application::get_driver_list() const
285{
286 return driver_list_;
287}
288
289vector< pair<QString, QString> > Application::get_input_format_list() const
290{
291 return input_format_list_;
292}
293
294vector< pair<QString, QString> > Application::get_output_format_list() const
295{
296 return output_format_list_;
297}
298
299vector< pair<QString, QString> > Application::get_pd_list() const
300{
301 return pd_list_;
302}
303
dac1bb97
ML
304bool Application::notify(QObject *receiver, QEvent *event)
305{
306 try {
307 return QApplication::notify(receiver, event);
6f925ba9 308 } catch (exception& e) {
c1a688de
SA
309 qDebug().nospace() << "Caught exception of type " << \
310 typeid(e).name() << " (" << e.what() << ")";
311#ifdef ENABLE_STACKTRACE
312 throw e;
313#else
dac1bb97 314 exit(1);
c1a688de 315#endif
dac1bb97
ML
316 return false;
317 }
318}