Update Spanish translation to latest changes
[pulseview.git] / pv / application.cpp
blob52cb8a01557291e3cdd41e5ffc8c831f1f37cf53
1 /*
2 * This file is part of the PulseView project.
4 * Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <iostream>
21 #include <typeinfo>
23 #include <QDebug>
24 #include <QDir>
25 #include <QLibraryInfo>
26 #include <QMessageBox>
27 #include <QWidget>
29 #include <boost/version.hpp>
31 #ifdef ENABLE_STACKTRACE
32 #include <boost/stacktrace.hpp>
33 #endif
35 #ifdef ENABLE_DECODE
36 #include <libsigrokdecode/libsigrokdecode.h>
37 #endif
39 #include <pv/exprtk.hpp>
41 #include "application.hpp"
42 #include "config.h"
43 #include "globalsettings.hpp"
45 using std::cout;
46 using std::endl;
47 using std::exception;
48 using std::shared_ptr;
50 #ifdef ENABLE_DECODE
51 static gint sort_pds(gconstpointer a, gconstpointer b)
53 const struct srd_decoder *sda, *sdb;
55 sda = (const struct srd_decoder *)a;
56 sdb = (const struct srd_decoder *)b;
57 return strcmp(sda->id, sdb->id);
59 #endif
61 Application::Application(int &argc, char* argv[]) :
62 QApplication(argc, argv)
64 setApplicationVersion(PV_VERSION_STRING);
65 setApplicationName("PulseView");
66 setOrganizationName("sigrok");
67 setOrganizationDomain("sigrok.org");
70 const QStringList Application::get_languages() const
72 const QStringList files = QDir(":/l10n/").entryList(QStringList("*.qm"), QDir::Files);
74 QStringList result;
75 result << "en"; // Add default language to the set
77 // Remove file extensions
78 for (const QString& file : files)
79 result << file.split(".").front();
81 result.sort(Qt::CaseInsensitive);
83 return result;
86 const QString Application::get_language_editors(const QString& language) const
88 if (language == "de") return "Sören Apel, Uwe Hermann";
89 if (language == "es_mx") return "Carlos Diaz";
91 return QString();
94 void Application::switch_language(const QString& language)
96 removeTranslator(&app_translator_);
97 removeTranslator(&qt_translator_);
98 removeTranslator(&qtbase_translator_);
100 if ((language != "C") && (language != "en")) {
101 // Application translations
102 QString resource = ":/l10n/" + language +".qm";
103 if (app_translator_.load(resource))
104 installTranslator(&app_translator_);
105 else
106 qWarning() << "Translation resource" << resource << "not found";
108 // Qt translations
109 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
110 QString tr_path(QLibraryInfo::path(QLibraryInfo::TranslationsPath));
111 #else
112 QString tr_path(QLibraryInfo::location(QLibraryInfo::TranslationsPath));
113 #endif
115 if (qt_translator_.load("qt_" + language, tr_path))
116 installTranslator(&qt_translator_);
117 else
118 qWarning() << "QT translations for" << language << "not found at" <<
119 tr_path << ", Qt translations package is probably missing";
121 // Qt base translations
122 if (qtbase_translator_.load("qtbase_" + language, tr_path))
123 installTranslator(&qtbase_translator_);
124 else
125 qWarning() << "QT base translations for" << language << "not found at" <<
126 tr_path << ", Qt translations package is probably missing";
129 if (!topLevelWidgets().empty()) {
130 // Force all windows to update
131 for (QWidget *widget : topLevelWidgets())
132 widget->update();
134 QMessageBox msg(topLevelWidgets().front());
135 msg.setText(tr("Some parts of the application may still " \
136 "use the previous language. Re-opening the affected windows or " \
137 "restarting the application will remedy this."));
138 msg.setStandardButtons(QMessageBox::Ok);
139 msg.setIcon(QMessageBox::Information);
140 msg.exec();
144 void Application::on_setting_changed(const QString &key, const QVariant &value)
146 if (key == pv::GlobalSettings::Key_General_Language)
147 switch_language(value.toString());
150 void Application::collect_version_info(pv::DeviceManager &device_manager)
152 // Library versions and features
153 version_info_.emplace_back(applicationName(), applicationVersion());
154 version_info_.emplace_back("Qt", qVersion());
155 version_info_.emplace_back("glibmm", PV_GLIBMM_VERSION);
156 version_info_.emplace_back("Boost", BOOST_LIB_VERSION);
157 version_info_.emplace_back("exprtk", QString::fromUtf8(exprtk::information::date));
159 version_info_.emplace_back("libsigrok", QString("%1/%2 (rt: %3/%4)")
160 .arg(SR_PACKAGE_VERSION_STRING, SR_LIB_VERSION_STRING,
161 sr_package_version_string_get(), sr_lib_version_string_get()));
163 GSList *l_orig = sr_buildinfo_libs_get();
164 for (GSList *l = l_orig; l; l = l->next) {
165 GSList *m = (GSList *)l->data;
166 const char *lib = (const char *)m->data;
167 const char *version = (const char *)m->next->data;
168 version_info_.emplace_back(QString(" - %1").arg(QString(lib)), QString(version));
169 g_slist_free_full(m, g_free);
171 g_slist_free(l_orig);
173 char *host = sr_buildinfo_host_get();
174 version_info_.emplace_back(" - Host", QString(host));
175 g_free(host);
177 char *scpi_backends = sr_buildinfo_scpi_backends_get();
178 version_info_.emplace_back(" - SCPI backends", QString(scpi_backends));
179 g_free(scpi_backends);
181 #ifdef ENABLE_DECODE
182 struct srd_decoder *dec;
184 version_info_.emplace_back("libsigrokdecode", QString("%1/%2 (rt: %3/%4)")
185 .arg(SRD_PACKAGE_VERSION_STRING, SRD_LIB_VERSION_STRING,
186 srd_package_version_string_get(), srd_lib_version_string_get()));
188 l_orig = srd_buildinfo_libs_get();
189 for (GSList *l = l_orig; l; l = l->next) {
190 GSList *m = (GSList *)l->data;
191 const char *lib = (const char *)m->data;
192 const char *version = (const char *)m->next->data;
193 version_info_.emplace_back(QString(" - %1").arg(QString(lib)), QString(version));
194 g_slist_free_full(m, g_free);
196 g_slist_free(l_orig);
198 host = srd_buildinfo_host_get();
199 version_info_.emplace_back(" - Host", QString(host));
200 g_free(host);
201 #endif
203 // Firmware paths
204 l_orig = sr_resourcepaths_get(SR_RESOURCE_FIRMWARE);
205 for (GSList *l = l_orig; l; l = l->next)
206 fw_path_list_.emplace_back((char*)l->data);
207 g_slist_free_full(l_orig, g_free);
209 // PD paths
210 #ifdef ENABLE_DECODE
211 l_orig = srd_searchpaths_get();
212 for (GSList *l = l_orig; l; l = l->next)
213 pd_path_list_.emplace_back((char*)l->data);
214 g_slist_free_full(l_orig, g_free);
215 #endif
217 // Device drivers
218 for (auto& entry : device_manager.context()->drivers())
219 if (device_manager.driver_supported(entry.second))
220 driver_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
221 QString::fromUtf8(entry.second->long_name().c_str()));
223 // Input formats
224 for (auto& entry : device_manager.context()->input_formats())
225 input_format_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
226 QString::fromUtf8(entry.second->description().c_str()));
228 // Output formats
229 for (auto& entry : device_manager.context()->output_formats())
230 output_format_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
231 QString::fromUtf8(entry.second->description().c_str()));
233 // Protocol decoders
234 #ifdef ENABLE_DECODE
235 GSList *sl = g_slist_copy((GSList *)srd_decoder_list());
236 sl = g_slist_sort(sl, sort_pds);
237 for (const GSList *l = sl; l; l = l->next) {
238 dec = (struct srd_decoder *)l->data;
239 pd_list_.emplace_back(QString::fromUtf8(dec->id),
240 QString::fromUtf8(dec->longname));
242 g_slist_free(sl);
243 #endif
246 void Application::print_version_info()
248 cout << PV_TITLE << " " << PV_VERSION_STRING << endl;
250 cout << endl << "Libraries and features:" << endl;
251 for (pair<QString, QString>& entry : version_info_)
252 cout << " " << entry.first.toStdString() << " " << entry.second.toStdString() << endl;
254 cout << endl << "Firmware search paths:" << endl;
255 for (QString& entry : fw_path_list_)
256 cout << " " << entry.toStdString() << endl;
258 cout << endl << "Protocol decoder search paths:" << endl;
259 for (QString& entry : pd_path_list_)
260 cout << " " << entry.toStdString() << endl;
262 cout << endl << "Supported hardware drivers:" << endl;
263 for (pair<QString, QString>& entry : driver_list_)
264 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
265 entry.second.toStdString() << endl;
267 cout << endl << "Supported input formats:" << endl;
268 for (pair<QString, QString>& entry : input_format_list_)
269 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
270 entry.second.toStdString() << endl;
272 cout << endl << "Supported output formats:" << endl;
273 for (pair<QString, QString>& entry : output_format_list_)
274 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
275 entry.second.toStdString() << endl;
277 #ifdef ENABLE_DECODE
278 cout << endl << "Supported protocol decoders:" << endl;
279 for (pair<QString, QString>& entry : pd_list_)
280 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
281 entry.second.toStdString() << endl;
282 #endif
285 vector< pair<QString, QString> > Application::get_version_info() const
287 return version_info_;
290 vector<QString> Application::get_fw_path_list() const
292 return fw_path_list_;
295 vector<QString> Application::get_pd_path_list() const
297 return pd_path_list_;
300 vector< pair<QString, QString> > Application::get_driver_list() const
302 return driver_list_;
305 vector< pair<QString, QString> > Application::get_input_format_list() const
307 return input_format_list_;
310 vector< pair<QString, QString> > Application::get_output_format_list() const
312 return output_format_list_;
315 vector< pair<QString, QString> > Application::get_pd_list() const
317 return pd_list_;
320 bool Application::notify(QObject *receiver, QEvent *event)
322 try {
323 return QApplication::notify(receiver, event);
324 } catch (exception& e) {
325 qDebug().nospace() << "Caught exception of type " << \
326 typeid(e).name() << " (" << e.what() << ")";
327 #ifdef ENABLE_STACKTRACE
328 throw e;
329 #else
330 exit(1);
331 #endif
332 return false;