src/toolsets/algorithmtoolset.cpp

changeset 1331
f10b0c32a85d
parent 1328
d68d1ce89d05
child 1403
7a2d84112983
equal deleted inserted replaced
1330:9155a6b153f3 1331:f10b0c32a85d
482 if (selectedObjects().isEmpty()) 482 if (selectedObjects().isEmpty())
483 return; 483 return;
484 484
485 // Determine the title of the new subfile 485 // Determine the title of the new subfile
486 QString subfileTitle; 486 QString subfileTitle;
487 LDComment* titleObject = dynamic_cast<LDComment*>(currentDocument()->getObject(0)); 487
488 488 if (currentDocument()->header.type != LDHeader::NoHeader)
489 if (titleObject) 489 subfileTitle = "~" + currentDocument()->header.description;
490 subfileTitle = "~" + titleObject->text();
491 else 490 else
492 subfileTitle = "~subfile"; 491 subfileTitle = "~Untitled subfile";
493 492
494 // Remove duplicate tildes 493 // Remove duplicate tildes
495 while (subfileTitle.startsWith("~~")) 494 while (subfileTitle.startsWith("~~"))
496 subfileTitle.remove(0, 1); 495 subfileTitle.remove(0, 1);
497 496
498 // If this the parent document isn't already in s/, we need to stuff it into 497 // If this the parent document isn't already in s/, we need to stuff it into
499 // a subdirectory named s/. Ensure it exists! 498 // a subdirectory named s/. Ensure it exists!
500 QString topDirectoryName = Basename(Dirname(currentDocument()->fullPath())); 499 QFileInfo path = currentDocument()->fullPath();
501 QString parentDocumentPath = currentDocument()->fullPath(); 500 QString parentDocumentPath = currentDocument()->fullPath();
502 QString subfileDirectory = Dirname(parentDocumentPath); 501 QDir subfileDirectory = path.absoluteDir();
503 502
504 if (topDirectoryName != "s") 503 if (path.dir().dirName() != "s")
505 { 504 {
506 QString desiredPath = subfileDirectory + "/s"; 505 QDir desiredPath = subfileDirectory.filePath("s");
507 QString title = tr ("Create subfile directory?"); 506
508 QString text = format(tr("The directory <b>%1</b> is suggested for subfiles. " 507 if (desiredPath.exists())
509 "This directory does not exist, do you want to create it?"), desiredPath);
510
511 if (QDir(desiredPath).exists()
512 or QMessageBox::question(m_window, title, text, (QMessageBox::Yes | QMessageBox::No), QMessageBox::No) == QMessageBox::Yes)
513 { 508 {
514 subfileDirectory = desiredPath; 509 subfileDirectory = desiredPath;
515 QDir().mkpath(subfileDirectory); 510 } else if (QMessageBox::question(
511 m_window,
512 tr("Create subfile directory?"),
513 format(tr("The directory <b>%1</b> is suggested for subfiles. "
514 "This directory does not exist, do you want to create it?"),
515 desiredPath.absolutePath()
516 ),
517 (QMessageBox::Yes | QMessageBox::No),
518 QMessageBox::No
519 ) == QMessageBox::Yes
520 ) {
521 if (subfileDirectory.mkdir("s"))
522 {
523 subfileDirectory = desiredPath;
524 }
525 else
526 {
527 QMessageBox::critical(
528 m_window,
529 tr("Error"),
530 format(tr("Unable to create directory %1: %2!"),
531 subfileDirectory.absolutePath(),
532 strerror(errno)
533 )
534 );
535 return;
536 }
516 } 537 }
517 else 538 else
539 {
518 return; 540 return;
541 }
519 } 542 }
520 543
521 // Determine the body of the name of the subfile 544 // Determine the body of the name of the subfile
522 QString fullSubfileName; 545 QString fullSubfilePath;
523 546
524 if (not parentDocumentPath.isEmpty()) 547 if (not parentDocumentPath.isEmpty())
525 { 548 {
526 // Chop existing '.dat' suffix 549 QString subfileRoot = QFileInfo(parentDocumentPath).baseName();
527 if (parentDocumentPath.endsWith (".dat"))
528 parentDocumentPath.chop (4);
529 550
530 // Remove the s?? suffix if it's there, otherwise we'll get filenames 551 // Remove the s?? suffix if it's there, otherwise we'll get filenames
531 // like s01s01.dat when subfiling subfiles. 552 // like s01s01.dat when subfiling subfiles.
532 QRegExp subfilesuffix {"s[0-9][0-9]$"}; 553 QRegExp subfilesuffix {"s[0-9][0-9]$"};
533 if (subfilesuffix.indexIn(parentDocumentPath) != -1) 554 if (subfilesuffix.indexIn(subfileRoot) != -1)
534 parentDocumentPath.chop(subfilesuffix.matchedLength()); 555 subfileRoot.chop(subfilesuffix.matchedLength());
535 556
536 int subfileIndex = 1; 557 int subfileIndex = 1;
537 QString digits; 558 QString digits;
538 559 QString subfileName;
539 // Now find the appropriate filename. Increase the number of the subfile until we find a name which isn't already taken. 560
561 // Now find the appropriate filename. Increase the number of the subfile until we find a
562 // name which isn't already taken.
540 do 563 do
541 { 564 {
542 digits.setNum(subfileIndex++); 565 digits.setNum(subfileIndex);
543 566
544 // Pad it with a zero 567 // Pad it with a zero
545 if (countof(digits) == 1) 568 if (countof(digits) == 1)
546 digits.prepend("0"); 569 digits.prepend("0");
547 570
548 fullSubfileName = subfileDirectory + "/" + Basename(parentDocumentPath) + "s" + digits + ".dat"; 571 subfileName = subfileRoot + "s" + digits + ".dat";
549 } while (m_documents->findDocumentByName("s\\" + Basename(fullSubfileName)) != nullptr or QFile {fullSubfileName}.exists()); 572 fullSubfilePath = subfileDirectory.filePath(subfileName);
573 subfileIndex += 1;
574 } while (
575 m_documents->findDocumentByName("s\\" + subfileName) != nullptr
576 or QFileInfo {fullSubfilePath}.exists()
577 );
550 } 578 }
551 579
552 // Create the new subfile document 580 // Create the new subfile document
553 LDDocument* subfile = m_window->newDocument(); 581 LDDocument* subfile = m_window->newDocument();
554 subfile->setFullPath(fullSubfileName); 582 subfile->setFullPath(fullSubfilePath);
555 subfile->header.description = subfileTitle; 583 subfile->header.description = subfileTitle;
556 subfile->header.type = LDHeader::Subpart; 584 subfile->header.type = LDHeader::Subpart;
557 subfile->header.name = LDDocument::shortenName(fullSubfileName); 585 subfile->header.name = LDDocument::shortenName(fullSubfilePath);
558 subfile->header.author = format("%1 [%2]", config::defaultName(), config::defaultUser()); 586 subfile->header.author = format("%1 [%2]", config::defaultName(), config::defaultUser());
559 587
560 if (config::useCaLicense()) 588 if (config::useCaLicense())
561 subfile->header.license = LDHeader::CaLicense; 589 subfile->header.license = LDHeader::CaLicense;
562 590

mercurial