aboutsummaryrefslogtreecommitdiffstats
path: root/qt-ui/templateedit.cpp
blob: 8d216d9986784023dc298bbf9a546b5045a4300e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "templateedit.h"
#include "printoptions.h"
#include "printer.h"
#include "ui_templateedit.h"

#include <QMessageBox>

TemplateEdit::TemplateEdit(QWidget *parent, struct print_options *printOptions, struct template_options *templateOptions) :
	QDialog(parent),
	ui(new Ui::TemplateEdit)
{
	ui->setupUi(this);
	this->templateOptions = templateOptions;
	newTemplateOptions = *templateOptions;
	this->printOptions = printOptions;

	// restore the settings and init the UI
	ui->fontSelection->setCurrentIndex(templateOptions->font_index);
	ui->fontsize->setValue(templateOptions->font_size);
	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");
	}

	ui->plainTextEdit->setPlainText(grantlee_template);
	updatePreview();
}

TemplateEdit::~TemplateEdit()
{
	delete ui;
}

void TemplateEdit::updatePreview()
{
	int width = ui->label->width();
	int height = ui->label->height();
	QPixmap map(width * 2, height * 2);
	map.fill(QColor::fromRgb(255, 255, 255));
	Printer printer(&map, printOptions, &newTemplateOptions, Printer::PREVIEW);
	printer.previewOnePage();
	ui->label->setPixmap(map.scaled(width, height, Qt::IgnoreAspectRatio));
}

void TemplateEdit::on_fontsize_valueChanged(int font_size)
{
	newTemplateOptions.font_size = font_size;
	updatePreview();
}

void TemplateEdit::on_linespacing_valueChanged(double line_spacing)
{
	newTemplateOptions.line_spacing = line_spacing;
	updatePreview();
}

void TemplateEdit::on_fontSelection_currentIndexChanged(int index)
{
	newTemplateOptions.font_index = index;
	updatePreview();
}

void TemplateEdit::on_colorpalette_currentIndexChanged(int index)
{
	newTemplateOptions.color_palette_index = index;
	updatePreview();
}

void TemplateEdit::saveSettings()
{
	if ((*templateOptions) != newTemplateOptions || grantlee_template.compare(ui->plainTextEdit->toPlainText())) {
		QMessageBox msgBox;
		msgBox.setText("Do you want to save your changes?");
		msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard);
		msgBox.setDefaultButton(QMessageBox::Discard);
		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;
				TemplateLayout::writeTemplate("custom.html", ui->plainTextEdit->toPlainText());
			}
		}
	}
}

void TemplateEdit::on_buttonBox_clicked(QAbstractButton *button)
{
	QDialogButtonBox::StandardButton standardButton = ui->buttonBox->standardButton(button);
	switch (standardButton) {
	case QDialogButtonBox::Ok:
		saveSettings();
		break;
	case QDialogButtonBox::Cancel:
		break;
	case QDialogButtonBox::Apply:
		saveSettings();
		updatePreview();
		break;
	}
}