66 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
66 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
67 // ============================================================================= |
67 // ============================================================================= |
68 void doSave (bool saveAs) { |
68 void doSave (bool saveAs) { |
69 str path = g_curfile->name (); |
69 str path = g_curfile->name (); |
70 |
70 |
71 if (~path == 0 || saveAs) { |
71 if (path.length () == 0 || saveAs) { |
72 path = QFileDialog::getSaveFileName (g_win, "Save As", |
72 path = QFileDialog::getSaveFileName (g_win, "Save As", |
73 g_curfile->name (), "LDraw files (*.dat *.ldr)"); |
73 g_curfile->name (), "LDraw files (*.dat *.ldr)"); |
74 |
74 |
75 if (~path == 0) { |
75 if (path.length () == 0) { |
76 // User didn't give a file name. This happens if the user cancelled |
76 // User didn't give a file name. This happens if the user cancelled |
77 // saving in the save file dialog. Abort. |
77 // saving in the save file dialog. Abort. |
78 return; |
78 return; |
79 } |
79 } |
80 } |
80 } |
81 |
81 |
82 if (g_curfile->save (path)) { |
82 if (g_curfile->save (path)) { |
83 g_curfile->setName (path); |
83 g_curfile->setName (path); |
84 g_win->updateTitle (); |
84 g_win->updateTitle (); |
85 |
|
86 logf ("Saved successfully to %s\n", path.chars ()); |
|
87 } else { |
85 } else { |
88 setlocale (LC_ALL, "C"); |
86 setlocale (LC_ALL, "C"); |
89 |
87 |
90 // Tell the user the save failed, and give the option for saving as with it. |
88 // Tell the user the save failed, and give the option for saving as with it. |
91 QMessageBox dlg (QMessageBox::Critical, "Save Failure", |
89 QMessageBox dlg (QMessageBox::Critical, "Save Failure", |
92 fmt ("Failed to save to %s\nReason: %s", path.chars(), strerror (errno)), |
90 fmt ("Failed to save to %1\nReason: %2", path, strerror (errno)), |
93 QMessageBox::Close, g_win); |
91 QMessageBox::Close, g_win); |
94 |
92 |
95 QPushButton* saveAsBtn = new QPushButton ("Save As"); |
93 QPushButton* saveAsBtn = new QPushButton ("Save As"); |
96 saveAsBtn->setIcon (getIcon ("file-save-as")); |
94 saveAsBtn->setIcon (getIcon ("file-save-as")); |
97 dlg.addButton (saveAsBtn, QMessageBox::ActionRole); |
95 dlg.addButton (saveAsBtn, QMessageBox::ActionRole); |
299 // ============================================================================= |
297 // ============================================================================= |
300 MAKE_ACTION (insertFrom, "Insert from File", "file-import", "Insert LDraw data from a file.", (0)) { |
298 MAKE_ACTION (insertFrom, "Insert from File", "file-import", "Insert LDraw data from a file.", (0)) { |
301 str fname = QFileDialog::getOpenFileName (); |
299 str fname = QFileDialog::getOpenFileName (); |
302 ulong idx = g_win->getInsertionPoint (); |
300 ulong idx = g_win->getInsertionPoint (); |
303 |
301 |
304 if (!~fname) |
302 if (!fname.length ()) |
305 return; |
303 return; |
306 |
304 |
307 FILE* fp = fopen (fname, "r"); |
305 FILE* fp = fopen (qchars (fname), "r"); |
308 if (!fp) { |
306 if (!fp) { |
309 critical (fmt ("Couldn't open %s\n%s", fname.chars(), strerror (errno))); |
307 critical (fmt ("Couldn't open %1 (%2)", fname, strerror (errno))); |
310 return; |
308 return; |
311 } |
309 } |
312 |
310 |
313 vector<LDObject*> objs = loadFileContents (fp, null); |
311 vector<LDObject*> objs = loadFileContents (fp, null); |
314 |
312 |
331 MAKE_ACTION (exportTo, "Export To File", "file-export", "Export current selection to file", (0)) { |
329 MAKE_ACTION (exportTo, "Export To File", "file-export", "Export current selection to file", (0)) { |
332 if (g_win->sel ().size () == 0) |
330 if (g_win->sel ().size () == 0) |
333 return; |
331 return; |
334 |
332 |
335 str fname = QFileDialog::getSaveFileName (); |
333 str fname = QFileDialog::getSaveFileName (); |
336 if (fname.len () == 0) |
334 if (fname.length () == 0) |
337 return; |
335 return; |
338 |
336 |
339 QFile file (fname); |
337 QFile file (fname); |
340 if (!file.open (QIODevice::WriteOnly | QIODevice::Text)) { |
338 if (!file.open (QIODevice::WriteOnly | QIODevice::Text)) { |
341 critical (fmt ("Unable to open %s for writing (%s)", fname.chars (), strerror (errno))); |
339 critical (fmt ("Unable to open %1 for writing (%2)", fname, strerror (errno))); |
342 return; |
340 return; |
343 } |
341 } |
344 |
342 |
345 for (LDObject* obj : g_win->sel ()) { |
343 for (LDObject* obj : g_win->sel ()) { |
346 str contents = obj->raw (); |
344 str contents = obj->raw (); |
347 file.write (contents, contents.len ()); |
345 QByteArray data = contents.toUtf8 (); |
|
346 file.write (data, data.size ()); |
348 file.write ("\r\n", 2); |
347 file.write ("\r\n", 2); |
349 } |
348 } |
350 } |
349 } |
351 |
350 |
352 // ============================================================================= |
351 // ============================================================================= |
391 ushort w, h; |
390 ushort w, h; |
392 uchar* imgdata = g_win->R ()->screencap (w, h); |
391 uchar* imgdata = g_win->R ()->screencap (w, h); |
393 QImage img = imageFromScreencap (imgdata, w, h); |
392 QImage img = imageFromScreencap (imgdata, w, h); |
394 |
393 |
395 str root = basename (g_curfile->name ()); |
394 str root = basename (g_curfile->name ()); |
396 if (~root >= 4 && root.substr (~root - 4, -1) == ".dat") |
395 if (root.right (4) == ".dat") |
397 root -= 4; |
396 root.chop (4); |
398 |
397 |
399 str defaultname = (~root > 0) ? fmt ("%s.png", root.c ()) : ""; |
398 str defaultname = (root.length () > 0) ? fmt ("%1.png", root) : ""; |
400 str fname = QFileDialog::getSaveFileName (g_win, "Save Screencap", defaultname, |
399 str fname = QFileDialog::getSaveFileName (g_win, "Save Screencap", defaultname, |
401 "PNG images (*.png);;JPG images (*.jpg);;BMP images (*.bmp);;All Files (*.*)"); |
400 "PNG images (*.png);;JPG images (*.jpg);;BMP images (*.bmp);;All Files (*.*)"); |
402 |
401 |
403 if (~fname > 0 && !img.save (fname)) |
402 if (fname.length () > 0 && !img.save (fname)) |
404 critical (fmt ("Couldn't open %s for writing to save screencap: %s", fname.c (), strerror (errno))); |
403 critical (fmt ("Couldn't open %1 for writing to save screencap: %2", fname, strerror (errno))); |
405 |
404 |
406 delete[] imgdata; |
405 delete[] imgdata; |
407 } |
406 } |
408 |
407 |
409 // ========================================================================================================================================= |
408 // ========================================================================================================================================= |
450 if (g_win->R ()->camera () == GL::Free) |
449 if (g_win->R ()->camera () == GL::Free) |
451 return; |
450 return; |
452 |
451 |
453 bool ok; |
452 bool ok; |
454 double depth = QInputDialog::getDouble (g_win, "Set Draw Depth", |
453 double depth = QInputDialog::getDouble (g_win, "Set Draw Depth", |
455 fmt ("Depth value for %s Camera:", g_win->R ()->cameraName ()), |
454 fmt ("Depth value for %1 Camera:", g_win->R ()->cameraName ()), |
456 g_win->R ()->depthValue (), -10000.0f, 10000.0f, 3, &ok); |
455 g_win->R ()->depthValue (), -10000.0f, 10000.0f, 3, &ok); |
457 |
456 |
458 if (ok) |
457 if (ok) |
459 g_win->R ()->setDepthValue (depth); |
458 g_win->R ()->setDepthValue (depth); |
460 } |
459 } |