rename_dialog.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /**************************************************************************/
  2. /* rename_dialog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "rename_dialog.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_string_names.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. #include "editor/plugins/script_editor_plugin.h"
  35. #include "scene/gui/check_box.h"
  36. #include "scene/gui/check_button.h"
  37. #include "scene/gui/control.h"
  38. #include "scene/gui/grid_container.h"
  39. #include "scene/gui/label.h"
  40. #include "scene/gui/option_button.h"
  41. #include "scene/gui/separator.h"
  42. #include "scene/gui/spin_box.h"
  43. #include "scene/gui/tab_container.h"
  44. #include "modules/regex/regex.h"
  45. RenameDialog::RenameDialog(SceneTreeEditor *p_scene_tree_editor) {
  46. scene_tree_editor = p_scene_tree_editor;
  47. preview_node = nullptr;
  48. set_title(TTR("Batch Rename"));
  49. VBoxContainer *vbc = memnew(VBoxContainer);
  50. add_child(vbc);
  51. // -- Search/Replace Area
  52. GridContainer *grd_main = memnew(GridContainer);
  53. grd_main->set_columns(2);
  54. grd_main->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  55. vbc->add_child(grd_main);
  56. // ---- 1st & 2nd row
  57. Label *lbl_search = memnew(Label);
  58. lbl_search->set_text(TTR("Search:"));
  59. lne_search = memnew(LineEdit);
  60. lne_search->set_name("lne_search");
  61. lne_search->set_accessibility_name(TTRC("Search"));
  62. lne_search->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  63. Label *lbl_replace = memnew(Label);
  64. lbl_replace->set_text(TTR("Replace:"));
  65. lne_replace = memnew(LineEdit);
  66. lne_replace->set_name("lne_replace");
  67. lne_replace->set_accessibility_name(TTRC("Replace"));
  68. lne_replace->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  69. grd_main->add_child(lbl_search);
  70. grd_main->add_child(lbl_replace);
  71. grd_main->add_child(lne_search);
  72. grd_main->add_child(lne_replace);
  73. // ---- 3rd & 4th row
  74. Label *lbl_prefix = memnew(Label);
  75. lbl_prefix->set_text(TTR("Prefix:"));
  76. lne_prefix = memnew(LineEdit);
  77. lne_prefix->set_name("lne_prefix");
  78. lne_prefix->set_accessibility_name(TTRC("Prefix"));
  79. lne_prefix->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  80. Label *lbl_suffix = memnew(Label);
  81. lbl_suffix->set_text(TTR("Suffix:"));
  82. lne_suffix = memnew(LineEdit);
  83. lne_suffix->set_name("lne_suffix");
  84. lne_prefix->set_accessibility_name(TTRC("Suffix"));
  85. lne_suffix->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  86. grd_main->add_child(lbl_prefix);
  87. grd_main->add_child(lbl_suffix);
  88. grd_main->add_child(lne_prefix);
  89. grd_main->add_child(lne_suffix);
  90. // -- Feature Tabs
  91. cbut_regex = memnew(CheckButton);
  92. cbut_regex->set_text(TTR("Use Regular Expressions"));
  93. vbc->add_child(cbut_regex);
  94. CheckButton *cbut_collapse_features = memnew(CheckButton);
  95. cbut_collapse_features->set_text(TTR("Advanced Options"));
  96. vbc->add_child(cbut_collapse_features);
  97. tabc_features = memnew(TabContainer);
  98. tabc_features->set_use_hidden_tabs_for_min_size(true);
  99. vbc->add_child(tabc_features);
  100. // ---- Tab Substitute
  101. VBoxContainer *vbc_substitute = memnew(VBoxContainer);
  102. vbc_substitute->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  103. vbc_substitute->set_name(TTR("Substitute"));
  104. tabc_features->add_child(vbc_substitute);
  105. cbut_substitute = memnew(CheckBox);
  106. cbut_substitute->set_text(TTR("Substitute"));
  107. vbc_substitute->add_child(cbut_substitute);
  108. GridContainer *grd_substitute = memnew(GridContainer);
  109. grd_substitute->set_columns(3);
  110. vbc_substitute->add_child(grd_substitute);
  111. // Name
  112. but_insert_name = memnew(Button);
  113. but_insert_name->set_text("NAME");
  114. but_insert_name->set_tooltip_text(String("${NAME}\n") + TTR("Node name."));
  115. but_insert_name->set_focus_mode(Control::FOCUS_NONE);
  116. but_insert_name->connect(SceneStringName(pressed), callable_mp(this, &RenameDialog::_insert_text).bind("${NAME}"));
  117. but_insert_name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  118. grd_substitute->add_child(but_insert_name);
  119. // Parent
  120. but_insert_parent = memnew(Button);
  121. but_insert_parent->set_text("PARENT");
  122. but_insert_parent->set_tooltip_text(String("${PARENT}\n") + TTR("Node's parent name, if available."));
  123. but_insert_parent->set_focus_mode(Control::FOCUS_NONE);
  124. but_insert_parent->connect(SceneStringName(pressed), callable_mp(this, &RenameDialog::_insert_text).bind("${PARENT}"));
  125. but_insert_parent->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  126. grd_substitute->add_child(but_insert_parent);
  127. // Type
  128. but_insert_type = memnew(Button);
  129. but_insert_type->set_text("TYPE");
  130. but_insert_type->set_tooltip_text(String("${TYPE}\n") + TTR("Node type."));
  131. but_insert_type->set_focus_mode(Control::FOCUS_NONE);
  132. but_insert_type->connect(SceneStringName(pressed), callable_mp(this, &RenameDialog::_insert_text).bind("${TYPE}"));
  133. but_insert_type->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  134. grd_substitute->add_child(but_insert_type);
  135. // Scene
  136. but_insert_scene = memnew(Button);
  137. but_insert_scene->set_text("SCENE");
  138. but_insert_scene->set_tooltip_text(String("${SCENE}\n") + TTR("Current scene name."));
  139. but_insert_scene->set_focus_mode(Control::FOCUS_NONE);
  140. but_insert_scene->connect(SceneStringName(pressed), callable_mp(this, &RenameDialog::_insert_text).bind("${SCENE}"));
  141. but_insert_scene->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  142. grd_substitute->add_child(but_insert_scene);
  143. // Root
  144. but_insert_root = memnew(Button);
  145. but_insert_root->set_text("ROOT");
  146. but_insert_root->set_tooltip_text(String("${ROOT}\n") + TTR("Root node name."));
  147. but_insert_root->set_focus_mode(Control::FOCUS_NONE);
  148. but_insert_root->connect(SceneStringName(pressed), callable_mp(this, &RenameDialog::_insert_text).bind("${ROOT}"));
  149. but_insert_root->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  150. grd_substitute->add_child(but_insert_root);
  151. // Count
  152. but_insert_count = memnew(Button);
  153. but_insert_count->set_text("COUNTER");
  154. but_insert_count->set_tooltip_text(String("${COUNTER}\n") + TTR("Sequential integer counter.\nCompare counter options."));
  155. but_insert_count->set_focus_mode(Control::FOCUS_NONE);
  156. but_insert_count->connect(SceneStringName(pressed), callable_mp(this, &RenameDialog::_insert_text).bind("${COUNTER}"));
  157. but_insert_count->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  158. grd_substitute->add_child(but_insert_count);
  159. chk_per_level_counter = memnew(CheckBox);
  160. chk_per_level_counter->set_text(TTR("Per-level Counter"));
  161. chk_per_level_counter->set_tooltip_text(TTR("If set, the counter restarts for each group of child nodes."));
  162. vbc_substitute->add_child(chk_per_level_counter);
  163. HBoxContainer *hbc_count_options = memnew(HBoxContainer);
  164. vbc_substitute->add_child(hbc_count_options);
  165. Label *lbl_count_start = memnew(Label);
  166. lbl_count_start->set_text(TTR("Start"));
  167. lbl_count_start->set_tooltip_text(TTR("Initial value for the counter."));
  168. hbc_count_options->add_child(lbl_count_start);
  169. spn_count_start = memnew(SpinBox);
  170. spn_count_start->set_tooltip_text(TTR("Initial value for the counter."));
  171. spn_count_start->set_accessibility_name(TTRC("Counter Initial Value"));
  172. spn_count_start->set_step(1);
  173. spn_count_start->set_min(0);
  174. hbc_count_options->add_child(spn_count_start);
  175. Label *lbl_count_step = memnew(Label);
  176. lbl_count_step->set_text(TTR("Step"));
  177. lbl_count_step->set_tooltip_text(TTR("Amount by which counter is incremented for each node."));
  178. hbc_count_options->add_child(lbl_count_step);
  179. spn_count_step = memnew(SpinBox);
  180. spn_count_step->set_tooltip_text(TTR("Amount by which counter is incremented for each node."));
  181. spn_count_step->set_accessibility_name(TTRC("Counter Step"));
  182. spn_count_step->set_step(1);
  183. hbc_count_options->add_child(spn_count_step);
  184. Label *lbl_count_padding = memnew(Label);
  185. lbl_count_padding->set_text(TTR("Padding"));
  186. lbl_count_padding->set_tooltip_text(TTR("Minimum number of digits for the counter.\nMissing digits are padded with leading zeros."));
  187. hbc_count_options->add_child(lbl_count_padding);
  188. spn_count_padding = memnew(SpinBox);
  189. spn_count_padding->set_tooltip_text(TTR("Minimum number of digits for the counter.\nMissing digits are padded with leading zeros."));
  190. spn_count_padding->set_accessibility_name(TTRC("Minimum Number of Digits"));
  191. spn_count_padding->set_step(1);
  192. hbc_count_options->add_child(spn_count_padding);
  193. // ---- Tab Process
  194. VBoxContainer *vbc_process = memnew(VBoxContainer);
  195. vbc_process->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  196. vbc_process->set_name(TTR("Post-Process"));
  197. tabc_features->add_child(vbc_process);
  198. cbut_process = memnew(CheckBox);
  199. cbut_process->set_text(TTR("Post-Process"));
  200. vbc_process->add_child(cbut_process);
  201. // ------ Style
  202. HBoxContainer *hbc_style = memnew(HBoxContainer);
  203. vbc_process->add_child(hbc_style);
  204. Label *lbl_style = memnew(Label);
  205. lbl_style->set_text(TTR("Style"));
  206. hbc_style->add_child(lbl_style);
  207. opt_style = memnew(OptionButton);
  208. opt_style->set_accessibility_name(TTRC("Style"));
  209. opt_style->add_item(TTR("Keep"));
  210. opt_style->add_item(TTR("PascalCase to snake_case"));
  211. opt_style->add_item(TTR("snake_case to PascalCase"));
  212. hbc_style->add_child(opt_style);
  213. // ------ Case
  214. HBoxContainer *hbc_case = memnew(HBoxContainer);
  215. vbc_process->add_child(hbc_case);
  216. Label *lbl_case = memnew(Label);
  217. lbl_case->set_text(TTR("Case"));
  218. hbc_case->add_child(lbl_case);
  219. opt_case = memnew(OptionButton);
  220. opt_case->set_accessibility_name(TTRC("Case"));
  221. opt_case->add_item(TTR("Keep"));
  222. opt_case->add_item(TTR("To Lowercase"));
  223. opt_case->add_item(TTR("To Uppercase"));
  224. hbc_case->add_child(opt_case);
  225. // -- Preview
  226. HSeparator *sep_preview = memnew(HSeparator);
  227. sep_preview->set_custom_minimum_size(Size2(10, 20));
  228. vbc->add_child(sep_preview);
  229. lbl_preview_title = memnew(Label);
  230. vbc->add_child(lbl_preview_title);
  231. lbl_preview = memnew(Label);
  232. lbl_preview->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  233. lbl_preview->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  234. lbl_preview->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  235. vbc->add_child(lbl_preview);
  236. // ---- Dialog related
  237. set_min_size(Size2(383, 0));
  238. set_ok_button_text(TTR("Rename"));
  239. Button *but_reset = add_button(TTR("Reset"));
  240. eh.errfunc = _error_handler;
  241. eh.userdata = this;
  242. // ---- Connections
  243. cbut_collapse_features->connect(SceneStringName(toggled), callable_mp(this, &RenameDialog::_features_toggled));
  244. // Substitute Buttons
  245. lne_search->connect(SceneStringName(focus_entered), callable_mp(this, &RenameDialog::_update_substitute));
  246. lne_search->connect(SceneStringName(focus_exited), callable_mp(this, &RenameDialog::_update_substitute));
  247. lne_replace->connect(SceneStringName(focus_entered), callable_mp(this, &RenameDialog::_update_substitute));
  248. lne_replace->connect(SceneStringName(focus_exited), callable_mp(this, &RenameDialog::_update_substitute));
  249. lne_prefix->connect(SceneStringName(focus_entered), callable_mp(this, &RenameDialog::_update_substitute));
  250. lne_prefix->connect(SceneStringName(focus_exited), callable_mp(this, &RenameDialog::_update_substitute));
  251. lne_suffix->connect(SceneStringName(focus_entered), callable_mp(this, &RenameDialog::_update_substitute));
  252. lne_suffix->connect(SceneStringName(focus_exited), callable_mp(this, &RenameDialog::_update_substitute));
  253. // Preview
  254. lne_prefix->connect(SceneStringName(text_changed), callable_mp(this, &RenameDialog::_update_preview));
  255. lne_suffix->connect(SceneStringName(text_changed), callable_mp(this, &RenameDialog::_update_preview));
  256. lne_search->connect(SceneStringName(text_changed), callable_mp(this, &RenameDialog::_update_preview));
  257. lne_replace->connect(SceneStringName(text_changed), callable_mp(this, &RenameDialog::_update_preview));
  258. spn_count_start->connect(SceneStringName(value_changed), callable_mp(this, &RenameDialog::_update_preview_int));
  259. spn_count_step->connect(SceneStringName(value_changed), callable_mp(this, &RenameDialog::_update_preview_int));
  260. spn_count_padding->connect(SceneStringName(value_changed), callable_mp(this, &RenameDialog::_update_preview_int));
  261. opt_style->connect(SceneStringName(item_selected), callable_mp(this, &RenameDialog::_update_preview_int));
  262. opt_case->connect(SceneStringName(item_selected), callable_mp(this, &RenameDialog::_update_preview_int));
  263. cbut_substitute->connect(SceneStringName(pressed), callable_mp(this, &RenameDialog::_update_preview).bind(""));
  264. cbut_regex->connect(SceneStringName(pressed), callable_mp(this, &RenameDialog::_update_preview).bind(""));
  265. cbut_process->connect(SceneStringName(pressed), callable_mp(this, &RenameDialog::_update_preview).bind(""));
  266. but_reset->connect(SceneStringName(pressed), callable_mp(this, &RenameDialog::reset));
  267. reset();
  268. _features_toggled(false);
  269. }
  270. void RenameDialog::_bind_methods() {
  271. ClassDB::bind_method("rename", &RenameDialog::rename);
  272. }
  273. void RenameDialog::_update_substitute() {
  274. LineEdit *focus_owner_line_edit = Object::cast_to<LineEdit>(get_viewport()->gui_get_focus_owner());
  275. bool is_main_field = _is_main_field(focus_owner_line_edit);
  276. but_insert_name->set_disabled(!is_main_field);
  277. but_insert_parent->set_disabled(!is_main_field);
  278. but_insert_type->set_disabled(!is_main_field);
  279. but_insert_scene->set_disabled(!is_main_field);
  280. but_insert_root->set_disabled(!is_main_field);
  281. but_insert_count->set_disabled(!is_main_field);
  282. // The focus mode seems to be reset when disabling/re-enabling
  283. but_insert_name->set_focus_mode(Control::FOCUS_NONE);
  284. but_insert_parent->set_focus_mode(Control::FOCUS_NONE);
  285. but_insert_type->set_focus_mode(Control::FOCUS_NONE);
  286. but_insert_scene->set_focus_mode(Control::FOCUS_NONE);
  287. but_insert_root->set_focus_mode(Control::FOCUS_NONE);
  288. but_insert_count->set_focus_mode(Control::FOCUS_NONE);
  289. }
  290. void RenameDialog::_post_popup() {
  291. ConfirmationDialog::_post_popup();
  292. EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
  293. preview_node = nullptr;
  294. Array selected_node_list = editor_selection->get_selected_nodes();
  295. ERR_FAIL_COND(selected_node_list.is_empty());
  296. preview_node = Object::cast_to<Node>(selected_node_list[0]);
  297. _update_preview();
  298. _update_substitute();
  299. }
  300. void RenameDialog::_update_preview_int(int new_value) {
  301. _update_preview();
  302. }
  303. void RenameDialog::_update_preview(const String &new_text) {
  304. if (lock_preview_update || preview_node == nullptr) {
  305. return;
  306. }
  307. has_errors = false;
  308. add_error_handler(&eh);
  309. String new_name = _apply_rename(preview_node, spn_count_start->get_value());
  310. if (!has_errors) {
  311. lbl_preview_title->set_text(TTR("Preview:"));
  312. lbl_preview->set_text(new_name);
  313. if (new_name == preview_node->get_name()) {
  314. // New name is identical to the old one. Don't color it as much to avoid distracting the user.
  315. const Color accent_color = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("accent_color"), EditorStringName(Editor));
  316. const Color text_color = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("default_color"), SNAME("RichTextLabel"));
  317. lbl_preview->add_theme_color_override(SceneStringName(font_color), accent_color.lerp(text_color, 0.5));
  318. } else {
  319. lbl_preview->add_theme_color_override(SceneStringName(font_color), EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("success_color"), EditorStringName(Editor)));
  320. }
  321. }
  322. remove_error_handler(&eh);
  323. }
  324. String RenameDialog::_apply_rename(const Node *node, int count) {
  325. String search = lne_search->get_text();
  326. String replace = lne_replace->get_text();
  327. String prefix = lne_prefix->get_text();
  328. String suffix = lne_suffix->get_text();
  329. String new_name = node->get_name();
  330. if (cbut_substitute->is_pressed()) {
  331. search = _substitute(search, node, count);
  332. replace = _substitute(replace, node, count);
  333. prefix = _substitute(prefix, node, count);
  334. suffix = _substitute(suffix, node, count);
  335. }
  336. if (cbut_regex->is_pressed()) {
  337. new_name = _regex(search, new_name, replace);
  338. } else {
  339. new_name = new_name.replace(search, replace);
  340. }
  341. new_name = prefix + new_name + suffix;
  342. if (cbut_process->is_pressed()) {
  343. new_name = _postprocess(new_name);
  344. }
  345. return new_name;
  346. }
  347. String RenameDialog::_substitute(const String &subject, const Node *node, int count) {
  348. String result = subject.replace("${COUNTER}", vformat("%0" + itos(spn_count_padding->get_value()) + "d", count));
  349. if (node) {
  350. result = result.replace("${NAME}", node->get_name());
  351. result = result.replace("${TYPE}", node->get_class());
  352. }
  353. int current = EditorNode::get_editor_data().get_edited_scene();
  354. // Always request the scene title with the extension stripped.
  355. // Otherwise, the result could vary depending on whether a scene with the same name
  356. // (but different extension) is currently open.
  357. result = result.replace("${SCENE}", EditorNode::get_editor_data().get_scene_title(current, true));
  358. Node *root_node = SceneTree::get_singleton()->get_edited_scene_root();
  359. if (root_node) {
  360. result = result.replace("${ROOT}", root_node->get_name());
  361. }
  362. if (node) {
  363. Node *parent_node = node->get_parent();
  364. if (parent_node) {
  365. if (node == root_node) {
  366. // Can not substitute parent of root.
  367. result = result.replace("${PARENT}", "");
  368. } else {
  369. result = result.replace("${PARENT}", parent_node->get_name());
  370. }
  371. }
  372. }
  373. return result;
  374. }
  375. void RenameDialog::_error_handler(void *p_self, const char *p_func, const char *p_file, int p_line, const char *p_error, const char *p_errorexp, bool p_editor_notify, ErrorHandlerType p_type) {
  376. RenameDialog *self = (RenameDialog *)p_self;
  377. String source_file = String::utf8(p_file);
  378. // Only show first error that is related to "regex"
  379. if (self->has_errors || !source_file.contains("regex")) {
  380. return;
  381. }
  382. String err_str;
  383. if (p_errorexp && p_errorexp[0]) {
  384. err_str = String::utf8(p_errorexp);
  385. } else {
  386. err_str = String::utf8(p_error);
  387. }
  388. self->has_errors = true;
  389. self->lbl_preview_title->set_text(TTR("Regular Expression Error:"));
  390. self->lbl_preview->add_theme_color_override(SceneStringName(font_color), EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("error_color"), EditorStringName(Editor)));
  391. self->lbl_preview->set_text(vformat(TTR("At character %s"), err_str));
  392. }
  393. String RenameDialog::_regex(const String &pattern, const String &subject, const String &replacement) {
  394. RegEx regex(pattern);
  395. return regex.sub(subject, replacement, true);
  396. }
  397. String RenameDialog::_postprocess(const String &subject) {
  398. int style_id = opt_style->get_selected();
  399. String result = subject;
  400. if (style_id == 1) {
  401. // PascalCase to snake_case
  402. result = result.to_snake_case();
  403. result = _regex("_+", result, "_");
  404. } else if (style_id == 2) {
  405. // snake_case to PascalCase
  406. RegEx pattern("_+(.?)");
  407. Array matches = pattern.search_all(result);
  408. // The name `_` would become empty; ignore it.
  409. if (matches.size() && result != "_") {
  410. String buffer;
  411. int start = 0;
  412. int end = 0;
  413. for (int i = 0; i < matches.size(); ++i) {
  414. start = ((Ref<RegExMatch>)matches[i])->get_start(1);
  415. buffer += result.substr(end, start - end - 1);
  416. buffer += result.substr(start, 1).to_upper();
  417. end = start + 1;
  418. }
  419. buffer += result.substr(end);
  420. result = buffer.to_pascal_case();
  421. }
  422. }
  423. int case_id = opt_case->get_selected();
  424. if (case_id == 1) {
  425. // To Lowercase
  426. result = result.to_lower();
  427. } else if (case_id == 2) {
  428. // To Uppercase
  429. result = result.to_upper();
  430. }
  431. return result;
  432. }
  433. void RenameDialog::_iterate_scene(const Node *node, const Array &selection, int *counter) {
  434. if (!node) {
  435. return;
  436. }
  437. if (selection.has(node)) {
  438. String new_name = _apply_rename(node, *counter);
  439. if (node->get_name() != new_name) {
  440. Pair<NodePath, String> rename_item;
  441. rename_item.first = node->get_path();
  442. rename_item.second = new_name;
  443. to_rename.push_back(rename_item);
  444. }
  445. *counter += spn_count_step->get_value();
  446. }
  447. int *cur_counter = counter;
  448. int level_counter = spn_count_start->get_value();
  449. if (chk_per_level_counter->is_pressed()) {
  450. cur_counter = &level_counter;
  451. }
  452. for (int i = 0; i < node->get_child_count(); ++i) {
  453. _iterate_scene(node->get_child(i), selection, cur_counter);
  454. }
  455. }
  456. void RenameDialog::rename() {
  457. // Editor selection is not ordered via scene tree. Instead iterate
  458. // over scene tree until all selected nodes are found in order.
  459. EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
  460. Array selected_node_list = editor_selection->get_selected_nodes();
  461. Node *root_node = SceneTree::get_singleton()->get_edited_scene_root();
  462. global_count = spn_count_start->get_value();
  463. to_rename.clear();
  464. // Forward recursive as opposed to the actual renaming.
  465. _iterate_scene(root_node, selected_node_list, &global_count);
  466. if (!to_rename.is_empty()) {
  467. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  468. undo_redo->create_action(TTR("Batch Rename"), UndoRedo::MERGE_DISABLE, root_node, true);
  469. // Make sure to iterate reversed so that child nodes will find parents.
  470. for (List<Pair<NodePath, String>>::Element *E = to_rename.back(); E; E = E->prev()) {
  471. Node *n = root_node->get_node(E->get().first);
  472. const String &new_name = E->get().second;
  473. if (!n) {
  474. ERR_PRINT("Skipping missing node: " + E->get().first.get_concatenated_subnames());
  475. continue;
  476. }
  477. scene_tree_editor->rename_node(n, new_name);
  478. }
  479. undo_redo->commit_action();
  480. }
  481. }
  482. void RenameDialog::reset() {
  483. lock_preview_update = true;
  484. lne_prefix->clear();
  485. lne_suffix->clear();
  486. lne_search->clear();
  487. lne_replace->clear();
  488. cbut_substitute->set_pressed(false);
  489. cbut_regex->set_pressed(false);
  490. cbut_process->set_pressed(false);
  491. chk_per_level_counter->set_pressed(true);
  492. spn_count_start->set_value(1);
  493. spn_count_step->set_value(1);
  494. spn_count_padding->set_value(1);
  495. opt_style->select(0);
  496. opt_case->select(0);
  497. lock_preview_update = false;
  498. _update_preview();
  499. }
  500. bool RenameDialog::_is_main_field(LineEdit *line_edit) {
  501. return line_edit &&
  502. (line_edit == lne_search || line_edit == lne_replace || line_edit == lne_prefix || line_edit == lne_suffix);
  503. }
  504. void RenameDialog::_insert_text(const String &text) {
  505. LineEdit *focus_owner = Object::cast_to<LineEdit>(get_viewport()->gui_get_focus_owner());
  506. if (_is_main_field(focus_owner)) {
  507. focus_owner->selection_delete();
  508. focus_owner->insert_text_at_caret(text);
  509. _update_preview();
  510. }
  511. }
  512. void RenameDialog::_features_toggled(bool pressed) {
  513. if (pressed) {
  514. tabc_features->show();
  515. } else {
  516. tabc_features->hide();
  517. }
  518. // Adjust to minimum size in y
  519. Size2i new_size = get_size();
  520. new_size.y = 0;
  521. set_size(new_size);
  522. }