-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmainwindow.cpp
361 lines (303 loc) · 9.14 KB
/
mainwindow.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "insertimagedialog.h"
#include "pagesetupdialog.h"
#include "inserttabledialog.h"
#include "setlinedistancedialog.h"
#include "countwordsdialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
init();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init()
{
//初始化主窗体
setWindowState(Qt::WindowMaximized);
setWindowTitle("Editor文本编辑系统");
//添加窗口中心滚动区
scrollArea = new QScrollArea(this); //MainWindow中心部件为滚动区
scrollArea->setBackgroundRole(QPalette::Dark); //滚动区灰色
setCentralWidget(scrollArea);
//在中心滚动区添加页面
editor = new RichTextProcess(this);
editor->resize(editor->getPaperWidth()*editor->getScale(),editor->getPaperHeight()*editor->getPaperNum()*editor->getScale()); //重新设置editor窗口大小,A4比例大小
scrollArea->setWidget(editor); //滚动区内添加文本编辑区
scrollArea->setAlignment(Qt::AlignCenter); //是文本编辑框居中对齐
//添加字体选择 OFD阅读器不支持Qt中支持的很多字体!!!!!!!!!
fontCombo = new QFontComboBox(this);
ui->toolBar->addWidget(fontCombo); //designer界面字体选择框添加不到工具栏中?用代码实现
fontCombo->setStatusTip("更改字体");
connect(fontCombo, SIGNAL(activated(QString)),this,SLOT(textFamily(QString))); //连接字体选择框的槽
//添加字号选择
fontSizeCombo = new QComboBox(this);
ui->toolBar->addWidget(fontSizeCombo);
fontSizeCombo->setEditable(true);
fontSizeCombo->setStatusTip("更改字号");
fontSizeCombo->addItem("11"); //与编辑框初始编辑字号相对应
QFontDatabase db; //字体库中选择
foreach(int size, db.standardSizes())
fontSizeCombo->addItem(QString::number(size));
connect(fontSizeCombo, SIGNAL(activated(QString)), this, SLOT(textSize(QString)));
//添加状态栏
pageNum = new QLabel();
pageNum->setText("共1页");
statusBar()->addWidget(pageNum);
connect(editor, SIGNAL(paperNumChanged(int)), this, SLOT(changeStatusBar(int)));
}
//创建字体选择框,改变字体时所需要的函数
void MainWindow::textFamily(const QString &f)
{
QTextCharFormat fmt;
fmt.setFontFamily(f);
editor->setFormat(fmt);
}
//创建字号选择框,改变字号时所需要的函数
void MainWindow::textSize(const QString &p)
{
qreal pointSize = p.toFloat();
if(p.toFloat()>0)
{
QTextCharFormat fmt;
fmt.setFontPointSize(pointSize);
editor->setFormat(fmt);
}
}
void MainWindow::on_action_saveAs_triggered()
{
editor->saveAs();
}
void MainWindow::on_action_left_triggered()
{
editor->setAlign(0);
}
void MainWindow::on_action_right_triggered()
{
editor->setAlign(1);
}
void MainWindow::on_action_center_triggered()
{
editor->setAlign(2);
}
void MainWindow::on_action_bold_triggered()
{
QTextCharFormat fmt;
QTextCursor cursor = editor->getCurrentPaper()->textCursor();
fmt = cursor.charFormat();
if(fmt.fontWeight()==QFont::Bold)
fmt.setFontWeight(QFont::Normal);
else
fmt.setFontWeight(QFont::Bold);
ui->action_bold->setChecked(false);
editor->setFormat(fmt);
}
void MainWindow::on_action_italic_triggered()
{
QTextCharFormat fmt;
QTextCursor cursor = editor->getCurrentPaper()->textCursor();
fmt = cursor.charFormat();
fmt.setFontItalic(!fmt.fontItalic());
ui->action_italic->setChecked(false);
editor->setFormat(fmt);
}
void MainWindow::on_action_underline_triggered()
{
QTextCharFormat fmt;
QTextCursor cursor = editor->getCurrentPaper()->textCursor();
fmt = cursor.charFormat();
fmt.setFontUnderline(!fmt.fontUnderline());
ui->action_underline->setChecked(false);
editor->setFormat(fmt);
}
void MainWindow::on_action_formatPainter_triggered()
{
editor->formatPainter(ui->action_formatPainter->isChecked());
}
void MainWindow::on_action_deletePage_triggered()
{
editor->deleteLastPaper(); //此处调用之前自动删除页面所用的槽函数,可行。
editor->resizePage(); //此处页面大小需要计算
}
void MainWindow::on_action_newPage_triggered()
{
editor->createNewPaper();
editor->resizePage(); //此处页面大小需要计算
}
void MainWindow::on_action_insertImage_triggered()
{
InsertImageDialog *dialog = new InsertImageDialog(editor, this);
dialog->exec();
//交由富文本处理类实现添加图片功能
// editor->insertImage();
}
void MainWindow::receiveLeftMargin(double num)
{
editor->getCurrentPaper()->setLeftMargin(num);
}
void MainWindow::receiveRightMargin(double num)
{
editor->getCurrentPaper()->setRightMargin(num);
}
void MainWindow::receiveTopMargin(double num)
{
editor->getCurrentPaper()->setTopMargin(num);
}
void MainWindow::receiveBottomMargin(double num)
{
editor->getCurrentPaper()->setBottomMargin(num);
}
void MainWindow::receiveSetMargainYesButton()
{
pageMarginDialog->close();
}
void MainWindow::receiveSetMargainResetButton()
{
receiveLeftMargin(30.00);
receiveRightMargin(30.00);
receiveTopMargin(30.00);
receiveBottomMargin(30.00);
pageMarginDialog->close();
}
void MainWindow::on_action_pageMargin_triggered()
{
PageSetupDialog *dialog = new PageSetupDialog(editor, this);
dialog->exec();
}
void MainWindow::on_action_color_triggered()
{
QColor color = QColorDialog::getColor(Qt::black);
if(!color.isValid())
return;
QTextCharFormat fmt;
fmt.setForeground(color);
editor->setFormat(fmt);
}
void MainWindow::on_action_lineDistance_triggered()
{
setLineDistanceDialog *dialog = new setLineDistanceDialog(editor, this);
dialog->exec();
}
void MainWindow::on_action_undo_triggered()
{
this->editor->getCurrentPaper()->undo();
}
void MainWindow::on_action_redo_triggered()
{
this->editor->getCurrentPaper()->redo();
}
void MainWindow::on_action_table_triggered()
{
InsertTableDialog *dialog = new InsertTableDialog(editor, this);
dialog->exec();
}
void MainWindow::on_action_saveAsEdit_triggered()
{
editor->saveToRichText();
}
void MainWindow::on_action_open_ofdx_triggered()
{
editor->openRichText();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
//判断文本编辑区是否有内容,若有则先提示保存,再重置界面
if(!editor->editorIsEmpty())
{
QMessageBox* msgBox = RichTextProcess::showMessage();
int ret = msgBox->exec();
if (ret == QMessageBox::Save)
{
editor->saveToRichText();
editor->resetEditor();
}
else if (ret == QMessageBox::Discard)
editor->resetEditor();
else
event->ignore();
msgBox->deleteLater();
}
}
void MainWindow::on_action_new_triggered()
{
if(!editor->editorIsEmpty())
{
QMessageBox* msgBox = RichTextProcess::showMessage();
int ret = msgBox->exec();
if (ret == QMessageBox::Save)
{
editor->saveToRichText();
editor->resetEditor();
}
else if (ret == QMessageBox::Discard)
editor->resetEditor();
msgBox->deleteLater();
}
}
void MainWindow::changeStatusBar(int i)
{
pageNum->setText("共"+QString::number(i)+"页");
}
void MainWindow::on_action_pageDirection_triggered()
{
editor->changePaperDirection();
}
void MainWindow::on_action_enlarge_triggered()
{
editor->enlarge();
}
void MainWindow::on_action_shrink_triggered()
{
// editor->shrink();
// QTextCharFormat charFmt;
// editor->getCurrentPaper()->selectAll();
// editor->getCurrentPaper()->mergeCurrentCharFormat(charFmt);
// editor->getCurrentPaper()->mergeCurrentCharFormat(charFmt);
editor->getCurrentPaper()->zoomIn();
qDebug() << "zoomIn";
}
/*
// 滚轮事件
void MainWindow::wheelEvent(QWheelEvent *event)
{
// 当滚轮远离使用者时进行放大,当滚轮向使用者方向旋转时进行缩小
if(event->delta() > 0){
editor->getCurrentPaper()->zoomIn();
}else{
qDebug() << "zoomout";
editor->getCurrentPaper()->zoomOut();
}
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
// // 事件过滤器
// // 如果是textEdit部件上的事件
if(obj == editor->getCurrentPaper()){
if(event->type() == QEvent::KeyPress) {
QKeyEvent *kevent = dynamic_cast<QKeyEvent *>(event);
if (kevent->key() == Qt::Key_Down) {
qDebug() << "zoomout";
editor->getCurrentPaper()->zoomOut();
}
else if(kevent->key() == Qt::Key_Up) {
editor->getCurrentPaper()->zoomIn();
}
}
}
return QMainWindow::eventFilter(obj,event);
}
*/
void MainWindow::on_action_count_triggered()
{
CountWordsDialog *dialog = new CountWordsDialog(editor->countWords());
dialog->exec();
}
void MainWindow::on_action_preview_triggered()
{
editor->preview();
}