SnippetList: lazy initialization

This commit is contained in:
Le Tan 2018-05-23 20:08:05 +08:00
parent eae345d920
commit 43509d7022
4 changed files with 91 additions and 68 deletions

View File

@ -10,7 +10,6 @@ class QPushButton;
class VListWidget;
class QListWidgetItem;
class QLabel;
class QAction;
class VCart : public QWidget, public VNavigationMode
{

View File

@ -15,7 +15,6 @@
#include "vnavigationmode.h"
#include "vlistwidget.h"
class QAction;
class VNote;
class QListWidget;
class QPushButton;

View File

@ -19,33 +19,21 @@ extern VMainWindow *g_mainWin;
const QString VSnippetList::c_infoShortcutSequence = "F2";
VSnippetList::VSnippetList(QWidget *p_parent)
: QWidget(p_parent)
: QWidget(p_parent),
m_initialized(false),
m_uiInitialized(false)
{
setupUI();
initShortcuts();
initActions();
if (!readSnippetsFromConfig()) {
VUtils::showMessage(QMessageBox::Warning,
tr("Warning"),
tr("Fail to read snippets from <span style=\"%1\">%2</span>.")
.arg(g_config->c_dataTextStyle)
.arg(g_config->getSnippetConfigFolder()),
"",
QMessageBox::Ok,
QMessageBox::Ok,
this);
}
updateContent();
updateNumberLabel();
}
void VSnippetList::setupUI()
{
if (m_uiInitialized) {
return;
}
m_uiInitialized = true;
m_addBtn = new QPushButton(VIconUtils::buttonIcon(":/resources/icons/add_snippet.svg"), "");
m_addBtn->setToolTip(tr("New Snippet"));
m_addBtn->setProperty("FlatBtn", true);
@ -88,40 +76,6 @@ void VSnippetList::setupUI()
setLayout(mainLayout);
}
void VSnippetList::initActions()
{
m_applyAct = new QAction(VIconUtils::menuIcon(":/resources/icons/apply_snippet.svg"),
tr("&Apply"),
this);
m_applyAct->setToolTip(tr("Insert this snippet in editor"));
connect(m_applyAct, &QAction::triggered,
this, [this]() {
QListWidgetItem *item = m_snippetList->currentItem();
handleItemActivated(item);
});
m_infoAct = new QAction(VIconUtils::menuIcon(":/resources/icons/snippet_info.svg"),
tr("&Info\t%1").arg(VUtils::getShortcutText(c_infoShortcutSequence)),
this);
m_infoAct->setToolTip(tr("View and edit snippet's information"));
connect(m_infoAct, &QAction::triggered,
this, &VSnippetList::snippetInfo);
m_deleteAct = new QAction(VIconUtils::menuDangerIcon(":/resources/icons/delete_snippet.svg"),
tr("&Delete"),
this);
m_deleteAct->setToolTip(tr("Delete selected snippets"));
connect(m_deleteAct, &QAction::triggered,
this, &VSnippetList::deleteSelectedItems);
m_sortAct = new QAction(VIconUtils::menuIcon(":/resources/icons/sort.svg"),
tr("&Sort"),
this);
m_sortAct->setToolTip(tr("Sort snippets manually"));
connect(m_sortAct, &QAction::triggered,
this, &VSnippetList::sortItems);
}
void VSnippetList::initShortcuts()
{
QShortcut *infoShortcut = new QShortcut(QKeySequence(c_infoShortcutSequence), this);
@ -178,11 +132,33 @@ void VSnippetList::handleContextMenuRequested(QPoint p_pos)
if (item) {
int itemCount = m_snippetList->selectedItems().size();
if (itemCount == 1) {
menu.addAction(m_applyAct);
menu.addAction(m_infoAct);
QAction *applyAct = new QAction(VIconUtils::menuIcon(":/resources/icons/apply_snippet.svg"),
tr("&Apply"),
&menu);
applyAct->setToolTip(tr("Insert this snippet in editor"));
connect(applyAct, &QAction::triggered,
this, [this]() {
QListWidgetItem *item = m_snippetList->currentItem();
handleItemActivated(item);
});
menu.addAction(applyAct);
QAction *infoAct = new QAction(VIconUtils::menuIcon(":/resources/icons/snippet_info.svg"),
tr("&Info\t%1").arg(VUtils::getShortcutText(c_infoShortcutSequence)),
&menu);
infoAct->setToolTip(tr("View and edit snippet's information"));
connect(infoAct, &QAction::triggered,
this, &VSnippetList::snippetInfo);
menu.addAction(infoAct);
}
menu.addAction(m_deleteAct);
QAction *deleteAct = new QAction(VIconUtils::menuDangerIcon(":/resources/icons/delete_snippet.svg"),
tr("&Delete"),
&menu);
deleteAct->setToolTip(tr("Delete selected snippets"));
connect(deleteAct, &QAction::triggered,
this, &VSnippetList::deleteSelectedItems);
menu.addAction(deleteAct);
}
m_snippetList->update();
@ -192,7 +168,13 @@ void VSnippetList::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 snippets manually"));
connect(sortAct, &QAction::triggered,
this, &VSnippetList::sortItems);
menu.addAction(sortAct);
}
if (!menu.actions().isEmpty()) {
@ -492,12 +474,16 @@ void VSnippetList::keyPressEvent(QKeyEvent *p_event)
void VSnippetList::showNavigation()
{
setupUI();
VNavigationMode::showNavigation(m_snippetList);
}
bool VSnippetList::handleKeyNavigation(int p_key, bool &p_succeed)
{
static bool secondKey = false;
setupUI();
return VNavigationMode::handleKeyNavigation(m_snippetList,
secondKey,
p_key,
@ -617,6 +603,8 @@ bool VSnippetList::deleteSnippetFile(const VSnippet &p_snippet, QString *p_errMs
void VSnippetList::focusInEvent(QFocusEvent *p_event)
{
init();
QWidget::focusInEvent(p_event);
if (m_snippets.isEmpty()) {
@ -632,3 +620,36 @@ void VSnippetList::updateNumberLabel() const
m_numLabel->setText(tr("%1 %2").arg(cnt)
.arg(cnt > 1 ? tr("Items") : tr("Item")));
}
void VSnippetList::init()
{
if (m_initialized) {
return;
}
m_initialized = true;
setupUI();
if (!readSnippetsFromConfig()) {
VUtils::showMessage(QMessageBox::Warning,
tr("Warning"),
tr("Fail to read snippets from <span style=\"%1\">%2</span>.")
.arg(g_config->c_dataTextStyle)
.arg(g_config->getSnippetConfigFolder()),
"",
QMessageBox::Ok,
QMessageBox::Ok,
this);
}
updateContent();
}
void VSnippetList::showEvent(QShowEvent *p_event)
{
init();
QWidget::showEvent(p_event);
}

View File

@ -12,7 +12,6 @@ class QPushButton;
class VListWidget;
class QListWidgetItem;
class QLabel;
class QAction;
class QKeyEvent;
class QFocusEvent;
@ -32,6 +31,8 @@ public:
bool handleKeyNavigation(int p_key, bool &p_succeed) Q_DECL_OVERRIDE;
protected:
void showEvent(QShowEvent *p_event) Q_DECL_OVERRIDE;
void keyPressEvent(QKeyEvent *p_event) Q_DECL_OVERRIDE;
void focusInEvent(QFocusEvent *p_event) Q_DECL_OVERRIDE;
@ -52,7 +53,7 @@ private slots:
private:
void setupUI();
void initActions();
void init();
void initShortcuts();
@ -89,16 +90,15 @@ private:
void updateNumberLabel() const;
bool m_initialized;
bool m_uiInitialized;
QPushButton *m_addBtn;
QPushButton *m_locateBtn;
QLabel *m_numLabel;
VListWidget *m_snippetList;
QAction *m_applyAct;
QAction *m_infoAct;
QAction *m_deleteAct;
QAction *m_sortAct;
QVector<VSnippet> m_snippets;
static const QString c_infoShortcutSequence;
@ -106,11 +106,15 @@ private:
inline const QVector<VSnippet> &VSnippetList::getSnippets() const
{
const_cast<VSnippetList *>(this)->init();
return m_snippets;
}
inline const VSnippet *VSnippetList::getSnippet(const QString &p_name) const
{
const_cast<VSnippetList *>(this)->init();
for (auto const & snip : m_snippets) {
if (snip.getName() == p_name) {
return &snip;