From 5f75d278d49c51a0a71c44e2dab1cf31ed338469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Thu, 20 Nov 2014 23:15:05 +0100 Subject: [PATCH] fixed StyledDelegates and added proxymodel to lists model --- YACReaderLibrary/db/folder_model.cpp | 3 ++ YACReaderLibrary/db/folder_model.h | 5 ++++ YACReaderLibrary/db/reading_list_model.cpp | 22 ++++++++++++++ YACReaderLibrary/db/reading_list_model.h | 19 ++++++++++++ YACReaderLibrary/library_window.cpp | 4 ++- YACReaderLibrary/library_window.h | 6 ++-- YACReaderLibrary/yacreader_folders_view.cpp | 6 ++-- .../yacreader_navigation_controller.cpp | 30 ++++++++++++++++++- .../yacreader_navigation_controller.h | 1 + .../yacreader_reading_lists_view.cpp | 11 ++++--- custom_widgets/yacreader_sidebar.cpp | 12 -------- custom_widgets/yacreader_sidebar.h | 3 -- 12 files changed, 92 insertions(+), 30 deletions(-) diff --git a/YACReaderLibrary/db/folder_model.cpp b/YACReaderLibrary/db/folder_model.cpp index 0883acda..27d1054c 100644 --- a/YACReaderLibrary/db/folder_model.cpp +++ b/YACReaderLibrary/db/folder_model.cpp @@ -166,6 +166,9 @@ QVariant FolderModel::data(const QModelIndex &index, int role) const return QVariant(YACReader::noHighlightedIcon(":/images/folder.png")); #endif + if(role == FolderModel::CompletedRole) + return item->data(FolderModel::Completed); + if (role != Qt::DisplayRole) return QVariant(); diff --git a/YACReaderLibrary/db/folder_model.h b/YACReaderLibrary/db/folder_model.h index 95b2de0a..4d0f16bb 100644 --- a/YACReaderLibrary/db/folder_model.h +++ b/YACReaderLibrary/db/folder_model.h @@ -125,6 +125,11 @@ public: Completed = 3 };//id INTEGER PRIMARY KEY, parentId INTEGER NOT NULL, name TEXT NOT NULL, path TEXT NOT NULL + enum Roles { + FinishedRole = Qt::UserRole + 1, + CompletedRole + }; + public slots: void deleteFolder(const QModelIndex & mi); diff --git a/YACReaderLibrary/db/reading_list_model.cpp b/YACReaderLibrary/db/reading_list_model.cpp index b2781e20..6057a106 100644 --- a/YACReaderLibrary/db/reading_list_model.cpp +++ b/YACReaderLibrary/db/reading_list_model.cpp @@ -58,6 +58,21 @@ QVariant ReadingListModel::data(const QModelIndex &index, int role) const ListItem * item = static_cast(index.internalPointer()); + if (role == ReadingListModel::TypeListsRole) + { + if(typeid(*item) == typeid(SpecialListItem)) + return QVariant(ReadingListModel::SpecialList); + + if(typeid(*item) == typeid(LabelItem)) + return QVariant(ReadingListModel::Label); + + if(typeid(*item) == typeid(ReadingListItem)) + return QVariant(ReadingListModel::ReadingList); + + if(typeid(*item) == typeid(ReadingListSeparatorItem)) + return QVariant(ReadingListModel::Separator); + } + if(typeid(*item) == typeid(ReadingListSeparatorItem)) return QVariant(); @@ -446,3 +461,10 @@ int ReadingListModel::addLabelIntoList(LabelItem *item) } + + +ReadingListModelProxy::ReadingListModelProxy(QObject *parent) + :QSortFilterProxyModel(parent) +{ + +} diff --git a/YACReaderLibrary/db/reading_list_model.h b/YACReaderLibrary/db/reading_list_model.h index c9ef1448..ee50bef7 100644 --- a/YACReaderLibrary/db/reading_list_model.h +++ b/YACReaderLibrary/db/reading_list_model.h @@ -2,6 +2,7 @@ #define READING_LIST_MODEL_H #include +#include #include #include #include @@ -14,6 +15,13 @@ class SpecialListItem; class ReadingListItem; class ReadingListSeparatorItem; +class ReadingListModelProxy : public QSortFilterProxyModel +{ + Q_OBJECT +public: + explicit ReadingListModelProxy(QObject *parent = 0); +}; + class ReadingListModel : public QAbstractItemModel { Q_OBJECT @@ -42,6 +50,17 @@ public: void rename(const QModelIndex & mi, const QString & name); void deleteItem(const QModelIndex & mi); + enum Roles { + TypeListsRole = Qt::UserRole + 1, + }; + + enum TypeList { + SpecialList, + Label, + ReadingList, + Separator + }; + signals: private: diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index b961a4a9..8d684dfe 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -418,6 +418,7 @@ void LibraryWindow::doModels() comicsModel = new ComicModel(); //lists listsModel = new ReadingListModel(); + listsModelProxy = new ReadingListModelProxy(); //setSearchFilter(YACReader::NoModifiers, ""); //clear search filter } @@ -1223,7 +1224,8 @@ void LibraryWindow::loadLibrary(const QString & name) foldersView->setModel(foldersModelProxy); listsModel->setupReadingListsData(path); - listsView->setModel(listsModel); + listsModelProxy->setSourceModel(listsModel); + listsView->setModel(listsModelProxy); if(foldersModel->rowCount(QModelIndex())>0) disableFoldersActions(false); diff --git a/YACReaderLibrary/library_window.h b/YACReaderLibrary/library_window.h index 32481f62..7d81bb4f 100644 --- a/YACReaderLibrary/library_window.h +++ b/YACReaderLibrary/library_window.h @@ -65,6 +65,7 @@ class EditShortcutsDialog; class ComicFilesManager; class QProgressDialog; class ReadingListModel; +class ReadingListModelProxy; class YACReaderReadingListsView; class YACReaderHistoryController; @@ -96,9 +97,7 @@ private: bool fullscreen; bool importedCovers; //if true, the library is read only (not updates,open comic or properties) bool fromMaximized; - //Ya no se usan proxies, el rendimiento de la BD es suficiente - //YACReaderTreeSearch * proxyFilter; - //YACReaderSortComics * proxySort; + PackageManager * packageManager; QSize slideSizeW; @@ -131,6 +130,7 @@ private: FolderModelProxy * foldersModelProxy; ComicModel * comicsModel; ReadingListModel * listsModel; + ReadingListModelProxy * listsModelProxy; //QStringList paths; YACReaderLibraries libraries; diff --git a/YACReaderLibrary/yacreader_folders_view.cpp b/YACReaderLibrary/yacreader_folders_view.cpp index ddc5b45b..54ae0aa1 100644 --- a/YACReaderLibrary/yacreader_folders_view.cpp +++ b/YACReaderLibrary/yacreader_folders_view.cpp @@ -87,10 +87,8 @@ YACReaderFoldersViewItemDeletegate::YACReaderFoldersViewItemDeletegate(QObject * void YACReaderFoldersViewItemDeletegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { -/* - FolderItem * item = static_cast(index.internalPointer()); - if(!item->data(FolderModel::Completed).toBool()) + if(index.data(FolderModel::CompletedRole).toBool()) { painter->save(); #ifdef Q_OS_MAC @@ -102,6 +100,6 @@ void YACReaderFoldersViewItemDeletegate::paint(QPainter *painter, const QStyleOp painter->drawRect(0,option.rect.y(),2,option.rect.height()); painter->restore(); } -*/ + QStyledItemDelegate::paint(painter, option, index); } diff --git a/YACReaderLibrary/yacreader_navigation_controller.cpp b/YACReaderLibrary/yacreader_navigation_controller.cpp index f7cd7a62..e4546a0c 100644 --- a/YACReaderLibrary/yacreader_navigation_controller.cpp +++ b/YACReaderLibrary/yacreader_navigation_controller.cpp @@ -9,10 +9,13 @@ #include "yacreader_history_controller.h" #include "comic_model.h" #include "folder_model.h" +#include "reading_list_model.h" #include "comics_view.h" #include "empty_folder_widget.h" #include "yacreader_search_line_edit.h" +#include "QsLog.h" + YACReaderNavigationController::YACReaderNavigationController(LibraryWindow *parent) : QObject(parent),libraryWindow(parent) { @@ -38,6 +41,8 @@ void YACReaderNavigationController::selectedFolder(const QModelIndex &mi) loadFolderInfo(modelIndex); + //if a folder is selected, listsView selection must be cleared + libraryWindow->listsView->clearSelection(); } void YACReaderNavigationController::reselectCurrentFolder() @@ -69,11 +74,34 @@ void YACReaderNavigationController::loadFolderInfo(const QModelIndex &modelIndex } } -void YACReaderNavigationController::selectedList(const QModelIndex &mi) +void YACReaderNavigationController::loadListInfo(const QModelIndex &modelIndex) { } +void YACReaderNavigationController::selectedList(const QModelIndex &mi) +{/* + //A proxy is used + QModelIndex modelIndex = libraryWindow->listsModelProxy->mapToSource(mi); + + //update history + libraryWindow->historyController->updateHistory(modelIndex); + + if(libraryWindow->status == LibraryWindow::Searching) + { + //when a list is selected the search mode has to be reset + libraryWindow->searchEdit->clearText(); + libraryWindow->clearSearchFilter(); + libraryWindow->listsView->scrollTo(mi,QAbstractItemView::PositionAtTop); + libraryWindow->listsView->setCurrentIndex(mi); + } + + loadListInfo(modelIndex); + + //if a list is selected, foldersView selection must be cleared + libraryWindow->foldersView->clearSelection();*/ +} + void YACReaderNavigationController::selectedIndexFromHistory(const QModelIndex &sourceMI) { //TODO NO searching allowed, just disable backward/forward actions in searching mode diff --git a/YACReaderLibrary/yacreader_navigation_controller.h b/YACReaderLibrary/yacreader_navigation_controller.h index c4ca9b3f..5375adce 100644 --- a/YACReaderLibrary/yacreader_navigation_controller.h +++ b/YACReaderLibrary/yacreader_navigation_controller.h @@ -29,6 +29,7 @@ public slots: void loadEmptyFolderInfo(const QModelIndex & modelIndex); void loadFolderInfo(const QModelIndex & modelIndex); + void loadListInfo(const QModelIndex & modelIndex); void loadPreviousStatus(); private: diff --git a/YACReaderLibrary/yacreader_reading_lists_view.cpp b/YACReaderLibrary/yacreader_reading_lists_view.cpp index 747b4d90..c8bfa0df 100644 --- a/YACReaderLibrary/yacreader_reading_lists_view.cpp +++ b/YACReaderLibrary/yacreader_reading_lists_view.cpp @@ -1,6 +1,7 @@ #include "yacreader_reading_lists_view.h" #include "reading_list_item.h" +#include "reading_list_model.h" YACReaderReadingListsView::YACReaderReadingListsView(QWidget *parent) :YACReaderTreeView(parent) @@ -20,9 +21,9 @@ YACReaderReadingListsViewItemDeletegate::YACReaderReadingListsViewItemDeletegate void YACReaderReadingListsViewItemDeletegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { - ListItem * item = static_cast(index.internalPointer()); + ReadingListModel::TypeList typeList = (ReadingListModel::TypeList)index.data(ReadingListModel::TypeListsRole).toInt(); - if(typeid(*item) == typeid(ReadingListSeparatorItem)) + if(typeList == ReadingListModel::Separator) { return; } @@ -45,14 +46,12 @@ void YACReaderReadingListsViewItemDeletegate::paint(QPainter *painter, const QSt QSize YACReaderReadingListsViewItemDeletegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { - ListItem * item = static_cast(index.internalPointer()); + ReadingListModel::TypeList typeList = (ReadingListModel::TypeList)index.data(ReadingListModel::TypeListsRole).toInt(); - if(typeid(*item) == typeid(ReadingListSeparatorItem)) + if(typeList == ReadingListModel::Separator) { QSize newSize = QStyledItemDelegate::sizeHint(option, index); - newSize.setHeight(7); - return newSize; } diff --git a/custom_widgets/yacreader_sidebar.cpp b/custom_widgets/yacreader_sidebar.cpp index 1216f2f3..d48694f9 100644 --- a/custom_widgets/yacreader_sidebar.cpp +++ b/custom_widgets/yacreader_sidebar.cpp @@ -20,9 +20,6 @@ YACReaderSideBar::YACReaderSideBar(QWidget *parent) : readingListsView = new YACReaderReadingListsView; selectedLibrary = new YACReaderLibraryListWidget; - connect(foldersView,SIGNAL(clicked(QModelIndex)),this,SLOT(selectedIndex(QModelIndex))); - connect(readingListsView,SIGNAL(clicked(QModelIndex)),this,SLOT(selectedIndex(QModelIndex))); - librariesTitle = new YACReaderTitledToolBar(tr("LIBRARIES")); foldersTitle = new YACReaderTitledToolBar(tr("FOLDERS")); readingListsTitle = new YACReaderTitledToolBar(tr("READING LISTS")); @@ -176,15 +173,6 @@ QSize YACReaderSideBar::sizeHint() const return QSize(275,200); } -void YACReaderSideBar::selectedIndex(const QModelIndex &mi) -{ - if(sender() == foldersView) - readingListsView->clearSelection(); - else if(sender() == readingListsView) - foldersView->clearSelection(); -} - - YACReaderSideBarSeparator::YACReaderSideBarSeparator(QWidget *parent) :QWidget(parent) { diff --git a/custom_widgets/yacreader_sidebar.h b/custom_widgets/yacreader_sidebar.h index 8fb1efbe..1a7f59cc 100644 --- a/custom_widgets/yacreader_sidebar.h +++ b/custom_widgets/yacreader_sidebar.h @@ -38,9 +38,6 @@ signals: public slots: -protected slots: - void selectedIndex(const QModelIndex & mi); - protected: void paintEvent(QPaintEvent *);