From a0798214231c652ac6142228f5ddfc4b65c921f8 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Wed, 2 Sep 2015 19:09:08 -0300 Subject: Start to clean CMake CMake can be a good system but if we keep everything into one big cmake file things can go nuts really quick. Since I already took quite a start on an subsurface layer separation some time ago, I'm improving it by making each module on Subsurface depend on it's own CMake module. This first patch moves the qt-ui part to qt-ui/CMakeLists.txt file, it cleans tons of the main cmake file ( moving all parts to the in ternal folder ), and makes things more easily manageable by the programmer that will change the ui bits, he doesn't need to play hide and seek with the CMakeLists.txt file anymore, trying to figure out where should he put his newly generated file. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- CMakeLists.txt | 89 +-------------- printer.cpp | 273 ----------------------------------------------- printer.h | 48 --------- qt-ui/CMakeLists.txt | 104 ++++++++++++++++++ qt-ui/printer.cpp | 273 +++++++++++++++++++++++++++++++++++++++++++++++ qt-ui/printer.h | 48 +++++++++ qt-ui/templatelayout.cpp | 182 +++++++++++++++++++++++++++++++ qt-ui/templatelayout.h | 168 +++++++++++++++++++++++++++++ templatelayout.cpp | 182 ------------------------------- templatelayout.h | 168 ----------------------------- 10 files changed, 777 insertions(+), 758 deletions(-) delete mode 100644 printer.cpp delete mode 100644 printer.h create mode 100644 qt-ui/CMakeLists.txt create mode 100644 qt-ui/printer.cpp create mode 100644 qt-ui/printer.h create mode 100644 qt-ui/templatelayout.cpp create mode 100644 qt-ui/templatelayout.h delete mode 100644 templatelayout.cpp delete mode 100644 templatelayout.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ae1e9b45..ba6544210 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -159,13 +159,6 @@ else() find_package(Grantlee5 REQUIRED) set(GRANTLEE_LIBRARIES Grantlee5::Templates) endif() - set(SUBSURFACE_PRINTING_SRCS - printer.cpp - templatelayout.cpp - qt-ui/templateedit.cpp - qt-ui/printdialog.cpp - qt-ui/printoptions.cpp - ) set(PRINTING_PKG PrintSupport) set(PRINTING_LIB Qt5::PrintSupport) endif() @@ -174,7 +167,6 @@ if(NO_USERMANUAL) message(STATUS "building without usermanual") add_definitions(-DNO_USERMANUAL) else() - set(USERMANUAL qt-ui/usermanual.cpp) set(WEBKIT_PKG WebKitWidgets) set(WEBKIT_LIB Qt5::WebKitWidgets) endif() @@ -410,71 +402,6 @@ set(SUBSURFACE_MODELS_LIB_SRCS ) source_group("Subsurface Models" FILES ${SUBSURFACE_MODELS}) -# the interface, in C++ -set(SUBSURFACE_INTERFACE - qt-ui/updatemanager.cpp - qt-ui/about.cpp - qt-ui/divecomputermanagementdialog.cpp - qt-ui/divelistview.cpp - qt-ui/diveplanner.cpp - qt-ui/diveshareexportdialog.cpp - qt-ui/downloadfromdivecomputer.cpp - qt-ui/globe.cpp - qt-ui/graphicsview-common.cpp - qt-ui/kmessagewidget.cpp - qt-ui/maintab.cpp - qt-ui/mainwindow.cpp - qt-ui/modeldelegates.cpp - qt-ui/metrics.cpp - qt-ui/notificationwidget.cpp - qt-ui/preferences.cpp - qt-ui/simplewidgets.cpp - qt-ui/starwidget.cpp - qt-ui/subsurfacewebservices.cpp - qt-ui/tableview.cpp - qt-ui/divelogimportdialog.cpp - qt-ui/tagwidget.cpp - qt-ui/groupedlineedit.cpp - ${USERMANUAL} - qt-ui/divelogexportdialog.cpp - qt-ui/divepicturewidget.cpp - qt-ui/usersurvey.cpp - qt-ui/configuredivecomputerdialog.cpp - qt-ui/undocommands.cpp - qt-ui/locationinformation.cpp - qt-ui/qtwaitingspinner.cpp - ${SUBSURFACE_PRINTING_SRCS} - ${SOCIALNETWORKS} - ${BT_SRC_FILES} -) -source_group("Subsurface Interface" FILES ${SUBSURFACE_INTERFACE}) - -# the profile widget -set(SUBSURFACE_PROFILE_LIB_SRCS - qt-ui/profile/profilewidget2.cpp - qt-ui/profile/diverectitem.cpp - qt-ui/profile/divepixmapitem.cpp - qt-ui/profile/divelineitem.cpp - qt-ui/profile/divetextitem.cpp - qt-ui/profile/animationfunctions.cpp - qt-ui/profile/divecartesianaxis.cpp - qt-ui/profile/diveprofileitem.cpp - qt-ui/profile/diveeventitem.cpp - qt-ui/profile/divetooltipitem.cpp - qt-ui/profile/ruleritem.cpp - qt-ui/profile/tankitem.cpp -) -source_group("Subsurface Profile" FILES ${SUBSURFACE_PROFILE_LIB_SRCS}) - -# the yearly statistics widget. -set(SUBSURFACE_STATISTICS_LIB_SRCS - qt-ui/statistics/statisticswidget.cpp - qt-ui/statistics/yearstatistics.cpp - qt-ui/statistics/statisticsbar.cpp - qt-ui/statistics/monthstatistics.cpp -) -source_group("Subsurface Statistics" FILES ${SUBSURFACE_STATISTICS_LIB_SRCS}) - # the main app. set(SUBSURFACE_APP main.cpp @@ -483,24 +410,10 @@ set(SUBSURFACE_APP ) source_group("Subsurface App" FILES ${SUBSURFACE_APP}) -# create the libraries -file(GLOB SUBSURFACE_UI qt-ui/*.ui) -qt5_wrap_ui(SUBSURFACE_UI_HDRS ${SUBSURFACE_UI}) -qt5_add_resources(SUBSURFACE_RESOURCES subsurface.qrc) -source_group("Subsurface Interface Files" FILES ${SUBSURFACE_UI}) - add_library(subsurface_corelib STATIC ${SUBSURFACE_CORE_LIB_SRCS} ) target_link_libraries(subsurface_corelib ${QT_LIBRARIES}) add_library(subsurface_models STATIC ${SUBSURFACE_MODELS_LIB_SRCS}) target_link_libraries(subsurface_models ${QT_LIBRARIES}) -add_library(subsurface_profile STATIC ${SUBSURFACE_PROFILE_LIB_SRCS}) -target_link_libraries(subsurface_profile ${QT_LIBRARIES}) -add_library(subsurface_statistics STATIC ${SUBSURFACE_STATISTICS_LIB_SRCS}) -target_link_libraries(subsurface_statistics ${QT_LIBRARIES}) -add_library(subsurface_generated_ui STATIC ${SUBSURFACE_UI_HDRS}) -target_link_libraries(subsurface_generated_ui ${QT_LIBRARIES}) -add_library(subsurface_interface STATIC ${SUBSURFACE_INTERFACE}) -target_link_libraries(subsurface_interface ${QT_LIBRARIES} ${MARBLE_LIBRARIES}) # add pthread to the end of the library list on Linux # this is only needed on Ubuntu (why do these idiots break everything?) @@ -509,6 +422,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT ANDROID) set(SUBSURFACE_LINK_LIBRARIES ${SUBSURFACE_LINK_LIBRARIES} -lpthread) endif() +add_subdirectory(qt-ui) + # create the executables if(SUBSURFACE_MOBILE) set(MOBILE_SRC qt-mobile/qmlmanager.cpp qt-mobile/qmlprofile.cpp qt-models/divelistmodel.cpp) diff --git a/printer.cpp b/printer.cpp deleted file mode 100644 index f0197d446..000000000 --- a/printer.cpp +++ /dev/null @@ -1,273 +0,0 @@ -#include "printer.h" -#include "templatelayout.h" -#include "statistics.h" -#include "helpers.h" - -#include -#include -#include -#include -#include - -Printer::Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions, PrintMode printMode) -{ - this->paintDevice = paintDevice; - this->printOptions = printOptions; - this->templateOptions = templateOptions; - this->printMode = printMode; - dpi = 0; - done = 0; - webView = new QWebView(); -} - -Printer::~Printer() -{ - delete webView; -} - -void Printer::putProfileImage(QRect profilePlaceholder, QRect viewPort, QPainter *painter, struct dive *dive, QPointer profile) -{ - int x = profilePlaceholder.x() - viewPort.x(); - int y = profilePlaceholder.y() - viewPort.y(); - // use the placeHolder and the viewPort position to calculate the relative position of the dive profile. - QRect pos(x, y, profilePlaceholder.width(), profilePlaceholder.height()); - profile->plotDive(dive, true); - - if (!printOptions->color_selected) { - QImage image(pos.width(), pos.height(), QImage::Format_ARGB32); - QPainter imgPainter(&image); - imgPainter.setRenderHint(QPainter::Antialiasing); - imgPainter.setRenderHint(QPainter::SmoothPixmapTransform); - profile->render(&imgPainter, QRect(0, 0, pos.width(), pos.height())); - imgPainter.end(); - - // convert QImage to grayscale before rendering - for (int i = 0; i < image.height(); i++) { - QRgb *pixel = reinterpret_cast(image.scanLine(i)); - QRgb *end = pixel + image.width(); - for (; pixel != end; pixel++) { - int gray_val = qGray(*pixel); - *pixel = QColor(gray_val, gray_val, gray_val).rgb(); - } - } - - painter->drawImage(pos, image); - } else { - profile->render(painter, pos); - } -} - -void Printer::flowRender() -{ - // add extra padding at the bottom to pages with height not divisible by view port - int paddingBottom = pageSize.height() - (webView->page()->mainFrame()->contentsSize().height() % pageSize.height()); - QString styleString = QString::fromUtf8("padding-bottom: ") + QString::number(paddingBottom) + "px;"; - webView->page()->mainFrame()->findFirstElement("body").setAttribute("style", styleString); - - // render the Qwebview - QPainter painter; - QRect viewPort(0, 0, 0, 0); - painter.begin(paintDevice); - painter.setRenderHint(QPainter::Antialiasing); - painter.setRenderHint(QPainter::SmoothPixmapTransform); - - // get all references to dontbreak divs - int start = 0, end = 0; - int fullPageResolution = webView->page()->mainFrame()->contentsSize().height(); - QWebElementCollection dontbreak = webView->page()->mainFrame()->findAllElements(".dontbreak"); - foreach (QWebElement dontbreakElement, dontbreak) { - if ((dontbreakElement.geometry().y() + dontbreakElement.geometry().height()) - start < pageSize.height()) { - // One more element can be placed - end = dontbreakElement.geometry().y() + dontbreakElement.geometry().height(); - } else { - // fill the page with background color - QRect fullPage(0, 0, pageSize.width(), pageSize.height()); - QBrush fillBrush(templateOptions->color_palette.color1); - painter.fillRect(fullPage, fillBrush); - QRegion reigon(0, 0, pageSize.width(), end - start); - viewPort.setRect(0, start, pageSize.width(), end - start); - - // render the base Html template - webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer, reigon); - - // scroll the webview to the next page - webView->page()->mainFrame()->scroll(0, dontbreakElement.geometry().y() - start); - - // rendering progress is 4/5 of total work - emit(progessUpdated((end * 80.0 / fullPageResolution) + done)); - - // add new pages only in print mode, while previewing we don't add new pages - if (printMode == Printer::PRINT) - static_cast(paintDevice)->newPage(); - else { - painter.end(); - return; - } - start = dontbreakElement.geometry().y(); - } - } - // render the remianing page - QRect fullPage(0, 0, pageSize.width(), pageSize.height()); - QBrush fillBrush(templateOptions->color_palette.color1); - painter.fillRect(fullPage, fillBrush); - QRegion reigon(0, 0, pageSize.width(), end - start); - webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer, reigon); - - painter.end(); -} - -void Printer::render(int Pages = 0) -{ - // keep original preferences - QPointer profile = MainWindow::instance()->graphics(); - int profileFrameStyle = profile->frameStyle(); - int animationOriginal = prefs.animation_speed; - double fontScale = profile->getFontPrintScale(); - double printFontScale = 1.0; - - // apply printing settings to profile - profile->setFrameStyle(QFrame::NoFrame); - profile->setPrintMode(true, !printOptions->color_selected); - profile->setToolTipVisibile(false); - prefs.animation_speed = 0; - - // render the Qwebview - QPainter painter; - QRect viewPort(0, 0, pageSize.width(), pageSize.height()); - painter.begin(paintDevice); - painter.setRenderHint(QPainter::Antialiasing); - painter.setRenderHint(QPainter::SmoothPixmapTransform); - - // get all refereces to diveprofile class in the Html template - QWebElementCollection collection = webView->page()->mainFrame()->findAllElements(".diveprofile"); - - QSize originalSize = profile->size(); - if (collection.count() > 0) { - printFontScale = (double)collection.at(0).geometry().size().height() / (double)profile->size().height(); - profile->resize(collection.at(0).geometry().size()); - } - profile->setFontPrintScale(printFontScale); - - int elemNo = 0; - for (int i = 0; i < Pages; i++) { - // render the base Html template - webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer); - - // render all the dive profiles in the current page - while (elemNo < collection.count() && collection.at(elemNo).geometry().y() < viewPort.y() + viewPort.height()) { - // dive id field should be dive_{{dive_no}} se we remove the first 5 characters - QString diveIdString = collection.at(elemNo).attribute("id"); - int diveId = diveIdString.remove(0, 5).toInt(0, 10); - putProfileImage(collection.at(elemNo).geometry(), viewPort, &painter, get_dive_by_uniq_id(diveId), profile); - elemNo++; - } - - // scroll the webview to the next page - webView->page()->mainFrame()->scroll(0, pageSize.height()); - viewPort.adjust(0, pageSize.height(), 0, pageSize.height()); - - // rendering progress is 4/5 of total work - emit(progessUpdated((i * 80.0 / Pages) + done)); - if (i < Pages - 1 && printMode == Printer::PRINT) - static_cast(paintDevice)->newPage(); - } - painter.end(); - - // return profle settings - profile->setFrameStyle(profileFrameStyle); - profile->setPrintMode(false); - profile->setFontPrintScale(fontScale); - profile->setToolTipVisibile(true); - profile->resize(originalSize); - prefs.animation_speed = animationOriginal; - - //replot the dive after returning the settings - profile->plotDive(0, true); -} - -//value: ranges from 0 : 100 and shows the progress of the templating engine -void Printer::templateProgessUpdated(int value) -{ - done = value / 5; //template progess if 1/5 of total work - emit progessUpdated(done); -} - -void Printer::print() -{ - // we can only print if "PRINT" mode is selected - if (printMode != Printer::PRINT) { - return; - } - - - QPrinter *printerPtr; - printerPtr = static_cast(paintDevice); - - TemplateLayout t(printOptions, templateOptions); - connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int))); - dpi = printerPtr->resolution(); - //rendering resolution = selected paper size in inchs * printer dpi - pageSize.setHeight(qCeil(printerPtr->pageRect(QPrinter::Inch).height() * dpi)); - pageSize.setWidth(qCeil(printerPtr->pageRect(QPrinter::Inch).width() * dpi)); - webView->page()->setViewportSize(pageSize); - webView->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff); - // export border width with at least 1 pixel - templateOptions->border_width = std::max(1, pageSize.width() / 1000); - if (printOptions->type == print_options::DIVELIST) { - webView->setHtml(t.generate()); - } else if (printOptions->type == print_options::STATISTICS ) { - webView->setHtml(t.generateStatistics()); - } - if (printOptions->color_selected && printerPtr->colorMode()) { - printerPtr->setColorMode(QPrinter::Color); - } else { - printerPtr->setColorMode(QPrinter::GrayScale); - } - // apply user settings - int divesPerPage; - - // get number of dives per page from data-numberofdives attribute in the body of the selected template - bool ok; - divesPerPage = webView->page()->mainFrame()->findFirstElement("body").attribute("data-numberofdives").toInt(&ok); - if (!ok) { - divesPerPage = 1; // print each dive in a single page if the attribute is missing or malformed - //TODO: show warning - } - int Pages; - if (divesPerPage == 0) { - flowRender(); - } else { - Pages = qCeil(getTotalWork(printOptions) / (float)divesPerPage); - render(Pages); - } -} - -void Printer::previewOnePage() -{ - if (printMode == PREVIEW) { - TemplateLayout t(printOptions, templateOptions); - - pageSize.setHeight(paintDevice->height()); - pageSize.setWidth(paintDevice->width()); - webView->page()->setViewportSize(pageSize); - // initialize the border settings - templateOptions->border_width = std::max(1, pageSize.width() / 1000); - if (printOptions->type == print_options::DIVELIST) { - webView->setHtml(t.generate()); - } else if (printOptions->type == print_options::STATISTICS ) { - webView->setHtml(t.generateStatistics()); - } - - bool ok; - int divesPerPage = webView->page()->mainFrame()->findFirstElement("body").attribute("data-numberofdives").toInt(&ok); - if (!ok) { - divesPerPage = 1; // print each dive in a single page if the attribute is missing or malformed - //TODO: show warning - } - if (divesPerPage == 0) { - flowRender(); - } else { - render(1); - } - } -} diff --git a/printer.h b/printer.h deleted file mode 100644 index 979cacd6a..000000000 --- a/printer.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef PRINTER_H -#define PRINTER_H - -#include -#include -#include -#include - -#include "profile/profilewidget2.h" -#include "printoptions.h" -#include "templateedit.h" - -class Printer : public QObject { - Q_OBJECT - -public: - enum PrintMode { - PRINT, - PREVIEW - }; - -private: - QPaintDevice *paintDevice; - QWebView *webView; - print_options *printOptions; - template_options *templateOptions; - QSize pageSize; - PrintMode printMode; - int done; - int dpi; - void render(int Pages); - void flowRender(); - void putProfileImage(QRect box, QRect viewPort, QPainter *painter, struct dive *dive, QPointer profile); - -private slots: - void templateProgessUpdated(int value); - -public: - Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions, PrintMode printMode); - ~Printer(); - void print(); - void previewOnePage(); - -signals: - void progessUpdated(int value); -}; - -#endif //PRINTER_H diff --git a/qt-ui/CMakeLists.txt b/qt-ui/CMakeLists.txt new file mode 100644 index 000000000..9def39ff3 --- /dev/null +++ b/qt-ui/CMakeLists.txt @@ -0,0 +1,104 @@ +# create the libraries +file(GLOB SUBSURFACE_UI *.ui) +qt5_wrap_ui(SUBSURFACE_UI_HDRS ${SUBSURFACE_UI}) +qt5_add_resources(SUBSURFACE_RESOURCES subsurface.qrc) +source_group("Subsurface Interface Files" FILES ${SUBSURFACE_UI}) + +# the interface, in C++ +set(SUBSURFACE_INTERFACE + updatemanager.cpp + about.cpp + divecomputermanagementdialog.cpp + divelistview.cpp + diveplanner.cpp + diveshareexportdialog.cpp + downloadfromdivecomputer.cpp + globe.cpp + graphicsview-common.cpp + kmessagewidget.cpp + maintab.cpp + mainwindow.cpp + modeldelegates.cpp + metrics.cpp + notificationwidget.cpp + preferences.cpp + simplewidgets.cpp + starwidget.cpp + subsurfacewebservices.cpp + tableview.cpp + divelogimportdialog.cpp + tagwidget.cpp + groupedlineedit.cpp + divelogexportdialog.cpp + divepicturewidget.cpp + usersurvey.cpp + configuredivecomputerdialog.cpp + undocommands.cpp + locationinformation.cpp + qtwaitingspinner.cpp +) + +if(NOT NO_USERMANUAL) + set(SUBSURFACE_INTERFACE ${SUBSURFACE_INTERFACE} + usermanual.cpp + ) +endif() + +if(NOT NO_PRINTING) + set(SUBSURFACE_INTERFACE ${SUBSURFACE_INTERFACE} + templateedit.cpp + printdialog.cpp + printoptions.cpp + printer.cpp + templatelayout.cpp + ) +endif() + +if (FBSUPPORT) + set(SUBSURFACE_INTERFACE ${SUBSURFACE_INTERFACE} + socialnetworks.cpp + ) +endif() + +if (BTSUPPORT) + set(SUBSURFACE_INTERFACE ${SUBSURFACE_INTERFACE} + btdeviceselectiondialog.cpp + ) +endif() + +source_group("Subsurface Interface" FILES ${SUBSURFACE_INTERFACE}) + +# the profile widget +set(SUBSURFACE_PROFILE_LIB_SRCS + profile/profilewidget2.cpp + profile/diverectitem.cpp + profile/divepixmapitem.cpp + profile/divelineitem.cpp + profile/divetextitem.cpp + profile/animationfunctions.cpp + profile/divecartesianaxis.cpp + profile/diveprofileitem.cpp + profile/diveeventitem.cpp + profile/divetooltipitem.cpp + profile/ruleritem.cpp + profile/tankitem.cpp +) +source_group("Subsurface Profile" FILES ${SUBSURFACE_PROFILE_LIB_SRCS}) + +# the yearly statistics widget. +set(SUBSURFACE_STATISTICS_LIB_SRCS + statistics/statisticswidget.cpp + statistics/yearstatistics.cpp + statistics/statisticsbar.cpp + statistics/monthstatistics.cpp +) +source_group("Subsurface Statistics" FILES ${SUBSURFACE_STATISTICS_LIB_SRCS}) + +add_library(subsurface_profile STATIC ${SUBSURFACE_PROFILE_LIB_SRCS}) +target_link_libraries(subsurface_profile ${QT_LIBRARIES}) +add_library(subsurface_statistics STATIC ${SUBSURFACE_STATISTICS_LIB_SRCS}) +target_link_libraries(subsurface_statistics ${QT_LIBRARIES}) +add_library(subsurface_generated_ui STATIC ${SUBSURFACE_UI_HDRS}) +target_link_libraries(subsurface_generated_ui ${QT_LIBRARIES}) +add_library(subsurface_interface STATIC ${SUBSURFACE_INTERFACE}) +target_link_libraries(subsurface_interface ${QT_LIBRARIES} ${MARBLE_LIBRARIES}) diff --git a/qt-ui/printer.cpp b/qt-ui/printer.cpp new file mode 100644 index 000000000..f0197d446 --- /dev/null +++ b/qt-ui/printer.cpp @@ -0,0 +1,273 @@ +#include "printer.h" +#include "templatelayout.h" +#include "statistics.h" +#include "helpers.h" + +#include +#include +#include +#include +#include + +Printer::Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions, PrintMode printMode) +{ + this->paintDevice = paintDevice; + this->printOptions = printOptions; + this->templateOptions = templateOptions; + this->printMode = printMode; + dpi = 0; + done = 0; + webView = new QWebView(); +} + +Printer::~Printer() +{ + delete webView; +} + +void Printer::putProfileImage(QRect profilePlaceholder, QRect viewPort, QPainter *painter, struct dive *dive, QPointer profile) +{ + int x = profilePlaceholder.x() - viewPort.x(); + int y = profilePlaceholder.y() - viewPort.y(); + // use the placeHolder and the viewPort position to calculate the relative position of the dive profile. + QRect pos(x, y, profilePlaceholder.width(), profilePlaceholder.height()); + profile->plotDive(dive, true); + + if (!printOptions->color_selected) { + QImage image(pos.width(), pos.height(), QImage::Format_ARGB32); + QPainter imgPainter(&image); + imgPainter.setRenderHint(QPainter::Antialiasing); + imgPainter.setRenderHint(QPainter::SmoothPixmapTransform); + profile->render(&imgPainter, QRect(0, 0, pos.width(), pos.height())); + imgPainter.end(); + + // convert QImage to grayscale before rendering + for (int i = 0; i < image.height(); i++) { + QRgb *pixel = reinterpret_cast(image.scanLine(i)); + QRgb *end = pixel + image.width(); + for (; pixel != end; pixel++) { + int gray_val = qGray(*pixel); + *pixel = QColor(gray_val, gray_val, gray_val).rgb(); + } + } + + painter->drawImage(pos, image); + } else { + profile->render(painter, pos); + } +} + +void Printer::flowRender() +{ + // add extra padding at the bottom to pages with height not divisible by view port + int paddingBottom = pageSize.height() - (webView->page()->mainFrame()->contentsSize().height() % pageSize.height()); + QString styleString = QString::fromUtf8("padding-bottom: ") + QString::number(paddingBottom) + "px;"; + webView->page()->mainFrame()->findFirstElement("body").setAttribute("style", styleString); + + // render the Qwebview + QPainter painter; + QRect viewPort(0, 0, 0, 0); + painter.begin(paintDevice); + painter.setRenderHint(QPainter::Antialiasing); + painter.setRenderHint(QPainter::SmoothPixmapTransform); + + // get all references to dontbreak divs + int start = 0, end = 0; + int fullPageResolution = webView->page()->mainFrame()->contentsSize().height(); + QWebElementCollection dontbreak = webView->page()->mainFrame()->findAllElements(".dontbreak"); + foreach (QWebElement dontbreakElement, dontbreak) { + if ((dontbreakElement.geometry().y() + dontbreakElement.geometry().height()) - start < pageSize.height()) { + // One more element can be placed + end = dontbreakElement.geometry().y() + dontbreakElement.geometry().height(); + } else { + // fill the page with background color + QRect fullPage(0, 0, pageSize.width(), pageSize.height()); + QBrush fillBrush(templateOptions->color_palette.color1); + painter.fillRect(fullPage, fillBrush); + QRegion reigon(0, 0, pageSize.width(), end - start); + viewPort.setRect(0, start, pageSize.width(), end - start); + + // render the base Html template + webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer, reigon); + + // scroll the webview to the next page + webView->page()->mainFrame()->scroll(0, dontbreakElement.geometry().y() - start); + + // rendering progress is 4/5 of total work + emit(progessUpdated((end * 80.0 / fullPageResolution) + done)); + + // add new pages only in print mode, while previewing we don't add new pages + if (printMode == Printer::PRINT) + static_cast(paintDevice)->newPage(); + else { + painter.end(); + return; + } + start = dontbreakElement.geometry().y(); + } + } + // render the remianing page + QRect fullPage(0, 0, pageSize.width(), pageSize.height()); + QBrush fillBrush(templateOptions->color_palette.color1); + painter.fillRect(fullPage, fillBrush); + QRegion reigon(0, 0, pageSize.width(), end - start); + webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer, reigon); + + painter.end(); +} + +void Printer::render(int Pages = 0) +{ + // keep original preferences + QPointer profile = MainWindow::instance()->graphics(); + int profileFrameStyle = profile->frameStyle(); + int animationOriginal = prefs.animation_speed; + double fontScale = profile->getFontPrintScale(); + double printFontScale = 1.0; + + // apply printing settings to profile + profile->setFrameStyle(QFrame::NoFrame); + profile->setPrintMode(true, !printOptions->color_selected); + profile->setToolTipVisibile(false); + prefs.animation_speed = 0; + + // render the Qwebview + QPainter painter; + QRect viewPort(0, 0, pageSize.width(), pageSize.height()); + painter.begin(paintDevice); + painter.setRenderHint(QPainter::Antialiasing); + painter.setRenderHint(QPainter::SmoothPixmapTransform); + + // get all refereces to diveprofile class in the Html template + QWebElementCollection collection = webView->page()->mainFrame()->findAllElements(".diveprofile"); + + QSize originalSize = profile->size(); + if (collection.count() > 0) { + printFontScale = (double)collection.at(0).geometry().size().height() / (double)profile->size().height(); + profile->resize(collection.at(0).geometry().size()); + } + profile->setFontPrintScale(printFontScale); + + int elemNo = 0; + for (int i = 0; i < Pages; i++) { + // render the base Html template + webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer); + + // render all the dive profiles in the current page + while (elemNo < collection.count() && collection.at(elemNo).geometry().y() < viewPort.y() + viewPort.height()) { + // dive id field should be dive_{{dive_no}} se we remove the first 5 characters + QString diveIdString = collection.at(elemNo).attribute("id"); + int diveId = diveIdString.remove(0, 5).toInt(0, 10); + putProfileImage(collection.at(elemNo).geometry(), viewPort, &painter, get_dive_by_uniq_id(diveId), profile); + elemNo++; + } + + // scroll the webview to the next page + webView->page()->mainFrame()->scroll(0, pageSize.height()); + viewPort.adjust(0, pageSize.height(), 0, pageSize.height()); + + // rendering progress is 4/5 of total work + emit(progessUpdated((i * 80.0 / Pages) + done)); + if (i < Pages - 1 && printMode == Printer::PRINT) + static_cast(paintDevice)->newPage(); + } + painter.end(); + + // return profle settings + profile->setFrameStyle(profileFrameStyle); + profile->setPrintMode(false); + profile->setFontPrintScale(fontScale); + profile->setToolTipVisibile(true); + profile->resize(originalSize); + prefs.animation_speed = animationOriginal; + + //replot the dive after returning the settings + profile->plotDive(0, true); +} + +//value: ranges from 0 : 100 and shows the progress of the templating engine +void Printer::templateProgessUpdated(int value) +{ + done = value / 5; //template progess if 1/5 of total work + emit progessUpdated(done); +} + +void Printer::print() +{ + // we can only print if "PRINT" mode is selected + if (printMode != Printer::PRINT) { + return; + } + + + QPrinter *printerPtr; + printerPtr = static_cast(paintDevice); + + TemplateLayout t(printOptions, templateOptions); + connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int))); + dpi = printerPtr->resolution(); + //rendering resolution = selected paper size in inchs * printer dpi + pageSize.setHeight(qCeil(printerPtr->pageRect(QPrinter::Inch).height() * dpi)); + pageSize.setWidth(qCeil(printerPtr->pageRect(QPrinter::Inch).width() * dpi)); + webView->page()->setViewportSize(pageSize); + webView->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff); + // export border width with at least 1 pixel + templateOptions->border_width = std::max(1, pageSize.width() / 1000); + if (printOptions->type == print_options::DIVELIST) { + webView->setHtml(t.generate()); + } else if (printOptions->type == print_options::STATISTICS ) { + webView->setHtml(t.generateStatistics()); + } + if (printOptions->color_selected && printerPtr->colorMode()) { + printerPtr->setColorMode(QPrinter::Color); + } else { + printerPtr->setColorMode(QPrinter::GrayScale); + } + // apply user settings + int divesPerPage; + + // get number of dives per page from data-numberofdives attribute in the body of the selected template + bool ok; + divesPerPage = webView->page()->mainFrame()->findFirstElement("body").attribute("data-numberofdives").toInt(&ok); + if (!ok) { + divesPerPage = 1; // print each dive in a single page if the attribute is missing or malformed + //TODO: show warning + } + int Pages; + if (divesPerPage == 0) { + flowRender(); + } else { + Pages = qCeil(getTotalWork(printOptions) / (float)divesPerPage); + render(Pages); + } +} + +void Printer::previewOnePage() +{ + if (printMode == PREVIEW) { + TemplateLayout t(printOptions, templateOptions); + + pageSize.setHeight(paintDevice->height()); + pageSize.setWidth(paintDevice->width()); + webView->page()->setViewportSize(pageSize); + // initialize the border settings + templateOptions->border_width = std::max(1, pageSize.width() / 1000); + if (printOptions->type == print_options::DIVELIST) { + webView->setHtml(t.generate()); + } else if (printOptions->type == print_options::STATISTICS ) { + webView->setHtml(t.generateStatistics()); + } + + bool ok; + int divesPerPage = webView->page()->mainFrame()->findFirstElement("body").attribute("data-numberofdives").toInt(&ok); + if (!ok) { + divesPerPage = 1; // print each dive in a single page if the attribute is missing or malformed + //TODO: show warning + } + if (divesPerPage == 0) { + flowRender(); + } else { + render(1); + } + } +} diff --git a/qt-ui/printer.h b/qt-ui/printer.h new file mode 100644 index 000000000..979cacd6a --- /dev/null +++ b/qt-ui/printer.h @@ -0,0 +1,48 @@ +#ifndef PRINTER_H +#define PRINTER_H + +#include +#include +#include +#include + +#include "profile/profilewidget2.h" +#include "printoptions.h" +#include "templateedit.h" + +class Printer : public QObject { + Q_OBJECT + +public: + enum PrintMode { + PRINT, + PREVIEW + }; + +private: + QPaintDevice *paintDevice; + QWebView *webView; + print_options *printOptions; + template_options *templateOptions; + QSize pageSize; + PrintMode printMode; + int done; + int dpi; + void render(int Pages); + void flowRender(); + void putProfileImage(QRect box, QRect viewPort, QPainter *painter, struct dive *dive, QPointer profile); + +private slots: + void templateProgessUpdated(int value); + +public: + Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions, PrintMode printMode); + ~Printer(); + void print(); + void previewOnePage(); + +signals: + void progessUpdated(int value); +}; + +#endif //PRINTER_H diff --git a/qt-ui/templatelayout.cpp b/qt-ui/templatelayout.cpp new file mode 100644 index 000000000..a376459a6 --- /dev/null +++ b/qt-ui/templatelayout.cpp @@ -0,0 +1,182 @@ +#include + +#include "templatelayout.h" +#include "helpers.h" +#include "display.h" + +QList grantlee_templates, grantlee_statistics_templates; + +int getTotalWork(print_options *printOptions) +{ + if (printOptions->print_selected) { + // return the correct number depending on all/selected dives + // but don't return 0 as we might divide by this number + return amount_selected ? amount_selected : 1; + } + int dives = 0, i; + struct dive *dive; + for_each_dive (i, dive) { + dives++; + } + return dives; +} + +void find_all_templates() +{ + grantlee_templates.clear(); + grantlee_statistics_templates.clear(); + QDir dir(getPrintingTemplatePathUser()); + QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot); + foreach (QFileInfo finfo, list) { + QString filename = finfo.fileName(); + if (filename.at(filename.size() - 1) != '~') { + grantlee_templates.append(finfo.fileName()); + } + } + // find statistics templates + dir.setPath(getPrintingTemplatePathUser() + QDir::separator() + "statistics"); + list = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot); + foreach (QFileInfo finfo, list) { + QString filename = finfo.fileName(); + if (filename.at(filename.size() - 1) != '~') { + grantlee_statistics_templates.append(finfo.fileName()); + } + } +} + +TemplateLayout::TemplateLayout(print_options *PrintOptions, template_options *templateOptions) : + m_engine(NULL) +{ + this->PrintOptions = PrintOptions; + this->templateOptions = templateOptions; +} + +TemplateLayout::~TemplateLayout() +{ + delete m_engine; +} + +QString TemplateLayout::generate() +{ + int progress = 0; + int totalWork = getTotalWork(PrintOptions); + + QString htmlContent; + m_engine = new Grantlee::Engine(this); + + QSharedPointer m_templateLoader = + QSharedPointer(new Grantlee::FileSystemTemplateLoader()); + m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser()); + m_engine->addTemplateLoader(m_templateLoader); + + Grantlee::registerMetaType(); + Grantlee::registerMetaType(); + Grantlee::registerMetaType(); + + QVariantList diveList; + + struct dive *dive; + int i; + for_each_dive (i, dive) { + //TODO check for exporting selected dives only + if (!dive->selected && PrintOptions->print_selected) + continue; + Dive d(dive); + diveList.append(QVariant::fromValue(d)); + progress++; + emit progressUpdated(progress * 100.0 / totalWork); + } + Grantlee::Context c; + c.insert("dives", diveList); + c.insert("template_options", QVariant::fromValue(*templateOptions)); + c.insert("print_options", QVariant::fromValue(*PrintOptions)); + + Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template); + if (!t || t->error()) { + qDebug() << "Can't load template"; + return htmlContent; + } + + htmlContent = t->render(&c); + + if (t->error()) { + qDebug() << "Can't render template"; + return htmlContent; + } + return htmlContent; +} + +QString TemplateLayout::generateStatistics() +{ + QString htmlContent; + m_engine = new Grantlee::Engine(this); + + QSharedPointer m_templateLoader = + QSharedPointer(new Grantlee::FileSystemTemplateLoader()); + m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser() + QDir::separator() + QString("statistics")); + m_engine->addTemplateLoader(m_templateLoader); + + Grantlee::registerMetaType(); + Grantlee::registerMetaType(); + Grantlee::registerMetaType(); + + QVariantList years; + + int i = 0; + while (stats_yearly != NULL && stats_yearly[i].period) { + YearInfo year(stats_yearly[i]); + years.append(QVariant::fromValue(year)); + i++; + } + + Grantlee::Context c; + c.insert("years", years); + c.insert("template_options", QVariant::fromValue(*templateOptions)); + c.insert("print_options", QVariant::fromValue(*PrintOptions)); + + Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template); + if (!t || t->error()) { + qDebug() << "Can't load template"; + return htmlContent; + } + + htmlContent = t->render(&c); + + if (t->error()) { + qDebug() << "Can't render template"; + return htmlContent; + } + + emit progressUpdated(100); + return htmlContent; +} + +QString TemplateLayout::readTemplate(QString template_name) +{ + QFile qfile(getPrintingTemplatePathUser() + QDir::separator() + template_name); + if (qfile.open(QFile::ReadOnly | QFile::Text)) { + QTextStream in(&qfile); + return in.readAll(); + } + return ""; +} + +void TemplateLayout::writeTemplate(QString template_name, QString grantlee_template) +{ + QFile qfile(getPrintingTemplatePathUser() + QDir::separator() + template_name); + if (qfile.open(QFile::ReadWrite | QFile::Text)) { + qfile.write(grantlee_template.toUtf8().data()); + qfile.resize(qfile.pos()); + qfile.close(); + } +} + +YearInfo::YearInfo() +{ + +} + +YearInfo::~YearInfo() +{ + +} diff --git a/qt-ui/templatelayout.h b/qt-ui/templatelayout.h new file mode 100644 index 000000000..a2868e7ff --- /dev/null +++ b/qt-ui/templatelayout.h @@ -0,0 +1,168 @@ +#ifndef TEMPLATELAYOUT_H +#define TEMPLATELAYOUT_H + +#include +#include "mainwindow.h" +#include "printoptions.h" +#include "statistics.h" +#include "qthelper.h" +#include "helpers.h" + +int getTotalWork(print_options *printOptions); +void find_all_templates(); + +extern QList grantlee_templates, grantlee_statistics_templates; + +class TemplateLayout : public QObject { + Q_OBJECT +public: + TemplateLayout(print_options *PrintOptions, template_options *templateOptions); + ~TemplateLayout(); + QString generate(); + QString generateStatistics(); + static QString readTemplate(QString template_name); + static void writeTemplate(QString template_name, QString grantlee_template); + +private: + Grantlee::Engine *m_engine; + print_options *PrintOptions; + template_options *templateOptions; + +signals: + void progressUpdated(int value); +}; + +class YearInfo { +public: + stats_t *year; + YearInfo(stats_t& year) + :year(&year) + { + + } + YearInfo(); + ~YearInfo(); +}; + +Q_DECLARE_METATYPE(Dive) +Q_DECLARE_METATYPE(template_options) +Q_DECLARE_METATYPE(print_options) +Q_DECLARE_METATYPE(YearInfo) + +GRANTLEE_BEGIN_LOOKUP(Dive) +if (property == "number") + return object.number(); +else if (property == "id") + return object.id(); +else if (property == "date") + return object.date(); +else if (property == "time") + return object.time(); +else if (property == "location") + return object.location(); +else if (property == "duration") + return object.duration(); +else if (property == "depth") + return object.depth(); +else if (property == "divemaster") + return object.divemaster(); +else if (property == "buddy") + return object.buddy(); +else if (property == "airTemp") + return object.airTemp(); +else if (property == "waterTemp") + return object.waterTemp(); +else if (property == "notes") + return object.notes(); +else if (property == "rating") + return object.rating(); +else if (property == "sac") + return object.sac(); +else if (property == "tags") + return object.tags(); +else if (property == "gas") + return object.gas(); +GRANTLEE_END_LOOKUP + +GRANTLEE_BEGIN_LOOKUP(template_options) +if (property == "font") { + switch (object.font_index) { + case 0: + return "Arial, Helvetica, sans-serif"; + case 1: + return "Impact, Charcoal, sans-serif"; + case 2: + return "Georgia, serif"; + case 3: + return "Courier, monospace"; + case 4: + return "Verdana, Geneva, sans-serif"; + } +} else if (property == "borderwidth") { + return object.border_width; +} else if (property == "font_size") { + return object.font_size / 9.0; +} else if (property == "line_spacing") { + return object.line_spacing; +} else if (property == "color1") { + return object.color_palette.color1.name(); +} else if (property == "color2") { + return object.color_palette.color2.name(); +} else if (property == "color3") { + return object.color_palette.color3.name(); +} else if (property == "color4") { + return object.color_palette.color4.name(); +} else if (property == "color5") { + return object.color_palette.color5.name(); +} else if (property == "color6") { + return object.color_palette.color6.name(); +} +GRANTLEE_END_LOOKUP + +GRANTLEE_BEGIN_LOOKUP(print_options) +if (property == "grayscale") { + if (object.color_selected) { + return ""; + } else { + return "-webkit-filter: grayscale(100%)"; + } +} +GRANTLEE_END_LOOKUP + +GRANTLEE_BEGIN_LOOKUP(YearInfo) +if (property == "year") { + return object.year->period; +} else if (property == "dives") { + return object.year->selection_size; +} else if (property == "min_temp") { + const char *unit; + double temp = get_temp_units(object.year->min_temp, &unit); + return object.year->min_temp == 0 ? "0" : QString::number(temp, 'g', 2) + unit; +} else if (property == "max_temp") { + const char *unit; + double temp = get_temp_units(object.year->max_temp, &unit); + return object.year->max_temp == 0 ? "0" : QString::number(temp, 'g', 2) + unit; +} else if (property == "total_time") { + return get_time_string(object.year->total_time.seconds, 0); +} else if (property == "avg_time") { + return get_minutes(object.year->total_time.seconds / object.year->selection_size); +} else if (property == "shortest_time") { + return get_minutes(object.year->shortest_time.seconds); +} else if (property == "longest_time") { + return get_minutes(object.year->longest_time.seconds); +} else if (property == "avg_depth") { + return get_depth_string(object.year->avg_depth); +} else if (property == "min_depth") { + return get_depth_string(object.year->min_depth); +} else if (property == "max_depth") { + return get_depth_string(object.year->max_depth); +} else if (property == "avg_sac") { + return get_volume_string(object.year->avg_sac); +} else if (property == "min_sac") { + return get_volume_string(object.year->min_sac); +} else if (property == "max_sac") { + return get_volume_string(object.year->max_sac); +} +GRANTLEE_END_LOOKUP + +#endif diff --git a/templatelayout.cpp b/templatelayout.cpp deleted file mode 100644 index a376459a6..000000000 --- a/templatelayout.cpp +++ /dev/null @@ -1,182 +0,0 @@ -#include - -#include "templatelayout.h" -#include "helpers.h" -#include "display.h" - -QList grantlee_templates, grantlee_statistics_templates; - -int getTotalWork(print_options *printOptions) -{ - if (printOptions->print_selected) { - // return the correct number depending on all/selected dives - // but don't return 0 as we might divide by this number - return amount_selected ? amount_selected : 1; - } - int dives = 0, i; - struct dive *dive; - for_each_dive (i, dive) { - dives++; - } - return dives; -} - -void find_all_templates() -{ - grantlee_templates.clear(); - grantlee_statistics_templates.clear(); - QDir dir(getPrintingTemplatePathUser()); - QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot); - foreach (QFileInfo finfo, list) { - QString filename = finfo.fileName(); - if (filename.at(filename.size() - 1) != '~') { - grantlee_templates.append(finfo.fileName()); - } - } - // find statistics templates - dir.setPath(getPrintingTemplatePathUser() + QDir::separator() + "statistics"); - list = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot); - foreach (QFileInfo finfo, list) { - QString filename = finfo.fileName(); - if (filename.at(filename.size() - 1) != '~') { - grantlee_statistics_templates.append(finfo.fileName()); - } - } -} - -TemplateLayout::TemplateLayout(print_options *PrintOptions, template_options *templateOptions) : - m_engine(NULL) -{ - this->PrintOptions = PrintOptions; - this->templateOptions = templateOptions; -} - -TemplateLayout::~TemplateLayout() -{ - delete m_engine; -} - -QString TemplateLayout::generate() -{ - int progress = 0; - int totalWork = getTotalWork(PrintOptions); - - QString htmlContent; - m_engine = new Grantlee::Engine(this); - - QSharedPointer m_templateLoader = - QSharedPointer(new Grantlee::FileSystemTemplateLoader()); - m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser()); - m_engine->addTemplateLoader(m_templateLoader); - - Grantlee::registerMetaType(); - Grantlee::registerMetaType(); - Grantlee::registerMetaType(); - - QVariantList diveList; - - struct dive *dive; - int i; - for_each_dive (i, dive) { - //TODO check for exporting selected dives only - if (!dive->selected && PrintOptions->print_selected) - continue; - Dive d(dive); - diveList.append(QVariant::fromValue(d)); - progress++; - emit progressUpdated(progress * 100.0 / totalWork); - } - Grantlee::Context c; - c.insert("dives", diveList); - c.insert("template_options", QVariant::fromValue(*templateOptions)); - c.insert("print_options", QVariant::fromValue(*PrintOptions)); - - Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template); - if (!t || t->error()) { - qDebug() << "Can't load template"; - return htmlContent; - } - - htmlContent = t->render(&c); - - if (t->error()) { - qDebug() << "Can't render template"; - return htmlContent; - } - return htmlContent; -} - -QString TemplateLayout::generateStatistics() -{ - QString htmlContent; - m_engine = new Grantlee::Engine(this); - - QSharedPointer m_templateLoader = - QSharedPointer(new Grantlee::FileSystemTemplateLoader()); - m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser() + QDir::separator() + QString("statistics")); - m_engine->addTemplateLoader(m_templateLoader); - - Grantlee::registerMetaType(); - Grantlee::registerMetaType(); - Grantlee::registerMetaType(); - - QVariantList years; - - int i = 0; - while (stats_yearly != NULL && stats_yearly[i].period) { - YearInfo year(stats_yearly[i]); - years.append(QVariant::fromValue(year)); - i++; - } - - Grantlee::Context c; - c.insert("years", years); - c.insert("template_options", QVariant::fromValue(*templateOptions)); - c.insert("print_options", QVariant::fromValue(*PrintOptions)); - - Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template); - if (!t || t->error()) { - qDebug() << "Can't load template"; - return htmlContent; - } - - htmlContent = t->render(&c); - - if (t->error()) { - qDebug() << "Can't render template"; - return htmlContent; - } - - emit progressUpdated(100); - return htmlContent; -} - -QString TemplateLayout::readTemplate(QString template_name) -{ - QFile qfile(getPrintingTemplatePathUser() + QDir::separator() + template_name); - if (qfile.open(QFile::ReadOnly | QFile::Text)) { - QTextStream in(&qfile); - return in.readAll(); - } - return ""; -} - -void TemplateLayout::writeTemplate(QString template_name, QString grantlee_template) -{ - QFile qfile(getPrintingTemplatePathUser() + QDir::separator() + template_name); - if (qfile.open(QFile::ReadWrite | QFile::Text)) { - qfile.write(grantlee_template.toUtf8().data()); - qfile.resize(qfile.pos()); - qfile.close(); - } -} - -YearInfo::YearInfo() -{ - -} - -YearInfo::~YearInfo() -{ - -} diff --git a/templatelayout.h b/templatelayout.h deleted file mode 100644 index a2868e7ff..000000000 --- a/templatelayout.h +++ /dev/null @@ -1,168 +0,0 @@ -#ifndef TEMPLATELAYOUT_H -#define TEMPLATELAYOUT_H - -#include -#include "mainwindow.h" -#include "printoptions.h" -#include "statistics.h" -#include "qthelper.h" -#include "helpers.h" - -int getTotalWork(print_options *printOptions); -void find_all_templates(); - -extern QList grantlee_templates, grantlee_statistics_templates; - -class TemplateLayout : public QObject { - Q_OBJECT -public: - TemplateLayout(print_options *PrintOptions, template_options *templateOptions); - ~TemplateLayout(); - QString generate(); - QString generateStatistics(); - static QString readTemplate(QString template_name); - static void writeTemplate(QString template_name, QString grantlee_template); - -private: - Grantlee::Engine *m_engine; - print_options *PrintOptions; - template_options *templateOptions; - -signals: - void progressUpdated(int value); -}; - -class YearInfo { -public: - stats_t *year; - YearInfo(stats_t& year) - :year(&year) - { - - } - YearInfo(); - ~YearInfo(); -}; - -Q_DECLARE_METATYPE(Dive) -Q_DECLARE_METATYPE(template_options) -Q_DECLARE_METATYPE(print_options) -Q_DECLARE_METATYPE(YearInfo) - -GRANTLEE_BEGIN_LOOKUP(Dive) -if (property == "number") - return object.number(); -else if (property == "id") - return object.id(); -else if (property == "date") - return object.date(); -else if (property == "time") - return object.time(); -else if (property == "location") - return object.location(); -else if (property == "duration") - return object.duration(); -else if (property == "depth") - return object.depth(); -else if (property == "divemaster") - return object.divemaster(); -else if (property == "buddy") - return object.buddy(); -else if (property == "airTemp") - return object.airTemp(); -else if (property == "waterTemp") - return object.waterTemp(); -else if (property == "notes") - return object.notes(); -else if (property == "rating") - return object.rating(); -else if (property == "sac") - return object.sac(); -else if (property == "tags") - return object.tags(); -else if (property == "gas") - return object.gas(); -GRANTLEE_END_LOOKUP - -GRANTLEE_BEGIN_LOOKUP(template_options) -if (property == "font") { - switch (object.font_index) { - case 0: - return "Arial, Helvetica, sans-serif"; - case 1: - return "Impact, Charcoal, sans-serif"; - case 2: - return "Georgia, serif"; - case 3: - return "Courier, monospace"; - case 4: - return "Verdana, Geneva, sans-serif"; - } -} else if (property == "borderwidth") { - return object.border_width; -} else if (property == "font_size") { - return object.font_size / 9.0; -} else if (property == "line_spacing") { - return object.line_spacing; -} else if (property == "color1") { - return object.color_palette.color1.name(); -} else if (property == "color2") { - return object.color_palette.color2.name(); -} else if (property == "color3") { - return object.color_palette.color3.name(); -} else if (property == "color4") { - return object.color_palette.color4.name(); -} else if (property == "color5") { - return object.color_palette.color5.name(); -} else if (property == "color6") { - return object.color_palette.color6.name(); -} -GRANTLEE_END_LOOKUP - -GRANTLEE_BEGIN_LOOKUP(print_options) -if (property == "grayscale") { - if (object.color_selected) { - return ""; - } else { - return "-webkit-filter: grayscale(100%)"; - } -} -GRANTLEE_END_LOOKUP - -GRANTLEE_BEGIN_LOOKUP(YearInfo) -if (property == "year") { - return object.year->period; -} else if (property == "dives") { - return object.year->selection_size; -} else if (property == "min_temp") { - const char *unit; - double temp = get_temp_units(object.year->min_temp, &unit); - return object.year->min_temp == 0 ? "0" : QString::number(temp, 'g', 2) + unit; -} else if (property == "max_temp") { - const char *unit; - double temp = get_temp_units(object.year->max_temp, &unit); - return object.year->max_temp == 0 ? "0" : QString::number(temp, 'g', 2) + unit; -} else if (property == "total_time") { - return get_time_string(object.year->total_time.seconds, 0); -} else if (property == "avg_time") { - return get_minutes(object.year->total_time.seconds / object.year->selection_size); -} else if (property == "shortest_time") { - return get_minutes(object.year->shortest_time.seconds); -} else if (property == "longest_time") { - return get_minutes(object.year->longest_time.seconds); -} else if (property == "avg_depth") { - return get_depth_string(object.year->avg_depth); -} else if (property == "min_depth") { - return get_depth_string(object.year->min_depth); -} else if (property == "max_depth") { - return get_depth_string(object.year->max_depth); -} else if (property == "avg_sac") { - return get_volume_string(object.year->avg_sac); -} else if (property == "min_sac") { - return get_volume_string(object.year->min_sac); -} else if (property == "max_sac") { - return get_volume_string(object.year->max_sac); -} -GRANTLEE_END_LOOKUP - -#endif -- cgit v1.2.3-70-g09d2