[qet] [1468] Added API to get a valid TitleBlockTemplateLocation from a string.

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


Revision: 1468
Author:   xavier
Date:     2012-01-15 20:26:40 +0100 (Sun, 15 Jan 2012)
Log Message:
-----------
Added API to get a valid TitleBlockTemplateLocation from a string.

Modified Paths:
--------------
    branches/0.3/sources/qetapp.cpp
    branches/0.3/sources/qetapp.h
    branches/0.3/sources/titleblock/templatelocation.cpp
    branches/0.3/sources/titleblock/templatelocation.h

Modified: branches/0.3/sources/qetapp.cpp
===================================================================
--- branches/0.3/sources/qetapp.cpp	2012-01-15 18:52:33 UTC (rev 1467)
+++ branches/0.3/sources/qetapp.cpp	2012-01-15 19:26:40 UTC (rev 1468)
@@ -304,7 +304,7 @@
 	if (!common_tbt_collection_) {
 		common_tbt_collection_ = new TitleBlockTemplatesFilesCollection(QETApp::commonTitleBlockTemplatesDir());
 		common_tbt_collection_ -> setTitle(tr("Cartouches QET", "title of the title block templates collection provided by QElectroTech"));
-		common_tbt_collection_ -> setProtocol("commontbt");
+		common_tbt_collection_ -> setProtocol(QETAPP_COMMON_TBT_PROTOCOL);
 	}
 	return(common_tbt_collection_);
 }
@@ -317,7 +317,7 @@
 	if (!custom_tbt_collection_) {
 		custom_tbt_collection_ = new TitleBlockTemplatesFilesCollection(QETApp::customTitleBlockTemplatesDir());
 		custom_tbt_collection_ -> setTitle(tr("Cartouches utilisateur", "title of the user's title block templates collection"));
-		custom_tbt_collection_ -> setProtocol("customtbt");
+		custom_tbt_collection_ -> setProtocol(QETAPP_CUSTOM_TBT_PROTOCOL);
 	}
 	return(custom_tbt_collection_);
 }
@@ -340,6 +340,24 @@
 }
 
 /**
+	@param protocol Protocol string
+	@return the templates collection matching the provided protocol, or 0 if none could be found
+*/
+TitleBlockTemplatesCollection *QETApp::titleBlockTemplatesCollection(const QString &protocol) {
+	if (protocol == QETAPP_COMMON_TBT_PROTOCOL) {
+		return(common_tbt_collection_);
+	} else if (protocol == QETAPP_CUSTOM_TBT_PROTOCOL) {
+		return(custom_tbt_collection_);
+	} else {
+		QETProject *project = QETApp::projectFromString(protocol);
+		if (project) {
+			return(project -> embeddedTitleBlockTemplatesCollection());
+		}
+	}
+	return(0);
+}
+
+/**
 	@return le nom de l'utilisateur courant
 */
 QString QETApp::userName() {
@@ -452,9 +470,9 @@
 		directory = commonElementsDir();
 	} else if (sym_path.startsWith("custom://")) {
 		directory = customElementsDir();
-	} else if (sym_path.startsWith("commontbt://")) {
+	} else if (sym_path.startsWith(QETAPP_COMMON_TBT_PROTOCOL "://")) {
 		directory = commonTitleBlockTemplatesDir();
-	} else if (sym_path.startsWith("customtbt://")) {
+	} else if (sym_path.startsWith(QETAPP_CUSTOM_TBT_PROTOCOL "://")) {
 		directory = customTitleBlockTemplatesDir();
 	} else return(QString());
 	return(directory + QDir::toNativeSeparators(sym_path.right(sym_path.length() - 9)));
@@ -1276,6 +1294,36 @@
 	}
 }
 
+/**
+	@param url The location of a collection item (title block template,
+	element, category, ...).
+	@return the id of the project mentionned in the URL, or -1 if none could be
+	found.
+*/
+int QETApp::projectIdFromString(const QString &url) {
+	QRegExp embedded("^project([0-9]+)\\+embed.*$", Qt::CaseInsensitive);
+	if (embedded.exactMatch(url)) {
+		bool conv_ok = false;
+		int project_id = embedded.capturedTexts().at(1).toInt(&conv_ok);
+		if (conv_ok) {
+			return(project_id);
+		}
+	}
+	return(-1);
+}
+
+/**
+	@param url The location of a collection item (title block template,
+	element, category, ...).
+	@return the project mentionned in the URL, or 0 if none could be
+	found.
+*/
+QETProject *QETApp::projectFromString(const QString &url) {
+	int project_id = projectIdFromString(url);
+	if (project_id == -1) return(0);
+	return(project(project_id));
+}
+
 /// construit le menu de l'icone dans le systray
 void QETApp::buildSystemTrayMenu() {
 	menu_systray -> clear();

Modified: branches/0.3/sources/qetapp.h
===================================================================
--- branches/0.3/sources/qetapp.h	2012-01-15 18:52:33 UTC (rev 1467)
+++ branches/0.3/sources/qetapp.h	2012-01-15 19:26:40 UTC (rev 1468)
@@ -23,6 +23,10 @@
 #include "elementslocation.h"
 #include "templatelocation.h"
 #include "qetarguments.h"
+
+#define QETAPP_COMMON_TBT_PROTOCOL "commontbt"
+#define QETAPP_CUSTOM_TBT_PROTOCOL "customtbt"
+
 class AboutQET;
 class QETDiagramEditor;
 class QETElementEditor;
@@ -39,6 +43,7 @@
 class QETTitleBlockTemplateEditor;
 class QTextOrientationSpinBoxWidget;
 class RecentFiles;
+
 /**
 	Cette classe represente l'application QElectroTech.
 	
@@ -71,6 +76,7 @@
 	static TitleBlockTemplatesFilesCollection *commonTitleBlockTemplatesCollection();
 	static TitleBlockTemplatesFilesCollection *customTitleBlockTemplatesCollection();
 	static QList<TitleBlockTemplatesCollection *> availableTitleBlockTemplatesCollections();
+	static TitleBlockTemplatesCollection *titleBlockTemplatesCollection(const QString &);
 	
 	static QString userName();
 	static QString commonElementsDir();
@@ -220,6 +226,8 @@
 		const QList<QETTitleBlockTemplateEditor *> &
 	);
 	template <class T> void addWindowsListToMenu(QMenu *, const QList<T *> &);
+	static int projectIdFromString(const QString &);
+	static QETProject *projectFromString(const QString &);
 };
 
 /**

Modified: branches/0.3/sources/titleblock/templatelocation.cpp
===================================================================
--- branches/0.3/sources/titleblock/templatelocation.cpp	2012-01-15 18:52:33 UTC (rev 1467)
+++ branches/0.3/sources/titleblock/templatelocation.cpp	2012-01-15 19:26:40 UTC (rev 1468)
@@ -37,6 +37,15 @@
 }
 
 /**
+	@param loc_str String describing the location of a title block template.
+*/
+TitleBlockTemplateLocation TitleBlockTemplateLocation::locationFromString(const QString &loc_str) {
+	TitleBlockTemplateLocation loc;
+	loc.fromString(loc_str);
+	return(loc);
+}
+
+/**
 	@return the parent collection of the template, or 0 if none was defined
 */
 TitleBlockTemplatesCollection *TitleBlockTemplateLocation::parentCollection() const {
@@ -73,6 +82,20 @@
 }
 
 /**
+	@param loc_str String describing the location of a title block template.
+*/
+void TitleBlockTemplateLocation::fromString(const QString &loc_str) {
+	collection_ = QETApp::titleBlockTemplatesCollection(QUrl(loc_str).scheme());
+	
+	QRegExp name_from_url("^[^:]*:\\/\\/(.*)$");
+	if (name_from_url.exactMatch(loc_str)) {
+		name_ = name_from_url.capturedTexts().at(1);
+	} else {
+		name_ = QString();
+	}
+}
+
+/**
 	@return A string representation of the location
 */
 QString TitleBlockTemplateLocation::toString() const {

Modified: branches/0.3/sources/titleblock/templatelocation.h
===================================================================
--- branches/0.3/sources/titleblock/templatelocation.h	2012-01-15 18:52:33 UTC (rev 1467)
+++ branches/0.3/sources/titleblock/templatelocation.h	2012-01-15 19:26:40 UTC (rev 1468)
@@ -31,6 +31,10 @@
 	TitleBlockTemplateLocation(const QString & = QString(), TitleBlockTemplatesCollection * = 0);
 	virtual ~TitleBlockTemplateLocation();
 	
+	// static methods
+	public:
+	TitleBlockTemplateLocation locationFromString(const QString &);
+	
 	// methods
 	public:
 	TitleBlockTemplatesCollection *parentCollection() const;
@@ -38,6 +42,7 @@
 	QString name() const;
 	void setName(const QString &);
 	bool isValid() const;
+	void fromString(const QString &);
 	QString toString() const;
 	QETProject *parentProject() const;
 	QString protocol() const;


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