this may be called beta... look for bugs
[qsnippetsmanager.git] / standarditemmodel.cpp
blob15e4cccb56841001889a7f58392d210b050607d6
1 #include "mainwindow.h"
2 #include "standarditemmodel.h"
4 StandardItemModel::StandardItemModel( MainWindow* aparent ) : p( aparent ) {
7 QStringList StandardItemModel::mimeTypes() const {
8 QStringList types;
9 types << "qsnip/itempointer";
10 types << "text/plain";
11 return types;
14 QMimeData* StandardItemModel::mimeData( const QModelIndexList &indexes ) const {
15 QMimeData *mimeData = new QMimeData();
16 QByteArray encodedData;
18 QDataStream stream( &encodedData, QIODevice::WriteOnly );
20 foreach( QModelIndex index, indexes ) {
21 Snippet* snippet = p->snippetForItem.value( itemFromIndex( index ) );
22 stream << QString::number( (long)itemFromIndex( index ) );
23 if( !snippet->isCategory() )
24 mimeData->setText( snippet->code() );
27 mimeData->setData( "qsnip/itempointer", encodedData );
28 return mimeData;
31 bool StandardItemModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
32 int, int, const QModelIndex &parent ) {
33 if( action == Qt::IgnoreAction )
34 return true;
36 QByteArray encodedData = data->data( "qsnip/itempointer" );
37 QDataStream stream( &encodedData, QIODevice::ReadOnly );
38 QStringList newItems;
40 while( !stream.atEnd() ) {
41 QString text;
42 stream >> text;
43 newItems << text;
46 QStandardItem* itemToMove = ( QStandardItem* )newItems.at( 0 ).toLong();
47 Snippet* newParentSnippet = p->snippetForItem.value( itemFromIndex( parent ) );
48 if( !newParentSnippet )
49 newParentSnippet = p->snippetForItem.value( invisibleRootItem() );
50 Snippet* snippet = p->snippetForItem.value( itemToMove );
52 if( !snippet || !itemToMove )
53 return false;
54 if( itemToMove->parent() == itemFromIndex( parent ) )
55 return false;
56 if( !newParentSnippet->isCategory() )
57 return false;
59 if( !itemToMove->parent() )
60 itemToMove = takeRow( itemToMove->index().row() ).at( 0 );
61 else
62 itemToMove = itemToMove->parent()->takeRow( itemToMove->index().row() ).at( 0 );
64 p->insertItem( itemToMove, p->snippetForItem.key( newParentSnippet ) );
66 return true;