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