Address comments in pull request:

- use "const" where possible
 - add spaces after commas
 - use dateTimeFormatExamples by reference (don't copy)
 - fix incorrect cast to unsigned int (declare variable as gunichar)
This commit is contained in:
Scott Gilbertson 2024-02-28 13:09:25 -05:00
parent 1db8b0d643
commit cd64ced60f
2 changed files with 28 additions and 24 deletions

View File

@ -100,9 +100,9 @@ namespace // local helper functions
// use that string to format dateTime. Append the formatted time to path. // use that string to format dateTime. Append the formatted time to path.
void appendFormattedTime(Glib::ustring& path, unsigned int& ix, const Glib::ustring& templateText, const Glib::DateTime& dateTime) void appendFormattedTime(Glib::ustring& path, unsigned int& ix, const Glib::ustring& templateText, const Glib::DateTime& dateTime)
{ {
constexpr unsigned int quoteMark = (unsigned int)'"'; constexpr gunichar quoteMark('"');
if ((ix + 1) < templateText.size() && templateText[ix] == quoteMark) { if ((ix + 1) < templateText.size() && templateText[ix] == quoteMark) {
auto endPos = templateText.find_first_of(quoteMark, ++ix); const auto endPos = templateText.find_first_of(quoteMark, ++ix);
if (endPos != Glib::ustring::npos) { if (endPos != Glib::ustring::npos) {
Glib::ustring formatString(templateText, ix, endPos-ix); Glib::ustring formatString(templateText, ix, endPos-ix);
path += dateTime.format(formatString); path += dateTime.format(formatString);
@ -1024,8 +1024,10 @@ Glib::ustring BatchQueue::calcAutoFileNameBase (const Glib::ustring& origFileNam
switch(options.savePathTemplate[ix++]) switch(options.savePathTemplate[ix++])
{ {
case 'E': // (approximate) time when export started case 'E': // (approximate) time when export started
{
dateTime = Glib::DateTime::create_now_local(); dateTime = Glib::DateTime::create_now_local();
break; break;
}
case 'F': // time when file was last saved case 'F': // time when file was last saved
{ {
Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(origFileName); Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(origFileName);
@ -1035,17 +1037,19 @@ Glib::ustring BatchQueue::calcAutoFileNameBase (const Glib::ustring& origFileNam
dateTime = info->get_modification_date_time(); dateTime = info->get_modification_date_time();
} }
} }
}
break; break;
}
case 'P': // time when picture was taken case 'P': // time when picture was taken
{ {
auto timestamp = FramesData(origFileName).getDateTimeAsTS(); const auto timestamp = FramesData(origFileName).getDateTimeAsTS();
dateTime = Glib::DateTime::create_now_local(timestamp); dateTime = Glib::DateTime::create_now_local(timestamp);
break;
} }
break;
default: default:
{
break; break;
} }
}
if (dateTime) { if (dateTime) {
appendFormattedTime(path, ix, options.savePathTemplate, dateTime); appendFormattedTime(path, ix, options.savePathTemplate, dateTime);
} }

View File

@ -344,7 +344,7 @@ void BatchQueuePanel::templateHelpButtonToggled()
if (buffer->get_text().empty()) { if (buffer->get_text().empty()) {
// Populate the help text the first time it's shown // Populate the help text the first time it's shown
populateTemplateHelpBuffer(buffer); populateTemplateHelpBuffer(buffer);
auto fullWidth = middleSplitPane->get_width(); const auto fullWidth = middleSplitPane->get_width();
middleSplitPane->set_position(fullWidth / 2); middleSplitPane->set_position(fullWidth / 2);
} }
scrolledTemplateHelpWindow->set_visible(visible); scrolledTemplateHelpWindow->set_visible(visible);
@ -354,13 +354,13 @@ void BatchQueuePanel::templateHelpButtonToggled()
void BatchQueuePanel::populateTemplateHelpBuffer(Glib::RefPtr<Gtk::TextBuffer> buffer) void BatchQueuePanel::populateTemplateHelpBuffer(Glib::RefPtr<Gtk::TextBuffer> buffer)
{ {
auto pos = buffer->begin(); auto pos = buffer->begin();
auto insertTopicHeading = [&pos, buffer](const Glib::ustring& text) { const auto insertTopicHeading = [&pos, buffer](const Glib::ustring& text) {
pos = buffer->insert_markup(pos, Glib::ustring::format("\n\n<u><b>", text, "</b></u>\n")); pos = buffer->insert_markup(pos, Glib::ustring::format("\n\n<u><b>", text, "</b></u>\n"));
}; };
auto insertTopicBody = [&pos, buffer](const Glib::ustring& text) { const auto insertTopicBody = [&pos, buffer](const Glib::ustring& text) {
pos = buffer->insert_markup(pos, Glib::ustring::format("\n", text, "\n")); pos = buffer->insert_markup(pos, Glib::ustring::format("\n", text, "\n"));
}; };
auto mainTitle = M("QUEUE_LOCATION_TEMPLATE_HELP_TITLE"); const auto mainTitle = M("QUEUE_LOCATION_TEMPLATE_HELP_TITLE");
pos = buffer->insert_markup(pos, Glib::ustring::format("<big><b>", mainTitle, "</b></big>\n")); pos = buffer->insert_markup(pos, Glib::ustring::format("<big><b>", mainTitle, "</b></big>\n"));
pos = buffer->insert_markup(pos, M("QUEUE_LOCATION_TEMPLATE_HELP_INTRO")); pos = buffer->insert_markup(pos, M("QUEUE_LOCATION_TEMPLATE_HELP_INTRO"));
@ -377,21 +377,21 @@ void BatchQueuePanel::populateTemplateHelpBuffer(Glib::RefPtr<Gtk::TextBuffer> b
pos = buffer->insert(pos, "\n"); pos = buffer->insert(pos, "\n");
pos = buffer->insert_markup(pos, M("QUEUE_LOCATION_TEMPLATE_HELP_PATHS_BODY_1")); pos = buffer->insert_markup(pos, M("QUEUE_LOCATION_TEMPLATE_HELP_PATHS_BODY_1"));
#ifdef _WIN32 #ifdef _WIN32
auto exampleFilePath = M("QUEUE_LOCATION_TEMPLATE_HELP_PATHS_EXAMPLE_WINDOWS"); const auto exampleFilePath = M("QUEUE_LOCATION_TEMPLATE_HELP_PATHS_EXAMPLE_WINDOWS");
#else #else
auto exampleFilePath = M("QUEUE_LOCATION_TEMPLATE_HELP_PATHS_EXAMPLE_LINUX"); const auto exampleFilePath = M("QUEUE_LOCATION_TEMPLATE_HELP_PATHS_EXAMPLE_LINUX");
#endif #endif
pos = buffer->insert_markup(pos, Glib::ustring::format("\n ", exampleFilePath, "\n")); pos = buffer->insert_markup(pos, Glib::ustring::format("\n ", exampleFilePath, "\n"));
pos = buffer->insert_markup(pos, M("QUEUE_LOCATION_TEMPLATE_HELP_PATHS_BODY_2")); pos = buffer->insert_markup(pos, M("QUEUE_LOCATION_TEMPLATE_HELP_PATHS_BODY_2"));
// Examples are generated from exampleFilePath using the actual template processing function // Examples are generated from exampleFilePath using the actual template processing function
Options savedOptions = options; // to be restored after generating example results const Options savedOptions = options; // to be restored after generating example results
options.saveUsePathTemplate = true; options.saveUsePathTemplate = true;
// Since this code only ever runs once (the first time the help text is presented), no attempt is // Since this code only ever runs once (the first time the help text is presented), no attempt is
// made to be efficient. Use a brute-force method to discover the number of elements in exampleFilePath. // made to be efficient. Use a brute-force method to discover the number of elements in exampleFilePath.
int pathElementCount = 0; int pathElementCount = 0;
for (int n=9; n>=0; n--) { for (int n=9; n>=0; n--) {
options.savePathTemplate = Glib::ustring::format("%d", n); options.savePathTemplate = Glib::ustring::format("%d", n);
auto result = BatchQueue::calcAutoFileNameBase(exampleFilePath); const auto result = BatchQueue::calcAutoFileNameBase(exampleFilePath);
if (!result.empty()) { if (!result.empty()) {
// The 'd' specifier returns an empty string if N exceeds the number of path elements, so // The 'd' specifier returns an empty string if N exceeds the number of path elements, so
// the largest N that does not return an empty string is the number of elements in exampleFilePath. // the largest N that does not return an empty string is the number of elements in exampleFilePath.
@ -401,7 +401,7 @@ void BatchQueuePanel::populateTemplateHelpBuffer(Glib::RefPtr<Gtk::TextBuffer> b
} }
// Function inserts examples for a particular specifier, with every valid N value for the // Function inserts examples for a particular specifier, with every valid N value for the
// number of elements in the path. // number of elements in the path.
auto insertPathExamples = [&buffer, &pos, pathElementCount, exampleFilePath](char letter, int offset1, int mult1, int offset2, int mult2) const auto insertPathExamples = [&buffer, &pos, pathElementCount, exampleFilePath](char letter, int offset1, int mult1, int offset2, int mult2)
{ {
for (int n=0; n<pathElementCount; n++) { for (int n=0; n<pathElementCount; n++) {
auto path1 = Glib::ustring::format("%", letter, offset1+n*mult1); auto path1 = Glib::ustring::format("%", letter, offset1+n*mult1);
@ -423,9 +423,9 @@ void BatchQueuePanel::populateTemplateHelpBuffer(Glib::RefPtr<Gtk::TextBuffer> b
insertPathExamples('p', 1, 1, -pathElementCount, 1); // <b>%p1</b> = <b>%p-4</b> = <i>/home/tom/photos/2010-10-31/</i> insertPathExamples('p', 1, 1, -pathElementCount, 1); // <b>%p1</b> = <b>%p-4</b> = <i>/home/tom/photos/2010-10-31/</i>
insertPathExamples('P', 1, 1, -pathElementCount, 1); // <b>%P1</b> = <b>%P-4</b> = <i>2010-10-31/</i> insertPathExamples('P', 1, 1, -pathElementCount, 1); // <b>%P1</b> = <b>%P-4</b> = <i>2010-10-31/</i>
{ {
Glib::ustring fspecifier("%f"); const Glib::ustring fspecifier("%f");
options.savePathTemplate = fspecifier; options.savePathTemplate = fspecifier;
auto result = BatchQueue::calcAutoFileNameBase(exampleFilePath); const auto result = BatchQueue::calcAutoFileNameBase(exampleFilePath);
pos = buffer->insert_markup(pos, Glib::ustring::format("\n <tt><b>", fspecifier, "</b> = <i>", result, "</i></tt>")); pos = buffer->insert_markup(pos, Glib::ustring::format("\n <tt><b>", fspecifier, "</b> = <i>", result, "</i></tt>"));
} }
@ -437,15 +437,15 @@ void BatchQueuePanel::populateTemplateHelpBuffer(Glib::RefPtr<Gtk::TextBuffer> b
insertTopicHeading(M("QUEUE_LOCATION_TEMPLATE_HELP_TIMESTAMP_TITLE")); insertTopicHeading(M("QUEUE_LOCATION_TEMPLATE_HELP_TIMESTAMP_TITLE"));
pos = buffer->insert_markup(pos, M("QUEUE_LOCATION_TEMPLATE_HELP_TIMESTAMP_BODY")); pos = buffer->insert_markup(pos, M("QUEUE_LOCATION_TEMPLATE_HELP_TIMESTAMP_BODY"));
Glib::ustring dateTimeFormatExamples[] = { const Glib::ustring dateTimeFormatExamples[] = {
"%Y-%m-%d", "%Y-%m-%d",
"%Y%m%d_%H%M%S", "%Y%m%d_%H%M%S",
"%y/%b/%-d/" "%y/%b/%-d/"
}; };
auto timezone = Glib::DateTime::create_now_local().get_timezone(); const auto timezone = Glib::DateTime::create_now_local().get_timezone();
auto timeForExamples = Glib::DateTime::create_from_iso8601("2001-02-03T04:05:06.123456", timezone); const auto timeForExamples = Glib::DateTime::create_from_iso8601("2001-02-03T04:05:06.123456", timezone);
for (auto fmt : dateTimeFormatExamples) { for (auto && fmt : dateTimeFormatExamples) {
auto result = timeForExamples.format(fmt); const auto result = timeForExamples.format(fmt);
pos = buffer->insert_markup(pos, Glib::ustring::format("\n <tt><b>%tE\"", fmt, "\"</b> = <i>", result, "</i></tt>")); pos = buffer->insert_markup(pos, Glib::ustring::format("\n <tt><b>%tE\"", fmt, "\"</b> = <i>", result, "</i></tt>"));
} }