vmainwindow.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <QtWidgets>
  2. #include "vmainwindow.h"
  3. #include "vdirectorytree.h"
  4. #include "vnote.h"
  5. #include "vfilelist.h"
  6. #include "vtabwidget.h"
  7. #include "vconfigmanager.h"
  8. VMainWindow::VMainWindow(QWidget *parent)
  9. : QMainWindow(parent)
  10. {
  11. VConfigInst;
  12. setupUI();
  13. initActions();
  14. initToolBar();
  15. vnote = new VNote();
  16. vnote->readGlobalConfig();
  17. updateNotebookComboBox();
  18. }
  19. VMainWindow::~VMainWindow()
  20. {
  21. delete vnote;
  22. }
  23. void VMainWindow::setupUI()
  24. {
  25. // Notebook directory browser tree
  26. notebookLabel = new QLabel(tr("Notebook"));
  27. directoryLabel = new QLabel(tr("Directory"));
  28. notebookComboBox = new QComboBox();
  29. notebookComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
  30. directoryTree = new VDirectoryTree();
  31. QVBoxLayout *nbLayout = new QVBoxLayout;
  32. nbLayout->addWidget(notebookLabel);
  33. nbLayout->addWidget(notebookComboBox);
  34. nbLayout->addWidget(directoryLabel);
  35. nbLayout->addWidget(directoryTree);
  36. QWidget *nbContainer = new QWidget();
  37. nbContainer->setLayout(nbLayout);
  38. nbContainer->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  39. // File list widget
  40. fileList = new VFileList();
  41. fileList->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  42. // Editor tab widget
  43. tabs = new VTabWidget();
  44. tabs->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  45. tabs->setTabBarAutoHide(true);
  46. // Main Splitter
  47. mainSplitter = new QSplitter();
  48. mainSplitter->addWidget(nbContainer);
  49. mainSplitter->addWidget(fileList);
  50. mainSplitter->addWidget(tabs);
  51. mainSplitter->setStretchFactor(0, 1);
  52. mainSplitter->setStretchFactor(1, 1);
  53. mainSplitter->setStretchFactor(2, 10);
  54. // Signals
  55. connect(notebookComboBox, SIGNAL(currentIndexChanged(int)), this,
  56. SLOT(setCurNotebookIndex(int)));
  57. connect(this, SIGNAL(curNotebookIndexChanged(const QString&)), directoryTree,
  58. SLOT(setTreePath(const QString&)));
  59. connect(directoryTree, &VDirectoryTree::currentDirectoryChanged,
  60. fileList, &VFileList::setDirectory);
  61. connect(fileList, &VFileList::fileClicked,
  62. tabs, &VTabWidget::openFile);
  63. setCentralWidget(mainSplitter);
  64. // Create and show the status bar
  65. statusBar();
  66. }
  67. void VMainWindow::initActions()
  68. {
  69. editNoteAct = new QAction(tr("&Edit"), this);
  70. editNoteAct->setStatusTip(tr("Edit current note"));
  71. connect(editNoteAct, &QAction::triggered,
  72. tabs, &VTabWidget::editFile);
  73. readNoteAct = new QAction(tr("&Read"), this);
  74. readNoteAct->setStatusTip(tr("Open current note in read mode"));
  75. connect(readNoteAct, &QAction::triggered,
  76. tabs, &VTabWidget::readFile);
  77. saveNoteAct = new QAction(tr("&Save"), this);
  78. saveNoteAct->setStatusTip(tr("Save current note"));
  79. connect(saveNoteAct, &QAction::triggered,
  80. tabs, &VTabWidget::saveFile);
  81. }
  82. void VMainWindow::initToolBar()
  83. {
  84. fileToolBar = addToolBar(tr("Note"));
  85. fileToolBar->setMovable(false);
  86. fileToolBar->addAction(editNoteAct);
  87. fileToolBar->addAction(readNoteAct);
  88. fileToolBar->addAction(saveNoteAct);
  89. }
  90. void VMainWindow::updateNotebookComboBox()
  91. {
  92. const QVector<VNotebook> &notebooks = vnote->getNotebooks();
  93. notebookComboBox->clear();
  94. for (int i = 0; i <notebooks.size(); ++i) {
  95. notebookComboBox->addItem(notebooks[i].getName());
  96. }
  97. qDebug() << "update notebook combobox with" << notebookComboBox->count()
  98. << "items";
  99. notebookComboBox->setCurrentIndex(vnote->getCurNotebookIndex());
  100. }
  101. void VMainWindow::setCurNotebookIndex(int index)
  102. {
  103. Q_ASSERT(index < vnote->getNotebooks().size());
  104. qDebug() << "set current notebook index:" << index;
  105. vnote->setCurNotebookIndex(index);
  106. notebookComboBox->setCurrentIndex(index);
  107. // Update directoryTree
  108. emit curNotebookIndexChanged(vnote->getNotebooks()[index].getPath());
  109. }