vmainwindow.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #include "dialog/vnewnotebookdialog.h"
  9. extern VConfigManager vconfig;
  10. VMainWindow::VMainWindow(QWidget *parent)
  11. : QMainWindow(parent)
  12. {
  13. // Must be called before those who uses VConfigManager
  14. vnote = new VNote();
  15. setupUI();
  16. initActions();
  17. initToolBar();
  18. initMenuBar();
  19. updateNotebookComboBox(vnote->getNotebooks());
  20. }
  21. VMainWindow::~VMainWindow()
  22. {
  23. delete vnote;
  24. }
  25. void VMainWindow::setupUI()
  26. {
  27. // Notebook directory browser tree
  28. notebookLabel = new QLabel(tr("Notebook"));
  29. directoryLabel = new QLabel(tr("Directory"));
  30. newNotebookBtn = new QPushButton(QIcon(":/resources/icons/create_notebook.png"), "");
  31. newNotebookBtn->setToolTip(tr("Create a new notebook"));
  32. deleteNotebookBtn = new QPushButton(QIcon(":/resources/icons/delete_notebook.png"), "");
  33. deleteNotebookBtn->setToolTip(tr("Delete current notebook"));
  34. notebookInfoBtn = new QPushButton(QIcon(":/resources/icons/notebook_info.png"), "");
  35. notebookInfoBtn->setToolTip(tr("View and edit current notebook's information"));
  36. notebookComboBox = new QComboBox();
  37. notebookComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
  38. directoryTree = new VDirectoryTree();
  39. QHBoxLayout *nbBtnLayout = new QHBoxLayout;
  40. nbBtnLayout->addWidget(notebookLabel);
  41. nbBtnLayout->addStretch();
  42. nbBtnLayout->addWidget(newNotebookBtn);
  43. nbBtnLayout->addWidget(deleteNotebookBtn);
  44. nbBtnLayout->addWidget(notebookInfoBtn);
  45. QVBoxLayout *nbLayout = new QVBoxLayout;
  46. nbLayout->addLayout(nbBtnLayout);
  47. nbLayout->addWidget(notebookComboBox);
  48. nbLayout->addWidget(directoryLabel);
  49. nbLayout->addWidget(directoryTree);
  50. QWidget *nbContainer = new QWidget();
  51. nbContainer->setLayout(nbLayout);
  52. nbContainer->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  53. // File list widget
  54. fileList = new VFileList();
  55. fileList->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  56. // Editor tab widget
  57. tabs = new VTabWidget();
  58. tabs->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  59. tabs->setTabBarAutoHide(true);
  60. // Main Splitter
  61. mainSplitter = new QSplitter();
  62. mainSplitter->addWidget(nbContainer);
  63. mainSplitter->addWidget(fileList);
  64. mainSplitter->addWidget(tabs);
  65. mainSplitter->setStretchFactor(0, 1);
  66. mainSplitter->setStretchFactor(1, 1);
  67. mainSplitter->setStretchFactor(2, 10);
  68. // Signals
  69. connect(notebookComboBox, SIGNAL(currentIndexChanged(int)), this,
  70. SLOT(setCurNotebookIndex(int)));
  71. connect(this, SIGNAL(curNotebookIndexChanged(const QString&)), directoryTree,
  72. SLOT(setTreePath(const QString&)));
  73. connect(directoryTree, &VDirectoryTree::currentDirectoryChanged,
  74. fileList, &VFileList::setDirectory);
  75. connect(fileList, &VFileList::fileClicked,
  76. tabs, &VTabWidget::openFile);
  77. connect(newNotebookBtn, &QPushButton::clicked,
  78. this, &VMainWindow::onNewNotebookBtnClicked);
  79. connect(vnote, &VNote::notebooksChanged,
  80. this, &VMainWindow::updateNotebookComboBox);
  81. setCentralWidget(mainSplitter);
  82. // Create and show the status bar
  83. statusBar();
  84. }
  85. void VMainWindow::initActions()
  86. {
  87. editNoteAct = new QAction(tr("&Edit"), this);
  88. editNoteAct->setStatusTip(tr("Edit current note"));
  89. connect(editNoteAct, &QAction::triggered,
  90. tabs, &VTabWidget::editFile);
  91. readNoteAct = new QAction(tr("&Read"), this);
  92. readNoteAct->setStatusTip(tr("Open current note in read mode"));
  93. connect(readNoteAct, &QAction::triggered,
  94. tabs, &VTabWidget::readFile);
  95. saveNoteAct = new QAction(tr("&Save"), this);
  96. saveNoteAct->setStatusTip(tr("Save current note"));
  97. connect(saveNoteAct, &QAction::triggered,
  98. tabs, &VTabWidget::saveFile);
  99. }
  100. void VMainWindow::initToolBar()
  101. {
  102. QToolBar *fileToolBar = addToolBar(tr("Note"));
  103. fileToolBar->setMovable(false);
  104. fileToolBar->addAction(editNoteAct);
  105. fileToolBar->addAction(readNoteAct);
  106. fileToolBar->addAction(saveNoteAct);
  107. }
  108. void VMainWindow::initMenuBar()
  109. {
  110. QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
  111. QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
  112. // To be implemented
  113. }
  114. void VMainWindow::updateNotebookComboBox(const QVector<VNotebook> &notebooks)
  115. {
  116. // Clearing and inserting items will emit the signal which corrupt the vconfig's
  117. // current index. We save it first and then set the combobox index to the
  118. // right one to resrote it.
  119. int curIndex = vconfig.getCurNotebookIndex();
  120. notebookComboBox->clear();
  121. if (notebooks.isEmpty()) {
  122. return;
  123. }
  124. for (int i = 0; i <notebooks.size(); ++i) {
  125. notebookComboBox->addItem(notebooks[i].getName());
  126. }
  127. qDebug() << "update notebook combobox with" << notebookComboBox->count()
  128. << "items, current notebook" << curIndex;
  129. notebookComboBox->setCurrentIndex(curIndex);
  130. }
  131. void VMainWindow::setCurNotebookIndex(int index)
  132. {
  133. Q_ASSERT(index < vnote->getNotebooks().size());
  134. // Update directoryTree
  135. QString treePath;
  136. if (index > -1) {
  137. vconfig.setCurNotebookIndex(index);
  138. treePath = vnote->getNotebooks()[index].getPath();
  139. }
  140. emit curNotebookIndexChanged(treePath);
  141. }
  142. void VMainWindow::onNewNotebookBtnClicked()
  143. {
  144. qDebug() << "request to create a notebook";
  145. QString info;
  146. QString defaultName("new_notebook");
  147. QString defaultPath;
  148. do {
  149. VNewNotebookDialog dialog(tr("Create a new notebook"), info, defaultName,
  150. defaultPath, this);
  151. if (dialog.exec() == QDialog::Accepted) {
  152. QString name = dialog.getNameInput();
  153. QString path = dialog.getPathInput();
  154. if (isConflictWithExistingNotebooks(name, path)) {
  155. info = "Name already exists or the path already has a notebook.";
  156. defaultName = name;
  157. defaultPath = path;
  158. continue;
  159. }
  160. vnote->createNotebook(name, path);
  161. }
  162. break;
  163. } while (true);
  164. }
  165. bool VMainWindow::isConflictWithExistingNotebooks(const QString &name, const QString &path)
  166. {
  167. const QVector<VNotebook> &notebooks = vnote->getNotebooks();
  168. for (int i = 0; i < notebooks.size(); ++i) {
  169. if (notebooks[i].getName() == name || notebooks[i].getPath() == path) {
  170. return true;
  171. }
  172. }
  173. return false;
  174. }