History: lazy initialization

This commit is contained in:
Le Tan 2018-05-18 20:59:32 +08:00
parent 18ee02d920
commit 07f1689e11
3 changed files with 59 additions and 67 deletions

View File

@ -1,7 +1,6 @@
#include "vhistorylist.h" #include "vhistorylist.h"
#include <QtWidgets> #include <QtWidgets>
#include <QDebug>
#include "utils/viconutils.h" #include "utils/viconutils.h"
#include "utils/vutils.h" #include "utils/vutils.h"
@ -22,14 +21,20 @@ extern VNote *g_vnote;
VHistoryList::VHistoryList(QWidget *p_parent) VHistoryList::VHistoryList(QWidget *p_parent)
: QWidget(p_parent), : QWidget(p_parent),
m_initialized(false), m_initialized(false),
m_uiSetuped(false),
m_updatePending(true), m_updatePending(true),
m_currentDate(QDate::currentDate()) m_currentDate(QDate::currentDate())
{ {
setupUI();
} }
void VHistoryList::setupUI() void VHistoryList::setupUI()
{ {
if (m_uiSetuped) {
return;
}
m_uiSetuped = true;
m_clearBtn = new QPushButton(VIconUtils::buttonDangerIcon(":/resources/icons/clear_history.svg"), ""); m_clearBtn = new QPushButton(VIconUtils::buttonDangerIcon(":/resources/icons/clear_history.svg"), "");
m_clearBtn->setToolTip(tr("Clear")); m_clearBtn->setToolTip(tr("Clear"));
m_clearBtn->setProperty("FlatBtn", true); m_clearBtn->setProperty("FlatBtn", true);
@ -77,40 +82,6 @@ void VHistoryList::setupUI()
setLayout(mainLayout); 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) void VHistoryList::addFile(const QString &p_filePath)
{ {
init(); init();
@ -212,9 +183,7 @@ void VHistoryList::init()
return; return;
} }
m_folderIcon = VIconUtils::treeViewIcon(":/resources/icons/dir_item.svg"); setupUI();
initActions();
g_config->getHistory(m_histories); g_config->getHistory(m_histories);
@ -295,13 +264,14 @@ void VHistoryList::updateList()
m_itemList->addItem(seps[i].m_item); 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) { for (auto it = m_histories.cbegin(); it != m_histories.cend(); ++it) {
QListWidgetItem *item = new QListWidgetItem(VUtils::fileNameFromPath(it->m_file)); QListWidgetItem *item = new QListWidgetItem(VUtils::fileNameFromPath(it->m_file));
item->setToolTip(it->m_file); item->setToolTip(it->m_file);
item->setData(Qt::UserRole, (qulonglong)&(*it)); item->setData(Qt::UserRole, (qulonglong)&(*it));
if (it->m_isFolder) { if (it->m_isFolder) {
item->setIcon(m_folderIcon); item->setIcon(folderIcon);
} }
if (it->m_isPinned) { if (it->m_isPinned) {
@ -330,11 +300,6 @@ void VHistoryList::updateList()
seps.clear(); seps.clear();
} }
QWidget *VHistoryList::getContentWidget() const
{
return m_itemList;
}
void VHistoryList::handleContextMenuRequested(QPoint p_pos) void VHistoryList::handleContextMenuRequested(QPoint p_pos)
{ {
QListWidgetItem *item = m_itemList->itemAt(p_pos); QListWidgetItem *item = m_itemList->itemAt(p_pos);
@ -345,11 +310,21 @@ void VHistoryList::handleContextMenuRequested(QPoint p_pos)
QMenu menu(this); QMenu menu(this);
menu.setToolTipsVisible(true); 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<QListWidgetItem *> selectedItems = m_itemList->selectedItems(); QList<QListWidgetItem *> selectedItems = m_itemList->selectedItems();
if (selectedItems.size() == 1) { 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; bool allPinned = true, allUnpinned = true;
@ -362,14 +337,30 @@ void VHistoryList::handleContextMenuRequested(QPoint p_pos)
} }
if (allUnpinned) { 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) { } 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.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)); menu.exec(m_itemList->mapToGlobal(p_pos));
} }
@ -480,12 +471,16 @@ void VHistoryList::locateCurrentItem()
void VHistoryList::showNavigation() void VHistoryList::showNavigation()
{ {
setupUI();
VNavigationMode::showNavigation(m_itemList); VNavigationMode::showNavigation(m_itemList);
} }
bool VHistoryList::handleKeyNavigation(int p_key, bool &p_succeed) bool VHistoryList::handleKeyNavigation(int p_key, bool &p_succeed)
{ {
static bool secondKey = false; static bool secondKey = false;
setupUI();
return VNavigationMode::handleKeyNavigation(m_itemList, return VNavigationMode::handleKeyNavigation(m_itemList,
secondKey, secondKey,
p_key, p_key,
@ -505,3 +500,11 @@ void VHistoryList::addFileToCart() const
.arg(items.size()) .arg(items.size())
.arg(items.size() > 1 ? tr("notes") : tr("note"))); .arg(items.size() > 1 ? tr("notes") : tr("note")));
} }
void VHistoryList::focusInEvent(QFocusEvent *p_event)
{
init();
QWidget::focusInEvent(p_event);
m_itemList->setFocus();
}

View File

@ -3,7 +3,6 @@
#include <QWidget> #include <QWidget>
#include <QLinkedList> #include <QLinkedList>
#include <QIcon>
#include "vhistoryentry.h" #include "vhistoryentry.h"
#include "vnavigationmode.h" #include "vnavigationmode.h"
@ -11,9 +10,8 @@
class QPushButton; class QPushButton;
class VListWidget; class VListWidget;
class QListWidgetItem; class QListWidgetItem;
class QLabel;
class QAction;
class QShowEvent; class QShowEvent;
class QFocusEvent;
class VHistoryList : public QWidget, public VNavigationMode class VHistoryList : public QWidget, public VNavigationMode
{ {
@ -21,8 +19,6 @@ class VHistoryList : public QWidget, public VNavigationMode
public: public:
explicit VHistoryList(QWidget *p_parent = nullptr); explicit VHistoryList(QWidget *p_parent = nullptr);
QWidget *getContentWidget() const;
void pinFiles(const QStringList &p_files); void pinFiles(const QStringList &p_files);
void pinFolder(const QString &p_folder); void pinFolder(const QString &p_folder);
@ -37,6 +33,8 @@ public slots:
protected: protected:
void showEvent(QShowEvent *p_event) Q_DECL_OVERRIDE; void showEvent(QShowEvent *p_event) Q_DECL_OVERRIDE;
void focusInEvent(QFocusEvent *p_event) Q_DECL_OVERRIDE;
private slots: private slots:
void handleContextMenuRequested(QPoint p_pos); void handleContextMenuRequested(QPoint p_pos);
@ -56,8 +54,6 @@ private slots:
private: private:
void setupUI(); void setupUI();
void initActions();
// Read data from config file. // Read data from config file.
void init(); void init();
@ -81,15 +77,11 @@ private:
QPushButton *m_clearBtn; QPushButton *m_clearBtn;
VListWidget *m_itemList; VListWidget *m_itemList;
QAction *m_openAct;
QAction *m_locateAct;
QAction *m_pinAct;
QAction *m_unpinAct;
QAction *m_addToCartAct;
// Whether data is loaded. // Whether data is loaded.
bool m_initialized; bool m_initialized;
bool m_uiSetuped;
// New files are appended to the end. // New files are appended to the end.
QLinkedList<VHistoryEntry> m_histories; QLinkedList<VHistoryEntry> m_histories;
@ -97,8 +89,6 @@ private:
bool m_updatePending; bool m_updatePending;
QDate m_currentDate; QDate m_currentDate;
QIcon m_folderIcon;
}; };
#endif // VHISTORYLIST_H #endif // VHISTORYLIST_H

View File

@ -279,8 +279,7 @@ void VMainWindow::setupNaviBox()
m_historyList = new VHistoryList(); m_historyList = new VHistoryList();
m_naviBox->addItem(m_historyList, m_naviBox->addItem(m_historyList,
":/resources/icons/history.svg", ":/resources/icons/history.svg",
tr("History"), tr("History"));
m_historyList->getContentWidget());
} }
void VMainWindow::setupNotebookPanel() void VMainWindow::setupNotebookPanel()