summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/printoptions.cpp
blob: 91099b92896a358570fc3a456f6ec5cf7165e6c5 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// SPDX-License-Identifier: GPL-2.0
#include "printoptions.h"
#include "templateedit.h"
#include "core/helpers.h"

#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>

PrintOptions::PrintOptions(QWidget *parent, struct print_options *printOpt, struct template_options *templateOpt)
{
	hasSetupSlots = false;
	ui.setupUi(this);
	if (parent)
		setParent(parent);
	if (!printOpt || !templateOpt)
		return;
	templateOptions = templateOpt;
	printOptions = printOpt;
	setup();
}

void PrintOptions::setup()
{
	// print type radio buttons
	switch (printOptions->type) {
	case print_options::DIVELIST:
		ui.radioDiveListPrint->setChecked(true);
		break;
	case print_options::STATISTICS:
		ui.radioStatisticsPrint->setChecked(true);
		break;
	}

	setupTemplates();

	// general print option checkboxes
	ui.printInColor->setChecked(printOptions->color_selected);
	ui.printSelected->setChecked(printOptions->print_selected);

	// connect slots only once
	if (hasSetupSlots)
		return;

	connect(ui.printInColor, SIGNAL(clicked(bool)), this, SLOT(printInColorClicked(bool)));
	connect(ui.printSelected, SIGNAL(clicked(bool)), this, SLOT(printSelectedClicked(bool)));

	hasSetupSlots = true;
}

void PrintOptions::setupTemplates()
{
	QStringList currList = printOptions->type == print_options::DIVELIST ?
		grantlee_templates : grantlee_statistics_templates;

	// temp. store the template from options, as addItem() updates it via:
	// on_printTemplate_currentIndexChanged()
	QString storedTemplate = printOptions->p_template;
	qSort(currList);
	int current_index = 0;
	ui.printTemplate->clear();
	Q_FOREACH(const QString& theme, currList) {
		 // find the stored template in the list
		if (theme == storedTemplate || theme == lastImportExportTemplate)
			current_index = currList.indexOf(theme);
		ui.printTemplate->addItem(theme.split('.')[0], theme);
	}
	ui.printTemplate->setCurrentIndex(current_index);
	lastImportExportTemplate = "";
}

// print type radio buttons
void PrintOptions::on_radioDiveListPrint_toggled(bool check)
{
	if (check) {
		printOptions->type = print_options::DIVELIST;

		// print options
		ui.printSelected->setEnabled(true);

		// print template
		ui.deleteButton->setEnabled(true);
		ui.exportButton->setEnabled(true);
		ui.importButton->setEnabled(true);

		setupTemplates();
	}
}

void PrintOptions::on_radioStatisticsPrint_toggled(bool check)
{
	if (check) {
		printOptions->type = print_options::STATISTICS;

		// print options
		ui.printSelected->setEnabled(false);

		// print template
		ui.deleteButton->setEnabled(false);
		ui.exportButton->setEnabled(false);
		ui.importButton->setEnabled(false);

		setupTemplates();
	}
}

// general print option checkboxes
void PrintOptions::printInColorClicked(bool check)
{
	printOptions->color_selected = check;
}

void PrintOptions::printSelectedClicked(bool check)
{
	printOptions->print_selected = check;
}


void PrintOptions::on_printTemplate_currentIndexChanged(int index)
{
	printOptions->p_template = ui.printTemplate->itemData(index).toString();
}

void PrintOptions::on_editButton_clicked()
{
	QString templateName = getSelectedTemplate();
	QString prefix = (printOptions->type == print_options::STATISTICS) ? "statistics/" : "";
	QFile f(getPrintingTemplatePathUser() + QDir::separator() + prefix + templateName);
	if (!f.open(QFile::ReadWrite | QFile::Text)) {
		QMessageBox msgBox(this);
		msgBox.setWindowTitle(tr("Read-only template!"));
		msgBox.setText(tr("The template '%1' is read-only and cannot be edited.\n"
			"Please export this template to a different file.").arg(templateName));
		msgBox.setStandardButtons(QMessageBox::Ok);
		msgBox.exec();
		return;
	} else {
		f.close();
	}
	TemplateEdit te(this, printOptions, templateOptions);
	te.exec();
	setup();
}

void PrintOptions::on_importButton_clicked()
{
	QString pathUser = getPrintingTemplatePathUser();
	QString filename = QFileDialog::getOpenFileName(this, tr("Import template file"), pathUser,
							tr("HTML files") + " (*.html)");
	if (filename.isEmpty())
		return;
	QFileInfo fileInfo(filename);

	const QString dest = pathUser + QDir::separator() + fileInfo.fileName();
	QFile f(dest);
	if (!f.open(QFile::ReadWrite | QFile::Text)) {
		QMessageBox msgBox(this);
		msgBox.setWindowTitle(tr("Read-only template!"));
		msgBox.setText(tr("The destination template '%1' is read-only and cannot be overwritten.").arg(fileInfo.fileName()));
		msgBox.setStandardButtons(QMessageBox::Ok);
		msgBox.exec();
		return;
	} else {
		f.close();
		if (filename != dest)
			f.remove();
	}

	QFile::copy(filename, dest);
	printOptions->p_template = fileInfo.fileName();
	lastImportExportTemplate = fileInfo.fileName();
	find_all_templates();
	setup();
}

void PrintOptions::on_exportButton_clicked()
{
	QString pathUser = getPrintingTemplatePathUser();
	QString filename = QFileDialog::getSaveFileName(this, tr("Export template files as"), pathUser,
							tr("HTML files") + " (*.html)");
	if (filename.isEmpty())
		return;
	const QString ext(".html");
	if (filename.endsWith(".htm", Qt::CaseInsensitive))
		filename += "l";
	else if (!filename.endsWith(ext, Qt::CaseInsensitive))
		filename += ext;
	QFileInfo fileInfo(filename);
	const QString dest = pathUser + QDir::separator() + getSelectedTemplate();

	QFile f(filename);
	if (!f.open(QFile::ReadWrite | QFile::Text)) {
		QMessageBox msgBox(this);
		msgBox.setWindowTitle(tr("Read-only template!"));
		msgBox.setText(tr("The destination template '%1' is read-only and cannot be overwritten.").arg(fileInfo.fileName()));
		msgBox.setStandardButtons(QMessageBox::Ok);
		msgBox.exec();
		return;
	} else {
		f.close();
		if (dest != filename)
			f.remove();
	}

	QFile::copy(dest, filename);
	if (!f.open(QFile::ReadWrite | QFile::Text))
		f.setPermissions(QFileDevice::ReadUser | QFileDevice::ReadOwner | QFileDevice::WriteUser | QFileDevice::WriteOwner);
	else
		f.close();
	lastImportExportTemplate = fileInfo.fileName();
	find_all_templates();
	setup();
}

void PrintOptions::on_deleteButton_clicked()
{
	QString templateName = getSelectedTemplate();
	QMessageBox msgBox(this);
	msgBox.setWindowTitle(tr("This action cannot be undone!"));
	msgBox.setText(tr("Delete template '%1'?").arg(templateName));
	msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
	msgBox.setDefaultButton(QMessageBox::Cancel);
	if (msgBox.exec() == QMessageBox::Ok) {
		QFile f(getPrintingTemplatePathUser() + QDir::separator() + templateName);
		if (!f.open(QFile::ReadWrite | QFile::Text)) {
			msgBox.setWindowTitle(tr("Read-only template!"));
			msgBox.setText(tr("The template '%1' is read-only and cannot be deleted.").arg(templateName));
			msgBox.setStandardButtons(QMessageBox::Ok);
			msgBox.exec();
			return;
		} else {
			f.close();
		}
		f.remove();
		find_all_templates();
		setup();
	}
}

QString PrintOptions::getSelectedTemplate()
{
	return ui.printTemplate->currentData().toString();
}