From 450156f2b8df628d060b2ac14303118ad0ea8b53 Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Wed, 19 Aug 2015 00:35:34 +0300 Subject: [PATCH] [app] Select the last used storage at startup --- app/qml/BooksMain.qml | 1 + app/qml/BooksStorageView.qml | 19 ++++++++++++++++--- app/src/BooksListWatcher.cpp | 2 +- app/src/BooksSettings.cpp | 18 ++++++++++++++++++ app/src/BooksSettings.h | 6 ++++++ app/src/BooksStorageModel.cpp | 17 +++++++++++++++++ app/src/BooksStorageModel.h | 2 ++ 7 files changed, 61 insertions(+), 4 deletions(-) diff --git a/app/qml/BooksMain.qml b/app/qml/BooksMain.qml index bc24b0d..d4f7064 100644 --- a/app/qml/BooksMain.qml +++ b/app/qml/BooksMain.qml @@ -41,6 +41,7 @@ ApplicationWindow { BooksSettings { id: globalSettings } BooksHints { id: globalHints } + SystemState { id: globalSystemState } initialPage: BooksMainPage { id: mainPage } diff --git a/app/qml/BooksStorageView.qml b/app/qml/BooksStorageView.qml index 8c40d47..508025c 100644 --- a/app/qml/BooksStorageView.qml +++ b/app/qml/BooksStorageView.qml @@ -115,9 +115,9 @@ SilicaFlickable { } } - SystemState { - id: globalSystemState - onLockModeChanged: if (lockMode == "locked") editMode = false + Connections { + target: globalSystemState + onLockModeChanged: if (target.lockMode === "locked") editMode = false } BookStorage { @@ -132,6 +132,11 @@ SilicaFlickable { id: storageListWatcher listView: storageList onSizeChanged: _cellWidth = calculateCellWidth() + onCurrentIndexChanged: { + if (storageList._completed && currentIndex >= 0) { + globalSettings.currentStorage = storageModel.deviceAt(currentIndex) + } + } } SilicaListView { @@ -144,6 +149,7 @@ SilicaFlickable { spacing: Theme.paddingMedium interactive: !dragInProgress && !dragScrollAnimation.running + property bool _completed readonly property real maxContentX: Math.max(0, contentWidth - width) onMaxContentXChanged: { @@ -154,6 +160,13 @@ SilicaFlickable { } } + Component.onCompleted: { + var index = model.deviceIndex(globalSettings.currentStorage) + // positionViewAtIndex doesn't work here, update contentX directly + if (index >= 0) contentX = (width + spacing) * index + _completed = true + } + delegate: BooksShelfView { width: storageList.width height: storageList.height diff --git a/app/src/BooksListWatcher.cpp b/app/src/BooksListWatcher.cpp index 90df81e..788a0c2 100644 --- a/app/src/BooksListWatcher.cpp +++ b/app/src/BooksListWatcher.cpp @@ -41,7 +41,7 @@ BooksListWatcher::BooksListWatcher(QObject* aParent) : QObject(aParent), - iCurrentIndex(0), + iCurrentIndex(-1), iContentX(0), iContentY(0), iListView(NULL), diff --git a/app/src/BooksSettings.cpp b/app/src/BooksSettings.cpp index e4944a1..865d7a0 100644 --- a/app/src/BooksSettings.cpp +++ b/app/src/BooksSettings.cpp @@ -45,10 +45,12 @@ #define KEY_FONT_SIZE "fontSize" #define KEY_PAGE_DETAILS "pageDetails" #define KEY_CURRENT_BOOK "currentBook" +#define KEY_CURRENT_STORAGE "currentStorage" #define KEY_INVERT_COLORS "invertColors" #define DEFAULT_FONT_SIZE 0 #define DEFAULT_PAGE_DETAILS 0 #define DEFAULT_CURRENT_BOOK QString() +#define DEFAULT_CURRENT_STORAGE QString() #define DEFAULT_INVERT_COLORS false // ========================================================================== @@ -196,6 +198,7 @@ BooksSettings::BooksSettings(QObject* aParent) : iFontSize(new MGConfItem(DCONF_PATH KEY_FONT_SIZE, this)), iPageDetails(new MGConfItem(DCONF_PATH KEY_PAGE_DETAILS, this)), iInvertColors(new MGConfItem(DCONF_PATH KEY_INVERT_COLORS, this)), + iCurrentStorage(new MGConfItem(DCONF_PATH KEY_CURRENT_STORAGE, this)), iCurrentBookPath(new MGConfItem(DCONF_PATH KEY_CURRENT_BOOK, this)), iCurrentBook(NULL) { @@ -204,6 +207,7 @@ BooksSettings::BooksSettings(QObject* aParent) : connect(iFontSize, SIGNAL(valueChanged()), SLOT(onFontSizeValueChanged())); connect(iPageDetails, SIGNAL(valueChanged()), SIGNAL(pageDetailsChanged())); connect(iInvertColors, SIGNAL(valueChanged()), SIGNAL(invertColorsChanged())); + connect(iCurrentStorage, SIGNAL(valueChanged()), SIGNAL(currentStorageChanged())); connect(iCurrentBookPath, SIGNAL(valueChanged()), SLOT(onCurrentBookPathChanged())); } @@ -259,6 +263,20 @@ BooksSettings::setInvertColors( iInvertColors->set(aValue); } +QString +BooksSettings::currentStorage() const +{ + return iCurrentStorage->value(DEFAULT_CURRENT_STORAGE).toString(); +} + +void +BooksSettings::setCurrentStorage( + QString aValue) +{ + HDEBUG(aValue); + iCurrentStorage->set(aValue); +} + QObject* BooksSettings::currentBook() const { diff --git a/app/src/BooksSettings.h b/app/src/BooksSettings.h index 074e237..de5f36c 100644 --- a/app/src/BooksSettings.h +++ b/app/src/BooksSettings.h @@ -48,6 +48,7 @@ class BooksSettings : public QObject Q_PROPERTY(int pageDetails READ pageDetails WRITE setPageDetails NOTIFY pageDetailsChanged) Q_PROPERTY(bool invertColors READ invertColors WRITE setInvertColors NOTIFY invertColorsChanged) Q_PROPERTY(QObject* currentBook READ currentBook WRITE setCurrentBook NOTIFY currentBookChanged) + Q_PROPERTY(QString currentStorage READ currentStorage WRITE setCurrentStorage NOTIFY currentStorageChanged) class TextStyle; public: @@ -73,12 +74,16 @@ public: QObject* currentBook() const; void setCurrentBook(QObject* aBook); + QString currentStorage() const; + void setCurrentStorage(QString aValue); + signals: void fontSizeChanged(); void textStyleChanged(); void pageDetailsChanged(); void invertColorsChanged(); void currentBookChanged(); + void currentStorageChanged(); private Q_SLOTS: void onFontSizeValueChanged(); @@ -92,6 +97,7 @@ private: MGConfItem* iFontSize; MGConfItem* iPageDetails; MGConfItem* iInvertColors; + MGConfItem* iCurrentStorage; MGConfItem* iCurrentBookPath; shared_ptr iTextStyle; BooksBook* iCurrentBook; diff --git a/app/src/BooksStorageModel.cpp b/app/src/BooksStorageModel.cpp index 76ecfea..44e79f8 100644 --- a/app/src/BooksStorageModel.cpp +++ b/app/src/BooksStorageModel.cpp @@ -102,6 +102,23 @@ int BooksStorageModel::count() const return iList.count(); } +int BooksStorageModel::deviceIndex(QString aDevice) const +{ + if (!aDevice.isEmpty()) { + for (int i=iList.count()-1; i>=0; i--) { + if (iList.at(i)->device() == aDevice) { + return i; + } + } + } + return -1; +} + +QString BooksStorageModel::deviceAt(int aIndex) const +{ + return validIndex(aIndex) ? iList.at(aIndex)->device() : QString(); +} + void BooksStorageModel::setDeleteAllRequest(int aIndex, bool aValue) { if (validIndex(aIndex)) { diff --git a/app/src/BooksStorageModel.h b/app/src/BooksStorageModel.h index 486b7dc..4dab32e 100644 --- a/app/src/BooksStorageModel.h +++ b/app/src/BooksStorageModel.h @@ -53,6 +53,8 @@ public: ~BooksStorageModel(); Q_INVOKABLE int count() const; + Q_INVOKABLE int deviceIndex(QString aDevice) const; + Q_INVOKABLE QString deviceAt(int aIndex) const; Q_INVOKABLE void setDeleteAllRequest(int aIndex, bool aValue); Q_INVOKABLE void cancelDeleteAllRequests(); Q_INVOKABLE QObject* get(int aIndex) const;