Enable the drag and drop inside the new element panel

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4284 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2015-12-09 20:27:31 +00:00
parent 827fba8ce4
commit 6bf52bb525
10 changed files with 324 additions and 154 deletions

View File

@@ -163,3 +163,17 @@ bool ElementCollectionItem::isElement() const {
bool ElementCollectionItem::isValid() const { bool ElementCollectionItem::isValid() const {
return false; return false;
} }
/**
* @brief ElementCollectionItem::items
* @return all child and subchild subsubchild... contained by this item
* This item isn't stored in the list
*/
QList<ElementCollectionItem *> ElementCollectionItem::items() const
{
QList<ElementCollectionItem *> list;
list.append(m_child_items);
foreach(ElementCollectionItem *eci, m_child_items)
list.append(eci->items());
return list;
}

View File

@@ -55,6 +55,7 @@ class ElementCollectionItem
virtual bool isDir() const; virtual bool isDir() const;
virtual bool isElement() const; virtual bool isElement() const;
virtual bool isValid() const; virtual bool isValid() const;
virtual QList <ElementCollectionItem *> items() const;
protected: protected:
ElementCollectionItem *m_parent_item; ElementCollectionItem *m_parent_item;

View File

@@ -1,19 +1,19 @@
/* /*
Copyright 2006-2015 The QElectroTech Team Copyright 2006-2015 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 "elementlocation.h" #include "elementlocation.h"
#include "qetapp.h" #include "qetapp.h"
@@ -25,10 +25,10 @@
* @param path : path of item in file system * @param path : path of item in file system
*/ */
ElementLocation::ElementLocation(QString path): ElementLocation::ElementLocation(QString path):
m_project(nullptr) m_project(nullptr)
{ {
if (!path.isEmpty()) if (!path.isEmpty())
setPath(path); setPath(path);
} }
/** /**
@@ -37,10 +37,10 @@ ElementLocation::ElementLocation(QString path):
* @param project : project * @param project : project
*/ */
ElementLocation::ElementLocation(QString path, QETProject *project) : ElementLocation::ElementLocation(QString path, QETProject *project) :
m_project(project) m_project(project)
{ {
if (!path.isEmpty()) if (!path.isEmpty())
setPath(path); setPath(path);
} }
ElementLocation::~ElementLocation() ElementLocation::~ElementLocation()
@@ -56,68 +56,100 @@ ElementLocation::~ElementLocation()
*/ */
bool ElementLocation::setPath(QString path) bool ElementLocation::setPath(QString path)
{ {
if (!path.endsWith(".elmt")) return false; QString tmp_path = path;
QString tmp_path = path; //The path is in file system
if (!m_project)
{
//The given path is relative to common or custom collection
if (path.startsWith("common://") || path.startsWith("custom://"))
{
QString p;
if (path.startsWith("common://"))
{
tmp_path.remove("common://");
p = QETApp::commonElementsDir() + tmp_path;
}
else
{
tmp_path.remove("custom://");
p = QETApp::customElementsDir() + tmp_path;
}
//The path is in file system //This is an element
if (!m_project) if (path.endsWith(".elmt"))
{ {
//Common collection QFile file(p);
if (path.startsWith("common://")) if (file.exists())
{ {
tmp_path.remove("common://"); m_file_system_path = p;
QString p = QETApp::commonElementsDir() + tmp_path; m_collection_path = path;
QFile file(p); return true;
if (file.exists()) }
{ return false;
m_file_system_path = p; }
m_collection_path = path; //They must be a directory
return true; else
} {
QDir dir(p);
return false; if(dir.exists())
} {
m_file_system_path = p;
//Custom collection m_collection_path = path;
if (path.startsWith("custom://")) return true;
{ }
tmp_path.remove("custom://"); return false;
QString p = QETApp::customElementsDir() + tmp_path; }
QFile file(p); }
if (file.exists()) //In this case, the path is supposed to be relative to the file system.
{ else
m_file_system_path = p; {
m_collection_path = path; if(path.endsWith(".elmt"))
return true; {
} QFile file(path);
if (file.exists())
return false; {
} m_file_system_path = path;
//In this case, the path is supposed to be relative to the file system. if (path.startsWith(QETApp::commonElementsDir()))
else {
{ path.remove(QETApp::commonElementsDir());
QFile file(path); path.prepend("common://");
if (file.exists()) m_collection_path = path;
{ }
m_file_system_path = path; else if (path.startsWith(QETApp::customElementsDir()))
if (path.startsWith(QETApp::commonElementsDir())) {
{ path.remove(QETApp::customElementsDir());
path.remove(QETApp::commonElementsDir()); path.prepend("custom://");
path.prepend("common://"); m_collection_path = path;
m_collection_path = path; }
} return true;
else if (path.startsWith(QETApp::customElementsDir())) }
{ return false;
path.remove(QETApp::customElementsDir()); }
path.prepend("common://"); else
m_collection_path = path; {
} QDir dir(path);
return true; if (dir.exists())
} {
return false; m_file_system_path = path;
} if (path.startsWith(QETApp::commonElementsDir()))
} {
path.remove(QETApp::commonElementsDir());
path.prepend("common://");
m_collection_path = path;
}
else if (path.startsWith(QETApp::customElementsDir()))
{
path.remove(QETApp::customElementsDir());
path.prepend("custom://");
m_collection_path = path;
}
return true;
}
return false;
}
}
}
return false; return false;
} }
@@ -136,38 +168,52 @@ bool ElementLocation::isNull() const
*/ */
void ElementLocation::setProject(QETProject *project) void ElementLocation::setProject(QETProject *project)
{ {
m_project = project; m_project = project;
}
/**
* @brief ElementLocation::isElement
* @return true if this location represent an element
*/
bool ElementLocation::isElement() const {
return m_collection_path.endsWith(".elmt");
}
/**
* @brief ElementLocation::isDirectory
* @return true if this location represent a directory
*/
bool ElementLocation::isDirectory() const {
return !isElement();
} }
/** /**
* @brief ElementLocation::collectionPath * @brief ElementLocation::collectionPath
* @return the colletion relative to the collection * @return the colletion relative to the collection
*/ */
QString ElementLocation::collectionPath() const QString ElementLocation::collectionPath() const {
{ return m_collection_path;
return m_collection_path;
} }
/** /**
* @brief ElementLocation::fileSystemPath * @brief ElementLocation::fileSystemPath
* @return The file system path of this element, * @return The file system path of this element, (the separator is always '/' see QDir::toNativeSeparators())
* If this element is embedded in a project return an empty string; * If this element is embedded in a project return an empty string;
*/ */
QString ElementLocation::fileSystemPath() const QString ElementLocation::fileSystemPath() const
{ {
if (!m_project) if (!m_project)
return QDir::fromNativeSeparators(m_file_system_path); return m_file_system_path;
else else
return QString(); return QString();
} }
/** /**
* @brief ElementLocation::project * @brief ElementLocation::project
* @return the project of this location if he was set. * @return the project of this location if he was set.
*/ */
QETProject *ElementLocation::project() const QETProject *ElementLocation::project() const {
{ return m_project;
return m_project;
} }
/** /**
@@ -177,18 +223,18 @@ QETProject *ElementLocation::project() const
*/ */
QDomElement ElementLocation::xml() QDomElement ElementLocation::xml()
{ {
if (!m_xml.isNull()) if (!m_xml.isNull())
return m_xml; return m_xml;
if (!m_project) if (!m_project)
{ {
QFile file (m_file_system_path); QFile file (m_file_system_path);
QDomDocument docu; QDomDocument docu;
if (docu.setContent(&file)) if (docu.setContent(&file))
m_xml = docu.documentElement().cloneNode().toElement(); m_xml = docu.documentElement().cloneNode().toElement();
} }
return m_xml; return m_xml;
} }
/** /**
@@ -197,15 +243,13 @@ QDomElement ElementLocation::xml()
*/ */
QUuid ElementLocation::uuid() QUuid ElementLocation::uuid()
{ {
if (!m_uuid.isNull()) return m_uuid; if (!m_uuid.isNull()) return m_uuid;
//Get the uuid of element //Get the uuid of element
QList<QDomElement> list_ = QET::findInDomElement(xml(), "uuid"); QList<QDomElement> list_ = QET::findInDomElement(xml(), "uuid");
if (!list_.isEmpty()) if (!list_.isEmpty())
m_uuid = QUuid(list_.first().attribute("uuid")); m_uuid = QUuid(list_.first().attribute("uuid"));
// else
// qDebug() << "The element : " << m_file_system_path << "haven't got an uuid, please edit and save this element with element editor to create an uuid";
return m_uuid; return m_uuid;
} }

View File

@@ -27,7 +27,7 @@ class QETProject;
/** /**
* @brief The ElementLocation class * @brief The ElementLocation class
* This class represent the location of an element in the file system * This class represent the location of an element or a directory in the file system
* or an embedded collection of a project. * or an embedded collection of a project.
* They also provide common things about an element, like the icon, uuid etc... * They also provide common things about an element, like the icon, uuid etc...
*/ */
@@ -41,6 +41,8 @@ class ElementLocation
bool setPath(QString path); bool setPath(QString path);
bool isNull() const; bool isNull() const;
void setProject(QETProject *project); void setProject(QETProject *project);
bool isElement() const;
bool isDirectory() const;
QString collectionPath() const; QString collectionPath() const;
QString fileSystemPath() const; QString fileSystemPath() const;

View File

@@ -197,10 +197,21 @@ bool ElementsCollectionModel::dropMimeData(const QMimeData *data, Qt::DropAction
QStringList ElementsCollectionModel::mimeTypes() const QStringList ElementsCollectionModel::mimeTypes() const
{ {
QStringList mime_list = QAbstractItemModel::mimeTypes(); QStringList mime_list = QAbstractItemModel::mimeTypes();
mime_list << "application/x-qet-element-uri"; mime_list << "application/x-qet-element-uri" << "application/x-qet-category-uri";
return mime_list; return mime_list;
} }
/**
* @brief ElementsCollectionModel::items
* @return All items handled by this model. The root item isn't stored in the list
*/
QList<ElementCollectionItem *> ElementsCollectionModel::items() const
{
QList <ElementCollectionItem *> list;
list.append(m_root_item->items());
return list;
}
/** /**
* @brief ElementsCollectionModel::addCommonCollection * @brief ElementsCollectionModel::addCommonCollection
* Add the common elements collection to this model * Add the common elements collection to this model

View File

@@ -46,6 +46,7 @@ class ElementsCollectionModel : public QAbstractItemModel
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; QStringList mimeTypes() const;
QList <ElementCollectionItem *> items() const;
void addCommonCollection(); void addCommonCollection();
void addCustomCollection(); void addCustomCollection();

View File

@@ -40,11 +40,14 @@
*/ */
ElementsCollectionWidget::ElementsCollectionWidget(QWidget *parent): ElementsCollectionWidget::ElementsCollectionWidget(QWidget *parent):
QWidget(parent), QWidget(parent),
m_model(nullptr),
m_item_at_context_menu(nullptr) m_item_at_context_menu(nullptr)
{ {
setUpWidget(); setUpWidget();
setUpAction(); setUpAction();
setUpConnection(); setUpConnection();
reload();
} }
/** /**
@@ -100,11 +103,11 @@ void ElementsCollectionWidget::setUpWidget()
m_tree_view->setContextMenuPolicy(Qt::CustomContextMenu); m_tree_view->setContextMenuPolicy(Qt::CustomContextMenu);
m_main_vlayout->addWidget(m_tree_view); m_main_vlayout->addWidget(m_tree_view);
//Setup the element collection model //Setup the progress bar
m_model = new ElementsCollectionModel(m_tree_view); m_progress_bar = new QProgressBar(this);
m_model->addCommonCollection(); m_progress_bar->setFormat(tr("Chargement") + " %p%");
m_model->addCustomCollection(); m_main_vlayout->addWidget(m_progress_bar);
m_tree_view->setModel(m_model); m_progress_bar->hide();
m_context_menu = new QMenu(this); m_context_menu = new QMenu(this);
} }
@@ -345,13 +348,25 @@ void ElementsCollectionWidget::newElement()
*/ */
void ElementsCollectionWidget::reload() void ElementsCollectionWidget::reload()
{ {
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();
new_model->addCustomCollection(); new_model->addCustomCollection();
QList <ElementCollectionItem *> list = new_model->items();
m_progress_bar->setMaximum(list.size());
m_progress_bar->setValue(0);
foreach (ElementCollectionItem *item, new_model->items())
{
item->name();
m_progress_bar->setValue(m_progress_bar->value() + 1);
}
m_tree_view->setModel(new_model); m_tree_view->setModel(new_model);
delete m_model; if (m_model) delete m_model;
m_model = new_model; m_model = new_model;
expandFirstItems(); expandFirstItems();
m_progress_bar->hide();
} }
/** /**

View File

@@ -27,6 +27,7 @@ class QVBoxLayout;
class QMenu; class QMenu;
class QLineEdit; class QLineEdit;
class ElementCollectionItem; class ElementCollectionItem;
class QProgressBar;
/** /**
* @brief The ElementsCollectionWidget class * @brief The ElementsCollectionWidget class
@@ -69,6 +70,7 @@ class ElementsCollectionWidget : public QWidget
QVBoxLayout *m_main_vlayout; QVBoxLayout *m_main_vlayout;
QMenu *m_context_menu; QMenu *m_context_menu;
ElementCollectionItem *m_item_at_context_menu; ElementCollectionItem *m_item_at_context_menu;
QProgressBar *m_progress_bar;
QAction *m_open_dir, QAction *m_open_dir,
*m_edit_element, *m_edit_element,

View File

@@ -53,10 +53,8 @@ bool FileElementCollectionItem::setRootPath(QString path)
{ {
m_path = path; m_path = path;
populate(); populate();
name();
return true; return true;
} }
return false; return false;
} }
@@ -73,8 +71,10 @@ QString FileElementCollectionItem::fileSystemPath() const
FileElementCollectionItem *parent = static_cast<FileElementCollectionItem*>(m_parent_item); FileElementCollectionItem *parent = static_cast<FileElementCollectionItem*>(m_parent_item);
//Get the path of the parent. //Get the path of the parent.
QString path = parent->fileSystemPath(); if (parent->isCollectionRoot())
return path + "/" + m_path; return parent->fileSystemPath() + m_path;
else
return parent->fileSystemPath() + "/" + m_path;
} }
/** /**
@@ -135,19 +135,7 @@ QVariant FileElementCollectionItem::data(int column, int role)
switch (role) switch (role)
{ {
case Qt::DisplayRole: case Qt::DisplayRole: {
{
//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::commonElementsDir())
return QObject::tr("Collection QET");
else if (m_path == QETApp::customElementsDir())
return QObject::tr("Collection utilisateur");
else
return QObject::tr("Collection inconnue");
}
return name(); return name();
} }
break; break;
@@ -193,7 +181,12 @@ QMimeData *FileElementCollectionItem::mimeData()
{ {
QMimeData *mime_data = new QMimeData(); QMimeData *mime_data = new QMimeData();
mime_data->setText(collectionPath()); mime_data->setText(collectionPath());
mime_data->setData("application/x-qet-element-uri", collectionPath().toLatin1());
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; return mime_data;
} }
@@ -207,9 +200,9 @@ QMimeData *FileElementCollectionItem::mimeData()
bool FileElementCollectionItem::canDropMimeData(const QMimeData *data, Qt::DropAction action, int column) const bool FileElementCollectionItem::canDropMimeData(const QMimeData *data, Qt::DropAction action, int column) const
{ {
Q_UNUSED(action); Q_UNUSED(column); Q_UNUSED(action); Q_UNUSED(column);
if (isCommonCollection()) return false;
if (data->hasFormat("application/x-qet-element-uri") && if (data->hasFormat("application/x-qet-element-uri") || data->hasFormat("application/x-qet-category-uri"))
fileSystemPath().startsWith(QETApp::customElementsDir()))
return true; return true;
else else
return false; return false;
@@ -224,7 +217,18 @@ bool FileElementCollectionItem::canDropMimeData(const QMimeData *data, Qt::DropA
*/ */
bool FileElementCollectionItem::dropMimeData(const QMimeData *data, Qt::DropAction action, int column) bool FileElementCollectionItem::dropMimeData(const QMimeData *data, Qt::DropAction action, int column)
{ {
Q_UNUSED(data); Q_UNUSED(action); Q_UNUSED(column); Q_UNUSED(action); Q_UNUSED(column);
if (isCommonCollection()) return false;
FileElementCollectionItem *feci = this;
if (isElement() && parent() && parent()->type() == FileElementCollectionItem::Type)
feci = static_cast<FileElementCollectionItem *>(parent());
if (data->hasFormat("application/x-qet-element-uri"))
return feci->handleElementDrop(data);
else if (data->hasFormat("application/x-qet-category-uri"))
return feci->handleDirectoryDrop(data);
return false; return false;
} }
@@ -303,27 +307,39 @@ QString FileElementCollectionItem::name()
else if (isDir()) else if (isDir())
{ {
//Open the qet_directory file, to get the traductions name of this dir if (isCollectionRoot())
QFile dir_conf(fileSystemPath() + "/qet_directory"); {
if (!dir_conf.exists()) if (m_path == QETApp::commonElementsDir())
m_name = QString(""); m_name = QObject::tr("Collection QET");
else if (m_path == QETApp::customElementsDir())
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)) if (!dir_conf.open(QIODevice::ReadOnly | QIODevice::Text))
m_name = QString(""); m_name = QString("");
//Get the content of the file //Get the content of the file
QDomDocument document; QDomDocument document;
if (!document.setContent(&dir_conf)) if (!document.setContent(&dir_conf))
m_name = QString(""); m_name = QString("");
QDomElement root = document.documentElement(); QDomElement root = document.documentElement();
if (root.tagName() != "qet-directory") if (root.tagName() != "qet-directory")
m_name = QString(""); m_name = QString("");
//Return the name for the current langage. //Return the name for the current langage.
NamesList nl; NamesList nl;
nl.fromXml(root); nl.fromXml(root);
m_name = nl.name(); m_name = nl.name();
}
} }
else if (isElement()) else if (isElement())
{ {
@@ -345,7 +361,6 @@ void FileElementCollectionItem::setPathName(QString path_name)
if (!m_parent_item) return; if (!m_parent_item) return;
m_path = path_name; m_path = path_name;
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"))
@@ -377,3 +392,64 @@ void FileElementCollectionItem::populate()
appendChild(feci); appendChild(feci);
} }
} }
/**
* @brief FileElementCollectionItem::handleElementDrop
* Handle a drop data that represente an element.
* @param data
* @return true if the data is successfully dropped
*/
bool FileElementCollectionItem::handleElementDrop(const QMimeData *data)
{
ElementLocation location(data->text());
return QFile::copy(location.fileSystemPath(), fileSystemPath() + "/" + location.fileSystemPath().split("/").last());
}
/**
* @brief FileElementCollectionItem::handleDirectoryDrop
* Handle a drop data that represent a directory
* @param data
* @return true if the data is successfully dropped
*/
bool FileElementCollectionItem::handleDirectoryDrop(const QMimeData *data)
{
ElementLocation location(data->text());
QDir origin_dir(location.fileSystemPath());
if (origin_dir.exists())
return createSubDir(origin_dir, QDir(fileSystemPath()));
else
return false;
}
/**
* @brief FileElementCollectionItem::createSubDir
* Copy the directory @ dir_to_copy and the qet_directory file to destination.
* Also copy all directorys and elements find in @dir_to_copy recursively
* @param dir_to_copy
* @param destination
* @return true if the copy of @dir_to_copy to destination is successfull.
*/
bool FileElementCollectionItem::createSubDir(QDir dir_to_copy, QDir destination)
{
if (destination.mkdir(dir_to_copy.dirName()))
{
QDir created_dir(destination.canonicalPath() + "/" + dir_to_copy.dirName());
//Copy the qet_directory file
QFile::copy(dir_to_copy.canonicalPath() + "/qet_directory", created_dir.canonicalPath() +"/qet_directory");
//Copy all dirs found in dir_to_copy to destination
foreach(QString str, dir_to_copy.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name))
createSubDir(QDir(dir_to_copy.canonicalPath() + "/" + str), created_dir);
//Copy all elements found in dir_to_copy to destination
dir_to_copy.setNameFilters(QStringList() << "*.elmt");
foreach(QString str, dir_to_copy.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
QFile::copy(dir_to_copy.canonicalPath() + "/" + str, created_dir.canonicalPath() + "/" + str);
return true;
}
else
return false;
}

View File

@@ -21,6 +21,7 @@
#include "elementcollectionitem.h" #include "elementcollectionitem.h"
#include "elementlocation.h" #include "elementlocation.h"
#include <QString> #include <QString>
#include <QDir>
/** /**
* @brief The FileElementCollectionItem class * @brief The FileElementCollectionItem class
@@ -58,6 +59,9 @@ class FileElementCollectionItem : public ElementCollectionItem
private: private:
void setPathName(QString path_name); void setPathName(QString path_name);
void populate(); void populate();
bool handleElementDrop (const QMimeData *data);
bool handleDirectoryDrop (const QMimeData *data);
bool createSubDir (QDir dir_to_copy, QDir destination);
private: private:
QString m_path; QString m_path;