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 <QtWidgets>
#include <QDebug>
#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<QListWidgetItem *> 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();
}

View File

@ -3,7 +3,6 @@
#include <QWidget>
#include <QLinkedList>
#include <QIcon>
#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<VHistoryEntry> m_histories;
@ -97,8 +89,6 @@ private:
bool m_updatePending;
QDate m_currentDate;
QIcon m_folderIcon;
};
#endif // VHISTORYLIST_H

View File

@ -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()