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

@@ -53,10 +53,8 @@ bool FileElementCollectionItem::setRootPath(QString path)
{
m_path = path;
populate();
name();
return true;
}
return false;
}
@@ -73,8 +71,10 @@ QString FileElementCollectionItem::fileSystemPath() const
FileElementCollectionItem *parent = static_cast<FileElementCollectionItem*>(m_parent_item);
//Get the path of the parent.
QString path = parent->fileSystemPath();
return path + "/" + m_path;
if (parent->isCollectionRoot())
return parent->fileSystemPath() + m_path;
else
return parent->fileSystemPath() + "/" + m_path;
}
/**
@@ -135,19 +135,7 @@ QVariant FileElementCollectionItem::data(int column, int role)
switch (role)
{
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");
}
case Qt::DisplayRole: {
return name();
}
break;
@@ -193,7 +181,12 @@ QMimeData *FileElementCollectionItem::mimeData()
{
QMimeData *mime_data = new QMimeData();
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;
}
@@ -207,9 +200,9 @@ QMimeData *FileElementCollectionItem::mimeData()
bool FileElementCollectionItem::canDropMimeData(const QMimeData *data, Qt::DropAction action, int column) const
{
Q_UNUSED(action); Q_UNUSED(column);
if (isCommonCollection()) return false;
if (data->hasFormat("application/x-qet-element-uri") &&
fileSystemPath().startsWith(QETApp::customElementsDir()))
if (data->hasFormat("application/x-qet-element-uri") || data->hasFormat("application/x-qet-category-uri"))
return true;
else
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)
{
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;
}
@@ -303,27 +307,39 @@ QString FileElementCollectionItem::name()
else if (isDir())
{
//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 (isCollectionRoot())
{
if (m_path == QETApp::commonElementsDir())
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))
m_name = QString("");
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("");
//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("");
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();
//Return the name for the current langage.
NamesList nl;
nl.fromXml(root);
m_name = nl.name();
}
}
else if (isElement())
{
@@ -345,7 +361,6 @@ void FileElementCollectionItem::setPathName(QString path_name)
if (!m_parent_item) return;
m_path = path_name;
name();
//This isn't an element, we create the childs
if (!path_name.endsWith(".elmt"))
@@ -377,3 +392,64 @@ void FileElementCollectionItem::populate()
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;
}