I have application with QTreeView which contents an image of remote file system (webdav) and QTreeView contents local file system.
I want to implement drag'n'drop actions for my app.
It works perfectly when i move items between my app's widgets or from outside my app. in QAbstractItemModel::dropMimeData i can control all activity. It looks like that:
bool MyModel
::dropMimeData (const QMimeData * data, Qt
::DropAction action,
int row,
int column,
const QModelIndex & parent
) {
QList<QUrl> urls = data->urls();
foreach
(const QUrl url,urls
) {
LocalFileItem * item = static_cast<RemoteFileItem *>(parent.internalPointer());
if(url.scheme() == "file")
copeLocalFile(item,url);
// url looks like dav:///public/test.txt
else if(url.scheme() == "dav")
{
copyRemoteFile(remoteFile,item);
}
}
return false;
}
bool MyModel::dropMimeData (const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
{
QList<QUrl> urls = data->urls();
foreach(const QUrl url,urls)
{
LocalFileItem * item = static_cast<RemoteFileItem *>(parent.internalPointer());
if(url.scheme() == "file")
copeLocalFile(item,url);
// url looks like dav:///public/test.txt
else if(url.scheme() == "dav")
{
QString remoteFile = url.path();
copyRemoteFile(remoteFile,item);
}
}
return false;
}
To copy to clipboard, switch view to plain text mode
It also works good when I copy file from local tree to the desktop, or even to another place outside my app. I just set correct url like that:
QMimeData * MyModel
::mimeData (const QModelIndexList
& indexes
) const {
QList<QUrl> urls;
{
if (index.isValid())
{
FileItem * item = static_cast<FileItem*>(index.internalPointer());
urls.append(item.url()); // url looks like file://c://test.txt
}
}
mimeData->setUrls(urls);
return mimeData;
}
{
types << "text/uri-list";
return types;
}
QMimeData * MyModel::mimeData (const QModelIndexList & indexes) const
{
QMimeData * mimeData = new QMimeData();
QList<QUrl> urls;
foreach (const QModelIndex index, indexes)
{
if (index.isValid())
{
FileItem * item = static_cast<FileItem*>(index.internalPointer());
urls.append(item.url()); // url looks like file://c://test.txt
}
}
mimeData->setUrls(urls);
return mimeData;
}
QStringList MyModel::mimeTypes() const
{
QStringList types;
types << "text/uri-list";
return types;
}
To copy to clipboard, switch view to plain text mode
But the problem is to copy from remote tree to desktop. All drop actions are controlled by OS. So I cannot handle the moment when my item actually was dropped.
It should be noted that coppied file not exists in local file system while dropping.
I have an idea how to solve this problem:
reimplement QMimeData and method retrieveData() like that:
{
//Qt::MouseButtons buttons = QApplication::mouseButtons ();
// here i need check if mouse buttom was release(item was dropped), create temporary file, copy content of remote file into temp file and return path to this file;
return QMimeData::retrieveData(mimeType,type
);
}
QVariant MimeData::retrieveData (const QString & mimeType, QVariant::Type type) const
{
//Qt::MouseButtons buttons = QApplication::mouseButtons ();
// here i need check if mouse buttom was release(item was dropped), create temporary file, copy content of remote file into temp file and return path to this file;
return QMimeData::retrieveData(mimeType,type);
}
To copy to clipboard, switch view to plain text mode
but I cannot find the way to get state of mouse button, QApplication::mouseButtons does not returns actual state.
Any other ideas?
Bookmarks