MarkdownEditor.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "MarkdownEditor.h"
25 
26 #include <sstream>
27 
28 #include <QDesktopServices>
29 #include <QMenu>
30 
31 #include <ArmarXGui/libraries/ArmarXGuiBase/ui_MarkdownEditor.h>
32 
33 #include "cpp-markdown/markdown.h"
34 
35 namespace armarx
36 {
37 
38  MarkdownEditor::MarkdownEditor(QWidget* parent) : QWidget(parent), ui(new Ui::MarkdownEditor)
39  {
40  ui->setupUi(this);
41  connect(ui->btnEdit, SIGNAL(toggled(bool)), this, SLOT(toggleEditor(bool)));
42  connect(ui->btnSyntax, SIGNAL(clicked()), this, SLOT(openSyntaxUrl()));
43  connect(ui->htmlView, SIGNAL(textChanged()), this, SLOT(__forwardTextChanged()));
44  }
45 
47  {
48  delete ui;
49  }
50 
51  QString
53  {
54  if (ui->btnEdit->isChecked())
55  {
56  return ui->htmlView->toPlainText();
57  }
58  else
59  {
60  return plainText;
61  }
62  }
63 
64  void
65  MarkdownEditor::setPlainText(const QString& plainText)
66  {
67  this->plainText = plainText;
68 
69  if (ui->btnEdit->isChecked())
70  {
71  ui->htmlView->setPlainText(plainText);
72  }
73  else
74  {
75  showMarkdown(plainText);
76  }
77  }
78 
79  void
80  MarkdownEditor::showMarkdown(const QString& rawString)
81  {
83 
84  if (rawString.length() > 0)
85  {
86  doc.read(rawString.toStdString());
87  }
88  else
89  {
90  doc.read(plainText.toStdString());
91  }
92 
93  std::stringstream html;
94  doc.write(html);
95  ui->htmlView->setHtml(html.str().c_str());
96  }
97 
98  void
100  {
101  if (toggled)
102  {
103  ui->htmlView->setPlainText(plainText);
104  ui->htmlView->setReadOnly(false);
105  }
106  else
107  {
108  plainText = ui->htmlView->toPlainText();
109  showMarkdown();
110  ui->htmlView->setReadOnly(true);
111  }
112 
113  // ui->plainView->setVisible(toggled);
114  // ui->htmlView->setVisible(!toggled);
115  }
116 
117  void
119  {
120  QDesktopServices::openUrl(QUrl("https://help.github.com/articles/markdown-basics/"));
121  }
122 
123  void
124  MarkdownEditor::__forwardTextChanged()
125  {
126  if (ui->btnEdit->isChecked())
127  {
128  emit textChanged();
129  }
130  }
131 
132 } // namespace armarx
armarx::MarkdownEditor::openSyntaxUrl
void openSyntaxUrl()
Definition: MarkdownEditor.cpp:118
armarx::MarkdownEditor::textChanged
void textChanged()
MarkdownEditor.h
markdown::Document
Definition: markdown.h:24
markdown.h
markdown::Document::read
bool read(const std::string &)
Definition: markdown.cpp:956
Ui
ArmarX Headers.
Definition: ArmarXMainWindow.h:54
markdown::Document::write
void write(std::ostream &)
Definition: markdown.cpp:1043
armarx::MarkdownEditor::MarkdownEditor
MarkdownEditor(QWidget *parent=0)
Definition: MarkdownEditor.cpp:38
armarx::MarkdownEditor::setPlainText
void setPlainText(const QString &plainText)
Definition: MarkdownEditor.cpp:65
armarx::MarkdownEditor::showMarkdown
void showMarkdown(const QString &rawString="")
Definition: MarkdownEditor.cpp:80
armarx::MarkdownEditor::toPlainText
QString toPlainText() const
Definition: MarkdownEditor.cpp:52
armarx::MarkdownEditor::toggleEditor
void toggleEditor(bool toggled)
Definition: MarkdownEditor.cpp:99
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::MarkdownEditor
The MarkdownEditor is a widget that provides editing of raw text and viewing of processed markdown te...
Definition: MarkdownEditor.h:45
armarx::MarkdownEditor::~MarkdownEditor
~MarkdownEditor() override
Definition: MarkdownEditor.cpp:46