From a8ef8d07f11ed4c34c22f3ef254c77e431b9c6ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Wed, 22 Oct 2014 12:57:32 +0200 Subject: [PATCH] added support for deleting folders --- YACReaderLibrary/comics_remover.cpp | 40 +++++++- YACReaderLibrary/comics_remover.h | 25 ++++- YACReaderLibrary/db/treeitem.cpp | 5 + YACReaderLibrary/db/treeitem.h | 1 + YACReaderLibrary/db/treemodel.cpp | 19 ++++ YACReaderLibrary/db/treemodel.h | 3 + YACReaderLibrary/images_win.qrc | 2 + YACReaderLibrary/library_window.cpp | 102 ++++++++++++++++++-- YACReaderLibrary/library_window.h | 6 ++ custom_widgets/yacreader_titled_toolbar.cpp | 17 +++- custom_widgets/yacreader_titled_toolbar.h | 1 + images/addNew_sidebar.png | Bin 0 -> 212 bytes images/delete_sidebar.png | Bin 0 -> 396 bytes shortcuts_management/shortcuts_manager.h | 2 + 14 files changed, 208 insertions(+), 15 deletions(-) create mode 100644 images/addNew_sidebar.png create mode 100644 images/delete_sidebar.png diff --git a/YACReaderLibrary/comics_remover.cpp b/YACReaderLibrary/comics_remover.cpp index d5d5676d..ef3fd009 100644 --- a/YACReaderLibrary/comics_remover.cpp +++ b/YACReaderLibrary/comics_remover.cpp @@ -1,13 +1,16 @@ #include "comics_remover.h" #include +#include -ComicsRemover::ComicsRemover(QModelIndexList & il, QList & ps, QObject *parent) : - QThread(parent),indexList(il), paths(ps) +#include "QsLog.h" + +ComicsRemover::ComicsRemover(QModelIndexList & il, QList & ps, QObject *parent) + :QObject(parent),indexList(il), paths(ps) { } -void ComicsRemover::run() +void ComicsRemover::process() { QString currentComicPath; QListIterator i(indexList); @@ -27,3 +30,34 @@ void ComicsRemover::run() emit finished(); } + + +FoldersRemover::FoldersRemover(QModelIndexList &il, QList &ps, QObject *parent) + :QObject(parent),indexList(il), paths(ps) +{ + +} + +void FoldersRemover::process() +{ + QString currentFolderPath; + QListIterator i(indexList); + QListIterator i2(paths); + i.toBack(); + i2.toBack(); + + QLOG_DEBUG() << "Deleting folders" << paths.at(0); + + while (i.hasPrevious() && i2.hasPrevious()) + { + QModelIndex mi = i.previous(); + currentFolderPath = i2.previous(); + QDir d(currentFolderPath); + if(d.removeRecursively() || !d.exists()) //the folder is in the DB but no in the drive... + emit remove(mi); + else + emit removeError(); + } + + emit finished(); +} diff --git a/YACReaderLibrary/comics_remover.h b/YACReaderLibrary/comics_remover.h index 5a77a84b..ff9d0a21 100644 --- a/YACReaderLibrary/comics_remover.h +++ b/YACReaderLibrary/comics_remover.h @@ -6,7 +6,7 @@ #include #include -class ComicsRemover : public QThread +class ComicsRemover : public QObject { Q_OBJECT public: @@ -17,12 +17,31 @@ signals: void removeError(); void finished(); -private: - void run(); +public slots: + void process(); private: QModelIndexList indexList; QList paths; }; +class FoldersRemover : public QObject +{ + Q_OBJECT +public: + explicit FoldersRemover(QModelIndexList & indexList, QList & paths, QObject *parent = 0); + +signals: + void remove(QModelIndex); + void removeError(); + void finished(); + +public slots: + void process(); + +private: + QModelIndexList indexList; + QList paths; +}; + #endif // COMICS_REMOVER_H diff --git a/YACReaderLibrary/db/treeitem.cpp b/YACReaderLibrary/db/treeitem.cpp index ec31049e..2cbeec9f 100644 --- a/YACReaderLibrary/db/treeitem.cpp +++ b/YACReaderLibrary/db/treeitem.cpp @@ -68,6 +68,11 @@ void TreeItem::setData(int column, const QVariant & value) itemData[column] = value; } +void TreeItem::removeChild(int childIndex) +{ + childItems.removeAt(childIndex); +} + void TreeItem::clearChildren() { qDeleteAll(childItems); diff --git a/YACReaderLibrary/db/treeitem.h b/YACReaderLibrary/db/treeitem.h index 833f4a94..cb21dbcb 100644 --- a/YACReaderLibrary/db/treeitem.h +++ b/YACReaderLibrary/db/treeitem.h @@ -65,6 +65,7 @@ public: QList comicNames; TreeItem * originalItem; void setData(int column, const QVariant &value); + void removeChild(int childIndex); void clearChildren(); QList children(); private: diff --git a/YACReaderLibrary/db/treemodel.cpp b/YACReaderLibrary/db/treemodel.cpp index 02a0e383..01aeb7a9 100644 --- a/YACReaderLibrary/db/treemodel.cpp +++ b/YACReaderLibrary/db/treemodel.cpp @@ -621,3 +621,22 @@ void TreeModel::fetchMoreFromDB(const QModelIndex &parent) db.close(); QSqlDatabase::removeDatabase(_databasePath); } + +void TreeModel::deleteFolder(const QModelIndex &mi) +{ + beginRemoveRows(mi.parent(),mi.row(),mi.row()); + + TreeItem * item = static_cast(mi.internalPointer()); + + TreeItem * parent = item->parent(); + parent->removeChild(mi.row()); + + Folder f; + f.setId(item->id); + + QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath); + DBHelper::removeFromDB(&f,db); + QSqlDatabase::removeDatabase(_databasePath); + + endRemoveRows(); +} diff --git a/YACReaderLibrary/db/treemodel.h b/YACReaderLibrary/db/treemodel.h index 8a5e2f4b..c164a536 100644 --- a/YACReaderLibrary/db/treemodel.h +++ b/YACReaderLibrary/db/treemodel.h @@ -96,6 +96,9 @@ public: Completed = 3 };//id INTEGER PRIMARY KEY, parentId INTEGER NOT NULL, name TEXT NOT NULL, path TEXT NOT NULL +public slots: + void deleteFolder(const QModelIndex & mi); + private: void setupModelData( QSqlQuery &sqlquery, TreeItem *parent); void updateFolderModelData( QSqlQuery &sqlquery, TreeItem *parent); diff --git a/YACReaderLibrary/images_win.qrc b/YACReaderLibrary/images_win.qrc index b41ecd8d..99e83640 100644 --- a/YACReaderLibrary/images_win.qrc +++ b/YACReaderLibrary/images_win.qrc @@ -20,6 +20,8 @@ ../images/grid_to_flow.gif ../images/empty_folder.png ../images/empty_search.png + ../images/addNew_sidebar.png + ../images/delete_sidebar.png ../images/iconSearchNew.png ../images/clearSearchNew.png diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index 60094585..4d416638 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -189,6 +189,9 @@ void LibraryWindow::doLayout() librariesTitle->addAction(openLibraryAction); librariesTitle->addSpacing(3); + foldersTitle->addAction(addFolderAction); + foldersTitle->addAction(deleteFolderAction); + foldersTitle->addSepartor(); foldersTitle->addAction(setRootIndexAction); foldersTitle->addAction(expandAllNodesAction); foldersTitle->addAction(colapseAllNodesAction); @@ -319,6 +322,8 @@ void LibraryWindow::setUpShortcutsManagement() editShortcutsDialog->addActionsGroup("Folders",QIcon(":/images/shortcuts_group_folders.png"), tmpList = QList() + << addFolderAction + << deleteFolderAction << setRootIndexAction << expandAllNodesAction << colapseAllNodesAction @@ -524,6 +529,18 @@ void LibraryWindow::createActions() icoHelpButton.addPixmap(QPixmap(":/images/main_toolbar/help.png"), QIcon::Normal); helpAboutAction->setIcon(icoHelpButton); + addFolderAction = new QAction(tr("Add new folder"), this); + addFolderAction->setData(ADD_FOLDER_ACTION_YL); + addFolderAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(ADD_FOLDER_ACTION_YL)); + addFolderAction->setToolTip(tr("Add new folder to the current library")); + addFolderAction->setIcon(QIcon(":/images/addNew_sidebar.png")); + + deleteFolderAction = new QAction(tr("Delete folder"), this); + deleteFolderAction->setData(REMOVE_FOLDER_ACTION_YL); + deleteFolderAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(REMOVE_FOLDER_ACTION_YL)); + deleteFolderAction->setToolTip(tr("Delete current folder from disk")); + deleteFolderAction->setIcon(QIcon(":/images/delete_sidebar.png")); + setRootIndexAction = new QAction(this); setRootIndexAction->setData(SET_ROOT_INDEX_ACTION_YL); setRootIndexAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(SET_ROOT_INDEX_ACTION_YL)); @@ -858,6 +875,10 @@ void LibraryWindow::createMenus() comicsView->setItemActions(itemActions); comicsView->setViewActions(viewActions); + foldersView->addAction(addFolderAction); + foldersView->addAction(deleteFolderAction); + YACReader::addSperator(foldersView); + foldersView->addAction(openContainingFolderAction); foldersView->addAction(updateFolderAction); YACReader::addSperator(foldersView); @@ -1016,6 +1037,8 @@ void LibraryWindow::createConnections() connect(removeLibraryAction,SIGNAL(triggered()),this,SLOT(removeLibrary())); connect(openComicAction,SIGNAL(triggered()),this,SLOT(openComic())); connect(helpAboutAction,SIGNAL(triggered()),had,SLOT(show())); + connect(addFolderAction,SIGNAL(triggered()),this,SLOT(addFolderToCurrentIndex())); + connect(deleteFolderAction,SIGNAL(triggered()),this,SLOT(deleteSelectedFolder())); connect(setRootIndexAction,SIGNAL(triggered()),this,SLOT(setRootIndex())); connect(expandAllNodesAction,SIGNAL(triggered()),foldersView,SLOT(expandAll())); connect(colapseAllNodesAction,SIGNAL(triggered()),foldersView,SLOT(collapseAll())); @@ -1446,6 +1469,65 @@ void LibraryWindow::enableNeededActions() } +void LibraryWindow::addFolderToCurrentIndex() +{ + QModelIndex currentIndex = getCurrentFolderIndex(); + + bool ok; + QString text = QInputDialog::getText(this, tr("Add new folder"), + tr("Folder name:"), QLineEdit::Normal, + "", &ok); + if (ok && !text.isEmpty()) + QLOG_INFO() << text; + + +} + +void LibraryWindow::deleteSelectedFolder() +{ + QModelIndex currentIndex = getCurrentFolderIndex(); + + if(!currentIndex.isValid()) + QMessageBox::information(this,tr("No folder selected"), tr("Please, select a folder first")); + else + { + int ret = QMessageBox::question(this,tr("Delete folder"),tr("The selected folder and all its contents will be deleted from your disk. Are you sure?"),QMessageBox::Yes,QMessageBox::No); + + if(ret == QMessageBox::Yes) + { + //no folders multiselection by now + QModelIndexList indexList; + indexList << currentIndex; + + QList paths; + paths << QDir::cleanPath(currentPath()+foldersModel->getFolderPath(currentIndex)); + + FoldersRemover * remover = new FoldersRemover(indexList,paths); + + QThread * thread = NULL; + + thread = new QThread(this); + + remover->moveToThread(thread); + + connect(thread, SIGNAL(started()), remover, SLOT(process())); + connect(remover, SIGNAL(remove(QModelIndex)), foldersModel, SLOT(deleteFolder(QModelIndex))); + connect(remover, SIGNAL(removeError()),this,SLOT(errorDeletingFolder())); + connect(remover, SIGNAL(finished()),this,SLOT(reloadCovers())); + connect(remover, SIGNAL(finished()), remover, SLOT(deleteLater())); + connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); + + if(thread != NULL) + thread->start(); + } + } +} + +void LibraryWindow::errorDeletingFolder() +{ + QMessageBox::critical(this,tr("Unable to delete"),tr("There was an issue trying to delete the selected folders. Please, check for write permissions and be sure that any applications are using these folders or any of the contained files.")); +} + void LibraryWindow::selectSubfolder(const QModelIndex &mi, int child) { QModelIndex dest = foldersModel->index(child,0,mi); @@ -2236,20 +2318,26 @@ void LibraryWindow::deleteComics() } ComicsRemover * remover = new ComicsRemover(indexList,paths); + QThread * thread = NULL; + + thread = new QThread(this); + + remover->moveToThread(thread); - //comicsView->showDeleteProgress(); comicsModel->startTransaction(); + connect(thread, SIGNAL(started()), remover, SLOT(process())); connect(remover, SIGNAL(remove(int)), comicsModel, SLOT(remove(int))); - connect(remover,SIGNAL(removeError()),this,SLOT(setRemoveError())); + connect(remover, SIGNAL(removeError()),this,SLOT(setRemoveError())); connect(remover, SIGNAL(finished()), comicsModel, SLOT(finishTransaction())); - //connect(remover, SIGNAL(finished()), comicsView, SLOT(hideDeleteProgress())); - connect(remover, SIGNAL(finished()),this,SLOT(checkEmptyFolder())); - connect(remover, SIGNAL(finished()),this,SLOT(checkRemoveError())); + + connect(remover, SIGNAL(finished()),this,SLOT(checkEmptyFolder())); + connect(remover, SIGNAL(finished()),this,SLOT(checkRemoveError())); connect(remover, SIGNAL(finished()), remover, SLOT(deleteLater())); - //connect(remover, SIGNAL(finished()), thread, SLOT(deleteLater())); + connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); - remover->start(); + if(thread != NULL) + thread->start(); } } diff --git a/YACReaderLibrary/library_window.h b/YACReaderLibrary/library_window.h index b0fa49db..d61af10e 100644 --- a/YACReaderLibrary/library_window.h +++ b/YACReaderLibrary/library_window.h @@ -147,6 +147,9 @@ private: //QAction * socialAction; //tree actions + QAction * addFolderAction; + QAction * deleteFolderAction; + //-- QAction * setRootIndexAction; QAction * expandAllNodesAction; QAction * colapseAllNodesAction; @@ -339,6 +342,9 @@ public slots: void reloadAfterCopyMove(); QModelIndex getCurrentFolderIndex(); void enableNeededActions(); + void addFolderToCurrentIndex(); + void deleteSelectedFolder(); + void errorDeletingFolder(); }; #endif diff --git a/custom_widgets/yacreader_titled_toolbar.cpp b/custom_widgets/yacreader_titled_toolbar.cpp index 9015ff29..79b54d3d 100644 --- a/custom_widgets/yacreader_titled_toolbar.cpp +++ b/custom_widgets/yacreader_titled_toolbar.cpp @@ -65,7 +65,7 @@ YACReaderTitledToolBar::YACReaderTitledToolBar(const QString & title, QWidget *p mainLayout->setSpacing(0); QString styleSheet = "QWidget {border:0px;}"; - setStyleSheet(styleSheet); + setStyleSheet(styleSheet); nameLabel = new DropShadowLabel(this); nameLabel->setText(title); @@ -108,5 +108,18 @@ void YACReaderTitledToolBar::addSpacing(int spacing) { QHBoxLayout * mainLayout = dynamic_cast(layout()); - mainLayout->addSpacing(spacing); + mainLayout->addSpacing(spacing); +} + +void YACReaderTitledToolBar::addSepartor() +{ + QHBoxLayout * mainLayout = dynamic_cast(layout()); + + QWidget * w = new QWidget(this); + w->setFixedSize(1,14); + w->setStyleSheet("QWidget {background-color:#6F6F6F;}"); + + mainLayout->addSpacing(10); + mainLayout->addWidget(w); + mainLayout->addSpacing(10); } diff --git a/custom_widgets/yacreader_titled_toolbar.h b/custom_widgets/yacreader_titled_toolbar.h index 0b4a03c1..21b9b75c 100644 --- a/custom_widgets/yacreader_titled_toolbar.h +++ b/custom_widgets/yacreader_titled_toolbar.h @@ -38,6 +38,7 @@ signals: public slots: void addAction(QAction * action); void addSpacing(int space); + void addSepartor(); private: DropShadowLabel * nameLabel; }; diff --git a/images/addNew_sidebar.png b/images/addNew_sidebar.png new file mode 100644 index 0000000000000000000000000000000000000000..f23c06760b93fd1545c05a1ac0a3aed5b6cdb41f GIT binary patch literal 212 zcmeAS@N?(olHy`uVBq!ia0vp^JRmj)8<3o<+3y6TBuiW)N`mv#O3D+9QW+dm@{>{( zJaZG%Q-e|yQz{EjrrH1%m3g{2hE&{2`t$$4J+qZX!Gi-#0&gEYn9!hi{78fB5obf@ zjZy_nx3UxN?63dN6!Y#VlbF$5%i?2g5tkFQ9L@f?A9IL4C=n~OWE!UgKc~Z)j-Eud zMcvI;F4;|q*ueSj&d$kx3$tS<00Qa&rc59k60Pgg&e IbxsLQ0I3X1O#lD@ literal 0 HcmV?d00001 diff --git a/images/delete_sidebar.png b/images/delete_sidebar.png new file mode 100644 index 0000000000000000000000000000000000000000..761201009fdc3ebe614133b16bd11ebddfa28ca7 GIT binary patch literal 396 zcmV;70dxL|P)brKoEv^lXaqmM-x$y zP&uNYf`W$92;U*6NFTaf0g2Kr6=gaXK0}HKiOLNHq5&il0`@NdD8Hoz62VBHXFRj> z*s--*i3T-LCnApilGy5C1GGRB?1FZdWm5@8QFIJ72d`jBR8L}H0ERd$l`p}F_K-0- z)0Ym12;8fG2s)lf!T~q~N1y`&&hL018RHSm1cS#S48tHvlJOtaFXS$=8E35x=)9r6 z23NFinIqjw>7epFf2Z?>`T=~>UQkPMWw=^L=iE!I5|-X%y0&3t=N0)Tn-VnnCFVz^ zCUx5z$@rbCgp!UO>V41GqLz0jZMVT`nx+$D%+|UwilTUn