-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainwindow.cpp.Vxqzju
359 lines (320 loc) · 11.2 KB
/
mainwindow.cpp.Vxqzju
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "simplepage.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setRandSource();
setFixedSize(1000,620);
settips();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::settips()
{
QFont font(tr("Times New Roman"));
font.setFamily(tr("Times New Roman"));
ui->fontCbx->setCurrentFont(font);
ui->fontCbx->setToolTip(tr("Sellect a font"));
ui->addFontBtn->setToolTip(tr("Add a font"));
ui->removeFontBtn->setToolTip(tr("Remove the sellected font"));
ui->CharacterSizeTxt->setToolTip(tr("Original size"));
ui->CharacterSpaceTxt->setToolTip(tr("Charactor additional pixel"));
ui->PenWidthSbx->setToolTip(tr("Original pen width\n(a useless argument)"));
ui->CharacterExample->setToolTip(tr("Generate example base on this charactor"));
ui->RandOffsetTxt->setToolTip(tr("Charactors' position will fluctuate"));
ui->RandPenWidthSbx->setToolTip(tr("Pen width may change randomly\n(a useless argument)"));
ui->RandomScaleTxt->setToolTip(tr("Become tiner or widther\n(from 1/n to n times)"));
ui->randRotateTxt->setToolTip(tr("Rotate character random\n from -n to n"));
ui->topMargainTxt->setToolTip(tr("Paper top margain"));
ui->bottemMargainTxt->setToolTip(tr("Paper bottem margain"));
ui->rightMargainTxt->setToolTip(tr("Paper righter margain"));
ui->leftMargainTxt->setToolTip(tr("Paper left margain"));
ui->columnSpacingTxt->setToolTip(tr("Spaceing of column"));
ui->lineSpacingTxt->setToolTip(tr("Spacing of line"));
}
void MainWindow::outPreview(QString *fileName)//load output picture
{
QImage *img = new QImage;
ifErrorDig(img->load(*fileName),
"An error occur when we load image!");
ui->PreviewLab->setPixmap(QPixmap::fromImage(*img));
}
QImage MainWindow::drawCharacter(QString character,int CharacterSpace,int characterSize)
{
//get requirement input by user
int penWidth= ui->PenWidthSbx->text().toInt();
//In order to ensure next character font is different than previous one. character number will be store
static int preCharacterNum = 0;
int totalFont = fontList.length();
QFont *font = new QFont;
if(totalFont==0){
font->setFamily(DEFALUTFONT);
}
else{
int fontnum;
do{
fontnum = qrand()%totalFont;
}while(fontnum==preCharacterNum&&totalFont!=1);
preCharacterNum = fontnum;
//set font
font->setFamily(fontList.at(fontnum).family());
}
font->setPixelSize(characterSize);//set font size
font->setWeight(penWidth);
//set image size
QFontMetrics fm(*font);
int charWidth = fm.width(character);
int charHeight = fm.height();
QSize imgSize(charWidth+CharacterSpace,charHeight+CharacterSpace);
QImage img(imgSize,QImage::Format_ARGB32);//Create a img
img.fill(Qt::transparent);//fill a transparent background
QPainter painter(&img);//create a painter
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);//set CompositionMode to paint a draw
//get a pen, set font and ready to draw
QPen pen = painter.pen();
pen.setColor(QColor(0,0,0));
painter.setPen(pen);
painter.setFont(*font);
//calculate draw position and move coordinate to that
QPoint drawPosition = img.rect().bottomLeft();
drawPosition.setX(drawPosition.x()+CharacterSpace/2);
drawPosition.setY(drawPosition.y()-characterSize/8-CharacterSpace/2);
painter.translate(drawPosition);
/*******random change before draw*******/
//random offset
int randoffNum = ui->RandOffsetTxt->text().toInt();
int offsetX = 0;
int offsetY = 0;
if(randoffNum != 0){
offsetX = getRandnum(randoffNum);
offsetY = getRandnum(randoffNum);
}
//rand pend width. PenWidth mast between 0 and 99, otherwise program will crash
int randPenWidth = ui->RandPenWidthSbx->text().toInt();
if(randPenWidth != 0)
{
randPenWidth += penWidth + getRandnum(randPenWidth);
if(randPenWidth>99)//make sure randPenWidth between 0 and 99
randPenWidth = 99;
else if(randPenWidth<0)
randPenWidth = 0;
font->setWeight(randPenWidth);
painter.setFont(*font);
}
//random rotate
int randRotateAngel = ui->randRotateTxt->text().toInt();
if(randRotateAngel != 0){
qreal randRotate = (qreal)(randRotateAngel*rand()*1./(RAND_MAX));
painter.rotate(randRotate);
}
//random scale. x and y will bcome 1/x~x and 1/y~y
float randScale = ui->RandomScaleTxt->text().toFloat();
if(randScale>0){
qreal randScaleX = (qreal)(rand()*(randScale-1./randScale)*1./(RAND_MAX)+(1./randScale));
qreal randScaleY = (qreal)(rand()*(randScale-1./randScale)*1./(RAND_MAX)+(1./randScale));
painter.scale(randScaleX,randScaleY);
}
/******************draw******************/
painter.drawText(offsetX,offsetY,character);
// painter.drawText(img.rect(),Qt::AlignCenter,character);
// ifErrorDig(img.save("./imgOutput.png","png",100),
// "An error occur when we save img!");
return img;
}
void MainWindow::generateExp()
{
QImage img = drawCharacter(ui->CharacterExample->text(),10,40);
ui->Example1->setPixmap(QPixmap::fromImage(img));
img = drawCharacter(ui->CharacterExample->text(),10,40);
ui->Example2->setPixmap(QPixmap::fromImage(img));
img = drawCharacter(ui->CharacterExample->text(),10,40);
ui->Example3->setPixmap(QPixmap::fromImage(img));
img = drawCharacter(ui->CharacterExample->text(),10,40);
ui->Example4->setPixmap(QPixmap::fromImage(img));
img = drawCharacter(ui->CharacterExample->text(),10,40);
ui->Example5->setPixmap(QPixmap::fromImage(img));
img = drawCharacter(ui->CharacterExample->text(),10,40);
ui->Example6->setPixmap(QPixmap::fromImage(img));
}
void MainWindow::ifErrorDig(bool result, QString tital)
{
if(!result)
{
QMessageBox megBox;//show a messageBox to alarm
megBox.setWindowTitle(tr("Σ(っ°Д°;)っ ERROR"));
megBox.setIcon(QMessageBox::Critical);//Question,Information,Warning,Critical
megBox.setText(tital);
megBox.addButton(tr("(╯‵□′)╯︵┻━┻"),QMessageBox::YesRole);
megBox.exec();
return ;
}
}
void MainWindow::setRandSource()
{
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
}
int MainWindow::getRandnum(int mod)
{
int outnum = (qrand()%mod)-mod/2;
return outnum;
}
void MainWindow::generateOutPage()
{
//delete exist page
while(!pageList.isEmpty()){
pageList.removeLast();
}
//load backgronud and prepare
pageList.append(new QImage(":/img/BackgroundA4.png"));
QPainter painter(pageList.at(0));
//get font size
QFont font;
font.setPixelSize(ui->CharacterSizeTxt->text().toInt());
if(fontList.length()==0){
font.setFamily("宋体");
}
else{
font.setFamily(fontList.at(0).family());
}
QFontMetrics fm(font);
//ready a page map
Simplepage pageMap(pageList.at(0)->size(),
ui->topMargainTxt->text().toInt(),ui->bottemMargainTxt->text().toInt(),
ui->leftMargainTxt->text().toInt(),ui->rightMargainTxt->text().toInt(),
ui->columnSpacingTxt->text().toInt(),ui->lineSpacingTxt->text().toInt(),
fm.width("例")+ui->CharacterSpaceTxt->text().toInt(),fm.height()+ui->CharacterSpaceTxt->text().toInt());
//draw
QPixmap pix;
QImage charImage;
int iLoop = 0;
int totalMamber = ui->textEdit->document()->toPlainText().length();
for(iLoop=0;iLoop<totalMamber;iLoop++){
if(ui->textEdit->document()->toPlainText().mid(iLoop,1)=="\n")
{
pageMap.firstColumn();
pageMap.netLine();
}
else
{
charImage = drawCharacter(ui->textEdit->document()->toPlainText().mid(iLoop,1),
ui->CharacterSpaceTxt->text().toInt(),
ui->CharacterSizeTxt->text().toInt());
painter.drawPixmap(pageMap.atPixel(),QPixmap::fromImage(charImage));
//...if full
pageMap.nextPoint();
}
}
ui->PreviewLab->setPixmap(QPixmap::fromImage(*pageList.at(0)));
}
/*******************************ui botten*******************************/
void MainWindow::on_Preview_triggered()
{
generateOutPage();
pageList.at(0)->save("./output.png","png",100);
}
void MainWindow::on_openPreview_triggered()
{
QString filePath = QCoreApplication::applicationDirPath() + "/output.png";
ui->textEdit->setText(filePath);
QDesktopServices::openUrl(QUrl::fromLocalFile(filePath.toStdString().c_str()));
}
void MainWindow::on_GenerateExp_triggered()
{
generateExp();
}
void MainWindow::on_SavePage_triggered()
{
generateOutPage();
QString fileName = QFileDialog::getSaveFileName(this,tr("Save"),"./",tr("Images (*.png)"));
pageList.at(0)->save(fileName,"png",100);
}
void MainWindow::on_Ssingal_triggered()
{
QString text = ui->textEdit->toPlainText();
QString filePath = QFileDialog::getSaveFileName(this,tr("Save"),"./",tr("Images (*.png)"));
QString fileName;
int lenth=0;
int order=0;
int iloop=0;
text.remove("\n");
lenth = text.length();
for(order=0;lenth!=0;order++)
lenth/=10;
lenth = text.length();
filePath.remove(".png",Qt::CaseInsensitive);
for(iloop=0; iloop<lenth; iloop++)
{
fileName = filePath + tr("%1.png").arg(QString::number(iloop),order,10,QLatin1Char('0'));
}
}
void MainWindow::on_PenWidthSbx_editingFinished()
{
generateExp();
}
void MainWindow::on_CharacterSizeTxt_editingFinished()
{
generateExp();
}
void MainWindow::on_CharacterSpaceTxt_editingFinished()
{
generateExp();
}
void MainWindow::on_CharacterExample_editingFinished()
{
generateExp();
}
void MainWindow::on_RandOffsetTxt_editingFinished()
{
generateExp();
}
void MainWindow::on_RandPenWidthSbx_editingFinished()
{
generateExp();
}
void MainWindow::on_RandomScaleTxt_editingFinished()
{
generateExp();
}
void MainWindow::on_randRotateTxt_editingFinished()
{
generateExp();
}
void MainWindow::on_addFontBtn_clicked()
{
//record font and add fontListViw. all fontfamliy are show to user
fontList.append(ui->fontCbx->currentFont());
QListWidgetItem *item = new QListWidgetItem;
item->setText(ui->fontCbx->currentFont().family());
item->setFont(ui->fontCbx->currentFont());
ui->fontListViw->addItem(item);
}
void MainWindow::on_removeFontBtn_clicked()
{
//ensure row is between 0 to length, otherwise program will crash
int row = ui->fontListViw->currentRow();
int length = ui->fontListViw->count();
if(length ==0)
return ;
if(row<0){
row = 0;
ui->fontListViw->setCurrentRow(row);
}
else if(row>=length)
{
row = length-1;
ui->fontListViw->setCurrentRow(row);
}
//remove item and font in fontList
fontList.removeAll(ui->fontListViw->currentItem()->font());
ui->fontListViw->takeItem(ui->fontListViw->currentRow());
//set selected item
if(row==length-1)
{
ui->fontListViw->setCurrentRow(row-1);
}
}