mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-24 03:10:52 +01:00
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:
@@ -1,116 +1,39 @@
|
||||
/*
|
||||
Copyright 2006-2016 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
Copyright 2006-2016 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "elementcollectionitem.h"
|
||||
#include <QMimeData>
|
||||
#include <QBrush>
|
||||
|
||||
/**
|
||||
* @brief ElementCollectionItem::ElementCollectionItem
|
||||
* Constructor
|
||||
* @param parent : the parent item of this item
|
||||
*/
|
||||
ElementCollectionItem::ElementCollectionItem(ElementCollectionItem *parent) :
|
||||
QObject(parent),
|
||||
m_parent_item (parent)
|
||||
ElementCollectionItem::ElementCollectionItem()
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief ElementCollectionItem::~ElementCollectionItem
|
||||
* Destructor
|
||||
* @brief ElementCollectionItem::clearData
|
||||
* Reset the data
|
||||
*/
|
||||
ElementCollectionItem::~ElementCollectionItem() {
|
||||
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)
|
||||
void ElementCollectionItem::clearData()
|
||||
{
|
||||
if (!(1 <= row+count && row+count <= m_child_items.size())) return false;
|
||||
|
||||
int last_ = row + (count-1);
|
||||
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;
|
||||
setText(QString());
|
||||
setToolTip(QString());
|
||||
setIcon(QIcon());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,10 +41,10 @@ ElementCollectionItem *ElementCollectionItem::childWithCollectionName(QString na
|
||||
* 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.
|
||||
* @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)
|
||||
*/
|
||||
ElementCollectionItem *ElementCollectionItem::lastItemForPath(const QString &path, QString &newt_item)
|
||||
ElementCollectionItem *ElementCollectionItem::lastItemForPath(const QString &path, QString &no_found_path)
|
||||
{
|
||||
QStringList str_list = path.split("/");
|
||||
if (str_list.isEmpty()) return nullptr;
|
||||
@@ -132,7 +55,7 @@ ElementCollectionItem *ElementCollectionItem::lastItemForPath(const QString &pat
|
||||
ElementCollectionItem *eci = return_eci->childWithCollectionName(str);
|
||||
if (!eci)
|
||||
{
|
||||
newt_item = str;
|
||||
no_found_path = str;
|
||||
return return_eci;
|
||||
}
|
||||
else
|
||||
@@ -143,27 +66,36 @@ ElementCollectionItem *ElementCollectionItem::lastItemForPath(const QString &pat
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementCollectionItem::itemAtPath
|
||||
* @param path
|
||||
* @return the item at path or nullptr if doesn't exist
|
||||
* @brief ElementCollectionItem::childWithCollectionName
|
||||
* Return the child with the collection name @name, else return nullptr
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
ElementCollectionItem *ElementCollectionItem::itemAtPath(const QString &path)
|
||||
ElementCollectionItem *ElementCollectionItem::childWithCollectionName(QString name) const
|
||||
{
|
||||
QStringList str_list = path.split("/");
|
||||
if (str_list.isEmpty()) return nullptr;
|
||||
|
||||
ElementCollectionItem *match_eci = this;
|
||||
foreach (QString str, str_list) {
|
||||
ElementCollectionItem *eci = match_eci->childWithCollectionName(str);
|
||||
if (!eci) {
|
||||
return nullptr;
|
||||
}
|
||||
else {
|
||||
match_eci = eci;
|
||||
}
|
||||
rowCount();
|
||||
foreach (QStandardItem *qsi, directChilds()) {
|
||||
ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsi);
|
||||
if (eci->name() == name)
|
||||
return 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
|
||||
* @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;
|
||||
//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
|
||||
if (child.isEmpty())
|
||||
return childCount();
|
||||
return rowCount();
|
||||
}
|
||||
//The item is a directory, we search from directory child
|
||||
else
|
||||
{
|
||||
child = directoriesChild();
|
||||
child = directoriesDirectChild();
|
||||
//There isn't directory, we insert at first position
|
||||
if(child.isEmpty())
|
||||
return 0;
|
||||
}
|
||||
|
||||
foreach (ElementCollectionItem *eci, child)
|
||||
if (eci->collectionName() > collection_name)
|
||||
return indexOfChild(eci);
|
||||
if (eci->name() > name)
|
||||
return model()->indexFromItem(eci).row();
|
||||
|
||||
return (indexOfChild(child.last())+1);
|
||||
return (model()->indexFromItem(child.last()).row() + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementCollectionItem::insertNewItem
|
||||
* By defualt do nothing, implement this method in subclass
|
||||
* to handle the insertion of a new item with name collection_name
|
||||
* @brief ElementCollectionItem::itemAtPath
|
||||
* @param path
|
||||
* @return the item at path or nullptr if doesn't exist
|
||||
*/
|
||||
void ElementCollectionItem::insertNewItem(const QString &collection_name) {Q_UNUSED (collection_name);}
|
||||
|
||||
/**
|
||||
* @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)
|
||||
ElementCollectionItem *ElementCollectionItem::itemAtPath(const QString &path)
|
||||
{
|
||||
Q_UNUSED(column);
|
||||
QStringList str_list = path.split("/");
|
||||
if (str_list.isEmpty())
|
||||
return nullptr;
|
||||
|
||||
switch (role)
|
||||
{
|
||||
case Qt::BackgroundRole:
|
||||
{
|
||||
if(m_show_bg_color)
|
||||
return QBrush(m_bg_color);
|
||||
else
|
||||
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();
|
||||
ElementCollectionItem *match_eci = this;
|
||||
foreach (QString str, str_list) {
|
||||
ElementCollectionItem *eci = match_eci->childWithCollectionName(str);
|
||||
if (!eci)
|
||||
return nullptr;
|
||||
else
|
||||
match_eci = eci;
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
return match_eci;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementCollectionItem::clearData
|
||||
* Reset the curent name
|
||||
* @brief ElementCollectionItem::elementsDirectChild
|
||||
* @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
|
||||
* @return The mime data of this item
|
||||
* @brief ElementCollectionItem::directoriesDirectChild
|
||||
* @return the direct directory child of this item
|
||||
*/
|
||||
QMimeData *ElementCollectionItem::mimeData() {
|
||||
return new QMimeData();
|
||||
}
|
||||
|
||||
bool ElementCollectionItem::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column) const
|
||||
QList<ElementCollectionItem *> ElementCollectionItem::directoriesDirectChild() const
|
||||
{
|
||||
Q_UNUSED(data); Q_UNUSED(action); Q_UNUSED(row); Q_UNUSED(column);
|
||||
return false;
|
||||
}
|
||||
QList <ElementCollectionItem *> dir_child;
|
||||
|
||||
bool ElementCollectionItem::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column)
|
||||
{
|
||||
Q_UNUSED(data); Q_UNUSED(action); Q_UNUSED(row); Q_UNUSED(column);
|
||||
return false;
|
||||
}
|
||||
foreach (QStandardItem *qsi, directChilds()) {
|
||||
ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsi);
|
||||
if (eci->isDir())
|
||||
dir_child.append(eci);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
return dir_child;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementCollectionItem::items
|
||||
* @return all child and subchild subsubchild... contained by this item
|
||||
* This item isn't stored in the list
|
||||
* @return every childs of this item (direct and indirect childs)
|
||||
*/
|
||||
QList<ElementCollectionItem *> ElementCollectionItem::items() const
|
||||
{
|
||||
QList<ElementCollectionItem *> list;
|
||||
list.append(m_child_items);
|
||||
foreach(ElementCollectionItem *eci, m_child_items)
|
||||
QList <ElementCollectionItem *> list;
|
||||
|
||||
for (int i=0 ; i<rowCount() ; i++) {
|
||||
ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(child(i));
|
||||
list.append(eci);
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user