-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_analysis_gui.py
More file actions
2235 lines (1918 loc) · 82.5 KB
/
Copy pathsource_analysis_gui.py
File metadata and controls
2235 lines (1918 loc) · 82.5 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
import os
import json
from typing import List, Dict, Optional
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QPushButton,
QTextEdit, QLabel, QGroupBox, QTableWidget,
QTableWidgetItem, QHeaderView, QFileDialog,
QMessageBox, QProgressBar, QFrame, QDialog,
QFormLayout, QScrollArea, QListWidget, QSplitter,
QListWidgetItem, QTabWidget, QComboBox, QGridLayout,
QSizePolicy, QGraphicsDropShadowEffect, QApplication,
QCheckBox)
from PyQt5.QtCore import Qt, pyqtSignal, QPropertyAnimation, QEasingCurve, QRect, QTimer
from PyQt5.QtGui import QFont, QColor, QIcon, QPalette, QLinearGradient, QBrush, QPainter
# 导入我们改进的分析器
# from improved_analyzer import MultiLanguageCodeQLAnalyzer, SourceCodeAnalysisThread
from fixed_codeql_analyzer import FixedCodeQLAnalyzer as MultiLanguageCodeQLAnalyzer, FixedSourceCodeAnalysisThread as SourceCodeAnalysisThread
# from fixed_codeql_analyzer import FixedCodeQLAnalyzer, FixedSourceCodeAnalysisThread
# from improved_codeql_analyzer import ImprovedCodeQLAnalyzer as MultiLanguageCodeQLAnalyzer, ImprovedSourceCodeAnalysisThread as SourceCodeAnalysisThread
# from integrated_codeql_tool import (
# FixedCodeQLAnalyzer as MultiLanguageCodeQLAnalyzer,
# IntegratedCodeQLAnalysisThread as SourceCodeAnalysisThread,
# IntegratedSourceCodeAnalysisTab as EnhancedSourceCodeAnalysisTab
# )
class LanguageInfoWidget(QFrame):
"""语言信息显示控件"""
def __init__(self, parent=None):
super().__init__(parent)
self.setFrameShape(QFrame.StyledPanel)
self.setStyleSheet("""
QFrame {
background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
stop:0 #ffffff, stop:1 #f8f9fa);
border-radius: 12px;
border: 1px solid rgba(102, 126, 234, 0.2);
padding: 12px;
margin: 4px;
}
""")
self.layout = QVBoxLayout(self)
self.layout.setContentsMargins(16, 12, 16, 12)
self.layout.setSpacing(8)
# 标题
self.title_label = QLabel("📊 文件语言分布")
self.title_label.setStyleSheet("""
QLabel {
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
font-size: 14px;
font-weight: 600;
color: #495057;
margin-bottom: 8px;
}
""")
self.layout.addWidget(self.title_label)
# 语言信息容器
self.info_layout = QVBoxLayout()
self.layout.addLayout(self.info_layout)
self.clear_info()
def update_language_info(self, language_files: Dict[str, List[str]]):
"""更新语言信息"""
# 清除之前的信息
self.clear_info()
if not language_files:
no_files_label = QLabel("❌ 未检测到支持的文件")
no_files_label.setStyleSheet("color: #6c757d; font-style: italic;")
self.info_layout.addWidget(no_files_label)
return
# 显示每种语言的文件信息
total_files = sum(len(files) for files in language_files.values())
for language, files in language_files.items():
# 语言图标映射
language_icons = {
'cpp': '🔧', 'java': '☕', 'javascript': '🟨',
'python': '🐍', 'csharp': '🔵', 'go': '🐹',
'ruby': '💎', 'swift': '🍎', 'kotlin': '🎯'
}
icon = language_icons.get(language, '📄')
percentage = (len(files) / total_files) * 100
lang_widget = QFrame()
lang_widget.setStyleSheet("""
QFrame {
background-color: rgba(102, 126, 234, 0.05);
border-radius: 6px;
border: 1px solid rgba(102, 126, 234, 0.1);
margin: 2px;
padding: 8px;
}
""")
lang_layout = QHBoxLayout(lang_widget)
lang_layout.setContentsMargins(8, 4, 8, 4)
# 语言名称和图标
lang_label = QLabel(f"{icon} {language.upper()}")
lang_label.setStyleSheet("""
QLabel {
font-weight: 600;
color: #495057;
font-size: 12px;
}
""")
# 文件数量和百分比
count_label = QLabel(f"{len(files)} 文件 ({percentage:.1f}%)")
count_label.setStyleSheet("""
QLabel {
color: #6c757d;
font-size: 12px;
}
""")
lang_layout.addWidget(lang_label)
lang_layout.addStretch()
lang_layout.addWidget(count_label)
self.info_layout.addWidget(lang_widget)
# 总计
total_widget = QFrame()
total_widget.setStyleSheet("""
QFrame {
background-color: rgba(46, 204, 113, 0.1);
border-radius: 6px;
border: 1px solid rgba(46, 204, 113, 0.2);
margin: 2px;
padding: 8px;
}
""")
total_layout = QHBoxLayout(total_widget)
total_layout.setContentsMargins(8, 4, 8, 4)
total_label = QLabel("📁 总计")
total_label.setStyleSheet("font-weight: 600; color: #2ecc71; font-size: 12px;")
total_count_label = QLabel(f"{total_files} 文件")
total_count_label.setStyleSheet("color: #27ae60; font-size: 12px;")
total_layout.addWidget(total_label)
total_layout.addStretch()
total_layout.addWidget(total_count_label)
self.info_layout.addWidget(total_widget)
def clear_info(self):
"""清除语言信息"""
while self.info_layout.count():
child = self.info_layout.takeAt(0)
if child.widget():
child.widget().deleteLater()
class EnhancedSourceFileListWidget(QListWidget):
"""增强的源文件列表控件,支持多语言检测"""
files_changed = pyqtSignal()
language_detected = pyqtSignal(dict) # 新增:语言检测信号
def __init__(self, parent=None):
super().__init__(parent)
self.analyzer = MultiLanguageCodeQLAnalyzer() # 用于语言检测
self.setAcceptDrops(True)
self.setDragDropMode(QListWidget.InternalMove)
self.setMinimumHeight(160)
self.setMaximumHeight(240)
self.setSelectionMode(QListWidget.ExtendedSelection)
self.setStyleSheet("""
QListWidget {
border: 2px dashed rgba(102, 126, 234, 0.3);
border-radius: 12px;
background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
stop:0 #ffffff, stop:1 #f8f9fa);
padding: 16px;
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
font-size: 13px;
}
QListWidget::item {
padding: 12px 16px;
margin: 4px;
background-color: white;
border: 1px solid #e9ecef;
border-radius: 8px;
color: #495057;
}
QListWidget::item:selected {
background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
stop:0 rgba(102, 126, 234, 0.1), stop:1 rgba(118, 75, 162, 0.1));
border-color: #667eea;
color: #2c3e50;
}
QListWidget::item:hover {
background-color: #f8f9fa;
border-color: #667eea;
}
""")
self.setup_placeholder()
def setup_placeholder(self):
"""设置占位符标签"""
self.placeholder_label = QLabel(
"📁 拖拽源代码文件到此处,或点击添加文件按钮\n"
"💡 支持多种语言: C/C++, Java, JavaScript/TypeScript, Python, C#, Go, Ruby, Swift, Kotlin\n"
"🔍 文件将自动检测语言类型并使用相应的分析策略"
)
self.placeholder_label.setAlignment(Qt.AlignCenter)
self.placeholder_label.setStyleSheet("""
QLabel {
color: #6c757d;
font-style: italic;
background: transparent;
border: none;
font-size: 13px;
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
line-height: 1.4;
}
""")
self.placeholder_label.setParent(self)
self.placeholder_label.setWordWrap(True)
self.update_placeholder()
def resizeEvent(self, event):
"""调整占位符位置"""
super().resizeEvent(event)
if hasattr(self, 'placeholder_label'):
self.placeholder_label.resize(self.width() - 32, 100)
self.placeholder_label.move(16, (self.height() - 100) // 2)
def update_placeholder(self):
"""更新占位符可见性"""
if hasattr(self, 'placeholder_label'):
self.placeholder_label.setVisible(self.count() == 0)
def addItem(self, item):
"""重写添加项目方法"""
super().addItem(item)
self.update_placeholder()
self.detect_and_emit_languages()
def clear(self):
"""重写清空方法"""
super().clear()
self.update_placeholder()
self.files_changed.emit()
self.language_detected.emit({})
def takeItem(self, row):
"""重写移除项目方法"""
result = super().takeItem(row)
self.update_placeholder()
self.files_changed.emit()
self.detect_and_emit_languages()
return result
def detect_and_emit_languages(self):
"""检测文件语言并发出信号"""
file_paths = self.get_file_paths()
if file_paths:
language_files = self.analyzer.detect_language_from_files(file_paths)
self.language_detected.emit(language_files)
else:
self.language_detected.emit({})
def is_source_file(self, file_path: str) -> bool:
"""检查是否为支持的源代码文件"""
# 使用新的分析器检测
language_files = self.analyzer.detect_language_from_files([file_path])
return len(language_files) > 0
def add_file(self, file_path: str):
"""添加文件到列表"""
if self.file_exists(file_path):
return False
# 检测语言并创建相应图标
language_files = self.analyzer.detect_language_from_files([file_path])
if not language_files:
return False # 不支持的文件类型
# 获取检测到的语言
detected_language = list(language_files.keys())[0]
# 语言图标映射
language_icons = {
'cpp': '🔧', 'java': '☕', 'javascript': '🟨',
'python': '🐍', 'csharp': '🔵', 'go': '🐹',
'ruby': '💎', 'swift': '🍎', 'kotlin': '🎯'
}
icon = language_icons.get(detected_language, '📄')
item = QListWidgetItem(f"{icon} {os.path.basename(file_path)} ({detected_language.upper()})")
item.setData(Qt.UserRole, file_path)
item.setToolTip(f"文件: {file_path}\n语言: {detected_language.upper()}\n类型: 源代码文件")
self.addItem(item)
self.files_changed.emit()
return True
def file_exists(self, file_path: str) -> bool:
"""检查文件是否已存在于列表中"""
for i in range(self.count()):
if self.item(i).data(Qt.UserRole) == file_path:
return True
return False
def get_file_paths(self) -> List[str]:
"""获取所有文件路径"""
paths = []
for i in range(self.count()):
paths.append(self.item(i).data(Qt.UserRole))
return paths
def get_selected_file_paths(self) -> List[str]:
"""获取选中的文件路径"""
paths = []
for item in self.selectedItems():
paths.append(item.data(Qt.UserRole))
return paths
def remove_selected(self):
"""移除选中的文件"""
selected_items = self.selectedItems()
if not selected_items:
return
for item in selected_items:
self.takeItem(self.row(item))
def clear_all(self):
"""清空所有文件"""
self.clear()
# 拖拽相关方法保持不变
def dragEnterEvent(self, event):
"""拖拽进入事件"""
if event.mimeData().hasUrls():
valid_files = []
for url in event.mimeData().urls():
file_path = url.toLocalFile()
if os.path.isfile(file_path) and self.is_source_file(file_path):
valid_files.append(file_path)
if valid_files:
event.acceptProposedAction()
self.setStyleSheet(self.styleSheet().replace(
"border: 2px dashed rgba(102, 126, 234, 0.3);",
"border: 2px solid rgba(102, 126, 234, 0.8);"
))
else:
event.ignore()
else:
event.ignore()
def dragLeaveEvent(self, event):
"""拖拽离开事件"""
self.setStyleSheet(self.styleSheet().replace(
"border: 2px solid rgba(102, 126, 234, 0.8);",
"border: 2px dashed rgba(102, 126, 234, 0.3);"
))
super().dragLeaveEvent(event)
def dropEvent(self, event):
"""拖拽放置事件"""
self.setStyleSheet(self.styleSheet().replace(
"border: 2px solid rgba(102, 126, 234, 0.8);",
"border: 2px dashed rgba(102, 126, 234, 0.3);"
))
if event.mimeData().hasUrls():
added_files = []
skipped_files = []
for url in event.mimeData().urls():
file_path = url.toLocalFile()
if os.path.isfile(file_path):
if self.is_source_file(file_path):
if not self.file_exists(file_path):
self.add_file(file_path)
added_files.append(os.path.basename(file_path))
else:
print(f"文件已存在: {file_path}")
else:
skipped_files.append(os.path.basename(file_path))
if added_files or skipped_files:
self.show_drop_feedback(added_files, skipped_files)
event.acceptProposedAction()
self.files_changed.emit()
else:
event.ignore()
def show_drop_feedback(self, added_files: List[str], skipped_files: List[str]):
"""显示拖拽反馈信息"""
message = ""
if added_files:
message += f"✅ 成功添加 {len(added_files)} 个文件:\n"
for file in added_files[:5]:
message += f" • {file}\n"
if len(added_files) > 5:
message += f" • ... 还有 {len(added_files) - 5} 个文件\n"
if skipped_files:
message += f"\n⚠️ 跳过 {len(skipped_files)} 个不支持的文件:\n"
for file in skipped_files[:3]:
message += f" • {file}\n"
if len(skipped_files) > 3:
message += f" • ... 还有 {len(skipped_files) - 3} 个文件\n"
if message:
feedback_label = QLabel(message.strip())
feedback_label.setStyleSheet("""
QLabel {
background-color: rgba(255, 255, 255, 0.95);
color: #2c3e50;
border: 1px solid #667eea;
border-radius: 8px;
padding: 12px;
font-size: 12px;
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
}
""")
feedback_label.setParent(self)
feedback_label.setAlignment(Qt.AlignCenter)
feedback_label.setWordWrap(True)
feedback_label.resize(self.width() - 40, 100)
feedback_label.move(20, (self.height() - 100) // 2)
feedback_label.show()
QTimer.singleShot(3000, feedback_label.deleteLater)
class EnhancedAnalysisOptionsWidget(QFrame):
"""增强的分析选项控件"""
def __init__(self, parent=None):
super().__init__(parent)
self.analyzer = MultiLanguageCodeQLAnalyzer()
self.setFrameShape(QFrame.StyledPanel)
self.setStyleSheet("""
QFrame {
background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
stop:0 #ffffff, stop:1 #f8f9fa);
border-radius: 12px;
border: 1px solid rgba(102, 126, 234, 0.2);
padding: 16px;
margin: 4px;
}
""")
self.init_ui()
def init_ui(self):
layout = QVBoxLayout(self)
layout.setContentsMargins(20, 16, 20, 16)
layout.setSpacing(16)
# 标题
title_label = QLabel("🔍 分析选项配置")
title_label.setStyleSheet("""
QLabel {
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
font-size: 16px;
font-weight: 600;
color: #495057;
margin-bottom: 8px;
}
""")
layout.addWidget(title_label)
# QL脚本选择
ql_layout = QHBoxLayout()
ql_label = QLabel("🔍 检测脚本:")
ql_label.setStyleSheet("""
QLabel {
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
font-weight: 600;
font-size: 14px;
color: #495057;
min-width: 100px;
}
""")
self.ql_script_combo = self.create_modern_combo()
# 按钮布局
button_layout = QHBoxLayout()
button_layout.setSpacing(8)
refresh_ql_btn = self.create_small_button("🔄", "刷新脚本列表")
refresh_ql_btn.clicked.connect(self.load_available_ql_scripts)
browse_ql_btn = self.create_small_button("📁", "浏览自定义QL脚本")
browse_ql_btn.clicked.connect(self.browse_custom_ql_script)
button_layout.addWidget(refresh_ql_btn)
button_layout.addWidget(browse_ql_btn)
ql_layout.addWidget(ql_label)
ql_layout.addWidget(self.ql_script_combo, 1)
ql_layout.addLayout(button_layout)
layout.addLayout(ql_layout)
# 分析模式选择
mode_layout = QVBoxLayout()
mode_label = QLabel("⚙️ 分析模式:")
mode_label.setStyleSheet("""
QLabel {
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
font-weight: 600;
font-size: 14px;
color: #495057;
margin-bottom: 8px;
}
""")
mode_layout.addWidget(mode_label)
# 无构建模式选项
self.no_build_checkbox = QCheckBox("🚀 无构建模式 (推荐)")
self.no_build_checkbox.setChecked(True)
self.no_build_checkbox.setStyleSheet("""
QCheckBox {
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
font-size: 13px;
color: #495057;
padding: 8px;
}
QCheckBox::indicator {
width: 18px;
height: 18px;
border-radius: 3px;
border: 2px solid #667eea;
}
QCheckBox::indicator:checked {
background-color: #667eea;
image: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHZpZXdCb3g9IjAgMCAxNiAxNiIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTEzLjUgNEw2IDExLjVMMi41IDhMMy41IDdMNiA5LjVMMTIuNSAzTDEzLjUgNFoiIGZpbGw9IndoaXRlIi8+Cjwvc3ZnPgo=);
}
""")
mode_layout.addWidget(self.no_build_checkbox)
# 深度分析选项
self.deep_analysis_checkbox = QCheckBox("🔬 深度分析模式")
self.deep_analysis_checkbox.setStyleSheet(self.no_build_checkbox.styleSheet())
mode_layout.addWidget(self.deep_analysis_checkbox)
layout.addLayout(mode_layout)
# QL脚本描述
self.ql_description_label = QLabel()
self.ql_description_label.setStyleSheet("""
QLabel {
color: #6c757d;
font-size: 12px;
padding: 12px;
background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
stop:0 #ffffff, stop:1 #f8f9fa);
border-radius: 8px;
border: 1px solid #e9ecef;
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
line-height: 1.4;
}
""")
self.ql_description_label.setWordWrap(True)
self.ql_script_combo.currentIndexChanged.connect(self.update_ql_description)
layout.addWidget(self.ql_description_label)
# 加载QL脚本
self.load_available_ql_scripts()
def create_modern_combo(self):
"""创建现代化下拉框"""
combo = QComboBox()
combo.setMinimumHeight(40)
combo.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
combo.setStyleSheet("""
QComboBox {
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
border: 2px solid #e9ecef;
border-radius: 10px;
padding: 8px 16px;
background-color: white;
font-size: 13px;
color: #495057;
min-width: 120px;
}
QComboBox:focus {
border-color: #667eea;
background-color: #ffffff;
}
QComboBox:hover {
border-color: #667eea;
}
QComboBox::drop-down {
border: none;
width: 30px;
margin-right: 8px;
}
QComboBox::down-arrow {
image: none;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 6px solid #6c757d;
}
QComboBox QAbstractItemView {
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
border: 2px solid #e9ecef;
border-radius: 10px;
background-color: white;
selection-background-color: #667eea;
selection-color: white;
padding: 4px;
}
QComboBox QAbstractItemView::item {
padding: 8px 16px;
border-radius: 6px;
margin: 2px;
}
""")
return combo
def create_small_button(self, text, tooltip):
"""创建小按钮"""
btn = QPushButton(text)
btn.setMaximumWidth(50)
btn.setToolTip(tooltip)
btn.setStyleSheet("""
QPushButton {
background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
stop:0 #667eea, stop:1 #764ba2);
color: white;
border: none;
border-radius: 8px;
padding: 8px;
font-weight: 500;
font-size: 14px;
}
QPushButton:hover {
background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
stop:0 #5a6fd8, stop:1 #6a4190);
}
QPushButton:pressed {
background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
stop:0 #4e5bc4, stop:1 #5e3785);
}
""")
return btn
def load_available_ql_scripts(self):
"""加载可用的QL脚本"""
self.ql_script_combo.clear()
try:
# 获取增强的多语言脚本
available_scripts = self.analyzer.get_available_ql_scripts()
for script in available_scripts:
self.ql_script_combo.addItem(script['name'], script['path'])
except Exception as e:
print(f"加载QL脚本失败: {e}")
# 添加默认选项
self.ql_script_combo.addItem("增强C/C++检测", "builtin_enhanced_cpp")
# 更新描述
if hasattr(self, 'ql_description_label'):
self.update_ql_description()
def browse_custom_ql_script(self):
"""浏览并选择自定义QL脚本"""
file_path, _ = QFileDialog.getOpenFileName(
self,
"选择自定义QL脚本",
"",
"CodeQL脚本文件 (*.ql);;所有文件 (*)"
)
if file_path and os.path.exists(file_path):
# 检查是否已经添加过这个脚本
for i in range(self.ql_script_combo.count()):
if self.ql_script_combo.itemData(i) == file_path:
self.ql_script_combo.setCurrentIndex(i)
self.show_info_message("脚本已存在", f"脚本 {os.path.basename(file_path)} 已在列表中,已自动选择。")
return
# 添加新的自定义脚本
script_name = f"自定义: {os.path.basename(file_path)}"
self.ql_script_combo.addItem(script_name, file_path)
self.ql_script_combo.setCurrentIndex(self.ql_script_combo.count() - 1)
self.update_ql_description()
self.show_info_message("脚本添加成功",
f"已成功添加自定义QL脚本:\n{os.path.basename(file_path)}\n\n"
f"路径:{file_path}")
def update_ql_description(self):
"""更新QL脚本描述"""
if not hasattr(self, 'ql_description_label'):
return
current_index = self.ql_script_combo.currentIndex()
if current_index >= 0:
script_path = self.ql_script_combo.itemData(current_index)
script_name = self.ql_script_combo.itemText(current_index)
if script_path and script_path.startswith('builtin_enhanced_'):
language = script_path.replace('builtin_enhanced_', '')
description = f"增强的{language.upper()}侧信道攻击和Spectre漏洞检测脚本,包括:\n" \
f"• 推测执行漏洞检测 (BCB, PHT, BTB)\n" \
f"• 时序侧信道攻击检测\n" \
f"• 缓存侧信道攻击检测\n" \
f"• 内存访问模式分析\n" \
f"• 密码学时序攻击检测"
elif script_name.startswith("自定义: "):
if os.path.exists(script_path):
try:
with open(script_path, 'r', encoding='utf-8') as f:
content = f.read(500)
import re
desc_match = re.search(r'\*\s*@description\s+(.+)', content)
if desc_match:
description = desc_match.group(1).strip()
else:
name_match = re.search(r'\*\s*@name\s+(.+)', content)
if name_match:
description = name_match.group(1).strip()
else:
description = "用户自定义的QL检测脚本"
except Exception as e:
description = f"无法读取脚本信息: {str(e)}"
description += f"\n📍 文件路径: {script_path}"
else:
description = f"⚠️ 脚本文件不存在: {script_path}"
else:
description = script_name
self.ql_description_label.setText(f"📄 {script_name}\n\n{description}")
def show_info_message(self, title, message):
"""显示信息消息"""
msg_box = QMessageBox(self)
msg_box.setWindowTitle(title)
msg_box.setIcon(QMessageBox.Information)
msg_box.setStyleSheet("""
QMessageBox {
background-color: white;
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
font-size: 14px;
}
QMessageBox QLabel {
color: #2c3e50;
font-size: 14px;
padding: 10px;
background-color: transparent;
}
QMessageBox QPushButton {
background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
stop:0 #667eea, stop:1 #764ba2);
color: white;
border: none;
border-radius: 8px;
padding: 8px 16px;
font-weight: 500;
font-size: 13px;
min-width: 80px;
}
""")
msg_box.setText(message)
msg_box.setStandardButtons(QMessageBox.Ok)
msg_box.button(QMessageBox.Ok).setText("确定")
msg_box.exec_()
def get_selected_ql_script(self) -> Optional[str]:
"""获取当前选择的QL脚本路径"""
current_index = self.ql_script_combo.currentIndex()
if current_index >= 0:
return self.ql_script_combo.itemData(current_index)
return None
def is_no_build_mode(self) -> bool:
"""是否使用无构建模式"""
return self.no_build_checkbox.isChecked()
def is_deep_analysis(self) -> bool:
"""是否使用深度分析"""
return self.deep_analysis_checkbox.isChecked()
class EnhancedSourceCodeAnalysisTab(QWidget):
"""增强的源代码分析标签页"""
def __init__(self, parent=None):
super().__init__(parent)
self.analysis_thread = None
self.analysis_results = []
self.current_language_files = {}
self.init_ui()
def init_ui(self):
# 创建主滚动区域
self.scroll_area = QScrollArea()
self.scroll_area.setWidgetResizable(True)
self.scroll_area.setFrameShape(QFrame.NoFrame)
# 创建内容容器
content_widget = QWidget()
content_widget.setStyleSheet("background-color: #f8f9fa;")
# 主布局
layout = QVBoxLayout(content_widget)
layout.setContentsMargins(20, 20, 20, 20)
layout.setSpacing(20)
# 文件选择区域
file_section = self.create_file_section()
layout.addWidget(file_section)
# 分析选项区域
options_section = self.create_options_section()
layout.addWidget(options_section)
# 分析控制区域
control_section = self.create_control_section()
layout.addWidget(control_section)
# 结果显示区域
results_section = self.create_results_section()
layout.addWidget(results_section)
# 设置内容到滚动区域
self.scroll_area.setWidget(content_widget)
# 主窗口布局
main_layout = QVBoxLayout(self)
main_layout.setContentsMargins(0, 0, 0, 0)
main_layout.addWidget(self.scroll_area)
def create_file_section(self):
"""创建文件选择区域"""
section = QFrame()
section.setFrameShape(QFrame.StyledPanel)
section.setStyleSheet("""
QFrame {
background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
stop:0 #ffffff, stop:1 #f8f9fa);
border-radius: 16px;
border: 1px solid rgba(230, 230, 250, 0.8);
margin: 8px;
padding: 0px;
}
""")
# 添加阴影效果
shadow = QGraphicsDropShadowEffect()
shadow.setBlurRadius(20)
shadow.setColor(QColor(0, 0, 0, 15))
shadow.setOffset(0, 4)
section.setGraphicsEffect(shadow)
layout = QVBoxLayout(section)
layout.setContentsMargins(24, 20, 24, 24)
layout.setSpacing(16)
# 标题
title_layout = QHBoxLayout()
title_layout.setSpacing(12)
icon_label = QLabel("📁")
icon_label.setStyleSheet("font-size: 20px; color: #667eea;")
title_label = QLabel("多语言源代码文件")
title_label.setStyleSheet("""
QLabel {
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
font-size: 18px;
font-weight: 600;
color: #2c3e50;
}
""")
title_layout.addWidget(icon_label)
title_layout.addWidget(title_label)
title_layout.addStretch()
layout.addLayout(title_layout)
# 文件列表和语言信息的水平布局
content_layout = QHBoxLayout()
content_layout.setSpacing(20)
# 文件列表
self.file_list = EnhancedSourceFileListWidget()
self.file_list.files_changed.connect(self.update_analyze_button_state)
self.file_list.language_detected.connect(self.update_language_info)
# 语言信息显示
self.language_info_widget = LanguageInfoWidget()
self.language_info_widget.setFixedWidth(280)
content_layout.addWidget(self.file_list, 2)
content_layout.addWidget(self.language_info_widget, 1)
layout.addLayout(content_layout)
# 文件操作按钮
button_layout = QHBoxLayout()
button_layout.setSpacing(12)
add_file_btn = self.create_action_button("➕ 添加文件", "primary")
add_file_btn.clicked.connect(self.add_files)
remove_file_btn = self.create_action_button("🗑️ 移除选中", "danger")
remove_file_btn.clicked.connect(self.file_list.remove_selected)
clear_files_btn = self.create_action_button("🧹 清空所有", "secondary")
clear_files_btn.clicked.connect(self.file_list.clear_all)
button_layout.addWidget(add_file_btn)
button_layout.addWidget(remove_file_btn)
button_layout.addWidget(clear_files_btn)
button_layout.addStretch()
layout.addLayout(button_layout)
return section
def create_options_section(self):
"""创建分析选项区域"""
self.options_widget = EnhancedAnalysisOptionsWidget()
return self.options_widget
def create_control_section(self):
"""创建分析控制区域"""
section = QFrame()
section.setFrameShape(QFrame.StyledPanel)
section.setStyleSheet("""
QFrame {
background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
stop:0 #ffffff, stop:1 #f8f9fa);
border-radius: 16px;
border: 1px solid rgba(230, 230, 250, 0.8);
margin: 8px;
padding: 0px;
}
""")
# 添加阴影效果
shadow = QGraphicsDropShadowEffect()
shadow.setBlurRadius(20)
shadow.setColor(QColor(0, 0, 0, 15))
shadow.setOffset(0, 4)
section.setGraphicsEffect(shadow)
layout = QVBoxLayout(section)
layout.setContentsMargins(24, 20, 24, 24)
layout.setSpacing(16)
# 标题
title_layout = QHBoxLayout()
title_layout.setSpacing(12)
icon_label = QLabel("🎮")
icon_label.setStyleSheet("font-size: 20px; color: #667eea;")
title_label = QLabel("分析控制")
title_label.setStyleSheet("""
QLabel {
font-family: 'Segoe UI', 'Microsoft YaHei', 'Arial';
font-size: 18px;
font-weight: 600;
color: #2c3e50;
}
""")
title_layout.addWidget(icon_label)
title_layout.addWidget(title_label)
title_layout.addStretch()
layout.addLayout(title_layout)
# 进度条
self.progress_bar = self.create_progress_bar()