[qet] [1140] Title block template editor: added an "Integrate logo" button.

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 1140
Author:   xavier
Date:     2010-12-28 19:57:24 +0100 (Tue, 28 Dec 2010)
Log Message:
-----------
Title block template editor: added an "Integrate logo" button.

Modified Paths:
--------------
    branches/0.3/sources/templateeditor.cpp
    branches/0.3/sources/templateeditor.h

Modified: branches/0.3/sources/templateeditor.cpp
===================================================================
--- branches/0.3/sources/templateeditor.cpp	2010-12-28 08:21:45 UTC (rev 1139)
+++ branches/0.3/sources/templateeditor.cpp	2010-12-28 18:57:24 UTC (rev 1140)
@@ -121,6 +121,79 @@
 }
 
 /**
+	Allows the user to easily integrate a logo in to the currently edited title block
+	template.
+*/
+void TemplateEditor::integrateLogo() {
+	// we need a filepath
+	QString filepath = QFileDialog::getOpenFileName(
+		this,
+		tr("S\351lectionnez un fichier image"),
+		QString(),
+		tr("Images vectorielles (*.svg);;Images bitmap (*.png *.jpg *.jpeg *.gif *.bmp *.xpm);;Tous les fichiers (*)")
+	);
+	if (filepath.isNull()) return;
+	
+	// that filepath needs to point to a valid, readable file
+	QFileInfo filepath_info(filepath);
+	if (!filepath_info.exists() || !filepath_info.isReadable()) {
+		QMessageBox::critical(this, tr("Erreur"), tr("Impossible d'ouvrir le fichier sp\351cifi\351"));
+		return;
+	}
+	QString filename = filepath_info.fileName();
+	
+	/// TODO identify whether the given file is a bitmap or vector graphics and integrate it accordingly
+	// now, we need the XML document of the currently edited template
+	QDomDocument xml_template;
+	if (!xml_template.setContent(getXmlString())) {
+		QMessageBox::critical(this, tr("Erreur"), tr("Le code XML du mod\350le ne semble pas \320tre valide."));
+		return;
+	}
+	
+	// we need a <logos> section
+	QDomElement logos_section = xml_template.documentElement().firstChildElement("logos");
+	QDomElement logo_xml_elmt;
+	if (logos_section.isNull()) {
+		logos_section = xml_template.createElement("logos");
+		xml_template.documentElement().appendChild(logos_section);
+	} else {
+		// is there a logo of the same name already?
+		QString tag = "logo";
+		for (QDomElement e = logos_section.firstChildElement(tag) ; !e.isNull() ; e = e.nextSiblingElement(tag)) {
+			if (e.attribute("name") == filename) {
+				logo_xml_elmt = e;
+				break;
+			}
+		}
+	}
+	
+	// we read the provided logo
+	QFile logo_file(filepath);
+	logo_file.open(QIODevice::ReadOnly);
+	QString base64_string = QString(logo_file.readAll().toBase64());
+	
+	// we insert it into our XML document
+	QDomText t = xml_template.createTextNode(base64_string);
+	
+	if (!logo_xml_elmt.isNull()) {
+		logo_xml_elmt.setAttribute("storage", "base64");
+		QDomNodeList children = logo_xml_elmt.childNodes();
+		for (int i = 0 ; i < children .count() ; ++ i) logo_xml_elmt.removeChild(children.at(i));
+		logo_xml_elmt.appendChild(t);
+	} else {
+		QDomElement new_logo = xml_template.createElement("logo");
+		new_logo.appendChild(t);
+		new_logo.setAttribute("storage", "base64");
+		new_logo.setAttribute("type", filepath_info.suffix());
+		new_logo.setAttribute("name", filename);
+		logos_section.appendChild(new_logo);
+	}
+	
+	// we put back the XML description in the text area
+	setXmlString(xml_template);
+}
+
+/**
 	Builds the user interface.
 */
 void TemplateEditor::build() {
@@ -135,15 +208,18 @@
 	template_xml_edit_ -> setFontFamily("monospace");
 	template_xml_edit_ -> setWordWrapMode(QTextOption::NoWrap);
 	
+	integrate_logo_ = new QPushButton(tr("Int\351grer un logo"));
 	validate_button_ = new QPushButton(tr("V\351rifier le mod\350le"));
 	save_button_ = new QPushButton(tr("Enregistrer et appliquer"));
 	quit_button_ = new QPushButton(tr("Quitter"));
 	
+	connect(integrate_logo_,  SIGNAL(released()), this, SLOT(integrateLogo()));
 	connect(validate_button_, SIGNAL(released()), this, SLOT(validate()));
 	connect(save_button_,     SIGNAL(released()), this, SLOT(save()));
 	connect(quit_button_,     SIGNAL(released()), this, SLOT(quit()));
 	
 	QHBoxLayout *h_layout0 = new QHBoxLayout();
+	h_layout0 -> addWidget(integrate_logo_);
 	h_layout0 -> addWidget(validate_button_);
 	h_layout0 -> addWidget(save_button_);
 	h_layout0 -> addWidget(quit_button_);
@@ -190,3 +266,14 @@
 	xml_str = xml_str.arg(Qt::escape(template_name_edit_ -> text())).arg(template_xml_edit_ -> toPlainText());
 	return(xml_str);
 }
+
+/**
+	Displays the given title block template XML code
+	@param xml_doc The XML description of a title block template
+*/
+void TemplateEditor::setXmlString(const QDomDocument &xml_doc) {
+	QString xml_str = xml_doc.toString(4);
+	xml_str.replace(QRegExp("^<titleblocktemplate[^>]*>"), "");
+	xml_str.replace(QRegExp("</titleblocktemplate>"), "");
+	template_xml_edit_ -> setPlainText("    " + xml_str.trimmed());
+}

Modified: branches/0.3/sources/templateeditor.h
===================================================================
--- branches/0.3/sources/templateeditor.h	2010-12-28 08:21:45 UTC (rev 1139)
+++ branches/0.3/sources/templateeditor.h	2010-12-28 18:57:24 UTC (rev 1140)
@@ -18,6 +18,7 @@
 #ifndef TEMPLATE_EDITOR_H
 #define TEMPLATE_EDITOR_H
 #include <QtGui>
+#include <QtXml>
 class QETProject;
 /**
 	This class allows the user to edit a title block template.
@@ -41,11 +42,13 @@
 	void validate();
 	void save();
 	void quit();
+	void integrateLogo();
 	
 	private:
 	void build();
 	void updateProjectLabel();
 	QString getXmlString() const;
+	void setXmlString(const QDomDocument &);
 	
 	// attributes
 	private:
@@ -55,6 +58,7 @@
 	QLabel *static_xml_3_;
 	QLineEdit *template_name_edit_;
 	QTextEdit *template_xml_edit_;
+	QPushButton *integrate_logo_;
 	QPushButton *validate_button_;
 	QPushButton *save_button_;
 	QPushButton *quit_button_;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/