outlineviewer.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include "outlineviewer.h"
  2. #include <QTimer>
  3. #include <QHBoxLayout>
  4. #include <QVBoxLayout>
  5. #include <QShowEvent>
  6. #include <QTreeWidgetItem>
  7. #include <QToolButton>
  8. #include <QToolTip>
  9. #include <QDebug>
  10. #include "treewidget.h"
  11. #include "titlebar.h"
  12. #include "configmgr.h"
  13. #include "widgetconfig.h"
  14. #include "navigationmodemgr.h"
  15. using namespace vnotex;
  16. // Use static expansion when the level is 6.
  17. #define STATIC_EXPANDED_LEVEL 6
  18. OutlineViewer::OutlineViewer(const QString &p_title, QWidget *p_parent)
  19. : QFrame(p_parent)
  20. {
  21. setupUI(p_title);
  22. m_autoExpandedLevel = ConfigMgr::getInst().getWidgetConfig().getOutlineAutoExpandedLevel();
  23. m_expandTimer = new QTimer(this);
  24. m_expandTimer->setSingleShot(true);
  25. m_expandTimer->setInterval(1000);
  26. connect(m_expandTimer, &QTimer::timeout,
  27. this, [this]() {
  28. // Auto adjust items after current heading change.
  29. if (m_autoExpandedLevel == STATIC_EXPANDED_LEVEL) {
  30. return;
  31. }
  32. expandTree(m_autoExpandedLevel);
  33. auto curItem = m_tree->currentItem();
  34. if (curItem) {
  35. m_tree->scrollToItem(curItem);
  36. }
  37. });
  38. }
  39. void OutlineViewer::setupUI(const QString &p_title)
  40. {
  41. auto mainLayout = new QVBoxLayout(this);
  42. mainLayout->setContentsMargins(0, 0, 0, 0);
  43. {
  44. auto titleBar = setupTitleBar(p_title, this);
  45. mainLayout->addWidget(titleBar);
  46. }
  47. m_tree = new TreeWidget(TreeWidget::Flag::None, this);
  48. TreeWidget::setupSingleColumnHeaderlessTree(m_tree, false, false);
  49. m_tree->setSelectionMode(QAbstractItemView::SingleSelection);
  50. TreeWidget::showHorizontalScrollbar(m_tree);
  51. mainLayout->addWidget(m_tree);
  52. connect(m_tree, &QTreeWidget::currentItemChanged,
  53. this, [this](QTreeWidgetItem *p_cur, QTreeWidgetItem *p_pre) {
  54. Q_UNUSED(p_pre);
  55. activateItem(p_cur);
  56. });
  57. connect(m_tree, &QTreeWidget::itemClicked,
  58. this, [this](QTreeWidgetItem *p_item, int p_col) {
  59. Q_UNUSED(p_col);
  60. // Will duplicate the signal. That's fine.
  61. activateItem(p_item, true);
  62. });
  63. setFocusProxy(m_tree);
  64. }
  65. NavigationModeWrapper<QTreeWidget, QTreeWidgetItem> *OutlineViewer::getNavigationModeWrapper()
  66. {
  67. if (!m_navigationWrapper) {
  68. m_navigationWrapper.reset(new NavigationModeWrapper<QTreeWidget, QTreeWidgetItem>(m_tree));
  69. }
  70. return m_navigationWrapper.data();
  71. }
  72. TitleBar *OutlineViewer::setupTitleBar(const QString &p_title, QWidget *p_parent)
  73. {
  74. auto titleBar = new TitleBar(p_title, TitleBar::Action::None, p_parent);
  75. auto decreaseBtn = titleBar->addActionButton(QStringLiteral("decrease_outline_level.svg"), tr("Decrease Expansion Level"));
  76. connect(decreaseBtn, &QToolButton::clicked,
  77. this, [this]() {
  78. auto &config = ConfigMgr::getInst().getWidgetConfig();
  79. m_autoExpandedLevel = config.getOutlineAutoExpandedLevel() - 1;
  80. if (m_autoExpandedLevel < 1) {
  81. m_autoExpandedLevel = 1;
  82. } else {
  83. config.setOutlineAutoExpandedLevel(m_autoExpandedLevel);
  84. expandTree(m_autoExpandedLevel);
  85. }
  86. showLevel();
  87. });
  88. auto increaseBtn = titleBar->addActionButton(QStringLiteral("increase_outline_level.svg"), tr("Increase Expansion Level"));
  89. connect(increaseBtn, &QToolButton::clicked,
  90. this, [this]() {
  91. auto &config = ConfigMgr::getInst().getWidgetConfig();
  92. m_autoExpandedLevel = config.getOutlineAutoExpandedLevel() + 1;
  93. if (m_autoExpandedLevel > 6) {
  94. m_autoExpandedLevel = 6;
  95. } else {
  96. config.setOutlineAutoExpandedLevel(m_autoExpandedLevel);
  97. expandTree(m_autoExpandedLevel);
  98. }
  99. showLevel();
  100. });
  101. return titleBar;
  102. }
  103. void OutlineViewer::setOutlineProvider(const QSharedPointer<OutlineProvider> &p_provider)
  104. {
  105. if (m_provider == p_provider) {
  106. return;
  107. }
  108. if (m_provider) {
  109. disconnect(m_provider.data(), 0, this, 0);
  110. disconnect(this, 0, m_provider.data(), 0);
  111. }
  112. m_provider = p_provider;
  113. if (m_provider) {
  114. connect(m_provider.data(), &OutlineProvider::outlineChanged,
  115. this, [this]() {
  116. if (isVisible()) {
  117. updateOutline(m_provider->getOutline());
  118. }
  119. });
  120. connect(m_provider.data(), &OutlineProvider::currentHeadingChanged,
  121. this, [this]() {
  122. updateCurrentHeading(m_provider->getCurrentHeadingIndex());
  123. });
  124. if (isVisible()) {
  125. updateOutline(m_provider->getOutline());
  126. updateCurrentHeading(m_provider->getCurrentHeadingIndex());
  127. }
  128. } else {
  129. updateOutline(nullptr);
  130. updateCurrentHeading(-1);
  131. }
  132. }
  133. void OutlineViewer::showEvent(QShowEvent *p_event)
  134. {
  135. QFrame::showEvent(p_event);
  136. // Update the tree.
  137. if (m_provider) {
  138. updateOutline(m_provider->getOutline());
  139. updateCurrentHeading(m_provider->getCurrentHeadingIndex());
  140. } else {
  141. updateOutline(nullptr);
  142. }
  143. }
  144. void OutlineViewer::updateOutline(const QSharedPointer<Outline> &p_outline)
  145. {
  146. if (!p_outline) {
  147. if (m_outline.isEmpty()) {
  148. return;
  149. }
  150. m_outline.clear();
  151. } else {
  152. if (m_outline == *p_outline) {
  153. return;
  154. }
  155. m_outline = *p_outline;
  156. }
  157. m_muted = true;
  158. updateTreeToOutline(m_tree, m_outline);
  159. expandTree(m_autoExpandedLevel);
  160. m_muted = false;
  161. }
  162. void OutlineViewer::updateCurrentHeading(int p_idx)
  163. {
  164. if (m_currentHeadingIndex == p_idx) {
  165. return;
  166. }
  167. m_currentHeadingIndex = p_idx;
  168. if (m_currentHeadingIndex >= m_outline.m_headings.size()) {
  169. m_currentHeadingIndex = -1;
  170. }
  171. m_muted = true;
  172. highlightHeading(m_currentHeadingIndex);
  173. m_muted = false;
  174. m_expandTimer->start();
  175. }
  176. void OutlineViewer::updateTreeToOutline(QTreeWidget *p_tree, const Outline &p_outline)
  177. {
  178. p_tree->clear();
  179. if (p_outline.isEmpty()) {
  180. p_tree->update();
  181. return;
  182. }
  183. int idx = 0;
  184. renderTreeAtLevel(p_outline.m_headings, idx, 1, p_tree, nullptr, nullptr);
  185. }
  186. void OutlineViewer::renderTreeAtLevel(const QVector<Outline::Heading> &p_headings,
  187. int &p_index,
  188. int p_level,
  189. QTreeWidget *p_tree,
  190. QTreeWidgetItem *p_parentItem,
  191. QTreeWidgetItem *p_lastItem)
  192. {
  193. while (p_index < p_headings.size()) {
  194. const auto &heading = p_headings[p_index];
  195. QTreeWidgetItem *item = nullptr;
  196. if (heading.m_level == p_level) {
  197. if (p_parentItem) {
  198. item = new QTreeWidgetItem(p_parentItem);
  199. } else {
  200. item = new QTreeWidgetItem(p_tree);
  201. }
  202. fillTreeItem(item, heading, p_index);
  203. p_lastItem = item;
  204. ++p_index;
  205. } else if (heading.m_level < p_level) {
  206. return;
  207. } else {
  208. renderTreeAtLevel(p_headings, p_index, p_level + 1, p_tree, p_lastItem, nullptr);
  209. }
  210. }
  211. }
  212. void OutlineViewer::fillTreeItem(QTreeWidgetItem *p_item, const Outline::Heading &p_heading, int p_index)
  213. {
  214. p_item->setData(Column::Name, Qt::UserRole, p_index);
  215. p_item->setText(Column::Name, p_heading.m_name);
  216. p_item->setToolTip(Column::Name, p_heading.m_name);
  217. }
  218. void OutlineViewer::highlightHeading(int p_idx)
  219. {
  220. if (p_idx == -1) {
  221. m_tree->setCurrentItem(nullptr);
  222. return;
  223. }
  224. auto item = TreeWidget::findItem(m_tree, p_idx);
  225. m_tree->setCurrentItem(item);
  226. }
  227. void OutlineViewer::expandTree(int p_level)
  228. {
  229. int cnt = m_tree->topLevelItemCount();
  230. if (cnt == 0) {
  231. return;
  232. }
  233. // Get the base level from the first heading.
  234. int baseLevel = m_outline.m_headings[0].m_level;
  235. int delta = p_level - baseLevel;
  236. if (delta <= 0) {
  237. m_tree->collapseAll();
  238. } else {
  239. m_tree->expandToDepth(delta - 1);
  240. }
  241. }
  242. void OutlineViewer::showLevel()
  243. {
  244. QToolTip::showText(mapToGlobal(QPoint(0, 0)), tr("Expansion level: %1").arg(m_autoExpandedLevel), this);
  245. }
  246. void OutlineViewer::activateItem(QTreeWidgetItem *p_item, bool p_focus)
  247. {
  248. if (!p_item || m_tree->selectedItems().isEmpty()) {
  249. return;
  250. }
  251. m_currentHeadingIndex = p_item->data(Column::Name, Qt::UserRole).toInt();
  252. if (m_currentHeadingIndex != -1 && !m_muted) {
  253. emit m_provider->headingClicked(m_currentHeadingIndex);
  254. if (p_focus) {
  255. emit focusViewArea();
  256. }
  257. }
  258. }