From 512fd4155489870ab667ca36cba76366b5d120bd Mon Sep 17 00:00:00 2001
From: Anna Udovichenko <anuta.udovichenko@gmail.com>
Date: Mon, 23 Apr 2012 18:10:27 +0300
Subject: [PATCH] Implemented deleting items

---
 src/board/UBFeaturesController.cpp |  70 +++++--------------
 src/board/UBFeaturesController.h   |  13 +++-
 src/gui/UBFeaturesActionBar.cpp    |  38 ++++++++++
 src/gui/UBFeaturesActionBar.h      |   8 ++-
 src/gui/UBFeaturesWidget.cpp       | 107 ++++++++++-------------------
 src/gui/UBFeaturesWidget.h         |   5 +-
 6 files changed, 114 insertions(+), 127 deletions(-)

diff --git a/src/board/UBFeaturesController.cpp b/src/board/UBFeaturesController.cpp
index cd260d5a..b6180dbe 100644
--- a/src/board/UBFeaturesController.cpp
+++ b/src/board/UBFeaturesController.cpp
@@ -55,6 +55,7 @@ void UBFeaturesController::initDirectoryTree()
 	mLibInteractiveDirectoryPath = UBSettings::settings()->applicationInteractivesDirectory();
 	mLibApplicationsDirectoryPath = UBSettings::settings()->applicationApplicationsLibraryDirectory();
 	mLibShapesDirectoryPath = UBSettings::settings()->applicationShapeLibraryDirectory() ;
+	trashDirectoryPath = UBSettings::userTrashDirPath();
 
 	featuresList = new QVector <UBFeature>();
 
@@ -70,6 +71,7 @@ void UBFeaturesController::initDirectoryTree()
 	flashPath = rootPath + "/Animations";
 	interactPath = rootPath + "/Interactivities";
 	shapesPath = rootPath + "/Shapes";
+	trashPath = rootPath + "/Trash";
 
 	featuresList->append( UBFeature( rootPath, QPixmap(":images/libpalette/AudiosCategory.svg"), "Audios" , mUserAudioDirectoryPath ) );
 	featuresList->append( UBFeature( rootPath, QPixmap(":images/libpalette/MoviesCategory.svg"), "Movies" , mUserVideoDirectoryPath ) );
@@ -78,6 +80,8 @@ void UBFeaturesController::initDirectoryTree()
 	featuresList->append( UBFeature( rootPath, QPixmap(":images/libpalette/FlashCategory.svg"), "Animations" , mUserAnimationDirectoryPath ) );
 	featuresList->append( UBFeature( rootPath, QPixmap(":images/libpalette/InteractivesCategory.svg"), "Interactivities" ,  mLibInteractiveDirectoryPath ) );
 	featuresList->append( UBFeature( rootPath, QPixmap(":images/libpalette/ShapesCategory.svg"), "Shapes" , mLibShapesDirectoryPath ) );
+	trashElement = UBFeature( rootPath, QPixmap(":images/libpalette/TrashCategory.svg"), "Trash", trashDirectoryPath, FEATURE_TRASH );
+	featuresList->append( trashElement );
 
 	foreach (UBToolsManager::UBToolDescriptor tool, tools)
 	{
@@ -93,6 +97,7 @@ void UBFeaturesController::initDirectoryTree()
 	fileSystemScan( mLibPicturesDirectoryPath, picturesPath  );
 	fileSystemScan( mLibShapesDirectoryPath, shapesPath  );
 	fileSystemScan( mLibInteractiveDirectoryPath, interactPath  );
+	fileSystemScan( trashDirectoryPath, trashPath );
 
 }
 
@@ -206,38 +211,15 @@ void UBFeaturesController::addItemToPage(const UBFeature &item)
 UBFeature UBFeaturesController::moveItemToFolder( const QUrl &url, const UBFeature &destination )
 {
 	UBFeature newElement = copyItemToFolder( url, destination );
-	QString sourcePath = url.toLocalFile();
-	QFile::remove( sourcePath );
-	QString thumbnailPath = UBFileSystemUtils::thumbnailPath( sourcePath );
-    if (thumbnailPath.length() && QFileInfo( thumbnailPath ).exists()) 
-	{
-        QFile::remove(thumbnailPath);
-    }
-	return newElement;
+	deleteItem( url );
 	/*QString sourcePath = url.toLocalFile();
-
-	Q_ASSERT( QFileInfo( sourcePath ).exists() );
-
-	QString name = QFileInfo( sourcePath ).fileName();
-	QString destPath = destination.getFullPath();
-	QString destVirtualPath = destination.getUrl() + "/" + destination.getName();
-	QString newFullPath = destPath + "/" + name;
-	QFile( sourcePath ).copy( newFullPath );
 	QFile::remove( sourcePath );
-
 	QString thumbnailPath = UBFileSystemUtils::thumbnailPath( sourcePath );
     if (thumbnailPath.length() && QFileInfo( thumbnailPath ).exists()) 
 	{
         QFile::remove(thumbnailPath);
-    }
-
-	QPixmap thumb = thumbnailForFile( newFullPath );
-	
-	UBFeatureElementType type = UBFeatureElementType::FEATURE_ITEM;
-	if ( UBFileSystemUtils::mimeTypeFromFileName( newFullPath ).contains("application") ) 
-        type = UBFeatureElementType::FEATURE_INTERACTIVE;
-	UBFeature newElement( destVirtualPath, thumb, name, destPath, type );
-	return newElement;*/
+    }*/
+	return newElement;
 }
 
 UBFeature UBFeaturesController::copyItemToFolder( const QUrl &url, const UBFeature &destination )
@@ -261,37 +243,23 @@ UBFeature UBFeaturesController::copyItemToFolder( const QUrl &url, const UBFeatu
 	return newElement;
 }
 
-/*
-void UBFeaturesController::addImageToCurrentPage( const QString &path )
+void UBFeaturesController::deleteItem( const QUrl &url )
 {
-	QPointF pos = UBApplication::boardController->activeScene()->normalizedSceneRect().center();
-	mLastItemOffsetIndex = qMin(mLastItemOffsetIndex, 5);
+	QString path = url.toLocalFile();
+	Q_ASSERT( QFileInfo( path ).exists() );
 
-    QGraphicsItem* itemInScene = 0;
-
-    if ( UBApplication::boardController->activeScene() ) 
+	QString thumbnailPath = UBFileSystemUtils::thumbnailPath( path );
+    if (thumbnailPath.length() && QFileInfo( thumbnailPath ).exists()) 
 	{
-        QString mimeType = UBFileSystemUtils::mimeTypeFromFileName( path );
-
-        pos = QPointF( pos.x() + 50 * mLastItemOffsetIndex, pos.y() + 50 * mLastItemOffsetIndex );
-        mLastItemOffsetIndex++;
-        //TODO UB 4.x move this logic to the scene ..
-        if (mimeType == "image/svg+xml") 
-		{
-			itemInScene = UBApplication::boardController->activeScene()->addSvg( QUrl::fromLocalFile(path), pos );
-        } 
-		else 
-		{
-            itemInScene = UBApplication::boardController->activeScene()->addPixmap( QPixmap(path), pos );
-        }
+        QFile::remove(thumbnailPath);
     }
+	QFile::remove( path );
+}
 
-    if (itemInScene) 
-	{
-        itemInScene = UBApplication::boardController->activeScene()->scaleToFitDocumentSize(itemInScene, false, UBSettings::objectInControlViewMargin);
-    }
+bool UBFeaturesController::isTrash( const QUrl &url )
+{
+	return url.toLocalFile().startsWith( trashDirectoryPath );
 }
-*/
 
 UBFeaturesController::~UBFeaturesController()
 {
diff --git a/src/board/UBFeaturesController.h b/src/board/UBFeaturesController.h
index 8a36cd44..f1c0e485 100644
--- a/src/board/UBFeaturesController.h
+++ b/src/board/UBFeaturesController.h
@@ -17,7 +17,8 @@ enum UBFeatureElementType
     FEATURE_FOLDER,
     FEATURE_INTERACTIVE,
 	FEATURE_INTERNAL,
-    FEATURE_ITEM
+    FEATURE_ITEM,
+	FEATURE_TRASH
 };
 
 class UBFeature
@@ -52,15 +53,18 @@ public:
 
     QVector <UBFeature>* getFeatures()const { return featuresList; }
 	
-    QString getRootPath()const { return rootPath; }
+    const QString& getRootPath()const { return rootPath; }
 
 	void addItemToPage(const UBFeature &item);
-	UBFeature getCurrentElement()const { return currentElement; }
+	const UBFeature& getCurrentElement()const { return currentElement; }
 	void setCurrentElement( const UBFeature &elem ) { currentElement = elem; }
+	const UBFeature & getTrashElement () const { return trashElement; }
 
 	static QPixmap thumbnailForFile( const QString &path );
 	static UBFeature moveItemToFolder( const QUrl &url, const UBFeature &destination );
 	static UBFeature copyItemToFolder( const QUrl &url, const UBFeature &destination );
+	static void deleteItem( const QUrl &url );
+	bool isTrash( const QUrl &url );
 	UBFeature newFolder( const QString &name );
 private:
 	void initDirectoryTree();
@@ -85,6 +89,7 @@ private:
     QString mLibAnimationDirectoryPath;
 	QString mLibApplicationsDirectoryPath;
 	QString mLibShapesDirectoryPath;
+	QString trashDirectoryPath;
 
 	QString rootPath;
 	QString audiosPath;
@@ -94,9 +99,11 @@ private:
 	QString flashPath;
 	QString shapesPath;
 	QString interactPath;
+	QString trashPath;
 
 	int mLastItemOffsetIndex;
 	UBFeature currentElement;
+	UBFeature trashElement;
 };
 
 
diff --git a/src/gui/UBFeaturesActionBar.cpp b/src/gui/UBFeaturesActionBar.cpp
index b1bc9cfe..ead9b859 100644
--- a/src/gui/UBFeaturesActionBar.cpp
+++ b/src/gui/UBFeaturesActionBar.cpp
@@ -87,6 +87,8 @@ UBFeaturesActionBar::UBFeaturesActionBar( UBFeaturesController *controller, QWid
     mLayout->addWidget(mpCloseBtn);
     mLayout->addWidget(mpRemoveFavoriteBtn);
 	setCurrentState( IN_ROOT );
+	mpDeleteBtn->setAcceptDrops(true);
+	setAcceptDrops( true );
 }
 
 void UBFeaturesActionBar::setCurrentState( UBFeaturesActionBarState state )
@@ -100,7 +102,16 @@ void UBFeaturesActionBar::setButtons()
     switch( currentState )
     {
 	case IN_FOLDER:
+		mpFavoriteBtn->show();
+        mpSocialBtn->hide();
+        mSearchBar->show();
+        mpDeleteBtn->show();
+        mpCloseBtn->hide();
+        mpRemoveFavoriteBtn->hide();
+        mpNewFolderBtn->show();
 		mpNewFolderBtn->setEnabled(true);
+		mpDeleteBtn->setEnabled(true);
+		break;
     case IN_ROOT:
         mpFavoriteBtn->show();
         mpSocialBtn->hide();
@@ -109,6 +120,8 @@ void UBFeaturesActionBar::setButtons()
         mpCloseBtn->hide();
         mpRemoveFavoriteBtn->hide();
         mpNewFolderBtn->show();
+		mpNewFolderBtn->setEnabled(false);
+		mpDeleteBtn->setEnabled(false);
         break;
     case IN_PROPERTIES:
         mpFavoriteBtn->show();
@@ -145,6 +158,31 @@ void UBFeaturesActionBar::onActionNewFolder()
     emit newFolderToCreate();
 }
 
+/*
+void UBFeaturesActionBar::dragMoveEvent(QDragMoveEvent *event)
+{
+    event->acceptProposedAction();
+}
+*/
+
+void UBFeaturesActionBar::dragEnterEvent( QDragEnterEvent *event )
+{
+    if (event->mimeData()->hasFormat("text/uri-list"))
+        event->acceptProposedAction();
+}
+
+void UBFeaturesActionBar::dropEvent( QDropEvent *event )
+{	
+	QWidget *dest = childAt( event->pos() );
+	if ( dest == mpDeleteBtn )
+	{
+		event->setDropAction( Qt::MoveAction );
+		event->accept();
+		emit deleteElements( *event->mimeData() );
+	}
+
+}
+
 UBFeaturesActionBar::~UBFeaturesActionBar()
 {
 }
\ No newline at end of file
diff --git a/src/gui/UBFeaturesActionBar.h b/src/gui/UBFeaturesActionBar.h
index 0eae4fe8..c41ec13d 100644
--- a/src/gui/UBFeaturesActionBar.h
+++ b/src/gui/UBFeaturesActionBar.h
@@ -3,6 +3,7 @@
 
 #include <QWidget>
 #include <QToolButton>
+#include <QDropEvent>
 #include "UBLibActionBar.h"
 #include "board/UBFeaturesController.h"
 
@@ -23,11 +24,16 @@ public:
 	
 	void setCurrentState( UBFeaturesActionBarState state );
 signals:
-	void searchElement(QString text);
+	void searchElement(const QString &text);
 	void newFolderToCreate();
+	void deleteElements( const QMimeData &data );
 private slots:
 	void onSearchTextChanged(QString txt);
 	void onActionNewFolder();
+protected:
+	//void dragMoveEvent(QDragMoveEvent *event);
+	void dragEnterEvent( QDragEnterEvent *event );
+	void dropEvent( QDropEvent *event );
 private:
 	void setButtons();
 	UBFeaturesController *featuresController;
diff --git a/src/gui/UBFeaturesWidget.cpp b/src/gui/UBFeaturesWidget.cpp
index 0e6f36b8..956e0130 100644
--- a/src/gui/UBFeaturesWidget.cpp
+++ b/src/gui/UBFeaturesWidget.cpp
@@ -48,7 +48,7 @@ UBFeaturesWidget::UBFeaturesWidget(QWidget *parent, const char *name):UBDockPale
 	
 
 	//featuresListView->setStyleSheet( QString("background: #EEEEEE;border-radius: 10px;border: 2px solid #999999;") );
-	featuresListView->setDragDropMode( QAbstractItemView::InternalMove );
+	featuresListView->setDragDropMode( QAbstractItemView::DragDrop );
 	featuresListView->setModel( featuresProxyModel );
 
 	featuresListView->setResizeMode( QListView::Adjust );
@@ -95,13 +95,14 @@ UBFeaturesWidget::UBFeaturesWidget(QWidget *parent, const char *name):UBDockPale
 		this, SLOT(currentSelected(const QModelIndex &)));*/
 	connect( featuresListView, SIGNAL(clicked ( const QModelIndex & ) ),
 		this, SLOT( currentSelected(const QModelIndex &) ) );
-	connect( mActionBar, SIGNAL( searchElement(QString) ), this, SLOT( searchStarted(QString) ) );
+	connect( mActionBar, SIGNAL( searchElement(const QString &) ), this, SLOT( const searchStarted(QString &) ) );
 	connect( mActionBar, SIGNAL( newFolderToCreate() ), this, SLOT( createNewFolder()  ) );
+	connect( mActionBar, SIGNAL( deleteElements(const QMimeData &)), this, SLOT( deleteElements(const QMimeData &) ) ); 
 	connect( pathListView, SIGNAL(clicked( const QModelIndex & ) ),
 		this, SLOT( currentPathChanged( const QModelIndex & ) ) );
 }
 
-void UBFeaturesWidget::searchStarted( QString pattern )
+void UBFeaturesWidget::searchStarted( const QString &pattern )
 {
 	if ( pattern.isEmpty() )
 	{
@@ -125,7 +126,8 @@ void UBFeaturesWidget::currentSelected(const QModelIndex &current)
 		QString path = model->data(current, Qt::UserRole).toString();
 		eUBLibElementType type = (eUBLibElementType)model->data(current, Qt::UserRole + 1).toInt();*/
 		UBFeature feature = model->data(current, Qt::UserRole + 1).value<UBFeature>();
-        if ( feature.getType() == FEATURE_FOLDER || feature.getType() == FEATURE_CATEGORY)
+        if ( feature.getType() == FEATURE_FOLDER || feature.getType() == FEATURE_CATEGORY ||
+			feature.getType() == FEATURE_TRASH )
 		{
 			QString newPath = feature.getUrl() + "/" + feature.getName();
 			//pathViewer->addPathElement( feature.getThumbnail(), newPath );
@@ -187,6 +189,28 @@ void UBFeaturesWidget::createNewFolder()
 	
 }
 
+void UBFeaturesWidget::deleteElements( const QMimeData & mimeData )
+{
+	if ( !mimeData.hasUrls() )
+		return;
+	QList<QUrl> urls = mimeData.urls();
+	
+	foreach ( QUrl url, urls )
+	{
+		if ( controller->isTrash( url) )
+		{
+			UBFeaturesController::deleteItem( url );
+		}
+		else
+		{
+			UBFeature elem = UBFeaturesController::moveItemToFolder( url, controller->getTrashElement() );
+			featuresModel->addItem( elem );
+		}
+	}
+	QSortFilterProxyModel *model = dynamic_cast<QSortFilterProxyModel *>( featuresListView->model() );
+	model->invalidate();
+}
+
 void UBFeaturesWidget::switchToListView()
 {
 	stackedWidget->setCurrentIndex(ID_LISTVIEW);
@@ -223,6 +247,12 @@ UBFeaturesListView::UBFeaturesListView( QWidget* parent, const char* name ) : QL
 	setObjectName(name);
 }
 
+void UBFeaturesListView::dragEnterEvent( QDragEnterEvent *event )
+{
+	if ( event->mimeData()->hasUrls() )
+		event->acceptProposedAction();
+}
+
 void UBFeaturesListView::dropEvent( QDropEvent *event )
 {
 	if( event->source() || dynamic_cast<UBFeaturesListView *>( event->source() ) )
@@ -356,73 +386,7 @@ void UBFeatureProperties::onAddToPage()
 UBFeatureProperties::~UBFeatureProperties()
 {
 }
-/*
-UBFeaturesPathViewer::UBFeaturesPathViewer(const QPixmap &root, const QString &rootPath, QGraphicsScene *sc, QWidget* parent, const char* name) : QGraphicsView(sc, parent)
-{
-	setObjectName(name);
-
-	//setAttribute(Qt::WA_StyledBackground, true);
-    //setStyleSheet(UBApplication::globalStyleSheet());
-
-	layout = new QGraphicsLinearLayout();
-
-	container = new QGraphicsWidget();
-	container->setMaximumWidth( width() - 20 );
-	container->setLayout( layout );
-	scene()->addItem( container );
-	
-	UBFolderWidget* pIconLabel = new UBFolderWidget();
-    pIconLabel->setStyleSheet(QString("background-color: transparent;"));
-	pIconLabel->setPixmap( root );
-	pIconLabel->setPath(rootPath);
-	connect( pIconLabel, SIGNAL( clicked(const QString &) ), parent, SLOT( currentPathChanged(const QString &) ) );
-
-	QGraphicsProxyWidget *iconWidget = scene()->addWidget( pIconLabel ) ;;
-	layout->addItem( iconWidget );
-	setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
-    setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
-	setAlignment( Qt::AlignLeft );
-	setFixedHeight( 70 );
-	arrowPixmap = new QPixmap(":images/navig_arrow.png");
-}
-
-void UBFeaturesPathViewer::addPathElement(const QPixmap &p, const QString &s)
-{
-	UBFolderWidget* pIconLabel = new UBFolderWidget();
-	pIconLabel->setStyleSheet(QString("background-color: transparent;"));
-	pIconLabel->setPixmap( *arrowPixmap );
-
-	QGraphicsProxyWidget *iconWidget = scene()->addWidget( pIconLabel );
-	layout->addItem( iconWidget );
-
-	
-	pIconLabel = new UBFolderWidget();
-	
-	pIconLabel->setStyleSheet(QString("background-color: transparent;"));
-	pIconLabel->setPixmap( p.scaledToHeight( height() - 30, Qt::SmoothTransformation) );
-	pIconLabel->setPath(s);
-	connect( pIconLabel, SIGNAL( clicked(const QString &) ), parent(), SLOT( currentPathChanged(const QString &) ) );
-
-	iconWidget = scene()->addWidget( pIconLabel );
-	layout->addItem( iconWidget );
-	scene()->invalidate();
-}
 
-void UBFeaturesPathViewer::truncatePath(int number)
-{
-	QList <QGraphicsItem*> items = scene()->items();
-	int itemsToDel = items.size() - number * 2;
-	for ( QList <QGraphicsItem*>::iterator it = items.begin() ; it != items.begin() + itemsToDel; ++it )
-	{
-		scene()->removeItem( (*it) );
-		QGraphicsLayoutItem *layoutItem = dynamic_cast<QGraphicsLayoutItem *>(*it);
-		Q_ASSERT(layout);
-		layout->removeItem(layoutItem);
-		delete layoutItem;
-	}
-	scene()->invalidate();
-}
-*/
 UBFeatureItemButton::UBFeatureItemButton(QWidget *parent, const char *name):QPushButton(parent)
 {
     setObjectName(name);
@@ -540,7 +504,7 @@ bool UBFeaturesModel::removeRows( int row, int count, const QModelIndex & parent
 {
 	if ( row < 0 )
 		return false;
-	if ( row + count >= featuresList->size() )
+	if ( row + count > featuresList->size() )
 		return false;
 	beginRemoveRows( parent, row, row + count - 1 );
 	featuresList->remove( row, count );
@@ -571,6 +535,7 @@ Qt::ItemFlags UBFeaturesModel::flags( const QModelIndex &index ) const
 			item.getType() == FEATURE_INTERNAL )
 			return Qt::ItemIsDragEnabled | defaultFlags;
         if ( item.getType() == FEATURE_FOLDER ||
+			item.getType() == FEATURE_TRASH ||
 			(item.getType() == FEATURE_CATEGORY && !item.getFullPath().isNull()))
 			return defaultFlags | Qt::ItemIsDropEnabled;
 		else return defaultFlags;
diff --git a/src/gui/UBFeaturesWidget.h b/src/gui/UBFeaturesWidget.h
index 4902d111..8f7b4337 100644
--- a/src/gui/UBFeaturesWidget.h
+++ b/src/gui/UBFeaturesWidget.h
@@ -75,12 +75,14 @@ private:
 	QStackedWidget *stackedWidget;
 
 	int currentStackedWidget;
+	QModelIndex trashIndex;
 private slots:
 	void currentSelected( const QModelIndex & );
 	//void currentPathChanged(const QString &);
 	void currentPathChanged( const QModelIndex & );
-	void searchStarted( QString );
+	void searchStarted( const QString & );
 	void createNewFolder();
+	void deleteElements( const QMimeData & );
 };
 
 class UBFeaturesListView : public QListView
@@ -90,6 +92,7 @@ public:
 	UBFeaturesListView( QWidget* parent=0, const char* name="UBFeaturesListView" );
     virtual ~UBFeaturesListView() {;}
 protected:
+	virtual void dragEnterEvent( QDragEnterEvent *event );
 	virtual void dropEvent( QDropEvent *event );
 };