-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter_gui.py
More file actions
1407 lines (1162 loc) · 59.1 KB
/
Copy pathconverter_gui.py
File metadata and controls
1407 lines (1162 loc) · 59.1 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
# -*- coding: utf-8 -*-
"""
Moodle XML Converter — GUI (PyQt5)
Графический интерфейс для конвертера Word -> Moodle XML.
"""
import os
import sys
import re
import unicodedata
import traceback
from typing import List, Optional, Dict
from copy import deepcopy
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QGroupBox, QPushButton, QLabel, QLineEdit, QFileDialog,
QComboBox, QCheckBox, QTextEdit, QProgressBar, QSplitter,
QMessageBox, QTreeWidget, QTreeWidgetItem, QHeaderView,
QAbstractItemView, QFrame
)
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QUrl
from PyQt5.QtGui import QColor, QFont, QBrush
# Подключаем конвертер
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from universal_moodle_converter_v3_stable import (
VALID_MARKERS, MARKER_TO_QTYPE, MoodleConverter,
QuestionTypeDetector,
)
try:
import docxlatex
except ImportError:
docxlatex = None
from lxml import etree
# ============================================================
# МАРКЕРЫ И ИХ ОПИСАНИЯ
# ============================================================
MARKER_DESCRIPTIONS = {
'': '(авто)',
'multichoice_one': 'Один правильный',
'multichoice_many': 'Несколько правильных (-100%)',
'shortanswer_phrase': 'Текстовый ввод',
'numerical_partial': 'Цифры partial scoring',
'numerical_numcombo': 'Цифры любой порядок',
'matching': 'Соотношение L/R',
'match_123': 'Последовательность',
'match': 'Соотношение (=matching)',
'ddmatch': 'Drag-and-drop',
'gapselect': 'Выпадающие списки',
'cloze': 'Встроенные ответы',
'numerical': 'Числовой (. = ,)',
}
MARKER_COLORS = {
'multichoice_one': QColor(200, 230, 255),
'multichoice_many': QColor(180, 220, 255),
'shortanswer_phrase': QColor(200, 255, 200),
'numerical_partial': QColor(220, 255, 200),
'numerical_numcombo': QColor(240, 255, 200),
'matching': QColor(255, 230, 200),
'match_123': QColor(255, 220, 180),
'match': QColor(255, 230, 200),
'ddmatch': QColor(255, 210, 210),
'gapselect': QColor(230, 200, 255),
'cloze': QColor(255, 255, 200),
'numerical': QColor(200, 255, 255),
}
COLOR_CORRECT = QColor(0, 130, 0)
COLOR_WRONG = QColor(180, 0, 0)
COLOR_TEXT = QColor(50, 50, 50)
COLOR_META = QColor(100, 100, 140)
COLOR_ERROR = QColor(255, 220, 220)
# ============================================================
# ПАРСЕР DOCX ДЛЯ ПРЕДПРОСМОТРА
# ============================================================
class ParsedQuestion:
"""Один вопрос, извлечённый из docx."""
__slots__ = ('name', 'grade', 'marker', 'subcategory',
'content', 'auto_type', 'errors', 'line_num', 'selected', 'image_data')
def __init__(self):
self.name: str = ''
self.grade: float = 1.0
self.marker: str = ''
self.subcategory: str = ''
self.content: List[str] = []
self.auto_type: str = ''
self.errors: List[str] = []
self.line_num: int = 0
self.selected: bool = True
self.image_data: Dict[str, str] = {}
def parse_docx_preview(docx_path: str) -> tuple:
"""Парсит docx и возвращает (questions, global_errors)."""
questions: List[ParsedQuestion] = []
errors: List[str] = []
if docxlatex is None:
errors.append('docxlatex не установлен (pip install docxlatex)')
return questions, errors
try:
from universal_moodle_converter_v3_stable import ImageProcessor
img_proc = ImageProcessor()
image_data = img_proc.extract_images(docx_path)
except Exception as e:
image_data = {}
try:
doc = docxlatex.Document(docx_path)
text = doc.get_text()
lines = text.split('\n')
except Exception as e:
errors.append(f'Ошибка чтения файла: {e}')
return questions, errors
current_marker = ''
current_subcategory = ''
current_q: Optional[ParsedQuestion] = None
for line_idx, line in enumerate(lines):
line = unicodedata.normalize('NFC', line.strip())
if not line:
continue
if line.startswith('V1:'):
continue
m = re.match(r'^\{(\w+)\}V2:(.*)', line)
if m:
current_marker = m.group(1)
current_subcategory = m.group(2).strip()
if current_marker not in VALID_MARKERS:
errors.append(f'Строка {line_idx+1}: неизвестный маркер {{{current_marker}}}')
continue
if line.startswith('V2:'):
current_subcategory = line[3:].strip()
continue
# Определяем начало вопроса (7 форматов)
is_question = line.startswith('I:')
if not is_question:
if line.startswith('I I:'):
line = 'I:' + line[4:]; is_question = True
elif line.startswith('I ') and 'Задание' in line:
line = 'I:' + line[2:]; is_question = True
elif line.startswith(':Задание'):
line = 'I:' + line[1:]; is_question = True
elif line.startswith('Задание ') and ', b=' in line:
if re.search(r'ТЗ\d+-\d+|Т\d+-\d+|T\d+-\d+', line):
line = 'I:' + line; is_question = True
elif re.match(r'^[A-Za-z0-9_\-=]+Задание', line) and ', b=' in line:
if re.search(r'ТЗ\d+-\d+|Т\d+-\d+|T\d+-\d+', line):
mm = re.match(r'^[A-Za-z0-9_\-=]+(Задание.*)', line)
if mm:
line = 'I:' + mm.group(1); is_question = True
elif re.match(r'^[А-Яа-яёЁ]+\s+[А-Яа-яёЁ]\.[А-Яа-яёЁ]\.,', line) and ', b=' in line:
tz = re.search(r'ТЗ(\d+)-(\d+)', line)
if tz:
line = f'I:Задание {tz.group(2)}. {line}'; is_question = True
if is_question:
if current_q is not None:
current_q.auto_type = QuestionTypeDetector.detect(
current_q.content, '', current_q.marker)
questions.append(current_q)
current_q = ParsedQuestion()
current_q.name = line
current_q.marker = current_marker
current_q.subcategory = current_subcategory
current_q.line_num = line_idx + 1
current_q.image_data = image_data
gm = re.search(r'b=(\d+)', line)
current_q.grade = float(gm.group(1)) if gm else 1.0
continue
if current_q is not None:
is_ans = (line.startswith('+:') or line.startswith('-:') or
(len(line) > 1 and line[0] in '+-' and line[1] in ' \t'))
if line.startswith(('S:', '->', '=>')) or is_ans or \
not line.startswith(('V1:', 'V2:', 'I:')):
current_q.content.append(line)
if current_q is not None:
current_q.auto_type = QuestionTypeDetector.detect(
current_q.content, '', current_q.marker)
questions.append(current_q)
# Предобработка ошибок
for q in questions:
has_plus = any(c.startswith('+:') or c.startswith('+') for c in q.content)
has_lr = any(re.match(r'^[LR]\d+:', c) for c in q.content)
has_num = any(re.match(r'^\d+:', c) for c in q.content)
skip = ('cloze', 'matching', 'match_123', 'match',
'shortanswer_phrase', 'numerical_numcombo')
if not has_plus and q.auto_type not in skip and not has_lr and not has_num:
q.errors.append('Нет правильного ответа (+:)')
if not q.content:
q.errors.append('Нет текста вопроса')
if not q.name:
q.errors.append('Пустое имя вопроса')
return questions, errors
# ============================================================
# ВАЛИДАТОР XML (ПОСТОБРАБОТКА)
# ============================================================
class XMLValidator:
MOODLE_QTYPES = {
'multichoice', 'shortanswer', 'matching', 'cloze',
'ddmatch', 'gapselect', 'numerical', 'category',
}
@staticmethod
def validate(xml_path: str) -> List[str]:
issues: List[str] = []
try:
tree = etree.parse(xml_path)
root = tree.getroot()
except Exception as e:
issues.append(f'XML не парсится: {e}')
return issues
if root.tag != 'quiz':
issues.append(f'Корневой элемент: {root.tag} (ожидается quiz)')
n = 0
for q in root.findall('.//question'):
qtype = q.get('type', '')
if qtype == 'category':
continue
n += 1
ne = q.find('name/text')
qn = ne.text if ne is not None and ne.text else f'#{n}'
if qtype not in XMLValidator.MOODLE_QTYPES:
issues.append(f'{qn}: неизвестный тип "{qtype}"')
qt = q.find('questiontext/text')
if qt is None or not qt.text:
issues.append(f'{qn}: пустой текст')
else:
txt = qt.text
for pat in ['_IMAGE_', '@@PLUGINFILE@@']:
if pat in txt and not any(f.text for f in q.findall('.//file')):
issues.append(f'{qn}: маркер {pat} без файла')
ans = q.findall('answer')
if qtype in ('multichoice', 'shortanswer', 'numerical') and not ans:
issues.append(f'{qn}: нет ответов')
for fe in q.findall('.//file'):
if fe.get('encoding') == 'base64' and len(fe.text or '') < 20:
issues.append(f'{qn}: пустая base64 картинка')
if qtype == 'matching':
sqs = q.findall('subquestion')
if not sqs:
issues.append(f'{qn}: matching без subquestion')
for sq in sqs:
if sq.find('answer') is None:
issues.append(f'{qn}: subquestion без answer')
if qtype == 'ddmatch' and not q.findall('subquestion'):
issues.append(f'{qn}: ddmatch без subquestion')
if qtype == 'gapselect' and not q.findall('selectoption'):
issues.append(f'{qn}: gapselect без selectoption')
if n == 0:
issues.append('Файл не содержит вопросов')
return issues
# ============================================================
# РАЗДЕЛЕНИЕ XML НА ЧАСТИ
# ============================================================
def split_xml_by_size(xml_path: str, max_bytes: int = 1_000_000) -> List[str]:
"""Разделяет XML файл на части до max_bytes.
Каждая часть начинается с дубликата категорий.
"""
import re
sz = os.path.getsize(xml_path)
if sz <= max_bytes:
return [xml_path]
with open(xml_path, 'r', encoding='utf-8') as f:
content = f.read()
quiz_start = content.find('<quiz>')
quiz_end = content.rfind('</quiz>')
xml_decl = content[:quiz_start]
inner = content[quiz_start + 6:quiz_end]
# Извлекаем первые две категории для дублирования
category_matches = list(re.finditer(r'(<question type="category">.*?</question>)', inner, re.DOTALL))
categories = [m.group(1) for m in category_matches[:2]]
# Все вопросы
all_items = list(re.finditer(r'(<question[^>]*>.*?</question>)', inner, re.DOTALL))
parts = []
current_part = ""
current_size = 0
part_num = 1
for match in all_items:
item = match.group(1)
# Пропускаем категории (они уже сохранены)
if '<question type="category">' in item:
continue
item_size = len(item.encode('utf-8'))
if current_size + item_size > max_bytes and current_size > 0:
parts.append(current_part)
part_num += 1
current_part = item
current_size = item_size
else:
current_part += item
current_size += item_size
if current_part:
parts.append(current_part)
base, ext = os.path.splitext(xml_path)
result_parts = []
for i, part_content in enumerate(parts, 1):
full_xml = f"{xml_decl}<quiz>{''.join(categories)}\n{part_content}</quiz>"
pp = f'{base}_part{i}{ext}'
with open(pp, 'w', encoding='utf-8') as out:
out.write(full_xml)
result_parts.append(pp)
return result_parts
# ============================================================
# РАБОЧИЙ ПОТОК
# ============================================================
class ConvertWorker(QThread):
progress = pyqtSignal(int, str)
finished = pyqtSignal(bool, str)
validation = pyqtSignal(list)
def __init__(self, docx_path, output_path, questions, split_xml, selected_indices=None, parent=None):
super().__init__(parent)
self.docx_path = docx_path
self.output_path = output_path
self.questions = questions
self.split_xml = split_xml
self.selected_indices = selected_indices
def run(self):
try:
self.progress.emit(10, 'Запуск конвертации...')
conv = MoodleConverter(self.docx_path, self.output_path, self.selected_indices)
conv._marker_overrides = {}
for q in self.questions:
if q.marker and getattr(q, 'selected', True):
conv._marker_overrides[q.name] = q.marker
self.progress.emit(30, 'Конвертация...')
ok = conv.convert()
if not ok:
self.finished.emit(False, '\n'.join(conv.errors))
return
self.progress.emit(70, 'Валидация XML...')
issues = XMLValidator.validate(self.output_path)
self.validation.emit(issues)
if self.split_xml:
self.progress.emit(85, 'Разделение на части...')
parts = split_xml_by_size(self.output_path)
if len(parts) > 1:
self.progress.emit(95, f'Разделено на {len(parts)} частей')
else:
self.progress.emit(95, 'Файл < 1МБ, разделение не нужно')
self.progress.emit(100, 'Готово!')
self.finished.emit(True, self.output_path)
except Exception as e:
self.finished.emit(False, f'Ошибка: {e}\n{traceback.format_exc()}')
# ============================================================
# ГЛАВНОЕ ОКНО
# ============================================================
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Moodle XML Converter')
self.setMinimumSize(1100, 780)
self.questions: List[ParsedQuestion] = []
self.worker = None
self._init_ui()
# ---------- UI ----------
def _init_ui(self):
central = QWidget()
self.setCentralWidget(central)
lay = QVBoxLayout(central)
lay.setContentsMargins(8, 8, 8, 8)
# --- Файлы ---
grp = QGroupBox('Файлы')
gl = QVBoxLayout(grp)
r1 = QHBoxLayout()
r1.addWidget(QLabel('Файл DOCX:'))
self.input_edit = QLineEdit()
self.input_edit.setPlaceholderText('Выберите файл .docx...')
r1.addWidget(self.input_edit, 1)
b1 = QPushButton('Обзор...')
b1.clicked.connect(self._browse_input)
r1.addWidget(b1)
gl.addLayout(r1)
r2 = QHBoxLayout()
r2.addWidget(QLabel('Папка вывода:'))
self.output_edit = QLineEdit()
self.output_edit.setPlaceholderText('Папка для сохранения XML...')
r2.addWidget(self.output_edit, 1)
b2 = QPushButton('Обзор...')
b2.clicked.connect(self._browse_output)
r2.addWidget(b2)
gl.addLayout(r2)
r3 = QHBoxLayout()
self.chk_split = QCheckBox('Разделить XML на части до 1 МБ')
r3.addWidget(self.chk_split)
r3.addStretch()
bp = QPushButton(' Предпросмотр ')
bp.setStyleSheet('font-weight:bold; padding:6px 18px;')
bp.clicked.connect(self._do_preview)
r3.addWidget(bp)
bc = QPushButton(' Конвертировать ')
bc.setStyleSheet('font-weight:bold; padding:6px 18px; background:#4CAF50; color:white;')
bc.clicked.connect(self._do_convert)
r3.addWidget(bc)
gl.addLayout(r3)
lay.addWidget(grp)
# --- Центр: дерево (50%) + предпросмотр Moodle (50%) ---
h_splitter = QSplitter(Qt.Horizontal)
h_splitter.setHandleWidth(1)
# Левая панель: дерево вопросов + лог
v_splitter = QSplitter(Qt.Vertical)
v_splitter.setHandleWidth(1)
# Компактная панель с чекбоксом и счётчиком (в одну строку)
header_widget = QWidget()
header_layout = QHBoxLayout(header_widget)
header_layout.setContentsMargins(5, 2, 5, 2)
header_layout.setSpacing(10)
self.chk_select_all = QCheckBox('Выделить все')
self.chk_select_all.setChecked(True)
self.chk_select_all.stateChanged.connect(self._toggle_select_all)
header_layout.addWidget(self.chk_select_all)
self.lbl_selected = QLabel('Выбрано: 0 / 0')
header_layout.addWidget(self.lbl_selected)
header_layout.addStretch()
header_widget.setStyleSheet('background: #f0f0f0; border-bottom: 1px solid #ccc;')
header_widget.setFixedHeight(32)
v_splitter.addWidget(header_widget)
self.tree = QTreeWidget()
self.tree.setHeaderLabels(['✓', '#', 'Имя / Содержимое', 'Маркер', 'Тип', 'Балл', 'Ошибки'])
self.tree.setColumnCount(7)
hdr = self.tree.header()
hdr.setStretchLastSection(False)
hdr.setSectionResizeMode(0, QHeaderView.Fixed)
hdr.setSectionResizeMode(1, QHeaderView.Fixed)
hdr.setSectionResizeMode(2, QHeaderView.Stretch)
hdr.setSectionResizeMode(3, QHeaderView.ResizeToContents)
hdr.setSectionResizeMode(4, QHeaderView.ResizeToContents)
hdr.setSectionResizeMode(5, QHeaderView.ResizeToContents)
hdr.setSectionResizeMode(6, QHeaderView.ResizeToContents)
self.tree.setColumnWidth(0, 30)
self.tree.setColumnWidth(1, 45)
self.tree.setColumnWidth(3, 180)
self.tree.setColumnWidth(4, 140)
self.tree.setColumnWidth(5, 50)
self.tree.setAlternatingRowColors(True)
self.tree.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.tree.setRootIsDecorated(True)
self.tree.setAnimated(True)
self.tree.setFont(QFont('Segoe UI', 9))
self.tree.itemChanged.connect(self._on_item_check_changed)
self.tree.itemClicked.connect(self._on_tree_item_clicked)
self.tree.currentItemChanged.connect(self._on_tree_current_changed)
v_splitter.addWidget(self.tree)
self.log_text = QTextEdit()
self.log_text.setReadOnly(True)
self.log_text.setMaximumHeight(200)
self.log_text.setFont(QFont('Consolas', 9))
v_splitter.addWidget(self.log_text)
v_splitter.setSizes([150, 500, 150])
# Правая панель: предпросмотр Moodle
preview_group = QGroupBox('Предпросмотр Moodle')
preview_layout = QVBoxLayout(preview_group)
self.preview_web = QWebEngineView()
self.preview_web.setStyleSheet('background: #fafafa; border: 1px solid #ccc;')
preview_layout.addWidget(self.preview_web)
h_splitter.addWidget(v_splitter)
h_splitter.addWidget(preview_group)
h_splitter.setSizes([500, 500])
lay.addWidget(h_splitter, 1)
# --- Низ ---
bot = QHBoxLayout()
self.progress = QProgressBar()
self.progress.setTextVisible(True)
bot.addWidget(self.progress, 1)
self.status_label = QLabel('Готов')
bot.addWidget(self.status_label)
lay.addLayout(bot)
# ---------- Browse ----------
def _browse_input(self):
p, _ = QFileDialog.getOpenFileName(self, 'Файл DOCX', '',
'Word (*.docx);;Все (*)')
if p:
self.input_edit.setText(p)
if not self.output_edit.text():
self.output_edit.setText(os.path.dirname(p))
def _browse_output(self):
p = QFileDialog.getExistingDirectory(self, 'Папка для сохранения')
if p:
self.output_edit.setText(p)
# ---------- Preview ----------
def _do_preview(self):
dp = self.input_edit.text().strip()
if not dp or not os.path.isfile(dp):
QMessageBox.warning(self, 'Ошибка', 'Выберите файл DOCX')
return
self.log_text.clear()
self.log('Парсинг файла...')
self.status_label.setText('Парсинг...')
QApplication.processEvents()
self.questions, errs = parse_docx_preview(dp)
for e in errs:
self.log(f'[ОШИБКА] {e}', 'red')
self.log(f'Найдено вопросов: {len(self.questions)}')
ec = sum(1 for q in self.questions if q.errors)
if ec:
self.log(f'Вопросов с ошибками: {ec}', 'orange')
self._fill_tree()
self._update_selected_count()
def _fill_tree(self):
self.tree.clear()
marker_list = [''] + sorted(VALID_MARKERS)
current_category = None
category_indices = [] # Индексы вопросов текущей категории
for idx, q in enumerate(self.questions):
# Проверяем смену категории (маркера блока)
if q.marker != current_category:
# Если была предыдущая категория с вопросами - добавляем её чекбокс перед новой
if category_indices and current_category is not None:
self._add_category_separator(current_category, category_indices)
current_category = q.marker
category_indices = [idx]
else:
category_indices.append(idx)
# --- Родительский элемент (заголовок вопроса) ---
item = QTreeWidgetItem()
item.setCheckState(0, Qt.Checked) # Галочка по умолчанию
item.setData(0, Qt.UserRole, idx) # Сохраняем индекс вопроса
item.setText(1, str(idx + 1))
item.setTextAlignment(1, Qt.AlignCenter)
name_clean = q.name.replace('I:', '').strip()
if len(name_clean) > 90:
name_clean = name_clean[:87] + '...'
item.setText(2, name_clean)
item.setToolTip(2, q.name)
# Маркер — текст (комбобокс встроим позже)
item.setText(3, '')
item.setText(4, q.auto_type)
item.setText(5, str(q.grade))
item.setTextAlignment(5, Qt.AlignCenter)
err_text = '; '.join(q.errors) if q.errors else ''
item.setText(6, err_text)
# Цвета
bg = MARKER_COLORS.get(q.marker, QColor(255, 255, 255))
if q.errors:
bg = COLOR_ERROR
for c in range(7):
item.setBackground(c, bg)
if q.errors:
item.setForeground(6, QBrush(QColor(180, 0, 0)))
# Делаем расширяемым
item.setChildIndicatorPolicy(QTreeWidgetItem.ShowIndicator)
# --- Дочерние элементы: тело вопроса ---
for line in q.content:
child = QTreeWidgetItem()
child.setFlags(child.flags() & ~Qt.ItemIsSelectable)
# Галочка не нужна для дочерних элементов
child.setCheckState(0, Qt.Unchecked)
display = line
if len(display) > 150:
display = display[:147] + '...'
child.setText(2, display)
child.setToolTip(2, line)
# Окраска строк содержимого
if line.startswith('+:') or (line.startswith('+') and len(line) > 1 and line[1] in ' \t'):
child.setForeground(2, QBrush(COLOR_CORRECT))
child.setText(1, '+')
child.setForeground(1, QBrush(COLOR_CORRECT))
elif line.startswith('-:') or (line.startswith('-') and len(line) > 1 and line[1] in ' \t'):
child.setForeground(2, QBrush(COLOR_WRONG))
child.setText(1, '-')
child.setForeground(1, QBrush(COLOR_WRONG))
elif line.startswith('S:'):
child.setForeground(2, QBrush(COLOR_TEXT))
child.setText(1, 'S')
child.setForeground(1, QBrush(COLOR_META))
elif re.match(r'^[LR]\d+:', line):
child.setForeground(2, QBrush(COLOR_META))
child.setText(1, line[:2])
elif re.match(r'^\d+:', line):
child.setForeground(2, QBrush(COLOR_META))
child.setText(1, '#')
else:
child.setForeground(2, QBrush(COLOR_TEXT))
item.addChild(child)
self.tree.addTopLevelItem(item)
# Комбобокс маркера
combo = QComboBox()
combo.setFont(QFont('Segoe UI', 8))
for m in marker_list:
desc = MARKER_DESCRIPTIONS.get(m, m)
label = f'{m} ({desc})' if m else desc
combo.addItem(label, m)
ci = marker_list.index(q.marker) if q.marker in marker_list else 0
combo.setCurrentIndex(ci)
combo.currentIndexChanged.connect(
lambda i, row=idx, cb=combo: self._marker_changed(row, cb))
self.tree.setItemWidget(item, 2, combo)
self.tree.expandAll()
# Свернём обратно, чтобы пользователь раскрывал вручную
self.tree.collapseAll()
# Добавляем последнюю категорию
if category_indices and current_category is not None:
self._add_category_separator(current_category, category_indices)
def _add_category_separator(self, marker, indices):
"""Добавляет разделитель категории с чекбоксом для выбора всех вопросов категории."""
desc = MARKER_DESCRIPTIONS.get(marker, marker) if marker else 'Без маркера'
label = f'✓ Выбрать все ({desc})'
sep = QTreeWidgetItem()
sep.setText(1, '▸')
sep.setText(2, label)
sep.setCheckState(0, Qt.Checked)
sep.setData(0, Qt.UserRole, {'category': True, 'indices': indices})
# Серый фон для разделителя
for c in range(7):
sep.setBackground(c, QColor(220, 220, 220))
sep.setForeground(2, QBrush(QColor(80, 80, 80)))
self.tree.addTopLevelItem(sep)
def _marker_changed(self, row, combo):
mk = combo.currentData()
if row >= len(self.questions):
return
q = self.questions[row]
q.marker = mk
q.auto_type = QuestionTypeDetector.detect(q.content, '', mk)
item = self.tree.topLevelItem(row)
if item:
item.setText(3, q.auto_type)
bg = MARKER_COLORS.get(mk, QColor(255, 255, 255))
if q.errors:
bg = COLOR_ERROR
for c in range(6):
item.setBackground(c, bg)
# ---------- Convert ----------
def _do_convert(self):
dp = self.input_edit.text().strip()
od = self.output_edit.text().strip()
if not dp or not os.path.isfile(dp):
QMessageBox.warning(self, 'Ошибка', 'Выберите файл DOCX')
return
if not od:
QMessageBox.warning(self, 'Ошибка', 'Укажите папку для сохранения')
return
os.makedirs(od, exist_ok=True)
bn = os.path.splitext(os.path.basename(dp))[0]
out = os.path.join(od, bn + '.xml')
if not self.questions:
self._do_preview()
# Проверяем выбранные вопросы
selected_indices = [i for i, q in enumerate(self.questions) if getattr(q, 'selected', True)]
if not selected_indices:
QMessageBox.warning(self, 'Ошибка', 'Не выбрано ни одного вопроса')
return
eq = [q for q in self.questions if q.errors and getattr(q, 'selected', True)]
if eq:
r = QMessageBox.question(
self, 'Предупреждение',
f'{len(eq)} выбранных вопросов с ошибками.\nПродолжить?',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if r != QMessageBox.Yes:
return
self.log('\n' + '=' * 60)
self.log(f'Запуск конвертации ({len(selected_indices)} вопросов)...')
self.progress.setValue(0)
self.status_label.setText('Конвертация...')
self.worker = ConvertWorker(dp, out, self.questions, self.chk_split.isChecked(), selected_indices)
self.worker.progress.connect(self._on_progress)
self.worker.finished.connect(self._on_finished)
self.worker.validation.connect(self._on_validation)
self.worker.start()
def _on_progress(self, pct, msg):
self.progress.setValue(pct)
self.status_label.setText(msg)
self.log(msg)
def _on_validation(self, issues):
if issues:
self.log(f'\n--- Постобработка: {len(issues)} проблем ---', 'orange')
for i in issues[:50]:
self.log(f' [!] {i}', 'orange')
if len(issues) > 50:
self.log(f' ... и ещё {len(issues) - 50}', 'orange')
else:
self.log('Постобработка: XML корректен', 'green')
def _on_finished(self, ok, result):
if ok:
self.log(f'\nСохранено: {result}', 'green')
mb = os.path.getsize(result) / (1024 * 1024)
self.log(f'Размер: {mb:.2f} МБ')
self.status_label.setText('Готово!')
QMessageBox.information(self, 'Готово',
f'Конвертация завершена!\n\nФайл: {result}\nРазмер: {mb:.2f} МБ')
else:
self.log(f'\nОШИБКА: {result}', 'red')
self.status_label.setText('Ошибка!')
QMessageBox.critical(self, 'Ошибка', result)
self.worker = None
# ---------- Log ----------
def log(self, text, color='black'):
self.log_text.append(f'<span style="color:{color}">{text}</span>')
sb = self.log_text.verticalScrollBar()
sb.setValue(sb.maximum())
# ---------- Checkbox ----------
def _on_item_check_changed(self, item, column):
if column == 0:
idx = item.data(0, Qt.UserRole)
checked = item.checkState(0) == Qt.Checked
# Проверяем: это категория или отдельный вопрос
if isinstance(idx, dict) and idx.get('category'):
# Это чекбокс категории - выбрать/снять все вопросы в ней
indices = idx.get('indices', [])
for q_idx in indices:
if q_idx < len(self.questions):
self.questions[q_idx].selected = checked
# Обновляем чекбоксы вопросов в дереве
self._refresh_question_checkboxes(indices)
elif idx is not None:
# Обычный вопрос
self.questions[idx].selected = checked
self._update_selected_count()
def _refresh_question_checkboxes(self, indices):
"""Обновляет чекбоксы конкретных вопросов в дереве."""
for i in range(self.tree.topLevelItemCount()):
item = self.tree.topLevelItem(i)
if item:
item_idx = item.data(0, Qt.UserRole)
if item_idx is not None and not isinstance(item_idx, dict):
if item_idx in indices:
q = self.questions[item_idx]
item.setCheckState(0, Qt.Checked if q.selected else Qt.Unchecked)
def _toggle_select_all(self, state):
checked = state == Qt.Checked
for i, q in enumerate(self.questions):
q.selected = checked
# Обновляем чекбоксы в дереве
for i in range(self.tree.topLevelItemCount()):
item = self.tree.topLevelItem(i)
if item:
item.setCheckState(0, Qt.Checked if checked else Qt.Unchecked)
self._update_selected_count()
def _update_selected_count(self):
count = sum(1 for q in self.questions if getattr(q, 'selected', True))
self.lbl_selected.setText(f'Выбрано: {count} / {len(self.questions)}')
self.status_label.setText(f'Выбрано вопросов: {count} / {len(self.questions)}')
def _resolve_question_idx(self, item):
idx = item.data(0, Qt.UserRole)
if idx is None or isinstance(idx, dict):
parent = item.parent()
if parent is not None:
idx = parent.data(0, Qt.UserRole)
if idx is not None and not isinstance(idx, dict) and idx < len(self.questions):
return idx
return None
def _on_tree_item_clicked(self, item, column):
idx = self._resolve_question_idx(item)
print(f'[CLICK] item={item.text(0)[:40]}, col={column}, idx={idx}')
self.log_text.append(f'[CLICK] item={item.text(0)[:30]}, column={column}, resolved_idx={idx}')
if idx is not None:
self._update_preview(idx)
def _on_tree_current_changed(self, current, previous):
if current is None:
return
idx = self._resolve_question_idx(current)
print(f'[CURRENT] item={current.text(0)[:40]}, idx={idx}')
if idx is not None:
self._update_preview(idx)
def _update_preview(self, q_idx):
self.log_text.append(f'[PREVIEW] _update_preview called for idx={q_idx}')
q = self.questions[q_idx]
if q.errors:
self.log_text.append(f'[PREVIEW] Question has {len(q.errors)} errors, showing error view')
self.preview_web.setHtml(f'<!DOCTYPE html><html><body style="font-family:sans-serif; padding:10px;"><div style="color:red; padding:10px;"><b>Ошибки в вопросе:</b><br>{ "<br>".join(q.errors) }</div></body></html>')
return
try:
from universal_moodle_converter_v3_stable import XMLGenerator
gen = XMLGenerator()
gen.set_image_data(q.image_data)
marker = q.marker
if not marker:
from universal_moodle_converter_v3_stable import QuestionTypeDetector
marker = QuestionTypeDetector.detect(q.content, '', marker)
self.log_text.append(f'[PREVIEW] marker={marker}, name={q.name[:30]}, content_lines={len(q.content)}')
grade = q.grade if q.grade else 1.0
if marker == 'multichoice_one':
gen.create_multichoice(q.name, q.content, grade, single=True, penalty_wrong=1.0)
elif marker == 'multichoice_many':
gen.create_multichoice(q.name, q.content, grade, single=False, penalty_wrong=1.0)
elif marker == 'matching':
gen.create_matching(q.name, q.content, grade)
elif marker == 'ddmatch':
gen.create_ddmatch(q.name, q.content, grade)
elif marker == 'gapselect':
gen.create_gapselect(q.name, q.content, grade)
elif marker == 'cloze':
gen.create_cloze(q.name, q.content, grade)
elif marker in ('numerical_partial', 'numerical_numcombo'):
gen.create_numerical(q.name, q.content, grade)
elif marker == 'shortanswer_phrase':
gen.create_shortanswer(q.name, q.content, grade)
else:
gen.create_multichoice(q.name, q.content, grade, single=True, penalty_wrong=1.0)
xml_str = etree.tostring(gen.root, pretty_print=True, encoding='unicode')
self.log_text.append(f'[PREVIEW] XML generated, len={len(xml_str)}, questions_in_root={len(gen.root)}')
html = self._xml_to_moodle_preview(q.name, q.content, marker, xml_str, q.marker)
self.log_text.append(f'[PREVIEW] HTML generated, len={len(html)}, calling setHtml...')
self.log_text.append(f'[PREVIEW] HTML start: {html[:200]}...')
self.preview_web.setHtml(html)
self.preview_web.update()
QApplication.processEvents()
self.log_text.append(f'[PREVIEW] setHtml called successfully')
except Exception as e:
tb = traceback.format_exc()
self.log_text.append(f'[PREVIEW ERROR] {e}\n{tb}')
self.preview_web.setHtml(f'<!DOCTYPE html><html><body style="font-family:sans-serif; padding:10px;"><div style="color:red; padding:10px;">Ошибка: {str(e)}</div></body></html>')
def _xml_to_moodle_preview(self, name, content, marker, xml_str, original_marker=''):
tree = etree.fromstring(xml_str)
q_elem = tree.find('.//question')
qtype = q_elem.get('type', '') if q_elem is not None else ''
def process_qtext(text, xml_elem):
text = text.replace('\n', '<br>')
images_html = ''
if hasattr(xml_elem, 'image_data') and xml_elem.image_data:
for img_key, img_data in xml_elem.image_data.items():
images_html += f'<img src="data:image/png;base64,{img_data}" style="max-width:300px; margin:5px;">'
for img in xml_elem.findall('.//image'):
if img.text:
images_html += f'<img src="data:image/png;base64,{img.text}" style="max-width:300px; margin:5px;">'
for f in xml_elem.findall('.//file'):
if f.text:
images_html += f'<img src="data:image/png;base64,{f.text}" style="max-width:300px; margin:5px;">'
text = text.replace('_@@PLUGINFILE@@/[^_]+_IMAGE_', '')
text = text.replace('_IMAGE_', '')
text = re.sub(r'IMAGE#\d+-image\d+', '<b>[IMAGE]</b>', text)
return images_html + text if images_html else text
moodle_css = '''
<style>
* { box-sizing: border-box; }
.que {
background: #f9f9f9;
border: 1px solid #ddd;
border-radius: 4px;
padding: 15px;
margin-bottom: 15px;
}
.que.multichoice { border-left: 4px solid #4a90d9; }
.que.gapselect { border-left: 4px solid #9b59b6; }
.que.matching, .que.match { border-left: 4px solid #f39c12; }
.que.ddmatch { border-left: 4px solid #e74c3c; }
.que.cloze { border-left: 4px solid #f1c40f; }
.que.numerical, .que.shortanswer { border-left: 4px solid #1abc9c; }
.formulation {
color: #333;
margin-bottom: 15px;
}