From 07f1689e110721791840343e3d8e5524086e83be Mon Sep 17 00:00:00 2001 From: Le Tan Date: Fri, 18 May 2018 20:59:32 +0800 Subject: [PATCH] History: lazy initialization --- src/vhistorylist.cpp | 103 ++++++++++++++++++++++--------------------- src/vhistorylist.h | 20 +++------ src/vmainwindow.cpp | 3 +- 3 files changed, 59 insertions(+), 67 deletions(-) diff --git a/src/vhistorylist.cpp b/src/vhistorylist.cpp index 8d553c80..67f4b646 100644 --- a/src/vhistorylist.cpp +++ b/src/vhistorylist.cpp @@ -1,7 +1,6 @@ #include "vhistorylist.h" #include -#include #include "utils/viconutils.h" #include "utils/vutils.h" @@ -22,14 +21,20 @@ extern VNote *g_vnote; VHistoryList::VHistoryList(QWidget *p_parent) : QWidget(p_parent), m_initialized(false), + m_uiSetuped(false), m_updatePending(true), m_currentDate(QDate::currentDate()) { - setupUI(); } void VHistoryList::setupUI() { + if (m_uiSetuped) { + return; + } + + m_uiSetuped = true; + m_clearBtn = new QPushButton(VIconUtils::buttonDangerIcon(":/resources/icons/clear_history.svg"), ""); m_clearBtn->setToolTip(tr("Clear")); m_clearBtn->setProperty("FlatBtn", true); @@ -77,40 +82,6 @@ void VHistoryList::setupUI() setLayout(mainLayout); } -void VHistoryList::initActions() -{ - m_openAct = new QAction(tr("&Open"), this); - m_openAct->setToolTip(tr("Open selected notes")); - connect(m_openAct, &QAction::triggered, - this, &VHistoryList::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, &VHistoryList::locateCurrentItem); - - m_pinAct = new QAction(VIconUtils::menuIcon(":/resources/icons/pin.svg"), - tr("Pin"), - this); - m_pinAct->setToolTip(tr("Pin selected notes in History")); - connect(m_pinAct, &QAction::triggered, - this, &VHistoryList::pinSelectedItems); - - m_unpinAct = new QAction(tr("Unpin"), this); - m_unpinAct->setToolTip(tr("Unpin selected notes in History")); - connect(m_unpinAct, &QAction::triggered, - this, &VHistoryList::unpinSelectedItems); - - m_addToCartAct = new QAction(VIconUtils::menuIcon(":/resources/icons/cart.svg"), - tr("Add To Cart"), - this); - m_addToCartAct->setToolTip(tr("Add selected notes to Cart for further processing")); - connect(m_addToCartAct, &QAction::triggered, - this, &VHistoryList::addFileToCart); -} - void VHistoryList::addFile(const QString &p_filePath) { init(); @@ -212,9 +183,7 @@ void VHistoryList::init() return; } - m_folderIcon = VIconUtils::treeViewIcon(":/resources/icons/dir_item.svg"); - - initActions(); + setupUI(); g_config->getHistory(m_histories); @@ -295,13 +264,14 @@ void VHistoryList::updateList() m_itemList->addItem(seps[i].m_item); } + QIcon folderIcon(VIconUtils::treeViewIcon(":/resources/icons/dir_item.svg")); for (auto it = m_histories.cbegin(); it != m_histories.cend(); ++it) { QListWidgetItem *item = new QListWidgetItem(VUtils::fileNameFromPath(it->m_file)); item->setToolTip(it->m_file); item->setData(Qt::UserRole, (qulonglong)&(*it)); if (it->m_isFolder) { - item->setIcon(m_folderIcon); + item->setIcon(folderIcon); } if (it->m_isPinned) { @@ -330,11 +300,6 @@ void VHistoryList::updateList() seps.clear(); } -QWidget *VHistoryList::getContentWidget() const -{ - return m_itemList; -} - void VHistoryList::handleContextMenuRequested(QPoint p_pos) { QListWidgetItem *item = m_itemList->itemAt(p_pos); @@ -345,11 +310,21 @@ void VHistoryList::handleContextMenuRequested(QPoint p_pos) QMenu menu(this); menu.setToolTipsVisible(true); - menu.addAction(m_openAct); + QAction *openAct = new QAction(tr("&Open"), &menu); + openAct->setToolTip(tr("Open selected notes")); + connect(openAct, &QAction::triggered, + this, &VHistoryList::openSelectedItems); + menu.addAction(openAct); QList selectedItems = m_itemList->selectedItems(); if (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, &VHistoryList::locateCurrentItem); + menu.addAction(locateAct); } bool allPinned = true, allUnpinned = true; @@ -362,14 +337,30 @@ void VHistoryList::handleContextMenuRequested(QPoint p_pos) } if (allUnpinned) { - menu.addAction(m_pinAct); + QAction *pinAct = new QAction(VIconUtils::menuIcon(":/resources/icons/pin.svg"), + tr("Pin"), + &menu); + pinAct->setToolTip(tr("Pin selected notes in History")); + connect(pinAct, &QAction::triggered, + this, &VHistoryList::pinSelectedItems); + menu.addAction(pinAct); } else if (allPinned) { - menu.addAction(m_unpinAct); + QAction *unpinAct = new QAction(tr("Unpin"), &menu); + unpinAct->setToolTip(tr("Unpin selected notes in History")); + connect(unpinAct, &QAction::triggered, + this, &VHistoryList::unpinSelectedItems); + menu.addAction(unpinAct); } menu.addSeparator(); - menu.addAction(m_addToCartAct); + QAction *addToCartAct = new QAction(VIconUtils::menuIcon(":/resources/icons/cart.svg"), + tr("Add To Cart"), + &menu); + addToCartAct->setToolTip(tr("Add selected notes to Cart for further processing")); + connect(addToCartAct, &QAction::triggered, + this, &VHistoryList::addFileToCart); + menu.addAction(addToCartAct); menu.exec(m_itemList->mapToGlobal(p_pos)); } @@ -480,12 +471,16 @@ void VHistoryList::locateCurrentItem() void VHistoryList::showNavigation() { + setupUI(); + VNavigationMode::showNavigation(m_itemList); } bool VHistoryList::handleKeyNavigation(int p_key, bool &p_succeed) { static bool secondKey = false; + setupUI(); + return VNavigationMode::handleKeyNavigation(m_itemList, secondKey, p_key, @@ -505,3 +500,11 @@ void VHistoryList::addFileToCart() const .arg(items.size()) .arg(items.size() > 1 ? tr("notes") : tr("note"))); } + +void VHistoryList::focusInEvent(QFocusEvent *p_event) +{ + init(); + + QWidget::focusInEvent(p_event); + m_itemList->setFocus(); +} diff --git a/src/vhistorylist.h b/src/vhistorylist.h index 8761e7be..bc7d36f4 100644 --- a/src/vhistorylist.h +++ b/src/vhistorylist.h @@ -3,7 +3,6 @@ #include #include -#include #include "vhistoryentry.h" #include "vnavigationmode.h" @@ -11,9 +10,8 @@ class QPushButton; class VListWidget; class QListWidgetItem; -class QLabel; -class QAction; class QShowEvent; +class QFocusEvent; class VHistoryList : public QWidget, public VNavigationMode { @@ -21,8 +19,6 @@ class VHistoryList : public QWidget, public VNavigationMode public: explicit VHistoryList(QWidget *p_parent = nullptr); - QWidget *getContentWidget() const; - void pinFiles(const QStringList &p_files); void pinFolder(const QString &p_folder); @@ -37,6 +33,8 @@ public slots: protected: void showEvent(QShowEvent *p_event) Q_DECL_OVERRIDE; + void focusInEvent(QFocusEvent *p_event) Q_DECL_OVERRIDE; + private slots: void handleContextMenuRequested(QPoint p_pos); @@ -56,8 +54,6 @@ private slots: private: void setupUI(); - void initActions(); - // Read data from config file. void init(); @@ -81,15 +77,11 @@ private: QPushButton *m_clearBtn; VListWidget *m_itemList; - QAction *m_openAct; - QAction *m_locateAct; - QAction *m_pinAct; - QAction *m_unpinAct; - QAction *m_addToCartAct; - // Whether data is loaded. bool m_initialized; + bool m_uiSetuped; + // New files are appended to the end. QLinkedList m_histories; @@ -97,8 +89,6 @@ private: bool m_updatePending; QDate m_currentDate; - - QIcon m_folderIcon; }; #endif // VHISTORYLIST_H diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index ecd1a463..d7f8b16e 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -279,8 +279,7 @@ void VMainWindow::setupNaviBox() m_historyList = new VHistoryList(); m_naviBox->addItem(m_historyList, ":/resources/icons/history.svg", - tr("History"), - m_historyList->getContentWidget()); + tr("History")); } void VMainWindow::setupNotebookPanel()