/marknoto/noteview.cpp

https://github.com/sadhen/marketo · C++ · 180 lines · 144 code · 26 blank · 10 comment · 11 complexity · 7b0817b73c672959a139bf5bfdf4d792 MD5 · raw file

  1. #include "noteview.h"
  2. #include "taglist.h"
  3. #include <KTextEditor/Document>
  4. #include <KTextEditor/View>
  5. #include <KConfigGroup>
  6. #include <KSharedConfig>
  7. #include <KActionCollection>
  8. #include <QAction>
  9. #include <QDir>
  10. #include <QVBoxLayout>
  11. #include <QUrl>
  12. #include <QDesktopServices>
  13. #include <QListWidget>
  14. #include <QLabel>
  15. #include <QLineEdit>
  16. #include <QMessageBox>
  17. NoteView::NoteView(QWidget* parent, KActionCollection *pActions)
  18. : QWidget(parent),
  19. actions(pActions)
  20. {
  21. done = new QStack<QUrl>();
  22. todo = new QStack<QUrl>();
  23. setupUI();
  24. setupConnect();
  25. }
  26. void NoteView::pureOpenUrl(const QUrl& url)
  27. {
  28. // TODO:if the url is not in the watching dir and is in three column view
  29. // switch to one column view
  30. title->setText(url.fileName());
  31. note->openUrl(url);
  32. note->setHighlightingMode("CommonMark");
  33. if (actions->action("file_preview")->isChecked())
  34. markPad->preview();
  35. else
  36. markPad->unpreview();
  37. }
  38. void NoteView::setupConnect()
  39. {
  40. connect(title, &QLineEdit::returnPressed,
  41. this, &NoteView::saveNote);
  42. }
  43. void NoteView::setupUI()
  44. {
  45. vl = new QVBoxLayout(this);
  46. title = new QLineEdit(this);
  47. markPad = new Markpado(this);
  48. note = markPad->m_note;
  49. vl->addWidget(title);
  50. vl->addWidget(markPad);
  51. title->setFixedHeight(24);
  52. title->setContentsMargins(0, 0, 0, 0);
  53. title->setAlignment(Qt::AlignHCenter);
  54. title->setStyleSheet("QLineEdit { border: 1px solid lightskyblue; border-radius: 2px; }");
  55. vl->setSpacing(0);
  56. }
  57. void NoteView::saveNote()
  58. {
  59. QString name(title->text());
  60. QUrl url = note->url();
  61. note->documentSave();
  62. if (QFileInfo(url.path()).isDir()) {
  63. url = QUrl::fromLocalFile(url.path().append("/").append(name));
  64. } else
  65. url.setUrl(url.url(QUrl::RemoveFilename).append(name));
  66. QDir dir(url.url(QUrl::RemoveFilename | QUrl::PreferLocalFile));
  67. dir.rename(note->url().fileName(), url.fileName());
  68. note->openUrl(url);
  69. markPad->view()->setFocus();
  70. }
  71. void NoteView::hideMetaData()
  72. {
  73. title->setHidden(true);
  74. }
  75. void NoteView::showMetaData()
  76. {
  77. title->setHidden(false);
  78. }
  79. void NoteView::setTitle(const QString& titleOfNote)
  80. {
  81. title->setText(titleOfNote);
  82. }
  83. void NoteView::forward()
  84. {
  85. QUrl url(todo->pop());
  86. done->push(url);
  87. pureOpenUrl(url);
  88. }
  89. void NoteView::backward()
  90. {
  91. QUrl url(done->pop());
  92. todo->push(url);
  93. pureOpenUrl(done->top());
  94. }
  95. bool NoteView::canBackward()
  96. {
  97. if (done->count() > 1)
  98. return true;
  99. else
  100. return false;
  101. }
  102. bool NoteView::canForward()
  103. {
  104. if (todo->isEmpty())
  105. return false;
  106. else
  107. return true;
  108. }
  109. void NoteView::openUrl(const QUrl& url)
  110. {
  111. pureOpenUrl(url);
  112. if (done->isEmpty() || url != done->top())
  113. done->push(url);
  114. if (canBackward())
  115. actions->action("go_backward")->setChecked(false);
  116. delete todo;
  117. todo = new QStack<QUrl>();
  118. actions->action("go_forward")->setChecked(true);
  119. /*
  120. QVectorIterator<QUrl> i(*done);
  121. while (i.hasNext())
  122. qDebug() << i.next();
  123. */
  124. }
  125. // Open url that from the note(links)
  126. void NoteView::slotOpen(const QUrl& url)
  127. {
  128. if (url.toString().startsWith("qrc:/")) {
  129. // case1: fake root link
  130. KConfigGroup cfg(KSharedConfig::openConfig(), "General Options");
  131. QString rootPath(cfg.readEntry("NoteDir"));
  132. QString notePath(url.toString().replace("qrc:/", "/"));
  133. QString path = rootPath + notePath;
  134. qDebug() << path;
  135. if (QFileInfo(path).exists()) {
  136. openUrl(QUrl::fromLocalFile(path));
  137. return ;
  138. }
  139. // case2: relative path link(automatically handled by API)
  140. openUrl(url);
  141. } else {
  142. QDesktopServices::openUrl(url);
  143. }
  144. }
  145. void NoteView::focusTitle()
  146. {
  147. title->setFocus();
  148. }
  149. NoteView::~NoteView()
  150. {
  151. delete done;
  152. delete todo;
  153. }
  154. #include "noteview.moc"