2013-06-17 22:58:26 +00:00
|
|
|
#include "qthelper.h"
|
2014-07-16 16:54:39 +00:00
|
|
|
#include "gettextfromc.h"
|
2014-06-10 05:45:44 +00:00
|
|
|
#include "statistics.h"
|
2014-06-02 21:28:02 +00:00
|
|
|
#include <exif.h>
|
|
|
|
#include "file.h"
|
2014-07-13 21:36:35 +00:00
|
|
|
#include <QFile>
|
2013-10-05 16:48:26 +00:00
|
|
|
#include <QRegExp>
|
2013-12-25 00:26:00 +00:00
|
|
|
#include <QDir>
|
2013-10-05 16:48:26 +00:00
|
|
|
#include <QDebug>
|
2014-04-17 15:21:39 +00:00
|
|
|
#include <QSettings>
|
|
|
|
#include <libxslt/documents.h>
|
|
|
|
|
2014-07-16 16:54:39 +00:00
|
|
|
#define translate(_context, arg) trGettext(arg)
|
2015-02-04 08:30:24 +00:00
|
|
|
static const QString DEGREE_SIGNS("dD" UTF8_DEGREE);
|
2013-06-17 22:58:26 +00:00
|
|
|
|
2013-10-04 18:39:33 +00:00
|
|
|
QString weight_string(int weight_in_grams)
|
|
|
|
{
|
|
|
|
QString str;
|
|
|
|
if (get_units()->weight == units::KG) {
|
|
|
|
int gr = weight_in_grams % 1000;
|
|
|
|
int kg = weight_in_grams / 1000;
|
2014-02-28 04:09:57 +00:00
|
|
|
if (kg >= 20.0) {
|
|
|
|
str = QString("0");
|
|
|
|
} else {
|
|
|
|
str = QString("%1.%2").arg(kg).arg((unsigned)(gr) / 100);
|
|
|
|
}
|
2013-10-04 18:39:33 +00:00
|
|
|
} else {
|
|
|
|
double lbs = grams_to_lbs(weight_in_grams);
|
2014-02-28 04:09:57 +00:00
|
|
|
str = QString("%1").arg(lbs, 0, 'f', lbs >= 40.0 ? 0 : 1);
|
2013-10-04 18:39:33 +00:00
|
|
|
}
|
|
|
|
return (str);
|
|
|
|
}
|
2013-10-05 16:48:26 +00:00
|
|
|
|
2014-05-12 16:19:57 +00:00
|
|
|
QString printGPSCoords(int lat, int lon)
|
|
|
|
{
|
|
|
|
unsigned int latdeg, londeg;
|
|
|
|
unsigned int latmin, lonmin;
|
|
|
|
double latsec, lonsec;
|
|
|
|
QString lath, lonh, result;
|
|
|
|
|
|
|
|
if (!lat && !lon)
|
|
|
|
return QString();
|
|
|
|
|
2014-07-16 16:54:39 +00:00
|
|
|
lath = lat >= 0 ? translate("gettextFromC", "N") : translate("gettextFromC", "S");
|
|
|
|
lonh = lon >= 0 ? translate("gettextFromC", "E") : translate("gettextFromC", "W");
|
2014-05-12 16:19:57 +00:00
|
|
|
lat = abs(lat);
|
|
|
|
lon = abs(lon);
|
2015-02-04 08:30:24 +00:00
|
|
|
latdeg = lat / 1000000U;
|
|
|
|
londeg = lon / 1000000U;
|
|
|
|
latmin = (lat % 1000000U) * 60U;
|
|
|
|
lonmin = (lon % 1000000U) * 60U;
|
2014-05-12 16:19:57 +00:00
|
|
|
latsec = (latmin % 1000000) * 60;
|
|
|
|
lonsec = (lonmin % 1000000) * 60;
|
|
|
|
result.sprintf("%u%s%02d\'%06.3f\"%s %u%s%02d\'%06.3f\"%s",
|
|
|
|
latdeg, UTF8_DEGREE, latmin / 1000000, latsec / 1000000, lath.toUtf8().data(),
|
|
|
|
londeg, UTF8_DEGREE, lonmin / 1000000, lonsec / 1000000, lonh.toUtf8().data());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-02-04 08:30:24 +00:00
|
|
|
static bool parseCoord(const QString& txt, int& pos, const QString& positives,
|
|
|
|
const QString& negatives, const QString& others,
|
|
|
|
double& value)
|
|
|
|
{
|
|
|
|
bool numberDefined = false, degreesDefined = false,
|
|
|
|
minutesDefined = false, secondsDefined = false;
|
|
|
|
double number = 0.0;
|
|
|
|
int posBeforeNumber = pos;
|
|
|
|
int sign = 0;
|
|
|
|
value = 0.0;
|
|
|
|
while (pos < txt.size()) {
|
|
|
|
if (txt[pos].isDigit()) {
|
|
|
|
if (numberDefined)
|
|
|
|
return false;
|
|
|
|
QRegExp numberRe("(\\d+(?:[\\.,]\\d+)?).*");
|
|
|
|
if (!numberRe.exactMatch(txt.mid(pos)))
|
|
|
|
return false;
|
|
|
|
number = numberRe.cap(1).toDouble();
|
|
|
|
numberDefined = true;
|
|
|
|
posBeforeNumber = pos;
|
|
|
|
pos += numberRe.cap(1).size() - 1;
|
|
|
|
} else if (positives.indexOf(txt[pos].toUpper()) >= 0) {
|
|
|
|
if (sign != 0)
|
|
|
|
return false;
|
|
|
|
sign = 1;
|
|
|
|
if (degreesDefined || numberDefined) {
|
|
|
|
//sign after the degrees =>
|
|
|
|
//at the end of the coordinate
|
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (negatives.indexOf(txt[pos].toUpper()) >= 0) {
|
|
|
|
if (sign != 0) {
|
|
|
|
if (others.indexOf(txt[pos]) >= 0)
|
|
|
|
//special case for the '-' sign => next coordinate
|
|
|
|
break;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sign = -1;
|
|
|
|
if (degreesDefined || numberDefined) {
|
|
|
|
//sign after the degrees =>
|
|
|
|
//at the end of the coordinate
|
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (others.indexOf(txt[pos].toUpper()) >= 0) {
|
|
|
|
//we are at the next coordinate.
|
|
|
|
break;
|
|
|
|
} else if (DEGREE_SIGNS.indexOf(txt[pos]) >= 0) {
|
|
|
|
if (!numberDefined)
|
|
|
|
return false;
|
|
|
|
if (degreesDefined) {
|
|
|
|
//next coordinate => need to put back the number
|
|
|
|
pos = posBeforeNumber;
|
|
|
|
numberDefined = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
value += number;
|
|
|
|
numberDefined = false;
|
|
|
|
degreesDefined = true;
|
|
|
|
} else if (txt[pos] == '\'') {
|
|
|
|
if (!numberDefined || minutesDefined)
|
|
|
|
return false;
|
|
|
|
value += number / 60.0;
|
|
|
|
numberDefined = false;
|
|
|
|
minutesDefined = true;
|
|
|
|
} else if (txt[pos] == '"') {
|
|
|
|
if (!numberDefined || secondsDefined)
|
|
|
|
return false;
|
|
|
|
value += number / 3600.0;
|
|
|
|
numberDefined = false;
|
|
|
|
secondsDefined = true;
|
|
|
|
} else if (txt[pos] == ' ' || txt[pos] == '\t') {
|
|
|
|
//ignore spaces
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
if (!degreesDefined && numberDefined) {
|
|
|
|
value = number; //just a single number => degrees
|
|
|
|
numberDefined = false;
|
|
|
|
degreesDefined = true;
|
|
|
|
}
|
|
|
|
if (!degreesDefined || numberDefined)
|
|
|
|
return false;
|
|
|
|
if (sign == -1) value *= -1.0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
bool parseGpsText(const QString &gps_text, double *latitude, double *longitude)
|
2013-10-05 16:48:26 +00:00
|
|
|
{
|
2015-02-04 08:30:24 +00:00
|
|
|
const QString trimmed = gps_text.trimmed();
|
|
|
|
if (trimmed.isEmpty()) {
|
2013-10-05 16:48:26 +00:00
|
|
|
*latitude = 0.0;
|
|
|
|
*longitude = 0.0;
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-04 08:30:24 +00:00
|
|
|
int pos = 0;
|
|
|
|
static const QString POS_LAT = QString("+N") + translate("gettextFromC", "N");
|
|
|
|
static const QString NEG_LAT = QString("-S") + translate("gettextFromC", "S");
|
|
|
|
static const QString POS_LON = QString("+E") + translate("gettextFromC", "E");
|
|
|
|
static const QString NEG_LON = QString("-W") + translate("gettextFromC", "W");
|
|
|
|
return parseCoord(gps_text, pos, POS_LAT, NEG_LAT, POS_LON + NEG_LON, *latitude) &&
|
|
|
|
parseCoord(gps_text, pos, POS_LON, NEG_LON, "", *longitude) &&
|
|
|
|
pos == gps_text.size();
|
2013-10-05 16:48:26 +00:00
|
|
|
}
|
|
|
|
|
2014-06-03 01:13:50 +00:00
|
|
|
bool gpsHasChanged(struct dive *dive, struct dive *master, const QString &gps_text, bool *parsed_out)
|
2013-10-05 16:48:26 +00:00
|
|
|
{
|
|
|
|
double latitude, longitude;
|
|
|
|
int latudeg, longudeg;
|
2014-06-03 01:13:50 +00:00
|
|
|
bool ignore;
|
|
|
|
bool *parsed = parsed_out ?: &ignore;
|
2014-07-30 16:46:21 +00:00
|
|
|
*parsed = true;
|
2013-10-05 16:48:26 +00:00
|
|
|
|
|
|
|
/* if we have a master and the dive's gps address is different from it,
|
|
|
|
* don't change the dive */
|
|
|
|
if (master && (master->latitude.udeg != dive->latitude.udeg ||
|
|
|
|
master->longitude.udeg != dive->longitude.udeg))
|
|
|
|
return false;
|
|
|
|
|
2013-12-23 22:56:40 +00:00
|
|
|
if (!(*parsed = parseGpsText(gps_text, &latitude, &longitude)))
|
2013-10-05 16:48:26 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
latudeg = rint(1000000 * latitude);
|
|
|
|
longudeg = rint(1000000 * longitude);
|
|
|
|
|
|
|
|
/* if dive gps didn't change, nothing changed */
|
|
|
|
if (dive->latitude.udeg == latudeg && dive->longitude.udeg == longudeg)
|
|
|
|
return false;
|
|
|
|
/* ok, update the dive and mark things changed */
|
|
|
|
dive->latitude.udeg = latudeg;
|
|
|
|
dive->longitude.udeg = longudeg;
|
|
|
|
return true;
|
2013-11-26 17:44:18 +00:00
|
|
|
}
|
2013-10-05 16:48:26 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
QList<int> getDivesInTrip(dive_trip_t *trip)
|
2013-11-26 17:44:18 +00:00
|
|
|
{
|
|
|
|
QList<int> ret;
|
2014-05-11 01:12:40 +00:00
|
|
|
int i;
|
|
|
|
struct dive *d;
|
|
|
|
for_each_dive (i, d) {
|
2014-02-28 04:09:57 +00:00
|
|
|
if (d->divetrip == trip) {
|
2013-11-26 17:44:18 +00:00
|
|
|
ret.push_back(get_divenr(d));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2013-10-05 16:48:26 +00:00
|
|
|
}
|
2014-01-07 01:30:01 +00:00
|
|
|
|
|
|
|
// we need this to be uniq, but also make sure
|
|
|
|
// it doesn't change during the life time of a Subsurface session
|
|
|
|
// oh, and it has no meaning whatsoever - that's why we have the
|
|
|
|
// silly initial number and increment by 3 :-)
|
2014-05-11 01:18:37 +00:00
|
|
|
int dive_getUniqID(struct dive *d)
|
2014-01-07 01:30:01 +00:00
|
|
|
{
|
|
|
|
static QSet<int> ids;
|
|
|
|
static int maxId = 83529;
|
|
|
|
|
|
|
|
int id = d->id;
|
|
|
|
if (id) {
|
|
|
|
if (!ids.contains(id)) {
|
|
|
|
qDebug() << "WTF - only I am allowed to create IDs";
|
|
|
|
ids.insert(id);
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
maxId += 3;
|
|
|
|
id = maxId;
|
|
|
|
Q_ASSERT(!ids.contains(id));
|
|
|
|
ids.insert(id);
|
|
|
|
return id;
|
|
|
|
}
|
2014-04-17 15:21:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
static xmlDocPtr get_stylesheet_doc(const xmlChar *uri, xmlDictPtr, int, void *, xsltLoadType)
|
|
|
|
{
|
|
|
|
QFile f(QLatin1String(":/xslt/") + (const char *)uri);
|
|
|
|
if (!f.open(QIODevice::ReadOnly))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Load and parse the data */
|
|
|
|
QByteArray source = f.readAll();
|
|
|
|
|
|
|
|
xmlDocPtr doc = xmlParseMemory(source, source.size());
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" xsltStylesheetPtr get_stylesheet(const char *name)
|
|
|
|
{
|
|
|
|
// this needs to be done only once, but doesn't hurt to run every time
|
|
|
|
xsltSetLoaderFunc(get_stylesheet_doc);
|
|
|
|
|
|
|
|
// get main document:
|
|
|
|
xmlDocPtr doc = get_stylesheet_doc((const xmlChar *)name, NULL, 0, NULL, XSLT_LOAD_START);
|
|
|
|
if (!doc)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// xsltSetGenericErrorFunc(stderr, NULL);
|
|
|
|
xsltStylesheetPtr xslt = xsltParseStylesheetDoc(doc);
|
|
|
|
if (!xslt) {
|
|
|
|
xmlFreeDoc(doc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return xslt;
|
|
|
|
}
|
2014-06-02 21:28:02 +00:00
|
|
|
|
2014-06-09 00:17:49 +00:00
|
|
|
extern "C" void picture_load_exif_data(struct picture *p, timestamp_t *timestamp)
|
2014-06-02 21:28:02 +00:00
|
|
|
{
|
|
|
|
EXIFInfo exif;
|
|
|
|
memblock mem;
|
|
|
|
|
|
|
|
if (readfile(p->filename, &mem) <= 0)
|
|
|
|
goto picture_load_exit;
|
|
|
|
if (exif.parseFrom((const unsigned char *)mem.buffer, (unsigned)mem.size) != PARSE_EXIF_SUCCESS)
|
|
|
|
goto picture_load_exit;
|
2014-06-09 00:17:49 +00:00
|
|
|
*timestamp = exif.epoch();
|
2014-06-02 21:28:02 +00:00
|
|
|
p->longitude.udeg= lrint(1000000.0 * exif.GeoLocation.Longitude);
|
|
|
|
p->latitude.udeg = lrint(1000000.0 * exif.GeoLocation.Latitude);
|
2014-07-01 16:29:01 +00:00
|
|
|
|
|
|
|
picture_load_exit:
|
|
|
|
free(mem.buffer);
|
|
|
|
return;
|
2014-06-02 21:28:02 +00:00
|
|
|
}
|
2014-06-10 05:45:44 +00:00
|
|
|
|
2014-08-17 17:42:47 +00:00
|
|
|
extern "C" char *get_file_name(const char *fileName)
|
2014-07-13 21:36:35 +00:00
|
|
|
{
|
2014-08-17 17:42:47 +00:00
|
|
|
QFileInfo fileInfo(fileName);
|
|
|
|
return strdup(fileInfo.fileName().toUtf8());
|
2014-07-13 21:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void copy_image_and_overwrite(const char *cfileName, const char *cnewName)
|
|
|
|
{
|
|
|
|
QString fileName = QString::fromUtf8(cfileName);
|
|
|
|
QString newName = QString::fromUtf8(cnewName);
|
2014-08-17 17:42:47 +00:00
|
|
|
newName += QFileInfo(cfileName).fileName();
|
2014-07-13 21:36:35 +00:00
|
|
|
QFile file(newName);
|
|
|
|
if (file.exists())
|
|
|
|
file.remove();
|
|
|
|
QFile::copy(fileName, newName);
|
|
|
|
}
|
|
|
|
|
2014-11-13 00:01:58 +00:00
|
|
|
extern "C" bool string_sequence_contains(const char *string_sequence, const char *text)
|
|
|
|
{
|
|
|
|
if (same_string(text, "") || same_string(string_sequence, ""))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QString stringSequence(string_sequence);
|
|
|
|
QStringList strings = stringSequence.split(",", QString::SkipEmptyParts);
|
2014-11-13 19:36:49 +00:00
|
|
|
Q_FOREACH (const QString& string, strings) {
|
2014-11-13 00:01:58 +00:00
|
|
|
if (string.trimmed().compare(QString(text).trimmed(), Qt::CaseInsensitive) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-10 05:45:44 +00:00
|
|
|
static bool lessThan(const QPair<QString, int> &a, const QPair<QString, int> &b)
|
|
|
|
{
|
|
|
|
return a.second < b.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void selectedDivesGasUsed(QVector<QPair<QString, int> > &gasUsedOrdered)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
struct dive *d;
|
|
|
|
QMap<QString, int> gasUsed;
|
|
|
|
for_each_dive (i, d) {
|
|
|
|
if (!d->selected)
|
|
|
|
continue;
|
|
|
|
volume_t diveGases[MAX_CYLINDERS] = {};
|
|
|
|
get_gas_used(d, diveGases);
|
|
|
|
for (j = 0; j < MAX_CYLINDERS; j++)
|
|
|
|
if (diveGases[j].mliter) {
|
|
|
|
QString gasName = gasname(&d->cylinder[j].gasmix);
|
|
|
|
gasUsed[gasName] += diveGases[j].mliter;
|
|
|
|
}
|
|
|
|
}
|
2014-11-13 19:37:54 +00:00
|
|
|
Q_FOREACH(const QString& gas, gasUsed.keys()) {
|
2014-06-10 20:38:51 +00:00
|
|
|
gasUsedOrdered.append(qMakePair(gas, gasUsed[gas]));
|
2014-06-10 05:45:44 +00:00
|
|
|
}
|
|
|
|
qSort(gasUsedOrdered.begin(), gasUsedOrdered.end(), lessThan);
|
|
|
|
}
|