From eae345d9208045632d4d550b6c5bcea8db05b9e3 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 23 May 2018 20:07:53 +0800 Subject: [PATCH] cart: lazy initialization --- src/vcart.cpp | 110 +++++++++++++++++++++++++++---------------- src/vcart.h | 18 +++---- src/vhistorylist.cpp | 9 ++-- src/vhistorylist.h | 2 +- src/vmainwindow.cpp | 3 +- 5 files changed, 86 insertions(+), 56 deletions(-) diff --git a/src/vcart.cpp b/src/vcart.cpp index e4983d61..b6553943 100644 --- a/src/vcart.cpp +++ b/src/vcart.cpp @@ -15,17 +15,20 @@ extern VMainWindow *g_mainWin; extern VNote *g_vnote; VCart::VCart(QWidget *p_parent) - : QWidget(p_parent) + : QWidget(p_parent), + m_initialized(false), + m_uiInitialized(false) { - setupUI(); - - updateNumberLabel(); - - initActions(); } void VCart::setupUI() { + if (m_uiInitialized) { + return; + } + + m_uiInitialized = true; + m_clearBtn = new QPushButton(VIconUtils::buttonDangerIcon(":/resources/icons/clear_cart.svg"), ""); m_clearBtn->setToolTip(tr("Clear")); m_clearBtn->setProperty("FlatBtn", true); @@ -73,35 +76,6 @@ void VCart::setupUI() setLayout(mainLayout); } -void VCart::initActions() -{ - m_openAct = new QAction(tr("&Open"), this); - m_openAct->setToolTip(tr("Open selected notes")); - connect(m_openAct, &QAction::triggered, - this, &VCart::openSelectedItems); - - m_locateAct = new QAction(VIconUtils::menuIcon(":/resources/icons/locate_note.svg"), - tr("&Locate To Folder"), - this); - m_locateAct->setToolTip(tr("Locate the folder of current note")); - connect(m_locateAct, &QAction::triggered, - this, &VCart::locateCurrentItem); - - m_deleteAct = new QAction(VIconUtils::menuDangerIcon(":/resources/icons/delete_cart_item.svg"), - tr("&Delete"), - this); - m_deleteAct->setToolTip(tr("Delete selected items from Cart")); - connect(m_deleteAct, &QAction::triggered, - this, &VCart::deleteSelectedItems); - - m_sortAct = new QAction(VIconUtils::menuIcon(":/resources/icons/sort.svg"), - tr("&Sort"), - this); - m_sortAct->setToolTip(tr("Sort items in Cart")); - connect(m_sortAct, &QAction::triggered, - this, &VCart::sortItems); -} - void VCart::handleContextMenuRequested(QPoint p_pos) { QListWidgetItem *item = m_itemList->itemAt(p_pos); @@ -109,13 +83,29 @@ void VCart::handleContextMenuRequested(QPoint p_pos) menu.setToolTipsVisible(true); if (item) { - menu.addAction(m_openAct); + QAction *openAct = new QAction(tr("&Open"), &menu); + openAct->setToolTip(tr("Open selected notes")); + connect(openAct, &QAction::triggered, + this, &VCart::openSelectedItems); + menu.addAction(openAct); if (m_itemList->selectedItems().size() == 1) { - menu.addAction(m_locateAct); + QAction *locateAct = new QAction(VIconUtils::menuIcon(":/resources/icons/locate_note.svg"), + tr("&Locate To Folder"), + &menu); + locateAct->setToolTip(tr("Locate the folder of current note")); + connect(locateAct, &QAction::triggered, + this, &VCart::locateCurrentItem); + menu.addAction(locateAct); } - menu.addAction(m_deleteAct); + QAction *deleteAct = new QAction(VIconUtils::menuDangerIcon(":/resources/icons/delete_cart_item.svg"), + tr("&Delete"), + &menu); + deleteAct->setToolTip(tr("Delete selected items from Cart")); + connect(deleteAct, &QAction::triggered, + this, &VCart::deleteSelectedItems); + menu.addAction(deleteAct); } if (m_itemList->count() == 0) { @@ -126,13 +116,21 @@ void VCart::handleContextMenuRequested(QPoint p_pos) menu.addSeparator(); } - menu.addAction(m_sortAct); + QAction *sortAct = new QAction(VIconUtils::menuIcon(":/resources/icons/sort.svg"), + tr("&Sort"), + &menu); + sortAct->setToolTip(tr("Sort items in Cart")); + connect(sortAct, &QAction::triggered, + this, &VCart::sortItems); + menu.addAction(sortAct); menu.exec(m_itemList->mapToGlobal(p_pos)); } void VCart::addFile(const QString &p_filePath) { + init(); + if (p_filePath.isEmpty() || findFileInCart(p_filePath) != -1) { return; @@ -216,11 +214,15 @@ QString VCart::getFilePath(const QListWidgetItem *p_item) const int VCart::count() const { + const_cast(this)->init(); + return m_itemList->count(); } QVector VCart::getFiles() const { + const_cast(this)->init(); + QVector files; int cnt = m_itemList->count(); for (int i = 0; i < cnt; ++i) { @@ -277,19 +279,45 @@ void VCart::updateNumberLabel() const void VCart::showNavigation() { + setupUI(); + VNavigationMode::showNavigation(m_itemList); } bool VCart::handleKeyNavigation(int p_key, bool &p_succeed) { static bool secondKey = false; + setupUI(); + return VNavigationMode::handleKeyNavigation(m_itemList, secondKey, p_key, p_succeed); } -QWidget *VCart::getContentWidget() const +void VCart::init() { - return m_itemList; + if (m_initialized) { + return; + } + + m_initialized = true; + + setupUI(); + updateNumberLabel(); +} + +void VCart::showEvent(QShowEvent *p_event) +{ + init(); + + QWidget::showEvent(p_event); +} + +void VCart::focusInEvent(QFocusEvent *p_event) +{ + init(); + + QWidget::focusInEvent(p_event); + m_itemList->setFocus(); } diff --git a/src/vcart.h b/src/vcart.h index 95380a72..4be62580 100644 --- a/src/vcart.h +++ b/src/vcart.h @@ -24,12 +24,15 @@ public: QVector getFiles() const; - QWidget *getContentWidget() const; - // Implementations for VNavigationMode. void showNavigation() Q_DECL_OVERRIDE; bool handleKeyNavigation(int p_key, bool &p_succeed) Q_DECL_OVERRIDE; +protected: + void showEvent(QShowEvent *p_event) Q_DECL_OVERRIDE; + + void focusInEvent(QFocusEvent *p_event) Q_DECL_OVERRIDE; + private slots: void handleContextMenuRequested(QPoint p_pos); @@ -46,7 +49,11 @@ private slots: private: void setupUI(); - void initActions(); + void init(); + + bool m_initialized; + + bool m_uiInitialized; // Return index of item. int findFileInCart(const QString &p_file) const; @@ -60,11 +67,6 @@ private: QPushButton *m_clearBtn; QLabel *m_numLabel; VListWidget *m_itemList; - - QAction *m_openAct; - QAction *m_locateAct; - QAction *m_deleteAct; - QAction *m_sortAct; }; #endif // VCART_H diff --git a/src/vhistorylist.cpp b/src/vhistorylist.cpp index a3b9d6db..5b578ebf 100644 --- a/src/vhistorylist.cpp +++ b/src/vhistorylist.cpp @@ -21,7 +21,7 @@ extern VNote *g_vnote; VHistoryList::VHistoryList(QWidget *p_parent) : QWidget(p_parent), m_initialized(false), - m_uiSetuped(false), + m_uiInitialized(false), m_updatePending(true), m_currentDate(QDate::currentDate()) { @@ -29,11 +29,11 @@ VHistoryList::VHistoryList(QWidget *p_parent) void VHistoryList::setupUI() { - if (m_uiSetuped) { + if (m_uiInitialized) { return; } - m_uiSetuped = true; + m_uiInitialized = true; m_clearBtn = new QPushButton(VIconUtils::buttonDangerIcon(":/resources/icons/clear_history.svg"), ""); m_clearBtn->setToolTip(tr("Clear")); @@ -183,11 +183,12 @@ void VHistoryList::init() return; } + m_initialized = true; + setupUI(); g_config->getHistory(m_histories); - m_initialized = true; m_updatePending = true; } diff --git a/src/vhistorylist.h b/src/vhistorylist.h index bc7d36f4..f6237f40 100644 --- a/src/vhistorylist.h +++ b/src/vhistorylist.h @@ -80,7 +80,7 @@ private: // Whether data is loaded. bool m_initialized; - bool m_uiSetuped; + bool m_uiInitialized; // New files are appended to the end. QLinkedList m_histories; diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index 7d121480..dc1e1174 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -1282,8 +1282,7 @@ void VMainWindow::initToolsDock() tr("Snippets")); m_toolBox->addItem(m_cart, ":/resources/icons/cart.svg", - tr("Cart"), - m_cart->getContentWidget()); + tr("Cart")); m_toolDock->setWidget(m_toolBox); addDockWidget(Qt::RightDockWidgetArea, m_toolDock);