vmainwindow.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <QtWidgets>
  2. #include "vmainwindow.h"
  3. #include "vdirectorytree.h"
  4. #include "vnote.h"
  5. #include "vfilelist.h"
  6. #include "vtabwidget.h"
  7. VMainWindow::VMainWindow(QWidget *parent)
  8. : QMainWindow(parent)
  9. {
  10. setupUI();
  11. initActions();
  12. initToolBar();
  13. vnote = new VNote();
  14. vnote->readGlobalConfig();
  15. updateNotebookComboBox();
  16. }
  17. VMainWindow::~VMainWindow()
  18. {
  19. delete vnote;
  20. }
  21. void VMainWindow::setupUI()
  22. {
  23. // Notebook directory browser tree
  24. notebookLabel = new QLabel(tr("Notebook"));
  25. notebookComboBox = new QComboBox();
  26. directoryTree = new VDirectoryTree();
  27. QHBoxLayout *nbTopLayout = new QHBoxLayout;
  28. notebookComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  29. notebookComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
  30. nbTopLayout->setAlignment(Qt::AlignLeft);
  31. nbTopLayout->addWidget(notebookLabel);
  32. nbTopLayout->addWidget(notebookComboBox);
  33. QVBoxLayout *nbLayout = new QVBoxLayout;
  34. nbLayout->addLayout(nbTopLayout);
  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(VNote::welcomePageUrl);
  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::currentFileChanged,
  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. }