Element panel widget : improve how an item is updated or added after editing an element with the Qet Element Editor.

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4555 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2016-06-17 08:41:09 +00:00
parent d53f7f84ff
commit 18f56b6540
8 changed files with 116 additions and 20 deletions

View File

@@ -241,6 +241,54 @@ void ElementsCollectionModel::addCustomCollection(bool set_data)
delete feci;
}
/**
* @brief ElementsCollectionModel::addLocation
* Add the element or directory to this model.
* If the location is already managed by this model, do nothing.
* @param location
*/
void ElementsCollectionModel::addLocation(ElementsLocation location)
{
QModelIndex index = indexFromLocation(location);
if (index.isValid())
return;
ElementCollectionItem *last_item = nullptr;
QString collection_name;
if (location.isProject()) {
QETProject *project = location.project();
if (project) {
XmlProjectElementCollectionItem *xpeci = m_project_hash.value(project);
last_item = xpeci->lastItemForPath(location.collectionPath(false), collection_name);
}
}
else if (location.isCustomCollection()) {
QList <ElementCollectionItem *> child_list;
for (int i=0 ; i<rowCount() ; i++)
child_list.append(static_cast<ElementCollectionItem *>(item(i)));
foreach(ElementCollectionItem *eci, child_list) {
if (eci->type() == FileElementCollectionItem::Type) {
FileElementCollectionItem *feci = static_cast<FileElementCollectionItem *>(eci);
if (feci->isCustomCollection()) {
last_item = feci->lastItemForPath(location.collectionPath(false), collection_name);
if(last_item)
break;
}
}
}
}
if (last_item)
last_item->addChildAtPath(collection_name);
}
/**
* @brief ElementsCollectionModel::addProject
* Add project to this model

View File

@@ -42,6 +42,7 @@ class ElementsCollectionModel : public QStandardItemModel
void addCommonCollection(bool set_data = true);
void addCustomCollection(bool set_data = true);
void addLocation(ElementsLocation location);
void addProject(QETProject *project, bool set_data = true);
void removeProject(QETProject *project);

View File

@@ -263,10 +263,7 @@ void ElementsCollectionWidget::editElement()
app->openElementLocations(QList<ElementsLocation>() << location);
foreach (QETElementEditor *element_editor, app->elementEditors())
{
if (element_editor->isEditing(location))
connect(element_editor, &QETElementEditor::destroyed, [eci](){ eci->clearData(); eci->setUpData();});
}
connect(element_editor, &QETElementEditor::saveToLocation, this, &ElementsCollectionWidget::locationWasSaved);
}
/**
@@ -481,6 +478,33 @@ void ElementsCollectionWidget::reload()
m_progress_bar->hide();
}
/**
* @brief ElementsCollectionWidget::locationWasSaved
* This method is connected with the signal savedToLocation of Element editor (see ElementsCollectionWidget::editElement())
* Update or add the item represented by location to m_model
* @param location
*/
void ElementsCollectionWidget::locationWasSaved(ElementsLocation location)
{
//Because this method update an item in the model, location must
//represente an existing element (in file system of project)
if (!location.exist())
return;
QModelIndex index = m_model->indexFromLocation(location);
if (index.isValid()) {
QStandardItem *item = m_model->itemFromIndex(index);
if (item) {
static_cast<ElementCollectionItem *>(item)->clearData();
static_cast<ElementCollectionItem *>(item)->setUpData();
}
}
else {
m_model->addLocation(location);
}
}
/**
* @brief ElementsCollectionWidget::search
* Search every item (directory or element) that match the text of m_search_field

View File

@@ -18,6 +18,8 @@
#ifndef ELEMENTSCOLLECTIONWIDGET_H
#define ELEMENTSCOLLECTIONWIDGET_H
#include "elementslocation.h"
#include <QWidget>
#include <QModelIndex>
#include <QTimer>
@@ -75,6 +77,9 @@ class ElementsCollectionWidget : public QWidget
private slots:
void reload();
private:
void locationWasSaved(ElementsLocation location);
private:
ElementsCollectionModel *m_model;

View File

@@ -203,6 +203,15 @@ bool FileElementCollectionItem::isCommonCollection() const
return fileSystemPath().startsWith(QETApp::commonElementsDirN());
}
/**
* @brief FileElementCollectionItem::isCustomCollection
* @return True if this item represent the custom collection
*/
bool FileElementCollectionItem::isCustomCollection() const
{
return fileSystemPath().startsWith(QETApp::customElementsDirN());
}
/**
* @brief FileElementCollectionItem::addChildAtPath
* Ask to this item item to add a child with collection name @collection_name

View File

@@ -44,6 +44,7 @@ class FileElementCollectionItem : public ElementCollectionItem
virtual QString collectionPath() const;
virtual bool isCollectionRoot() const;
bool isCommonCollection() const;
bool isCustomCollection() const;
virtual void addChildAtPath(const QString &collection_name);
void setUpData();

View File

@@ -1131,7 +1131,10 @@ bool QETElementEditor::slot_save()
//Else save to the known location
bool result_save = toLocation(location_);
if (result_save) ce_scene -> undoStack().setClean();
if (result_save) {
ce_scene -> undoStack().setClean();
emit saveToLocation(location_);
}
return(result_save);
}
}
@@ -1147,20 +1150,19 @@ bool QETElementEditor::slot_save()
* @return true if save with success
*/
bool QETElementEditor::slot_saveAs() {
// Check element befor writing
// Check element befor writing
if (checkElement()) {
// demande une localisation a l'utilisateur
//Ask a location to user
ElementsLocation location = ElementDialog::getSaveElementLocation(this);
if (location.isNull()) return(false);
// tente l'enregistrement
bool result_save = toLocation(location);
if (result_save) {
setLocation(location);
ce_scene -> undoStack().setClean();
emit saveToLocation(location);
}
// retourne un booleen representatif de la reussite de l'enregistrement
return(result_save);
}
QMessageBox::critical(this, tr("Echec de l'enregistrement"), tr("L'enregistrement à échoué,\nles conditions requises ne sont pas valides"));
@@ -1173,9 +1175,9 @@ bool QETElementEditor::slot_saveAs() {
* @return true if save with success
*/
bool QETElementEditor::slot_saveAsFile() {
// Check element befor writing
// Check element befor writing
if (checkElement()) {
// demande un nom de fichier a l'utilisateur pour enregistrer l'element
//Ask a filename to user, for save the element
QString fn = QFileDialog::getSaveFileName(
this,
tr("Enregistrer sous", "dialog title"),
@@ -1185,19 +1187,22 @@ bool QETElementEditor::slot_saveAsFile() {
"filetypes allowed when saving an element file"
)
);
// si aucun nom n'est entre, renvoie faux.
if (fn.isEmpty()) return(false);
// si le nom ne se termine pas par l'extension .elmt, celle-ci est ajoutee
if (!fn.endsWith(".elmt", Qt::CaseInsensitive)) fn += ".elmt";
// tente d'enregistrer le fichier
if (fn.isEmpty())
return(false);
//If the name doesn't end by .elmt, we add it
if (!fn.endsWith(".elmt", Qt::CaseInsensitive))
fn += ".elmt";
bool result_save = toFile(fn);
// si l'enregistrement reussit, le nom du fichier est conserve
//If the save success, the filename is keep
if (result_save) {
setFileName(fn);
QETApp::elementsRecentFiles() -> fileWasOpened(fn);
ce_scene -> undoStack().setClean();
}
// retourne un booleen representatif de la reussite de l'enregistrement
return(result_save);
}
QMessageBox::critical(this, tr("Echec de l'enregistrement"), tr("L'enregistrement à échoué,\nles conditions requises ne sont pas valides"));

View File

@@ -114,6 +114,9 @@ class QETElementEditor : public QETMainWindow {
static QString getOpenElementFileName(QWidget * = 0, const QString & = QString());
void contextMenu(QPoint p);
signals:
void saveToLocation(ElementsLocation loc);
protected:
void closeEvent(QCloseEvent *);
virtual void firstActivation(QEvent *);