diff --git a/printer.cpp b/printer.cpp index 5b1995501..10942865a 100644 --- a/printer.cpp +++ b/printer.cpp @@ -160,7 +160,12 @@ void Printer::print() divesPerPage = 1; // print each dive in a single page if the attribute is missing or malformed //TODO: show warning } - int Pages = ceil(getTotalWork(printOptions) / (float)divesPerPage); + int Pages; + if (divesPerPage == 0) { + Pages = ceil(webView->page()->mainFrame()->contentsSize().height() / (float)pageSize.height()); + } else { + Pages = ceil(getTotalWork(printOptions) / (float)divesPerPage); + } render(Pages); } diff --git a/printing_templates/custom.html b/printing_templates/Custom.html similarity index 100% rename from printing_templates/custom.html rename to printing_templates/Custom.html diff --git a/printing_templates/Flowlayout.html b/printing_templates/Flowlayout.html new file mode 100644 index 000000000..a3f7951d9 --- /dev/null +++ b/printing_templates/Flowlayout.html @@ -0,0 +1,204 @@ + + + + + +
+{% block main_rows %} + {% for dive in dives %} +
+
+
+
+ + + + + + + + + + + + + + + + + + + + + +
+

Dive No.

+
+

{{ dive.number }}

+
+

Date

+

{{ dive.date }}

+
+

Location

+
+

{{ dive.location }}

+
+

Max depth

+
+

{{ dive.depth }}

+
+

Duration

+
+

{{ dive.duration }}

+
+ + + + + + + + + + + + + + + + + + + + + + +
+

Time.

+
+

{{ dive.time }}

+
+

Air Temp.

+

{{ dive.airTemp }}

+
+

Water Temp.

+
+

{{ dive.waterTemp }}

+
+

Buddy

+
+

{{ dive.buddy }}

+
+

Dive Master

+
+

{{ dive.divemaster }}

+
+ + + + + + + + + +
+

Notes

+
+
+

{{ dive.notes }}

+
+
+
+
+
+
+ {% endfor %} +{% endblock %} +
+ + diff --git a/printing_templates/one_dive.html b/printing_templates/One Dive.html similarity index 100% rename from printing_templates/one_dive.html rename to printing_templates/One Dive.html diff --git a/printing_templates/Table.html b/printing_templates/Table.html new file mode 100644 index 000000000..c97267405 --- /dev/null +++ b/printing_templates/Table.html @@ -0,0 +1,83 @@ + + + + + +
+ + + + + + + + + + +{% block main_rows %} + {% for dive in dives %} + + + + + + + + + + {% endfor %} +{% endblock %} +
Dive #DateTimeDepthDurationMasterLocation
{{ dive.number }}{{ dive.date }}{{ dive.time }}{{ dive.depth }}{{ dive.duration }}{{ dive.divemaster }}{{ dive.location }}
+
+ + diff --git a/printing_templates/two_dives.html b/printing_templates/Two Dives.html similarity index 100% rename from printing_templates/two_dives.html rename to printing_templates/Two Dives.html diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index ed7c62c62..b6459d3c2 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -199,6 +199,9 @@ MainWindow::MainWindow() : QMainWindow(), ReverseGeoLookupThread *geoLookup = ReverseGeoLookupThread::instance(); connect(geoLookup, SIGNAL(started()),information(), SLOT(disableGeoLookupEdition())); connect(geoLookup, SIGNAL(finished()), information(), SLOT(enableGeoLookupEdition())); +#ifndef NO_PRINTING + find_all_templates(); +#endif } MainWindow::~MainWindow() diff --git a/qt-ui/printdialog.cpp b/qt-ui/printdialog.cpp index 31ec354c8..25c102201 100644 --- a/qt-ui/printdialog.cpp +++ b/qt-ui/printdialog.cpp @@ -12,7 +12,7 @@ #define SETTINGS_GROUP "PrintDialog" -template_options::color_palette_struct almond_colors, custom_colors; +template_options::color_palette_struct almond_colors, blueshades_colors, custom_colors; PrintDialog::PrintDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f) { @@ -22,6 +22,11 @@ PrintDialog::PrintDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f almond_colors.color3 = QColor::fromRgb(136, 160, 150); almond_colors.color4 = QColor::fromRgb(187, 171, 139); almond_colors.color5 = QColor::fromRgb(239, 130, 117); + blueshades_colors.color1 = QColor::fromRgb(182, 192, 206); + blueshades_colors.color2 = QColor::fromRgb(142, 152, 166); + blueshades_colors.color3 = QColor::fromRgb(31, 49, 75); + blueshades_colors.color4 = QColor::fromRgb(21, 45, 84); + blueshades_colors.color5 = QColor::fromRgb(5, 25, 56); // check if the options were previously stored in the settings; if not use some defaults. QSettings s; @@ -30,11 +35,11 @@ PrintDialog::PrintDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f printOptions.print_selected = true; printOptions.color_selected = true; printOptions.landscape = false; - printOptions.p_template = print_options::ONE_DIVE; + printOptions.p_template = "one_dive.html"; printOptions.type = print_options::DIVELIST; templateOptions.font_index = 0; templateOptions.font_size = 9; - templateOptions.color_palette_index = 0; + templateOptions.color_palette_index = ALMOND; templateOptions.line_spacing = 1; custom_colors = almond_colors; } else { @@ -43,7 +48,7 @@ PrintDialog::PrintDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f printOptions.print_selected = s.value("print_selected").toBool(); printOptions.color_selected = s.value("color_selected").toBool(); printOptions.landscape = s.value("landscape").toBool(); - printOptions.p_template = (print_options::print_template)s.value("template_selected").toInt(); + printOptions.p_template = s.value("template_selected").toString(); qprinter.setOrientation((QPrinter::Orientation)printOptions.landscape); templateOptions.font_index = s.value("font").toInt(); templateOptions.font_size = s.value("font_size").toDouble(); @@ -56,11 +61,22 @@ PrintDialog::PrintDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f custom_colors.color5 = QColor(s.value("custom_color_5").toString()); } + // handle cases from old QSettings group + if (templateOptions.font_size < 9) { + templateOptions.font_size = 9; + } + if (templateOptions.line_spacing < 1) { + templateOptions.line_spacing = 1; + } + switch (templateOptions.color_palette_index) { - case 0: // almond + case ALMOND: // almond templateOptions.color_palette = almond_colors; break; - case 1: // custom + case BLUESHADES: // blueshades + templateOptions.color_palette = blueshades_colors; + break; + case CUSTOM: // custom templateOptions.color_palette = custom_colors; break; } @@ -140,7 +156,7 @@ void PrintDialog::onFinished() void PrintDialog::previewClicked(void) { - if (printOptions.type == print_options::TABLE || printOptions.type == print_options::STATISTICS) { + if (printOptions.type == print_options::STATISTICS) { QMessageBox msgBox; msgBox.setText("This feature is not implemented yet"); msgBox.exec(); @@ -156,7 +172,7 @@ void PrintDialog::previewClicked(void) void PrintDialog::printClicked(void) { - if (printOptions.type == print_options::TABLE || printOptions.type == print_options::STATISTICS) { + if (printOptions.type == print_options::STATISTICS) { QMessageBox msgBox; msgBox.setText("This feature is not implemented yet"); msgBox.exec(); @@ -170,8 +186,6 @@ void PrintDialog::printClicked(void) connect(printer, SIGNAL(progessUpdated(int)), progressBar, SLOT(setValue(int))); printer->print(); break; - case print_options::TABLE: - break; case print_options::STATISTICS: break; } diff --git a/qt-ui/printoptions.cpp b/qt-ui/printoptions.cpp index 0e6a0b320..1da95a94d 100644 --- a/qt-ui/printoptions.cpp +++ b/qt-ui/printoptions.cpp @@ -1,6 +1,10 @@ #include "printoptions.h" #include "templateedit.h" +#include "helpers.h" + #include +#include +#include PrintOptions::PrintOptions(QWidget *parent, struct print_options *printOpt, struct template_options *templateOpt) { @@ -22,24 +26,26 @@ void PrintOptions::setup() case print_options::DIVELIST: ui.radioDiveListPrint->setChecked(true); break; - case print_options::TABLE: - ui.radioTablePrint->setChecked(true); - break; case print_options::STATISTICS: ui.radioStatisticsPrint->setChecked(true); break; } - switch (printOptions->p_template) { - case print_options::ONE_DIVE: - ui.printTemplate->setCurrentIndex(0); - break; - case print_options::TWO_DIVE: - ui.printTemplate->setCurrentIndex(1); - break; - case print_options::CUSTOM: - ui.printTemplate->setCurrentIndex(2); - break; + + // insert existing templates in the UI and select the current template + qSort(grantlee_templates); + int current_index = 0, index = 0; + for (QList::iterator i = grantlee_templates.begin(); i != grantlee_templates.end(); ++i) { + if ((*i).compare(printOptions->p_template) == 0) { + current_index = index; + break; + } + index++; } + ui.printTemplate->clear(); + for (QList::iterator i = grantlee_templates.begin(); i != grantlee_templates.end(); ++i) { + ui.printTemplate->addItem((*i).split('.')[0], QVariant::fromValue(*i)); + } + ui.printTemplate->setCurrentIndex(current_index); // general print option checkboxes if (printOptions->color_selected) @@ -65,13 +71,6 @@ void PrintOptions::on_radioDiveListPrint_clicked(bool check) } } -void PrintOptions::on_radioTablePrint_clicked(bool check) -{ - if (check) { - printOptions->type = print_options::TABLE; - } -} - void PrintOptions::on_radioStatisticsPrint_clicked(bool check) { if (check) { @@ -93,17 +92,7 @@ void PrintOptions::printSelectedClicked(bool check) void PrintOptions::on_printTemplate_currentIndexChanged(int index) { - switch(index){ - case 0: - printOptions->p_template = print_options::ONE_DIVE; - break; - case 1: - printOptions->p_template = print_options::TWO_DIVE; - break; - case 2: - printOptions->p_template = print_options::CUSTOM; - break; - } + printOptions->p_template = ui.printTemplate->itemData(index).toString(); } void PrintOptions::on_editButton_clicked() @@ -112,3 +101,46 @@ void PrintOptions::on_editButton_clicked() te.exec(); setup(); } + +void PrintOptions::on_importButton_clicked() +{ + QString filename = QFileDialog::getOpenFileName(this, tr("Import Template file"), "", + tr("HTML files (*.html)")); + if (filename.isEmpty()) + return; + QFileInfo fileInfo(filename); + QFile::copy(filename, getSubsurfaceDataPath("printing_templates") + QDir::separator() + fileInfo.fileName()); + printOptions->p_template = fileInfo.fileName(); + find_all_templates(); + setup(); +} + +void PrintOptions::on_exportButton_clicked() +{ + QString filename = QFileDialog::getSaveFileName(this, tr("Export Template files as"), "", + tr("HTML files (*.html)")); + if (filename.isEmpty()) + return; + QFile::copy(getSubsurfaceDataPath("printing_templates") + QDir::separator() + getSelectedTemplate(), filename); +} + +void PrintOptions::on_deleteButton_clicked() +{ + QString templateName = getSelectedTemplate(); + QMessageBox msgBox; + msgBox.setText("This action cannot be undone!"); + msgBox.setInformativeText("Delete '" + templateName + "' template?"); + msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); + msgBox.setDefaultButton(QMessageBox::Cancel); + if (msgBox.exec() == QMessageBox::Ok) { + QFile f(getSubsurfaceDataPath("printing_templates") + QDir::separator() + templateName); + f.remove(); + find_all_templates(); + setup(); + } +} + +QString PrintOptions::getSelectedTemplate() +{ + return ui.printTemplate->currentData().toString(); +} diff --git a/qt-ui/printoptions.h b/qt-ui/printoptions.h index 3a55c6994..2e2e2a9f4 100644 --- a/qt-ui/printoptions.h +++ b/qt-ui/printoptions.h @@ -8,14 +8,9 @@ struct print_options { enum print_type { DIVELIST, - TABLE, STATISTICS } type; - enum print_template { - ONE_DIVE, - TWO_DIVE, - CUSTOM - } p_template; + QString p_template; bool print_selected; bool color_selected; bool landscape; @@ -49,7 +44,13 @@ struct template_options { } }; -extern template_options::color_palette_struct almond_colors, custom_colors; +extern template_options::color_palette_struct almond_colors, blueshades_colors, custom_colors; + +enum color_palette { + ALMOND, + BLUESHADES, + CUSTOM +}; // should be based on a custom QPrintDialog class class PrintOptions : public QWidget { @@ -58,6 +59,7 @@ class PrintOptions : public QWidget { public: explicit PrintOptions(QWidget *parent, struct print_options *printOpt, struct template_options *templateOpt); void setup(); + QString getSelectedTemplate(); private: Ui::PrintOptions ui; @@ -70,10 +72,12 @@ slots: void printInColorClicked(bool check); void printSelectedClicked(bool check); void on_radioStatisticsPrint_clicked(bool check); - void on_radioTablePrint_clicked(bool check); void on_radioDiveListPrint_clicked(bool check); void on_printTemplate_currentIndexChanged(int index); void on_editButton_clicked(); + void on_importButton_clicked(); + void on_exportButton_clicked(); + void on_deleteButton_clicked(); }; #endif // PRINTOPTIONS_H diff --git a/qt-ui/printoptions.ui b/qt-ui/printoptions.ui index 7e66d4cb0..1c2523d39 100644 --- a/qt-ui/printoptions.ui +++ b/qt-ui/printoptions.ui @@ -46,19 +46,6 @@ - - - - 0 - 0 - - - - &Table print - - - - @@ -71,19 +58,6 @@ - - - - Qt::Vertical - - - - 20 - 40 - - - - @@ -129,36 +103,55 @@ - + - - One dive per page - + - - Two dives per page - + + + + + + 0 + 0 + + + + + 16777215 + 16777215 + + + + Edit + + + + + + + Delete + + + + + + + Export + + + + + + + Import + + + + - - - Custom template - - - - - - - - - 106 - 26 - - - - Edit - - + @@ -167,7 +160,6 @@ radioDiveListPrint - radioStatisticsPrint printSelected printInColor diff --git a/qt-ui/templateedit.cpp b/qt-ui/templateedit.cpp index 6a6328f64..6df11ea79 100644 --- a/qt-ui/templateedit.cpp +++ b/qt-ui/templateedit.cpp @@ -21,13 +21,7 @@ TemplateEdit::TemplateEdit(QWidget *parent, struct print_options *printOptions, ui->colorpalette->setCurrentIndex(templateOptions->color_palette_index); ui->linespacing->setValue(templateOptions->line_spacing); - if (printOptions->p_template == print_options::ONE_DIVE) { - grantlee_template = TemplateLayout::readTemplate("one_dive.html"); - } else if (printOptions->p_template == print_options::TWO_DIVE) { - grantlee_template = TemplateLayout::readTemplate("two_dives.html"); - } else if (printOptions->p_template == print_options::CUSTOM) { - grantlee_template = TemplateLayout::readTemplate("custom.html"); - } + grantlee_template = TemplateLayout::readTemplate(printOptions->p_template); // gui btnGroup = new QButtonGroup; @@ -98,10 +92,13 @@ void TemplateEdit::on_colorpalette_currentIndexChanged(int index) { newTemplateOptions.color_palette_index = index; switch (newTemplateOptions.color_palette_index) { - case 0: // almond + case ALMOND: // almond newTemplateOptions.color_palette = almond_colors; break; - case 1: // custom + case BLUESHADES: // blueshades + newTemplateOptions.color_palette = blueshades_colors; + break; + case CUSTOM: // custom newTemplateOptions.color_palette = custom_colors; break; } @@ -118,10 +115,10 @@ void TemplateEdit::saveSettings() if (msgBox.exec() == QMessageBox::Save) { memcpy(templateOptions, &newTemplateOptions, sizeof(struct template_options)); if (grantlee_template.compare(ui->plainTextEdit->toPlainText())) { - printOptions->p_template = print_options::CUSTOM; + printOptions->p_template = "custom.html"; TemplateLayout::writeTemplate("custom.html", ui->plainTextEdit->toPlainText()); } - if (templateOptions->color_palette_index == 1) { + if (templateOptions->color_palette_index == 2) { custom_colors = templateOptions->color_palette; } } @@ -150,10 +147,14 @@ void TemplateEdit::colorSelect(QAbstractButton *button) { // reset custom colors palette switch (newTemplateOptions.color_palette_index) { - case 0: // almond + case ALMOND: // almond newTemplateOptions.color_palette = almond_colors; custom_colors = newTemplateOptions.color_palette; break; + case BLUESHADES: // blueshades + newTemplateOptions.color_palette = blueshades_colors; + custom_colors = newTemplateOptions.color_palette; + break; } //change selected color @@ -180,6 +181,6 @@ void TemplateEdit::colorSelect(QAbstractButton *button) newTemplateOptions.color_palette.color5 = color; break; } - newTemplateOptions.color_palette_index = 1; + newTemplateOptions.color_palette_index = CUSTOM; updatePreview(); } diff --git a/qt-ui/templateedit.ui b/qt-ui/templateedit.ui index 4f9c9c2ed..1442954cf 100644 --- a/qt-ui/templateedit.ui +++ b/qt-ui/templateedit.ui @@ -6,436 +6,493 @@ 0 0 - 774 + 770 433 Edit Template - - - - 400 - 380 - 341 - 32 - - - - Qt::Horizontal - - - QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - 300 - 30 - 441 - 331 - - - - 2 - - - - Style - - - - - 10 - 20 - 401 - 171 - + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Preview + + + + + + + + 0 + 0 + + + + + 180 + 240 + + + + + 180 + 254 + + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + false + + + 0 + + + + Style + + + + + + + + + + Font + + + + + + + + Arial + + + + + Impact + + + + + Georgia + + + + + Courier + + + + + Verdana + + + + + + + + + + + + Font size + + + + + + + 9 + + + 18 + + + + + + + + + + + Color pallet + + + + + + + + Almond + + + + + Shades of blue + + + + + Custom + + + + + + + + + + + + Line spacing + + + + + + + 1.000000000000000 + + + 3.000000000000000 + + + 0.250000000000000 + + + 1.250000000000000 + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + Template + + + + + + Qt::ScrollBarAsNeeded + + + QPlainTextEdit::NoWrap + + + + + + + + + 16777215 + 16777215 + + + + Colors + + + + + + + + + + + 0 + 0 + + + + Background + + + + + + + + 0 + 0 + + + + color1 + + + Qt::AlignCenter + + + + + + + Edit + + + + + + + + + + + + 0 + 0 + + + + Table cells + + + + + + + + 0 + 0 + + + + color2 + + + Qt::AlignCenter + + + + + + + Edit + + + + + + + + + + + + 0 + 0 + + + + Text 1 + + + + + + + + 0 + 0 + + + + color3 + + + Qt::AlignCenter + + + + + + + Edit + + + + + + + + + + + + 0 + 0 + + + + Text 2 + + + + + + + + 0 + 0 + + + + color4 + + + Qt::AlignCenter + + + + + + + Edit + + + + + + + + + + + + 0 + 0 + + + + Borders + + + + + + + + 0 + 0 + + + + color5 + + + Qt::AlignCenter + + + + + + + Edit + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + + + + + Qt::Horizontal - - - - - - - Font - - - - - - - - Arial - - - - - Impact - - - - - Georgia - - - - - Courier - - - - - Verdana - - - - - - - - - - - - Font size - - - - - - - 9 - - - 18 - - - - - - - - - - - Color pallet - - - - - - - - Almond - - - - - Custom - - - - - - - - - - - - Line spacing - - - - - - - 1.000000000000000 - - - 3.000000000000000 - - - 0.250000000000000 - - - 1.250000000000000 - - - - - - - - - - - Template - - - - - 0 - 0 - 441 - 301 - - - - Qt::ScrollBarAsNeeded - - - QPlainTextEdit::NoWrap + + QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - Colors - - - - - 10 - 30 - 411 - 171 - - - - - - - - - - 0 - 0 - - - - Background - - - - - - - - 0 - 0 - - - - color1 - - - Qt::AlignCenter - - - - - - - Edit - - - - - - - - - - - - 0 - 0 - - - - Table cells - - - - - - - - 0 - 0 - - - - color2 - - - Qt::AlignCenter - - - - - - - Edit - - - - - - - - - - - - 0 - 0 - - - - Text 1 - - - - - - - - 0 - 0 - - - - color3 - - - Qt::AlignCenter - - - - - - - Edit - - - - - - - - - - - - 0 - 0 - - - - Text 2 - - - - - - - - 0 - 0 - - - - color4 - - - Qt::AlignCenter - - - - - - - Edit - - - - - - - - - - - - 0 - 0 - - - - Borders - - - - - - - - 0 - 0 - - - - color5 - - - Qt::AlignCenter - - - - - - - Edit - - - - - - - - - - - - - 30 - 30 - 59 - 14 - - - - Preview - - - - - - 50 - 70 - 211 - 291 - - - - - - + + diff --git a/templatelayout.cpp b/templatelayout.cpp index fec57cb2a..30919891a 100644 --- a/templatelayout.cpp +++ b/templatelayout.cpp @@ -4,6 +4,8 @@ #include "helpers.h" #include "display.h" +QList grantlee_templates; + int getTotalWork(print_options *printOptions) { if (printOptions->print_selected) { @@ -19,6 +21,19 @@ int getTotalWork(print_options *printOptions) return dives; } +void find_all_templates() +{ + grantlee_templates.clear(); + QDir dir(getSubsurfaceDataPath("printing_templates")); + 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()); + } + } +} + TemplateLayout::TemplateLayout(print_options *PrintOptions, template_options *templateOptions) : m_engine(NULL) { @@ -35,7 +50,6 @@ QString TemplateLayout::generate() { int progress = 0; int totalWork = getTotalWork(PrintOptions); - QString templateName; QString htmlContent; m_engine = new Grantlee::Engine(this); @@ -69,14 +83,7 @@ QString TemplateLayout::generate() Grantlee::Context c(mapping); - if (PrintOptions->p_template == print_options::ONE_DIVE) { - templateName = "one_dive.html"; - } else if (PrintOptions->p_template == print_options::TWO_DIVE) { - templateName = "two_dives.html"; - } else if (PrintOptions->p_template == print_options::CUSTOM) { - templateName = "custom.html"; - } - Grantlee::Template t = m_engine->loadByName(templateName); + Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template); if (!t || t->error()) { qDebug() << "Can't load template"; return htmlContent; diff --git a/templatelayout.h b/templatelayout.h index e37e9e868..5f4678923 100644 --- a/templatelayout.h +++ b/templatelayout.h @@ -6,6 +6,9 @@ #include "printoptions.h" int getTotalWork(print_options *printOptions); +void find_all_templates(); + +extern QList grantlee_templates; class TemplateLayout : public QObject { Q_OBJECT