vtoolbox.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "vtoolbox.h"
  2. #include <QPushButton>
  3. #include <QStackedLayout>
  4. #include <QHBoxLayout>
  5. #include <QVBoxLayout>
  6. #include <QVariant>
  7. #include <QLabel>
  8. #include "vnote.h"
  9. #include "utils/vutils.h"
  10. #include "utils/viconutils.h"
  11. extern VNote *g_vnote;
  12. VToolBox::VToolBox(QWidget *p_parent)
  13. : QWidget(p_parent),
  14. m_currentIndex(-1)
  15. {
  16. setupUI();
  17. }
  18. void VToolBox::setupUI()
  19. {
  20. m_btnLayout = new QHBoxLayout();
  21. m_btnLayout->addStretch();
  22. m_btnLayout->setContentsMargins(0, 0, 0, 2);
  23. m_btnLayout->setSpacing(0);
  24. QWidget *wid = new QWidget();
  25. wid->setProperty("ToolBoxTitle", true);
  26. wid->setLayout(m_btnLayout);
  27. m_widgetLayout = new QStackedLayout();
  28. QVBoxLayout *mainLayout = new QVBoxLayout();
  29. mainLayout->addWidget(wid);
  30. mainLayout->addLayout(m_widgetLayout);
  31. mainLayout->setContentsMargins(0, 0, 0, 0);
  32. setLayout(mainLayout);
  33. }
  34. int VToolBox::addItem(QWidget *p_widget, const QString &p_iconFile, const QString &p_text)
  35. {
  36. int idx = m_items.size();
  37. // New a button.
  38. QIcon icon = VIconUtils::toolBoxIcon(p_iconFile);
  39. QPushButton *btn = new QPushButton(icon, "");
  40. btn->setToolTip(p_text);
  41. btn->setProperty("FlatBtn", true);
  42. connect(btn, &QPushButton::clicked,
  43. this, [this]() {
  44. QObject *btn = sender();
  45. for (int i = 0; i < m_items.size(); ++i) {
  46. if (m_items[i].m_btn == btn) {
  47. setCurrentIndex(i);
  48. break;
  49. }
  50. }
  51. });
  52. m_btnLayout->insertWidget(idx, btn);
  53. // Insert widget to layout.
  54. m_widgetLayout->insertWidget(idx, p_widget);
  55. m_items.push_back(ItemInfo(p_widget,
  56. p_text,
  57. btn,
  58. icon,
  59. VIconUtils::toolBoxActiveIcon(p_iconFile)));
  60. if (m_items.size() == 1) {
  61. setCurrentIndex(0);
  62. }
  63. return idx;
  64. }
  65. void VToolBox::setCurrentIndex(int p_idx)
  66. {
  67. if (p_idx < 0 || p_idx >= m_items.size()) {
  68. m_currentIndex = -1;
  69. } else {
  70. m_currentIndex = p_idx;
  71. }
  72. setCurrentButtonIndex(m_currentIndex);
  73. m_widgetLayout->setCurrentIndex(m_currentIndex);
  74. QWidget *widget = m_widgetLayout->widget(m_currentIndex);
  75. if (widget) {
  76. widget->setFocus();
  77. }
  78. }
  79. void VToolBox::setCurrentWidget(QWidget *p_widget)
  80. {
  81. int idx = -1;
  82. for (int i = 0; i < m_items.size(); ++i) {
  83. if (m_items[i].m_widget == p_widget) {
  84. idx = i;
  85. break;
  86. }
  87. }
  88. setCurrentIndex(idx);
  89. }
  90. void VToolBox::setCurrentButtonIndex(int p_idx)
  91. {
  92. // Remove the text of all button.
  93. for (int i = 0; i < m_items.size(); ++i) {
  94. QPushButton *btn = m_items[i].m_btn;
  95. btn->setText("");
  96. btn->setIcon(m_items[i].m_icon);
  97. btn->clearFocus();
  98. VUtils::setDynamicProperty(btn, "ToolBoxActiveBtn", false);
  99. }
  100. if (p_idx < 0 || p_idx >= m_items.size()) {
  101. return;
  102. }
  103. QPushButton *curBtn = m_items[p_idx].m_btn;
  104. curBtn->setText(m_items[p_idx].m_text);
  105. curBtn->setIcon(m_items[p_idx].m_activeIcon);
  106. VUtils::setDynamicProperty(curBtn, "ToolBoxActiveBtn", true);
  107. }
  108. void VToolBox::showNavigation()
  109. {
  110. clearNavigation();
  111. if (!isVisible()) {
  112. return;
  113. }
  114. for (int i = 0; i < 26 && i < m_items.size(); ++i) {
  115. const ItemInfo &item = m_items[i];
  116. QChar key('a' + i);
  117. m_keyMap[key] = item.m_widget;
  118. QString str = QString(m_majorKey) + key;
  119. QLabel *label = new QLabel(str, this);
  120. label->setStyleSheet(g_vnote->getNavigationLabelStyle(str));
  121. label->show();
  122. QRect rect = item.m_btn->geometry();
  123. // Display the label at the end to show the file name.
  124. label->move(rect.x(), rect.y() + rect.height() / 2);
  125. m_naviLabels.append(label);
  126. }
  127. }
  128. bool VToolBox::handleKeyNavigation(int p_key, bool &p_succeed)
  129. {
  130. static bool secondKey = false;
  131. bool ret = false;
  132. p_succeed = false;
  133. QChar keyChar = VUtils::keyToChar(p_key);
  134. if (secondKey && !keyChar.isNull()) {
  135. secondKey = false;
  136. p_succeed = true;
  137. ret = true;
  138. auto it = m_keyMap.find(keyChar);
  139. if (it != m_keyMap.end()) {
  140. QWidget *widget = static_cast<QWidget *>(it.value());
  141. setCurrentWidget(widget);
  142. }
  143. } else if (keyChar == m_majorKey) {
  144. // Major key pressed.
  145. // Need second key if m_keyMap is not empty.
  146. if (m_keyMap.isEmpty()) {
  147. p_succeed = true;
  148. } else {
  149. secondKey = true;
  150. }
  151. ret = true;
  152. }
  153. return ret;
  154. }