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,41 +1,33 @@
/*
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 "fileelementcollectionitem.h"
#include "qetapp.h"
#include "elementslocation.h"
#include "nameslist.h"
#include "qetapp.h"
#include "qeticons.h"
#include "elementcollectionhandler.h"
#include <QDir>
/**
* @brief FileElementCollectionItem::FileElementCollectionItem
* Default constructor
* @param parent : parent item of this item
* Constructor
*/
FileElementCollectionItem::FileElementCollectionItem(ElementCollectionItem *parent) :
ElementCollectionItem(parent)
{}
/**
* @brief FileElementCollectionItem::~FileElementCollectionItem
* Destructor
*/
FileElementCollectionItem::~FileElementCollectionItem()
FileElementCollectionItem::FileElementCollectionItem()
{}
/**
@@ -45,16 +37,15 @@ FileElementCollectionItem::~FileElementCollectionItem()
* @param path
* @return true if path exist.
*/
bool FileElementCollectionItem::setRootPath(QString path)
bool FileElementCollectionItem::setRootPath(QString path, bool set_data, bool hide_element)
{
QDir dir(path);
if (dir.exists())
{
QDir dir(path);
if (dir.exists()) {
m_path = path;
populate();
populate(set_data, hide_element);
return true;
}
return false;
}
@@ -64,226 +55,29 @@ bool FileElementCollectionItem::setRootPath(QString path)
*/
QString FileElementCollectionItem::fileSystemPath() const
{
//Parent must be a file element collection item
if (!m_parent_item || m_parent_item->type() != FileElementCollectionItem::Type)
if (isCollectionRoot())
return m_path;
FileElementCollectionItem *parent = static_cast<FileElementCollectionItem*>(m_parent_item);
//Get the path of the parent.
// 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;
}
FileElementCollectionItem *feci = static_cast<FileElementCollectionItem *> (parent());
if (feci)
return feci->fileSystemPath() + "/" + m_path;
else
return QString();
}
/**
* @brief FileElementCollectionItem::collectionName
* @return The collection name of this item
* @brief FileElementCollectionItem::dirPath
* @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
{
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()
QString FileElementCollectionItem::dirPath() const
{
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
return (Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);
return QString();
}
/**
@@ -296,16 +90,98 @@ bool FileElementCollectionItem::isDir() const
return false;
else
return true;
}
/**
* @brief FileElementCollectionItem::isElement
* @return true if this item represent an element
*/
bool FileElementCollectionItem::isElement() const {
bool FileElementCollectionItem::isElement() const
{
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
* @return true if this item represent the root of collection
@@ -320,84 +196,70 @@ bool FileElementCollectionItem::isCollectionRoot() const
/**
* @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());
}
/**
* @brief FileElementCollectionItem::isValid
* @return
* @brief FileElementCollectionItem::addChildAtPath
* 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 (m_path.isEmpty())
return false;
else
return true;
if (collection_name.isEmpty())
return;
FileElementCollectionItem *feci = new FileElementCollectionItem();
insertRow(rowForInsertItem(collection_name), feci);
feci->setPathName(collection_name);
feci->setUpData();
}
/**
* @brief FileElementCollectionItem::name
* @return the located name of this item
* @brief FileElementCollectionItem::setUpData
* 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 (isCollectionRoot())
{
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 (isDir())
setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
else
setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);
if (!dir_conf.open(QIODevice::ReadOnly | QIODevice::Text))
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;
setToolTip(collectionPath());
}
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);
feci->setPathName(collection_name);
insertChild(rowForInsertItem(collection_name), feci);
if (isCollectionRoot()) {
if (m_path == QETApp::commonElementsDirN())
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.
* @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;
//This isn't an element, we create the childs
if (!path_name.endsWith(".elmt"))
populate();
populate(set_data, hide_element);
}
/**
* @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());
//Get all directory in this directory.
foreach(QString str, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name))
{
FileElementCollectionItem *feci = new FileElementCollectionItem(this);
feci->setPathName(str);
appendChild(feci);
FileElementCollectionItem *feci = new FileElementCollectionItem();
appendRow(feci);
feci->setPathName(str, set_data, hide_element);
if (set_data)
feci->setUpData();
}
if (hide_element)
return;
//Get all elmt file in this directory
dir.setNameFilters(QStringList() << "*.elmt");
foreach(QString str, dir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
{
FileElementCollectionItem *feci = new FileElementCollectionItem(this);
feci->setPathName(str);
appendChild(feci);
FileElementCollectionItem *feci = new FileElementCollectionItem();
appendRow(feci);
feci->setPathName(str, set_data);
if (set_data)
feci->setUpData();
}
}