diff --git a/sources/diagram.cpp b/sources/diagram.cpp index 347b9a5c8..a4801e292 100644 --- a/sources/diagram.cpp +++ b/sources/diagram.cpp @@ -761,6 +761,10 @@ void Diagram::setTitleBlockTemplate(const QString &template_name) { QString current_name = border_and_titleblock.titleBlockTemplateName(); const TitleBlockTemplate *titleblock_template = project_ -> getTemplateByName(template_name); border_and_titleblock.titleBlockTemplateRemoved(current_name, titleblock_template); + + if (template_name != current_name) { + emit(usedTitleBlockTemplateChanged(template_name)); + } } /** @@ -900,6 +904,15 @@ bool Diagram::usesElement(const ElementsLocation &location) { return(false); } +/** + @param a title block template name + @return true if the provided template is used by this diagram, false + otherwise. +*/ +bool Diagram::usesTitleBlockTemplate(const QString &name) { + return(name == border_and_titleblock.titleBlockTemplateName()); +} + /** Cette methode permet d'appliquer de nouvelles options de rendu tout en accedant aux proprietes de rendu en cours. diff --git a/sources/diagram.h b/sources/diagram.h index 550ccbf09..95d15a85a 100644 --- a/sources/diagram.h +++ b/sources/diagram.h @@ -165,6 +165,7 @@ class Diagram : public QGraphicsScene { void continueMoveElementTexts(const QPointF &); void endMoveElementTexts(); bool usesElement(const ElementsLocation &); + bool usesTitleBlockTemplate(const QString &); QUndoStack &undoStack(); QGIManager &qgiManager(); @@ -183,6 +184,7 @@ class Diagram : public QGraphicsScene { signals: void written(); void readOnlyChanged(bool); + void usedTitleBlockTemplateChanged(const QString &); }; /** diff --git a/sources/elementspanel.cpp b/sources/elementspanel.cpp index 6fe0e233c..611251f31 100644 --- a/sources/elementspanel.cpp +++ b/sources/elementspanel.cpp @@ -760,11 +760,7 @@ QTreeWidgetItem *ElementsPanel::addElement(QTreeWidgetItem *qtwi_parent, Element if (QETProject *element_project = element -> location().project()) { // affiche en rouge les elements inutilises dans un projet if (!element_project -> usesElement(element -> location())) { - QLinearGradient t(0, 0, 200, 0); - t.setColorAt(0, QColor("#ffc0c0")); - t.setColorAt(1, QColor("#ffffff")); - qtwi -> setBackground(0, QBrush(t)); - qtwi -> setToolTip(0, QString(tr("%1 [non utilis\351 dans le projet]")).arg(qtwi -> toolTip(0))); + markItemAsUnused(qtwi); } } locations_.insert(qtwi, element -> location()); @@ -788,6 +784,8 @@ QTreeWidgetItem *ElementsPanel::addTitleBlockTemplatesCollection( ) { if (!collection) return(0); + QString selected_template; + // check whether we have an item for the given collection QTreeWidgetItem *qtwi_tbt_collection = title_blocks_collections_.key(collection); if (!qtwi_tbt_collection) { @@ -809,7 +807,24 @@ QTreeWidgetItem *ElementsPanel::addTitleBlockTemplatesCollection( this, SLOT(titleBlockTemplatesCollectionChanged(TitleBlockTemplatesCollection*, const QString &)) ); + // if the added collection is rattached to a project, we're interested in + // knowing how many times each template is used. + if (QETProject *project = collection -> parentProject()) { + connect( + project, SIGNAL(diagramUsedTemplate(TitleBlockTemplatesCollection *, const QString &)), + this, SLOT(titleBlockTemplatesCollectionChanged(TitleBlockTemplatesCollection *, const QString &)) + ); + } } else { + // save the currently selected template, if any + if (QTreeWidgetItem *current_qtwi = currentItem()) { + for (int i = 0 ; i < qtwi_tbt_collection -> childCount() ; ++ i) { + if (qtwi_tbt_collection -> child(i) == current_qtwi) { + selected_template = nameOfTitleBlockTemplate(qtwi_tbt_collection -> child(i)); + } + } + } + // the collection has already been added // remove the child title block templates foreach(QTreeWidgetItem *qtwi_tbt, qtwi_tbt_collection -> takeChildren()) { @@ -839,8 +854,26 @@ QTreeWidgetItem *ElementsPanel::addTitleBlockTemplatesCollection( ); qtwi_tbt -> setToolTip(0, template_location.toString()); qtwi_tbt -> setIcon(0, QET::Icons::TitleBlock); + + // special action for templates that belong to a project + if (QETProject *tbt_project = template_location.parentProject()) { + // display unused templates using a red background + if (!tbt_project -> usesTitleBlockTemplate(template_location)) { + markItemAsUnused(qtwi_tbt); + } + } + title_blocks_.insert(qtwi_tbt, template_location); } + + // restore the previously selected template, if any + if (!selected_template.isEmpty()) { + TitleBlockTemplateLocation location = collection -> location(selected_template); + QTreeWidgetItem *previously_selected_item = title_blocks_.key(location, 0); + if (previously_selected_item) { + setCurrentItem(previously_selected_item); + } + } return(qtwi_tbt_collection); } @@ -892,6 +925,11 @@ void ElementsPanel::reload(bool reload_collections) { } // vide l'arbre et le hash + foreach (TitleBlockTemplatesCollection *tbt_collection, title_blocks_collections_) { + if (QETProject *project = tbt_collection -> parentProject()) { + disconnect(project, 0, this, 0); + } + } clear(); locations_.clear(); projects_.clear(); @@ -1399,3 +1437,15 @@ void ElementsPanel::ensureHierarchyIsVisible(QList items) { } } +/** + Mark the provided QTreeWidgetItem as unused in its parent project. + @param qtwi A QTreeWidgetItem +*/ +void ElementsPanel::markItemAsUnused(QTreeWidgetItem *qtwi) { + QLinearGradient t(0, 0, 200, 0); + t.setColorAt(0, QColor("#ffc0c0")); + t.setColorAt(1, QColor("#ffffff")); + qtwi -> setBackground(0, QBrush(t)); + qtwi -> setToolTip(0, QString(tr("%1 [non utilis\351 dans le projet]")).arg(qtwi -> toolTip(0))); +} + diff --git a/sources/elementspanel.h b/sources/elementspanel.h index 164f78124..8fa66cebd 100644 --- a/sources/elementspanel.h +++ b/sources/elementspanel.h @@ -137,6 +137,7 @@ class ElementsPanel : public QTreeWidget { QString diagramTitleToDisplay(Diagram *) const; QString titleBlockTemplateNameToDisplay(const QString &) const; void ensureHierarchyIsVisible(QList); + void markItemAsUnused(QTreeWidgetItem *); // attributs private: diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index 7150342de..4a3f3f80c 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -723,6 +723,23 @@ bool QETProject::usesElement(const ElementsLocation &location) { return(false); } +/** + @param location Location of a title block template + @return true if the provided template is used by at least one diagram + within this project, false otherwise +*/ +bool QETProject::usesTitleBlockTemplate(const TitleBlockTemplateLocation &location) { + // a diagram can only use a title block template embedded wihtin its parent project + if (location.parentProject() != this) return(false); + + foreach (Diagram *diagram, diagrams()) { + if (diagram -> usesTitleBlockTemplate(location.name())) { + return(true); + } + } + return(false); +} + /** Supprime tous les elements inutilises dans le projet @param handler Gestionnaire d'erreur @@ -1047,6 +1064,10 @@ void QETProject::addDiagram(Diagram *diagram) { this, SLOT(updateDiagramsFolioData()) ); + connect( + diagram, SIGNAL(usedTitleBlockTemplateChanged(const QString &)), + this, SLOT(usedTitleBlockTemplateChanged(const QString &)) + ); // ajoute le schema au projet diagrams_ << diagram; @@ -1175,6 +1196,14 @@ void QETProject::removeDiagramsTitleBlockTemplate(TitleBlockTemplatesCollection } } +/** + Handles the fact a digram changed the title block template it used + @param template_name Name of the template +*/ +void QETProject::usedTitleBlockTemplateChanged(const QString &template_name) { + emit(diagramUsedTemplate(embeddedTitleBlockTemplatesCollection(), template_name)); +} + /** Copie l'element integ_elmt dans la categorie target_cat en utilisant le gestionnaire handler ; en cas d'erreur, error_message est rempli. diff --git a/sources/qetproject.h b/sources/qetproject.h index 7820867c2..a485bfb07 100644 --- a/sources/qetproject.h +++ b/sources/qetproject.h @@ -108,6 +108,7 @@ class QETProject : public QObject { QString integrateElement(const QString &, MoveElementsHandler *, QString &); QString integrateTitleBlockTemplate(const TitleBlockTemplateLocation &, MoveTitleBlockTemplatesHandler *handler); bool usesElement(const ElementsLocation &); + bool usesTitleBlockTemplate(const TitleBlockTemplateLocation &); void cleanUnusedElements(MoveElementsHandler *); void cleanEmptyCategories(MoveElementsHandler *); bool projectWasModified(); @@ -126,12 +127,14 @@ class QETProject : public QObject { void projectInformationsChanged(QETProject *); void diagramAdded(QETProject *, Diagram *); void diagramRemoved(QETProject *, Diagram *); + void diagramUsedTemplate(TitleBlockTemplatesCollection *, const QString &); void readOnlyChanged(QETProject *, bool); private slots: void updateDiagramsFolioData(); void updateDiagramsTitleBlockTemplate(TitleBlockTemplatesCollection *, const QString &); void removeDiagramsTitleBlockTemplate(TitleBlockTemplatesCollection *, const QString &); + void usedTitleBlockTemplateChanged(const QString &); private: void setupTitleBlockTemplatesCollection(); diff --git a/sources/titleblock/templatelocation.cpp b/sources/titleblock/templatelocation.cpp index 84cbf1832..04fc41034 100644 --- a/sources/titleblock/templatelocation.cpp +++ b/sources/titleblock/templatelocation.cpp @@ -153,3 +153,12 @@ bool TitleBlockTemplateLocation::isReadOnly() const { if (!collection_) return(false); return(collection_ -> isReadOnly(name_)); } + +/** + @param location other location that should be compared to this one + @return true if locations are equal, false otherwise +*/ +bool TitleBlockTemplateLocation::operator==(const TitleBlockTemplateLocation &location) const { + return(location.collection_ == collection_ && location.name_ == name_); +} + diff --git a/sources/titleblock/templatelocation.h b/sources/titleblock/templatelocation.h index 340044f88..952678395 100644 --- a/sources/titleblock/templatelocation.h +++ b/sources/titleblock/templatelocation.h @@ -50,6 +50,7 @@ class TitleBlockTemplateLocation { QDomElement getTemplateXmlDescription() const; TitleBlockTemplate *getTemplate() const; bool isReadOnly() const; + bool operator==(const TitleBlockTemplateLocation &) const; // attributes private: