Skip to content

Commit

Permalink
support pdf export with checkboxes (#1155)
Browse files Browse the repository at this point in the history
  • Loading branch information
liulex authored and pbek committed Feb 21, 2019
1 parent b26a433 commit 040c9dc
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 41 deletions.
32 changes: 21 additions & 11 deletions src/mainwindow.cpp
Expand Up @@ -4765,23 +4765,27 @@ bool MainWindow::prepareExportNoteAsPDFPrinter(QPrinter *printer) {
* @param textEdit
*/
void MainWindow::exportNoteAsPDF(QPlainTextEdit *textEdit) {
QPrinter *printer = new QPrinter(QPrinter::HighResolution);

if (prepareExportNoteAsPDFPrinter(printer)) {
textEdit->document()->print(printer);
Utils::Misc::openFolderSelect(printer->outputFileName());
}
exportNoteAsPDF(textEdit->document());
}

/**
* @brief Exports the content of a text edit widget as PDF
* @param textEdit
*/
void MainWindow::exportNoteAsPDF(QTextEdit *textEdit) {
exportNoteAsPDF(textEdit->document());
}

/**
* @brief Exports the document as PDF
* @param doc
*/
void MainWindow::exportNoteAsPDF(QTextDocument *doc)
{
QPrinter *printer = new QPrinter(QPrinter::HighResolution);

if (prepareExportNoteAsPDFPrinter(printer)) {
textEdit->document()->print(printer);
doc->print(printer);
Utils::Misc::openFolderSelect(printer->outputFileName());
}
}
Expand Down Expand Up @@ -5862,6 +5866,7 @@ void MainWindow::noteTextEditCustomContextMenuRequested(
QString html = currentNote.textToMarkdownHtml(
selectedNoteTextEditText(), NoteFolder::currentLocalPath(),
getMaxImageWidth());
html = Utils::Misc::parseTaskList(html, false);
QTextEdit *textEdit = new QTextEdit(this);
textEdit->setHtml(html);
exportNoteAsPDF(textEdit);
Expand Down Expand Up @@ -5944,10 +5949,15 @@ void MainWindow::on_actionOpen_List_triggered() {
* @brief Exports the current note as PDF (markdown)
*/
void MainWindow::on_action_Export_note_as_PDF_markdown_triggered() {
// reload the preview in case it is turned off
setNoteTextFromNote(&currentNote, true, true);

exportNoteAsPDF(ui->noteTextView);
bool decrypt = ui->noteTextEdit->isHidden();
QString html = currentNote.toMarkdownHtml(NoteFolder::currentLocalPath(),
getMaxImageWidth(), false, decrypt);
html = Utils::Misc::parseTaskList(html, false);

auto doc = ui->noteTextView->document()->clone();
doc->setHtml(html);
exportNoteAsPDF(doc);
doc->deleteLater();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/mainwindow.h
Expand Up @@ -711,6 +711,8 @@ private slots:

void exportNoteAsPDF(QTextEdit *textEdit);

void exportNoteAsPDF(QTextDocument *doc);

void printNote(QTextEdit *textEdit);

void updateEncryptNoteButtons();
Expand Down
40 changes: 40 additions & 0 deletions src/utils/misc.cpp
Expand Up @@ -624,6 +624,46 @@ QString Utils::Misc::htmlToMarkdown(QString text) {
return text;
}

/**
* Returns new html with task lists by checkboxes
*
* @param html
* @param clickable
* @return
*/
QString Utils::Misc::parseTaskList(const QString &html, bool clickable)
{
auto text = html;

if (!clickable) {
text.replace(QRegExp(R"((<li>\s*(<p>)*\s*)\[ ?\])", Qt::CaseInsensitive), "\\1&#9744;");
text.replace(QRegExp(R"((<li>\s*(<p>)*\s*)\[[xX]\])", Qt::CaseInsensitive), "\\1&#9745;");
return text;
}

// TODO
// to ensure the clicking behavior of checkboxes,
// line numbers of checkboxes in the original markdown text
// should be provided by the markdown parser

const QString checkboxStart = R"(<a class="task-list-item-checkbox" href="checkbox://_)";
text.replace(QRegExp(R"((<li>\s*(<p>)*\s*)\[ ?\])", Qt::CaseInsensitive), "\\1" + checkboxStart + "\">&#9744;</a>");
text.replace(QRegExp(R"((<li>\s*(<p>)*\s*)\[[xX]\])", Qt::CaseInsensitive), "\\1" + checkboxStart + "\">&#9745;</a>");

int count = 0;
int pos = 0;
while (true) {
pos = text.indexOf(checkboxStart + "\"", pos);
if (pos == -1)
break;

pos += checkboxStart.length();
text.insert(pos, QString::number(count++));
}

return text;
}

/**
* Returns a list of all parents until the top
*
Expand Down
1 change: 1 addition & 0 deletions src/utils/misc.h
Expand Up @@ -58,6 +58,7 @@ namespace Utils {
bool ifNotEmptyOnly = false);
QString makePathRelativeToPortableDataPathIfNeeded(QString path);
QString htmlToMarkdown(QString text);
QString parseTaskList(const QString &html, bool clickable);
QByteArray startSynchronousProcess(
QString executablePath, QStringList parameters,
QByteArray data = QByteArray());
Expand Down
31 changes: 2 additions & 29 deletions src/widgets/notepreviewwidget.cpp
Expand Up @@ -12,6 +12,7 @@
*/

#include "notepreviewwidget.h"
#include "utils/misc.h"
#include <QLayout>
#include <QDebug>
#include <QRegExp>
Expand Down Expand Up @@ -157,38 +158,10 @@ void NotePreviewWidget::animateGif(const QString &text) {
}
}

/**
* @brief Replace task lists with checkboxes
* @return Html text with checkboxes
*/
QString NotePreviewWidget::handleTaskLists(const QString &text_) {
//TODO
// to ensure the clicking behavior of checkboxes,
// line numbers of checkboxes in the original markdown text
// should be provided by the markdown parser
auto text = text_;

const QString checkboxStart = R"(<a class="task-list-item-checkbox" href="checkbox://_)";
text.replace(QRegExp(R"((<li>\s*(<p>)*\s*)\[ ?\])", Qt::CaseInsensitive), "\\1" + checkboxStart + "\">&#9744;</a>");
text.replace(QRegExp(R"((<li>\s*(<p>)*\s*)\[[xX]\])", Qt::CaseInsensitive), "\\1" + checkboxStart + "\">&#9745;</a>");

int count = 0;
int pos = 0;
while (true) {
pos = text.indexOf(checkboxStart, pos);
if (pos == -1)
break;

pos += checkboxStart.length();
text.insert(pos, QString::number(count++));
}
return text;
}

void NotePreviewWidget::setHtml(const QString &text) {
animateGif(text);

QTextBrowser::setHtml(handleTaskLists(text));
QTextBrowser::setHtml(Utils::Misc::parseTaskList(text, true));
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/widgets/notepreviewwidget.h
Expand Up @@ -37,7 +37,6 @@ class NotePreviewWidget : public QTextBrowser

QStringList extractGifUrls(const QString &text) const;
void animateGif(const QString &text);
QString handleTaskLists(const QString &text);

public slots:
void hide();
Expand Down

0 comments on commit 040c9dc

Please sign in to comment.