| 
									
										
										
										
											2017-04-27 20:26:05 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2013-07-09 15:37:53 +03:00
										 |  |  | #include "printoptions.h"
 | 
					
						
							| 
									
										
										
										
											2015-06-26 04:21:31 +02:00
										 |  |  | #include "templateedit.h"
 | 
					
						
							| 
									
										
										
										
											2018-06-03 22:15:19 +02:00
										 |  |  | #include "core/qthelper.h"
 | 
					
						
							| 
									
										
										
										
											2015-07-28 09:05:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-11 20:13:10 +02:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2015-07-28 09:05:58 +02:00
										 |  |  | #include <QFileDialog>
 | 
					
						
							|  |  |  | #include <QMessageBox>
 | 
					
						
							| 
									
										
										
										
											2013-07-09 15:37:53 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-29 03:16:19 +02:00
										 |  |  | PrintOptions::PrintOptions(QWidget *parent, struct print_options *printOpt, struct template_options *templateOpt) | 
					
						
							| 
									
										
										
										
											2013-07-09 15:37:53 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | 	hasSetupSlots = false; | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	ui.setupUi(this); | 
					
						
							| 
									
										
										
										
											2013-07-09 23:43:21 +03:00
										 |  |  | 	if (parent) | 
					
						
							|  |  |  | 		setParent(parent); | 
					
						
							| 
									
										
										
										
											2015-07-05 06:28:23 +02:00
										 |  |  | 	if (!printOpt || !templateOpt) | 
					
						
							| 
									
										
										
										
											2013-07-09 23:43:21 +03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2015-06-29 03:16:19 +02:00
										 |  |  | 	templateOptions = templateOpt; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:28:23 +02:00
										 |  |  | 	printOptions = printOpt; | 
					
						
							|  |  |  | 	setup(); | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:28:23 +02:00
										 |  |  | void PrintOptions::setup() | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | { | 
					
						
							|  |  |  | 	// print type radio buttons
 | 
					
						
							| 
									
										
										
										
											2013-07-10 15:34:57 +03:00
										 |  |  | 	switch (printOptions->type) { | 
					
						
							| 
									
										
										
										
											2015-05-30 13:10:31 +02:00
										 |  |  | 	case print_options::DIVELIST: | 
					
						
							|  |  |  | 		ui.radioDiveListPrint->setChecked(true); | 
					
						
							| 
									
										
										
										
											2014-07-24 10:56:39 -07:00
										 |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2015-05-30 13:10:31 +02:00
										 |  |  | 	case print_options::STATISTICS: | 
					
						
							|  |  |  | 		ui.radioStatisticsPrint->setChecked(true); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-24 09:53:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-21 18:07:30 +02:00
										 |  |  | 	setupTemplates(); | 
					
						
							| 
									
										
										
										
											2015-05-30 13:10:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | 	// general print option checkboxes
 | 
					
						
							| 
									
										
										
										
											2016-01-08 15:32:06 -02:00
										 |  |  | 	ui.printInColor->setChecked(printOptions->color_selected); | 
					
						
							|  |  |  | 	ui.printSelected->setChecked(printOptions->print_selected); | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// connect slots only once
 | 
					
						
							|  |  |  | 	if (hasSetupSlots) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	connect(ui.printInColor, SIGNAL(clicked(bool)), this, SLOT(printInColorClicked(bool))); | 
					
						
							|  |  |  | 	connect(ui.printSelected, SIGNAL(clicked(bool)), this, SLOT(printSelectedClicked(bool))); | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	hasSetupSlots = true; | 
					
						
							| 
									
										
										
										
											2013-07-09 15:37:53 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-21 18:07:30 +02:00
										 |  |  | void PrintOptions::setupTemplates() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-01-08 15:21:49 -02:00
										 |  |  | 	QStringList currList = printOptions->type == print_options::DIVELIST ? | 
					
						
							|  |  |  | 		grantlee_templates : grantlee_statistics_templates; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 01:34:57 +03:00
										 |  |  | 	// temp. store the template from options, as addItem() updates it via:
 | 
					
						
							|  |  |  | 	// on_printTemplate_currentIndexChanged()
 | 
					
						
							|  |  |  | 	QString storedTemplate = printOptions->p_template; | 
					
						
							| 
									
										
										
										
											2019-04-03 20:21:53 +02:00
										 |  |  | 	currList.sort(); | 
					
						
							| 
									
										
										
										
											2016-01-08 15:21:49 -02:00
										 |  |  | 	int current_index = 0; | 
					
						
							|  |  |  | 	ui.printTemplate->clear(); | 
					
						
							|  |  |  | 	Q_FOREACH(const QString& theme, currList) { | 
					
						
							| 
									
										
										
										
											2017-11-23 16:40:05 +02:00
										 |  |  | 		 // find the stored template in the list
 | 
					
						
							|  |  |  | 		if (theme == storedTemplate || theme == lastImportExportTemplate) | 
					
						
							| 
									
										
										
										
											2016-01-08 15:21:49 -02:00
										 |  |  | 			current_index = currList.indexOf(theme); | 
					
						
							|  |  |  | 		ui.printTemplate->addItem(theme.split('.')[0], theme); | 
					
						
							| 
									
										
										
										
											2015-08-21 18:07:30 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-08 15:21:49 -02:00
										 |  |  | 	ui.printTemplate->setCurrentIndex(current_index); | 
					
						
							| 
									
										
										
										
											2017-11-23 16:40:05 +02:00
										 |  |  | 	lastImportExportTemplate = ""; | 
					
						
							| 
									
										
										
										
											2015-08-21 18:07:30 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | // print type radio buttons
 | 
					
						
							| 
									
										
										
										
											2015-08-12 00:57:19 +02:00
										 |  |  | void PrintOptions::on_radioDiveListPrint_toggled(bool check) | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-30 13:10:31 +02:00
										 |  |  | 	if (check) { | 
					
						
							|  |  |  | 		printOptions->type = print_options::DIVELIST; | 
					
						
							| 
									
										
										
										
											2015-08-12 00:57:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// print options
 | 
					
						
							|  |  |  | 		ui.printSelected->setEnabled(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// print template
 | 
					
						
							|  |  |  | 		ui.deleteButton->setEnabled(true); | 
					
						
							|  |  |  | 		ui.exportButton->setEnabled(true); | 
					
						
							|  |  |  | 		ui.importButton->setEnabled(true); | 
					
						
							| 
									
										
										
										
											2015-08-21 18:07:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		setupTemplates(); | 
					
						
							| 
									
										
										
										
											2015-05-30 13:10:31 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-12 00:57:19 +02:00
										 |  |  | void PrintOptions::on_radioStatisticsPrint_toggled(bool check) | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-30 13:10:31 +02:00
										 |  |  | 	if (check) { | 
					
						
							|  |  |  | 		printOptions->type = print_options::STATISTICS; | 
					
						
							| 
									
										
										
										
											2015-08-12 00:57:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// print options
 | 
					
						
							|  |  |  | 		ui.printSelected->setEnabled(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// print template
 | 
					
						
							|  |  |  | 		ui.deleteButton->setEnabled(false); | 
					
						
							|  |  |  | 		ui.exportButton->setEnabled(false); | 
					
						
							|  |  |  | 		ui.importButton->setEnabled(false); | 
					
						
							| 
									
										
										
										
											2015-08-21 18:07:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		setupTemplates(); | 
					
						
							| 
									
										
										
										
											2015-05-30 13:10:31 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // general print option checkboxes
 | 
					
						
							|  |  |  | void PrintOptions::printInColorClicked(bool check) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-14 00:57:42 +02:00
										 |  |  | 	printOptions->color_selected = check; | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PrintOptions::printSelectedClicked(bool check) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-14 00:57:42 +02:00
										 |  |  | 	printOptions->print_selected = check; | 
					
						
							| 
									
										
										
										
											2013-07-10 10:54:25 +03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-11 20:13:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PrintOptions::on_printTemplate_currentIndexChanged(int index) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-24 09:53:07 +02:00
										 |  |  | 	printOptions->p_template = ui.printTemplate->itemData(index).toString(); | 
					
						
							| 
									
										
										
										
											2015-06-11 20:13:10 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-26 04:21:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void PrintOptions::on_editButton_clicked() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-23 02:54:27 +02:00
										 |  |  | 	QString templateName = getSelectedTemplate(); | 
					
						
							| 
									
										
										
										
											2017-11-23 18:23:36 +02:00
										 |  |  | 	QString prefix = (printOptions->type == print_options::STATISTICS) ? "statistics/" : ""; | 
					
						
							|  |  |  | 	QFile f(getPrintingTemplatePathUser() + QDir::separator() + prefix + templateName); | 
					
						
							| 
									
										
										
										
											2017-11-23 02:54:27 +02:00
										 |  |  | 	if (!f.open(QFile::ReadWrite | QFile::Text)) { | 
					
						
							|  |  |  | 		QMessageBox msgBox(this); | 
					
						
							|  |  |  | 		msgBox.setWindowTitle(tr("Read-only template!")); | 
					
						
							| 
									
										
										
										
											2017-11-28 07:31:08 +01:00
										 |  |  | 		msgBox.setText(tr("The template '%1' is read-only and cannot be edited.\n" | 
					
						
							| 
									
										
										
										
											2017-11-23 02:54:27 +02:00
										 |  |  | 			"Please export this template to a different file.").arg(templateName)); | 
					
						
							|  |  |  | 		msgBox.setStandardButtons(QMessageBox::Ok); | 
					
						
							|  |  |  | 		msgBox.exec(); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		f.close(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-05 07:26:39 +02:00
										 |  |  | 	TemplateEdit te(this, printOptions, templateOptions); | 
					
						
							| 
									
										
										
										
											2015-06-26 04:21:31 +02:00
										 |  |  | 	te.exec(); | 
					
						
							| 
									
										
										
										
											2015-07-05 07:29:46 +02:00
										 |  |  | 	setup(); | 
					
						
							| 
									
										
										
										
											2015-06-26 04:21:31 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-26 14:59:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 09:05:58 +02:00
										 |  |  | void PrintOptions::on_importButton_clicked() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-23 02:24:29 +02:00
										 |  |  | 	QString pathUser = getPrintingTemplatePathUser(); | 
					
						
							|  |  |  | 	QString filename = QFileDialog::getOpenFileName(this, tr("Import template file"), pathUser, | 
					
						
							| 
									
										
										
										
											2017-10-27 14:52:27 +02:00
										 |  |  | 							tr("HTML files") + " (*.html)"); | 
					
						
							| 
									
										
										
										
											2015-07-28 11:26:41 +03:00
										 |  |  | 	if (filename.isEmpty()) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2015-07-28 09:05:58 +02:00
										 |  |  | 	QFileInfo fileInfo(filename); | 
					
						
							| 
									
										
										
										
											2017-11-23 18:03:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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); | 
					
						
							| 
									
										
										
										
											2015-07-28 09:16:17 +02:00
										 |  |  | 	printOptions->p_template = fileInfo.fileName(); | 
					
						
							| 
									
										
										
										
											2017-11-23 16:40:05 +02:00
										 |  |  | 	lastImportExportTemplate = fileInfo.fileName(); | 
					
						
							| 
									
										
										
										
											2015-07-28 09:05:58 +02:00
										 |  |  | 	find_all_templates(); | 
					
						
							|  |  |  | 	setup(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PrintOptions::on_exportButton_clicked() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-23 02:24:29 +02:00
										 |  |  | 	QString pathUser = getPrintingTemplatePathUser(); | 
					
						
							|  |  |  | 	QString filename = QFileDialog::getSaveFileName(this, tr("Export template files as"), pathUser, | 
					
						
							| 
									
										
										
										
											2017-10-27 14:52:27 +02:00
										 |  |  | 							tr("HTML files") + " (*.html)"); | 
					
						
							| 
									
										
										
										
											2015-07-28 11:26:41 +03:00
										 |  |  | 	if (filename.isEmpty()) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2017-11-23 16:42:54 +02:00
										 |  |  | 	const QString ext(".html"); | 
					
						
							|  |  |  | 	if (filename.endsWith(".htm", Qt::CaseInsensitive)) | 
					
						
							|  |  |  | 		filename += "l"; | 
					
						
							|  |  |  | 	else if (!filename.endsWith(ext, Qt::CaseInsensitive)) | 
					
						
							|  |  |  | 		filename += ext; | 
					
						
							| 
									
										
										
										
											2017-11-23 18:03:46 +02:00
										 |  |  | 	QFileInfo fileInfo(filename); | 
					
						
							|  |  |  | 	const QString dest = pathUser + QDir::separator() + getSelectedTemplate(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 02:54:27 +02:00
										 |  |  | 	QFile f(filename); | 
					
						
							| 
									
										
										
										
											2017-11-23 18:03:46 +02:00
										 |  |  | 	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); | 
					
						
							| 
									
										
										
										
											2017-11-23 02:54:27 +02:00
										 |  |  | 	if (!f.open(QFile::ReadWrite | QFile::Text)) | 
					
						
							|  |  |  | 		f.setPermissions(QFileDevice::ReadUser | QFileDevice::ReadOwner | QFileDevice::WriteUser | QFileDevice::WriteOwner); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		f.close(); | 
					
						
							| 
									
										
										
										
											2017-11-23 16:40:05 +02:00
										 |  |  | 	lastImportExportTemplate = fileInfo.fileName(); | 
					
						
							| 
									
										
										
										
											2017-11-23 02:24:29 +02:00
										 |  |  | 	find_all_templates(); | 
					
						
							|  |  |  | 	setup(); | 
					
						
							| 
									
										
										
										
											2015-07-28 09:05:58 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PrintOptions::on_deleteButton_clicked() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QString templateName = getSelectedTemplate(); | 
					
						
							| 
									
										
										
										
											2017-11-23 02:54:27 +02:00
										 |  |  | 	QMessageBox msgBox(this); | 
					
						
							|  |  |  | 	msgBox.setWindowTitle(tr("This action cannot be undone!")); | 
					
						
							|  |  |  | 	msgBox.setText(tr("Delete template '%1'?").arg(templateName)); | 
					
						
							| 
									
										
										
										
											2015-07-28 09:05:58 +02:00
										 |  |  | 	msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); | 
					
						
							|  |  |  | 	msgBox.setDefaultButton(QMessageBox::Cancel); | 
					
						
							|  |  |  | 	if (msgBox.exec() == QMessageBox::Ok) { | 
					
						
							| 
									
										
										
										
											2015-10-19 00:25:14 +03:00
										 |  |  | 		QFile f(getPrintingTemplatePathUser() + QDir::separator() + templateName); | 
					
						
							| 
									
										
										
										
											2017-11-23 18:03:46 +02:00
										 |  |  | 		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(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-07-28 09:05:58 +02:00
										 |  |  | 		f.remove(); | 
					
						
							|  |  |  | 		find_all_templates(); | 
					
						
							|  |  |  | 		setup(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-26 14:59:59 +02:00
										 |  |  | QString PrintOptions::getSelectedTemplate() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return ui.printTemplate->currentData().toString(); | 
					
						
							|  |  |  | } |