2013-05-03 18:04:51 +00:00
|
|
|
/* qt-gui.cpp */
|
|
|
|
/* Qt UI implementation */
|
2012-10-11 00:42:59 +00:00
|
|
|
#include <libintl.h>
|
|
|
|
#include <glib/gi18n.h>
|
2011-09-20 19:40:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2012-01-03 05:53:33 +00:00
|
|
|
#include <unistd.h>
|
2012-09-09 16:06:44 +00:00
|
|
|
#include <sys/stat.h>
|
2013-01-05 20:56:45 +00:00
|
|
|
#include <sys/time.h>
|
2013-01-05 20:16:02 +00:00
|
|
|
#include <ctype.h>
|
2011-09-20 19:40:34 +00:00
|
|
|
|
|
|
|
#include "dive.h"
|
|
|
|
#include "divelist.h"
|
|
|
|
#include "display.h"
|
2012-09-25 14:28:47 +00:00
|
|
|
#include "uemis.h"
|
2013-01-09 20:07:09 +00:00
|
|
|
#include "device.h"
|
2013-01-18 01:05:48 +00:00
|
|
|
#include "webservice.h"
|
2013-02-26 17:24:02 +00:00
|
|
|
#include "version.h"
|
2011-09-20 19:40:34 +00:00
|
|
|
#include "libdivecomputer.h"
|
2013-04-07 22:20:43 +00:00
|
|
|
#include "qt-ui/mainwindow.h"
|
2013-05-23 06:24:33 +00:00
|
|
|
#include "helpers.h"
|
2011-09-20 19:40:34 +00:00
|
|
|
|
2013-05-03 18:04:51 +00:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QTextCodec>
|
|
|
|
#include <QTranslator>
|
2013-05-10 04:37:21 +00:00
|
|
|
#include <QSettings>
|
2013-05-10 16:24:06 +00:00
|
|
|
#include <QDesktopWidget>
|
2013-05-31 07:43:38 +00:00
|
|
|
#include <QStyle>
|
|
|
|
#include <QDebug>
|
2013-04-07 22:20:43 +00:00
|
|
|
|
2013-05-23 06:24:33 +00:00
|
|
|
const char *default_dive_computer_vendor;
|
|
|
|
const char *default_dive_computer_product;
|
|
|
|
const char *default_dive_computer_device;
|
|
|
|
|
2013-05-03 18:04:51 +00:00
|
|
|
class Translator: public QTranslator
|
2013-04-02 16:49:17 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2013-05-03 18:04:51 +00:00
|
|
|
Translator(QObject *parent = 0);
|
|
|
|
~Translator() {}
|
2013-04-02 16:49:17 +00:00
|
|
|
|
2013-05-03 18:04:51 +00:00
|
|
|
virtual QString translate(const char *context, const char *sourceText,
|
2013-05-22 03:31:56 +00:00
|
|
|
const char *disambiguation = NULL) const;
|
2013-04-02 16:49:17 +00:00
|
|
|
};
|
|
|
|
|
2013-05-03 18:04:51 +00:00
|
|
|
Translator::Translator(QObject *parent):
|
|
|
|
QTranslator(parent)
|
2013-04-02 16:49:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-03 18:04:51 +00:00
|
|
|
QString Translator::translate(const char *context, const char *sourceText,
|
2013-05-22 03:31:56 +00:00
|
|
|
const char *disambiguation) const
|
2013-04-02 16:49:17 +00:00
|
|
|
{
|
2013-05-03 18:04:51 +00:00
|
|
|
return gettext(sourceText);
|
2013-04-02 16:49:17 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 18:04:51 +00:00
|
|
|
static QApplication *application = NULL;
|
2013-04-02 16:49:17 +00:00
|
|
|
|
2013-05-03 18:04:51 +00:00
|
|
|
int error_count;
|
|
|
|
const char *existing_filename;
|
2013-04-02 16:49:17 +00:00
|
|
|
|
2013-05-22 06:13:45 +00:00
|
|
|
void init_qt_ui(int *argcp, char ***argvp, char *errormessage)
|
2013-04-18 07:59:31 +00:00
|
|
|
{
|
|
|
|
application->installTranslator(new Translator(application));
|
|
|
|
MainWindow *window = new MainWindow();
|
2013-05-22 06:13:45 +00:00
|
|
|
window->showError(errormessage);
|
2013-04-18 07:59:31 +00:00
|
|
|
window->show();
|
|
|
|
}
|
|
|
|
|
2013-05-24 01:36:18 +00:00
|
|
|
const char *getSetting(QSettings &s, QString name)
|
2013-05-23 06:24:33 +00:00
|
|
|
{
|
|
|
|
QVariant v;
|
|
|
|
v = s.value(name);
|
|
|
|
if (v.isValid()) {
|
2013-05-28 18:21:27 +00:00
|
|
|
return strdup(v.toString().toUtf8().data());
|
2013-05-23 06:24:33 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-02 00:09:15 +00:00
|
|
|
void init_ui(int *argcp, char ***argvp)
|
2011-09-20 19:40:34 +00:00
|
|
|
{
|
2013-05-10 04:37:21 +00:00
|
|
|
QVariant v;
|
2013-05-31 07:43:38 +00:00
|
|
|
|
2013-04-22 13:45:57 +00:00
|
|
|
application = new QApplication(*argcp, *argvp);
|
2013-04-25 22:28:31 +00:00
|
|
|
|
2013-05-31 07:43:38 +00:00
|
|
|
// the Gtk theme makes things unbearably ugly
|
|
|
|
// so switch to Oxygen in this case
|
|
|
|
if (application->style()->objectName() == "gtk+")
|
|
|
|
application->setStyle("Oxygen");
|
|
|
|
|
2013-04-25 22:28:31 +00:00
|
|
|
#if QT_VERSION < 0x050000
|
|
|
|
// ask QString in Qt 4 to interpret all char* as UTF-8,
|
|
|
|
// like Qt 5 does.
|
|
|
|
// 106 is "UTF-8", this is faster than lookup by name
|
|
|
|
// [http://www.iana.org/assignments/character-sets/character-sets.xml]
|
|
|
|
QTextCodec::setCodecForCStrings(QTextCodec::codecForMib(106));
|
|
|
|
#endif
|
2013-05-22 03:31:56 +00:00
|
|
|
QCoreApplication::setOrganizationName("Subsurface");
|
|
|
|
QCoreApplication::setOrganizationDomain("subsurface.hohndel.org");
|
|
|
|
QCoreApplication::setApplicationName("Subsurface");
|
2013-04-25 22:28:31 +00:00
|
|
|
|
2013-05-23 06:24:33 +00:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup("GeneralSettings");
|
|
|
|
prefs.default_filename = getSetting(s, "default_filename");
|
|
|
|
s.endGroup();
|
|
|
|
s.beginGroup("DiveComputer");
|
|
|
|
default_dive_computer_vendor = getSetting(s, "dive_computer_vendor");
|
|
|
|
default_dive_computer_product = getSetting(s,"dive_computer_product");
|
|
|
|
default_dive_computer_device = getSetting(s, "dive_computer_device");
|
|
|
|
s.endGroup();
|
2011-09-20 19:40:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_ui(void)
|
|
|
|
{
|
2013-04-01 10:57:51 +00:00
|
|
|
application->exec();
|
2011-09-20 19:40:34 +00:00
|
|
|
}
|
|
|
|
|
2012-05-02 17:03:48 +00:00
|
|
|
void exit_ui(void)
|
|
|
|
{
|
2013-04-01 10:57:51 +00:00
|
|
|
delete application;
|
2012-09-13 17:46:09 +00:00
|
|
|
if (existing_filename)
|
2012-09-16 03:51:06 +00:00
|
|
|
free((void *)existing_filename);
|
2013-05-23 06:24:33 +00:00
|
|
|
if (default_dive_computer_device)
|
|
|
|
free((void *)default_dive_computer_device);
|
2012-05-02 17:03:48 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 19:27:00 +00:00
|
|
|
void set_filename(const char *filename, gboolean force)
|
2011-09-21 04:50:26 +00:00
|
|
|
{
|
2012-09-10 19:27:00 +00:00
|
|
|
if (!force && existing_filename)
|
|
|
|
return;
|
2012-09-16 03:51:06 +00:00
|
|
|
free((void *)existing_filename);
|
2012-07-31 17:55:41 +00:00
|
|
|
if (filename)
|
2011-09-21 04:50:26 +00:00
|
|
|
existing_filename = strdup(filename);
|
2012-11-10 14:32:06 +00:00
|
|
|
else
|
|
|
|
existing_filename = NULL;
|
2011-09-21 04:50:26 +00:00
|
|
|
}
|
2012-12-13 06:26:29 +00:00
|
|
|
|
2012-12-26 21:47:54 +00:00
|
|
|
const char *get_dc_nickname(const char *model, uint32_t deviceid)
|
2012-12-22 05:00:06 +00:00
|
|
|
{
|
2013-01-09 19:38:35 +00:00
|
|
|
struct device_info *known = get_device_info(model, deviceid);
|
2012-12-26 21:47:54 +00:00
|
|
|
if (known) {
|
|
|
|
if (known->nickname && *known->nickname)
|
|
|
|
return known->nickname;
|
|
|
|
else
|
|
|
|
return known->model;
|
2012-12-22 05:00:06 +00:00
|
|
|
}
|
2012-12-23 05:37:13 +00:00
|
|
|
return NULL;
|
2012-12-22 05:00:06 +00:00
|
|
|
}
|
|
|
|
|
2012-12-13 06:26:29 +00:00
|
|
|
void set_dc_nickname(struct dive *dive)
|
|
|
|
{
|
2013-05-03 18:04:51 +00:00
|
|
|
/* needs Qt implementation */
|
2013-06-07 17:59:06 +00:00
|
|
|
/*well, I don't know how to do this here... = ( */
|
2012-12-13 06:26:29 +00:00
|
|
|
}
|
2013-02-05 23:10:40 +00:00
|
|
|
|
2013-05-07 03:36:37 +00:00
|
|
|
QString get_depth_string(depth_t depth, bool showunit)
|
|
|
|
{
|
|
|
|
if (prefs.units.length == units::METERS) {
|
|
|
|
double meters = depth.mm / 1000.0;
|
|
|
|
return QString("%1%2").arg(meters, 0, 'f', meters >= 20.0 ? 0 : 1 ).arg(showunit ? _("m") : "");
|
|
|
|
} else {
|
|
|
|
double feet = mm_to_feet(depth.mm);
|
|
|
|
return QString("%1%2").arg(feet, 0, 'f', 1). arg(showunit ? _("ft") : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString get_weight_string(weight_t weight, bool showunit)
|
|
|
|
{
|
|
|
|
if (prefs.units.weight == units::KG) {
|
|
|
|
double kg = weight.grams / 1000.0;
|
|
|
|
return QString("%1%2").arg(kg, 0, 'f', kg >= 20.0 ? 0 : 1 ).arg(showunit ? _("kg") : "");
|
|
|
|
} else {
|
|
|
|
double lbs = grams_to_lbs(weight.grams);
|
|
|
|
return QString("%1%2").arg(lbs, 0, 'f', lbs >= 40.0 ? 0 : 1 ).arg(showunit ? _("lbs") : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString get_temperature_string(temperature_t temp, bool showunit)
|
|
|
|
{
|
|
|
|
if (prefs.units.temperature == units::CELSIUS) {
|
|
|
|
double celsius = mkelvin_to_C(temp.mkelvin);
|
2013-05-10 22:56:05 +00:00
|
|
|
return QString("%1%2%3").arg(celsius, 0, 'f', 1).arg(showunit ? (UTF8_DEGREE): "")
|
|
|
|
.arg(showunit ? _("C") : "");
|
2013-05-07 03:36:37 +00:00
|
|
|
} else {
|
|
|
|
double fahrenheit = mkelvin_to_F(temp.mkelvin);
|
2013-05-10 22:56:05 +00:00
|
|
|
return QString("%1%2%3").arg(fahrenheit, 0, 'f', 1).arg(showunit ? (UTF8_DEGREE): "")
|
|
|
|
.arg(showunit ? _("F") : "");
|
2013-05-07 03:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString get_volume_string(volume_t volume, bool showunit)
|
|
|
|
{
|
|
|
|
if (prefs.units.volume == units::LITER) {
|
|
|
|
double liter = volume.mliter / 1000.0;
|
|
|
|
return QString("%1%2").arg(liter, 0, 'f', liter >= 40.0 ? 0 : 1 ).arg(showunit ? _("l") : "");
|
|
|
|
} else {
|
|
|
|
double cuft = ml_to_cuft(volume.mliter);
|
|
|
|
return QString("%1%2").arg(cuft, 0, 'f', cuft >= 20.0 ? 0 : (cuft >= 2.0 ? 1 : 2)).arg(showunit ? _("cuft") : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString get_pressure_string(pressure_t pressure, bool showunit)
|
|
|
|
{
|
|
|
|
if (prefs.units.pressure == units::BAR) {
|
|
|
|
double bar = pressure.mbar / 1000.0;
|
|
|
|
return QString("%1%2").arg(bar, 0, 'f', 1).arg(showunit ? _("bar") : "");
|
|
|
|
} else {
|
|
|
|
double psi = mbar_to_PSI(pressure.mbar);
|
|
|
|
return QString("%1%2").arg(psi, 0, 'f', 0).arg(showunit ? _("psi") : "");
|
|
|
|
}
|
|
|
|
}
|
2013-04-01 10:57:51 +00:00
|
|
|
|
2013-05-10 16:24:06 +00:00
|
|
|
double get_screen_dpi()
|
|
|
|
{
|
|
|
|
QDesktopWidget *mydesk = application->desktop();
|
|
|
|
return mydesk->physicalDpiX();
|
|
|
|
}
|
2013-05-23 06:24:33 +00:00
|
|
|
|
|
|
|
int is_default_dive_computer(const char *vendor, const char *product)
|
|
|
|
{
|
|
|
|
return default_dive_computer_vendor && !strcmp(vendor, default_dive_computer_vendor) &&
|
|
|
|
default_dive_computer_product && !strcmp(product, default_dive_computer_product);
|
|
|
|
}
|
|
|
|
|
|
|
|
int is_default_dive_computer_device(const char *name)
|
|
|
|
{
|
|
|
|
return default_dive_computer_device && !strcmp(name, default_dive_computer_device);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_default_dive_computer(const char *vendor, const char *product)
|
|
|
|
{
|
|
|
|
QSettings s;
|
|
|
|
|
|
|
|
if (!vendor || !*vendor)
|
|
|
|
return;
|
|
|
|
if (!product || !*product)
|
|
|
|
return;
|
|
|
|
if (is_default_dive_computer(vendor, product))
|
|
|
|
return;
|
|
|
|
if (default_dive_computer_vendor)
|
|
|
|
free((void *)default_dive_computer_vendor);
|
|
|
|
if (default_dive_computer_product)
|
|
|
|
free((void *)default_dive_computer_product);
|
|
|
|
default_dive_computer_vendor = strdup(vendor);
|
|
|
|
default_dive_computer_product = strdup(product);
|
|
|
|
s.beginGroup("DiveComputer");
|
|
|
|
s.setValue("dive_computer_vendor", vendor);
|
|
|
|
s.setValue("dive_computer_product", product);
|
|
|
|
s.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_default_dive_computer_device(const char *name)
|
|
|
|
{
|
|
|
|
QSettings s;
|
|
|
|
|
|
|
|
if (!name || !*name)
|
|
|
|
return;
|
|
|
|
if (is_default_dive_computer_device(name))
|
|
|
|
return;
|
|
|
|
if (default_dive_computer_device)
|
|
|
|
free((void *)default_dive_computer_device);
|
|
|
|
default_dive_computer_device = strdup(name);
|
|
|
|
s.beginGroup("DiveComputer");
|
|
|
|
s.setValue("dive_computer_device", name);
|
|
|
|
s.endGroup();
|
|
|
|
}
|
|
|
|
|
2013-05-30 21:26:08 +00:00
|
|
|
QString getSubsurfaceDataPath(QString folderToFind)
|
|
|
|
{
|
|
|
|
QString execdir;
|
|
|
|
QDir folder;
|
|
|
|
|
|
|
|
// first check if we are running in the build dir, so this
|
|
|
|
// is just subdirectory of the current directory
|
|
|
|
execdir = QCoreApplication::applicationDirPath();
|
|
|
|
folder = QDir(execdir.append(QDir::separator()).append(folderToFind));
|
|
|
|
if (folder.exists())
|
|
|
|
return folder.absolutePath();
|
|
|
|
|
|
|
|
// next check for the Linux typical $(prefix)/share/subsurface
|
|
|
|
execdir = QCoreApplication::applicationDirPath();
|
|
|
|
folder = QDir(execdir.replace("bin", "share/subsurface/").append(folderToFind));
|
|
|
|
if (folder.exists())
|
|
|
|
return folder.absolutePath();
|
|
|
|
|
|
|
|
// then look for the usual location on a Mac
|
|
|
|
execdir = QCoreApplication::applicationDirPath();
|
|
|
|
folder = QDir(execdir.append("/../Resources/share/").append(folderToFind));
|
|
|
|
if (folder.exists())
|
|
|
|
return folder.absolutePath();
|
|
|
|
return QString("");
|
|
|
|
}
|
|
|
|
|
2013-04-01 20:08:13 +00:00
|
|
|
#include "qt-gui.moc"
|