This repository has been archived by the owner on Oct 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
canvas.cpp
1032 lines (920 loc) · 31.9 KB
/
canvas.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "canvas.h"
#include <QFileInfo>
#include <QDebug>
#include <QDateTime>
#include "operateSet.h"
#include <QTranslator>
/*
* Author:qiuzhiqian
* Email:[email protected]
* Github:https://github.com/qiuzhiqian
* Date:2017.07.23
* Description:这个类主要用来创建一个桌面的截图画布
**/
//考虑继承QLabel
Canvas::Canvas(QWidget *parent) : QWidget(parent)
{
okFlag=1;
const QRect screenGeometry = QApplication::desktop()->screenGeometry(this);
screen_width=screenGeometry.width();
screen_height=screenGeometry.height();
//this->setWindowFlags(Qt::WindowStaysOnTopHint); //窗口置顶
this->raise(); //软置顶
this->showFullScreen(); //画布全屏显示
clipboard = QApplication::clipboard(); //获取系统剪贴板指针
setMouseTracking(true); //鼠标移动捕捉
canvasInit();
initToolBar();
drawEditFlag=0;
}
void Canvas::canvasInit()
{
pointS.rx()=0;
pointS.ry()=0;
pointE.rx()=0;
pointE.ry()=0;
pointDrag.rx()=0;
pointDrag.ry()=0;
rectFlag=DrawStatus::waitDraw;
lineList.clear();
rectList.clear();
ellipseList.clear();
textList.clear();
drawPen.setBrush(Qt::red);
drawPen.setWidthF(2);
drawPen.setStyle(Qt::SolidLine);
}
void Canvas::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
if(rectFlag==DrawStatus::waitDraw)
{
pointS.rx()=event->x();
pointS.ry()=event->y();
pointE.rx()=event->x();
pointE.ry()=event->y();
shotArea=getRectF(pointS,pointE);
rectFlag=DrawStatus::drawing;
}
else if(rectFlag==DrawStatus::drawed) //捕捉拖拽
{
if(drawEditFlag==0)
{
pointDrag.setX(event->x());
pointDrag.setY(event->y());
}
else if(drawEditFlag==1) //画线
{
pointS.rx()=event->x();
pointS.ry()=event->y();
pointE.rx()=event->x();
pointE.ry()=event->y();
LinePaint tempLine(pointS.toPoint(),pointE.toPoint());
//qDebug()<<"drawPen";
tempLine.setPen(drawPen);
lineList.append(tempLine);
}
else if(drawEditFlag==2) //画矩形
{
pointS.rx()=event->x();
pointS.ry()=event->y();
pointE.rx()=event->x();
pointE.ry()=event->y();
RectPaint tempRect=getRectF(pointS,pointE);
tempRect.setPen(drawPen);
rectList.append(tempRect);
}
else if(drawEditFlag==3) //画椭圆
{
pointS.rx()=event->x();
pointS.ry()=event->y();
pointE.rx()=event->x();
pointE.ry()=event->y();
RectPaint tempRect=getRectF(pointS,pointE);
tempRect.setPen(drawPen);
ellipseList.append(tempRect);
}
else if(drawEditFlag==4) //添加文本
{
int x=event->x();
int y=event->y();
TextPaint *curText=new TextPaint(this);
int cnt=textList.length();
for(int i=0;i<cnt;i++)
{
textList.at(i)->clearFocus();
}
//QFont tempFont("Timers",32,QFont::Black);
QFontMetrics fm(textfont);
curText->setGeometry(x,y,10,fm.height()+2);
curText->setFont(textfont);
QPalette palette;
//palette.setColor(QPalette::Text,Qt::red);
palette.setColor(QPalette::Text,textcolor);
curText->setPalette(palette);
curText->setVisible(true);
textList.append(curText);
curText->setFocus();
QWidget::mousePressEvent(event);
update();
}
}
}
else if(event->button()==Qt::RightButton) //重新绘制区域
{
setCursor(Qt::ArrowCursor);
if(rectFlag==DrawStatus::waitDraw)
{
slt_cancel();
}
else
{
canvasInit();
hideToolBar();
update();
}
}
}
void Canvas::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons()&Qt::LeftButton)
{
if(rectFlag==DrawStatus::drawing)
{
pointE.setX(event->x());
pointE.setY(event->y());
shotArea=getRectF(pointS,pointE);
}
else if(rectFlag==DrawStatus::drawed) //拖拽
{
QPointF tempTL,tempBR;
tempTL=shotArea.topLeft();
tempBR=shotArea.bottomRight();
switch(cursorCaptureFlag)
{
case 0: //无效区域
setCursor(Qt::ArrowCursor);
break;
case 1: //左上
setCursor(Qt::SizeFDiagCursor);
tempTL.setX(event->x());
tempTL.setY(event->y());
break;
case 2: //左下
setCursor(Qt::SizeBDiagCursor);
tempTL.setX(event->x());
tempBR.setY(event->y());
break;
case 3: //左边
setCursor(Qt::SizeHorCursor);
tempTL.setX(event->x());
break;
case 4: //右上
setCursor(Qt::SizeBDiagCursor);
tempBR.setX(event->x());
tempTL.setY(event->y());
break;
case 5: //右下
setCursor(Qt::SizeFDiagCursor);
tempBR.setX(event->x());
tempBR.setY(event->y());
break;
case 6: //右边
setCursor(Qt::SizeHorCursor);
tempBR.setX(event->x());
shotArea=getRectF(pointS,pointE);
break;
case 7: //上边
setCursor(Qt::SizeVerCursor);
tempTL.setY(event->y());
break;
case 8: //下边
setCursor(Qt::SizeVerCursor);
tempBR.setY(event->y());
break;
case 9: //中央
if(drawEditFlag==0)
{
setCursor(Qt::SizeAllCursor);
qreal dx=event->x()-pointDrag.x(); //获取坐标差
qreal dy=event->y()-pointDrag.y();
pointDrag.setX(event->x()); //刷新拖拽点坐标
pointDrag.setY(event->y());
if( (tempTL.x()+dx)>0 && (tempBR.x()+dx)<screen_width )
{
tempTL.rx()+=dx;
tempBR.rx()+=dx;
}
if( (tempTL.y()+dy)>0 && (tempBR.y()+dy)<screen_height )
{
tempTL.ry()+=dy;
tempBR.ry()+=dy;
}
}
else if(drawEditFlag==1) //绘线
{
pointE.setX(event->x());
pointE.setY(event->y());
lineList.last().setPoints(pointS.toPoint(),pointE.toPoint());
}
else if(drawEditFlag==2) //绘矩形
{
pointE.setX(event->x());
pointE.setY(event->y());
rectList.last().setTopLeft(pointS);
rectList.last().setBottomRight(pointE);
}
else if(drawEditFlag==3) //绘椭圆
{
pointE.setX(event->x());
pointE.setY(event->y());
ellipseList.last().setTopLeft(pointS);
ellipseList.last().setBottomRight(pointE);
}
break;
}
shotArea.setTopLeft(tempTL);
shotArea.setBottomRight(tempBR);
showToolBar(); //实时更新工具条位置
}
update();
}
else if(event->buttons()==Qt::NoButton) //没有按键按下
{
if(rectFlag==DrawStatus::drawed) //捕捉拖拽
{
cursorCaptureFlag=caputerRect(shotArea,event->x(),event->y());
switch(cursorCaptureFlag)
{
case 0: //无效区域
setCursor(Qt::ArrowCursor);
break;
case 1: //左上
setCursor(Qt::SizeFDiagCursor);
break;
case 2: //左下
setCursor(Qt::SizeBDiagCursor);
break;
case 3: //左边
setCursor(Qt::SizeHorCursor);
break;
case 4: //右上
setCursor(Qt::SizeBDiagCursor);
break;
case 5: //右下
setCursor(Qt::SizeFDiagCursor);
break;
case 6: //右边
setCursor(Qt::SizeHorCursor);
break;
case 7: //上边
setCursor(Qt::SizeVerCursor);
break;
case 8: //下边
setCursor(Qt::SizeVerCursor);
break;
case 9: //中央
if(drawEditFlag==0)
{
setCursor(Qt::SizeAllCursor);
}
else //追加图形
{
setCursor(Qt::ArrowCursor);
}
break;
}
}
}
}
void Canvas::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
if(rectFlag==DrawStatus::drawing)
{
pointE.setX(event->x());
pointE.setY(event->y());
shotArea=getRectF(pointS,pointE);
rectFlag=DrawStatus::drawed; //矩形绘制完成
qDebug()<<"draw end.";
showToolBar();
}
else if(rectFlag==DrawStatus::drawed)
{
if(drawEditFlag==0)
{
showToolBar();
}
else if(drawEditFlag==1)
{
if( (pointS.x()==pointE.x()) && (pointS.y()==pointE.y()) )//直线长度为0
{
lineList.removeLast();
}
}
else if(drawEditFlag==2)
{
if( (pointS.x()==pointE.x()) && (pointS.y()==pointE.y()) )//矩形区域为空
{
rectList.removeLast();
}
}
else if(drawEditFlag==3)
{
if( (pointS.x()==pointE.x()) && (pointS.y()==pointE.y()) )//矩形区域为空
{
ellipseList.removeLast();
}
}
}
update();
}
}
void Canvas::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
QPixmap tempmask(screen_width, screen_width);
tempmask.fill((QColor(0, 0, 0, 160)));
painter.drawPixmap(0,0,fullPixmap); //先绘制全屏原图背景
painter.drawPixmap(0,0,tempmask); //然后绘制半透明背景,用来降低亮度
switch(rectFlag) //截图状态机
{
case DrawStatus::waitDraw:
{
break;
}
case DrawStatus::drawing:
{
painter.setPen(QPen(Qt::green,2,Qt::DashLine));//设置画笔形式
//painter.setBrush(Qt::white);
painter.drawRect(shotArea); //然后绘制矩形框
fullPixmap.setDevicePixelRatio(2.0);
QScreen *screen = QGuiApplication::primaryScreen();
RectPaint shotAreaOnImage;
shotAreaOnImage.setX(shotArea.x()*screen->orientation());
shotAreaOnImage.setY(shotArea.y()*screen->orientation());
shotAreaOnImage.setWidth(shotArea.width()*screen->orientation());
shotAreaOnImage.setHeight(shotArea.height()*screen->orientation());
painter.drawPixmap(shotArea,fullPixmap,shotAreaOnImage); //然后将矩形框中的半透明图像替换成原图
break;
}
case DrawStatus::drawed:
{
painter.setPen(QPen(Qt::green,2,Qt::DashLine));//设置画笔形式
//painter.setBrush(Qt::white);
QScreen *screen = QGuiApplication::primaryScreen();
RectPaint shotAreaOnImage;
shotAreaOnImage.setX(shotArea.x()*screen->orientation());
shotAreaOnImage.setY(shotArea.y()*screen->orientation());
shotAreaOnImage.setWidth(shotArea.width()*screen->orientation());
shotAreaOnImage.setHeight(shotArea.height()*screen->orientation());
painter.drawRect(shotArea); //然后绘制矩形框
painter.drawPixmap(shotArea,fullPixmap,shotAreaOnImage); //然后将矩形框中的半透明图像替换成原图
break;
}
default:
{
break;
}
}
quint16 len=lineList.length();
if(len)
{
for(quint16 i=0;i<len;i++)
{
painter.setPen(lineList[i].getPen());//设置画笔形式
painter.drawLine(lineList[i]); //然后绘制矩形框
}
}
len=rectList.length();
if(len)
{
for(quint16 i=0;i<len;i++)
{
painter.setPen(rectList[i].getPen());//设置画笔形式
painter.drawRect(rectList[i]); //然后绘制矩形框
}
}
len=ellipseList.length();
if(len)
{
for(quint16 i=0;i<len;i++)
{
painter.setPen(ellipseList[i].getPen());//设置画笔形式
painter.drawEllipse(ellipseList[i]); //然后绘制矩形框
}
}
//len=textList.length();
//if(len)
//{
// for(quint16 i=0;i<len;i++)
// {
// painter.setPen(ellipseList[i].getPen());//设置画笔形式
// painter.drawEllipse(ellipseList[i]); //然后绘制矩形框
// }
//}
QWidget::paintEvent(e);
}
void Canvas::setbgPixmap(QPixmap &px)
{
fullPixmap=px;
}
void Canvas::shootScreen(QRectF &rect) //截图函数
{
QScreen *screen = QGuiApplication::primaryScreen();
originalPixmap = screen->grabWindow(0,rect.x(),rect.y(),rect.width(),rect.height());
}
void Canvas::initToolBar() //工具条初始化
{
toolbar=new QWidget(this);
QWidget *MainToolBar=new QWidget(); //主工具栏
QHBoxLayout *mainToolLayout=new QHBoxLayout();
btn_cancel=new QPushButton(tr("Quit"));
btn_saveClipboard=new QPushButton(tr("Copy"));
btn_saveFile=new QPushButton(tr("Save"));
btn_drawLine=new QPushButton(tr("Line"));
btn_drawRect=new QPushButton(tr("Rect"));
btn_drawEllipse=new QPushButton(tr("Ellipse"));
btn_drawText=new QPushButton(tr("Text"));
btn_cancel->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_saveClipboard->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_saveFile->setStyleSheet("background-color: rgb(255, 255, 255);");
mainToolLayout->addWidget(btn_drawLine);
mainToolLayout->addWidget(btn_drawRect);
mainToolLayout->addWidget(btn_drawEllipse);
mainToolLayout->addWidget(btn_drawText);
mainToolLayout->addWidget(btn_cancel);
mainToolLayout->addWidget(btn_saveClipboard);
mainToolLayout->addWidget(btn_saveFile);
mainToolLayout->setContentsMargins(0,0,0,0); //去除边框间隙
mainToolLayout->setSpacing(0);
MainToolBar->setLayout(mainToolLayout);
shapeToolBar=new QWidget(); //形状拓展工具条
QHBoxLayout *shapeToolLayout=new QHBoxLayout();
textToolBar=new QWidget(); //文本拓展工具条
QHBoxLayout *textToolLayout=new QHBoxLayout();
cbx_lineSize=new QComboBox();
btn_colorSelect=new QPushButton();
cbx_lineStyle=new QComboBox();
QStringList sizeitems;
sizeitems<<"1"<<"2"<<"3"<<"4"<<"5"<<"6"<<"7"<<"8"<<"9"<<"10"<<"11"<<"12"<<"13"<<"14"<<"15"<<"16"<<"17"<<"18"<<"19"<<"20";
cbx_lineSize->addItems(sizeitems);
btn_colorSelect->setStyleSheet("background-color: rgb(255, 0, 0);");
cbx_lineStyle->addItem(QIcon(":/pic/1.ico"),"");
cbx_lineStyle->addItem(QIcon(":/pic/2.ico"),"");
cbx_lineStyle->addItem(QIcon(":/pic/3.ico"),"");
cbx_lineStyle->addItem(QIcon(":/pic/4.ico"),"");
cbx_lineStyle->addItem(QIcon(":/pic/5.ico"),"");
cbx_lineStyle->setIconSize(QSize(80,20));
shapeToolLayout->addWidget(cbx_lineSize);
shapeToolLayout->addWidget(btn_colorSelect);
shapeToolLayout->addWidget(cbx_lineStyle);
shapeToolLayout->addStretch();
shapeToolLayout->setContentsMargins(0,0,0,0); //去除边框间隙
shapeToolLayout->setSpacing(0);
shapeToolBar->setLayout(shapeToolLayout);
shapeToolBar->setVisible(false);
//初始化拓展工具的初始状态
cbx_lineSize->setCurrentText(QString::number(drawPen.width()));
QColor color=drawPen.brush().color();
QString colorStyle=QString("background-color: rgb(%1, %2, %3);").arg(color.red()).arg(color.green()).arg(color.blue());
btn_colorSelect->setStyleSheet(colorStyle);
switch(drawPen.style())
{
case Qt::SolidLine:
cbx_lineStyle->setCurrentIndex(0);
break;
case Qt::DashLine:
cbx_lineStyle->setCurrentIndex(1);
break;
case Qt::DotLine:
cbx_lineStyle->setCurrentIndex(2);
break;
case Qt::DashDotLine:
cbx_lineStyle->setCurrentIndex(3);
break;
case Qt::DashDotDotLine:
cbx_lineStyle->setCurrentIndex(4);
break;
default:
cbx_lineStyle->setCurrentIndex(0);
break;
}
btn_colorText = new QPushButton();
btn_FontText = new QPushButton(tr("Font:10"));
textcolor = QColor(Qt::red);
textfont = QFont("Times", 10, QFont::Bold);
colorStyle=QString("background-color: rgb(%1, %2, %3);").arg(textcolor.red()).arg(textcolor.green()).arg(textcolor.blue());
btn_colorText->setStyleSheet(colorStyle);
btn_FontText->setFont(textfont);
textToolLayout->addWidget(btn_colorText);
textToolLayout->addWidget(btn_FontText);
textToolLayout->addStretch();
textToolLayout->setContentsMargins(0,0,0,0); //去除边框间隙
textToolLayout->setSpacing(0);
textToolBar = new QWidget(this);
textToolBar->setLayout(textToolLayout);
textToolBar->setVisible(false);
QVBoxLayout *toolLayout=new QVBoxLayout();
toolLayout->addWidget(MainToolBar);
toolLayout->addWidget(shapeToolBar);
toolLayout->addWidget(textToolBar);
toolLayout->setContentsMargins(0,0,0,0); //去除边框间隙
toolLayout->setSpacing(0);
toolbar->setLayout(toolLayout);
toolbar->setVisible(true); //如果不显示的话,后面的width和height会计算不准
toolBarWidth = toolbar->width();
toolBarHeight = toolbar->height();
//qDebug()<<"W:"<<toolBarWidth;
//qDebug()<<"H:"<<toolBarHeight;
toolbar->setVisible(false);
btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);");
hideToolBar();
connect(btn_drawLine,SIGNAL(clicked(bool)),this,SLOT(slt_drawLine()));
connect(btn_drawRect,SIGNAL(clicked(bool)),this,SLOT(slt_drawRect()));
connect(btn_drawEllipse,SIGNAL(clicked(bool)),this,SLOT(slt_drawEllipse()));
connect(btn_drawText,SIGNAL(clicked(bool)),this,SLOT(slt_drawText()));
connect(btn_cancel,SIGNAL(clicked(bool)),this,SLOT(slt_cancel()));
connect(btn_saveClipboard,SIGNAL(clicked(bool)),this,SLOT(slt_saveClipboard()));
connect(btn_saveFile,SIGNAL(clicked(bool)),this,SLOT(slt_saveFile()));
//拓展工具信号槽
connect(cbx_lineSize,SIGNAL(currentTextChanged(QString)),this,SLOT(slt_changePenWidth(QString)));
connect(btn_colorSelect,SIGNAL(clicked(bool)),this,SLOT(slt_changePenColor()));
connect(cbx_lineStyle,SIGNAL(currentIndexChanged(int)),SLOT(slt_changePenStyle(int)));
connect(btn_colorText,SIGNAL(clicked(bool)),this,SLOT(slt_changeTextColor()));
connect(btn_FontText,SIGNAL(clicked(bool)),SLOT(slt_changeTextFont()));
}
void Canvas::showToolBar() //显示工具条
{
qreal x,y;
int bar_width=toolBarWidth;//toolbar->width();
int bar_height=toolBarHeight;//toolbar->height();
int offset=5;
qDebug()<<"bottomLeft().x"<<shotArea.bottomLeft().x();
qDebug()<<"bar_width"<<bar_width;
qDebug()<<"offset"<<offset;
qDebug()<<"screen_width"<<screen_width;
if(shotArea.bottomLeft().x()+bar_width+offset<screen_width) //x轴方向边距足够
{
x=shotArea.bottomLeft().x()+offset;
}
else //x轴方向边距不足
{
qDebug()<<"no more x.";
x=screen_width-(bar_width+offset);
}
if(screen_height-shotArea.bottomLeft().y()>(bar_height+offset)) //下边距充足
{
y=shotArea.bottomLeft().y()+offset;
}
else if(shotArea.topLeft().y()>(bar_height+offset)) //上边距充足
{
y=shotArea.topLeft().y()-(bar_height+offset);
}
else if(shotArea.topLeft().y()<0) //上下边距都不够,且上边距在桌面外
{
y=offset;
}
else //上下边距都不够,且上边距在桌面内
{
y=shotArea.topLeft().y()+offset;
}
toolbar->move(x,y);
toolbar->setVisible(true);
}
void Canvas::hideToolBar() //掩藏工具条
{
toolbar->setVisible(false);
}
void Canvas::refrashToolBar()
{
btn_drawLine->setText(tr("Line"));
btn_drawRect->setText(tr("Rect"));
btn_drawEllipse->setText(tr("Ellipse"));
btn_drawText->setText(tr("Text"));
btn_cancel->setText(tr("Quit"));
btn_saveClipboard->setText(tr("Copy"));
btn_saveFile->setText(tr("Save"));
}
void Canvas::slt_drawLine()
{
if(drawEditFlag!=1)
{
drawEditFlag=1;
btn_drawLine->setStyleSheet("background-color: rgb(146, 189, 108);");
btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);");
shapeToolBar->setVisible(true);
textToolBar->setVisible(false);
toolbar->adjustSize();
showToolBar();
}
else
{
drawEditFlag=0;
btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);");
shapeToolBar->setVisible(false);
textToolBar->setVisible(false);
toolbar->adjustSize();
showToolBar();
}
}
void Canvas::slt_drawRect()
{
if(drawEditFlag!=2)
{
drawEditFlag=2;
btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawRect->setStyleSheet("background-color: rgb(146, 189, 108);");
btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);");
shapeToolBar->setVisible(true);
textToolBar->setVisible(false);
toolbar->adjustSize();
showToolBar();
}
else
{
drawEditFlag=0;
btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);");
shapeToolBar->setVisible(false);
textToolBar->setVisible(false);
toolbar->adjustSize();
showToolBar();
}
}
void Canvas::slt_drawEllipse()
{
if(drawEditFlag!=3)
{
drawEditFlag=3;
btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawEllipse->setStyleSheet("background-color: rgb(146, 189, 108);");
btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);");
shapeToolBar->setVisible(true);
textToolBar->setVisible(false);
toolbar->adjustSize();
showToolBar();
}
else
{
drawEditFlag=0;
btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);");
shapeToolBar->setVisible(false);
textToolBar->setVisible(false);
toolbar->adjustSize();
showToolBar();
}
}
void Canvas::slt_drawText()
{
if(drawEditFlag!=4)
{
drawEditFlag=4;
btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawText->setStyleSheet("background-color: rgb(146, 189, 108);");
shapeToolBar->setVisible(false);
textToolBar->setVisible(true);
toolbar->adjustSize();
showToolBar();
}
else
{
drawEditFlag=0;
btn_drawLine->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawRect->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawEllipse->setStyleSheet("background-color: rgb(255, 255, 255);");
btn_drawText->setStyleSheet("background-color: rgb(255, 255, 255);");
shapeToolBar->setVisible(false);
textToolBar->setVisible(false);
toolbar->adjustSize();
showToolBar();
}
}
void Canvas::slt_saveFile()
{
shootScreen(shotArea);
QDateTime current_date_time = QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyyMMdd_hhmmsszzz");
QString savefile="ScreenShot_"+current_date+".jpg";
QString savepath=OperateSet::readSetting("Setting","QuickSaveDir",".").toString();
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save File"),
savepath+"/"+savefile,
tr("JPEG File (*.jpg);;BMP File (*.bmp);;PNG File (*.png);;PPM File (*.ppm);;XBM File (*.xbm);;XPM File (*.xpm)")
);
originalPixmap.save(fileName);
QFileInfo fi = QFileInfo(fileName);
//QString file_name = fi.fileName();
QString file_path = fi.absolutePath();
if(file_path!=savepath)
{
OperateSet::writeSetting("Setting","QuickSaveDir",file_path);
}
slt_cancel();
}
void Canvas::slt_saveClipboard()
{
shootScreen(shotArea);
clipboard->setPixmap(originalPixmap);
slt_cancel();
}
void Canvas::slt_cancel()
{
rectFlag=DrawStatus::waitDraw;
this->close();
}
void Canvas::slt_changePenWidth(QString s)
{
drawPen.setWidth(s.toInt());
}
void Canvas::slt_changePenColor()
{
QColor color = QColorDialog::getColor(Qt::blue);
if(color.isValid())
{
//如果颜色有效的话,将colorFrame设置为选中的颜色
//colorFrame->setPalette(QPalette(color));
//btn_colorSelect->setPalette(QPalette(color));
QString colorStyle=QString("background-color: rgb(%1, %2, %3);").arg(color.red()).arg(color.green()).arg(color.blue());
btn_colorSelect->setStyleSheet(colorStyle);
}
drawPen.setColor(color);
}
void Canvas::slt_changePenStyle(int index)
{
switch(index)
{
case 0:
drawPen.setStyle(Qt::SolidLine);
break;
case 1:
drawPen.setStyle(Qt::DashLine);
break;
case 2:
drawPen.setStyle(Qt::DotLine);
break;
case 3:
drawPen.setStyle(Qt::DashDotLine);
break;
case 4:
drawPen.setStyle(Qt::DashDotDotLine);
break;
default:
drawPen.setStyle(Qt::SolidLine);
break;
}
}
void Canvas::slt_changeTextColor()
{
QColor color = QColorDialog::getColor(Qt::blue);
if(color.isValid())
{
textcolor = color;
QString colorStyle=QString("background-color: rgb(%1, %2, %3);").arg(textcolor.red()).arg(textcolor.green()).arg(textcolor.blue());
btn_colorText->setStyleSheet(colorStyle);
}
}
void Canvas::slt_changeTextFont()
{
bool bRet = true;
QFont font = QFontDialog::getFont(&bRet);
if(bRet)
{
textfont = font;
font.setPointSize(10);
btn_FontText->setFont(font);
btn_FontText->setText("Font"+QString::number(textfont.pointSize()));
}
}
//通过任意两点构造一个矩形
RectPaint Canvas::getRectF(QPointF p1, QPointF p2)
{
float x1,y1,x2,y2;
if(p1.x()<p2.x())
{
x1=p1.x();
x2=p2.x();
}
else
{
x1=p2.x();
x2=p1.x();
}
if(p1.y()<p2.y())
{
y1=p1.y();
y2=p2.y();
}
else
{
y1=p2.y();
y2=p1.y();
}
QPointF ps(x1,y1);
QPointF pe(x2,y2);
RectPaint rect(ps,pe);
return rect;
}
void Canvas::changeLanguage(QString lan)
{
QTranslator translator;
bool sta=false;
if(lan=="zh_cn") //简体中文
{
sta=translator.load("zh_cn.qm");
}
else if(lan=="en")
{
sta=translator.load("en.qm");
}
if(sta)
{
QApplication::installTranslator(&translator);
refrashToolBar(); //刷新自定义语言翻译
}
}
quint8 Canvas::caputerRect(QRectF t_rect,qreal t_x,qreal t_y)
{
quint8 pos=0;
if(qAbs(t_x-t_rect.topLeft().x())<2) //左
{
if(qAbs(t_y-t_rect.topLeft().y())<2) //上
{
pos=1;
}
else if(qAbs(t_y-t_rect.bottomRight().y())<2) //下
{
pos=2;
}
else if( (t_y>=t_rect.topLeft().y()+2) && (t_y<=t_rect.bottomRight().y()-2) ) //上下之间
{
pos=3;
}
else
{
pos=0;
}
}
else if(qAbs(t_x-t_rect.bottomRight().x())<2) //右
{
if(qAbs(t_y-t_rect.topLeft().y())<2) //上
{
pos=4;
}
else if(qAbs(t_y-t_rect.bottomRight().y())<2) //下
{
pos=5;
}
else if( (t_y>=t_rect.topLeft().y()+2) && (t_y<=t_rect.bottomRight().y()-2) ) //上下之间
{