vmainwindow.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. notebookComboBox = new QComboBox();
  28. directoryTree = new VDirectoryTree();
  29. QHBoxLayout *nbTopLayout = new QHBoxLayout;
  30. notebookComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  31. notebookComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
  32. nbTopLayout->setAlignment(Qt::AlignLeft);
  33. nbTopLayout->addWidget(notebookLabel);
  34. nbTopLayout->addWidget(notebookComboBox);
  35. QVBoxLayout *nbLayout = new QVBoxLayout;
  36. nbLayout->addLayout(nbTopLayout);
  37. nbLayout->addWidget(directoryTree);
  38. QWidget *nbContainer = new QWidget();
  39. nbContainer->setLayout(nbLayout);
  40. nbContainer->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  41. // File list widget
  42. fileList = new VFileList();
  43. fileList->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  44. // Editor tab widget
  45. tabs = new VTabWidget();
  46. tabs->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  47. tabs->setTabBarAutoHide(true);
  48. // Main Splitter
  49. mainSplitter = new QSplitter();
  50. mainSplitter->addWidget(nbContainer);
  51. mainSplitter->addWidget(fileList);
  52. mainSplitter->addWidget(tabs);
  53. mainSplitter->setStretchFactor(0, 1);
  54. mainSplitter->setStretchFactor(1, 1);
  55. mainSplitter->setStretchFactor(2, 10);
  56. // Signals
  57. connect(notebookComboBox, SIGNAL(currentIndexChanged(int)), this,
  58. SLOT(setCurNotebookIndex(int)));
  59. connect(this, SIGNAL(curNotebookIndexChanged(const QString&)), directoryTree,
  60. SLOT(setTreePath(const QString&)));
  61. connect(directoryTree, &VDirectoryTree::currentDirectoryChanged,
  62. fileList, &VFileList::setDirectory);
  63. connect(fileList, &VFileList::fileClicked,
  64. tabs, &VTabWidget::openFile);
  65. setCentralWidget(mainSplitter);
  66. // Create and show the status bar
  67. statusBar();
  68. }
  69. void VMainWindow::initActions()
  70. {
  71. editNoteAct = new QAction(tr("&Edit"), this);
  72. editNoteAct->setStatusTip(tr("Edit current note"));
  73. connect(editNoteAct, &QAction::triggered,
  74. tabs, &VTabWidget::editFile);
  75. readNoteAct = new QAction(tr("&Read"), this);
  76. readNoteAct->setStatusTip(tr("Open current note in read mode"));
  77. connect(readNoteAct, &QAction::triggered,
  78. tabs, &VTabWidget::readFile);
  79. saveNoteAct = new QAction(tr("&Save"), this);
  80. saveNoteAct->setStatusTip(tr("Save current note"));
  81. connect(saveNoteAct, &QAction::triggered,
  82. tabs, &VTabWidget::saveFile);
  83. }
  84. void VMainWindow::initToolBar()
  85. {
  86. fileToolBar = addToolBar(tr("Note"));
  87. fileToolBar->setMovable(false);
  88. fileToolBar->addAction(editNoteAct);
  89. fileToolBar->addAction(readNoteAct);
  90. fileToolBar->addAction(saveNoteAct);
  91. }
  92. void VMainWindow::updateNotebookComboBox()
  93. {
  94. const QVector<VNotebook> &notebooks = vnote->getNotebooks();
  95. notebookComboBox->clear();
  96. for (int i = 0; i <notebooks.size(); ++i) {
  97. notebookComboBox->addItem(notebooks[i].getName());
  98. }
  99. qDebug() << "update notebook combobox with" << notebookComboBox->count()
  100. << "items";
  101. notebookComboBox->setCurrentIndex(vnote->getCurNotebookIndex());
  102. }
  103. void VMainWindow::setCurNotebookIndex(int index)
  104. {
  105. Q_ASSERT(index < vnote->getNotebooks().size());
  106. qDebug() << "set current notebook index:" << index;
  107. vnote->setCurNotebookIndex(index);
  108. notebookComboBox->setCurrentIndex(index);
  109. // Update directoryTree
  110. emit curNotebookIndexChanged(vnote->getNotebooks()[index].getPath());
  111. }