Allow user to select common or custom title block as default title block for a new project / diagram in project

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3912 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun 2015-04-17 11:36:28 +00:00
parent 6ec0c1dbbc
commit cf7ffb5452
11 changed files with 207 additions and 72 deletions

View File

@ -244,6 +244,7 @@ TitleBlockProperties BorderTitleBlock::exportTitleBlock() {
ip.template_name = titleBlockTemplateName();
ip.display_at = m_edge;
ip.context = additional_fields_;
ip.collection = QET::QetCollection::Embendded;
return(ip);
}

View File

@ -729,3 +729,41 @@ bool QET::eachStrIsEqual(const QStringList &qsl) {
}
return true;
}
/**
* @brief QET::qetCollectionToString
* @param c QetCollection value to convert
* @return The QetCollection enum value converted to a QString
*/
QString QET::qetCollectionToString(const QET::QetCollection &c)
{
switch (c)
{
case Common :
return "common";
case Custom :
return "custom";
case Embendded :
return "embendded";
default:
return "common";
}
}
/**
* @brief QET::qetCollectionFromString
* @param str string to convert
* @return The corresponding QetCollection value from a string.
* If the string don't match anything, we return the failsafe value QetCollection::Common
*/
QET::QetCollection QET::qetCollectionFromString(const QString &str)
{
if (str == "common")
return QetCollection::Common;
else if (str == "custom")
return QetCollection::Custom;
else if (str == "embendded")
return QetCollection::Embendded;
else
return QetCollection::Common;
}

View File

@ -128,6 +128,16 @@ namespace QET {
RelativeToTotalLength, ///< the length is just a fraction of the total available length
RelativeToRemainingLength ///< the length is just a fraction of the length that is still available when other types of lengths have been removed
};
///Enum used to specify the origin of a collection of thing (title block, element etc...)
enum QetCollection {
Common, ///< From common collection
Custom, ///< From user collection
Embendded ///< From an embeddded collection (a project for exemple)
};
QString qetCollectionToString (const QetCollection &c);
QetCollection qetCollectionFromString (const QString &str);
bool lineContainsPoint(const QLineF &, const QPointF &);
bool orthogonalProjection(const QPointF &, const QLineF &, QPointF * = 0);

View File

@ -381,6 +381,7 @@ TitleBlockTemplatesFilesCollection *QETApp::commonTitleBlockTemplatesCollection(
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(QETAPP_COMMON_TBT_PROTOCOL);
common_tbt_collection_ -> setCollection(QET::QetCollection::Common);
}
return(common_tbt_collection_);
}
@ -394,6 +395,7 @@ TitleBlockTemplatesFilesCollection *QETApp::customTitleBlockTemplatesCollection(
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(QETAPP_CUSTOM_TBT_PROTOCOL);
custom_tbt_collection_ -> setCollection(QET::QetCollection::Custom);
}
return(custom_tbt_collection_);
}

View File

@ -455,8 +455,11 @@ void QETProject::setDefaultTitleBlockProperties(const TitleBlockProperties &titl
//Integrate the title block in this project
if (!titleblock.template_name.isEmpty())
{
TitleBlockTemplatesFilesCollection *collection = nullptr;
collection = titleblock.collection == QET::QetCollection::Common ? QETApp::commonTitleBlockTemplatesCollection() :
QETApp::customTitleBlockTemplatesCollection();
QScopedPointer<IntegrationMoveTitleBlockTemplatesHandler> m(new IntegrationMoveTitleBlockTemplatesHandler);
integrateTitleBlockTemplate(QETApp::commonTitleBlockTemplatesCollection()->location(titleblock.template_name), m.data());
integrateTitleBlockTemplate(collection -> location(titleblock.template_name), m.data());
}
}

View File

@ -66,6 +66,23 @@ void TitleBlockTemplatesCollection::setProtocol(const QString &protocol) {
if (!protocol.isEmpty()) protocol_ = protocol;
}
/**
* @brief TitleBlockTemplatesCollection::collection
* @return the collection where is stored this collection.
*/
QET::QetCollection TitleBlockTemplatesCollection::collection() const {
return m_collection;
}
/**
* @brief TitleBlockTemplatesCollection::setCollection
* Set the storage of this collection
* @param c
*/
void TitleBlockTemplatesCollection::setCollection(QET::QetCollection c) {
m_collection = c;
}
/**
@return the project this collection is affiliated to, or 0 if this
collection is not related to any project.
@ -96,6 +113,7 @@ TitleBlockTemplatesProjectCollection::TitleBlockTemplatesProjectCollection(QETPr
TitleBlockTemplatesCollection(parent),
project_(project)
{
m_collection = QET::QetCollection::Embendded;
}
/**

View File

@ -21,6 +21,7 @@
#include <QtCore>
#include <QtXml>
#include "templatelocation.h"
#include "qet.h"
class TitleBlockTemplate;
class QETProject;
@ -55,6 +56,8 @@ class TitleBlockTemplatesCollection : public QObject {
virtual void setTitle(const QString &);
virtual QString protocol() const;
virtual void setProtocol(const QString &);
virtual QET::QetCollection collection () const;
virtual void setCollection (QET::QetCollection);
virtual QETProject *parentProject();
virtual QList<TitleBlockTemplateLocation> templatesLocations();
@ -68,6 +71,7 @@ class TitleBlockTemplatesCollection : public QObject {
QString title_;
/// Protocol used to designate this collection
QString protocol_;
QET::QetCollection m_collection;
/// Already parsed embedded titleblock templates
QHash<QString, TitleBlockTemplate *> titleblock_templates_;
};

View File

@ -26,7 +26,8 @@
TitleBlockProperties::TitleBlockProperties() :
date(),
useDate(UseDateValue),
display_at(Qt::BottomEdge)
display_at(Qt::BottomEdge),
collection (QET::QetCollection::Common)
{
}
@ -49,7 +50,8 @@ bool TitleBlockProperties::operator==(const TitleBlockProperties &ip) {
ip.folio == folio &&\
ip.template_name == template_name &&\
ip.context == context &&\
ip.display_at == display_at
ip.display_at == display_at &&\
ip.collection == collection
);
}
@ -73,8 +75,10 @@ void TitleBlockProperties::toXml(QDomElement &e) const {
e.setAttribute("folio", folio);
e.setAttribute("date", exportDate());
e.setAttribute("displayAt", (display_at == Qt::BottomEdge? "bottom" : "right"));
if (!template_name.isEmpty()) {
if (!template_name.isEmpty())
{
e.setAttribute("titleblocktemplate", template_name);
e.setAttribute("titleblocktemplateCollection", QET::qetCollectionToString(collection));
}
if (context.keys().count()) {
@ -97,8 +101,12 @@ void TitleBlockProperties::fromXml(const QDomElement &e) {
if (e.hasAttribute("date")) setDateFromString(e.attribute("date"));
if (e.hasAttribute("displayAt")) display_at = (e.attribute("displayAt") == "bottom" ? Qt::BottomEdge : Qt::RightEdge);
// reads the template used to render the title block
if (e.hasAttribute("titleblocktemplate")) template_name = e.attribute("titleblocktemplate");
// reads the template used to render the title block
if (e.hasAttribute("titleblocktemplate"))
{
template_name = e.attribute("titleblocktemplate");
collection = QET::qetCollectionFromString(e.attribute("titleblocktemplateCollection"));
}
// reads the additional fields used to fill the title block
context.clear();
@ -121,6 +129,7 @@ void TitleBlockProperties::toSettings(QSettings &settings, const QString &prefix
settings.setValue(prefix + "date", exportDate());
settings.setValue(prefix + "displayAt", (display_at == Qt::BottomEdge? "bottom" : "right"));
settings.setValue(prefix + "titleblocktemplate", template_name.isEmpty()? QString() : template_name);
settings.setValue(prefix + "titleblocktemplateCollection", QET::qetCollectionToString(collection));
context.toSettings(settings, prefix + "properties");
}
@ -137,6 +146,7 @@ void TitleBlockProperties::fromSettings(QSettings &settings, const QString &pref
setDateFromString(settings.value(prefix + "date").toString());
display_at = (settings.value(prefix + "displayAt", QVariant("bottom")).toString() == "bottom" ? Qt::BottomEdge : Qt::RightEdge);
template_name = settings.value(prefix + "titleblocktemplate").toString();
collection = QET::qetCollectionFromString(settings.value(prefix + "titleblocktemplateCollection").toString());
context.fromSettings(settings, prefix + "properties");
}

View File

@ -19,6 +19,7 @@
#define TITLEBLOCK_PROPERTIES_H
#include <QtXml>
#include "diagramcontext.h"
#include "qet.h"
/**
This class provides a container for the properties of a particular title
block, i.e. title, author, date, filename, folio, template, custom
@ -56,6 +57,7 @@ class TitleBlockProperties {
QString template_name; ///< Name of the template used to render the title block - an empty string means "the default template provided by the application"
DiagramContext context; ///< Container for the additional, user-defined fields
Qt::Edge display_at; ///< Edge to display the titleblock
QET::QetCollection collection; ///<Specify the location of the title block
private:
QString exportDate() const;

View File

@ -31,8 +31,7 @@
*/
TitleBlockPropertiesWidget::TitleBlockPropertiesWidget(const TitleBlockProperties &titleblock, bool current_date, QWidget *parent) :
QWidget(parent),
ui(new Ui::TitleBlockPropertiesWidget),
m_tbt_collection(nullptr)
ui(new Ui::TitleBlockPropertiesWidget)
{
ui->setupUi(this);
initDialog(current_date);
@ -49,12 +48,32 @@ TitleBlockPropertiesWidget::TitleBlockPropertiesWidget(const TitleBlockPropertie
*/
TitleBlockPropertiesWidget::TitleBlockPropertiesWidget(TitleBlockTemplatesCollection *tbt_collection, const TitleBlockProperties &titleblock, bool current_date, QWidget *parent) :
QWidget(parent),
ui(new Ui::TitleBlockPropertiesWidget),
m_tbt_collection(nullptr)
ui(new Ui::TitleBlockPropertiesWidget)
{
ui->setupUi(this);
initDialog(current_date);
setTitleBlockTemplatesCollection(tbt_collection);
addCollection(tbt_collection);
updateTemplateList();
setProperties(titleblock);
}
/**
* @brief TitleBlockPropertiesWidget::TitleBlockPropertiesWidget
* Default constructor with several template collection
* @param tbt_collection template list
* @param titleblock properties to edit
* @param current_date if true, display the radio button "current date"
* @param parent parent widget
*/
TitleBlockPropertiesWidget::TitleBlockPropertiesWidget(QList<TitleBlockTemplatesCollection *> tbt_collection, const TitleBlockProperties &titleblock, bool current_date, QWidget *parent) :
QWidget(parent),
ui(new Ui::TitleBlockPropertiesWidget)
{
ui->setupUi(this);
initDialog(current_date);
foreach (TitleBlockTemplatesCollection *c, tbt_collection)
addCollection(c);
updateTemplateList();
setProperties(titleblock);
}
@ -108,11 +127,14 @@ void TitleBlockPropertiesWidget::setProperties(const TitleBlockProperties &prope
}
} //About date
if (!properties.template_name.isEmpty()) {
int matching_index = ui -> m_tbt_cb -> findData (properties.template_name);
if (matching_index != -1)
ui -> m_tbt_cb -> setCurrentIndex(matching_index);
//Set the current titleblock if any
int index = 0;
if (!properties.template_name.isEmpty())
{
index = getIndexFor(properties.template_name, properties.collection);
if (index == -1) index = 0;
}
ui -> m_tbt_cb -> setCurrentIndex(index);
m_dcw -> setContext(properties.context);
}
@ -145,6 +167,7 @@ TitleBlockProperties TitleBlockPropertiesWidget::properties() const {
if (!currentTitleBlockTemplateName().isEmpty())
{
prop.template_name = currentTitleBlockTemplateName();
prop.collection = m_map_index_to_collection_type.at(ui->m_tbt_cb->currentIndex());
}
prop.context = m_dcw -> context();
@ -182,46 +205,14 @@ QString TitleBlockPropertiesWidget::currentTitleBlockTemplateName() const {
}
/**
* @brief TitleBlockPropertiesWidget::setCurrentTitleBlockTemplateName
* set the current title block "name", if "name" doesn't match, this method do nothing
* @param name
*/
void TitleBlockPropertiesWidget::setCurrentTitleBlockTemplateName (const QString &name) {
int index = ui -> m_tbt_cb -> findData(name);
if (index != -1)
ui -> m_tbt_cb -> setCurrentIndex(index);
}
/**
* @brief TitleBlockPropertiesWidget::setTitleBlockTemplatesCollection
* Set the collection of title block
* @brief TitleBlockPropertiesWidget::addCollection
* add a collection of title block available in the combo box
* @param tbt_collection
*/
void TitleBlockPropertiesWidget::setTitleBlockTemplatesCollection(TitleBlockTemplatesCollection *tbt_collection) {
if (!tbt_collection) return;
setTitleBlockTemplatesVisible(true);
if (m_tbt_collection && tbt_collection != m_tbt_collection) {
// forget any connection with the previous collection
disconnect(m_tbt_collection, 0, this, 0);
}
m_tbt_collection = tbt_collection;
updateTemplateList();
connect(m_tbt_collection, SIGNAL(changed(TitleBlockTemplatesCollection*,QString)), this, SLOT(updateTemplateList()));
}
/**
* @brief TitleBlockPropertiesWidget::setTitleBlockTemplatesList
* Set the list of title block used.
* Fill the combo box of title box with each name of title block.
* @param tbt
*/
void TitleBlockPropertiesWidget::setTitleBlockTemplatesList(const QStringList &tbt) {
ui -> m_tbt_cb ->clear();
ui -> m_tbt_cb -> addItem(QET::Icons::TitleBlock, tr("Modèle par défaut"));
foreach (QString tbt_name, tbt) {
ui -> m_tbt_cb -> addItem(QET::Icons::TitleBlock, tbt_name, tbt_name);
}
void TitleBlockPropertiesWidget::addCollection(TitleBlockTemplatesCollection *tbt_collection)
{
if (!tbt_collection || m_tbt_collection_list.contains(tbt_collection)) return;
m_tbt_collection_list << tbt_collection;
}
/**
@ -247,7 +238,24 @@ void TitleBlockPropertiesWidget::initDialog(const bool &current_date) {
m_tbt_menu -> addAction(m_tbt_duplicate);
ui -> m_tbt_pb -> setMenu(m_tbt_menu);
connect(ui->m_tbt_cb, SIGNAL(currentIndexChanged(QString)), this, SLOT(changeCurrentTitleBlockTemplate(QString)));
connect(ui->m_tbt_cb, SIGNAL(currentIndexChanged(int)), this, SLOT(changeCurrentTitleBlockTemplate(int)));
}
/**
* @brief TitleBlockPropertiesWidget::getIndexFor
* Find the index of the combo box for the title block @tbt_name available on the collection @collection
* @param tbt_name : title block name
* @param collection : title block collection
* @return the index of the title block or -1 if no match
*/
int TitleBlockPropertiesWidget::getIndexFor(const QString &tbt_name, const QET::QetCollection collection) const
{
for (int i = 0; i<ui->m_tbt_cb->count(); i++) {
if (ui->m_tbt_cb->itemData(i).toString() == tbt_name)
if (m_map_index_to_collection_type.at(i) == collection)
return i;
}
return -1;
}
void TitleBlockPropertiesWidget::editCurrentTitleBlockTemplate() {
@ -260,33 +268,70 @@ void TitleBlockPropertiesWidget::duplicateCurrentTitleBlockTemplate() {
/**
* @brief TitleBlockPropertiesWidget::updateTemplateList
* Update the title block template list
* Update the title block template list available in the combo box
*/
void TitleBlockPropertiesWidget::updateTemplateList() {
if (!m_tbt_collection) return;
void TitleBlockPropertiesWidget::updateTemplateList()
{
ui -> m_tbt_cb ->clear();
QString current_template_name = currentTitleBlockTemplateName();
setTitleBlockTemplatesList(m_tbt_collection -> templates());
setCurrentTitleBlockTemplateName(current_template_name);
if (m_tbt_collection_list.isEmpty())
{
setTitleBlockTemplatesVisible(false);
return;
}
setTitleBlockTemplatesVisible(true);
//Add the default title block
m_map_index_to_collection_type.clear();
m_map_index_to_collection_type.append(QET::QetCollection::Common);
ui -> m_tbt_cb -> addItem(QET::Icons::QETLogo, tr("Modèle par défaut"));
//Add every title block stored in m_tbt_collection_list
foreach (TitleBlockTemplatesCollection *tbt_c, m_tbt_collection_list)
{
QIcon icon;
QET::QetCollection qc = tbt_c -> collection();
if (qc == QET::QetCollection::Common)
icon = QET::Icons::QETLogo;
else if (qc == QET::QetCollection::Custom)
icon = QET::Icons::Home;
else if (qc == QET::QetCollection::Embendded)
icon = QET::Icons::TitleBlock;
foreach(QString tbt_name, tbt_c -> templates())
{
m_map_index_to_collection_type.append(qc);
ui -> m_tbt_cb -> addItem(icon, tbt_name, tbt_name);
}
}
}
/**
* @brief TitleBlockPropertiesWidget::changeCurrentTitleBlockTemplate
* Load the additionnal field of title block "text"
*/
void TitleBlockPropertiesWidget::changeCurrentTitleBlockTemplate(QString name) {
// delete all entry
void TitleBlockPropertiesWidget::changeCurrentTitleBlockTemplate(int index)
{
m_dcw -> clear();
// get template
TitleBlockTemplate *tpl = m_tbt_collection -> getTemplate( name );
QET::QetCollection qc = m_map_index_to_collection_type.at(index);
TitleBlockTemplatesCollection *collection = nullptr;
foreach (TitleBlockTemplatesCollection *c, m_tbt_collection_list)
if (c -> collection() == qc)
collection = c;
if (!collection) return;
// get template
TitleBlockTemplate *tpl = collection -> getTemplate(ui -> m_tbt_cb -> currentText());
if(tpl != 0) {
// get all template fields
// get all template fields
QStringList fields = tpl -> listOfVariables();
// set fields to additional_fields_ widget
// set fields to additional_fields_ widget
DiagramContext templateContext;
for(int i =0; i<fields.count(); i++)
templateContext.addValue(fields.at(i), "");
m_dcw->setContext(templateContext);
m_dcw -> setContext(templateContext);
}
}

View File

@ -21,6 +21,7 @@
#include <QWidget>
#include "titleblockproperties.h"
#include "diagramcontextwidget.h"
#include "qet.h"
class QMenu;
class TitleBlockTemplatesCollection;
@ -36,6 +37,7 @@ class TitleBlockPropertiesWidget : public QWidget
public:
explicit TitleBlockPropertiesWidget(const TitleBlockProperties &titleblock = TitleBlockProperties(), bool current_date = false, QWidget *parent = 0);
explicit TitleBlockPropertiesWidget(TitleBlockTemplatesCollection *tbt_collection, const TitleBlockProperties &titleblock = TitleBlockProperties(), bool current_date = false, QWidget *parent = 0);
explicit TitleBlockPropertiesWidget(QList <TitleBlockTemplatesCollection *> tbt_collection, const TitleBlockProperties &titleblock = TitleBlockProperties(), bool current_date = false, QWidget *parent = 0);
~TitleBlockPropertiesWidget();
void setProperties(const TitleBlockProperties &properties);
@ -43,19 +45,18 @@ class TitleBlockPropertiesWidget : public QWidget
void setTitleBlockTemplatesVisible(const bool &visible);
void setReadOnly (const bool &ro);
void setTitleBlockTemplatesCollection(TitleBlockTemplatesCollection *tbt_collection);
private:
void addCollection (TitleBlockTemplatesCollection *tbt_collection);
QString currentTitleBlockTemplateName () const;
void setCurrentTitleBlockTemplateName (const QString &name);
void setTitleBlockTemplatesList(const QStringList &tbt);
void initDialog(const bool &current_date);
int getIndexFor (const QString &tbt_name, const QET::QetCollection collection) const;
private slots:
void editCurrentTitleBlockTemplate();
void duplicateCurrentTitleBlockTemplate();
void updateTemplateList();
void changeCurrentTitleBlockTemplate(QString name);
void changeCurrentTitleBlockTemplate(int);
void on_m_date_now_pb_clicked();
signals:
@ -64,9 +65,10 @@ class TitleBlockPropertiesWidget : public QWidget
private:
Ui::TitleBlockPropertiesWidget *ui;
DiagramContextWidget *m_dcw;
TitleBlockTemplatesCollection *m_tbt_collection;
QAction *m_tbt_edit, *m_tbt_duplicate;
QMenu *m_tbt_menu;
QList <TitleBlockTemplatesCollection *> m_tbt_collection_list;
QList <QET::QetCollection> m_map_index_to_collection_type;
};
#endif // TITLEBLOCKPROPERTIESWIDGET_H