VToolBox: support Navigation mode

This commit is contained in:
Le Tan 2017-11-12 13:59:23 +08:00
parent f032dede34
commit 9b730c2889
5 changed files with 102 additions and 5 deletions

View File

@ -112,6 +112,7 @@ void VMainWindow::registerCaptainAndNavigationTargets()
m_captain->registerNavigationTarget(directoryTree);
m_captain->registerNavigationTarget(m_fileList);
m_captain->registerNavigationTarget(editArea);
m_captain->registerNavigationTarget(m_toolBox);
m_captain->registerNavigationTarget(outline);
m_captain->registerNavigationTarget(m_snippetList);

View File

@ -604,3 +604,14 @@ bool VSnippetList::deleteSnippetFile(const VSnippet &p_snippet, QString *p_errMs
return true;
}
void VSnippetList::focusInEvent(QFocusEvent *p_event)
{
QWidget::focusInEvent(p_event);
if (m_snippets.isEmpty()) {
m_addBtn->setFocus();
} else {
m_snippetList->setFocus();
}
}

View File

@ -14,6 +14,7 @@ class QListWidgetItem;
class QLabel;
class QAction;
class QKeyEvent;
class QFocusEvent;
class VSnippetList : public QWidget, public VNavigationMode
@ -29,6 +30,8 @@ public:
protected:
void keyPressEvent(QKeyEvent *p_event) Q_DECL_OVERRIDE;
void focusInEvent(QFocusEvent *p_event) Q_DECL_OVERRIDE;
private slots:
void newSnippet();

View File

@ -5,7 +5,12 @@
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QVariant>
#include <QLabel>
#include "vnote.h"
#include "utils/vutils.h"
extern VNote *g_vnote;
VToolBox::VToolBox(QWidget *p_parent)
: QWidget(p_parent),
@ -64,10 +69,6 @@ int VToolBox::addItem(QWidget *p_widget, const QIcon &p_iconSet, const QString &
void VToolBox::setCurrentIndex(int p_idx)
{
if (m_currentIndex == p_idx) {
return;
}
if (p_idx < 0 || p_idx >= m_items.size()) {
m_currentIndex = -1;
} else {
@ -77,6 +78,24 @@ void VToolBox::setCurrentIndex(int p_idx)
setCurrentButtonIndex(m_currentIndex);
m_widgetLayout->setCurrentIndex(m_currentIndex);
QWidget *widget = m_widgetLayout->widget(m_currentIndex);
if (widget) {
widget->setFocus();
}
}
void VToolBox::setCurrentWidget(QWidget *p_widget)
{
int idx = -1;
for (int i = 0; i < m_items.size(); ++i) {
if (m_items[i].m_widget == p_widget) {
idx = i;
break;
}
}
setCurrentIndex(idx);
}
void VToolBox::setCurrentButtonIndex(int p_idx)
@ -94,3 +113,58 @@ void VToolBox::setCurrentButtonIndex(int p_idx)
m_items[p_idx].m_btn->setText(m_items[p_idx].m_text);
}
void VToolBox::showNavigation()
{
clearNavigation();
if (!isVisible()) {
return;
}
for (int i = 0; i < 26 && i < m_items.size(); ++i) {
const ItemInfo &item = m_items[i];
QChar key('a' + i);
m_keyMap[key] = item.m_widget;
QString str = QString(m_majorKey) + key;
QLabel *label = new QLabel(str, this);
label->setStyleSheet(g_vnote->getNavigationLabelStyle(str));
label->show();
QRect rect = item.m_btn->geometry();
// Display the label at the end to show the file name.
label->move(rect.x(), rect.y() + rect.height() / 2);
m_naviLabels.append(label);
}
}
bool VToolBox::handleKeyNavigation(int p_key, bool &p_succeed)
{
static bool secondKey = false;
bool ret = false;
p_succeed = false;
QChar keyChar = VUtils::keyToChar(p_key);
if (secondKey && !keyChar.isNull()) {
secondKey = false;
p_succeed = true;
ret = true;
auto it = m_keyMap.find(keyChar);
if (it != m_keyMap.end()) {
QWidget *widget = static_cast<QWidget *>(it.value());
setCurrentWidget(widget);
}
} else if (keyChar == m_majorKey) {
// Major key pressed.
// Need second key if m_keyMap is not empty.
if (m_keyMap.isEmpty()) {
p_succeed = true;
} else {
secondKey = true;
}
ret = true;
}
return ret;
}

View File

@ -6,11 +6,13 @@
#include <QString>
#include <QVector>
#include "vnavigationmode.h"
class QPushButton;
class QStackedLayout;
class QBoxLayout;
class VToolBox : public QWidget
class VToolBox : public QWidget, public VNavigationMode
{
Q_OBJECT
public:
@ -20,6 +22,12 @@ public:
void setCurrentIndex(int p_idx);
void setCurrentWidget(QWidget *p_widget);
// Implementations for VNavigationMode.
void showNavigation() Q_DECL_OVERRIDE;
bool handleKeyNavigation(int p_key, bool &p_succeed) Q_DECL_OVERRIDE;
private:
struct ItemInfo
{