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 #include <ArmarXGui/libraries/ArmarXGuiBase/ui_MarkdownEditor.h>
26 
27 #include "cpp-markdown/markdown.h"
28 
29 #include <QDesktopServices>
30 #include <QMenu>
31 #include <sstream>
32 
33 namespace armarx
34 {
35 
36  MarkdownEditor::MarkdownEditor(QWidget* parent) :
37  QWidget(parent),
38  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 
46  }
47 
49  {
50  delete ui;
51  }
52 
54  {
55  if (ui->btnEdit->isChecked())
56  {
57  return ui->htmlView->toPlainText();
58  }
59  else
60  {
61  return plainText;
62  }
63  }
64 
65  void 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 MarkdownEditor::showMarkdown(const QString& rawString)
80  {
82 
83  if (rawString.length() > 0)
84  {
85  doc.read(rawString.toStdString());
86  }
87  else
88  {
89  doc.read(plainText.toStdString());
90  }
91 
92  std::stringstream html;
93  doc.write(html);
94  ui->htmlView->setHtml(html.str().c_str());
95  }
96 
97  void MarkdownEditor::toggleEditor(bool toggled)
98  {
99  if (toggled)
100  {
101  ui->htmlView->setPlainText(plainText);
102  ui->htmlView->setReadOnly(false);
103  }
104  else
105  {
106  plainText = ui->htmlView->toPlainText();
107  showMarkdown();
108  ui->htmlView->setReadOnly(true);
109  }
110 
111  // ui->plainView->setVisible(toggled);
112  // ui->htmlView->setVisible(!toggled);
113  }
114 
116  {
117  QDesktopServices::openUrl(QUrl("https://help.github.com/articles/markdown-basics/"));
118  }
119 
120  void MarkdownEditor::__forwardTextChanged()
121  {
122  if (ui->btnEdit->isChecked())
123  {
124  emit textChanged();
125  }
126  }
127 
128 }
armarx::MarkdownEditor::openSyntaxUrl
void openSyntaxUrl()
Definition: MarkdownEditor.cpp:115
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:906
Ui
ArmarX Headers.
Definition: ArmarXMainWindow.h:58
markdown::Document::write
void write(std::ostream &)
Definition: markdown.cpp:991
armarx::MarkdownEditor::MarkdownEditor
MarkdownEditor(QWidget *parent=0)
Definition: MarkdownEditor.cpp:36
armarx::MarkdownEditor::setPlainText
void setPlainText(const QString &plainText)
Definition: MarkdownEditor.cpp:65
armarx::MarkdownEditor::showMarkdown
void showMarkdown(const QString &rawString="")
Definition: MarkdownEditor.cpp:79
armarx::MarkdownEditor::toPlainText
QString toPlainText() const
Definition: MarkdownEditor.cpp:53
armarx::MarkdownEditor::toggleEditor
void toggleEditor(bool toggled)
Definition: MarkdownEditor.cpp:97
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
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:48