elements collection model : use QStandardItemModel has base class instead of QAbstractItemModel.

Add minor improvement.


git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4538 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2016-06-05 16:34:46 +00:00
parent 9a0e14161f
commit 1b00a457d7
14 changed files with 881 additions and 1477 deletions

View File

@@ -1,116 +1,39 @@
/* /*
Copyright 2006-2016 The QElectroTech Team Copyright 2006-2016 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
QElectroTech is distributed in the hope that it will be useful, QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "elementcollectionitem.h" #include "elementcollectionitem.h"
#include <QMimeData>
#include <QBrush>
/** /**
* @brief ElementCollectionItem::ElementCollectionItem * @brief ElementCollectionItem::ElementCollectionItem
* Constructor * Constructor
* @param parent : the parent item of this item
*/ */
ElementCollectionItem::ElementCollectionItem(ElementCollectionItem *parent) : ElementCollectionItem::ElementCollectionItem()
QObject(parent),
m_parent_item (parent)
{} {}
/** /**
* @brief ElementCollectionItem::~ElementCollectionItem * @brief ElementCollectionItem::clearData
* Destructor * Reset the data
*/ */
ElementCollectionItem::~ElementCollectionItem() { void ElementCollectionItem::clearData()
qDeleteAll(m_child_items);
}
/**
* @brief ElementCollectionItem::appendChild
* Add @item to the child of this item
* @param item
*/
void ElementCollectionItem::appendChild(ElementCollectionItem *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 (!(1 <= row+count && row+count <= m_child_items.size())) return false; setText(QString());
setToolTip(QString());
int last_ = row + (count-1); setIcon(QIcon());
if (last_ < row) return false;
emit beginRemoveRows(this, row, last_);
for (int i=0 ; i<count ; i++)
{
ElementCollectionItem *eci = m_child_items.takeAt(row);
delete eci;
}
emit endRemoveRows();
return true;
}
/**
* @brief ElementCollectionItem::insertChild
* Insert item at position row in the child item list
* @param row
* @param item
* @return true if item was inserted, if item is already a chil of this item, return false
*/
bool ElementCollectionItem::insertChild(int row, ElementCollectionItem *item)
{
if (m_child_items.contains(item)) return false;
beginInsertRows(this, row, row);
m_child_items.insert(row, item);
endInsertRows();
return true;
}
/**
* @brief ElementCollectionItem::child
* @param row
* @return The child at @row of this item.
* If there isn't child at @row, return default ElementCollectionItem
*/
ElementCollectionItem *ElementCollectionItem::child(int row) const {
return m_child_items.value(row);
}
/**
* @brief ElementCollectionItem::childWithCollectionName
* Return the child with the collection name @name, else return nullptr
* @param name
* @return
*/
ElementCollectionItem *ElementCollectionItem::childWithCollectionName(QString name) const
{
foreach (ElementCollectionItem *eci, m_child_items)
if (eci->collectionName() == name) return eci;
return nullptr;
} }
/** /**
@@ -118,10 +41,10 @@ ElementCollectionItem *ElementCollectionItem::childWithCollectionName(QString na
* Return the last existing item in this ElementCollectionItem hierarchy according to the given path. * Return the last existing item in this ElementCollectionItem hierarchy according to the given path.
* Next_item is the first non existing item in this hierarchy according to the given path. * Next_item is the first non existing item in this hierarchy according to the given path.
* @param path : The path to find last item. The path must be in form : path/otherPath/.../.../myElement.elmt. * @param path : The path to find last item. The path must be in form : path/otherPath/.../.../myElement.elmt.
* @param newt_item : The first item that not exist in this hierarchy * @param no_found_path : The first item that not exist in this hierarchy
* @return : The last item that exist in this hierarchy, or nullptr can't find (an error was occurred, or path already exist) * @return : The last item that exist in this hierarchy, or nullptr can't find (an error was occurred, or path already exist)
*/ */
ElementCollectionItem *ElementCollectionItem::lastItemForPath(const QString &path, QString &newt_item) ElementCollectionItem *ElementCollectionItem::lastItemForPath(const QString &path, QString &no_found_path)
{ {
QStringList str_list = path.split("/"); QStringList str_list = path.split("/");
if (str_list.isEmpty()) return nullptr; if (str_list.isEmpty()) return nullptr;
@@ -132,7 +55,7 @@ ElementCollectionItem *ElementCollectionItem::lastItemForPath(const QString &pat
ElementCollectionItem *eci = return_eci->childWithCollectionName(str); ElementCollectionItem *eci = return_eci->childWithCollectionName(str);
if (!eci) if (!eci)
{ {
newt_item = str; no_found_path = str;
return return_eci; return return_eci;
} }
else else
@@ -143,27 +66,36 @@ ElementCollectionItem *ElementCollectionItem::lastItemForPath(const QString &pat
} }
/** /**
* @brief ElementCollectionItem::itemAtPath * @brief ElementCollectionItem::childWithCollectionName
* @param path * Return the child with the collection name @name, else return nullptr
* @return the item at path or nullptr if doesn't exist * @param name
* @return
*/ */
ElementCollectionItem *ElementCollectionItem::itemAtPath(const QString &path) ElementCollectionItem *ElementCollectionItem::childWithCollectionName(QString name) const
{ {
QStringList str_list = path.split("/"); rowCount();
if (str_list.isEmpty()) return nullptr; foreach (QStandardItem *qsi, directChilds()) {
ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsi);
ElementCollectionItem *match_eci = this; if (eci->name() == name)
foreach (QString str, str_list) { return eci;
ElementCollectionItem *eci = match_eci->childWithCollectionName(str);
if (!eci) {
return nullptr;
}
else {
match_eci = eci;
}
} }
return match_eci; return nullptr;
}
/**
* @brief ElementCollectionItem::directChilds
* Return the direct child of this item
* @return
*/
QList<QStandardItem *> ElementCollectionItem::directChilds() const
{
QList <QStandardItem *> item_list;
for (int i=0 ; i<rowCount() ; i++)
item_list.append(child(i));
return item_list;
} }
/** /**
@@ -173,250 +105,106 @@ ElementCollectionItem *ElementCollectionItem::itemAtPath(const QString &path)
* @param collection_name * @param collection_name
* @return * @return
*/ */
int ElementCollectionItem::rowForInsertItem(const QString &collection_name) int ElementCollectionItem::rowForInsertItem(const QString &name)
{ {
if (collection_name.isEmpty()) return -1; if (name.isEmpty())
return -1;
QList <ElementCollectionItem *> child; QList <ElementCollectionItem *> child;
//The item to insert is an element we search from element child //The item to insert is an element we search from element child
if (collection_name.endsWith(".elmt")) if (name.endsWith(".elmt"))
{ {
child = elementsChild(); child = elementsDirectChild();
//There isn't element, we insert at last position //There isn't element, we insert at last position
if (child.isEmpty()) if (child.isEmpty())
return childCount(); return rowCount();
} }
//The item is a directory, we search from directory child //The item is a directory, we search from directory child
else else
{ {
child = directoriesChild(); child = directoriesDirectChild();
//There isn't directory, we insert at first position //There isn't directory, we insert at first position
if(child.isEmpty()) if(child.isEmpty())
return 0; return 0;
} }
foreach (ElementCollectionItem *eci, child) foreach (ElementCollectionItem *eci, child)
if (eci->collectionName() > collection_name) if (eci->name() > name)
return indexOfChild(eci); return model()->indexFromItem(eci).row();
return (indexOfChild(child.last())+1); return (model()->indexFromItem(child.last()).row() + 1);
} }
/** /**
* @brief ElementCollectionItem::insertNewItem * @brief ElementCollectionItem::itemAtPath
* By defualt do nothing, implement this method in subclass * @param path
* to handle the insertion of a new item with name collection_name * @return the item at path or nullptr if doesn't exist
*/ */
void ElementCollectionItem::insertNewItem(const QString &collection_name) {Q_UNUSED (collection_name);} ElementCollectionItem *ElementCollectionItem::itemAtPath(const QString &path)
/**
* @brief ElementCollectionItem::childCount
* @return the number of childs of this item
*/
int ElementCollectionItem::childCount() const {
return m_child_items.size();
}
/**
* @brief ElementCollectionItem::columnCount
* @return the number of columns (always 1)
*/
int ElementCollectionItem::columnCount() const {
return 1;
}
/**
* @brief ElementCollectionItem::data
* @param column
* @param role
* @return the data at @column and @role.
*/
QVariant ElementCollectionItem::data(int column, int role)
{ {
Q_UNUSED(column); QStringList str_list = path.split("/");
if (str_list.isEmpty())
return nullptr;
switch (role) ElementCollectionItem *match_eci = this;
{ foreach (QString str, str_list) {
case Qt::BackgroundRole: ElementCollectionItem *eci = match_eci->childWithCollectionName(str);
{ if (!eci)
if(m_show_bg_color) return nullptr;
return QBrush(m_bg_color); else
else match_eci = eci;
return QVariant();
}
case Qt::StatusTipRole:
{
if (isElement())
return tr("Glissez-déposez cet élément « %1 » sur un folio pour l'y insérer, double-cliquez dessus pour l'éditer").arg(name());
else
return tr("Double-cliquez pour réduire ou développer cette catégorie d'éléments");
}
default:
return QVariant();
} }
return QVariant(); return match_eci;
} }
/** /**
* @brief ElementCollectionItem::clearData * @brief ElementCollectionItem::elementsDirectChild
* Reset the curent name * @return The direct element child of this item
*/ */
void ElementCollectionItem::clearData() QList<ElementCollectionItem *> ElementCollectionItem::elementsDirectChild() const
{ {
m_name = QString(); QList <ElementCollectionItem *> element_child;
foreach (QStandardItem *qsi, directChilds()) {
ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsi);
if (eci->isElement())
element_child.append(eci);
}
return element_child;
} }
/** /**
* @brief ElementCollectionItem::mimeData * @brief ElementCollectionItem::directoriesDirectChild
* @return The mime data of this item * @return the direct directory child of this item
*/ */
QMimeData *ElementCollectionItem::mimeData() { QList<ElementCollectionItem *> ElementCollectionItem::directoriesDirectChild() const
return new QMimeData();
}
bool ElementCollectionItem::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column) const
{ {
Q_UNUSED(data); Q_UNUSED(action); Q_UNUSED(row); Q_UNUSED(column); QList <ElementCollectionItem *> dir_child;
return false;
}
bool ElementCollectionItem::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column) foreach (QStandardItem *qsi, directChilds()) {
{ ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsi);
Q_UNUSED(data); Q_UNUSED(action); Q_UNUSED(row); Q_UNUSED(column); if (eci->isDir())
return false; dir_child.append(eci);
} }
/** return dir_child;
* @brief ElementCollectionItem::flags
* @return the flag of this item
*/
Qt::ItemFlags ElementCollectionItem::flags() {
return (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
}
/**
* @brief ElementCollectionItem::parent
* @return parent item of this item.
* return nullptr if item haven't got parent
*/
ElementCollectionItem *ElementCollectionItem::parent() {
return m_parent_item;
}
/**
* @brief ElementCollectionItem::row
* @return the index of this item, from his parent.
* If item haven't got parent return 0
*/
int ElementCollectionItem::row() const
{
if (m_parent_item)
return m_parent_item->m_child_items.indexOf(const_cast<ElementCollectionItem *>(this));
else
return 0;
}
/**
* @brief ElementCollectionItem::name
* @return the located name of this item
*/
QString ElementCollectionItem::name() {
return m_name;
}
/**
* @brief ElementCollectionItem::collectionName
* @return The collection name of this item
*/
QString ElementCollectionItem::collectionName() const {
return QString();
}
/**
* @brief ElementCollectionItem::isDir
* @return true if this item represent a directory
*/
bool ElementCollectionItem::isDir() const {
return false;
}
/**
* @brief ElementCollectionItem::isElement
* @return true if this item represent a directory
*/
bool ElementCollectionItem::isElement() const {
return false;
}
/**
* @brief ElementCollectionItem::isValid
* @return true if this item refer to an dir or an element.
*/
bool ElementCollectionItem::isValid() const {
return false;
} }
/** /**
* @brief ElementCollectionItem::items * @brief ElementCollectionItem::items
* @return all child and subchild subsubchild... contained by this item * @return every childs of this item (direct and indirect childs)
* This item isn't stored in the list
*/ */
QList<ElementCollectionItem *> ElementCollectionItem::items() const QList<ElementCollectionItem *> ElementCollectionItem::items() const
{ {
QList<ElementCollectionItem *> list; QList <ElementCollectionItem *> list;
list.append(m_child_items);
foreach(ElementCollectionItem *eci, m_child_items) for (int i=0 ; i<rowCount() ; i++) {
ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(child(i));
list.append(eci);
list.append(eci->items()); list.append(eci->items());
return list; }
}
/**
* @brief ElementCollectionItem::elementsChild
* @return All elements child of this item
*/
QList<ElementCollectionItem *> ElementCollectionItem::elementsChild() const
{
QList<ElementCollectionItem *> list;
foreach (ElementCollectionItem *eci, m_child_items)
if (eci->isElement())
list.append(eci);
return list; return list;
} }
/**
* @brief ElementCollectionItem::directoriesChild
* @return All directories child of this item
*/
QList<ElementCollectionItem *> ElementCollectionItem::directoriesChild() const
{
QList<ElementCollectionItem *> list;
foreach (ElementCollectionItem *eci, m_child_items)
if (eci->isDir())
list.append(eci);
return list;
}
/**
* @brief ElementCollectionItem::indexOfChild
* @param child
* @return the index of child or -1 if not found
*/
int ElementCollectionItem::indexOfChild(ElementCollectionItem *child) const {
return m_child_items.indexOf(child);
}
/**
* @brief ElementCollectionItem::setBackgroundColor
* Set the background color for this item to @color
* if @show is true, use the background color, else let's Qt use the appropriate color
* @param color
* @param show
*/
void ElementCollectionItem::setBackgroundColor(Qt::GlobalColor color, bool show)
{
m_bg_color = color;
m_show_bg_color = show;
}

View File

@@ -1,28 +1,24 @@
/* /*
Copyright 2006-2016 The QElectroTech Team Copyright 2006-2016 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
QElectroTech is distributed in the hope that it will be useful, QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef ELEMENTCOLLECTIONITEM_H #ifndef ELEMENTCOLLECTIONITEM2_H
#define ELEMENTCOLLECTIONITEM_H #define ELEMENTCOLLECTIONITEM2_H
#include <QVariant> #include <QStandardItem>
class QMimeData;
class ElementCollectionItem;
class QList<ElementCollectionItem>;
/** /**
* @brief The ElementCollectionItem class * @brief The ElementCollectionItem class
@@ -30,61 +26,34 @@ class QList<ElementCollectionItem>;
* This class must be herited for specialisation. * This class must be herited for specialisation.
* This item is used by ElementsCollectionModel for manage the elements collection * This item is used by ElementsCollectionModel for manage the elements collection
*/ */
class ElementCollectionItem : public QObject class ElementCollectionItem : public QStandardItem
{ {
Q_OBJECT public:
ElementCollectionItem();
public: enum {Type = UserType+1};
ElementCollectionItem(ElementCollectionItem *parent = nullptr); virtual int type() const { return Type; }
virtual ~ElementCollectionItem();
enum {Type = 1 , UserType = 100}; virtual bool isDir() const = 0;
virtual int type() const {return Type;} virtual bool isElement() const = 0;
virtual QString localName() = 0;
void appendChild (ElementCollectionItem *item); virtual QString name() const = 0;
bool removeChild (int row, int count); virtual QString collectionPath() const = 0;
bool insertChild (int row, ElementCollectionItem *item); virtual bool isCollectionRoot() const = 0;
ElementCollectionItem *child(int row) const; virtual void addChildAtPath(const QString &collection_name) = 0;
ElementCollectionItem *childWithCollectionName(QString name) const; virtual void setUpData() = 0;
ElementCollectionItem *lastItemForPath(const QString &path, QString &newt_item); virtual void setUpIcon() = 0;
ElementCollectionItem *itemAtPath(const QString &path);
int rowForInsertItem(const QString &collection_name);
virtual void insertNewItem(const QString &collection_name);
int childCount() const;
int columnCount() const;
virtual QVariant data(int column, int role);
virtual void clearData(); virtual void clearData();
virtual QMimeData *mimeData();
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 Qt::ItemFlags flags();
ElementCollectionItem *parent();
int row() const;
virtual QString name();
virtual QString collectionName() const;
virtual QString collectionPath() const {return QString();}
virtual bool isDir() const; ElementCollectionItem *lastItemForPath(const QString &path, QString &no_found_path);
virtual bool isElement() const; ElementCollectionItem *childWithCollectionName(QString name) const;
virtual bool isValid() const; QList<QStandardItem *> directChilds() const;
virtual QList <ElementCollectionItem *> items() const; int rowForInsertItem(const QString &name);
QList<ElementCollectionItem *> elementsChild() const; ElementCollectionItem *itemAtPath(const QString &path);
QList<ElementCollectionItem *> directoriesChild() const;
int indexOfChild(ElementCollectionItem *child) const;
void setBackgroundColor(Qt::GlobalColor color, bool show);
signals: QList<ElementCollectionItem *> elementsDirectChild() const;
void beginInsertRows(ElementCollectionItem *parent, int first, int last); QList<ElementCollectionItem *> directoriesDirectChild() const;
void endInsertRows(); QList<ElementCollectionItem *> items() const;
void beginRemoveRows(ElementCollectionItem *parent, int first, int last);
void endRemoveRows();
protected:
ElementCollectionItem *m_parent_item;
QList <ElementCollectionItem *> m_child_items;
QString m_name;
Qt::GlobalColor m_bg_color;
bool m_show_bg_color = false;
}; };
#endif // ELEMENTCOLLECTIONITEM_H #endif // ELEMENTCOLLECTIONITEM2_H

View File

@@ -17,251 +17,76 @@
*/ */
#include "elementscollectionmodel.h" #include "elementscollectionmodel.h"
#include "elementcollectionitem.h" #include "elementcollectionitem.h"
#include "qetapp.h"
#include "fileelementcollectionitem.h" #include "fileelementcollectionitem.h"
#include "xmlprojectelementcollectionitem.h" #include "xmlprojectelementcollectionitem.h"
#include "qetproject.h" #include "qetapp.h"
#include "xmlelementcollection.h" #include "xmlelementcollection.h"
#include "qetproject.h"
#include "elementcollectionhandler.h"
/** /**
* @brief ElementsCollectionModel::ElementsCollectionModel * @brief ElementsCollectionModel::ElementsCollectionModel
* Defaut constructor * Constructor
* @param parent : parent QObject * @param parent
*/ */
ElementsCollectionModel::ElementsCollectionModel(QObject *parent) : ElementsCollectionModel::ElementsCollectionModel(QObject *parent) :
QAbstractItemModel (parent) QStandardItemModel(parent)
{ {
m_root_item = new ElementCollectionItem();
}
ElementsCollectionModel::~ElementsCollectionModel()
{
delete m_root_item;
}
/**
* @brief ElementsCollectionModel::index
* Create a index for child of parent at row @row and column @column.
* If there isn't child return default QModelIndex
* @param row : the wanted row
* @param column : the wanted column
* @param parent : the parent index
* @return the wanted index or a unvalid index.
*/
QModelIndex ElementsCollectionModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
ElementCollectionItem *parent_item = nullptr;
if (!parent.isValid())
parent_item = m_root_item;
else
parent_item = static_cast<ElementCollectionItem*>(parent.internalPointer());
ElementCollectionItem *child_item = parent_item->child(row);
if (child_item->isValid()) {
if (m_hide_element) {
if (child_item->isDir()) {
return createIndex(row, column, child_item);
}
else {
return QModelIndex();
}
}
else {
return createIndex(row, column, child_item);
}
}
else {
return QModelIndex();
}
}
/**
* @brief ElementsCollectionModel::parent
* @param child :
* @return the parent index of child if have parent.
* If child haven't got parent or parent is the root_item, return default index
*/
QModelIndex ElementsCollectionModel::parent(const QModelIndex &child) const
{
if (!child.isValid())
return QModelIndex();
ElementCollectionItem *child_item = static_cast<ElementCollectionItem*> (child.internalPointer());
ElementCollectionItem *parent_item = child_item->parent();
if (parent_item == m_root_item)
return QModelIndex();
return createIndex(parent_item->row(), 0, parent_item);
}
/**
* @brief ElementsCollectionModel::rowCount
* @param parent
* @return the number of row for @parent.
* If @parent is unvalid, return the number of row of the root_item
*/
int ElementsCollectionModel::rowCount(const QModelIndex &parent) const
{
ElementCollectionItem *parent_item = nullptr;
if (!parent.isValid())
parent_item = m_root_item;
else
parent_item = static_cast<ElementCollectionItem*> (parent.internalPointer());
if (m_hide_element) {
int count_ = 0;
for (int i = 0 ; i<parent_item->childCount() ; i++)
{
if (parent_item->child(i)->isDir()) {
count_ ++;
}
}
return count_;
}
else {
return parent_item->childCount();
}
}
/**
* @brief ElementsCollectionModel::columnCount
* @param parent
* @return the number of column for @parent.
* If @parent is unvalid, return the number of column of the root_item
*/
int ElementsCollectionModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<ElementCollectionItem*>(parent.internalPointer())->columnCount();
else
return m_root_item->columnCount();
} }
/** /**
* @brief ElementsCollectionModel::data * @brief ElementsCollectionModel::data
* Reimplemented from QStandardItemModel
* @param index * @param index
* @param role * @param role
* @return the data of index for the given role or a default QVariant if no data. * @return
*/ */
QVariant ElementsCollectionModel::data(const QModelIndex &index, int role) const QVariant ElementsCollectionModel::data(const QModelIndex &index, int role) const
{ {
if (!index.isValid()) if (role == Qt::DecorationRole) {
return QVariant(); QStandardItem *item = itemFromIndex(index);
ElementCollectionItem *item = static_cast<ElementCollectionItem*>(index.internalPointer()); if (item->type() == FileElementCollectionItem::Type)
return item->data(index.column(), role); static_cast<FileElementCollectionItem*>(item)->setUpIcon();
} else if (item->type() == XmlProjectElementCollectionItem::Type)
static_cast<XmlProjectElementCollectionItem*>(item)->setUpIcon();
}
/** return QStandardItemModel::data(index, 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 (!(1 <= 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
* Reimplemented from QStandardItemModel
* @param indexes * @param indexes
* @return the mime data of the items at @indexes * @return
*/ */
QMimeData *ElementsCollectionModel::mimeData(const QModelIndexList &indexes) const QMimeData *ElementsCollectionModel::mimeData(const QModelIndexList &indexes) const
{ {
QModelIndex index = indexes.first(); QModelIndex index = indexes.first();
if (index.isValid()) if (index.isValid())
{ {
ElementCollectionItem *item = static_cast<ElementCollectionItem*>(index.internalPointer()); ElementCollectionItem *item = static_cast<ElementCollectionItem*>(itemFromIndex(index));
return item->mimeData();
QMimeData *mime_data = new QMimeData();
mime_data->setText(item->collectionPath());
if (item->isElement())
mime_data->setData("application/x-qet-element-uri", item->collectionPath().toLatin1());
else
mime_data->setData("application/x-qet-category-uri", item->collectionPath().toLatin1());
return mime_data;
} }
else else
return new QMimeData(); return new QMimeData();
} }
/** /**
* @brief ElementsCollectionModel::flags * @brief ElementsCollectionModel::mimeTypes
* @param index * Reimplemented from QStandardItemModel
* @return the flags of the item at @index * @return
*/ */
Qt::ItemFlags ElementsCollectionModel::flags(const QModelIndex &index) const
{
if (index.isValid())
{
ElementCollectionItem *eci = static_cast<ElementCollectionItem*>(index.internalPointer());
return eci->flags();
}
else
return Qt::NoItemFlags;
}
bool ElementsCollectionModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
{
if (!(QAbstractItemModel::canDropMimeData(data, action, row, column, parent) && parent.isValid())) return false;
ElementCollectionItem *eci = static_cast <ElementCollectionItem *>(parent.internalPointer());
if (!eci) return false;
return eci->canDropMimeData(data, action, row, column);
}
bool ElementsCollectionModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
if (!parent.isValid()) return false;
ElementCollectionItem *eci = static_cast<ElementCollectionItem*> (parent.internalPointer());
if (!eci || eci->isElement()) return false;
m_parent_at_drop = parent;
//We temporarily disconnect for avoid double insertion of item
foreach (QETProject *project, m_project_list)
disconnect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection);
connect(eci, &ElementCollectionItem::beginInsertRows, this, &ElementsCollectionModel::bir);
connect(eci, &ElementCollectionItem::endInsertRows, this, &ElementsCollectionModel::endInsertRows);
connect(eci, &ElementCollectionItem::beginRemoveRows, this, &ElementsCollectionModel::brr);
connect(eci, &ElementCollectionItem::endRemoveRows, this, &ElementsCollectionModel::endRemoveRows);
bool rb = eci->dropMimeData(data, action, row, column);
disconnect(eci, &ElementCollectionItem::beginInsertRows, this, &ElementsCollectionModel::bir);
disconnect(eci, &ElementCollectionItem::endInsertRows, this, &ElementsCollectionModel::endInsertRows);
disconnect(eci, &ElementCollectionItem::beginRemoveRows, this, &ElementsCollectionModel::brr);
disconnect(eci, &ElementCollectionItem::endRemoveRows, this, &ElementsCollectionModel::endRemoveRows);
foreach(QETProject *project, m_project_list)
connect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection);
m_parent_at_drop = QModelIndex();
return rb;
}
QStringList ElementsCollectionModel::mimeTypes() const QStringList ElementsCollectionModel::mimeTypes() const
{ {
QStringList mime_list = QAbstractItemModel::mimeTypes(); QStringList mime_list = QAbstractItemModel::mimeTypes();
@@ -270,25 +95,132 @@ QStringList ElementsCollectionModel::mimeTypes() const
} }
/** /**
* @brief ElementsCollectionModel::items * @brief ElementsCollectionModel::canDropMimeData
* @return All items handled by this model. The root item isn't stored in the list * Reimplemented from QStandardItemModel
* @param data
* @param action
* @param row
* @param column
* @param parent
* @return
*/ */
QList<ElementCollectionItem *> ElementsCollectionModel::items() const bool ElementsCollectionModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
{ {
QList <ElementCollectionItem *> list; if (!(QStandardItemModel::canDropMimeData(data, action, row, column, parent) && parent.isValid()))
list.append(m_root_item->items()); return false;
return list;
QStandardItem *qsi = itemFromIndex(parent.child(row, column));
if (!qsi)
qsi = itemFromIndex(parent);
//Drop in the common collection is forbiden
if (qsi->type() == FileElementCollectionItem::Type)
if (static_cast<FileElementCollectionItem *>(qsi)->isCommonCollection())
return false;
ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsi);
if (data->hasFormat("application/x-qet-element-uri") || data->hasFormat("application/x-qet-category-uri")) {
return true;
//Return false if user try to drop a item from a folder to the same folder
ElementsLocation drop_location(data->text());
for (int i=0 ; i<eci->rowCount() ; i++)
if (static_cast<ElementCollectionItem *>(eci->child(i))->collectionPath() == drop_location.collectionPath())
return false;
return true;
}
else
return false;
}
/**
* @brief ElementsCollectionModel::dropMimeData
* Reimplemented from QStandardItemModel
* @param data
* @param action
* @param row
* @param column
* @param parent
* @return
*/
bool ElementsCollectionModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
Q_UNUSED(action);
QStandardItem *qsi = itemFromIndex(parent.child(row, column));
if (!qsi)
qsi = itemFromIndex(parent);
if (qsi->type() == FileElementCollectionItem::Type) {
FileElementCollectionItem *feci = static_cast<FileElementCollectionItem *>(qsi);
if (feci->isCommonCollection())
return false;
if (feci->isElement() && feci->parent() && feci->parent()->type() == FileElementCollectionItem::Type)
feci = static_cast<FileElementCollectionItem *>(feci->parent());
ElementCollectionHandler ech;
ElementsLocation source(data->text());
ElementsLocation destination(feci->fileSystemPath());
ElementsLocation location = ech.copy(source, destination);
if (location.exist())
{
//If feci have a child with the same path of location,
//we remove the existing child befor insert new child
for (int i=0 ; i<feci->rowCount() ; i++) {
if (static_cast<FileElementCollectionItem *>(feci->child(i))->collectionPath() == location.collectionPath())
feci->removeRow(i);
}
feci->addChildAtPath(location.fileName());
return true;
}
return false;
}
else if (qsi->type() == XmlProjectElementCollectionItem::Type) {
XmlProjectElementCollectionItem *xpeci = static_cast<XmlProjectElementCollectionItem*>(qsi);
if (xpeci->isElement() && xpeci->parent() && xpeci->parent()->type() == XmlProjectElementCollectionItem::Type)
xpeci = static_cast<XmlProjectElementCollectionItem *>(xpeci->parent());
//before do the copy, we get all collection path of xpeci child,
//for remove it if the copied item have the same path of an existing child.
//We can't do this after the copy, because at the copy if the xml collection have a DomElement with the same path,
//he was removed before the new xml DomElement is inserted
//So the existing child of this will return a null QString when call collectionPath(), because the item
//doesn't exist anymore in the xml collection.
QList <QString> child_path_list;
for (int i=0 ; i<xpeci->rowCount() ; i++)
child_path_list.append(static_cast<XmlProjectElementCollectionItem *>(xpeci->child(i, 0))->collectionPath());
ElementCollectionHandler ech;
ElementsLocation source(data->text());
ElementsLocation destination(xpeci->collectionPath());
ElementsLocation location = ech.copy(source, destination);
return location.exist();
}
return false;
} }
/** /**
* @brief ElementsCollectionModel::addCommonCollection * @brief ElementsCollectionModel::addCommonCollection
* Add the common elements collection to this model * Add the common elements collection to this model
*/ */
void ElementsCollectionModel::addCommonCollection() void ElementsCollectionModel::addCommonCollection(bool set_data)
{ {
FileElementCollectionItem *feci = new FileElementCollectionItem(m_root_item); FileElementCollectionItem *feci = new FileElementCollectionItem();
if (feci->setRootPath(QETApp::commonElementsDirN())) if (feci->setRootPath(QETApp::commonElementsDirN(), set_data, m_hide_element)) {
m_root_item->appendChild(feci); invisibleRootItem()->appendRow(feci);
if (set_data)
feci->setUpData();
}
else else
delete feci; delete feci;
} }
@@ -297,92 +229,124 @@ void ElementsCollectionModel::addCommonCollection()
* @brief ElementsCollectionModel::addCustomCollection * @brief ElementsCollectionModel::addCustomCollection
* Add the custom elements collection to this model * Add the custom elements collection to this model
*/ */
void ElementsCollectionModel::addCustomCollection() void ElementsCollectionModel::addCustomCollection(bool set_data)
{ {
FileElementCollectionItem *feci = new FileElementCollectionItem(m_root_item); FileElementCollectionItem *feci = new FileElementCollectionItem();
if (feci->setRootPath(QETApp::customElementsDirN())) if (feci->setRootPath(QETApp::customElementsDirN(), set_data, m_hide_element)) {
m_root_item->appendChild(feci); invisibleRootItem()->appendRow(feci);
if (set_data)
feci->setUpData();
}
else else
delete feci; delete feci;
} }
/** /**
* @brief ElementsCollectionModel::addProject * @brief ElementsCollectionModel::addProject
* Add @project to the displayed collection * Add project to this model
* @param project * @param project : project to add.
* @return true if project was successfully added. If project is already * @param set_data : if true, setUpData is called for every ElementCollectionItem of project
* handled, return false.
*/ */
bool ElementsCollectionModel::addProject(QETProject *project) void ElementsCollectionModel::addProject(QETProject *project, bool set_data)
{ {
if (m_project_list.contains(project)) return false; if (m_project_list.contains(project))
return;
m_project_list.append(project); m_project_list.append(project);
int row = m_project_list.indexOf(project); int row = m_project_list.indexOf(project);
beginInsertRows(QModelIndex(), row, row); XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem();
XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem(project, m_root_item); m_project_hash.insert(project, xpeci);
bool r = m_root_item->insertChild(row, xpeci);
endInsertRows(); xpeci->setProject(project);
insertRow(row, xpeci);
if (set_data)
xpeci->setUpData();
connect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection); connect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection);
connect(project->embeddedElementCollection(), &XmlElementCollection::elementChanged, this, &ElementsCollectionModel::updateItem); connect(project->embeddedElementCollection(), &XmlElementCollection::elementChanged, this, &ElementsCollectionModel::updateItem);
return r;
} }
/** /**
* @brief ElementsCollectionModel::removeProject * @brief ElementsCollectionModel::removeProject
* Remove @project from this model * Remove project from this model
* @param project * @param project
* @return true if the project was successfully removed, false if not (or project doesn't managed)
*/ */
bool ElementsCollectionModel::removeProject(QETProject *project) void ElementsCollectionModel::removeProject(QETProject *project)
{ {
if (!m_project_list.contains(project)) return false; if (!m_project_list.contains(project))
return;
int row = m_project_list.indexOf(project); int row = m_project_list.indexOf(project);
if (removeRows(row, 1, QModelIndex())) { if (removeRows(row, 1, QModelIndex())) {
m_project_list.removeOne(project); m_project_list.removeOne(project);
m_project_hash.remove(project);
disconnect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection); disconnect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection);
connect(project->embeddedElementCollection(), &XmlElementCollection::elementChanged, this, &ElementsCollectionModel::updateItem); disconnect(project->embeddedElementCollection(), &XmlElementCollection::elementChanged, this, &ElementsCollectionModel::updateItem);
return true;
} }
else
return false;
} }
/** /**
* @brief ElementsCollectionModel::project * @brief ElementsCollectionModel::project
* @return A list of project handled by this model * @return every project added to this model
*/ */
QList<QETProject *> ElementsCollectionModel::project() const { QList<QETProject *> ElementsCollectionModel::project() const
{
return m_project_list; return m_project_list;
} }
/** /**
* @brief ElementsCollectionModel::index * @brief ElementsCollectionModel::items
* @param location * @return every ElementCollectionItem owned by this model
* @return Return the index of the item represented by location.
* index can be no valid
*/ */
QModelIndex ElementsCollectionModel::index(const ElementsLocation &location) const QList <ElementCollectionItem *> ElementsCollectionModel::items() const
{ {
if (!location.exist()) { QList <ElementCollectionItem *> list;
return QModelIndex();
for (int i=0 ; i<rowCount() ; i++) {
ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(item(i));
list.append(eci);
list.append(eci->items());
} }
return list;
}
/**
* @brief ElementsCollectionModel::hideElement
* Hide element in this model, only directory is managed
*/
void ElementsCollectionModel::hideElement()
{
m_hide_element = true;
foreach(ElementCollectionItem *eci, items()) {
if (eci->isElement()) {
removeRow(eci->row(), indexFromItem(eci).parent());
}
}
}
/**
* @brief ElementsCollectionModel::indexFromLocation
* Return the index who represent @location.
* Index can be non valid
* @param location
* @return
*/
QModelIndex ElementsCollectionModel::indexFromLocation(const ElementsLocation &location)
{
if (!location.exist())
return QModelIndex();
QList <ElementCollectionItem *> child_list; QList <ElementCollectionItem *> child_list;
for (int i=0 ; i<m_root_item->childCount() ; i++) { for (int i=0 ; i<rowCount() ; i++)
child_list.append(m_root_item->child(i)); child_list.append(static_cast<ElementCollectionItem *>(item(i)));
}
foreach(ElementCollectionItem *eci, child_list) { foreach(ElementCollectionItem *eci, child_list) {
ElementCollectionItem *match_eci = nullptr; ElementCollectionItem *match_eci = nullptr;
if (eci->type() == FileElementCollectionItem::Type) { if (eci->type() == FileElementCollectionItem::Type) {
FileElementCollectionItem *feci = static_cast<FileElementCollectionItem *>(eci); if (FileElementCollectionItem *feci = static_cast<FileElementCollectionItem *>(eci)) {
if (feci) {
if ( (location.isCommonCollection() && feci->isCommonCollection()) || if ( (location.isCommonCollection() && feci->isCommonCollection()) ||
(location.isCustomCollection() && !feci->isCommonCollection()) ) { (location.isCustomCollection() && !feci->isCommonCollection()) ) {
match_eci = feci->itemAtPath(location.collectionPath(false)); match_eci = feci->itemAtPath(location.collectionPath(false));
@@ -390,59 +354,30 @@ QModelIndex ElementsCollectionModel::index(const ElementsLocation &location) con
} }
} }
else if (eci->type() == XmlProjectElementCollectionItem::Type) { else if (eci->type() == XmlProjectElementCollectionItem::Type) {
XmlProjectElementCollectionItem *xpeci = static_cast<XmlProjectElementCollectionItem *>(eci); if (XmlProjectElementCollectionItem *xpeci = static_cast<XmlProjectElementCollectionItem *>(eci)) {
if (xpeci) {
match_eci = xpeci->itemAtPath(location.collectionPath(false)); match_eci = xpeci->itemAtPath(location.collectionPath(false));
} }
} }
if (match_eci) { if (match_eci)
return createIndex(match_eci->row(), 0, match_eci); return indexFromItem(match_eci);
}
} }
return QModelIndex(); return QModelIndex();
} }
/** /**
* @brief ElementsCollectionModel::hideElement * @brief ElementsCollectionModel::elementIntegratedToCollection
* Hide element.
* Only directory is provided by the model
*/
void ElementsCollectionModel::hideElement()
{
m_hide_element = true;
}
/**
* @brief ElementsCollectionModel::itemForProject
* @param project
* @return the root item of project @project, or nullptr if not found.
*/
XmlProjectElementCollectionItem *ElementsCollectionModel::itemForProject(QETProject *project)
{
if (!m_project_list.contains(project)) return nullptr;
QModelIndex index_ = index(m_project_list.indexOf(project), 0);
if (!index_.isValid()) return nullptr;
XmlProjectElementCollectionItem *xpeci = static_cast<XmlProjectElementCollectionItem *>(index_.internalPointer());
if (xpeci)
return xpeci;
else
return nullptr;
}
/**
* @brief ElementsCollectionModel::elementAddedToEmbeddedCollection
* When an element is added to embedded collection of a project, * When an element is added to embedded collection of a project,
* this method create and display the new element * this method create and display the new element
* @param path -The path of the new element in the embedded collection of a project * @param path -The path of the new element in the embedded collection of a project
*/ */
void ElementsCollectionModel::elementIntegratedToCollection (QString path) void ElementsCollectionModel::elementIntegratedToCollection(QString path)
{ {
QObject *object = sender(); QObject *object = sender();
XmlElementCollection *collection = static_cast<XmlElementCollection *> (object); XmlElementCollection *collection = static_cast<XmlElementCollection *> (object);
if (!collection) return; if (!collection)
return;
QETProject *project = nullptr; QETProject *project = nullptr;
@@ -454,19 +389,14 @@ void ElementsCollectionModel::elementIntegratedToCollection (QString path)
} }
if (project) { if (project) {
XmlProjectElementCollectionItem *xpeci = itemForProject(project); XmlProjectElementCollectionItem *xpeci = m_project_hash.value(project);
if (!xpeci) return;
QString collection_name; QString collection_name;
ElementCollectionItem *eci = xpeci->lastItemForPath(path, collection_name); ElementCollectionItem *eci = xpeci->lastItemForPath(path, collection_name);
if (!eci) return; if (!eci)
return;
int new_row = eci->rowForInsertItem(collection_name); eci->addChildAtPath(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();
} }
} }
@@ -479,7 +409,8 @@ void ElementsCollectionModel::updateItem(QString path)
{ {
QObject *object = sender(); QObject *object = sender();
XmlElementCollection *collection = static_cast<XmlElementCollection *> (object); XmlElementCollection *collection = static_cast<XmlElementCollection *> (object);
if (!collection) return; if (!collection)
return;
QETProject *project = nullptr; QETProject *project = nullptr;
@@ -491,30 +422,11 @@ void ElementsCollectionModel::updateItem(QString path)
} }
if (project) { if (project) {
XmlProjectElementCollectionItem *xpeci = itemForProject(project); ElementCollectionItem *eci = m_project_hash.value(project)->itemAtPath(path);
if (!xpeci) { if (!eci)
return; return;
}
ElementCollectionItem *eci = xpeci->itemAtPath(path);
if (!eci) {
return;
}
eci->clearData(); eci->clearData();
eci->setUpData();
} }
} }
void ElementsCollectionModel::bir(ElementCollectionItem *eci, int first, int last)
{
Q_UNUSED(eci);
if (!m_parent_at_drop.isValid()) return;
beginInsertRows(m_parent_at_drop, first, last);
}
void ElementsCollectionModel::brr(ElementCollectionItem *eci, int first, int last)
{
Q_UNUSED(eci);
if (!m_parent_at_drop.isValid()) return;
beginRemoveRows(m_parent_at_drop, first, last);
}

View File

@@ -15,66 +15,51 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef ELEMENTSCOLLECTIONMODEL_H #ifndef ELEMENTSCOLLECTIONMODEL2_H
#define ELEMENTSCOLLECTIONMODEL_H #define ELEMENTSCOLLECTIONMODEL2_H
#include <QAbstractItemModel> #include <QStandardItemModel>
#include "elementslocation.h" #include "elementslocation.h"
class ElementCollectionItem;
class QETProject;
class QList<QETProject>; class QList<QETProject>;
class XmlProjectElementCollectionItem; class XmlProjectElementCollectionItem;
class QHash<QETProject, XmlProjectElementCollectionItem>;
class ElementCollectionItem;
class QList<ElementCollectionItem>;
/** class ElementsCollectionModel : public QStandardItemModel
* @brief The ElementsCollectionModel class
* Provide a data model for co;llection of elements.
*/
class ElementsCollectionModel : public QAbstractItemModel
{ {
Q_OBJECT Q_OBJECT
public: public:
ElementsCollectionModel(QObject *parent = nullptr); ElementsCollectionModel(QObject *parent = Q_NULLPTR);
~ElementsCollectionModel();
virtual QModelIndex index (int row, int column, const QModelIndex &parent = QModelIndex()) const;
virtual QModelIndex parent (const QModelIndex &child) const;
virtual int rowCount(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 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
virtual QVariant data(const QModelIndex &index, int role) const;
virtual QMimeData *mimeData(const QModelIndexList &indexes) const; virtual QMimeData *mimeData(const QModelIndexList &indexes) const;
virtual Qt::ItemFlags flags(const QModelIndex &index) const; virtual QStringList mimeTypes() 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;
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent); virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
QStringList mimeTypes() const;
QList <ElementCollectionItem *> items() const;
void addCommonCollection(); void addCommonCollection(bool set_data = true);
void addCustomCollection(); void addCustomCollection(bool set_data = true);
bool addProject(QETProject *project);
bool removeProject(QETProject *project); void addProject(QETProject *project, bool set_data = true);
void removeProject(QETProject *project);
QList<QETProject *> project() const; QList<QETProject *> project() const;
QModelIndex index(const ElementsLocation &location) const;
QList <ElementCollectionItem *> items() const;
void hideElement(); void hideElement();
bool isHideElement() {return m_hide_element;}
QModelIndex indexFromLocation(const ElementsLocation &location);
private: private:
XmlProjectElementCollectionItem *itemForProject(QETProject *project);
void elementIntegratedToCollection (QString path); void elementIntegratedToCollection (QString path);
void updateItem (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);
private: private:
ElementCollectionItem *m_root_item;
QList <QETProject *> m_project_list; QList <QETProject *> m_project_list;
QModelIndex m_parent_at_drop; QHash <QETProject *, XmlProjectElementCollectionItem *> m_project_hash;
bool m_hide_element = false; bool m_hide_element = false;
}; };
#endif // ELEMENTSCOLLECTIONMODEL_H #endif // ELEMENTSCOLLECTIONMODEL2_H

View File

@@ -34,6 +34,7 @@
#include <QMenu> #include <QMenu>
#include <QDesktopServices> #include <QDesktopServices>
#include <QUrl> #include <QUrl>
#include <QTimer>
/** /**
* @brief ElementsCollectionWidget::ElementsCollectionWidget * @brief ElementsCollectionWidget::ElementsCollectionWidget
@@ -47,8 +48,6 @@ ElementsCollectionWidget::ElementsCollectionWidget(QWidget *parent):
setUpWidget(); setUpWidget();
setUpAction(); setUpAction();
setUpConnection(); setUpConnection();
reload();
} }
/** /**
@@ -57,18 +56,13 @@ ElementsCollectionWidget::ElementsCollectionWidget(QWidget *parent):
*/ */
void ElementsCollectionWidget::expandFirstItems() void ElementsCollectionWidget::expandFirstItems()
{ {
if (!m_model)
return;
for (int i=0; i < m_model->rowCount() ; i++) for (int i=0; i < m_model->rowCount() ; i++)
showAndExpandItem(m_model->index(i, 0), false); showAndExpandItem(m_model->index(i, 0), false);
} }
/**
* @brief ElementsCollectionWidget::model
* @return The ElementsCollectionModel used by the tree view
*/
ElementsCollectionModel *ElementsCollectionWidget::model() const {
return m_model;
}
/** /**
* @brief ElementsCollectionWidget::addProject * @brief ElementsCollectionWidget::addProject
* Add @project to be displayed * Add @project to be displayed
@@ -82,6 +76,16 @@ void ElementsCollectionWidget::removeProject(QETProject *project) {
m_model->removeProject(project); m_model->removeProject(project);
} }
bool ElementsCollectionWidget::event(QEvent *event)
{
if (m_first_show && event->type() == QEvent::WindowActivate) {
m_first_show = false;
QTimer::singleShot(250, this, &ElementsCollectionWidget::reload);
}
return QWidget::event(event);
}
void ElementsCollectionWidget::setUpAction() void ElementsCollectionWidget::setUpAction()
{ {
m_open_dir = new QAction(QET::Icons::DocumentOpen, tr("Ouvrir le dossier correspondant"), this); m_open_dir = new QAction(QET::Icons::DocumentOpen, tr("Ouvrir le dossier correspondant"), this);
@@ -249,7 +253,7 @@ void ElementsCollectionWidget::editElement()
foreach (QETElementEditor *element_editor, app->elementEditors()) foreach (QETElementEditor *element_editor, app->elementEditors())
{ {
if (element_editor->isEditing(location)) if (element_editor->isEditing(location))
connect(element_editor, &QETElementEditor::destroyed, eci, &ElementCollectionItem::clearData); connect(element_editor, &QETElementEditor::destroyed, [eci](){ eci->clearData(); eci->setUpData();});
} }
} }
@@ -393,7 +397,7 @@ void ElementsCollectionWidget::showThisDir()
{ {
ElementCollectionItem *eci = elementCollectionItemForIndex(m_showed_index); ElementCollectionItem *eci = elementCollectionItemForIndex(m_showed_index);
if (eci) if (eci)
eci->setBackgroundColor(Qt::yellow, false); eci->setBackground(QBrush());
} }
m_showed_index = m_index_at_context_menu; m_showed_index = m_index_at_context_menu;
@@ -403,7 +407,7 @@ void ElementsCollectionWidget::showThisDir()
showAndExpandItem(m_showed_index, true, true); showAndExpandItem(m_showed_index, true, true);
ElementCollectionItem *eci = elementCollectionItemForIndex(m_showed_index); ElementCollectionItem *eci = elementCollectionItemForIndex(m_showed_index);
if (eci) if (eci)
eci->setBackgroundColor(Qt::yellow, true); eci->setBackground(QBrush(Qt::yellow));
search(m_search_field->text()); search(m_search_field->text());
} }
else else
@@ -421,7 +425,7 @@ void ElementsCollectionWidget::resetShowThisDir()
{ {
ElementCollectionItem *eci = elementCollectionItemForIndex(m_showed_index); ElementCollectionItem *eci = elementCollectionItemForIndex(m_showed_index);
if (eci) if (eci)
eci->setBackgroundColor(Qt::yellow, false); eci->setBackground(QBrush());
} }
m_showed_index = QModelIndex(); m_showed_index = QModelIndex();
@@ -435,19 +439,19 @@ void ElementsCollectionWidget::reload()
{ {
m_progress_bar->show(); m_progress_bar->show();
ElementsCollectionModel *new_model = new ElementsCollectionModel(m_tree_view); ElementsCollectionModel *new_model = new ElementsCollectionModel(m_tree_view);
new_model->addCommonCollection(); new_model->addCommonCollection(false);
new_model->addCustomCollection(); new_model->addCustomCollection(false);
if (m_model) if (m_model)
foreach (QETProject *project, m_model->project()) foreach (QETProject *project, m_model->project())
new_model->addProject(project); new_model->addProject(project, false);
QList <ElementCollectionItem *> list = new_model->items(); QList <ElementCollectionItem *> list = new_model->items();
m_progress_bar->setMaximum(list.size()); m_progress_bar->setMaximum(list.size());
m_progress_bar->setValue(0); m_progress_bar->setValue(0);
foreach (ElementCollectionItem *item, new_model->items()) foreach (ElementCollectionItem *item, new_model->items())
{ {
item->name(); item->setUpData();
m_progress_bar->setValue(m_progress_bar->value() + 1); m_progress_bar->setValue(m_progress_bar->value() + 1);
} }
@@ -493,7 +497,6 @@ void ElementsCollectionWidget::search(const QString &text)
} }
hideCollection(true); hideCollection(true);
QStringList text_list = text.split("+", QString::SkipEmptyParts); QStringList text_list = text.split("+", QString::SkipEmptyParts);
QModelIndexList match_index; QModelIndexList match_index;
foreach (QString txt, text_list) { foreach (QString txt, text_list) {
@@ -543,11 +546,13 @@ void ElementsCollectionWidget::hideItem(bool hide, const QModelIndex &index, boo
*/ */
void ElementsCollectionWidget::showAndExpandItem(const QModelIndex &index, bool parent, bool child) void ElementsCollectionWidget::showAndExpandItem(const QModelIndex &index, bool parent, bool child)
{ {
if (parent && index.isValid()) if (index.isValid()) {
showAndExpandItem(index.parent(), parent); if (parent)
showAndExpandItem(index.parent(), parent);
hideItem(false, index, child); hideItem(false, index, child);
m_tree_view->expand(index); m_tree_view->expand(index);
}
} }
/** /**
@@ -556,5 +561,5 @@ void ElementsCollectionWidget::showAndExpandItem(const QModelIndex &index, bool
* @return The internal pointer of index casted to ElementCollectionItem; * @return The internal pointer of index casted to ElementCollectionItem;
*/ */
ElementCollectionItem *ElementsCollectionWidget::elementCollectionItemForIndex(const QModelIndex &index) { ElementCollectionItem *ElementsCollectionWidget::elementCollectionItemForIndex(const QModelIndex &index) {
return static_cast<ElementCollectionItem*>(index.internalPointer()); return static_cast<ElementCollectionItem*>(m_model->itemFromIndex(index));
} }

View File

@@ -44,11 +44,13 @@ class ElementsCollectionWidget : public QWidget
ElementsCollectionWidget(QWidget *parent = nullptr); ElementsCollectionWidget(QWidget *parent = nullptr);
void expandFirstItems(); void expandFirstItems();
ElementsCollectionModel *model() const;
void addProject (QETProject *project); void addProject (QETProject *project);
void removeProject (QETProject *project); void removeProject (QETProject *project);
protected:
virtual bool event(QEvent *event);
private: private:
void setUpAction(); void setUpAction();
void setUpWidget(); void setUpWidget();
@@ -70,11 +72,11 @@ class ElementsCollectionWidget : public QWidget
void showAndExpandItem (const QModelIndex &index, bool parent = true, bool child = false); void showAndExpandItem (const QModelIndex &index, bool parent = true, bool child = false);
ElementCollectionItem *elementCollectionItemForIndex (const QModelIndex &index); ElementCollectionItem *elementCollectionItemForIndex (const QModelIndex &index);
private: private:
ElementsCollectionModel *m_model; ElementsCollectionModel *m_model;
QLineEdit *m_search_field; QLineEdit *m_search_field;
ElementsTreeView *m_tree_view; ElementsTreeView *m_tree_view;
QVBoxLayout *m_main_vlayout; QVBoxLayout *m_main_vlayout;
QMenu *m_context_menu; QMenu *m_context_menu;
QModelIndex m_index_at_context_menu; QModelIndex m_index_at_context_menu;
QModelIndex m_showed_index; QModelIndex m_showed_index;
@@ -90,6 +92,8 @@ class ElementsCollectionWidget : public QWidget
*m_new_element, *m_new_element,
*m_show_this_dir, *m_show_this_dir,
*m_show_all_dir; *m_show_all_dir;
bool m_first_show = true;
}; };
#endif // ELEMENTSCOLLECTIONWIDGET_H #endif // ELEMENTSCOLLECTIONWIDGET_H

View File

@@ -23,6 +23,7 @@
#include "element.h" #include "element.h"
#include <QDrag> #include <QDrag>
#include <QStandardItemModel>
static int MAX_DND_PIXMAP_WIDTH = 500; static int MAX_DND_PIXMAP_WIDTH = 500;
static int MAX_DND_PIXMAP_HEIGHT = 375; static int MAX_DND_PIXMAP_HEIGHT = 375;
@@ -49,18 +50,16 @@ void ElementsTreeView::startDrag(Qt::DropActions supportedActions)
return; return;
} }
ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(index.internalPointer()); if (QStandardItemModel *qsim = static_cast<QStandardItemModel *>(model())) {
if (ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsim->itemFromIndex(index))) {
if (!eci) { ElementsLocation loc (eci->collectionPath());
QTreeView::startDrag(supportedActions); if (loc.exist()) {
return; startElementDrag(loc);
return;
}
}
} }
QTreeView::startDrag(supportedActions);
ElementsLocation loc (eci->collectionPath());
if (loc.exist())
startElementDrag(loc);
else
QTreeView::startDrag(supportedActions);
} }
/** /**

View File

@@ -1,41 +1,33 @@
/* /*
Copyright 2006-2016 The QElectroTech Team Copyright 2006-2016 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
QElectroTech is distributed in the hope that it will be useful, QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "fileelementcollectionitem.h" #include "fileelementcollectionitem.h"
#include "qetapp.h"
#include "elementslocation.h" #include "elementslocation.h"
#include "nameslist.h" #include "qetapp.h"
#include "qeticons.h" #include "qeticons.h"
#include "elementcollectionhandler.h"
#include <QDir>
/** /**
* @brief FileElementCollectionItem::FileElementCollectionItem * @brief FileElementCollectionItem::FileElementCollectionItem
* Default constructor * Constructor
* @param parent : parent item of this item
*/ */
FileElementCollectionItem::FileElementCollectionItem(ElementCollectionItem *parent) : FileElementCollectionItem::FileElementCollectionItem()
ElementCollectionItem(parent)
{}
/**
* @brief FileElementCollectionItem::~FileElementCollectionItem
* Destructor
*/
FileElementCollectionItem::~FileElementCollectionItem()
{} {}
/** /**
@@ -45,16 +37,15 @@ FileElementCollectionItem::~FileElementCollectionItem()
* @param path * @param path
* @return true if path exist. * @return true if path exist.
*/ */
bool FileElementCollectionItem::setRootPath(QString path) bool FileElementCollectionItem::setRootPath(QString path, bool set_data, bool hide_element)
{ {
QDir dir(path); QDir dir(path);
if (dir.exists()) {
if (dir.exists())
{
m_path = path; m_path = path;
populate(); populate(set_data, hide_element);
return true; return true;
} }
return false; return false;
} }
@@ -64,226 +55,29 @@ bool FileElementCollectionItem::setRootPath(QString path)
*/ */
QString FileElementCollectionItem::fileSystemPath() const QString FileElementCollectionItem::fileSystemPath() const
{ {
//Parent must be a file element collection item if (isCollectionRoot())
if (!m_parent_item || m_parent_item->type() != FileElementCollectionItem::Type)
return m_path; return m_path;
FileElementCollectionItem *parent = static_cast<FileElementCollectionItem*>(m_parent_item); FileElementCollectionItem *feci = static_cast<FileElementCollectionItem *> (parent());
if (feci)
//Get the path of the parent. return feci->fileSystemPath() + "/" + m_path;
// if (parent->isCollectionRoot())
// return parent->fileSystemPath() + m_path;
// else
// return parent->fileSystemPath() + "/" + m_path;
return parent->fileSystemPath() + "/" + m_path;
}
/**
* @brief FileElementCollectionItem::dirPath
* @return the dir path of this item
*/
QString FileElementCollectionItem::dirPath() const
{
if (isDir())
return fileSystemPath();
//Parent must be a file element collection item
if (m_parent_item->type() != FileElementCollectionItem::Type) return QString();
FileElementCollectionItem *parent = static_cast<FileElementCollectionItem*>(m_parent_item);
//Get the path of the parent.
return parent->fileSystemPath();
}
/**
* @brief FileElementCollectionItem::collectionPath
* @return The path of this item relative to the collection.
*/
QString FileElementCollectionItem::collectionPath() const
{
//Parent must be a file element collection item
//else this item is the root of collection path.
if (!m_parent_item || m_parent_item->type() != FileElementCollectionItem::Type)
{
if (m_path == QETApp::commonElementsDirN())
return "common://";
else
return "custom://";
}
else if (m_parent_item->type() == FileElementCollectionItem::Type)
{
FileElementCollectionItem *feci = static_cast<FileElementCollectionItem*>(m_parent_item);
if (feci->isCollectionRoot())
return feci->collectionPath() + m_path;
else
return feci->collectionPath() + "/" + m_path;
}
else else
return QString(); return QString();
} }
/** /**
* @brief FileElementCollectionItem::collectionName * @brief FileElementCollectionItem::dirPath
* @return The collection name of this item * @return the dir path of this item (if this item is a dir return the path,
* if item is an element return the path of the parent directory)
*/ */
QString FileElementCollectionItem::collectionName() const QString FileElementCollectionItem::dirPath() const
{
if (isCollectionRoot()) return QString();
else return m_path;
}
/**
* @brief FileElementCollectionItem::data
* @param column
* @param role
* @return the item data at column and role
*/
QVariant FileElementCollectionItem::data(int column, int role)
{
//element collection have only one column
if (column > 0)
return QVariant();
switch (role)
{
case Qt::DisplayRole: {
return name();
}
break;
case Qt::DecorationRole:
{
//This item have no parent or parent isn't a file element, so it is the root of a collection
if (!m_parent_item || m_parent_item->type() != FileElementCollectionItem::Type)
{
if (m_path == QETApp::commonElementsDirN())
return QIcon(":/ico/16x16/qet.png");
else
return QIcon(":/ico/16x16/go-home.png");
}
if (isDir())
return QET::Icons::Folder;
else if (isElement())
{
if (m_icon.isNull())
{
ElementsLocation loc(collectionPath());
m_icon = loc.icon();
}
return m_icon;
}
}
case Qt::ToolTipRole:
return collectionPath();
break;
default:
return ElementCollectionItem::data(column, role);
break;
}
}
/**
* @brief FileElementCollectionItem::clearData
* Reset the current icon
*/
void FileElementCollectionItem::clearData()
{
m_icon = QIcon();
ElementCollectionItem::clearData();
}
/**
* @brief FileElementCollectionItem::mimeData
* @return the mime data of this item
*/
QMimeData *FileElementCollectionItem::mimeData()
{
QMimeData *mime_data = new QMimeData();
mime_data->setText(collectionPath());
if (isElement())
mime_data->setData("application/x-qet-element-uri", collectionPath().toLatin1());
else
mime_data->setData("application/x-qet-category-uri", collectionPath().toLatin1());
return mime_data;
}
/**
* @brief FileElementCollectionItem::canDropMimeData
* @param data
* @param action
* @param column
* @return True if the data can be dropped
*/
bool FileElementCollectionItem::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column) const
{
Q_UNUSED(action); Q_UNUSED(row); Q_UNUSED(column);
if (isCommonCollection()) return false;
if (data->hasFormat("application/x-qet-element-uri") || data->hasFormat("application/x-qet-category-uri"))
{
//Return false if user try to drop a item from a folder to the same folder
ElementsLocation drop_location(data->text());
for (int i=0 ; i<childCount() ; i++)
{
if (static_cast<FileElementCollectionItem *>(child(i))->collectionPath() == drop_location.collectionPath())
return false;
}
return true;
}
else
return false;
}
/**
* @brief FileElementCollectionItem::dropMimeData
* @param data
* @param action
* @param column
* @return Handle the drop of a data
*/
bool FileElementCollectionItem::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column)
{
Q_UNUSED(action); Q_UNUSED(row); Q_UNUSED(column);
if (isCommonCollection()) return false;
FileElementCollectionItem *feci = this;
if (isElement() && parent() && parent()->type() == FileElementCollectionItem::Type)
feci = static_cast<FileElementCollectionItem *>(parent());
ElementCollectionHandler ech;
ElementsLocation source(data->text());
ElementsLocation destination(feci->fileSystemPath());
ElementsLocation location = ech.copy(source, destination);
if (location.exist())
{
//If this item have a child with the same path of location,
//we remove the existing child befor insert new child
for (int i=0 ; i<childCount() ; i++)
if (static_cast<FileElementCollectionItem *>(child(i))->collectionPath() == location.collectionPath())
removeChild(i, 1);
insertNewItem(location.fileName());
return true;
}
return false;
}
/**
* @brief FileElementCollectionItem::flags
* @return the flags of this item
*/
Qt::ItemFlags FileElementCollectionItem::flags()
{ {
if (isDir()) if (isDir())
return (Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled); return fileSystemPath();
else if (parent() && parent()->type() == FileElementCollectionItem::Type)
return static_cast<FileElementCollectionItem*>(parent())->fileSystemPath();
else else
return (Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled); return QString();
} }
/** /**
@@ -296,16 +90,98 @@ bool FileElementCollectionItem::isDir() const
return false; return false;
else else
return true; return true;
} }
/** /**
* @brief FileElementCollectionItem::isElement * @brief FileElementCollectionItem::isElement
* @return true if this item represent an element * @return true if this item represent an element
*/ */
bool FileElementCollectionItem::isElement() const { bool FileElementCollectionItem::isElement() const
{
return (!isDir()); return (!isDir());
} }
/**
* @brief FileElementCollectionItem::localName
* @return the located name of this item
*/
QString FileElementCollectionItem::localName()
{
if (!text().isNull())
return text();
else if (isDir()) {
if (isCollectionRoot()) {
if (m_path == QETApp::commonElementsDirN())
setText(QObject::tr("Collection QET"));
else if (m_path == QETApp::customElementsDirN())
setText(QObject::tr("Collection utilisateur"));
else
setText(QObject::tr("Collection inconnue"));
}
else {
//Open the qet_directory file, to get the traductions name of this dir
QFile dir_conf(fileSystemPath() + "/qet_directory");
if (dir_conf.exists() && dir_conf.open(QIODevice::ReadOnly | QIODevice::Text)) {
//Get the content of the file
QDomDocument document;
if (document.setContent(&dir_conf)) {
QDomElement root = document.documentElement();
if (root.tagName() == "qet-directory") {
NamesList nl;
nl.fromXml(root);
setText(nl.name());
}
}
}
}
}
else if (isElement()) {
ElementsLocation loc(collectionPath());
setText(loc.name());
}
return text();
}
/**
* @brief FileElementCollectionItem::name
* @return The collection name of this item
*/
QString FileElementCollectionItem::name() const
{
if (isCollectionRoot())
return QString();
else
return m_path;
}
/**
* @brief FileElementCollectionItem::collectionPath
* @return The path of this item relative to the collection.
*/
QString FileElementCollectionItem::collectionPath() const
{
if (isCollectionRoot()) {
if (m_path == QETApp::commonElementsDirN())
return "common://";
else
return "custom://";
}
else if (parent() && parent()->type() == FileElementCollectionItem::Type) {
ElementCollectionItem *eci = static_cast<ElementCollectionItem*>(parent());
if (eci->isCollectionRoot())
return eci->collectionPath() + m_path;
else
return eci->collectionPath() + "/" + m_path;
}
else
return QString();
}
/** /**
* @brief FileElementCollectionItem::isCollectionRoot * @brief FileElementCollectionItem::isCollectionRoot
* @return true if this item represent the root of collection * @return true if this item represent the root of collection
@@ -320,84 +196,70 @@ bool FileElementCollectionItem::isCollectionRoot() const
/** /**
* @brief FileElementCollectionItem::isCommonCollection * @brief FileElementCollectionItem::isCommonCollection
* @return True if this item is part of the common element collection item * @return True if this item represent the common collection
*/ */
bool FileElementCollectionItem::isCommonCollection() const { bool FileElementCollectionItem::isCommonCollection() const
{
return fileSystemPath().startsWith(QETApp::commonElementsDirN()); return fileSystemPath().startsWith(QETApp::commonElementsDirN());
} }
/** /**
* @brief FileElementCollectionItem::isValid * @brief FileElementCollectionItem::addChildAtPath
* @return * Ask to this item item to add a child with collection name @collection_name
* @param collection_name
*/ */
bool FileElementCollectionItem::isValid() const void FileElementCollectionItem::addChildAtPath(const QString &collection_name)
{ {
return true; if (collection_name.isEmpty())
if (m_path.isEmpty()) return;
return false;
else FileElementCollectionItem *feci = new FileElementCollectionItem();
return true; insertRow(rowForInsertItem(collection_name), feci);
feci->setPathName(collection_name);
feci->setUpData();
} }
/** /**
* @brief FileElementCollectionItem::name * @brief FileElementCollectionItem::setUpData
* @return the located name of this item * SetUp the data of this item
*/ */
QString FileElementCollectionItem::name() void FileElementCollectionItem::setUpData()
{ {
if (!m_name.isNull()) return m_name; //Setup the displayed name
localName();
else if (isDir()) if (isDir())
{ setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
if (isCollectionRoot()) else
{ setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);
if (m_path == QETApp::commonElementsDirN())
m_name = QObject::tr("Collection QET");
else if (m_path == QETApp::customElementsDirN())
m_name = QObject::tr("Collection utilisateur");
else
m_name = QObject::tr("Collection inconnue");
}
else
{
//Open the qet_directory file, to get the traductions name of this dir
QFile dir_conf(fileSystemPath() + "/qet_directory");
if (!dir_conf.exists())
m_name = QString("");
if (!dir_conf.open(QIODevice::ReadOnly | QIODevice::Text)) setToolTip(collectionPath());
m_name = QString("");
//Get the content of the file
QDomDocument document;
if (!document.setContent(&dir_conf))
m_name = QString("");
QDomElement root = document.documentElement();
if (root.tagName() != "qet-directory")
m_name = QString("");
//Return the name for the current langage.
NamesList nl;
nl.fromXml(root);
m_name = nl.name();
}
}
else if (isElement())
{
ElementsLocation loc(collectionPath());
m_name = loc.name();
}
return m_name;
} }
void FileElementCollectionItem::insertNewItem(const QString &collection_name) /**
* @brief FileElementCollectionItem::setUpIcon
* SetUp the icon of this item.
* Because icon use several memory, we use this method for setup icon instead setUpData.
*/
void FileElementCollectionItem::setUpIcon()
{ {
if (collection_name.isEmpty()) return; if (!icon().isNull())
return;
FileElementCollectionItem *feci = new FileElementCollectionItem(this); if (isCollectionRoot()) {
feci->setPathName(collection_name); if (m_path == QETApp::commonElementsDirN())
insertChild(rowForInsertItem(collection_name), feci); setIcon(QIcon(":/ico/16x16/qet.png"));
else
setIcon(QIcon(":/ico/16x16/go-home.png"));
}
else {
if (isDir())
setIcon(QET::Icons::Folder);
else {
ElementsLocation loc(collectionPath());
setIcon(loc.icon());
}
}
} }
/** /**
@@ -407,39 +269,45 @@ void FileElementCollectionItem::insertNewItem(const QString &collection_name)
* For create a new file collection see setRootPath. * For create a new file collection see setRootPath.
* @param path_name * @param path_name
*/ */
void FileElementCollectionItem::setPathName(QString path_name) void FileElementCollectionItem::setPathName(QString path_name, bool set_data, bool hide_element)
{ {
if (!m_parent_item) return;
m_path = path_name; m_path = path_name;
//This isn't an element, we create the childs //This isn't an element, we create the childs
if (!path_name.endsWith(".elmt")) if (!path_name.endsWith(".elmt"))
populate(); populate(set_data, hide_element);
} }
/** /**
* @brief FileElementCollectionItem::populate * @brief FileElementCollectionItem::populate
* Item populate itself with childs found in the system path. * Create the childs of this item
* @param set_data : if true, call setUpData for every child of this item
*/ */
void FileElementCollectionItem::populate() void FileElementCollectionItem::populate(bool set_data, bool hide_element)
{ {
QDir dir (fileSystemPath()); QDir dir (fileSystemPath());
//Get all directory in this directory. //Get all directory in this directory.
foreach(QString str, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)) foreach(QString str, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name))
{ {
FileElementCollectionItem *feci = new FileElementCollectionItem(this); FileElementCollectionItem *feci = new FileElementCollectionItem();
feci->setPathName(str); appendRow(feci);
appendChild(feci); feci->setPathName(str, set_data, hide_element);
if (set_data)
feci->setUpData();
} }
if (hide_element)
return;
//Get all elmt file in this directory //Get all elmt file in this directory
dir.setNameFilters(QStringList() << "*.elmt"); dir.setNameFilters(QStringList() << "*.elmt");
foreach(QString str, dir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name)) foreach(QString str, dir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
{ {
FileElementCollectionItem *feci = new FileElementCollectionItem(this); FileElementCollectionItem *feci = new FileElementCollectionItem();
feci->setPathName(str); appendRow(feci);
appendChild(feci); feci->setPathName(str, set_data);
if (set_data)
feci->setUpData();
} }
} }

View File

@@ -1,27 +1,24 @@
/* /*
Copyright 2006-2016 The QElectroTech Team Copyright 2006-2016 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
QElectroTech is distributed in the hope that it will be useful, QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef FILEELEMENTCOLLECTIONITEM_H #ifndef FILEELEMENTCOLLECTIONITEM2_H
#define FILEELEMENTCOLLECTIONITEM_H #define FILEELEMENTCOLLECTIONITEM2_H
#include "elementcollectionitem.h" #include "elementcollectionitem.h"
#include <QString>
#include <QDir>
#include <QIcon>
/** /**
* @brief The FileElementCollectionItem class * @brief The FileElementCollectionItem class
@@ -30,45 +27,37 @@
*/ */
class FileElementCollectionItem : public ElementCollectionItem class FileElementCollectionItem : public ElementCollectionItem
{ {
Q_OBJECT public:
FileElementCollectionItem();
public: enum { Type = UserType+2 };
FileElementCollectionItem(ElementCollectionItem *parent = nullptr); virtual int type() const { return Type;}
~FileElementCollectionItem();
enum {Type = UserType + 1}; bool setRootPath(QString path, bool set_data = true, bool hide_element = false);
virtual int type() const {return Type;}
bool setRootPath(QString path);
QString fileSystemPath() const; QString fileSystemPath() const;
QString dirPath() const; QString dirPath() const;
QString collectionPath() const;
QString collectionName() const;
virtual QVariant data(int column, int role);
virtual void clearData();
virtual QMimeData *mimeData();
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 Qt::ItemFlags flags();
virtual bool isDir() const; virtual bool isDir() const;
virtual bool isElement() const; virtual bool isElement() const;
virtual QString localName();
virtual QString name() const;
virtual QString collectionPath() const;
virtual bool isCollectionRoot() const; virtual bool isCollectionRoot() const;
bool isCommonCollection() const; bool isCommonCollection() const;
virtual bool isValid() const; virtual void addChildAtPath(const QString &collection_name);
virtual QString name();
virtual void insertNewItem(const QString &collection_name); void setUpData();
void setUpIcon();
private: void hire();
void setPathName(QString path_name);
void populate();
private:
private:
void setPathName(QString path_name, bool set_data = true, bool hide_element = false);
void populate(bool set_data = true, bool hide_element = false);
private:
QString m_path; QString m_path;
QIcon m_icon;
}; };
#endif // FILEELEMENTCOLLECTIONITEM_H #endif // FILEELEMENTCOLLECTIONITEM2_H

View File

@@ -1,266 +1,32 @@
/* /*
Copyright 2006-2016 The QElectroTech Team Copyright 2006-2016 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
QElectroTech is distributed in the hope that it will be useful, QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "xmlprojectelementcollectionitem.h"
#include "qetproject.h" #include "qetproject.h"
#include "qeticons.h"
#include "xmlelementcollection.h" #include "xmlelementcollection.h"
#include "nameslist.h" #include "qeticons.h"
#include "qetapp.h" #include "xmlprojectelementcollectionitem.h"
#include "elementcollectionhandler.h"
#include <algorithm>
/** /**
* @brief XmlProjectElementCollectionItem::XmlProjectElementCollectionItem * @brief XmlProjectElementCollectionItem::XmlProjectElementCollectionItem
* Default constructor. * Constructor
* @param project -project for this item
* @param parent -paretn item
*/ */
XmlProjectElementCollectionItem::XmlProjectElementCollectionItem(QETProject *project, ElementCollectionItem *parent) : XmlProjectElementCollectionItem::XmlProjectElementCollectionItem()
ElementCollectionItem(parent),
m_project(project)
{
m_dom_element = project->embeddedElementCollection()->root();
populate();
}
/**
* @brief XmlProjectElementCollectionItem::XmlProjectElementCollectionItem
* Private constructor
* @param project -project for this item
* @param dom_element: the dom_element must represent this item
* @param parent
*/
XmlProjectElementCollectionItem::XmlProjectElementCollectionItem(QETProject *project, const QDomElement &dom_element, ElementCollectionItem *parent) :
ElementCollectionItem(parent),
m_project(project),
m_dom_element(dom_element)
{
populate();
}
/**
* @brief XmlProjectElementCollectionItem::~XmlProjectElementCollectionItem
*/
XmlProjectElementCollectionItem::~XmlProjectElementCollectionItem()
{} {}
/**
* @brief XmlProjectElementCollectionItem::data
* The data used by the view who display this item through the model
* @param column
* @param role
* @return
*/
QVariant XmlProjectElementCollectionItem::data(int column, int role)
{
if (column > 0)
return QVariant();
switch (role)
{
case Qt::DisplayRole:
return name();
break;
case Qt::DecorationRole:
if (isCollectionRoot())
return QIcon(QET::Icons::ProjectFileGP);
else if (isDir())
return QET::Icons::Folder;
else
{
if (m_icon.isNull())
{
ElementsLocation loc(embeddedPath(), m_project);
m_icon = loc.icon();
}
return m_icon;
}
break;
case Qt::ToolTipRole:
if (isCollectionRoot())
return m_project->filePath();
else
return collectionPath();
break;
default:
return ElementCollectionItem::data(column, role);
}
}
void XmlProjectElementCollectionItem::clearData()
{
m_icon = QIcon();
ElementCollectionItem::clearData();
}
/**
* @brief XmlProjectElementCollectionItem::mimeData
* @return The mimedata of this item
*/
QMimeData *XmlProjectElementCollectionItem::mimeData()
{
QMimeData *mime_data = new QMimeData();
mime_data->setText(collectionPath());
if (isElement())
mime_data->setData("application/x-qet-element-uri", collectionPath().toLatin1());
else
mime_data->setData("application/x-qet-category-uri", collectionPath().toLatin1());
return mime_data;
}
/**
* @brief XmlProjectElementCollectionItem::canDropMimeData
* @param data
* @param action
* @param row
* @param column
* @return True if the data can be dropped
*/
bool XmlProjectElementCollectionItem::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column) const
{
Q_UNUSED(action); Q_UNUSED(row); Q_UNUSED(column);
if (data->hasFormat("application/x-qet-element-uri") || data->hasFormat("application/x-qet-category-uri"))
{
//Return false if user try to drop a item from a folder to the same folder
ElementsLocation drop_location(data->text());
for (int i=0 ; i<childCount() ; i++)
{
if (static_cast<XmlProjectElementCollectionItem *>(child(i))->collectionPath() == drop_location.collectionPath())
return false;
}
return true;
}
else
return false;
}
/**
* @brief XmlProjectElementCollectionItem::dropMimeData
* @param data
* @param action
* @param row
* @param column
* @return handle a drop of a mime data
*/
bool XmlProjectElementCollectionItem::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column)
{
Q_UNUSED(action); Q_UNUSED(row); Q_UNUSED(column);
XmlProjectElementCollectionItem *xpeci = this;
if (isElement() && parent() && parent()->type() == XmlProjectElementCollectionItem::Type)
xpeci = static_cast<XmlProjectElementCollectionItem *>(parent());
//before do the copy, we get all collection path of child,
//for remove it if the copied item have the same path of an existing child.
//We can't do this after the copy, because at the copy if the xml collection have a DomElement with the same path,
//he was removed before the new xml DomElement is inserted
//So the existing child of this will return a null QString when call collectionPath(), because the item
//doesn't exist anymore in the xml collection.
QList <QString> child_path_list;
for (int i=0 ; i<childCount() ; i++)
child_path_list.append(static_cast<XmlProjectElementCollectionItem *>(child(i))->collectionPath());
ElementCollectionHandler ech;
ElementsLocation source(data->text());
ElementsLocation destination(xpeci->collectionPath());
ElementsLocation location = ech.copy(source, destination);
if (location.exist())
{
//If this item have a child with the same path of location, we remove the existing child before insert new child
for (int i=0 ; i<child_path_list.size() ; i++)
if (child_path_list.at(i) == location.projectCollectionPath())
removeChild(i,1);
insertNewItem(location.fileName());
return true;
}
return false;
}
/**
* @brief XmlProjectElementCollectionItem::flags
* @return The flags of this item
*/
Qt::ItemFlags XmlProjectElementCollectionItem::flags()
{
if (isDir())
return (Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
else
return (Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);
}
/**
* @brief XmlProjectElementCollectionItem::isCollectionRoot
* @return True if this item represent the root collection of a project
*/
bool XmlProjectElementCollectionItem::isCollectionRoot() const
{
if (!m_parent_item) return true;
else if (m_parent_item->type() != XmlProjectElementCollectionItem::Type) return true;
else return false;
}
/**
* @brief XmlProjectElementCollectionItem::name
* @return The name of this item, name is notably use for Qt::DisplayRole data
*/
QString XmlProjectElementCollectionItem::name()
{
if (!m_name.isNull()) return m_name;
if (isCollectionRoot())
{
if (m_project->title().isEmpty())
return QString("Projet sans titre");
else
return m_project->title();
}
else
{
ElementsLocation location (embeddedPath(), m_project);
m_name = location.name();
return m_name;
}
}
/**
* @brief XmlProjectElementCollectionItem::isValid
* @return Always true
*/
bool XmlProjectElementCollectionItem::isValid() const {
return true;
}
/**
* @brief XmlProjectElementCollectionItem::project
* @return The project for this collection item
*/
QETProject *XmlProjectElementCollectionItem::project() const {
return m_project;
}
/** /**
* @brief XmlProjectElementCollectionItem::isDir * @brief XmlProjectElementCollectionItem::isDir
* @return true if this item represent a directory * @return true if this item represent a directory
@@ -281,15 +47,49 @@ bool XmlProjectElementCollectionItem::isElement() const
else return false; else return false;
} }
/**
* @brief XmlProjectElementCollectionItem::localName
* @return the located name of this item
*/
QString XmlProjectElementCollectionItem::localName()
{
if (!text().isNull())
return text();
if (isCollectionRoot()) {
if (m_project->title().isEmpty())
setText(QObject::tr("Projet sans titre"));
else
setText(m_project->title());
}
else {
ElementsLocation location (embeddedPath(), m_project);
setText(location.name());
}
return text();
}
/**
* @brief XmlProjectElementCollectionItem::name
* @return The collection name of this item
*/
QString XmlProjectElementCollectionItem::name() const
{
return m_dom_element.attribute("name");
}
/** /**
* @brief XmlProjectElementCollectionItem::collectionPath * @brief XmlProjectElementCollectionItem::collectionPath
* @return The collection path of this item. * @return The path of this item relative to the collection.
* The path is in form : project0+embed://dir/subdir/myElement.elmt
*/ */
QString XmlProjectElementCollectionItem::collectionPath() const QString XmlProjectElementCollectionItem::collectionPath() const
{ {
ElementsLocation loc (embeddedPath(), m_project); ElementsLocation loc (embeddedPath(), m_project);
return loc.projectCollectionPath(); QString p = loc.projectCollectionPath();
if (p.isEmpty())
p = QObject::tr("Collection");
return p;
} }
/** /**
@@ -300,37 +100,42 @@ QString XmlProjectElementCollectionItem::collectionPath() const
QString XmlProjectElementCollectionItem::embeddedPath() const QString XmlProjectElementCollectionItem::embeddedPath() const
{ {
if (isCollectionRoot()) if (isCollectionRoot())
{
return "embed://"; return "embed://";
else if (parent()){
XmlProjectElementCollectionItem *xpeci = static_cast<XmlProjectElementCollectionItem *>(parent());
if (xpeci->isCollectionRoot())
return xpeci->embeddedPath() + name();
else
return xpeci->embeddedPath() + "/" + name();
} }
else else
{ return QString();
XmlProjectElementCollectionItem *parent = static_cast<XmlProjectElementCollectionItem *>(m_parent_item);
if (parent->isCollectionRoot())
return parent->embeddedPath() + collectionName();
else
return parent->embeddedPath() + "/" + collectionName();
}
} }
/** /**
* @brief XmlProjectElementCollectionItem::collectionName * @brief XmlProjectElementCollectionItem::isCollectionRoot
* @return The collection name of this item * @return true if this item represent the root of collection
*/ */
QString XmlProjectElementCollectionItem::collectionName() const { bool XmlProjectElementCollectionItem::isCollectionRoot() const
return m_dom_element.attribute("name");
}
/**
* @brief XmlProjectElementCollectionItem::insertNewItem
* When this XmlProjectElementCollectionItem is already created, we must to use this method for insert a new item.
* Befor use this, see rowForInsertItem and lastItemForPath
* @param collection_name : the collection name to search in the child of QDomElement.
*/
void XmlProjectElementCollectionItem::insertNewItem(const QString &collection_name)
{ {
if (collection_name.isEmpty()) return; if (!parent())
return true;
else if (parent()->type() != XmlProjectElementCollectionItem::Type)
return true;
else
return false;
}
/**
* @brief XmlProjectElementCollectionItem::addChildAtPath
* Ask to this item item to add a child with collection name @collection_name
* @param collection_name
*/
void XmlProjectElementCollectionItem::addChildAtPath(const QString &collection_name)
{
if (collection_name.isEmpty())
return;
QDomNodeList node_list; QDomNodeList node_list;
if (collection_name.endsWith(".elmt")) if (collection_name.endsWith(".elmt"))
@@ -349,31 +154,120 @@ void XmlProjectElementCollectionItem::insertNewItem(const QString &collection_na
} }
} }
XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem(m_project, child_element, this); XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem ();
insertChild(rowForInsertItem(collection_name), xpeci); insertRow(rowForInsertItem(collection_name), xpeci);
xpeci->setXmlElement(child_element, m_project);
xpeci->setUpData();
}
/**
* @brief XmlProjectElementCollectionItem::project
* @return the paretn project of the managed collection
*/
QETProject *XmlProjectElementCollectionItem::project() const
{
return m_project;
}
/**
* @brief XmlProjectElementCollectionItem::setProject
* Set the project for this item.
* Use this method for set this item the root of the collection
* @param project : project to manage the collection
* @param set_data : if true, call setUpData for every child of this item
*/
void XmlProjectElementCollectionItem::setProject(QETProject *project, bool set_data, bool hide_element)
{
if (m_project)
return;
m_project = project;
m_dom_element = project->embeddedElementCollection()->root();
populate(set_data, hide_element);
}
/**
* @brief XmlProjectElementCollectionItem::setUpData
* SetUp the data of this item
*/
void XmlProjectElementCollectionItem::setUpData()
{
//Setup the displayed name
localName();
if (isDir())
setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
else
setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);
setToolTip(collectionPath());
}
/**
* @brief XmlProjectElementCollectionItem::setUpIcon
* SetUp the icon of this item.
* Because icon use several memory, we use this method for setup icon instead setUpData.
*/
void XmlProjectElementCollectionItem::setUpIcon()
{
if (!icon().isNull())
return;
if (isCollectionRoot())
setIcon(QET::Icons::ProjectFileGP);
else if (isDir())
setIcon(QET::Icons::Folder);
else {
ElementsLocation loc(embeddedPath(), m_project);
setIcon(loc.icon());
}
} }
/** /**
* @brief XmlProjectElementCollectionItem::populate * @brief XmlProjectElementCollectionItem::populate
* Populate this item * Create the childs of this item
* @param set_data : if true, call setUpData for every child of this item
*/ */
void XmlProjectElementCollectionItem::populate() void XmlProjectElementCollectionItem::populate(bool set_data, bool hide_element)
{ {
QList <QDomElement> dom_category = m_project->embeddedElementCollection()->directories(m_dom_element); QList <QDomElement> dom_category = m_project->embeddedElementCollection()->directories(m_dom_element);
std::sort(dom_category.begin(), dom_category.end(), [](QDomElement a, QDomElement b){return (a.attribute("name") < b.attribute("name"));}); std::sort(dom_category.begin(), dom_category.end(), [](QDomElement a, QDomElement b){return (a.attribute("name") < b.attribute("name"));});
foreach (QDomElement element, dom_category) foreach (QDomElement element, dom_category)
{ {
XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem(m_project, element, this); XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem();
this->appendChild(xpeci); appendRow(xpeci);
xpeci->setXmlElement(element, m_project, set_data, hide_element);
if (set_data)
xpeci->setUpData();
} }
if (hide_element)
return;
QList <QDomElement> dom_elements = m_project->embeddedElementCollection()->elements(m_dom_element); QList <QDomElement> dom_elements = m_project->embeddedElementCollection()->elements(m_dom_element);
std::sort(dom_elements.begin(), dom_elements.end(), [](QDomElement a, QDomElement b){return (a.attribute("name") < b.attribute("name"));}); std::sort(dom_elements.begin(), dom_elements.end(), [](QDomElement a, QDomElement b){return (a.attribute("name") < b.attribute("name"));});
foreach (QDomElement element, dom_elements) foreach (QDomElement element, dom_elements)
{ {
XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem(m_project, element, this); XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem();
this->appendChild(xpeci); appendRow(xpeci);
xpeci->setXmlElement(element, m_project, set_data);
if (set_data)
xpeci->setUpData();
} }
} }
/**
* @brief XmlProjectElementCollectionItem::setXmlElement
* Set the managed content of this item
* @param element : the dom element (directory or element), to be managed by this item
* @param project : the parent project of managed collection
* @param set_data : if true, call setUpData for every child of this item
*/
void XmlProjectElementCollectionItem::setXmlElement(QDomElement element, QETProject *project, bool set_data, bool hide_element)
{
m_dom_element = element;
m_project = project;
populate(set_data, hide_element);
}

View File

@@ -1,27 +1,25 @@
/* /*
Copyright 2006-2016 The QElectroTech Team Copyright 2006-2016 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
QElectroTech is distributed in the hope that it will be useful, QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef XMLPROJECTELEMENTCOLLECTIONITEM_H #ifndef XMLPROJECTELEMENTCOLLECTIONITEM2_H
#define XMLPROJECTELEMENTCOLLECTIONITEM_H #define XMLPROJECTELEMENTCOLLECTIONITEM2_H
#include "elementcollectionitem.h" #include "elementcollectionitem.h"
#include <QDomElement> #include <QDomElement>
#include <QIcon>
class QETProject; class QETProject;
@@ -31,43 +29,33 @@ class QETProject;
*/ */
class XmlProjectElementCollectionItem : public ElementCollectionItem class XmlProjectElementCollectionItem : public ElementCollectionItem
{ {
Q_OBJECT
public:
XmlProjectElementCollectionItem(QETProject *project, ElementCollectionItem *parent = nullptr);
private:
XmlProjectElementCollectionItem (QETProject *project, const QDomElement &dom_element, ElementCollectionItem *parent = nullptr);
public: public:
~XmlProjectElementCollectionItem(); XmlProjectElementCollectionItem();
enum {Type = UserType + 2}; enum {Type = UserType+3};
virtual int type() const {return Type;} virtual int type() const { return Type; }
virtual QVariant data(int column, int role);
virtual void clearData();
virtual QMimeData *mimeData();
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 Qt::ItemFlags flags();
virtual bool isCollectionRoot() const;
virtual QString name();
virtual bool isValid() const;
QETProject *project() const;
virtual bool isDir() const; virtual bool isDir() const;
virtual bool isElement() const; virtual bool isElement() const;
QString collectionPath() const; virtual QString localName();
QString embeddedPath() const; virtual QString name() const;
virtual QString collectionName() const; virtual QString collectionPath() const;
void insertNewItem(const QString &collection_name); virtual QString embeddedPath() const;
virtual bool isCollectionRoot() const;
virtual void addChildAtPath(const QString &collection_name);
QETProject * project() const;
void setProject (QETProject *project, bool set_data = true, bool hide_element = false);
void setUpData();
void setUpIcon();
private: private:
void populate(); void populate(bool set_data = true, bool hide_element = false);
void setXmlElement(QDomElement element, QETProject *project, bool set_data = true, bool hide_element = false);
private: private:
QETProject *m_project; QETProject *m_project = nullptr;
QDomElement m_dom_element; QDomElement m_dom_element;
QIcon m_icon;
}; };
#endif // XMLPROJECTELEMENTCOLLECTIONITEM_H #endif // XMLPROJECTELEMENTCOLLECTIONITEM2_H

View File

@@ -85,14 +85,14 @@ void ElementDialog::setUpWidget()
m_tree_view = new QTreeView(this); m_tree_view = new QTreeView(this);
ElementsCollectionModel *model = new ElementsCollectionModel(m_tree_view); m_model = new ElementsCollectionModel(m_tree_view);
if (m_mode == OpenElement) {model->addCommonCollection();} if (m_mode == OpenElement) {m_model->addCommonCollection();}
model->addCustomCollection(); m_model->addCustomCollection();
foreach (QETProject *project, QETApp::registeredProjects()) { foreach (QETProject *project, QETApp::registeredProjects()) {
model->addProject(project); m_model->addProject(project);
} }
m_tree_view->setModel(model); m_tree_view->setModel(m_model);
m_tree_view->setHeaderHidden(true); m_tree_view->setHeaderHidden(true);
layout->addWidget(m_tree_view); layout->addWidget(m_tree_view);
@@ -138,7 +138,7 @@ void ElementDialog::setUpConnection()
*/ */
void ElementDialog::indexClicked(const QModelIndex &index) void ElementDialog::indexClicked(const QModelIndex &index)
{ {
ElementCollectionItem *eci = static_cast<ElementCollectionItem*> (index.internalPointer()); ElementCollectionItem *eci = static_cast<ElementCollectionItem*> (m_model->itemFromIndex(index));
m_location = ElementsLocation(eci->collectionPath()); m_location = ElementsLocation(eci->collectionPath());
checkCurrentLocation(); checkCurrentLocation();
} }

View File

@@ -24,6 +24,8 @@
class QDialogButtonBox; class QDialogButtonBox;
class QFileNameEdit; class QFileNameEdit;
class QTreeView; class QTreeView;
class ElementsCollectionModel;
/** /**
This class provides several dialogs to select an element or a category This class provides several dialogs to select an element or a category
(e.g. new or existing, for opening or for saving...). (e.g. new or existing, for opening or for saving...).
@@ -66,6 +68,7 @@ class ElementDialog : public QDialog
QDialogButtonBox *m_buttons_box = nullptr; QDialogButtonBox *m_buttons_box = nullptr;
QFileNameEdit *m_text_field = nullptr; QFileNameEdit *m_text_field = nullptr;
QTreeView *m_tree_view = nullptr; QTreeView *m_tree_view = nullptr;
ElementsCollectionModel *m_model= nullptr;
public: public:
static ElementsLocation getOpenElementLocation(QWidget *parent = nullptr); static ElementsLocation getOpenElementLocation(QWidget *parent = nullptr);

View File

@@ -61,7 +61,7 @@ NewElementWizard::~NewElementWizard() {
*/ */
void NewElementWizard::preselectedLocation(const ElementsLocation &location) void NewElementWizard::preselectedLocation(const ElementsLocation &location)
{ {
QModelIndex index = m_model->index(location); QModelIndex index = m_model->indexFromLocation(location);
if (index.isValid()) { if (index.isValid()) {
m_tree_view->scrollTo(index); m_tree_view->scrollTo(index);
m_tree_view->setCurrentIndex(index); m_tree_view->setCurrentIndex(index);
@@ -170,7 +170,7 @@ bool NewElementWizard::validStep1()
QModelIndex index = m_tree_view->currentIndex(); QModelIndex index = m_tree_view->currentIndex();
if (index.isValid()) { if (index.isValid()) {
ElementCollectionItem *eci = static_cast<ElementCollectionItem*>(index.internalPointer()); ElementCollectionItem *eci = static_cast<ElementCollectionItem*>(m_model->itemFromIndex(index));
if (eci && eci->isDir()) { if (eci && eci->isDir()) {
ElementsLocation loc(eci->collectionPath()); ElementsLocation loc(eci->collectionPath());
if (loc.exist()) { if (loc.exist()) {