Remove the use of ElementsCollectionItem and ElementDefinition from qetelementeditor class

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4413 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2016-03-31 17:28:44 +00:00
parent 6d4d1535e5
commit 9a6764ae08
10 changed files with 243 additions and 158 deletions

View File

@@ -61,6 +61,7 @@ class ElementCollectionItem : public QObject
int row() const; int row() const;
virtual QString name(); virtual QString name();
virtual QString collectionName() const; virtual QString collectionName() const;
virtual QString collectionPath() const {return QString();}
virtual bool isDir() const; virtual bool isDir() const;
virtual bool isElement() const; virtual bool isElement() const;

View File

@@ -240,11 +240,9 @@ void ElementsCollectionWidget::editElement()
{ {
ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu); ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu);
if (!eci || if ( !(eci && eci->isElement()) ) return;
!eci->isElement() ||
(eci->type() != FileElementCollectionItem::Type)) return;
ElementsLocation location(static_cast<FileElementCollectionItem*>(eci)->collectionPath()); ElementsLocation location(eci->collectionPath());
QETApp *app = QETApp::instance(); QETApp *app = QETApp::instance();
app->openElementLocations(QList<ElementsLocation>() << location); app->openElementLocations(QList<ElementsLocation>() << location);

View File

@@ -22,6 +22,7 @@
#include "elementscollectioncache.h" #include "elementscollectioncache.h"
#include "elementfactory.h" #include "elementfactory.h"
#include "element.h" #include "element.h"
#include "qetxml.h"
// make this class usable with QVariant // make this class usable with QVariant
int ElementsLocation::MetaTypeId = qRegisterMetaType<ElementsLocation>("ElementsLocation"); int ElementsLocation::MetaTypeId = qRegisterMetaType<ElementsLocation>("ElementsLocation");
@@ -430,6 +431,24 @@ bool ElementsLocation::exist() const
} }
} }
/**
* @brief ElementsLocation::isWritable
* @return True if this element can be writable (can use set xml)
*/
bool ElementsLocation::isWritable() const
{
if (m_project)
return !m_project->isReadOnly();
else if (isFileSystem())
{
if (fileSystemPath().startsWith(QETApp::commonElementsDirN()))
return false;
else
return true;
}
return false;
}
/** /**
* @brief ElementsLocation::projectCollection * @brief ElementsLocation::projectCollection
* @return If this location represente a item in an embedded project collection, return this collection * @return If this location represente a item in an embedded project collection, return this collection
@@ -492,7 +511,7 @@ QDomElement ElementsLocation::xml() const
QFile file (m_file_system_path); QFile file (m_file_system_path);
QDomDocument docu; QDomDocument docu;
if (docu.setContent(&file)) if (docu.setContent(&file))
return docu.documentElement().cloneNode().toElement(); return docu.documentElement();
} }
else else
{ {
@@ -512,6 +531,51 @@ QDomElement ElementsLocation::xml() const
return QDomElement(); return QDomElement();
} }
/**
* @brief ElementsLocation::setXml
* Replace the current xml description by @xml_element;
* The document element of @xml_document must have tagname "definition" to be written
* This definition must be writable
* @param xml_element
* @return true if success
*/
bool ElementsLocation::setXml(const QDomDocument &xml_document) const
{
if (!isWritable())
return false;
if (xml_document.documentElement().tagName() != "definition")
{
qDebug() << "ElementsLocation::setXml : tag name of document element isn't 'definition'";
return false;
}
if (isFileSystem())
{
QString error;
QETXML::writeXmlFile(xml_document, fileSystemPath(), &error);
if (!error.isEmpty())
{
qDebug() << "ElementsLocation::setXml error : " << error;
return false;
}
else
return true;
}
else if (isProject() && exist())
{
QDomElement dom_element = xml();
QDomNode parent_node = dom_element.parentNode();
parent_node.removeChild(dom_element);
parent_node.appendChild(xml_document.documentElement().cloneNode(true));
return true;
}
return false;
}
/** /**
* @brief ElementsLocation::uuid * @brief ElementsLocation::uuid
* @return The uuid of the pointed element * @return The uuid of the pointed element

View File

@@ -63,11 +63,13 @@ class ElementsLocation
bool isFileSystem() const; bool isFileSystem() const;
bool isProject() const; bool isProject() const;
bool exist() const; bool exist() const;
bool isWritable() const;
XmlElementCollection *projectCollection() const; XmlElementCollection *projectCollection() const;
NamesList nameList(); NamesList nameList();
QDomElement xml() const; QDomElement xml() const;
bool setXml(const QDomDocument &xml_document) const;
QUuid uuid() const; QUuid uuid() const;
QIcon icon() const; QIcon icon() const;
QString name() const; QString name() const;

View File

@@ -102,6 +102,12 @@ QVariant XmlProjectElementCollectionItem::data(int column, int role)
} }
} }
void XmlProjectElementCollectionItem::clearData()
{
m_icon = QIcon();
ElementCollectionItem::clearData();
}
/** /**
* @brief XmlProjectElementCollectionItem::mimeData * @brief XmlProjectElementCollectionItem::mimeData
* @return The mimedata of this item * @return The mimedata of this item

View File

@@ -44,6 +44,7 @@ class XmlProjectElementCollectionItem : public ElementCollectionItem
virtual int type() const {return Type;} virtual int type() const {return Type;}
virtual QVariant data(int column, int role); virtual QVariant data(int column, int role);
virtual void clearData();
virtual QMimeData *mimeData(); virtual QMimeData *mimeData();
virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column) const; virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column) const;
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column); virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column);

View File

@@ -23,7 +23,6 @@
#include "customelementpart.h" #include "customelementpart.h"
#include "newelementwizard.h" #include "newelementwizard.h"
#include "elementitemeditor.h" #include "elementitemeditor.h"
#include "elementdefinition.h"
#include "elementdialog.h" #include "elementdialog.h"
#include "recentfiles.h" #include "recentfiles.h"
#include "qeticons.h" #include "qeticons.h"
@@ -107,17 +106,15 @@ QETElementEditor::~QETElementEditor() {
} }
/** /**
@param el Le nouvel emplacement de l'element edite * @brief QETElementEditor::setLocation
* The new location to edit
* @param el
*/ */
void QETElementEditor::setLocation(const ElementsLocation &el) { void QETElementEditor::setLocation(const ElementsLocation &el)
{
location_ = el; location_ = el;
opened_from_file = false; opened_from_file = false;
// modifie le mode lecture seule si besoin setReadOnly(!location_.isWritable());
ElementsCollectionItem *item = QETApp::collectionItem(location_);
bool must_be_read_only = item && !item -> isWritable();
if (isReadOnly() != must_be_read_only) {
setReadOnly(must_be_read_only);
}
slot_updateTitle(); slot_updateTitle();
} }
@@ -845,39 +842,20 @@ bool QETElementEditor::toFile(const QString &fn) {
return(writing); return(writing);
} }
/** /**
Enregistre l'element vers un emplacement * @brief QETElementEditor::toLocation
@param location Emplacement de l'element a enregistrer * Save the element to Location
@return true en cas de reussite, false sinon * @param location : location where we must save the current element
* @return true if succesfully saved
*/ */
bool QETElementEditor::toLocation(const ElementsLocation &location) { bool QETElementEditor::toLocation(const ElementsLocation &location)
ElementsCollectionItem *item = QETApp::collectionItem(location); {
ElementDefinition *element; if (!location.setXml(ce_scene->toXml()))
if (item) { {
// l'element existe deja QET::QetMessageBox::critical(this,
element = qobject_cast<ElementDefinition *>(item);
} else {
// l'element n'existe pas encore, on demande sa creation
element = QETApp::createElement(location);
}
if (!element) {
QET::QetMessageBox::critical(
this,
tr("Erreur", "message box title"), tr("Erreur", "message box title"),
tr("Impossible d'atteindre l'élément", "message box content") tr("Impossible d'enregistrer l'élément", "message box content"));
);
return(false);
}
// enregistre l'element
element -> setXml(ce_scene -> toXml().documentElement());
if (!element -> write()) {
QET::QetMessageBox::critical(
this,
tr("Erreur", "message box title"),
tr("Impossible d'enregistrer l'élément", "message box content")
);
return(false); return(false);
} }
@@ -1101,68 +1079,73 @@ void QETElementEditor::openElement(const QString &filepath) {
} }
/** /**
Recharge l'element edite * @brief QETElementEditor::slot_reload
* Reload the element from the file or location
*/ */
void QETElementEditor::slot_reload() { void QETElementEditor::slot_reload()
// s'il ya des modifications, on demande a l'utilisateur s'il est certain {
// de vouloir recharger //If user already edit the element, ask confirmation to reload
if (!ce_scene -> undoStack().isClean()) { if (!ce_scene -> undoStack().isClean())
QMessageBox::StandardButton answer = QET::QetMessageBox::question( {
this, QMessageBox::StandardButton answer = QET::QetMessageBox::question(this,
tr("Recharger l'élément", "dialog title"), tr("Recharger l'élément", "dialog title"),
tr("Vous avez efffectué des modifications sur cet élément. Si vous le rechargez, ces modifications seront perdues. Voulez-vous vraiment recharger l'élément ?", "dialog content"), tr("Vous avez efffectué des modifications sur cet élément. Si vous le rechargez, ces modifications seront perdues. Voulez-vous vraiment recharger l'élément ?", "dialog content"),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
QMessageBox::Cancel QMessageBox::Cancel);
);
if (answer != QMessageBox::Yes) return; if (answer != QMessageBox::Yes) return;
} }
// recharge l'element //Reload the element
if (opened_from_file) {
// l'element a ete ouvert a partir d'un chemin de fichier
ce_scene -> reset(); ce_scene -> reset();
if (opened_from_file)
fromFile(filename_); fromFile(filename_);
} else { else
// l'element a ete ouvert a partir d'un emplacement (ElementsLocation)
// il peut s'agir aussi bien d'un fichier que d'un element XML
if (ElementsCollectionItem *item = QETApp::collectionItem(location_)) {
item -> reload();
ce_scene -> reset();
fromLocation(location_); fromLocation(location_);
} }
}
}
/** /**
Enregistre l'element en cours d'edition. * @brief QETElementEditor::slot_save
Si le nom du fichier en cours n'est pas connu, cette methode equivaut a * Save the current editing element.
l'action "Enregistrer sous" * If the filepath or location is unknow, use save_as instead
@see slot_saveAs() * @return true if save with success
*/ */
bool QETElementEditor::slot_save() { bool QETElementEditor::slot_save()
{
// Check element befor writing // Check element befor writing
if (checkElement()) { if (checkElement())
// si on ne connait pas le nom du fichier en cours, enregistrer revient a enregistrer sous {
if (opened_from_file) { //If we don't know the name of the current file, use save as instead
if (filename_.isEmpty()) return(slot_saveAsFile()); if (opened_from_file)
// sinon on enregistre dans le nom de fichier connu {
if (filename_.isEmpty())
return(slot_saveAsFile());
//Else wa save to the file at filename_ path
bool result_save = toFile(filename_); bool result_save = toFile(filename_);
if (result_save) ce_scene -> undoStack().setClean(); if (result_save) ce_scene -> undoStack().setClean();
return(result_save); return(result_save);
} else { }
if (location_.isNull()) return(slot_saveAs()); else
// sinon on enregistre a l'emplacement connu {
if (location_.isNull())
return(slot_saveAs());
//Else save to the known location
bool result_save = toLocation(location_); bool result_save = toLocation(location_);
if (result_save) ce_scene -> undoStack().setClean(); if (result_save) ce_scene -> undoStack().setClean();
return(result_save); return(result_save);
} }
} }
QMessageBox::critical(this, tr("Echec de l'enregistrement"), tr("L'enregistrement à échoué,\nles conditions requises ne sont pas valides")); QMessageBox::critical(this, tr("Echec de l'enregistrement"), tr("L'enregistrement à échoué,\nles conditions requises ne sont pas valides"));
return false; return false;
} }
/** /**
Demande une localisation a l'utilisateur et enregistre l'element * @brief QETElementEditor::slot_saveAs
* Ask a location to user and save the current edited element
* to this location
* @return true if save with success
*/ */
bool QETElementEditor::slot_saveAs() { bool QETElementEditor::slot_saveAs() {
// Check element befor writing // Check element befor writing
@@ -1186,7 +1169,9 @@ bool QETElementEditor::slot_saveAs() {
} }
/** /**
Demande un nom de fichier a l'utilisateur et enregistre l'element * @brief QETElementEditor::slot_saveAsFile
* Ask a file to user and save the current edited element to this file
* @return true if save with success
*/ */
bool QETElementEditor::slot_saveAsFile() { bool QETElementEditor::slot_saveAsFile() {
// Check element befor writing // Check element befor writing
@@ -1444,53 +1429,48 @@ QString QETElementEditor::getOpenElementFileName(QWidget *parent, const QString
} }
/** /**
@param location Emplacement de l'element a editer * @brief QETElementEditor::fromLocation
* Location of the element to edit
* @param location
*/ */
void QETElementEditor::fromLocation(const ElementsLocation &location) { void QETElementEditor::fromLocation(const ElementsLocation &location)
{
// l'element doit exister if (!location.isElement())
ElementsCollectionItem *item = QETApp::collectionItem(location); {
ElementDefinition *element = 0; QET::QetMessageBox::critical(this,
if (!item) {
QET::QetMessageBox::critical(
this,
tr("Élément inexistant.", "message box title"), tr("Élément inexistant.", "message box title"),
tr("L'élément n'existe pas.", "message box content") tr("Le chemin virtuel choisi ne correspond pas à un élément.", "message box content"));
); return;
}
if (!location.exist())
{
QET::QetMessageBox::critical(this,
tr("Élément inexistant.", "message box title"),
tr("L'élément n'existe pas.", "message box content"));
return; return;
} }
if (!item -> isElement() || !(element = qobject_cast<ElementDefinition *>(item)) || element -> isNull()) { //The file must be an xml document
QET::QetMessageBox::critical(
this,
tr("Élément inexistant.", "message box title"),
tr("Le chemin virtuel choisi ne correspond pas à un élément.", "message box content")
);
return;
}
// le fichier doit etre un document XML
QDomDocument document_xml; QDomDocument document_xml;
QDomNode node = document_xml.importNode(element -> xml(), true); QDomNode node = document_xml.importNode(location.xml(), true);
document_xml.appendChild(node); document_xml.appendChild(node);
// chargement de l'element //Load the element
ce_scene -> fromXml(document_xml); ce_scene -> fromXml(document_xml);
slot_createPartsList(); slot_createPartsList();
// gestion de la lecture seule //location is read only
if (!element -> isWritable()) { if (!location.isWritable())
QET::QetMessageBox::warning( {
this, QET::QetMessageBox::warning(this,
tr("Édition en lecture seule", "message box title"), tr("Édition en lecture seule", "message box title"),
tr("Vous n'avez pas les privilèges nécessaires pour modifier cet élement. Il sera donc ouvert en lecture seule.", "message box content") tr("Vous n'avez pas les privilèges nécessaires pour modifier cet élement. Il sera donc ouvert en lecture seule.", "message box content"));
);
setReadOnly(true); setReadOnly(true);
} else { }
else {
setReadOnly(false); setReadOnly(false);
} }
// memorise le fichier
setLocation(location); setLocation(location);
slot_updateMenus(); slot_updateMenus();
} }
@@ -1525,38 +1505,35 @@ void QETElementEditor::pasteFromFile() {
} }
/** /**
Denande un element a l'utilisateur, met son contenu dans le presse-papiers, * @brief QETElementEditor::pasteFromElement
et appelle ElementView::PasteInArea * Ask an element to user, copy the xml definition of the element
* to the clipboard and call ElementView::PasteInArea
*/ */
void QETElementEditor::pasteFromElement() { void QETElementEditor::pasteFromElement()
// demande le chemin virtuel de l'element a ouvrir a l'utilisateur {
//Ask for a location
ElementsLocation location = ElementDialog::getOpenElementLocation(this); ElementsLocation location = ElementDialog::getOpenElementLocation(this);
if (location.isNull()) return; if (location.isNull())
return;
// verifie l'existence de l'element choisi if (!location.isElement())
ElementsCollectionItem *item = QETApp::collectionItem(location); {
ElementDefinition *element = 0; QET::QetMessageBox::critical(this,
if (!item) {
QET::QetMessageBox::critical(
this,
tr("Élément inexistant.", "message box title"), tr("Élément inexistant.", "message box title"),
tr("L'élément n'existe pas.", "message box content") tr("Le chemin virtuel choisi ne correspond pas à un élément.", "message box content"));
); return;
}
if (!location.exist())
{
QET::QetMessageBox::critical(this,
tr("Élément inexistant.", "message box title"),
tr("L'élément n'existe pas.", "message box content"));
return; return;
} }
if (!item -> isElement() || !(element = qobject_cast<ElementDefinition *>(item)) || element -> isNull()) { //Create an xml document from the location xml
QET::QetMessageBox::critical(
this,
tr("Élément inexistant.", "message box title"),
tr("Le chemin virtuel choisi ne correspond pas à un élément.", "message box content")
);
return;
}
// creation d'un document XML a partir de la description XML de l'element
QDomDocument document_xml; QDomDocument document_xml;
QDomNode node = document_xml.importNode(element -> xml(), true); QDomNode node = document_xml.importNode(location.xml(), true);
document_xml.appendChild(node); document_xml.appendChild(node);
copyAndPasteXml(document_xml); copyAndPasteXml(document_xml);

View File

@@ -104,7 +104,7 @@ class QETElementEditor : public QETMainWindow {
void fromFile(const QString &); void fromFile(const QString &);
void fromLocation(const ElementsLocation &); void fromLocation(const ElementsLocation &);
bool toFile(const QString &); bool toFile(const QString &);
bool toLocation(const ElementsLocation &); bool toLocation(const ElementsLocation &location);
bool isEditing(const ElementsLocation &); bool isEditing(const ElementsLocation &);
bool isEditing(const QString &); bool isEditing(const QString &);
ElementScene *elementScene() const; ElementScene *elementScene() const;

View File

@@ -220,3 +220,37 @@ QDomElement QETXML::fileSystemElementToXmlCollectionElement(QDomDocument &docume
else else
return QDomElement(); return QDomElement();
} }
/**
* @brief QETXML::writeXmlFile
* Export an XML document to an UTF-8 text file indented with 4 spaces, with LF end of lines and no BOM.
* @param xml_document : An XML document to be exported
* @param file_path : Path to the file to be written
* @param error_message : If non-zero, will contain an error message explaining what happened when this function returns false.
* @return false if an error occured, true otherwise
*/
bool QETXML::writeXmlFile(const QDomDocument &xml_document, const QString &file_path, QString *error_message)
{
QFile file(file_path);
// Note: we do not set QIODevice::Text to avoid generating CRLF end of lines
bool file_opening = file.open(QIODevice::WriteOnly);
if (!file_opening)
{
if (error_message)
{
*error_message = QString(QObject::tr("Impossible d'ouvrir le fichier %1 en écriture, erreur %2 rencontrée.",
"error message when attempting to write an XML file")
).arg(file_path).arg(file.error());
}
return(false);
}
QTextStream out(&file);
out.setCodec("UTF-8");
out.setGenerateByteOrderMark(false);
out << xml_document.toString(4);
file.close();
return(true);
}

View File

@@ -39,6 +39,8 @@ namespace QETXML
QDomElement fileSystemDirToXmlCollectionDir (QDomDocument &document, const QDir &dir, QString rename = QString()); QDomElement fileSystemDirToXmlCollectionDir (QDomDocument &document, const QDir &dir, QString rename = QString());
QDomElement fileSystemElementToXmlCollectionElement (QDomDocument &document, QFile &file, QString rename = QString()); QDomElement fileSystemElementToXmlCollectionElement (QDomDocument &document, QFile &file, QString rename = QString());
bool writeXmlFile(const QDomDocument &xml_document, const QString &file_path, QString *error_message = nullptr);
} }
#endif // QETXML_H #endif // QETXML_H