Implemented the common and custom title block templates collections.

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/branches/0.3@1433 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
xavier
2012-01-08 17:04:34 +00:00
parent c86b064847
commit 91adb47b88
19 changed files with 1267 additions and 274 deletions

View File

@@ -32,7 +32,6 @@
QETTitleBlockTemplateEditor::QETTitleBlockTemplateEditor(QWidget *parent) :
QMainWindow(parent),
read_only(false),
parent_project_(0),
tb_template_(0),
logo_manager_(0)
{
@@ -54,7 +53,30 @@ QETTitleBlockTemplateEditor::~QETTitleBlockTemplateEditor() {
@return the location of the currently edited template
*/
TitleBlockTemplateLocation QETTitleBlockTemplateEditor::location() const {
return(TitleBlockTemplateLocation(parent_project_, template_name_));
return(location_);
}
/**
@param location Location of the tile block template to be edited.
*/
bool QETTitleBlockTemplateEditor::edit(const TitleBlockTemplateLocation &location) {
// the template name may be empty to create a new one
const TitleBlockTemplate *tb_template_orig;
if (location.name().isEmpty()) {
// loads the default title block template provided by the application
// it will be used as a start point to design the title block
tb_template_orig = QETApp::defaultTitleBlockTemplate();
} else {
tb_template_orig = location.getTemplate();
}
if (!tb_template_orig) {
/// TODO The TBT does not exist, manage error
return(false);
}
location_ = location;
editCopyOf(tb_template_orig);
return(true);
}
/**
@@ -82,9 +104,42 @@ bool QETTitleBlockTemplateEditor::edit(QETProject *project, const QString &templ
return(false);
}
tb_template_ = tb_template_orig -> clone();
parent_project_ = project;
template_name_ = template_name;
location_.setParentCollection(project -> embeddedTitleBlockTemplatesCollection());
location_.setName(template_name);
return(editCopyOf(tb_template_orig));
}
/**
@param file_path Path of the template file to edit.
@return false if a problem occured while opening the template, true otherwise.
*/
bool QETTitleBlockTemplateEditor::edit(const QString &file_path) {
// get title block template object from the file, edit it
TitleBlockTemplate *tbt = new TitleBlockTemplate();
bool loading = tbt -> loadFromXmlFile(file_path);
if (!loading) {
/// TODO the file opening failed, warn the user?
return(false);
}
return(edit(tbt));
}
/**
@param tbt Title block template to be edited
@return false if a problem occured while opening the template, true otherwise.
*/
bool QETTitleBlockTemplateEditor::editCopyOf(const TitleBlockTemplate *tbt) {
if (!tbt) return(false);
return(edit(tbt -> clone()));
}
/**
@param tbt Title block template to be directly edited
@return false if a problem occured while opening the template, true otherwise.
*/
bool QETTitleBlockTemplateEditor::edit(TitleBlockTemplate *tbt) {
if (!tbt) return(false);
tb_template_ = tbt;
template_edition_area_view_ -> setTitleBlockTemplate(tb_template_);
template_cell_editor_widget_ -> updateLogosComboBox(tb_template_);
updateEditorTitle();
@@ -242,6 +297,9 @@ void QETTitleBlockTemplateEditor::initWidgets() {
);
}
/**
Initialize the logo manager
*/
void QETTitleBlockTemplateEditor::initLogoManager() {
logo_manager_ = new TitleBlockTemplateLogoManager(tb_template_);
connect(
@@ -302,7 +360,7 @@ void QETTitleBlockTemplateEditor::updateEditorTitle() {
);
QString title;
if (template_name_.isEmpty()) {
if (location_.name().isEmpty()) {
title = min_title;
} else {
title = QString(
@@ -310,38 +368,37 @@ void QETTitleBlockTemplateEditor::updateEditorTitle() {
"%1 - %2",
"window title: %1 is the base window title, %2 is a template name"
)
).arg(min_title).arg(template_name_);
).arg(min_title).arg(location_.name());
}
setWindowTitle(title);
}
/**
Save the template as \a name within \a project.
Save the template under the provided location.
@see QETProject::setTemplateXmlDescription()
@param project Parent project
@param name Template name
@param location Location where the title block template should be saved.
*/
void QETTitleBlockTemplateEditor::saveAs(QETProject *project, const QString &name) {
if (!project || name.isEmpty()) return;
void QETTitleBlockTemplateEditor::saveAs(const TitleBlockTemplateLocation &location) {
TitleBlockTemplatesCollection *collection = location.parentCollection();
if (!collection) return;
QDomDocument doc;
QDomElement elmt = doc.createElement("root");
tb_template_ -> saveToXmlElement(elmt);
elmt.setAttribute("name", name);
elmt.setAttribute("name", location.name());
doc.appendChild(elmt);
project -> setTemplateXmlDescription(name, elmt);
collection -> setTemplateXmlDescription(location.name(), elmt);
parent_project_ = project;
template_name_ = name;
location_ = location;
}
/**
Save the currently edited title block template back to its parent project.
*/
void QETTitleBlockTemplateEditor::save() {
if (parent_project_ && !template_name_.isEmpty()) {
saveAs(parent_project_, template_name_);
if (location_.isValid()) {
saveAs(location_);
} else {
saveAs();
}
@@ -353,13 +410,14 @@ void QETTitleBlockTemplateEditor::save() {
void QETTitleBlockTemplateEditor::saveAs() {
TitleBlockTemplateLocation location = getTitleBlockTemplateLocationFromUser();
if (location.isValid()) {
saveAs(location.project(), location.name());
saveAs(location);
}
}
/**
Ask the user for a title block template location
@return The location chosen by the user, or an empty TitleBlockTemplateLocation if the user cancelled the dialog
Ask the user for a title block template location @return The location chosen
by the user, or an empty TitleBlockTemplateLocation if the user cancelled the
dialog
*/
TitleBlockTemplateLocation QETTitleBlockTemplateEditor::getTitleBlockTemplateLocationFromUser() {
TitleBlockTemplateLocationChooser *chooser = new TitleBlockTemplateLocationChooser(location());

View File

@@ -51,10 +51,8 @@ class QETTitleBlockTemplateEditor : public QMainWindow {
/// actions
QAction *save_, *save_as_, *quit_, *configure_, *about_qt_, *about_qet_, *merge_cells_, *split_cell_;
QAction *zoom_in_, *zoom_out_, *zoom_fit_, *zoom_reset_;
/// Parent project of the currently edited template
QETProject *parent_project_;
/// Name of the currently edited template
QString template_name_;
/// Location of the currently edited template
TitleBlockTemplateLocation location_;
/// Template Object edited
TitleBlockTemplate *tb_template_;
/// Template preview
@@ -84,7 +82,11 @@ class QETTitleBlockTemplateEditor : public QMainWindow {
public slots:
void selectedCellsChanged(QList<TitleBlockCell *>);
bool edit(const TitleBlockTemplateLocation &);
bool edit(QETProject *, const QString &);
bool edit(const QString &);
bool editCopyOf(const TitleBlockTemplate *);
bool edit(TitleBlockTemplate *);
void editLogos();
void save();
void saveAs();
@@ -96,7 +98,7 @@ class QETTitleBlockTemplateEditor : public QMainWindow {
void pushGridUndoCommand(TitleBlockTemplateCommand *);
void pushUndoCommand(QUndoCommand *);
void updateEditorTitle();
void saveAs(QETProject *, const QString &);
void saveAs(const TitleBlockTemplateLocation &);
};
#endif

View File

@@ -43,10 +43,12 @@ TitleBlockTemplateDeleter::~TitleBlockTemplateDeleter() {
*/
bool TitleBlockTemplateDeleter::exec() {
if (!template_location_.isValid()) return(false);
QETProject *project = template_location_.project();
QString name = template_location_.name();
if (!project -> embeddedTitleBlockTemplates().contains(name)) {
QString name = template_location_.name();
TitleBlockTemplatesCollection *collection = template_location_.parentCollection();
if (!collection) return(false);
if (!collection -> templates().contains(name)) {
return(false);
}
@@ -65,6 +67,6 @@ bool TitleBlockTemplateDeleter::exec() {
if (answer != QMessageBox::Yes) return(false);
// delete the title block template
project -> removeTemplateByName(name);
collection -> removeTemplate(name);
return(true);
}

View File

@@ -16,14 +16,16 @@
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "templatelocation.h"
#include "templatescollection.h"
#include "qetapp.h"
/**
Constructor
@param project Parent project of the title block template
@param collection Parent collection of the title block template
@param name Name of the title block template within its parent project or collection
*/
TitleBlockTemplateLocation::TitleBlockTemplateLocation(QETProject *project, const QString &name) :
project_(project),
TitleBlockTemplateLocation::TitleBlockTemplateLocation(const QString &name, TitleBlockTemplatesCollection *collection) :
collection_(collection),
name_(name)
{
}
@@ -35,18 +37,18 @@ TitleBlockTemplateLocation::~TitleBlockTemplateLocation() {
}
/**
@return the parent project of the template, or 0 if none was defined
@return the parent collection of the template, or 0 if none was defined
*/
QETProject *TitleBlockTemplateLocation::project() const {
return(project_);
TitleBlockTemplatesCollection *TitleBlockTemplateLocation::parentCollection() const {
return(collection_);
}
/**
@param project The new parent project of the template, or 0 if none
@param project The new parent collection of the template, or 0 if none
applies.
*/
void TitleBlockTemplateLocation::setProject(QETProject *project) {
project_ = project;
void TitleBlockTemplateLocation::setParentCollection(TitleBlockTemplatesCollection *collection) {
collection_ = collection;
}
/**
@@ -67,5 +69,43 @@ void TitleBlockTemplateLocation::setName(const QString &name) {
@return true if this location is null, false otherwise
*/
bool TitleBlockTemplateLocation::isValid() const {
return(project_ && !name_.isEmpty());
return(!name_.isEmpty());
}
/**
@return A string representation of the location
*/
QString TitleBlockTemplateLocation::toString() const {
return(protocol() + QString("://") + name_);
}
/**
This is a convenience method equivalent to
parentCollection() -> parentProject().
*/
QETProject *TitleBlockTemplateLocation::parentProject() const {
if (collection_) {
return(collection_ -> parentProject());
}
return(0);
}
/**
This is a convenience method equivalent to
parentCollection() -> protocol().
*/
QString TitleBlockTemplateLocation::protocol() const {
if (collection_) {
return(collection_ -> protocol());
}
return("unknown");
}
/**
This is a convenience method equivalent to
parentCollection() -> getTemplate(...).
*/
TitleBlockTemplate *TitleBlockTemplateLocation::getTemplate() const {
if (!collection_ || name_.isEmpty()) return(0);
return(collection_ -> getTemplate(name_));
}

View File

@@ -19,6 +19,8 @@
#define TITLEBLOCK_SLASH_TEMPLATE_LOCATION_H
#include <QtCore>
class QETProject;
class TitleBlockTemplate;
class TitleBlockTemplatesCollection;
/**
This class represents the location of a title block template.
@@ -26,20 +28,24 @@ class QETProject;
class TitleBlockTemplateLocation {
// constructor, destructor
public:
TitleBlockTemplateLocation(QETProject * = 0, const QString & = QString());
TitleBlockTemplateLocation(const QString & = QString(), TitleBlockTemplatesCollection * = 0);
virtual ~TitleBlockTemplateLocation();
// methods
public:
QETProject *project() const;
void setProject(QETProject *);
TitleBlockTemplatesCollection *parentCollection() const;
void setParentCollection(TitleBlockTemplatesCollection *);
QString name() const;
void setName(const QString &);
bool isValid() const;
QString toString() const;
QETProject *parentProject() const;
QString protocol() const;
TitleBlockTemplate *getTemplate() const;
// attributes
private:
QETProject *project_; ///< Parent project of the template, if any
QString name_; ///< Name of the template
TitleBlockTemplatesCollection *collection_; ///< Collection the template belongs to
QString name_; ///< Name of the template
};
#endif

View File

@@ -1,6 +1,7 @@
#include "templatelocationchooser.h"
#include "qetapp.h"
#include "qetproject.h"
#include "templatescollection.h"
/**
Constructor
@@ -27,15 +28,14 @@ TitleBlockTemplateLocationChooser::~TitleBlockTemplateLocationChooser() {
@return the current location
*/
TitleBlockTemplateLocation TitleBlockTemplateLocationChooser::location() const {
return(TitleBlockTemplateLocation(project(), name()));
return(TitleBlockTemplateLocation(name(), collection()));
}
/**
@return the currently selected project
@return the currently selected collection
*/
QETProject *TitleBlockTemplateLocationChooser::project() const {
uint project_id = projects_ -> itemData(projects_ -> currentIndex()).toUInt();
return(QETApp::project(project_id));
TitleBlockTemplatesCollection *TitleBlockTemplateLocationChooser::collection() const {
return(collections_index_[collections_ -> currentIndex()]);
}
/**
@@ -51,15 +51,7 @@ QString TitleBlockTemplateLocationChooser::name() const {
@param location to be displayed by this widget
*/
void TitleBlockTemplateLocationChooser::setLocation(const TitleBlockTemplateLocation &location) {
// we need a project id
int project_id = QETApp::projectId(location.project());
if (project_id == -1) return;
// attempt to find this project id in our project combo box
int project_index = projects_ -> findData(QVariant(static_cast<uint>(project_id)));
if (project_index == -1) return;
projects_ -> setCurrentIndex(project_index);
collections_ -> setCurrentIndex(collections_index_.keys(location.parentCollection()).first());
if (!location.name().isEmpty()) {
int template_index = templates_ -> findText(location.name());
@@ -76,43 +68,64 @@ void TitleBlockTemplateLocationChooser::setLocation(const TitleBlockTemplateLoca
@param location Initial location displayed by the widget
*/
void TitleBlockTemplateLocationChooser::init() {
projects_ = new QComboBox();
collections_ = new QComboBox();
templates_ = new QComboBox();
new_name_ = new QLineEdit();
QMap<uint, QETProject *> projects = QETApp::registeredProjects();
foreach (uint project_id, projects.keys()) {
projects_ -> addItem(projects[project_id] -> title(), project_id);
}
updateTemplates();
connect(projects_, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTemplates()));
updateCollections();
connect(collections_, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTemplates()));
connect(templates_, SIGNAL(currentIndexChanged(int)), this, SLOT(updateNewName()));
QFormLayout *form_layout = new QFormLayout();
form_layout -> addRow(tr("Projet parent", "used in save as form"), projects_);
form_layout -> addRow(tr("Modèle existant", "used in save as form"), templates_);
form_layout -> addRow(tr("ou nouveau nom", "used in save as form"), new_name_);
form_layout -> addRow(tr("Collection parente", "used in save as form"), collections_);
form_layout -> addRow(tr("Modèle existant", "used in save as form"), templates_);
form_layout -> addRow(tr("ou nouveau nom", "used in save as form"), new_name_);
setLayout(form_layout);
}
/**
Update the collections list
*/
void TitleBlockTemplateLocationChooser::updateCollections() {
collections_ -> clear();
collections_index_.clear();
int index = 0;
foreach(TitleBlockTemplatesCollection *collection, QETApp::availableTitleBlockTemplatesCollections()) {
collections_ -> addItem(collection -> title());
collections_index_.insert(index, collection);
++ index;
}
updateTemplates();
}
/**
Update the templates list according to the selected project.
*/
void TitleBlockTemplateLocationChooser::updateTemplates() {
int current_index = projects_ -> currentIndex();
if (current_index == -1) return;
uint project_id = projects_ -> itemData(current_index).toUInt();
QETProject *project = QETApp::project(project_id);
if (!project) return;
TitleBlockTemplatesCollection *current_collection = collection();
if (!current_collection) return;
templates_ -> clear();
templates_ -> addItem(tr("Nouveau modèle (entrez son nom)", "combox box entry"), QVariant(false));
QStringList available_templates = project -> embeddedTitleBlockTemplates();
QStringList available_templates = current_collection -> templates();
if (available_templates.count()) {
templates_ -> insertSeparator(1);
foreach (QString template_name, available_templates) {
templates_ -> addItem(template_name, QVariant(true));
}
}
updateNewName();
}
/**
Enable or diable the "new name" text field depending of the selected
template.
*/
void TitleBlockTemplateLocationChooser::updateNewName() {
int template_index = templates_ -> currentIndex();
new_name_ -> setEnabled(!template_index);
}

View File

@@ -19,6 +19,8 @@
#define TITLEBLOCK_SLASH_LOCATION_CHOOSER_H
#include <QtGui>
#include "templatelocation.h"
class TitleBlockTemplateCollection;
/**
This class is a widget that allows the user to choose a target title block
template.
@@ -35,7 +37,7 @@ class TitleBlockTemplateLocationChooser : public QWidget {
// methods
public:
TitleBlockTemplateLocation location() const;
QETProject *project() const;
TitleBlockTemplatesCollection *collection() const;
QString name() const;
void setLocation(const TitleBlockTemplateLocation &);
private:
@@ -43,12 +45,16 @@ class TitleBlockTemplateLocationChooser : public QWidget {
// slots
private slots:
void updateCollections();
void updateTemplates();
void updateNewName();
// attributes
private:
QComboBox *projects_; ///< Projects combo box
QComboBox *templates_; ///< Existing templates combo box
QLineEdit *new_name_; ///< New template name textfield
QComboBox *collections_; ///< Collections combo box
/// Collections index within the combo box
QHash<int, TitleBlockTemplatesCollection *> collections_index_;
QComboBox *templates_; ///< Existing templates combo box
QLineEdit *new_name_; ///< New template name textfield
};
#endif

View File

@@ -0,0 +1,464 @@
/*
Copyright 2006-2012 Xavier Guerrin
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "templatescollection.h"
#include "titleblocktemplate.h"
#include "qetapp.h"
#include "qetproject.h"
/**
Constructor
@param parent Parent QObject
*/
TitleBlockTemplatesCollection::TitleBlockTemplatesCollection(QObject *parent) :
QObject(parent),
protocol_("unknown")
{
}
/**
Destructor
*/
TitleBlockTemplatesCollection::~TitleBlockTemplatesCollection() {
}
/**
@return the title of this collection
*/
QString TitleBlockTemplatesCollection::title() const {
return(title_);
}
/**
@oaram title New title for this collection
*/
void TitleBlockTemplatesCollection::setTitle(const QString &title) {
title_ = title;
}
/**
@return the protocol used by this collection ; examples: commontbt,
customtbt, embedtbt, ...
*/
QString TitleBlockTemplatesCollection::protocol() const {
return(protocol_);
}
/**
Define the protocol for this collection
@param protocol New protocol for this collection
*/
void TitleBlockTemplatesCollection::setProtocol(const QString &protocol) {
if (!protocol.isEmpty()) protocol_ = protocol;
}
/**
@return the project this collection is affiliated to, or 0 if this
collection is not related to any project.
*/
QETProject *TitleBlockTemplatesCollection::parentProject() {
return(0);
}
/**
Constructor
@param project Parent project
@param parent Parent QObject
*/
TitleBlockTemplatesProjectCollection::TitleBlockTemplatesProjectCollection(QETProject *project, QObject *parent) :
TitleBlockTemplatesCollection(parent),
project_(project)
{
}
/**
Destructor
*/
TitleBlockTemplatesProjectCollection::~TitleBlockTemplatesProjectCollection() {
}
/**
@return a human readable title for this collection
*/
QString TitleBlockTemplatesProjectCollection::title() const {
if (!title_.isEmpty()) return(title_);
// if the title attribute is empty, we generate a suitable one using the
// parent project
QString final_title;
if (project_) {
QString project_title = project_ -> title();
if (project_title.isEmpty()) {
final_title = QString(
tr(
"Cartouches du projet sans titre (id %1)",
"collection title when the parent project has an empty title -- %1 is the project internal id"
)
);
final_title = final_title.arg(QETApp::projectId(project_));
} else {
final_title = QString(
tr(
"Cartouches du projet \"%1\"",
"collection title when the project has a suitble title -- %1 is the project title"
)
);
final_title = final_title.arg(project_title);
}
}
return(final_title);
}
/**
@return the protocol used to mention this collection
*/
QString TitleBlockTemplatesProjectCollection::protocol() const {
if (project_) {
int project_id = QETApp::projectId(project_);
if (project_id != -1) {
return(QString("project%1+embedtbt").arg(project_id));
}
}
// fall back on the parent method
return(TitleBlockTemplatesCollection::protocol());
}
/**
@return the parent project of this project collection
*/
QETProject *TitleBlockTemplatesProjectCollection::parentProject() {
return(project_);
}
/**
@return the list of title block templates embedded within the project.
*/
QStringList TitleBlockTemplatesProjectCollection::templates() {
return(titleblock_templates_xml_.keys());
}
/**
@param template_name Name of the requested template
@return the requested template, or 0 if there is no valid template of this
name within the project
*/
TitleBlockTemplate *TitleBlockTemplatesProjectCollection::getTemplate(const QString &template_name){
// Do we have already loaded this template?
if (titleblock_templates_.contains(template_name)) {
return(titleblock_templates_[template_name]);
}
// No? Do we even know of it?
if (!titleblock_templates_xml_.contains(template_name)) {
return(0);
}
// Ok, we have its XML description, we have to generate a TitleBlockTemplate object
TitleBlockTemplate *titleblock_template = new TitleBlockTemplate(this);
if (titleblock_template -> loadFromXmlElement(titleblock_templates_xml_[template_name])) {
titleblock_templates_.insert(template_name, titleblock_template);
return(titleblock_template);
} else {
return(0);
}
}
/**
@param template_name Name of the requested template
@return the XML description of the requested template, or a null QDomElement
if the project does not have such an titleblock template
*/
QDomElement TitleBlockTemplatesProjectCollection::getTemplateXmlDescription(const QString &template_name) {
if (titleblock_templates_xml_.contains(template_name)) {
return(titleblock_templates_xml_[template_name]);
}
return(QDomElement());
}
/**
This methods allows adding or modifying a template embedded within the
project. This method emits the signal changed() if
necessary.
@param template_name Name / Identifier of the template - will be used to
determine whether the given description will be added or will replace an
existing one.
@param xml_elmt An \<titleblocktemplate\> XML element describing the
template. Its "name" attribute must equal to template_name.
@return false if a problem occured, true otherwise
*/
bool TitleBlockTemplatesProjectCollection::setTemplateXmlDescription(const QString &template_name, const QDomElement &xml_elmt) {
// check basic stuff
if (xml_elmt.tagName() != "titleblocktemplate" || xml_elmt.attribute("name") != template_name) {
return(false);
}
// we *require* a project (at least for the moment...)
if (!project_) return(false);
// we import the provided XML element in the project document
QDomElement import = xml_document_.importNode(xml_elmt, true).toElement();
// we either replace the previous description
if (titleblock_templates_xml_.contains(template_name)) {
QDomElement old_description = titleblock_templates_xml_[template_name];
if (!old_description.parentNode().isNull()) {
old_description.parentNode().replaceChild(import, old_description);
}
}
titleblock_templates_xml_.insert(template_name, import);
if (titleblock_templates_.contains(template_name)) {
titleblock_templates_[template_name] -> loadFromXmlElement(titleblock_templates_xml_[template_name]);
}
emit(changed(this, template_name));
return(true);
}
/**
This methods allows removing a template embedded within the project. This
method emits the signal changed() if necessary.
@param template_name Name of the template to be removed
*/
void TitleBlockTemplatesProjectCollection::removeTemplate(const QString &template_name) {
emit(aboutToRemove(this, template_name));
// remove the template itself
titleblock_templates_xml_.remove(template_name);
titleblock_templates_.remove(template_name);
// warn the rest of the world that the list of templates embedded within this project has changed
emit(changed(this, template_name));
}
/**
@param template_name Name of the requested template
@return the location object for the requested template
*/
TitleBlockTemplateLocation TitleBlockTemplatesProjectCollection::location(const QString &template_name) {
return(TitleBlockTemplateLocation(template_name, this));
}
/**
@return true if this collection is read only
*/
bool TitleBlockTemplatesProjectCollection::isReadOnly() const {
if (project_) {
return(project_ -> isReadOnly());
}
return(false);
}
/**
@param xml_element XML element to be parsed to load title block templates
*/
void TitleBlockTemplatesProjectCollection::fromXml(const QDomElement &xml_element) {
foreach (QDomElement e, QET::findInDomElement(xml_element, "titleblocktemplates", "titleblocktemplate")) {
// each titleblock template must have a name
if (!e.hasAttribute("name")) continue;
QString titleblock_template_name = e.attribute("name");
// if several templates have the same name, we keep the first one encountered
if (titleblock_templates_xml_.contains(titleblock_template_name)) continue;
// we simply store the XML element describing the titleblock template,
// without any further analysis for the moment
titleblock_templates_xml_.insert(titleblock_template_name, e);
}
}
/**
Constructor
@param path Path of the directory containing the collection
@param parent Parent QObject
*/
TitleBlockTemplatesFilesCollection::TitleBlockTemplatesFilesCollection(const QString &path, QObject *parent) :
TitleBlockTemplatesCollection(parent),
dir_(
path,
QString("*%1").arg(TITLEBLOCKS_FILE_EXTENSION),
QDir::SortFlags(QDir::Name | QDir::IgnoreCase),
QDir::Readable | QDir::Files
)
{
if (dir_.exists()) {
watcher_.addPath(dir_.canonicalPath());
}
connect(&watcher_, SIGNAL(directoryChanged(const QString &)), this, SLOT(fileSystemChanged(const QString &)));
}
/**
Destructor
*/
TitleBlockTemplatesFilesCollection::~TitleBlockTemplatesFilesCollection() {
}
/**
@return the canonical path of the directory hosting this collection.
*/
QString TitleBlockTemplatesFilesCollection::path(const QString &template_name) const {
if (template_name.isEmpty()) {
return(dir_.canonicalPath());
} else {
return(dir_.absoluteFilePath(toFileName(template_name)));
}
}
/**
@return the list of templates contained in this collection
*/
QStringList TitleBlockTemplatesFilesCollection::templates() {
QStringList templates_names;
QRegExp replace_regexp(QString("%1$").arg(TITLEBLOCKS_FILE_EXTENSION));
foreach(QString name, dir_.entryList()) {
templates_names << name.replace(replace_regexp, "");
}
return(templates_names);
}
/**
@return the template which name is \a template_name, or 0 if the template
could not be loaded.
*/
TitleBlockTemplate *TitleBlockTemplatesFilesCollection::getTemplate(const QString &template_name) {
if (!templates().contains(template_name)) return(0);
TitleBlockTemplate *tbtemplate = new TitleBlockTemplate();
QString tbt_file_path = path(template_name);
bool loading = tbtemplate -> loadFromXmlFile(tbt_file_path);
if (!loading) {
delete tbtemplate;
return(0);
}
return(tbtemplate);
}
/**
@param template_name Name of a template (which has to already exist)
@return the XML description for the \a template_name template, or a null QDomElement if no such template exists.
*/
QDomElement TitleBlockTemplatesFilesCollection::getTemplateXmlDescription(const QString &template_name) {
QString xml_file_path = path(template_name);
QFileInfo xml_file_info(xml_file_path);
if (!xml_file_info.exists() || !xml_file_info.isReadable()) {
return(QDomElement());
}
QFile xml_file(xml_file_path);
if (!xml_file.open(QIODevice::ReadOnly)) {
return(QDomElement());
}
QDomDocument *xml_document = new QDomDocument();
bool xml_parsing = xml_document -> setContent(&xml_file);
if (!xml_parsing) {
delete xml_document;
return(QDomElement());
}
return(xml_document -> documentElement());
}
/**
Set the XML description of the \a template_name template to \a xml_element.
@param template_name Name of a template (which does not have to already exist)
@param xml_element XML element describing the template
*/
bool TitleBlockTemplatesFilesCollection::setTemplateXmlDescription(const QString &template_name, const QDomElement &xml_element) {
if (template_name.isEmpty()) return(false);
// prevent the watcher from emitting signals while we open and write to file
blockSignals(true);
QFile xml_file(path(template_name));
if (!xml_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
return(false);
}
QDomDocument doc;
doc.appendChild(doc.importNode(xml_element, true));
QTextStream out(&xml_file);
out.setCodec("UTF-8");
out << doc.toString(4);
xml_file.close();
// emit a single signal for the change
blockSignals(false);
emit(changed(this, template_name));
return(true);
}
/**
Remove the \a template_name template.
*/
void TitleBlockTemplatesFilesCollection::removeTemplate(const QString &template_name) {
emit(aboutToRemove(this, template_name));
// prevent the watcher from emitting signals while we open and write to file
blockSignals(true);
dir_.remove(toFileName(template_name));
// emit a single signal for the removal
blockSignals(false);
emit(changed(this, template_name));
}
/**
@param template_name Name of a template supposed to be contained within
this collection.
@return
*/
TitleBlockTemplateLocation TitleBlockTemplatesFilesCollection::location(const QString &template_name) {
return(TitleBlockTemplateLocation(template_name, this));
}
/**
@return true if this collection is read only, false otherwise
*/
bool TitleBlockTemplatesFilesCollection::isReadOnly() const {
QFileInfo info(dir_.canonicalPath());
return(!info.isWritable());
}
/**
@param file_name A file name
@return the template name for \a file_name
*/
QString TitleBlockTemplatesFilesCollection::toTemplateName(const QString &file_name) {
static QRegExp replace_regexp(QString("%1$").arg(TITLEBLOCKS_FILE_EXTENSION));
QString template_name(file_name);
return(template_name.replace(replace_regexp, ""));
}
/**
@param template_name A template name
@return the file name for \a template_name
*/
QString TitleBlockTemplatesFilesCollection::toFileName(const QString &template_name) {
return(QString("%1%2").arg(template_name).arg(TITLEBLOCKS_FILE_EXTENSION));
}
/**
Handle the changes occuring on the file system.
@param str Path of the directory that changed.
*/
void TitleBlockTemplatesFilesCollection::fileSystemChanged(const QString &str) {
Q_UNUSED(str);
dir_.refresh();
emit(changed(this));
}

View File

@@ -0,0 +1,148 @@
/*
Copyright 2006-2012 Xavier Guerrin
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TITLEBLOCK_SLASH_TEMPLATES_COLLECTION_H
#define TITLEBLOCK_SLASH_TEMPLATES_COLLECTION_H
#define TITLEBLOCKS_FILE_EXTENSION ".titleblock"
#include <QtCore>
#include <QtXml>
#include "templatelocation.h"
class TitleBlockTemplate;
class QETProject;
/**
This abstract class represents a generic collection of title block templates.
Unlike elements collections, title block templates collections do not provide
a tree structure. Instead, they provide a simple, flat list of available
templates.
*/
class TitleBlockTemplatesCollection : public QObject {
Q_OBJECT
// Constructors, destructor
public:
TitleBlockTemplatesCollection(QObject *parent);
virtual ~TitleBlockTemplatesCollection();
private:
TitleBlockTemplatesCollection(const TitleBlockTemplatesCollection &);
// methods
public:
virtual QStringList templates() = 0;
virtual TitleBlockTemplate *getTemplate(const QString &) = 0;
virtual QDomElement getTemplateXmlDescription(const QString &) = 0;
virtual bool setTemplateXmlDescription(const QString &, const QDomElement &) = 0;
virtual void removeTemplate(const QString &) = 0;
virtual TitleBlockTemplateLocation location(const QString & = QString()) = 0;
virtual bool isReadOnly() const = 0;
virtual QString title() const;
virtual void setTitle(const QString &);
virtual QString protocol() const;
virtual void setProtocol(const QString &);
virtual QETProject *parentProject();
signals:
void changed(TitleBlockTemplatesCollection *, const QString & = QString());
void aboutToRemove(TitleBlockTemplatesCollection *, const QString &);
// attributes
protected:
/// Human-readable title for this collection
QString title_;
/// Protocol used to designate this collection
QString protocol_;
/// Already parsed embedded titleblock templates
QHash<QString, TitleBlockTemplate *> titleblock_templates_;
};
/**
This class represents a simple abastraction layer for a collection of title
block templates embedded within a project.
*/
class TitleBlockTemplatesProjectCollection : public TitleBlockTemplatesCollection {
Q_OBJECT
// Constructors, destructor
public:
TitleBlockTemplatesProjectCollection(QETProject *, QObject *parent = 0);
virtual ~TitleBlockTemplatesProjectCollection();
private:
TitleBlockTemplatesProjectCollection(const TitleBlockTemplatesProjectCollection &);
// methods
public:
virtual QString title() const;
virtual QString protocol() const;
virtual QETProject *parentProject();
virtual QStringList templates();
virtual TitleBlockTemplate *getTemplate(const QString &);
virtual QDomElement getTemplateXmlDescription(const QString &);
virtual bool setTemplateXmlDescription(const QString &, const QDomElement &);
virtual void removeTemplate(const QString &);
virtual TitleBlockTemplateLocation location(const QString & = QString());
virtual bool isReadOnly() const;
virtual void fromXml(const QDomElement &);
// attributes
private:
/// Parent project
QETProject *project_;
/// Parent QDomDocument for stored QDomElements
QDomDocument xml_document_;
/// XML descriptions of embedded titleblock templates
QHash<QString, QDomElement> titleblock_templates_xml_;
};
/**
This classe represents a simple abastraction layer for a file-based title
block templates directory.
*/
class TitleBlockTemplatesFilesCollection : public TitleBlockTemplatesCollection {
Q_OBJECT
// Constructors, destructor
public:
TitleBlockTemplatesFilesCollection(const QString &, QObject * = 0);
virtual ~TitleBlockTemplatesFilesCollection();
private:
TitleBlockTemplatesFilesCollection(const TitleBlockTemplatesFilesCollection &);
// methods
public:
QString path(const QString & = QString()) const;
virtual QStringList templates();
virtual TitleBlockTemplate *getTemplate(const QString &);
virtual QDomElement getTemplateXmlDescription(const QString &);
virtual bool setTemplateXmlDescription(const QString &, const QDomElement &);
virtual void removeTemplate(const QString &);
virtual TitleBlockTemplateLocation location(const QString & = QString());
virtual bool isReadOnly() const;
static QString toTemplateName(const QString &);
static QString toFileName(const QString &);
private slots:
void fileSystemChanged(const QString &str);
// attributes
private:
/// File System Watcher object to track the files changes made outside the application
QFileSystemWatcher watcher_;
/// Collection real directory
QDir dir_;
};
#endif