Browse Source

cart: lazy initialization

Le Tan 7 years ago
parent
commit
eae345d920
5 changed files with 86 additions and 56 deletions
  1. 69 41
      src/vcart.cpp
  2. 10 8
      src/vcart.h
  3. 5 4
      src/vhistorylist.cpp
  4. 1 1
      src/vhistorylist.h
  5. 1 2
      src/vmainwindow.cpp

+ 69 - 41
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<VCart *>(this)->init();
+
     return m_itemList->count();
 }
 
 QVector<QString> VCart::getFiles() const
 {
+    const_cast<VCart *>(this)->init();
+
     QVector<QString> 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();
 }

+ 10 - 8
src/vcart.h

@@ -24,12 +24,15 @@ public:
 
     QVector<QString> 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

+ 5 - 4
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;
 }
 

+ 1 - 1
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<VHistoryEntry> m_histories;

+ 1 - 2
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);