vmainwindow.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <QtWidgets>
  2. #include "vmainwindow.h"
  3. #include "vdirectorytree.h"
  4. #include "vnote.h"
  5. #include "vfilelist.h"
  6. VMainWindow::VMainWindow(QWidget *parent)
  7. : QMainWindow(parent)
  8. {
  9. setupUI();
  10. vnote = new VNote();
  11. vnote->readGlobalConfig();
  12. updateNotebookComboBox();
  13. }
  14. VMainWindow::~VMainWindow()
  15. {
  16. delete vnote;
  17. }
  18. void VMainWindow::setupUI()
  19. {
  20. // Notebook directory browser tree
  21. notebookLabel = new QLabel(tr("Notebook"));
  22. notebookComboBox = new QComboBox();
  23. directoryTree = new VDirectoryTree();
  24. QHBoxLayout *nbTopLayout = new QHBoxLayout;
  25. notebookComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  26. notebookComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
  27. nbTopLayout->setAlignment(Qt::AlignLeft);
  28. nbTopLayout->addWidget(notebookLabel);
  29. nbTopLayout->addWidget(notebookComboBox);
  30. QVBoxLayout *nbLayout = new QVBoxLayout;
  31. nbLayout->addLayout(nbTopLayout);
  32. nbLayout->addWidget(directoryTree);
  33. QWidget *nbContainer = new QWidget();
  34. nbContainer->setLayout(nbLayout);
  35. nbContainer->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  36. // File list widget
  37. fileList = new VFileList();
  38. fileList->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
  39. // Editor tab widget
  40. editorTabWidget = new QTabWidget();
  41. editorTabWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  42. editorTabWidget->setTabBarAutoHide(true);
  43. QFile welcomeFile(":/resources/welcome.html");
  44. QString welcomeText("Welcome to VNote!");
  45. if (welcomeFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
  46. welcomeText = QString(welcomeFile.readAll());
  47. welcomeFile.close();
  48. }
  49. QTextBrowser *welcomePage = new QTextBrowser();
  50. welcomePage->setHtml(welcomeText);
  51. editorTabWidget->addTab(welcomePage, tr("Welcome to VNote"));
  52. // Main Splitter
  53. mainSplitter = new QSplitter();
  54. mainSplitter->addWidget(nbContainer);
  55. mainSplitter->addWidget(fileList);
  56. mainSplitter->addWidget(editorTabWidget);
  57. mainSplitter->setStretchFactor(0, 1);
  58. mainSplitter->setStretchFactor(1, 1);
  59. mainSplitter->setStretchFactor(2, 10);
  60. // Signals
  61. connect(notebookComboBox, SIGNAL(currentIndexChanged(int)), this,
  62. SLOT(setCurNotebookIndex(int)));
  63. connect(this, SIGNAL(curNotebookIndexChanged(const QString&)), directoryTree,
  64. SLOT(setTreePath(const QString&)));
  65. connect(directoryTree, &VDirectoryTree::currentDirectoryChanged,
  66. fileList, &VFileList::setDirectory);
  67. setCentralWidget(mainSplitter);
  68. // Create and show the status bar
  69. statusBar();
  70. }
  71. void VMainWindow::updateNotebookComboBox()
  72. {
  73. const QVector<VNotebook> &notebooks = vnote->getNotebooks();
  74. notebookComboBox->clear();
  75. for (int i = 0; i <notebooks.size(); ++i) {
  76. notebookComboBox->addItem(notebooks[i].getName());
  77. }
  78. qDebug() << "update notebook combobox with" << notebookComboBox->count()
  79. << "items";
  80. notebookComboBox->setCurrentIndex(vnote->getCurNotebookIndex());
  81. }
  82. void VMainWindow::setCurNotebookIndex(int index)
  83. {
  84. Q_ASSERT(index < vnote->getNotebooks().size());
  85. qDebug() << "set current notebook index:" << index;
  86. vnote->setCurNotebookIndex(index);
  87. notebookComboBox->setCurrentIndex(index);
  88. // Update directoryTree
  89. emit curNotebookIndexChanged(vnote->getNotebooks()[index].getPath());
  90. }