-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.cpp
195 lines (158 loc) · 6.12 KB
/
window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// window.cpp
// markdown
//
// Created by Jackey Chen on 5/27/15.
//
//
#include "window.h"
#include "mdhandler.h"
#include <QWidget>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QTextEdit>
#include <QGridLayout>
#include <QSizePolicy>
#include <QTextStream>
#include <QFile>
#include <iostream>
#include <QFileDialog>
#include <QDir>
#include <QPrinter>
namespace md {
Window::Window(QWidget *parent) {
QFile styleFile(":/style/screen.css");
styleFile.open(QIODevice::ReadOnly | QIODevice::Text);
screenStyleFile = new QTextStream(&styleFile);
createMenus();
createComponents();
initMdHandler();
}
Window::~Window() {
delete screenStyleFile;
}
void Window::initMdHandler() {
MdHandler *mdhandler = new MdHandler(mdEditor);
QObject::connect(mdEditor, SIGNAL(textChanged()), mdhandler, SLOT(handleMdChanged()));
QObject::connect(mdhandler, SIGNAL(mdCompiled(QString)), this, SLOT(handleMdCompiled(QString)));
}
void Window::createMenus() {
QAction *action;
// 文件相关菜单
QMenu *file = new QMenu(this);
action = new QAction(tr("&Open"), this);
action->setShortcut(QKeySequence::Open);
file->addAction(action);
file->addSeparator();
saveAction = new QAction(tr("&Save"), this);
saveAction->setShortcut(QKeySequence::Save);
file->addAction(saveAction);
QObject::connect(saveAction, SIGNAL(triggered()), this, SLOT(saveTriggered()));
htmlAction =new QAction(tr("&Save As HTML"), this);
QObject::connect(htmlAction, SIGNAL(triggered()), this, SLOT(saveAsHTML()));
file->addAction(htmlAction);
pdfAction = new QAction(tr("&Save As PDF"), this);
QObject::connect(pdfAction, SIGNAL(triggered()), this, SLOT(saveAsPDF()));
file->addAction(pdfAction);
file->addSeparator();
menuBar()->addMenu(file)->setText(tr("&File"));
// 编辑相关菜单
QMenu *edit = new QMenu(this);
menuBar()->addMenu(edit)->setText(tr("&Edit"));
statusBar();
}
void Window::createComponents() {
mdEditor = new QTextEdit(this);
mdEditor->setSizePolicy(QSizePolicy ::Expanding , QSizePolicy ::Expanding);
mdEditor->setText("");
htmlEditor = new QTextEdit(this);
htmlEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QTextDocument *document = new QTextDocument();
if (screenStyleFile) {
screenStyleFile->reset();
screenStyleFile->seek(0);
QString css = screenStyleFile->readAll();
document->setDefaultStyleSheet(css);
}
else {
qDebug("Read css failed");
}
const QString defaultHtml = MdHandler::markdown(mdEditor->toPlainText());
document->setHtml(defaultHtml);
htmlEditor->setDocument(document);
QGridLayout *mainLayout = new QGridLayout();
mainLayout->addWidget(mdEditor, 0, 0);
mainLayout->addWidget(htmlEditor, 0, 1);
QWidget *main = new QWidget();
main->setLayout(mainLayout);
setCentralWidget(main);
setWindowTitle(tr("Markdown Editor From Fumer"));
setMinimumSize(800, 900);
}
void Window::handleMdCompiled(QString str) {
str.append("</body></html>");
str.prepend("<html><body>");
htmlEditor->setHtml(str);
}
void Window::saveAsHTML() {
if (htmlSaveTo.isEmpty()) {
QString homeDir = QDir::homePath();
htmlSaveTo = QFileDialog::getSaveFileName(this, tr("Save HTML To"), homeDir.append("/Documents"), tr("Pdf Files (*.html)"));
}
// 用户选择文件后 存储选择位置
if (!htmlSaveTo.isEmpty()) {
QString html = htmlEditor->toHtml();
QFile saveTo(htmlSaveTo);
saveTo.open(QIODevice::WriteOnly | QIODevice::Text);
char *data = html.toUtf8().data();
saveTo.write(html.toUtf8().data(), strlen(data));
saveTo.close();
saveType = SAVE_HTML;
}
}
void Window::saveAsPDF() {
if (pdfSaveTo.isEmpty()) {
QString homeDir = QDir::homePath();
pdfSaveTo = QFileDialog::getSaveFileName(this, tr("Save PDF To"), homeDir.append("/Documents"), tr("HTML Files (*.pdf)"));
}
if (!pdfSaveTo.isEmpty()) {
QString html = htmlEditor->toHtml();
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(pdfSaveTo);
QTextDocument *document = new QTextDocument();
document->setHtml(html);
document->print(&printer);
delete document;
saveType = SAVE_PDF;
}
}
void Window::saveTriggered() {
switch (saveType) {
case SAVE_HTML:
if (!htmlSaveTo.isEmpty()) {
emit htmlAction->trigger();
}
break;
case SAVE_PDF:
if (!pdfSaveTo.isEmpty()) {
emit pdfAction->trigger();
}
break;
}
QString homeDir = QDir::homePath();
if (mdSaveTo.isEmpty()) {
mdSaveTo = QFileDialog::getSaveFileName(this,
tr("Save Markdown File To"),
homeDir.append("/Documents"),
tr("Markdown files (*.md)"));
}
QFile saveTo(mdSaveTo);
saveTo.open(QIODevice::WriteOnly | QIODevice::Text);
char *data = mdEditor->toPlainText().toUtf8().data();
saveTo.write(data, strlen(data));
saveTo.close();
}
}