mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-19 23:20:52 +01:00
Improve the remove of an item in the new panel. No need to reload the collection, use QAbstractItemModel::removeRows instead.
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4285 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -44,6 +44,25 @@ void ElementCollectionItem::appendChild(ElementCollectionItem *item) {
|
|||||||
m_child_items << item;
|
m_child_items << item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief ElementCollectionItem::removeChild
|
||||||
|
* Remove and delete count childs starting at position row
|
||||||
|
* @param row
|
||||||
|
* @return true if childs was successfully removed
|
||||||
|
*/
|
||||||
|
bool ElementCollectionItem::removeChild(int row, int count)
|
||||||
|
{
|
||||||
|
if (!(0 <= row+count && row+count <= m_child_items.size())) return false;
|
||||||
|
|
||||||
|
for (int i=0 ; i<count ; i++)
|
||||||
|
{
|
||||||
|
ElementCollectionItem *eci = m_child_items.takeAt(row);
|
||||||
|
delete eci;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief ElementCollectionItem::child
|
* @brief ElementCollectionItem::child
|
||||||
* @param row
|
* @param row
|
||||||
@@ -177,3 +196,22 @@ QList<ElementCollectionItem *> ElementCollectionItem::items() const
|
|||||||
list.append(eci->items());
|
list.append(eci->items());
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ class ElementCollectionItem
|
|||||||
virtual int type() const {return Type;}
|
virtual int type() const {return Type;}
|
||||||
|
|
||||||
void appendChild (ElementCollectionItem *item);
|
void appendChild (ElementCollectionItem *item);
|
||||||
|
bool removeChild (int row, int count);
|
||||||
ElementCollectionItem *child(int row);
|
ElementCollectionItem *child(int row);
|
||||||
int childCount() const;
|
int childCount() const;
|
||||||
int columnCount() const;
|
int columnCount() const;
|
||||||
@@ -57,6 +58,9 @@ class ElementCollectionItem
|
|||||||
virtual bool isValid() const;
|
virtual bool isValid() const;
|
||||||
virtual QList <ElementCollectionItem *> items() const;
|
virtual QList <ElementCollectionItem *> items() const;
|
||||||
|
|
||||||
|
virtual bool canRemoveContent();
|
||||||
|
virtual bool removeContent();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ElementCollectionItem *m_parent_item;
|
ElementCollectionItem *m_parent_item;
|
||||||
QList <ElementCollectionItem *> m_child_items;
|
QList <ElementCollectionItem *> m_child_items;
|
||||||
|
|||||||
@@ -131,6 +131,31 @@ QVariant ElementsCollectionModel::data(const QModelIndex &index, int role) const
|
|||||||
return item->data(index.column(), role);
|
return item->data(index.column(), role);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief ElementsCollectionModel::removeRows
|
||||||
|
* Reimplemented from QAbstractItemModel
|
||||||
|
* @param row
|
||||||
|
* @param count
|
||||||
|
* @param parent
|
||||||
|
* @return true if rows was successfully removed
|
||||||
|
*/
|
||||||
|
bool ElementsCollectionModel::removeRows(int row, int count, const QModelIndex &parent)
|
||||||
|
{
|
||||||
|
ElementCollectionItem *eci = nullptr;
|
||||||
|
if (!parent.isValid())
|
||||||
|
eci = m_root_item;
|
||||||
|
else
|
||||||
|
eci = static_cast<ElementCollectionItem *>(parent.internalPointer());
|
||||||
|
|
||||||
|
if (!(0 <= row+count && row+count <= eci->childCount())) return false;
|
||||||
|
|
||||||
|
beginRemoveRows(parent, row, (row + count -1));
|
||||||
|
bool r = eci->removeChild(row, count);
|
||||||
|
endRemoveRows();
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief ElementsCollectionModel::mimeData
|
* @brief ElementsCollectionModel::mimeData
|
||||||
* @param indexes
|
* @param indexes
|
||||||
|
|||||||
@@ -41,6 +41,8 @@ class ElementsCollectionModel : public QAbstractItemModel
|
|||||||
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
|
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
|
||||||
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
||||||
|
|
||||||
|
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
|
||||||
|
|
||||||
virtual QMimeData *mimeData(const QModelIndexList &indexes) const;
|
virtual QMimeData *mimeData(const QModelIndexList &indexes) const;
|
||||||
virtual Qt::ItemFlags flags(const QModelIndex &index) const;
|
virtual Qt::ItemFlags flags(const QModelIndex &index) const;
|
||||||
virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const;
|
virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const;
|
||||||
|
|||||||
@@ -141,11 +141,11 @@ void ElementsCollectionWidget::setUpConnection()
|
|||||||
*/
|
*/
|
||||||
void ElementsCollectionWidget::customContextMenu(const QPoint &point)
|
void ElementsCollectionWidget::customContextMenu(const QPoint &point)
|
||||||
{
|
{
|
||||||
QModelIndex index = m_tree_view->indexAt(point);
|
m_index_at_context_menu = m_tree_view->indexAt(point);
|
||||||
if (!index.isValid()) return;
|
if (!m_index_at_context_menu.isValid()) return;
|
||||||
|
|
||||||
m_context_menu->clear();
|
m_context_menu->clear();
|
||||||
ElementCollectionItem *eci = static_cast<ElementCollectionItem*>(index.internalPointer());
|
ElementCollectionItem *eci = static_cast<ElementCollectionItem*>(m_index_at_context_menu.internalPointer());
|
||||||
m_item_at_context_menu = eci;
|
m_item_at_context_menu = eci;
|
||||||
|
|
||||||
if (eci->isElement())
|
if (eci->isElement())
|
||||||
@@ -218,29 +218,22 @@ void ElementsCollectionWidget::deleteElement()
|
|||||||
ElementCollectionItem *eci = m_item_at_context_menu;
|
ElementCollectionItem *eci = m_item_at_context_menu;
|
||||||
m_item_at_context_menu = nullptr;
|
m_item_at_context_menu = nullptr;
|
||||||
|
|
||||||
if (!eci ||
|
if (!eci) return;
|
||||||
!eci->isElement() ||
|
if (!(eci->isElement() && eci->canRemoveContent())) return;
|
||||||
(eci->type() != FileElementCollectionItem::Type)) return;
|
|
||||||
|
|
||||||
|
|
||||||
FileElementCollectionItem *feci = static_cast<FileElementCollectionItem*>(eci);
|
|
||||||
//We can't remove an element in the common collection
|
|
||||||
if (feci->isCommonCollection()) return;
|
|
||||||
|
|
||||||
if (QET::QetMessageBox::question(this,
|
if (QET::QetMessageBox::question(this,
|
||||||
tr("Supprimer l'élément ?", "message box title"),
|
tr("Supprimer l'élément ?", "message box title"),
|
||||||
tr("Êtes-vous sûr de vouloir supprimer cet élément ?\n", "message box content"),
|
tr("Êtes-vous sûr de vouloir supprimer cet élément ?\n", "message box content"),
|
||||||
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
|
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
|
||||||
{
|
{
|
||||||
QFile file(feci->fileSystemPath());
|
if (!eci->removeContent())
|
||||||
if (!file.remove())
|
|
||||||
{
|
{
|
||||||
QET::QetMessageBox::warning(this,
|
QET::QetMessageBox::warning(this,
|
||||||
tr("Suppression de l'élément", "message box title"),
|
tr("Suppression de l'élément", "message box title"),
|
||||||
tr("La suppression de l'élément a échoué.", "message box content"));
|
tr("La suppression de l'élément a échoué.", "message box content"));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
reload();
|
m_model->removeRows(m_index_at_context_menu.row(), 1, m_index_at_context_menu.parent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,13 +246,8 @@ void ElementsCollectionWidget::deleteDirectory()
|
|||||||
ElementCollectionItem *eci = m_item_at_context_menu;
|
ElementCollectionItem *eci = m_item_at_context_menu;
|
||||||
m_item_at_context_menu = nullptr;
|
m_item_at_context_menu = nullptr;
|
||||||
|
|
||||||
if (!eci ||
|
if (!eci) return;
|
||||||
!eci->isDir() ||
|
if (!(eci->isDir() && eci->canRemoveContent())) return;
|
||||||
(eci->type() != FileElementCollectionItem::Type)) return;
|
|
||||||
|
|
||||||
FileElementCollectionItem *feci = static_cast<FileElementCollectionItem*>(eci);
|
|
||||||
//We can't remove directory int the common collection or remove the elements directory
|
|
||||||
if (feci->isCommonCollection() || feci->isCollectionRoot()) return;
|
|
||||||
|
|
||||||
if (QET::QetMessageBox::question(this,
|
if (QET::QetMessageBox::question(this,
|
||||||
tr("Supprimer le dossier?", "message box title"),
|
tr("Supprimer le dossier?", "message box title"),
|
||||||
@@ -268,15 +256,14 @@ void ElementsCollectionWidget::deleteDirectory()
|
|||||||
"message box content"),
|
"message box content"),
|
||||||
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
|
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
|
||||||
{
|
{
|
||||||
QDir dir(feci->fileSystemPath());
|
if (!eci->removeContent())
|
||||||
if (!dir.removeRecursively())
|
|
||||||
{
|
{
|
||||||
QET::QetMessageBox::warning(this,
|
QET::QetMessageBox::warning(this,
|
||||||
tr("Suppression du dossier", "message box title"),
|
tr("Suppression du dossier", "message box title"),
|
||||||
tr("La suppression du dossier a échoué.", "message box content"));
|
tr("La suppression du dossier a échoué.", "message box content"));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
reload();
|
m_model->removeRows(m_index_at_context_menu.row(), 1, m_index_at_context_menu.parent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ class ElementsCollectionWidget : public QWidget
|
|||||||
QVBoxLayout *m_main_vlayout;
|
QVBoxLayout *m_main_vlayout;
|
||||||
QMenu *m_context_menu;
|
QMenu *m_context_menu;
|
||||||
ElementCollectionItem *m_item_at_context_menu;
|
ElementCollectionItem *m_item_at_context_menu;
|
||||||
|
QModelIndex m_index_at_context_menu;
|
||||||
QProgressBar *m_progress_bar;
|
QProgressBar *m_progress_bar;
|
||||||
|
|
||||||
QAction *m_open_dir,
|
QAction *m_open_dir,
|
||||||
|
|||||||
@@ -349,6 +349,40 @@ QString FileElementCollectionItem::name()
|
|||||||
return m_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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief FileElementCollectionItem::setPathName
|
* @brief FileElementCollectionItem::setPathName
|
||||||
* Set the name of this item in the file system path.
|
* Set the name of this item in the file system path.
|
||||||
|
|||||||
@@ -56,6 +56,9 @@ class FileElementCollectionItem : public ElementCollectionItem
|
|||||||
virtual bool isValid() const;
|
virtual bool isValid() const;
|
||||||
virtual QString name();
|
virtual QString name();
|
||||||
|
|
||||||
|
virtual bool canRemoveContent();
|
||||||
|
virtual bool removeContent();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setPathName(QString path_name);
|
void setPathName(QString path_name);
|
||||||
void populate();
|
void populate();
|
||||||
|
|||||||
Reference in New Issue
Block a user