mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-31 16:50:52 +01:00
Improve the way how an element is updated in the new element panel.
Now Qet only use the new embbeded collection (XmlElementCollection). No need to reload the old element panel for add a new element created by the new element panel Elements are always imported to the embbeded collection of a project git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4468 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -142,6 +142,30 @@ ElementCollectionItem *ElementCollectionItem::lastItemForPath(const QString &pat
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementCollectionItem::itemAtPath
|
||||
* @param path
|
||||
* @return the item at path or nullptr if doesn't exist
|
||||
*/
|
||||
ElementCollectionItem *ElementCollectionItem::itemAtPath(const QString &path)
|
||||
{
|
||||
QStringList str_list = path.split("/");
|
||||
if (str_list.isEmpty()) return nullptr;
|
||||
|
||||
ElementCollectionItem *match_eci = this;
|
||||
foreach (QString str, str_list) {
|
||||
ElementCollectionItem *eci = match_eci->childWithCollectionName(str);
|
||||
if (!eci) {
|
||||
return nullptr;
|
||||
}
|
||||
else {
|
||||
match_eci = eci;
|
||||
}
|
||||
}
|
||||
|
||||
return match_eci;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementCollectionItem::rowForInsertItem
|
||||
* Return the row for insert a new child item to this item with name @collection_name.
|
||||
@@ -396,22 +420,3 @@ void ElementCollectionItem::setBackgroundColor(Qt::GlobalColor color, bool show)
|
||||
m_bg_color = color;
|
||||
m_show_bg_color = show;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementCollectionItem::canRemoveContent
|
||||
* @return true if this item can remove the content that he represent
|
||||
* By default return false.
|
||||
*/
|
||||
bool ElementCollectionItem::canRemoveContent() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementCollectionItem::removeContent
|
||||
* Remove the content that he represent this item (a directory or an element).
|
||||
* This method do nothing and return false. Inherit it, to handle removing
|
||||
* @return true if the content was successfully removed
|
||||
*/
|
||||
bool ElementCollectionItem::removeContent() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ class ElementCollectionItem : public QObject
|
||||
ElementCollectionItem *child(int row) const;
|
||||
ElementCollectionItem *childWithCollectionName(QString name) const;
|
||||
ElementCollectionItem *lastItemForPath(const QString &path, QString &newt_item);
|
||||
ElementCollectionItem *itemAtPath(const QString &path);
|
||||
int rowForInsertItem(const QString &collection_name);
|
||||
virtual void insertNewItem(const QString &collection_name);
|
||||
int childCount() const;
|
||||
@@ -72,10 +73,6 @@ class ElementCollectionItem : public QObject
|
||||
int indexOfChild(ElementCollectionItem *child) const;
|
||||
void setBackgroundColor(Qt::GlobalColor color, bool show);
|
||||
|
||||
|
||||
virtual bool canRemoveContent();
|
||||
virtual bool removeContent();
|
||||
|
||||
signals:
|
||||
void beginInsertRows(ElementCollectionItem *parent, int first, int last);
|
||||
void endInsertRows();
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "fileelementcollectionitem.h"
|
||||
#include "xmlprojectelementcollectionitem.h"
|
||||
#include "qetproject.h"
|
||||
#include "xmlelementcollection.h"
|
||||
|
||||
/**
|
||||
* @brief ElementsCollectionModel::ElementsCollectionModel
|
||||
@@ -273,7 +274,7 @@ void ElementsCollectionModel::addCustomCollection()
|
||||
|
||||
/**
|
||||
* @brief ElementsCollectionModel::addProject
|
||||
* Add @project to the disalyed collection
|
||||
* Add @project to the displayed collection
|
||||
* @param project
|
||||
* @return true if project was successfully added. If project is already
|
||||
* handled, return false.
|
||||
@@ -288,21 +289,27 @@ bool ElementsCollectionModel::addProject(QETProject *project)
|
||||
XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem(project, m_root_item);
|
||||
bool r = m_root_item->insertChild(row, xpeci);
|
||||
endInsertRows();
|
||||
connect(project, &QETProject::elementIntegratedToCollection, this, &ElementsCollectionModel::elementIntegratedToCollection);
|
||||
|
||||
connect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection);
|
||||
connect(project->embeddedElementCollection(), &XmlElementCollection::elementChanged, this, &ElementsCollectionModel::updateItem);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementsCollectionModel::removeProject
|
||||
* Remove @project from this model
|
||||
* @param project
|
||||
* @return true if the project was successfully removed, false if not (or project doesn't managed)
|
||||
*/
|
||||
bool ElementsCollectionModel::removeProject(QETProject *project)
|
||||
{
|
||||
if (!m_project_list.contains(project)) return false;
|
||||
|
||||
int row = m_project_list.indexOf(project);
|
||||
if (removeRows(row, 1, QModelIndex()))
|
||||
{
|
||||
if (removeRows(row, 1, QModelIndex())) {
|
||||
m_project_list.removeOne(project);
|
||||
disconnect(project, &QETProject::elementIntegratedToCollection, this, &ElementsCollectionModel::elementIntegratedToCollection);
|
||||
disconnect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection);
|
||||
connect(project->embeddedElementCollection(), &XmlElementCollection::elementChanged, this, &ElementsCollectionModel::updateItem);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
@@ -339,24 +346,73 @@ XmlProjectElementCollectionItem *ElementsCollectionModel::itemForProject(QETProj
|
||||
* @brief ElementsCollectionModel::elementAddedToEmbeddedCollection
|
||||
* When an element is added to embedded collection of a project,
|
||||
* this method create and display the new element
|
||||
* @param project -The project where new element was added.
|
||||
* @param path -The path of the new element in the embedded collection of project
|
||||
* @param path -The path of the new element in the embedded collection of a project
|
||||
*/
|
||||
void ElementsCollectionModel::elementIntegratedToCollection(QETProject *project, QString path)
|
||||
void ElementsCollectionModel::elementIntegratedToCollection (QString path)
|
||||
{
|
||||
XmlProjectElementCollectionItem *xpeci = itemForProject(project);
|
||||
if (!xpeci) return;
|
||||
QObject *object = sender();
|
||||
XmlElementCollection *collection = static_cast<XmlElementCollection *> (object);
|
||||
if (!collection) return;
|
||||
|
||||
QString collection_name;
|
||||
ElementCollectionItem *eci = xpeci->lastItemForPath(path, collection_name);
|
||||
if (!eci) return;
|
||||
QETProject *project = nullptr;
|
||||
|
||||
int new_row = eci->rowForInsertItem(collection_name);
|
||||
if (new_row <= -1) return;
|
||||
QModelIndex parent_index = createIndex(eci->row(), 0, eci);
|
||||
beginInsertRows(parent_index, new_row, new_row);
|
||||
eci->insertNewItem(collection_name);
|
||||
endInsertRows();
|
||||
//Get the owner project of the collection
|
||||
foreach (QETProject *prj, m_project_list) {
|
||||
if (prj->embeddedElementCollection() == collection) {
|
||||
project = prj;
|
||||
}
|
||||
}
|
||||
|
||||
if (project) {
|
||||
XmlProjectElementCollectionItem *xpeci = itemForProject(project);
|
||||
if (!xpeci) return;
|
||||
|
||||
QString collection_name;
|
||||
ElementCollectionItem *eci = xpeci->lastItemForPath(path, collection_name);
|
||||
if (!eci) return;
|
||||
|
||||
int new_row = eci->rowForInsertItem(collection_name);
|
||||
if (new_row <= -1) return;
|
||||
QModelIndex parent_index = createIndex(eci->row(), 0, eci);
|
||||
beginInsertRows(parent_index, new_row, new_row);
|
||||
eci->insertNewItem(collection_name);
|
||||
endInsertRows();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementsCollectionModel::updateItem
|
||||
* Update the item at path
|
||||
* @param path
|
||||
*/
|
||||
void ElementsCollectionModel::updateItem(QString path)
|
||||
{
|
||||
QObject *object = sender();
|
||||
XmlElementCollection *collection = static_cast<XmlElementCollection *> (object);
|
||||
if (!collection) return;
|
||||
|
||||
QETProject *project = nullptr;
|
||||
|
||||
//Get the owner project of the collection
|
||||
foreach (QETProject *prj, m_project_list) {
|
||||
if (prj->embeddedElementCollection() == collection) {
|
||||
project = prj;
|
||||
}
|
||||
}
|
||||
|
||||
if (project) {
|
||||
XmlProjectElementCollectionItem *xpeci = itemForProject(project);
|
||||
if (!xpeci) {
|
||||
return;
|
||||
}
|
||||
|
||||
ElementCollectionItem *eci = xpeci->itemAtPath(path);
|
||||
if (!eci) {
|
||||
return;
|
||||
}
|
||||
|
||||
eci->clearData();
|
||||
}
|
||||
}
|
||||
|
||||
void ElementsCollectionModel::bir(ElementCollectionItem *eci, int first, int last)
|
||||
|
||||
@@ -61,7 +61,8 @@ class ElementsCollectionModel : public QAbstractItemModel
|
||||
|
||||
private:
|
||||
XmlProjectElementCollectionItem *itemForProject(QETProject *project);
|
||||
void elementIntegratedToCollection (QETProject *project, QString path);
|
||||
void elementIntegratedToCollection (QString path);
|
||||
void updateItem (QString path);
|
||||
//Use as slot in method drop mime data
|
||||
void bir(ElementCollectionItem *eci, int first, int last);
|
||||
void brr(ElementCollectionItem *eci, int first, int last);
|
||||
|
||||
@@ -263,21 +263,26 @@ void ElementsCollectionWidget::deleteElement()
|
||||
ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu);
|
||||
|
||||
if (!eci) return;
|
||||
if (!(eci->isElement() && eci->canRemoveContent())) return;
|
||||
|
||||
ElementsLocation loc(eci->collectionPath());
|
||||
if (! (loc.isElement() && loc.exist() && loc.isFileSystem() && loc.collectionPath().startsWith("custom://")) ) return;
|
||||
|
||||
if (QET::QetMessageBox::question(this,
|
||||
tr("Supprimer l'élément ?", "message box title"),
|
||||
tr("Êtes-vous sûr de vouloir supprimer cet élément ?\n", "message box content"),
|
||||
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
|
||||
{
|
||||
if (!eci->removeContent())
|
||||
QFile file(loc.fileSystemPath());
|
||||
if (file.remove())
|
||||
{
|
||||
m_model->removeRows(m_index_at_context_menu.row(), 1, m_index_at_context_menu.parent());
|
||||
}
|
||||
else
|
||||
{
|
||||
QET::QetMessageBox::warning(this,
|
||||
tr("Suppression de l'élément", "message box title"),
|
||||
tr("La suppression de l'élément a échoué.", "message box content"));
|
||||
}
|
||||
else
|
||||
m_model->removeRows(m_index_at_context_menu.row(), 1, m_index_at_context_menu.parent());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,7 +295,9 @@ void ElementsCollectionWidget::deleteDirectory()
|
||||
ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu);
|
||||
|
||||
if (!eci) return;
|
||||
if (!(eci->isDir() && eci->canRemoveContent())) return;
|
||||
|
||||
ElementsLocation loc (eci->collectionPath());
|
||||
if (! (loc.isDirectory() && loc.exist() && loc.isFileSystem() && loc.collectionPath().startsWith("custom://")) ) return;
|
||||
|
||||
if (QET::QetMessageBox::question(this,
|
||||
tr("Supprimer le dossier?", "message box title"),
|
||||
@@ -299,14 +306,17 @@ void ElementsCollectionWidget::deleteDirectory()
|
||||
"message box content"),
|
||||
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
|
||||
{
|
||||
if (!eci->removeContent())
|
||||
QDir dir (loc.fileSystemPath());
|
||||
if (dir.removeRecursively())
|
||||
{
|
||||
m_model->removeRows(m_index_at_context_menu.row(), 1, m_index_at_context_menu.parent());
|
||||
}
|
||||
else
|
||||
{
|
||||
QET::QetMessageBox::warning(this,
|
||||
tr("Suppression du dossier", "message box title"),
|
||||
tr("La suppression du dossier a échoué.", "message box content"));
|
||||
}
|
||||
else
|
||||
m_model->removeRows(m_index_at_context_menu.row(), 1, m_index_at_context_menu.parent());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,8 +354,9 @@ void ElementsCollectionWidget::newDirectory()
|
||||
|
||||
ElementsLocation location(feci->collectionPath());
|
||||
ElementsCategoryEditor new_dir_editor(location, false, this);
|
||||
if (new_dir_editor.exec() == QDialog::Accepted)
|
||||
reload();;
|
||||
if (new_dir_editor.exec() == QDialog::Accepted) {
|
||||
reload();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -677,3 +677,19 @@ QString ElementsLocation::fileName() const
|
||||
uint qHash(const ElementsLocation &location) {
|
||||
return(qHash(location.toString()));
|
||||
}
|
||||
|
||||
QDebug operator<< (QDebug debug, const ElementsLocation &location)
|
||||
{
|
||||
QDebugStateSaver saver(debug);
|
||||
debug.noquote();
|
||||
|
||||
QString msg;
|
||||
msg += "ElementsLocation(";
|
||||
msg += (location.isProject()? location.projectCollectionPath() : location.collectionPath(true));
|
||||
msg += location.exist()? ", true" : ", false";
|
||||
msg +=")";
|
||||
|
||||
debug << msg;
|
||||
|
||||
return debug;
|
||||
}
|
||||
|
||||
@@ -83,6 +83,9 @@ class ElementsLocation
|
||||
public:
|
||||
static int MetaTypeId; ///< Id of the corresponding Qt meta type
|
||||
};
|
||||
|
||||
QDebug operator<<(QDebug debug, const ElementsLocation &location);
|
||||
|
||||
Q_DECLARE_METATYPE(ElementsLocation)
|
||||
uint qHash(const ElementsLocation &);
|
||||
#endif
|
||||
|
||||
@@ -391,40 +391,6 @@ QString FileElementCollectionItem::name()
|
||||
return m_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief FileElementCollectionItem::canRemoveContent
|
||||
* Reimplemented from ElementCollectionItem
|
||||
* @return
|
||||
*/
|
||||
bool FileElementCollectionItem::canRemoveContent()
|
||||
{
|
||||
if (isCommonCollection()) return false;
|
||||
else if (isDir() && isCollectionRoot()) return false;
|
||||
else return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief FileElementCollectionItem::removeContent
|
||||
* Reimplemented from ElementCollectionItem
|
||||
* @return
|
||||
*/
|
||||
bool FileElementCollectionItem::removeContent()
|
||||
{
|
||||
if (!canRemoveContent()) return false;
|
||||
|
||||
if (isElement())
|
||||
{
|
||||
QFile file(fileSystemPath());
|
||||
return file.remove();
|
||||
}
|
||||
else if (isDir() && !isCollectionRoot())
|
||||
{
|
||||
QDir dir (fileSystemPath());
|
||||
return dir.removeRecursively();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void FileElementCollectionItem::insertNewItem(const QString &collection_name)
|
||||
{
|
||||
if (collection_name.isEmpty()) return;
|
||||
|
||||
@@ -60,8 +60,6 @@ class FileElementCollectionItem : public ElementCollectionItem
|
||||
virtual bool isValid() const;
|
||||
virtual QString name();
|
||||
|
||||
virtual bool canRemoveContent();
|
||||
virtual bool removeContent();
|
||||
virtual void insertNewItem(const QString &collection_name);
|
||||
|
||||
private:
|
||||
|
||||
@@ -433,6 +433,7 @@ bool XmlElementCollection::addElementDefinition(const QString &dir_path, const Q
|
||||
* @brief XmlElementCollection::copy
|
||||
* Copy the content represented by source (an element or a directory) to destination.
|
||||
* Destination must be a directory of this collection.
|
||||
* If the destination already have an item at the same path of source, he will be replaced by source.
|
||||
* @param source : content to copy
|
||||
* @param destination : destination of the copy, must be a directory of this collection
|
||||
* @param rename : rename the copy with @rename else use the name of source
|
||||
@@ -545,10 +546,12 @@ ElementsLocation XmlElementCollection::copyDirectory(ElementsLocation &source, E
|
||||
|
||||
/**
|
||||
* @brief XmlElementCollection::copyElement
|
||||
* Copy the element represented by source to destination (must be a directory)
|
||||
* If element already exist in destination he will be replaced by the new.
|
||||
* @param source : element to copy
|
||||
* @param destination : destination of the copy
|
||||
* @param rename : rename the copy with @rename else use the name of source
|
||||
* @return
|
||||
* @return The ElementsLocation of the copy
|
||||
*/
|
||||
ElementsLocation XmlElementCollection::copyElement(ElementsLocation &source, ElementsLocation &destination, QString rename)
|
||||
{
|
||||
@@ -575,13 +578,25 @@ ElementsLocation XmlElementCollection::copyElement(ElementsLocation &source, Ele
|
||||
|
||||
//Remove the previous element with the same path
|
||||
QDomElement element = child(destination.collectionPath(false) + "/" + new_elmt_name);
|
||||
if (!element.isNull())
|
||||
bool removed = false;
|
||||
if (!element.isNull()) {
|
||||
element.parentNode().removeChild(element);
|
||||
removed = true;
|
||||
}
|
||||
|
||||
//Get the xml directory where the new element must be added
|
||||
QDomElement dir_dom = directory(destination.collectionPath(false));
|
||||
if (dir_dom.isNull()) return ElementsLocation();
|
||||
dir_dom.appendChild(elmt_dom);
|
||||
|
||||
return ElementsLocation(destination.projectCollectionPath() + "/" + new_elmt_name);
|
||||
ElementsLocation copy_loc(destination.projectCollectionPath() + "/" + new_elmt_name);
|
||||
|
||||
if (removed) {
|
||||
emit elementChanged(copy_loc.collectionPath(false));
|
||||
}
|
||||
else {
|
||||
emit elementAdded(copy_loc.collectionPath(false));
|
||||
}
|
||||
|
||||
return copy_loc;
|
||||
}
|
||||
|
||||
@@ -62,6 +62,12 @@ class XmlElementCollection : public QObject
|
||||
* @param collection_path, the path of element in this collection
|
||||
*/
|
||||
void elementAdded(QString collection_path);
|
||||
/**
|
||||
* @brief elementChanged
|
||||
* This signal is emited when the defintion of the element at path was changed
|
||||
* @param collection_path, the path of this element in this collection
|
||||
*/
|
||||
void elementChanged (QString collection_path);
|
||||
|
||||
public slots:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user