subsurface/qt-gui.cpp

134 lines
2.8 KiB
C++
Raw Normal View History

/* qt-gui.cpp */
/* Qt UI implementation */
Conversion to gettext to allow localization This is just the first step - convert the string literals, try to catch all the places where this isn't possible and the program needs to convert string constants at runtime (those are the N_ macros). Add a very rough first German localization so I can at least test what I have done. Seriously, I have never used a localized OS, so I am certain that I have many of the 'standard' translations wrong. Someone please take over :-) Major issues with this: - right now it hardcodes the search path for the message catalog to be ./locale - that's of course bogus, but it works well while doing initial testing. Once the tooling support is there we just should use the OS default. - even though de_DE defaults to ISO-8859-15 (or ISO-8859-1 - the internets can't seem to agree) I went with UTF-8 as that is what Gtk appears to want to use internally. ISO-8859-15 encoded .mo files create funny looking artefacts instead of Umlaute. - no support at all in the Makefile - I was hoping someone with more experience in how to best set this up would contribute a good set of Makefile rules - likely this will help fix the first issue in that it will also install the .mo file(s) in the correct place(s) For now simply run msgfmt -c -o subsurface.mo deutsch.po to create the subsurface.mo file and then move it to ./locale/de_DE.UTF-8/LC_MESSAGES/subsurface.mo If you make changes to the sources and need to add new strings to be translated, this is what seems to work (again, should be tooled through the Makefile): xgettext -o subsurface-new.pot -s -k_ -kN_ --add-comments="++GETTEXT" *.c msgmerge -s -U po/deutsch.po subsurface-new.pot If you do this PLEASE do one commit that just has the new msgid as changes in line numbers create a TON of diff-noise. Do changes to translations in a SEPARATE commit. - no testing at all on Windows or Mac It builds on Windows :-) Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-10-11 00:42:59 +00:00
#include <libintl.h>
#include <glib/gi18n.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <ctype.h>
#include "dive.h"
#include "divelist.h"
#include "display.h"
#include "uemis.h"
#include "device.h"
#include "webservice.h"
#include "version.h"
#include "libdivecomputer.h"
#include "qt-ui/mainwindow.h"
#include <QApplication>
#include <QFileDialog>
#include <QFileInfo>
#include <QStringList>
#include <QTextCodec>
#include <QTranslator>
class Translator: public QTranslator
{
Q_OBJECT
public:
Translator(QObject *parent = 0);
~Translator() {}
virtual QString translate(const char *context, const char *sourceText,
const char *disambiguation = NULL) const;
};
Translator::Translator(QObject *parent):
QTranslator(parent)
{
}
QString Translator::translate(const char *context, const char *sourceText,
const char *disambiguation) const
{
return gettext(sourceText);
}
static QApplication *application = NULL;
int error_count;
const char *existing_filename;
void init_qt_ui(int *argcp, char ***argvp)
{
application->installTranslator(new Translator(application));
MainWindow *window = new MainWindow();
window->show();
}
void init_ui(int *argcp, char ***argvp)
{
application = new QApplication(*argcp, *argvp);
#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
subsurface_open_conf();
load_preferences();
First cut of explicit trip tracking This code establishes the explicit trip data structures and loads and saves them in the XML data. No attempts are made to edit / modify the trips, yet. Loading XML files without trip data creates the trips based on timing as before. Saving out the same, unmodified data will create 'trip' entries in the XML file with a 'number' that reflects the number of dives in that trip. The trip tag also stores the beginning time of the first dive in the trip and the location of the trip (which we display in the summary entries in the UI). The logic allows for dives that aren't part of a dive trip. All other dives simply belong to the "previous" dive trip - i.e. the dive trip with the latest start time that is earlier or equal to the start time of this dive. This logic significantly simplifies the tracking of trips compared to other approaches that I have tried. The automatic grouping into trips now is an option that defaults to off (as it makes changes to the XML file - and people who don't want this feature shouldn't have trips added to their XML files that they then need to manually remove). For now you have to select this option, then exit the program and start it again. Still to do is to trigger the trip generation at run time. We also need a way to mark dives as not part of trips and to allow options to combine trips, split trips, edit trip location data, etc. The code has only had some limited testing when opening multiple files. The code is known to fail if a location name contains unquoted special characters like an "'". This commit also fixes a visual inconsistency in the preferences dialog where the font selector button didn't have a frame around it that told you what this option was about. Inspired-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-08-22 05:04:24 +00:00
default_dive_computer_vendor = subsurface_get_conf("dive_computer_vendor");
default_dive_computer_product = subsurface_get_conf("dive_computer_product");
default_dive_computer_device = subsurface_get_conf("dive_computer_device");
return;
}
void run_ui(void)
{
application->exec();
}
void exit_ui(void)
{
delete application;
subsurface_close_conf();
if (existing_filename)
free((void *)existing_filename);
if (default_dive_computer_device)
free((void *)default_dive_computer_device);
}
void set_filename(const char *filename, gboolean force)
{
if (!force && existing_filename)
return;
free((void *)existing_filename);
if (filename)
existing_filename = strdup(filename);
else
existing_filename = NULL;
}
const char *get_dc_nickname(const char *model, uint32_t deviceid)
{
struct device_info *known = get_device_info(model, deviceid);
if (known) {
if (known->nickname && *known->nickname)
return known->nickname;
else
return known->model;
}
return NULL;
}
void set_dc_nickname(struct dive *dive)
{
/* needs Qt implementation */
}
#include "qt-gui.moc"