vmainwindow.cpp 3.2 KB

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