| 85 | } |
| 86 | |
| 87 | void Thumbnail::SaveDocFile(Base::Writer& writer) const |
| 88 | { |
| 89 | QImage img; |
| 90 | bool created = false; |
| 91 | |
| 92 | // 1. Try to create the thumbnail from the viewer |
| 93 | if (this->viewer) { |
| 94 | if (this->viewer->thread() != QThread::currentThread()) { |
| 95 | qWarning("Cannot create a thumbnail from non-GUI thread"); |
| 96 | } |
| 97 | else { |
| 98 | QColor invalid; |
| 99 | this->viewer->imageFromFramebuffer( |
| 100 | this->size, |
| 101 | this->size, |
| 102 | 4, |
| 103 | invalid, |
| 104 | img, |
| 105 | View3DInventorViewer::RenderIntent::RasterCapture |
| 106 | ); |
| 107 | created = !img.isNull(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // 2. If creation failed (e.g. no viewer or background thread), try to restore from the existing file |
| 112 | if (!created) { |
| 113 | QString filename = this->uri.toLocalFile(); |
| 114 | Base::FileInfo fi(filename.toUtf8().constData()); |
| 115 | if (fi.exists()) { |
| 116 | try { |
| 117 | zipios::ZipFile zf(fi.filePath()); |
| 118 | // getEntry uses default MatchPath=MATCH. |
| 119 | zipios::ConstEntryPointer entry = zf.getEntry("thumbnails/Thumbnail.png"); |
| 120 | if (entry && entry->isValid()) { |
| 121 | // getInputStream returns a pointer that must be deleted |
| 122 | std::istream* is = zf.getInputStream(entry); |
| 123 | if (is) { |
| 124 | if (is->good()) { |
| 125 | writer.Stream() << is->rdbuf(); |
| 126 | delete is; |
| 127 | return; |
| 128 | } |
| 129 | delete is; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | catch (const std::exception&) { |
| 134 | // If the file isn't a zip or is locked, we ignore it and proceed to fallback |
| 135 | } |
| 136 | catch (...) { |
| 137 | // Ignore unknown exceptions |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // If we still have no image and no viewer to generate one, we can do nothing more |
| 143 | if (!this->viewer) { |
| 144 | return; |
nothing calls this directly
no test coverage detected