* @brief Parses the proto file at filePath and emits previewReady on success. */
| 931 | * @brief Parses the proto file at filePath and emits previewReady on success. |
| 932 | */ |
| 933 | void DataModel::ProtoImporter::showPreview(const QString& filePath) |
| 934 | { |
| 935 | QFile file(filePath); |
| 936 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 937 | Misc::Utilities::showMessageBox( |
| 938 | tr("Failed to open proto file: %1").arg(file.errorString()), |
| 939 | tr("Verify the file path and read permissions, then try again."), |
| 940 | QMessageBox::Critical, |
| 941 | tr("Protobuf Import Error")); |
| 942 | return; |
| 943 | } |
| 944 | |
| 945 | static constexpr qsizetype kMaxProtoFileBytes = 10 * 1024 * 1024; |
| 946 | if (file.size() > kMaxProtoFileBytes) { |
| 947 | Misc::Utilities::showMessageBox(tr("Proto file is too large (the limit is 10 MB)."), |
| 948 | tr("Verify you selected the correct .proto definition file."), |
| 949 | QMessageBox::Critical, |
| 950 | tr("Protobuf Import Error")); |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | QTextStream stream(&file); |
| 955 | const QString src = stream.readAll(); |
| 956 | file.close(); |
| 957 | |
| 958 | QVector<ProtoMessage> messages; |
| 959 | QString package; |
| 960 | ParseError err{0, QString()}; |
| 961 | |
| 962 | Parser parser(src, package, messages); |
| 963 | if (!parser.parseFile(err)) { |
| 964 | Misc::Utilities::showMessageBox( |
| 965 | tr("Failed to parse proto file at line %1: %2").arg(QString::number(err.line), err.message), |
| 966 | tr("Only proto3 syntax is supported. Verify the file format and try again."), |
| 967 | QMessageBox::Critical, |
| 968 | tr("Protobuf Import Error")); |
| 969 | return; |
| 970 | } |
| 971 | |
| 972 | if (messages.isEmpty()) { |
| 973 | Misc::Utilities::showMessageBox(tr("Proto file contains no message definitions"), |
| 974 | tr("The selected file has no `message` blocks to import."), |
| 975 | QMessageBox::Warning, |
| 976 | tr("Protobuf Import Warning")); |
| 977 | return; |
| 978 | } |
| 979 | |
| 980 | m_messages = messages; |
| 981 | m_protoFilePath = filePath; |
| 982 | |
| 983 | Q_EMIT messagesChanged(); |
| 984 | Q_EMIT fileNameChanged(); |
| 985 | Q_EMIT previewReady(); |
| 986 | } |
| 987 | |
| 988 | /** |
| 989 | * @brief Generates a project covering every top-level message and hands it to ProjectModel. |