-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1497 lines (1468 loc) · 72.7 KB
/
main.cpp
File metadata and controls
1497 lines (1468 loc) · 72.7 KB
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 <QApplication>
#include <QPushButton>
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QRadioButton>
#include <QMessageBox>
#include <QLineEdit>
#include <QValidator>
#include <QPainter>
#include <QScreen>
#include <QGridLayout>
#include <QPointF>
#include <string>
#include <sstream>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include <QtWidgets>
#include <QMouseEvent>
#include <QDoubleValidator>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <cmath>
#include <QListWidget>
#include <QListWidgetItem>
QGraphicsLineItem BG;//背景线,用于防止画布自适应
const int maxsize=100;//可以根据需要来修改每一种图形集合最大允许的存放容量
//【各种参数】
//(图形参数)
//直线
double sx,sy,fx,fy;
//圆
double cx,cy,r;
//椭圆
double sEx,sEy,fEx,fEy;
//正方形
double cSx,cSy,a;
//长方形
double sRx,sRy,fRx,fRy;
//三角形
double p1x,p1y,p2x,p2y,p3x,p3y;
//(变换参数)
//平移
double Move_x; double Move_y;
//缩放
double Scaler;
//旋转
double thita;
//颜色
int Red=0, Green=0, Blue=0;
//命令开关(鼠标操作)
bool ClicktoDrawLine = false;
bool ClicktoDrawCircle = false;
bool ClicktoDrawEllipse = false;
bool ClicktoDrawSquare = false;
bool ClicktoDrawRectangle = false;
bool ClicktoDrawTriangle = false;
int CLICK_TIMES=0;
bool ClicktoSetColor = false;
bool Pen = false; bool Brush = false;
bool setcolor = false;
bool PY = false; bool SF = false; bool XZ = false;
bool Drag_to_Move = false; bool Drag_to_Zoom = false; bool Drag_to_Revolve = false;
bool Select_Item = false;
//创建元素列表
QGraphicsLineItem line[maxsize];
QGraphicsEllipseItem circle[maxsize];
QGraphicsEllipseItem ellipse[maxsize];
QGraphicsRectItem square[maxsize];
QGraphicsRectItem rectangle[maxsize];
QGraphicsPolygonItem triangle[maxsize];
QGraphicsItemGroup Elements_List[maxsize*6];
int line_amount=0,circle_amount=0,ellipse_amount=0,square_amount=0,rectangle_amount=0,triangle_amount=0;
int current_item_in_my_list=0;
int total_items_in_my_list=0;
//可以绘图的画布
class MAP : public QWidget {
public:
MAP(QWidget *parent = nullptr) : QWidget(parent) {
view = new QGraphicsView(this);
// 将QGraphicsScene与QGraphicsView绑定
view->setScene(&scene);
// 这个画布可以一直追踪鼠标
setMouseTracking(true);
}
//把不能直接调用的函数抓出来,以便在主函数中调用
void addShape(QGraphicsItem *shape) {
scene.addItem(shape);
}
void clearShape(QGraphicsItem *shape) {
scene.removeItem(shape);
}
//连接主函数
void setWindowToRaise(QWidget *windowToRaise){
window2 = windowToRaise;
}
protected:
void paintEvent(QPaintEvent *event) override {
//建立数学场景
Q_UNUSED(event);
QPainter painter(this);
//将场景中的元素显示出来
scene.render(&painter);
//防止显示自适应
QScreen *primaryScreen = QGuiApplication::primaryScreen();
QRect screenRect = primaryScreen->geometry();
BG.setLine(0, 0, screenRect.width(), screenRect.height());
BG.setPen(QPen(palette().window().color()));
BG.setZValue(0);
scene.addItem(&BG);
update();
}
//按下鼠标
QList<QGraphicsItem*> save;//用于存储变换中涉及的元素的中间变量
QPointF Drag_Start_Position,Drag_End_Position;//用于存储拖动鼠标坐标的中间变量
void mousePressEvent(QMouseEvent *Event) override{
//单击map时却把w提到窗口们的最前面
if (Event->button() == Qt::LeftButton||Event->button() == Qt::RightButton)
{
if (window2)
{
window2->raise();
}
}
QWidget::mousePressEvent(Event);
if (ClicktoDrawLine){
//画线
if (Event->button() == Qt::LeftButton){
//记录线段起点的坐标
sx = static_cast<double>(Event->pos().x());
sy = static_cast<double>(Event->pos().y());
}
}
else if(ClicktoDrawCircle){
//画圆
if (Event->button() == Qt::LeftButton){
//求圆心
cx = static_cast<double>(Event->pos().x());
cy = static_cast<double>(Event->pos().y());
}
}
else if (ClicktoDrawEllipse){
//画椭圆
if (Event->button() == Qt::LeftButton){
//记录椭圆外切矩阵的左上顶点
sEx = static_cast<double>(Event->pos().x());
sEy = static_cast<double>(Event->pos().y());
}
}
else if (ClicktoDrawSquare){
//画正方形
if (Event->button() == Qt::LeftButton){
//记录正方形的中心
cSx = static_cast<double>(Event->pos().x());
cSy = static_cast<double>(Event->pos().y());
}
}
else if (ClicktoDrawRectangle){
//画长方形
if (Event->button() == Qt::LeftButton){
//记录椭圆外切矩阵的左上顶点
sRx = static_cast<double>(Event->pos().x());
sRy = static_cast<double>(Event->pos().y());
}
}
else if (ClicktoDrawTriangle){
//画三角形
if (Event->button() == Qt::LeftButton){
//分别记录三次单击的顶点坐标
CLICK_TIMES++;
if (CLICK_TIMES==1){
p1x = static_cast<double>(Event->pos().x());
p1y = static_cast<double>(Event->pos().y());
}
else if (CLICK_TIMES==2){
p2x = static_cast<double>(Event->pos().x());
p2y = static_cast<double>(Event->pos().y());
}
else if (CLICK_TIMES==3){
p3x = static_cast<double>(Event->pos().x());
p3y = static_cast<double>(Event->pos().y());
//创建三角形元素,颜色初始化
QPolygonF trianglePolygon;
trianglePolygon << QPointF(p1x,p1y) << QPointF(p2x,p2y) << QPointF(p3x,p3y);
triangle[triangle_amount].setPolygon(trianglePolygon);
triangle[triangle_amount].setPen(QPen(QColor(0,0,0)));
triangle[triangle_amount].setBrush(QBrush(QColor(255,255,255)));
//构建完直线元素之后要把它搬到画布上
Elements_List->addToGroup(&triangle[triangle_amount]);
//刷新绘图
update();
triangle_amount++;
ClicktoDrawTriangle = false;
CLICK_TIMES=0;
}
}
}
else if (ClicktoSetColor){
// 将鼠标坐标转换为场景坐标
QPointF scenePos = view->mapToScene(Event->pos());
// 使用scene中的items方法获取位于鼠标位置的所有元素
QList<QGraphicsItem*> itemsAtMousePos = scene.items(scenePos);
// 遍历所有位于鼠标位置的元素
for (QGraphicsItem* item : itemsAtMousePos) {
// 修改选中元素的颜色
if (Pen){
//利用索引找到对应元素,更改对应元素的颜色
if (item->type() == QGraphicsLineItem::Type) {
QGraphicsLineItem *lineItem = qgraphicsitem_cast<QGraphicsLineItem*>(item);
lineItem->setPen(QColor(Red, Green, Blue));
} else if (item->type() == QGraphicsEllipseItem::Type) {
QGraphicsEllipseItem *ellipseItem = qgraphicsitem_cast<QGraphicsEllipseItem*>(item);
ellipseItem->setPen(QColor(Red, Green, Blue));
} else if (item->type() == QGraphicsRectItem::Type) {
QGraphicsRectItem *rectangleItem = qgraphicsitem_cast<QGraphicsRectItem*>(item);
rectangleItem->setPen(QColor(Red, Green, Blue));
} else if (item->type() == QGraphicsPolygonItem::Type) {
QGraphicsPolygonItem *triangleItem = qgraphicsitem_cast<QGraphicsPolygonItem*>(item);
triangleItem->setPen(QColor(Red, Green, Blue));
}
}
else if (Brush){
//利用索引找到对应元素,更改对应元素的颜色
if (item->type() == QGraphicsEllipseItem::Type) {
QGraphicsEllipseItem *ellipseItem = qgraphicsitem_cast<QGraphicsEllipseItem*>(item);
ellipseItem->setBrush(QColor(Red, Green, Blue));
} else if (item->type() == QGraphicsRectItem::Type) {
QGraphicsRectItem *rectangleItem = qgraphicsitem_cast<QGraphicsRectItem*>(item);
rectangleItem->setBrush(QColor(Red, Green, Blue));
} else if (item->type() == QGraphicsPolygonItem::Type) {
QGraphicsPolygonItem *triangleItem = qgraphicsitem_cast<QGraphicsPolygonItem*>(item);
triangleItem->setBrush(QColor(Red, Green, Blue));
}
}
}
Pen = false;
Brush = false;
ClicktoSetColor = false;
setcolor = false;
}
else if (Drag_to_Move){
if (Select_Item){
//选中元素的命令
// 将鼠标坐标转换为场景坐标
QPointF scenePos = view->mapToScene(Event->pos());
// 使用scene中的items方法获取位于鼠标位置的所有元素
QList<QGraphicsItem*> itemsAtMousePos = scene.items(scenePos);
save = itemsAtMousePos;
}
else if (!Select_Item){
//记录鼠标的起点坐标
Drag_Start_Position = view->mapToScene(Event->pos());
}
}
else if (Drag_to_Zoom){
if (Select_Item){
//选中元素的命令
// 将鼠标坐标转换为场景坐标
QPointF scenePos = view->mapToScene(Event->pos());
// 使用scene中的items方法获取位于鼠标位置的所有元素
QList<QGraphicsItem*> itemsAtMousePos = scene.items(scenePos);
save = itemsAtMousePos;
}
else if (!Select_Item){
//记录鼠标的起点坐标
Drag_Start_Position = view->mapToScene(Event->pos());
}
}
else if (Drag_to_Revolve){
if (Select_Item){
//选中元素的命令
// 将鼠标坐标转换为场景坐标
QPointF scenePos = view->mapToScene(Event->pos());
// 使用scene中的items方法获取位于鼠标位置的所有元素
QList<QGraphicsItem*> itemsAtMousePos = scene.items(scenePos);
save = itemsAtMousePos;
}
else if (!Select_Item){
//记录鼠标的起点坐标
Drag_Start_Position = view->mapToScene(Event->pos());
}
}
}
//松开鼠标
void mouseReleaseEvent(QMouseEvent *Event) override {
if (ClicktoDrawLine){
//画线
if (Event->button() == Qt::LeftButton){
//记录线段终点的坐标
fx = static_cast<double>(Event->pos().x());
fy = static_cast<double>(Event->pos().y());
//创建直线元素,设置起点与终点,颜色初始化
line[line_amount].setLine(sx, sy, fx, fy);
line[line_amount].setPen(QPen(QColor(0,0,0)));
//构建完直线元素之后要把它搬到画布上
Elements_List->addToGroup(&line[line_amount]);
//刷新绘图
update();
line_amount++;
ClicktoDrawLine = false;
}
}
else if (ClicktoDrawCircle){
//画圆
if (Event->button() == Qt::LeftButton){
//求半径
r = std::pow(std::pow(static_cast<double>(Event->pos().x())-cx,2) + std::pow(static_cast<double>(Event->pos().y())-cy,2),0.5);
//创建圆的元素,转换成用外切矩阵表达的形式,颜色初始化
circle[circle_amount].setRect(cx-r, cy-r, 2*r, 2*r);
circle[circle_amount].setPen(QPen(QColor(0,0,0)));
circle[circle_amount].setBrush(QBrush(QColor(255,255,255)));
//构建完圆元素之后要把它搬到画布上
Elements_List->addToGroup(&circle[circle_amount]);
//刷新绘图
update();
circle_amount++;
ClicktoDrawCircle = false;
}
}
else if (ClicktoDrawEllipse){
//画椭圆
if (Event->button() == Qt::LeftButton){
//记录椭圆外切矩形的右下顶点
fEx = static_cast<double>(Event->pos().x());
fEy = static_cast<double>(Event->pos().y());
//创建椭圆元素,用外切矩阵表达的形式,颜色初始化
ellipse[ellipse_amount].setRect(sEx, sEy, fEx-sEx, fEy-sEy);
ellipse[ellipse_amount].setPen(QPen(QColor(0,0,0)));
ellipse[ellipse_amount].setBrush(QBrush(QColor(255,255,255)));
//构建完圆元素之后要把它搬到画布上
Elements_List->addToGroup(&ellipse[ellipse_amount]);
//刷新绘图
update();
ellipse_amount++;
ClicktoDrawEllipse = false;
}
}
else if (ClicktoDrawSquare){
//画正方形
if (Event->button() == Qt::LeftButton){
//求边长(利用外切圆的半径,鼠标动作与绘制外切圆一致)
a = std::pow(std::pow(static_cast<double>(Event->pos().x())-cSx,2) + std::pow(static_cast<double>(Event->pos().y())-cSy,2),0.5)*std::pow(2,0.5);
//创建正方形的元素,转换成Rect格式,颜色初始化
square[square_amount].setRect(cSx-a/2, cSy-a/2, a, a);
square[square_amount].setPen(QPen(QColor(0,0,0)));
square[square_amount].setBrush(QBrush(QColor(255,255,255)));
//构建完正方形元素之后要把它搬到画布上
Elements_List->addToGroup(&square[square_amount]);
//刷新绘图
update();
square_amount++;
ClicktoDrawSquare = false;
}
}
else if (ClicktoDrawRectangle){
//画长方形
if (Event->button() == Qt::LeftButton){
//记录矩形的右下顶点
fRx = static_cast<double>(Event->pos().x());
fRy = static_cast<double>(Event->pos().y());
//创建矩形元素,用Rect的格式,颜色初始化
rectangle[rectangle_amount].setRect(sRx, sRy, fRx-sRx, fRy-sRy);
rectangle[rectangle_amount].setPen(QPen(QColor(0,0,0)));
rectangle[rectangle_amount].setBrush(QBrush(QColor(255,255,255)));
//构建完圆元素之后要把它搬到画布上
Elements_List->addToGroup(&rectangle[rectangle_amount]);
//刷新绘图
update();
rectangle_amount++;
ClicktoDrawRectangle = false;
}
}
else if (Drag_to_Move){
if (Select_Item){
//选中元素的命令(放开鼠标后)
Select_Item = false;
}
else if (!Select_Item){
//记录鼠标的终点坐标并实施平移
Drag_End_Position = view->mapToScene(Event->pos());
QPointF Move_Vector = Drag_End_Position - Drag_Start_Position;
QPointF Centroid;
Move_x = Move_Vector.x();
Move_y = Move_Vector.y();
// 遍历所有位于鼠标位置的元素
for (QGraphicsItem* item : save) {
// 平移选中的元素
if (item->type() == QGraphicsLineItem::Type) {
QGraphicsLineItem *lineItem = qgraphicsitem_cast<QGraphicsLineItem*>(item);
//把平移向量用图形坐标系表示,对旋转后平移的bug修正
thita = lineItem->rotation();
thita = thita/180*M_PI;
Move_Vector = QPointF(Move_Vector.x()*std::cos(thita) + Move_Vector.y()*std::sin(thita), Move_Vector.y()*std::cos(thita)-Move_Vector.x()*std::sin(thita));
lineItem->setLine(QLineF(lineItem->line().p1()+Move_Vector,lineItem->line().p2()+Move_Vector));
} else if (item->type() == QGraphicsEllipseItem::Type) {
QGraphicsEllipseItem *ellipseItem = qgraphicsitem_cast<QGraphicsEllipseItem*>(item);
thita = ellipseItem->rotation();
thita = thita/180*M_PI;
Move_Vector = QPointF(Move_Vector.x()*std::cos(thita) + Move_Vector.y()*std::sin(thita), Move_Vector.y()*std::cos(thita)-Move_Vector.x()*std::sin(thita));
ellipseItem->setRect(ellipseItem->rect().topLeft().x()+Move_Vector.x(), ellipseItem->rect().topLeft().y()+Move_Vector.y(), ellipseItem->rect().width(), ellipseItem->rect().height());
} else if (item->type() == QGraphicsRectItem::Type) {
QGraphicsRectItem *rectangleItem = qgraphicsitem_cast<QGraphicsRectItem*>(item);
thita = rectangleItem->rotation();
thita = thita/180*M_PI;
Move_Vector = QPointF(Move_Vector.x()*std::cos(thita) + Move_Vector.y()*std::sin(thita), Move_Vector.y()*std::cos(thita)-Move_Vector.x()*std::sin(thita));
rectangleItem->setRect(rectangleItem->rect().topLeft().x()+Move_Vector.x(), rectangleItem->rect().topLeft().y()+Move_Vector.y(), rectangleItem->rect().width(), rectangleItem->rect().height());
} else if (item->type() == QGraphicsPolygonItem::Type) {
QGraphicsPolygonItem *triangleItem = qgraphicsitem_cast<QGraphicsPolygonItem*>(item);
thita = triangleItem->rotation();
thita = thita/180*M_PI;
Move_Vector = QPointF(Move_Vector.x()*std::cos(thita) + Move_Vector.y()*std::sin(thita), Move_Vector.y()*std::cos(thita)-Move_Vector.x()*std::sin(thita));
QPolygonF polygon = triangleItem->polygon();//保留老的三角形信息
QPolygonF triangle;
triangle << polygon.at(0) + Move_Vector << polygon.at(1) + Move_Vector << polygon.at(2) + Move_Vector;
triangleItem->setPolygon(triangle);
}
}
PY = false;
Drag_to_Move = false;
}
}
else if (Drag_to_Zoom)
{
if (Select_Item){
//选中元素的命令(放开鼠标后)
Select_Item = false;
}
else if (!Select_Item){
//记录鼠标的终点坐标并实施缩放
Drag_End_Position = view->mapToScene(Event->pos());
QPointF Centroid;
// 遍历所有位于鼠标位置的元素
for (QGraphicsItem* item : save) {
// 缩放选中的元素——先找到元素的中心,然后借助鼠标位置求出缩放比例
if (item->type() == QGraphicsLineItem::Type) {
QGraphicsLineItem *lineItem = qgraphicsitem_cast<QGraphicsLineItem*>(item);
//找回旋转前的坐标系
thita = lineItem->rotation();
lineItem->transformOriginPoint();
lineItem->setRotation(0);
Centroid =(lineItem->line().p1() + lineItem->line().p2())/2;
Scaler = std::pow(std::pow(Drag_End_Position.x()-Centroid.x(),2)+std::pow(Drag_End_Position.y()-Centroid.y(),2),0.5)/std::pow(std::pow(Drag_Start_Position.x()-Centroid.x(),2)+std::pow(Drag_Start_Position.y()-Centroid.y(),2),0.5);
lineItem->setLine(QLineF((lineItem->line().p1()-Centroid)*Scaler+Centroid,(lineItem->line().p2()-Centroid)*Scaler+Centroid));
lineItem->setTransformOriginPoint(Centroid);
lineItem->setRotation(thita);
} else if (item->type() == QGraphicsEllipseItem::Type) {
QGraphicsEllipseItem *ellipseItem = qgraphicsitem_cast<QGraphicsEllipseItem*>(item);
thita = ellipseItem->rotation();
ellipseItem->transformOriginPoint();
ellipseItem->setRotation(0);
Centroid =ellipseItem->rect().topLeft()+QPointF(ellipseItem->rect().width(),ellipseItem->rect().height())/2;
Scaler = std::pow(std::pow(Drag_End_Position.x()-Centroid.x(),2)+std::pow(Drag_End_Position.y()-Centroid.y(),2),0.5)/std::pow(std::pow(Drag_Start_Position.x()-Centroid.x(),2)+std::pow(Drag_Start_Position.y()-Centroid.y(),2),0.5);
ellipseItem->setRect(Centroid.x()-ellipseItem->rect().width()*Scaler/2,Centroid.y()-ellipseItem->rect().height()*Scaler/2,ellipseItem->rect().width()*Scaler,ellipseItem->rect().height()*Scaler);
ellipseItem->setTransformOriginPoint(Centroid);
ellipseItem->setRotation(thita);
} else if (item->type() == QGraphicsRectItem::Type) {
QGraphicsRectItem *rectangleItem = qgraphicsitem_cast<QGraphicsRectItem*>(item);
thita = rectangleItem->rotation();
rectangleItem->transformOriginPoint();
rectangleItem->setRotation(0);
Centroid =rectangleItem->rect().topLeft()+QPointF(rectangleItem->rect().width(),rectangleItem->rect().height())/2;
Scaler = std::pow(std::pow(Drag_End_Position.x()-Centroid.x(),2)+std::pow(Drag_End_Position.y()-Centroid.y(),2),0.5)/std::pow(std::pow(Drag_Start_Position.x()-Centroid.x(),2)+std::pow(Drag_Start_Position.y()-Centroid.y(),2),0.5);
rectangleItem->setRect(Centroid.x()-rectangleItem->rect().width()*Scaler/2,Centroid.y()-rectangleItem->rect().height()*Scaler/2,rectangleItem->rect().width()*Scaler,rectangleItem->rect().height()*Scaler);
rectangleItem->setTransformOriginPoint(Centroid);
rectangleItem->setRotation(thita);
} else if (item->type() == QGraphicsPolygonItem::Type) {
QGraphicsPolygonItem *triangleItem = qgraphicsitem_cast<QGraphicsPolygonItem*>(item);
thita = triangleItem->rotation();
triangleItem->transformOriginPoint();
triangleItem->setRotation(0);
QPolygonF polygon = triangleItem->polygon();//保留老的三角形信息
QPolygonF triangle;
Centroid = (polygon.at(0) + polygon.at(1) + polygon.at(2))/3;
Scaler = std::pow(std::pow(Drag_End_Position.x()-Centroid.x(),2)+std::pow(Drag_End_Position.y()-Centroid.y(),2),0.5)/std::pow(std::pow(Drag_Start_Position.x()-Centroid.x(),2)+std::pow(Drag_Start_Position.y()-Centroid.y(),2),0.5);
triangle << Centroid+(polygon.at(0)-Centroid)*Scaler << Centroid+(polygon.at(1)-Centroid)*Scaler << Centroid+(polygon.at(2)-Centroid)*Scaler;
triangleItem->setPolygon(triangle);
triangleItem->setTransformOriginPoint(Centroid);
triangleItem->setRotation(thita);
}
}
Drag_to_Zoom = false;
SF = false;
}
}
else if (Drag_to_Revolve)
{
if (Select_Item){
//选中元素的命令(放开鼠标后)
Select_Item = false;
}
else if (!Select_Item){
//记录鼠标的终点坐标并实施旋转
Drag_End_Position = view->mapToScene(Event->pos());
QPointF Centroid;
double k1,k2;
// 遍历所有位于鼠标位置的元素
for (QGraphicsItem* item : save) {
// 旋转选中的元素——先找到元素的中心,然后借助鼠标位置求出旋转角度
if (item->type() == QGraphicsLineItem::Type) {
QGraphicsLineItem *lineItem = qgraphicsitem_cast<QGraphicsLineItem*>(item);
Centroid =(lineItem->line().p1() + lineItem->line().p2())/2;
k1=(Centroid.y()-Drag_Start_Position.y())/(Centroid.x()-Drag_Start_Position.x());
k2=(Centroid.y()-Drag_End_Position.y())/(Centroid.x()-Drag_End_Position.x());
thita = lineItem->rotation() + (std::atan(k2)-std::atan(k1))/M_PI*180;
lineItem->setTransformOriginPoint(Centroid);
lineItem->setRotation(thita);
} else if (item->type() == QGraphicsEllipseItem::Type) {
QGraphicsEllipseItem *ellipseItem = qgraphicsitem_cast<QGraphicsEllipseItem*>(item);
Centroid =ellipseItem->rect().topLeft()+QPointF(ellipseItem->rect().width(),ellipseItem->rect().height())/2;
k1=(Centroid.y()-Drag_Start_Position.y())/(Centroid.x()-Drag_Start_Position.x());
k2=(Centroid.y()-Drag_End_Position.y())/(Centroid.x()-Drag_End_Position.x());
thita = ellipseItem->rotation() + (std::atan(k2)-std::atan(k1))/M_PI*180;
ellipseItem->setTransformOriginPoint(Centroid);
ellipseItem->setRotation(thita);
} else if (item->type() == QGraphicsRectItem::Type) {
QGraphicsRectItem *rectangleItem = qgraphicsitem_cast<QGraphicsRectItem*>(item);
Centroid =rectangleItem->rect().topLeft()+QPointF(rectangleItem->rect().width(),rectangleItem->rect().height())/2;
k1=(Centroid.y()-Drag_Start_Position.y())/(Centroid.x()-Drag_Start_Position.x());
k2=(Centroid.y()-Drag_End_Position.y())/(Centroid.x()-Drag_End_Position.x());
thita = rectangleItem->rotation() + (std::atan(k2)-std::atan(k1))/M_PI*180;
rectangleItem->setTransformOriginPoint(Centroid);
rectangleItem->setRotation(thita);
} else if (item->type() == QGraphicsPolygonItem::Type) {
QGraphicsPolygonItem *triangleItem = qgraphicsitem_cast<QGraphicsPolygonItem*>(item);
QPolygonF polygon = triangleItem->polygon();//保留老的三角形信息
QPolygonF triangle;
Centroid = (polygon.at(0) + polygon.at(1) + polygon.at(2))/3;
k1=(Centroid.y()-Drag_Start_Position.y())/(Centroid.x()-Drag_Start_Position.x());
k2=(Centroid.y()-Drag_End_Position.y())/(Centroid.x()-Drag_End_Position.x());
thita = triangleItem->rotation()+ (std::atan(k2)-std::atan(k1))/M_PI*180;
triangleItem->setTransformOriginPoint(Centroid);
triangleItem->setRotation(thita);
}
}
Drag_to_Revolve = false;
XZ = false;
Elements_List->setTransformOriginPoint(0,0);
Elements_List->setRotation(0);
}
}
}
private:
QGraphicsScene scene;
QGraphicsView *view;
QWidget *window2;
};
//一个Update List的函数(在这里建立元素与列表中每一项的对应关系)
void UL(QGraphicsItemGroup GraphicsGroup[maxsize*6], QListWidget &LW)
{ //在更新列表之前需要把列表中的一切先清空
LW.clear();
//建立索引系数
int index = 0;
// 遍历QGraphicsItemGroup中的图形元素
foreach (QGraphicsItem *item, GraphicsGroup->childItems()) {
QString itemType;
// 根据图形项的类型确定它是什么类型
if (item->type() == QGraphicsLineItem::Type) {
itemType = "直线"+QString::number(index);
} else if (item->type() == QGraphicsEllipseItem::Type) {
QGraphicsEllipseItem *ellipseItem = qgraphicsitem_cast<QGraphicsEllipseItem*>(item);
if (ellipseItem->rect().width() == ellipseItem->rect().height()){
itemType = "圆"+QString::number(index);
}
else{
itemType = "椭圆"+QString::number(index);
}
} else if (item->type() == QGraphicsRectItem::Type) {
QGraphicsRectItem *rectangleItem = qgraphicsitem_cast<QGraphicsRectItem*>(item);
if (rectangleItem->rect().width() == rectangleItem->rect().height()){
itemType = "正方形"+QString::number(index);
}
else{
itemType = "长方形"+QString::number(index);
}
} else if (item->type() == QGraphicsPolygonItem::Type) {
itemType = "三角形"+QString::number(index);
}
//按照大类列表
QListWidgetItem *listItem = new QListWidgetItem(itemType);
LW.addItem(listItem);
//建立元素与列表之间的索引
listItem->setData(Qt::UserRole, index);
index++;
}
total_items_in_my_list = index;
}
//利用索引值求得QGraphicsItemGroup实例中的对应元素的地址
QGraphicsItem* FindtheCorrespondingElementbyIndex(QGraphicsItemGroup *GraphicsGroup){
int id = 0;
foreach (QGraphicsItem *item, GraphicsGroup->childItems()) {
if (id == current_item_in_my_list){
return item;
}
id++;
}
}
//获取单击列表时对应元素的索引
void CurrentItemClicked(QListWidgetItem *item) {
if (item) {
current_item_in_my_list = item->data(Qt::UserRole).toInt();
}
}
int main(int argc, char *argv[])
{
bool ok;int Insert_Option;
QApplication app(argc, argv);
QListWidget YSLB;//元素列表
//连接先前定义的索引函数,这样单击列表项的时候就会记录列表项的索引位置
QObject::connect(&YSLB, &QListWidget::itemClicked, &CurrentItemClicked);
//提醒用户选择元素的消息框
QWidget UseListtoSelectItem_MessageBox;
QLabel MessageBoxLabel("请选择一个待操作的元素,然后按“OK”");
MessageBoxLabel.setAlignment(Qt::AlignCenter); // 居中对齐文本
UseListtoSelectItem_MessageBox.setWindowTitle("列表选择");
QVBoxLayout layoutUseListtoSelectItem_MessageBox;
UseListtoSelectItem_MessageBox.setLayout(&layoutUseListtoSelectItem_MessageBox);
layoutUseListtoSelectItem_MessageBox.addWidget(&MessageBoxLabel);
QPushButton *okButton = new QPushButton("OK");
layoutUseListtoSelectItem_MessageBox.addWidget(okButton);
layoutUseListtoSelectItem_MessageBox.setAlignment(okButton, Qt::AlignCenter); // 居中对齐按钮
layoutUseListtoSelectItem_MessageBox.setSpacing(20); // 设置控件之间的垂直间距
QWidget w,
CRYS,
CRYS_Line_Input_Window,
CRYS_Circle_Input_Window,
CRYS_Ellipse_Input_Window,
CRYS_Square_Input_Window,
CRYS_Rectangle_Input_Window,
CRYS_Triangle_Input_Window,
QCYS,
SZYS,
BH,
Move_Button_Window,Move_Input_Window,
Zoom_Button_Window,Zoom_Input_Window,
Revolve_Button_Window,Revolve_Input_Window,
XZYS;
MAP map;
QMessageBox::StandardButton SZYS_BK;
QMessageBox::StandardButton SZYS_TC;
w.setWindowTitle("矢量图小精灵");w.move(0,0);w.resize(360,30);w.raise();
map.setWindowTitle("画布");map.showFullScreen();map.addShape(Elements_List);map.setWindowToRaise(&w);
Elements_List->setZValue(1);
//初始化Layout
QHBoxLayout *layoutCRYS = new QHBoxLayout;
QHBoxLayout *layoutCRYS_Line_Input = new QHBoxLayout;
QHBoxLayout *layoutCRYS_Circle_Input = new QHBoxLayout;
QVBoxLayout *layoutCRYS_Ellipse_Input = new QVBoxLayout;
QHBoxLayout *layoutCRYS_Square_Input = new QHBoxLayout;
QVBoxLayout *layoutCRYS_Rectangle_Input = new QVBoxLayout;
QVBoxLayout *layoutCRYS_Triangle_Input = new QVBoxLayout;
QHBoxLayout *layoutQCYS = new QHBoxLayout;
QHBoxLayout *layoutSZYS = new QHBoxLayout;
QHBoxLayout *layoutBH = new QHBoxLayout;
QHBoxLayout *layoutXZYS = new QHBoxLayout;
QGridLayout *layoutMove_Direction = new QGridLayout;
QHBoxLayout *Move_Input_layout = new QHBoxLayout;
QHBoxLayout *Zoom_Button_layout = new QHBoxLayout;
QHBoxLayout *Zoom_Input_layout = new QHBoxLayout;
QHBoxLayout *Revolve_Button_layout = new QHBoxLayout;
QHBoxLayout *Revolve_Input_layout = new QHBoxLayout;
QLineEdit *CRYS_Line_Input_sx = new QLineEdit;
QLineEdit *CRYS_Line_Input_sy = new QLineEdit;
QLineEdit *CRYS_Line_Input_fx = new QLineEdit;
QLineEdit *CRYS_Line_Input_fy = new QLineEdit;
QLineEdit *CRYS_Circle_Input_cx = new QLineEdit;
QLineEdit *CRYS_Circle_Input_cy = new QLineEdit;
QLineEdit *CRYS_Circle_Input_r = new QLineEdit;
QLineEdit *CRYS_Ellipse_Input_sEx = new QLineEdit;
QLineEdit *CRYS_Ellipse_Input_sEy = new QLineEdit;
QLineEdit *CRYS_Ellipse_Input_fEx = new QLineEdit;
QLineEdit *CRYS_Ellipse_Input_fEy = new QLineEdit;
QLineEdit *CRYS_Square_Input_cSx = new QLineEdit;
QLineEdit *CRYS_Square_Input_cSy = new QLineEdit;
QLineEdit *CRYS_Square_Input_a = new QLineEdit;
QLineEdit *CRYS_Rectangle_Input_sRx = new QLineEdit;
QLineEdit *CRYS_Rectangle_Input_sRy = new QLineEdit;
QLineEdit *CRYS_Rectangle_Input_fRx = new QLineEdit;
QLineEdit *CRYS_Rectangle_Input_fRy = new QLineEdit;
QLineEdit *CRYS_Triangle_Input_p1x = new QLineEdit;
QLineEdit *CRYS_Triangle_Input_p1y = new QLineEdit;
QLineEdit *CRYS_Triangle_Input_p2x = new QLineEdit;
QLineEdit *CRYS_Triangle_Input_p2y = new QLineEdit;
QLineEdit *CRYS_Triangle_Input_p3x = new QLineEdit;
QLineEdit *CRYS_Triangle_Input_p3y = new QLineEdit;
QLineEdit *Color_R = new QLineEdit;
QLineEdit *Color_G = new QLineEdit;
QLineEdit *Color_B = new QLineEdit;
QLineEdit *Move_Input_V = new QLineEdit;
QLineEdit *Move_Input_H = new QLineEdit;
QLineEdit *Scaler_Input = new QLineEdit;
QLineEdit *thita_Input = new QLineEdit;
CRYS_Line_Input_sx->setPlaceholderText("起点的横坐标x");
CRYS_Line_Input_sy->setPlaceholderText("起点的纵坐标y");
CRYS_Line_Input_fx->setPlaceholderText("终点的横坐标x");
CRYS_Line_Input_fy->setPlaceholderText("终点的纵坐标y");
CRYS_Circle_Input_cx->setPlaceholderText("圆心横坐标");
CRYS_Circle_Input_cy->setPlaceholderText("圆心纵坐标");
CRYS_Circle_Input_r->setPlaceholderText("半径");
CRYS_Ellipse_Input_sEx->setPlaceholderText("椭圆外切矩形对角线顶点1横坐标");
CRYS_Ellipse_Input_sEy->setPlaceholderText("椭圆外切矩形对角线顶点1纵坐标");
CRYS_Ellipse_Input_fEx->setPlaceholderText("椭圆外切矩形对角线顶点2横坐标");
CRYS_Ellipse_Input_fEy->setPlaceholderText("椭圆外切矩形对角线顶点2纵坐标");
CRYS_Square_Input_cSx->setPlaceholderText("中心横坐标");
CRYS_Square_Input_cSy->setPlaceholderText("中心纵坐标");
CRYS_Square_Input_a->setPlaceholderText("边长");
CRYS_Rectangle_Input_sRx->setPlaceholderText("对角线顶点1的横坐标");
CRYS_Rectangle_Input_sRy->setPlaceholderText("对角线顶点1的纵坐标");
CRYS_Rectangle_Input_fRx->setPlaceholderText("对角线顶点2的横坐标");
CRYS_Rectangle_Input_fRy->setPlaceholderText("对角线顶点2的纵坐标");
CRYS_Triangle_Input_p1x->setPlaceholderText("顶点1横坐标");
CRYS_Triangle_Input_p1y->setPlaceholderText("顶点1纵坐标");
CRYS_Triangle_Input_p2x->setPlaceholderText("顶点2横坐标");
CRYS_Triangle_Input_p2y->setPlaceholderText("顶点2纵坐标");
CRYS_Triangle_Input_p3x->setPlaceholderText("顶点3横坐标");
CRYS_Triangle_Input_p3y->setPlaceholderText("顶点3纵坐标");
Color_R->setPlaceholderText("红(Red)");
Color_G->setPlaceholderText("绿(Green)");
Color_B->setPlaceholderText("蓝(Blue)");
Scaler_Input->setPlaceholderText("请输入该元素的缩放比例");
thita_Input->setPlaceholderText("请输入旋转转角(角度值,逆时针为正)");
QDoubleValidator *validatorsx = new QDoubleValidator(CRYS_Line_Input_sx);
QDoubleValidator *validatorsy = new QDoubleValidator(CRYS_Line_Input_sy);
QDoubleValidator *validatorfx = new QDoubleValidator(CRYS_Line_Input_fx);
QDoubleValidator *validatorfy = new QDoubleValidator(CRYS_Line_Input_fy);
QDoubleValidator *validatorcx = new QDoubleValidator(CRYS_Circle_Input_cx);
QDoubleValidator *validatorcy = new QDoubleValidator(CRYS_Circle_Input_cy);
QDoubleValidator *validatorr = new QDoubleValidator(CRYS_Circle_Input_r);
validatorr->setBottom(0);//r>0
QDoubleValidator *validatorsEx = new QDoubleValidator(CRYS_Ellipse_Input_sEx);
QDoubleValidator *validatorsEy = new QDoubleValidator(CRYS_Ellipse_Input_sEy);
QDoubleValidator *validatorfEx = new QDoubleValidator(CRYS_Ellipse_Input_fEx);
QDoubleValidator *validatorfEy = new QDoubleValidator(CRYS_Ellipse_Input_fEy);
QDoubleValidator *validatorcSx = new QDoubleValidator(CRYS_Square_Input_cSx);
QDoubleValidator *validatorcSy = new QDoubleValidator(CRYS_Square_Input_cSy);
QDoubleValidator *validatora = new QDoubleValidator(CRYS_Square_Input_a);
validatora->setBottom(0);//a>0
QDoubleValidator *validatorsRx = new QDoubleValidator(CRYS_Rectangle_Input_sRx);
QDoubleValidator *validatorsRy = new QDoubleValidator(CRYS_Rectangle_Input_sRy);
QDoubleValidator *validatorfRx = new QDoubleValidator(CRYS_Rectangle_Input_fRx);
QDoubleValidator *validatorfRy = new QDoubleValidator(CRYS_Rectangle_Input_fRy);
QDoubleValidator *validatorp1x = new QDoubleValidator(CRYS_Triangle_Input_p1x);
QDoubleValidator *validatorp1y = new QDoubleValidator(CRYS_Triangle_Input_p1y);
QDoubleValidator *validatorp2x = new QDoubleValidator(CRYS_Triangle_Input_p2x);
QDoubleValidator *validatorp2y = new QDoubleValidator(CRYS_Triangle_Input_p2y);
QDoubleValidator *validatorp3x = new QDoubleValidator(CRYS_Triangle_Input_p3x);
QDoubleValidator *validatorp3y = new QDoubleValidator(CRYS_Triangle_Input_p3y);
QDoubleValidator *validatormoveinputV = new QDoubleValidator(Move_Input_V);
QDoubleValidator *validatormoveinputH = new QDoubleValidator(Move_Input_H);
QValidator *validatorR = new QIntValidator(0, 255, Color_R); // 只允许输入0到255的整数
QValidator *validatorG = new QIntValidator(0, 255, Color_G); // 只允许输入0到255的整数
QValidator *validatorB = new QIntValidator(0, 255, Color_B); // 只允许输入0到255的整数
//【设计窗口】
CRYS.setWindowTitle("插入元素");
CRYS_Line_Input_Window.setWindowTitle("请按照文本提示依次输入直线的参数(原点在屏幕左上角,横轴正方向为右,纵轴正方向为下)");
CRYS_Circle_Input_Window.setWindowTitle("请按照文本提示依次输入圆的参数(原点在屏幕左上角,横轴正方向为右,纵轴正方向为下)");
CRYS_Ellipse_Input_Window.setWindowTitle("请按照文本提示依次输入椭圆的参数(原点在屏幕左上角,横轴正方向为右,纵轴正方向为下)");
CRYS_Square_Input_Window.setWindowTitle("请按照文本提示依次输入正方形的参数(原点在屏幕左上角,横轴正方向为右,纵轴正方向为下)");
CRYS_Rectangle_Input_Window.setWindowTitle("请按照文本提示依次输入长方形的参数(原点在屏幕左上角,横轴正方向为右,纵轴正方向为下)");
CRYS_Triangle_Input_Window.setWindowTitle("请按照文本提示依次输入三角形的参数(原点在屏幕左上角,横轴正方向为右,纵轴正方向为下)");
QCYS.setWindowTitle("清除元素");
SZYS.setWindowTitle("设置元素的颜色属性");
SZYS.resize(300,100);
BH.setWindowTitle("元素的变换");
Move_Input_V->setPlaceholderText("竖直坐标");
Move_Input_H->setPlaceholderText("水平坐标");
XZYS.setWindowTitle("请选择一种选中目标元素的方法");
XZYS.resize(480,100);
Move_Button_Window.setWindowTitle("单击按钮微调");
Move_Input_Window.setWindowTitle("设置目标值");
Zoom_Button_Window.setWindowTitle("单击按钮微调");
Zoom_Button_Window.resize(360,100);
Zoom_Input_Window.setWindowTitle("设置目标值");
Revolve_Button_Window.setWindowTitle("单击按钮微调转角");
Revolve_Button_Window.resize(375,100);
Revolve_Input_Window.setWindowTitle("设置目标值");
//【创建按钮】
//插入元素
//创建单选按钮
QRadioButton *CRYSO1 = new QRadioButton("使用鼠标");
QRadioButton *CRYSO2 = new QRadioButton("设置目标值");
//创建确定取消按钮
QPushButton *CRYSQD = new QPushButton("确定");
QPushButton *CRYSQX = new QPushButton("取消");
//设置目标值建立直线
QPushButton *CRYS_Line_Input_QD = new QPushButton("确定");
//设置目标值建立圆
QPushButton *CRYS_Circle_Input_QD = new QPushButton("确定");
//设置目标值建立椭圆
QPushButton *CRYS_Ellipse_Input_QD = new QPushButton("确定");
//设置目标值建立正方形
QPushButton *CRYS_Square_Input_QD = new QPushButton("确定");
//设置目标值建立长方形
QPushButton *CRYS_Rectangle_Input_QD = new QPushButton("确定");
//设置目标值建立三角形
QPushButton *CRYS_Triangle_Input_QD = new QPushButton("确定");
//清除元素按钮
QPushButton *QCYS1 = new QPushButton("清除指定元素");
QPushButton *QCYS2 = new QPushButton("批量清除元素");
QPushButton *QCYS3 = new QPushButton("全部清除");
//设置颜色按钮【2】
QPushButton *SZYS1 = new QPushButton("边框");
QPushButton *SZYS2 = new QPushButton("填充");
QPushButton *SZYS3 = new QPushButton("背景");
//设置颜色按钮【3】
QPushButton *RGBQD = new QPushButton("确定");
QPushButton *RGBQX = new QPushButton("取消");
//变换按钮
QPushButton *BHQD = new QPushButton("确定");
//方向按钮
QPushButton *Up = new QPushButton("↑");
QPushButton *Down = new QPushButton("↓");
QPushButton *Left = new QPushButton("←");
QPushButton *Right = new QPushButton("→");
//缩放按钮
QPushButton *ZOOM = new QPushButton("+");
QPushButton *SHRINK = new QPushButton("-");
QPushButton *Zoom_Done = new QPushButton("确定");
//旋转按钮
QPushButton *clockwise = new QPushButton("顺时针");
QPushButton *anticlockwise = new QPushButton("逆时针");
QPushButton *Rotate_Done = new QPushButton("确定");
//单选按钮
QRadioButton *BHO1 = new QRadioButton("使用鼠标拖曳");
QRadioButton *BHO2 = new QRadioButton("设置目标值");
QPushButton *BHO2Done = new QPushButton("确定");
QRadioButton *BHO3= new QRadioButton("单击按钮微调");
//选中元素的方式【选项】
QRadioButton *XZYS1 = new QRadioButton("鼠标选择");
QRadioButton *XZYS2 = new QRadioButton("列表选择");
//选中元素的方式【确定与取消】
QPushButton *XZYSQD = new QPushButton("确定");
QPushButton *XZYSQX = new QPushButton("取消");
//【排列按钮位置】
//插入元素
CRYS.setLayout(layoutCRYS);
layoutCRYS->addWidget(CRYSO1);
layoutCRYS->addWidget(CRYSO2);
layoutCRYS->addWidget(CRYSQD);
layoutCRYS->addWidget(CRYSQX);
//插入直线>设置目标值
CRYS_Line_Input_Window.setLayout(layoutCRYS_Line_Input);
CRYS_Line_Input_sx->setValidator(validatorsx);
CRYS_Line_Input_sy->setValidator(validatorsy);
CRYS_Line_Input_fx->setValidator(validatorfx);
CRYS_Line_Input_fy->setValidator(validatorfy);
layoutCRYS_Line_Input->addWidget(CRYS_Line_Input_sx);
layoutCRYS_Line_Input->addWidget(CRYS_Line_Input_sy);
layoutCRYS_Line_Input->addWidget(CRYS_Line_Input_fx);
layoutCRYS_Line_Input->addWidget(CRYS_Line_Input_fy);
layoutCRYS_Line_Input->addWidget(CRYS_Line_Input_QD);
//插入圆>设置目标值
CRYS_Circle_Input_Window.setLayout(layoutCRYS_Circle_Input);
CRYS_Circle_Input_cx->setValidator(validatorcx);
CRYS_Circle_Input_cy->setValidator(validatorcy);
CRYS_Circle_Input_r->setValidator(validatorr);
layoutCRYS_Circle_Input->addWidget(CRYS_Circle_Input_cx);
layoutCRYS_Circle_Input->addWidget(CRYS_Circle_Input_cy);
layoutCRYS_Circle_Input->addWidget(CRYS_Circle_Input_r);
layoutCRYS_Circle_Input->addWidget(CRYS_Circle_Input_QD);
//插入椭圆>设置目标值
CRYS_Ellipse_Input_Window.setLayout(layoutCRYS_Ellipse_Input);
CRYS_Ellipse_Input_sEx->setValidator(validatorsEx);
CRYS_Ellipse_Input_sEy->setValidator(validatorsEy);
CRYS_Ellipse_Input_fEx->setValidator(validatorfEx);
CRYS_Ellipse_Input_fEy->setValidator(validatorfEy);
layoutCRYS_Ellipse_Input->addWidget(CRYS_Ellipse_Input_sEx);
layoutCRYS_Ellipse_Input->addWidget(CRYS_Ellipse_Input_sEy);
layoutCRYS_Ellipse_Input->addWidget(CRYS_Ellipse_Input_fEx);
layoutCRYS_Ellipse_Input->addWidget(CRYS_Ellipse_Input_fEy);
layoutCRYS_Ellipse_Input->addWidget(CRYS_Ellipse_Input_QD);
//插入正方形>设置目标值
CRYS_Square_Input_Window.setLayout(layoutCRYS_Square_Input);
CRYS_Square_Input_cSx->setValidator(validatorcSx);
CRYS_Square_Input_cSy->setValidator(validatorcSy);
CRYS_Square_Input_a->setValidator(validatora);
layoutCRYS_Square_Input->addWidget(CRYS_Square_Input_cSx);
layoutCRYS_Square_Input->addWidget(CRYS_Square_Input_cSy);
layoutCRYS_Square_Input->addWidget(CRYS_Square_Input_a);
layoutCRYS_Square_Input->addWidget(CRYS_Square_Input_QD);
//插入矩形>设置目标值
CRYS_Rectangle_Input_Window.setLayout(layoutCRYS_Rectangle_Input);
CRYS_Rectangle_Input_sRx->setValidator(validatorsRx);
CRYS_Rectangle_Input_sRy->setValidator(validatorsRy);
CRYS_Rectangle_Input_fRx->setValidator(validatorfRx);
CRYS_Rectangle_Input_fRy->setValidator(validatorfRy);
layoutCRYS_Rectangle_Input->addWidget(CRYS_Rectangle_Input_sRx);
layoutCRYS_Rectangle_Input->addWidget(CRYS_Rectangle_Input_sRy);
layoutCRYS_Rectangle_Input->addWidget(CRYS_Rectangle_Input_fRx);
layoutCRYS_Rectangle_Input->addWidget(CRYS_Rectangle_Input_fRy);
layoutCRYS_Rectangle_Input->addWidget(CRYS_Rectangle_Input_QD);
//插入三角形>设置目标值
CRYS_Triangle_Input_Window.setLayout(layoutCRYS_Triangle_Input);
CRYS_Triangle_Input_p1x->setValidator(validatorp1x);
CRYS_Triangle_Input_p1y->setValidator(validatorp1y);
CRYS_Triangle_Input_p2x->setValidator(validatorp2x);
CRYS_Triangle_Input_p2y->setValidator(validatorp2y);
CRYS_Triangle_Input_p3x->setValidator(validatorp3x);
CRYS_Triangle_Input_p3y->setValidator(validatorp3y);
layoutCRYS_Triangle_Input->addWidget(CRYS_Triangle_Input_p1x);
layoutCRYS_Triangle_Input->addWidget(CRYS_Triangle_Input_p1y);
layoutCRYS_Triangle_Input->addWidget(CRYS_Triangle_Input_p2x);
layoutCRYS_Triangle_Input->addWidget(CRYS_Triangle_Input_p2y);
layoutCRYS_Triangle_Input->addWidget(CRYS_Triangle_Input_p3x);
layoutCRYS_Triangle_Input->addWidget(CRYS_Triangle_Input_p3y);
layoutCRYS_Triangle_Input->addWidget(CRYS_Triangle_Input_QD);
//清除元素
QCYS.setLayout(layoutQCYS);
layoutQCYS->addWidget(QCYS1);
layoutQCYS->addWidget(QCYS2);
layoutQCYS->addWidget(QCYS3);
//设置颜色【按钮】
SZYS.setLayout(layoutSZYS);
layoutSZYS->addWidget(SZYS1);
layoutSZYS->addWidget(SZYS2);
layoutSZYS->addWidget(SZYS3);
//变换(选项)
layoutBH->addWidget(BHO1);
layoutBH->addWidget(BHO3);
layoutBH->addWidget(BHO2);
//变换(按钮)
BH.setLayout(layoutBH);
layoutBH->addWidget(BHQD);
//平移
Move_Button_Window.setLayout(layoutMove_Direction);
layoutMove_Direction->addWidget(Up,1,2);
layoutMove_Direction->addWidget(Down,3,2);
layoutMove_Direction->addWidget(Left,2,1);
layoutMove_Direction->addWidget(Right,2,3);
Move_Input_Window.setLayout(Move_Input_layout);
Move_Input_layout->addWidget(Move_Input_H);
Move_Input_layout->addWidget(Move_Input_V);
Move_Input_layout->addWidget(BHO2Done);
//缩放
Zoom_Button_Window.setLayout(Zoom_Button_layout);
Zoom_Button_layout->addWidget(ZOOM);
Zoom_Button_layout->addWidget(SHRINK);
Zoom_Input_Window.setLayout(Zoom_Input_layout);
Zoom_Input_layout->addWidget(Scaler_Input);
Zoom_Input_layout->addWidget(Zoom_Done);
//旋转
Revolve_Button_Window.setLayout(Revolve_Button_layout);
Revolve_Button_layout->addWidget(anticlockwise);
Revolve_Button_layout->addWidget(clockwise);
Revolve_Input_Window.setLayout(Revolve_Input_layout);
Revolve_Input_layout->addWidget(thita_Input);
Revolve_Input_layout->addWidget(Rotate_Done);
//选中元素的方式
XZYS.setLayout(layoutXZYS);
layoutXZYS->addWidget(XZYS1);
layoutXZYS->addWidget(XZYS2);
layoutXZYS->addWidget(XZYSQD);
layoutXZYS->addWidget(XZYSQX);
//【设计菜单】
//构建菜单的架构
QMenuBar *MenuBar = new QMenuBar(&w);
w.setLayout(new QVBoxLayout);
w.layout()->setMenuBar(MenuBar);
w.layout()->addWidget(&YSLB);
//设计具体的选项
QMenu *Insert = MenuBar->addMenu("插入元素");
QObject::connect(CRYSQD, &QPushButton::clicked, [&]() {
CRYS.hide();
if (CRYSO1->isChecked()&&Insert_Option==1){
//使用鼠标建立元素的命令
ClicktoDrawLine = true;
}
else if(CRYSO2->isChecked()&&Insert_Option==1){
//设置目标值建立元素的命令
CRYS_Line_Input_Window.show();
QObject::connect(CRYS_Line_Input_QD, &QPushButton::clicked, [&]() {
if (CRYS_Line_Input_sx->text().isEmpty()||CRYS_Line_Input_sy->text().isEmpty()||CRYS_Line_Input_fx->text().isEmpty()||CRYS_Line_Input_fy->text().isEmpty()){
QMessageBox::critical(nullptr, "直线坐标输入错误", "请在每个文本框内输入数字");
}else{
CRYS_Line_Input_Window.hide();
sx = (CRYS_Line_Input_sx->text()).toDouble();
sy = (CRYS_Line_Input_sy->text()).toDouble();
fx = (CRYS_Line_Input_fx->text()).toDouble();
fy = (CRYS_Line_Input_fy->text()).toDouble();
line[line_amount].setLine(sx, sy, fx, fy);
line[line_amount].setPen(QPen(QColor(0,0,0)));
Elements_List->addToGroup(&line[line_amount]);
line_amount++;
}
});