|
|
@@ -1,6 +1,9 @@
|
|
|
#include "vnotebookselector.h"
|
|
|
#include <QDebug>
|
|
|
#include <QJsonObject>
|
|
|
+#include <QListWidget>
|
|
|
+#include <QAction>
|
|
|
+#include <QMenu>
|
|
|
#include "vnotebook.h"
|
|
|
#include "vconfigmanager.h"
|
|
|
#include "dialog/vnewnotebookdialog.h"
|
|
|
@@ -10,15 +13,48 @@
|
|
|
#include "utils/vutils.h"
|
|
|
#include "vnote.h"
|
|
|
#include "veditarea.h"
|
|
|
+#include "vnofocusitemdelegate.h"
|
|
|
|
|
|
extern VConfigManager vconfig;
|
|
|
|
|
|
+const int VNotebookSelector::c_notebookStartIdx = 1;
|
|
|
+
|
|
|
VNotebookSelector::VNotebookSelector(VNote *vnote, QWidget *p_parent)
|
|
|
: QComboBox(p_parent), m_vnote(vnote), m_notebooks(m_vnote->getNotebooks()),
|
|
|
- m_editArea(NULL)
|
|
|
+ m_editArea(NULL), m_lastValidIndex(-1)
|
|
|
{
|
|
|
+ m_listWidget = new QListWidget(this);
|
|
|
+ m_listWidget->setItemDelegate(new VNoFocusItemDelegate(this));
|
|
|
+ m_listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
+ connect(m_listWidget, &QListWidget::customContextMenuRequested,
|
|
|
+ this, &VNotebookSelector::requestPopupListContextMenu);
|
|
|
+
|
|
|
+ setModel(m_listWidget->model());
|
|
|
+ setView(m_listWidget);
|
|
|
+
|
|
|
+ m_listWidget->viewport()->installEventFilter(this);
|
|
|
+
|
|
|
+ initActions();
|
|
|
+
|
|
|
connect(this, SIGNAL(currentIndexChanged(int)),
|
|
|
this, SLOT(handleCurIndexChanged(int)));
|
|
|
+ connect(this, SIGNAL(activated(int)),
|
|
|
+ this, SLOT(handleItemActivated(int)));
|
|
|
+}
|
|
|
+
|
|
|
+void VNotebookSelector::initActions()
|
|
|
+{
|
|
|
+ m_deleteNotebookAct = new QAction(QIcon(":/resources/icons/delete_notebook.svg"),
|
|
|
+ tr("&Delete"), this);
|
|
|
+ m_deleteNotebookAct->setStatusTip(tr("Delete current notebook"));
|
|
|
+ connect(m_deleteNotebookAct, SIGNAL(triggered(bool)),
|
|
|
+ this, SLOT(deleteNotebook()));
|
|
|
+
|
|
|
+ m_notebookInfoAct = new QAction(QIcon(":/resources/icons/notebook_info.svg"),
|
|
|
+ tr("&Info"));
|
|
|
+ m_notebookInfoAct->setStatusTip(tr("View and edit current notebook's information"));
|
|
|
+ connect(m_notebookInfoAct, SIGNAL(triggered(bool)),
|
|
|
+ this, SLOT(editNotebookInfo()));
|
|
|
}
|
|
|
|
|
|
void VNotebookSelector::updateComboBox()
|
|
|
@@ -28,42 +64,99 @@ void VNotebookSelector::updateComboBox()
|
|
|
disconnect(this, SIGNAL(currentIndexChanged(int)),
|
|
|
this, SLOT(handleCurIndexChanged(int)));
|
|
|
clear();
|
|
|
+ m_listWidget->clear();
|
|
|
+
|
|
|
+ insertAddNotebookItem();
|
|
|
+
|
|
|
for (int i = 0; i < m_notebooks.size(); ++i) {
|
|
|
- addItem(QIcon(":/resources/icons/notebook_item.svg"), m_notebooks[i]->getName());
|
|
|
+ addNotebookItem(m_notebooks[i]->getName());
|
|
|
}
|
|
|
setCurrentIndex(-1);
|
|
|
connect(this, SIGNAL(currentIndexChanged(int)),
|
|
|
this, SLOT(handleCurIndexChanged(int)));
|
|
|
|
|
|
- if (m_notebooks.isEmpty() && index != -1) {
|
|
|
- index = -1;
|
|
|
- vconfig.setCurNotebookIndex(index);
|
|
|
+ if (m_notebooks.isEmpty()) {
|
|
|
+ vconfig.setCurNotebookIndex(-1);
|
|
|
+ setCurrentIndex(0);
|
|
|
+ } else {
|
|
|
+ setCurrentIndexNotebook(index);
|
|
|
}
|
|
|
- setCurrentIndex(index);
|
|
|
qDebug() << "notebooks" << m_notebooks.size() << "current index" << index;
|
|
|
}
|
|
|
|
|
|
+void VNotebookSelector::setCurrentIndexNotebook(int p_index)
|
|
|
+{
|
|
|
+ if (p_index > -1) {
|
|
|
+ p_index += c_notebookStartIdx;
|
|
|
+ }
|
|
|
+ setCurrentIndex(p_index);
|
|
|
+}
|
|
|
+
|
|
|
+void VNotebookSelector::insertAddNotebookItem()
|
|
|
+{
|
|
|
+ QListWidgetItem *item = new QListWidgetItem();
|
|
|
+ item->setIcon(QIcon(":/resources/icons/create_notebook.svg"));
|
|
|
+ item->setText("Add Notebook");
|
|
|
+ QFont font;
|
|
|
+ font.setItalic(true);
|
|
|
+ item->setData(Qt::FontRole, font);
|
|
|
+ item->setToolTip(tr("Create or import a notebook."));
|
|
|
+ m_listWidget->insertItem(0, item);
|
|
|
+}
|
|
|
+
|
|
|
void VNotebookSelector::handleCurIndexChanged(int p_index)
|
|
|
{
|
|
|
- qDebug() << "current index changed" << p_index;
|
|
|
+ qDebug() << "current index changed" << p_index << "startIdx" << c_notebookStartIdx;
|
|
|
VNotebook *nb = NULL;
|
|
|
if (p_index > -1) {
|
|
|
- nb = m_notebooks[p_index];
|
|
|
+ if (p_index < c_notebookStartIdx) {
|
|
|
+ // Click a special action item.
|
|
|
+ if (m_listWidget->count() == c_notebookStartIdx) {
|
|
|
+ // There is no regular notebook item. Just let it be selected.
|
|
|
+ p_index = -1;
|
|
|
+ } else {
|
|
|
+ // handleItemActivated() will handle the logics.
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ int nbIdx = p_index - c_notebookStartIdx;
|
|
|
+ Q_ASSERT(nbIdx >= 0);
|
|
|
+ nb = m_notebooks[nbIdx];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ m_lastValidIndex = p_index;
|
|
|
+ QString tooltip;
|
|
|
+ if (p_index > -1) {
|
|
|
+ p_index -= c_notebookStartIdx;
|
|
|
+ tooltip = nb->getName();
|
|
|
}
|
|
|
+ setToolTip(tooltip);
|
|
|
vconfig.setCurNotebookIndex(p_index);
|
|
|
emit curNotebookChanged(nb);
|
|
|
}
|
|
|
|
|
|
+void VNotebookSelector::handleItemActivated(int p_index)
|
|
|
+{
|
|
|
+ if (p_index > -1 && p_index < c_notebookStartIdx) {
|
|
|
+ // Click a special action item
|
|
|
+ if (m_lastValidIndex > -1) {
|
|
|
+ setCurrentIndex(m_lastValidIndex);
|
|
|
+ }
|
|
|
+ newNotebook();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
void VNotebookSelector::update()
|
|
|
{
|
|
|
updateComboBox();
|
|
|
}
|
|
|
|
|
|
-void VNotebookSelector::newNotebook()
|
|
|
+bool VNotebookSelector::newNotebook()
|
|
|
{
|
|
|
QString info;
|
|
|
QString defaultName("new_notebook");
|
|
|
QString defaultPath;
|
|
|
+
|
|
|
do {
|
|
|
VNewNotebookDialog dialog(tr("Create notebook"), info, defaultName,
|
|
|
defaultPath, this);
|
|
|
@@ -77,9 +170,11 @@ void VNotebookSelector::newNotebook()
|
|
|
continue;
|
|
|
}
|
|
|
createNotebook(name, path);
|
|
|
+ return true;
|
|
|
}
|
|
|
break;
|
|
|
} while (true);
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
VNotebook *VNotebookSelector::findNotebook(const QString &p_name)
|
|
|
@@ -98,14 +193,22 @@ void VNotebookSelector::createNotebook(const QString &p_name, const QString &p_p
|
|
|
m_notebooks.append(nb);
|
|
|
vconfig.setNotebooks(m_notebooks);
|
|
|
|
|
|
- addItem(QIcon(":/resources/icons/notebook_item.svg"), nb->getName());
|
|
|
- setCurrentIndex(m_notebooks.size() - 1);
|
|
|
+ addNotebookItem(nb->getName());
|
|
|
+ setCurrentIndexNotebook(m_notebooks.size() - 1);
|
|
|
}
|
|
|
|
|
|
void VNotebookSelector::deleteNotebook()
|
|
|
{
|
|
|
- int curIndex = currentIndex();
|
|
|
- VNotebook *notebook = m_notebooks[curIndex];
|
|
|
+ QList<QListWidgetItem *> items = m_listWidget->selectedItems();
|
|
|
+ if (items.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Q_ASSERT(items.size() == 1);
|
|
|
+ QListWidgetItem *item = items[0];
|
|
|
+ int index = indexOfListItem(item);
|
|
|
+
|
|
|
+ VNotebook *notebook = getNotebookFromComboIndex(index);
|
|
|
+ Q_ASSERT(notebook);
|
|
|
QString curName = notebook->getName();
|
|
|
QString curPath = notebook->getPath();
|
|
|
|
|
|
@@ -126,7 +229,7 @@ void VNotebookSelector::deleteNotebook(VNotebook *p_notebook)
|
|
|
m_notebooks.remove(idx);
|
|
|
vconfig.setNotebooks(m_notebooks);
|
|
|
|
|
|
- removeItem(idx);
|
|
|
+ removeNotebookItem(idx);
|
|
|
|
|
|
VNotebook::deleteNotebook(p_notebook);
|
|
|
}
|
|
|
@@ -141,11 +244,18 @@ int VNotebookSelector::indexOfNotebook(const VNotebook *p_notebook)
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
void VNotebookSelector::editNotebookInfo()
|
|
|
{
|
|
|
- int curIndex = currentIndex();
|
|
|
- Q_ASSERT(curIndex > -1);
|
|
|
- VNotebook *notebook = m_notebooks[curIndex];
|
|
|
+ QList<QListWidgetItem *> items = m_listWidget->selectedItems();
|
|
|
+ if (items.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Q_ASSERT(items.size() == 1);
|
|
|
+ QListWidgetItem *item = items[0];
|
|
|
+ int index = indexOfListItem(item);
|
|
|
+
|
|
|
+ VNotebook *notebook = getNotebookFromComboIndex(index);
|
|
|
QString info;
|
|
|
QString curName = notebook->getName();
|
|
|
QString defaultPath = notebook->getPath();
|
|
|
@@ -164,10 +274,72 @@ void VNotebookSelector::editNotebookInfo()
|
|
|
continue;
|
|
|
}
|
|
|
notebook->rename(name);
|
|
|
- setItemText(curIndex, name);
|
|
|
+ updateComboBoxItem(index, name);
|
|
|
vconfig.setNotebooks(m_notebooks);
|
|
|
emit notebookUpdated(notebook);
|
|
|
}
|
|
|
break;
|
|
|
} while (true);
|
|
|
}
|
|
|
+
|
|
|
+void VNotebookSelector::addNotebookItem(const QString &p_name)
|
|
|
+{
|
|
|
+ QListWidgetItem *item = new QListWidgetItem(m_listWidget);
|
|
|
+ item->setText(p_name);
|
|
|
+ item->setToolTip(p_name);
|
|
|
+ item->setIcon(QIcon(":/resources/icons/notebook_item.svg"));
|
|
|
+}
|
|
|
+
|
|
|
+void VNotebookSelector::removeNotebookItem(int p_index)
|
|
|
+{
|
|
|
+ QListWidgetItem *item = m_listWidget->item(p_index + c_notebookStartIdx);
|
|
|
+ m_listWidget->removeItemWidget(item);
|
|
|
+ delete item;
|
|
|
+}
|
|
|
+
|
|
|
+void VNotebookSelector::updateComboBoxItem(int p_index, const QString &p_name)
|
|
|
+{
|
|
|
+ QListWidgetItem *item = m_listWidget->item(p_index);
|
|
|
+ item->setText(p_name);
|
|
|
+ item->setToolTip(p_name);
|
|
|
+}
|
|
|
+
|
|
|
+void VNotebookSelector::requestPopupListContextMenu(QPoint p_pos)
|
|
|
+{
|
|
|
+ QModelIndex index = m_listWidget->indexAt(p_pos);
|
|
|
+ if (!index.isValid() || index.row() < c_notebookStartIdx) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ QListWidgetItem *item = m_listWidget->itemAt(p_pos);
|
|
|
+ Q_ASSERT(item);
|
|
|
+ m_listWidget->clearSelection();
|
|
|
+ item->setSelected(true);
|
|
|
+
|
|
|
+ QMenu menu(this);
|
|
|
+ menu.addAction(m_deleteNotebookAct);
|
|
|
+ menu.addAction(m_notebookInfoAct);
|
|
|
+
|
|
|
+ menu.exec(m_listWidget->mapToGlobal(p_pos));
|
|
|
+}
|
|
|
+
|
|
|
+bool VNotebookSelector::eventFilter(QObject *watched, QEvent *event)
|
|
|
+{
|
|
|
+ if (event->type() == QEvent::MouseButtonRelease) {
|
|
|
+ if (static_cast<QMouseEvent *>(event)->button() == Qt::RightButton) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return QComboBox::eventFilter(watched, event);
|
|
|
+}
|
|
|
+
|
|
|
+int VNotebookSelector::indexOfListItem(const QListWidgetItem *p_item)
|
|
|
+{
|
|
|
+ int nrItems = m_listWidget->count();
|
|
|
+ for (int i = 0; i < nrItems; ++i) {
|
|
|
+ if (m_listWidget->item(i) == p_item) {
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+}
|