diff --git a/CMakeLists.txt b/CMakeLists.txt index 95872d1b4..ba318ca60 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,6 +102,8 @@ SET(SUBSURFACE_CORE_LIB_SRCS linux.c #gettextfrommoc should be added because we are using it on the c-code. gettextfromc.cpp + #dirk ported some core functionality to c++. + qthelper.cpp ) #the interface, in C++ @@ -192,7 +194,7 @@ ADD_DEPENDENCIES(subsurface_corelib version) ENABLE_TESTING() ADD_EXECUTABLE( TestUnitConversion tests/testunitconversion.cpp ) -TARGET_LINK_LIBRARIES( TestUnitConversion ${QT_LIBRARIES}) +TARGET_LINK_LIBRARIES( TestUnitConversion ${QT_LIBRARIES} ${SUBSURFACE_LINK_LIBRARIES} -lzip -ldivecomputer subsurface_corelib) ADD_TEST( NAME TestUnitConversion COMMAND TestUnitConversion) ADD_EXECUTABLE( TestProfile tests/testprofile.cpp ) diff --git a/qt-gui.cpp b/qt-gui.cpp index a3b8b44b6..934589f8d 100644 --- a/qt-gui.cpp +++ b/qt-gui.cpp @@ -9,8 +9,6 @@ #include #include -#include - #include "dive.h" #include "divelist.h" #include "display.h" @@ -45,11 +43,6 @@ // this will create a warning when executing lupdate #define translate(_context, arg) gettextFromC::instance()->tr(arg) -const char *default_dive_computer_vendor; -const char *default_dive_computer_product; -const char *default_dive_computer_device; -DiveComputerList dcList; - static QApplication *application = NULL; static MainWindow *window = NULL; @@ -197,34 +190,6 @@ const QString get_dc_nickname(const char *model, uint32_t deviceid) return model; } -void set_dc_nickname(struct dive *dive) -{ - if (!dive) - return; - - struct divecomputer *dc = &dive->dc; - - while (dc) { - if (dc->model && *dc->model && dc->deviceid && - !dcList.getExact(dc->model, dc->deviceid)) { - // we don't have this one, yet - const DiveComputerNode *existNode = dcList.get(dc->model); - if (existNode) { - // we already have this model but a different deviceid - QString simpleNick(dc->model); - if (dc->deviceid == 0) - simpleNick.append(" (unknown deviceid)"); - else - simpleNick.append(" (").append(QString::number(dc->deviceid, 16)).append(")"); - dcList.addDC(dc->model, dc->deviceid, simpleNick); - } else { - dcList.addDC(dc->model, dc->deviceid); - } - } - dc = dc->next; - } -} - QString get_depth_string(int mm, bool showunit, bool showdecimal) { if (prefs.units.length == units::METERS) { @@ -358,55 +323,6 @@ double get_screen_dpi() return mydesk->physicalDpiX(); } -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(); -} - QString getSubsurfaceDataPath(QString folderToFind) { QString execdir; @@ -440,28 +356,6 @@ QString getSubsurfaceDataPath(QString folderToFind) return QString(""); } -void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname) -{ - dcList.addDC(model, deviceid, nickname, serial, firmware); -} - -bool compareDC(const DiveComputerNode &a, const DiveComputerNode &b) -{ - return a.deviceId < b.deviceId; -} - -void call_for_each_dc(void *f, void (*callback)(void *, const char *, uint32_t, - const char *, const char *, const char *)) -{ - QList values = dcList.dcMap.values(); - qSort(values.begin(), values.end(), compareDC); - for (int i = 0; i < values.size(); i++) { - const DiveComputerNode *node = &values.at(i); - callback(f, node->model.toUtf8().data(), node->deviceId, node->nickName.toUtf8().data(), - node->serialNumber.toUtf8().data(), node->firmware.toUtf8().data()); - } -} - int gettimezoneoffset() { QDateTime dt1 = QDateTime::currentDateTime(); @@ -530,36 +424,3 @@ QString get_trip_date_string(timestamp_t when, int nr) .arg(monthname(tm.tm_mon)) .arg(tm.tm_year + 1900); } - -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; -} - -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; -} diff --git a/qthelper.cpp b/qthelper.cpp index 8ad85402a..c8f4668ff 100644 --- a/qthelper.cpp +++ b/qthelper.cpp @@ -1,12 +1,19 @@ #include "qthelper.h" -#include "qt-ui/mainwindow.h" #include "qt-gui.h" #include #include -#include -#include -#define tr(_arg) MainWindow::instance()->information()->tr(_arg) +#include +#include +#include + +#define tr(_arg) QObject::tr(_arg) + +const char *default_dive_computer_vendor; +const char *default_dive_computer_product; +const char *default_dive_computer_device; +DiveComputerList dcList; + DiveComputerList::DiveComputerList() { } @@ -265,3 +272,136 @@ int getUniqID(struct dive *d) ids.insert(id); return id; } + +extern "C" void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname) +{ + dcList.addDC(model, deviceid, nickname, serial, firmware); +} + +extern "C" bool compareDC(const DiveComputerNode &a, const DiveComputerNode &b) +{ + return a.deviceId < b.deviceId; +} + +extern "C" void call_for_each_dc(void *f, void (*callback)(void *, const char *, uint32_t, + const char *, const char *, const char *)) +{ + QList values = dcList.dcMap.values(); + qSort(values.begin(), values.end(), compareDC); + for (int i = 0; i < values.size(); i++) { + const DiveComputerNode *node = &values.at(i); + callback(f, node->model.toUtf8().data(), node->deviceId, node->nickName.toUtf8().data(), + node->serialNumber.toUtf8().data(), node->firmware.toUtf8().data()); + } +} + + +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; +} + +extern "C" 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); +} + +extern "C" 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(); +} + +extern "C" void set_dc_nickname(struct dive *dive) +{ + if (!dive) + return; + + struct divecomputer *dc = &dive->dc; + + while (dc) { + if (dc->model && *dc->model && dc->deviceid && + !dcList.getExact(dc->model, dc->deviceid)) { + // we don't have this one, yet + const DiveComputerNode *existNode = dcList.get(dc->model); + if (existNode) { + // we already have this model but a different deviceid + QString simpleNick(dc->model); + if (dc->deviceid == 0) + simpleNick.append(" (unknown deviceid)"); + else + simpleNick.append(" (").append(QString::number(dc->deviceid, 16)).append(")"); + dcList.addDC(dc->model, dc->deviceid, simpleNick); + } else { + dcList.addDC(dc->model, dc->deviceid); + } + } + dc = dc->next; + } +} diff --git a/tests/testprofile.cpp b/tests/testprofile.cpp new file mode 100644 index 000000000..d8aea3cae --- /dev/null +++ b/tests/testprofile.cpp @@ -0,0 +1,9 @@ +#include "testprofile.h" +#include "dive.h" + +void TestProfile::testRedCeiling() +{ + parse_file("../dives/deep.xml"); +} + +QTEST_MAIN(TestProfile) \ No newline at end of file diff --git a/tests/testprofile.h b/tests/testprofile.h new file mode 100644 index 000000000..25f094b1b --- /dev/null +++ b/tests/testprofile.h @@ -0,0 +1,12 @@ +#ifndef TESTPROFILE_H +#define TESTPROFILE_H + +#include + +class TestProfile : public QObject{ + Q_OBJECT +private slots: + void testRedCeiling(); +}; + +#endif diff --git a/tests/testunitconversion.cpp b/tests/testunitconversion.cpp index b4d04067f..3adf975c4 100644 --- a/tests/testunitconversion.cpp +++ b/tests/testunitconversion.cpp @@ -20,6 +20,7 @@ void TestUnitConversion::testUnitConversions() QCOMPARE(IS_FP_SAME(bar_to_atm(1.013), 1), true); QCOMPARE(IS_FP_SAME(mbar_to_atm(1013), 1), true); QCOMPARE(mbar_to_PSI(1013), (int)15); + get_units(); } QTEST_MAIN(TestUnitConversion)