vtabwidget.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <QtWidgets>
  2. #include <QtDebug>
  3. #include "vtabwidget.h"
  4. #include "veditor.h"
  5. #include "vnote.h"
  6. VTabWidget::VTabWidget(QWidget *parent)
  7. : QTabWidget(parent)
  8. {
  9. setTabsClosable(true);
  10. connect(tabBar(), &QTabBar::tabCloseRequested,
  11. this, &VTabWidget::handleTabCloseRequest);
  12. openWelcomePage();
  13. }
  14. void VTabWidget::openWelcomePage()
  15. {
  16. int idx = openFileInTab(VNote::welcomePagePath, "", false);
  17. setTabText(idx, "Welcome to VNote");
  18. setTabToolTip(idx, "VNote");
  19. }
  20. int VTabWidget::insertTabWithData(int index, QWidget *page, const QString &label,
  21. const QJsonObject &tabData)
  22. {
  23. int idx = insertTab(index, page, label);
  24. QTabBar *tabs = tabBar();
  25. tabs->setTabData(idx, tabData);
  26. Q_ASSERT(tabs->tabText(idx) == label);
  27. return idx;
  28. }
  29. int VTabWidget::appendTabWithData(QWidget *page, const QString &label, const QJsonObject &tabData)
  30. {
  31. return insertTabWithData(count(), page, label, tabData);
  32. }
  33. void VTabWidget::openFile(QJsonObject fileJson)
  34. {
  35. if (fileJson.isEmpty()) {
  36. return;
  37. }
  38. qDebug() << "open file:" << fileJson;
  39. QString path = fileJson["path"].toString();
  40. QString name = fileJson["name"].toString();
  41. // Find if it has been opened already
  42. int idx = findTabByFile(path, name);
  43. if (idx > -1) {
  44. setCurrentIndex(idx);
  45. return;
  46. }
  47. idx = openFileInTab(path, name, true);
  48. setCurrentIndex(idx);
  49. }
  50. int VTabWidget::openFileInTab(const QString &path, const QString &name, bool modifiable)
  51. {
  52. VEditor *editor = new VEditor(path, name, modifiable);
  53. QJsonObject tabJson;
  54. tabJson["path"] = path;
  55. tabJson["name"] = name;
  56. int idx = appendTabWithData(editor, name, tabJson);
  57. setTabToolTip(idx, path);
  58. return idx;
  59. }
  60. int VTabWidget::findTabByFile(const QString &path, const QString &name)
  61. {
  62. QTabBar *tabs = tabBar();
  63. int nrTabs = tabs->count();
  64. for (int i = 0; i < nrTabs; ++i) {
  65. QJsonObject tabJson = tabs->tabData(i).toJsonObject();
  66. if (tabJson["name"] == name && tabJson["path"] == path) {
  67. return i;
  68. }
  69. }
  70. return -1;
  71. }
  72. void VTabWidget::handleTabCloseRequest(int index)
  73. {
  74. qDebug() << "request closing tab" << index;
  75. VEditor *editor = dynamic_cast<VEditor *>(widget(index));
  76. Q_ASSERT(editor);
  77. bool ok = editor->requestClose();
  78. if (ok) {
  79. removeTab(index);
  80. delete editor;
  81. }
  82. }
  83. void VTabWidget::readFile()
  84. {
  85. VEditor *editor = dynamic_cast<VEditor *>(currentWidget());
  86. Q_ASSERT(editor);
  87. editor->readFile();
  88. }
  89. void VTabWidget::editFile()
  90. {
  91. VEditor *editor = dynamic_cast<VEditor *>(currentWidget());
  92. Q_ASSERT(editor);
  93. editor->editFile();
  94. }
  95. void VTabWidget::saveFile()
  96. {
  97. VEditor *editor = dynamic_cast<VEditor *>(currentWidget());
  98. Q_ASSERT(editor);
  99. editor->saveFile();
  100. }