VBForm.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. #include "VBForm.h"
  2. #include "VBProperty.h"
  3. #include "VBWidget.h"
  4. #include "VBWidgetRegistry.h"
  5. #include <AK/JsonArray.h>
  6. #include <AK/JsonObject.h>
  7. #include <AK/StringBuilder.h>
  8. #include <LibCore/CFile.h>
  9. #include <LibDraw/PNGLoader.h>
  10. #include <LibGUI/GAction.h>
  11. #include <LibGUI/GBoxLayout.h>
  12. #include <LibGUI/GMenu.h>
  13. #include <LibGUI/GMessageBox.h>
  14. #include <LibGUI/GPainter.h>
  15. static VBForm* s_current;
  16. VBForm* VBForm::current()
  17. {
  18. return s_current;
  19. }
  20. VBForm::VBForm(const String& name, GWidget* parent)
  21. : GWidget(parent)
  22. , m_name(name)
  23. {
  24. s_current = this;
  25. set_fill_with_background_color(true);
  26. set_background_color(Color::WarmGray);
  27. set_greedy_for_hits(true);
  28. m_context_menu = make<GMenu>();
  29. m_context_menu->add_action(GCommonActions::make_move_to_front_action([this](auto&) {
  30. if (auto* widget = single_selected_widget())
  31. widget->gwidget()->move_to_front();
  32. }));
  33. m_context_menu->add_action(GCommonActions::make_move_to_back_action([this](auto&) {
  34. if (auto* widget = single_selected_widget())
  35. widget->gwidget()->move_to_back();
  36. }));
  37. m_context_menu->add_separator();
  38. m_context_menu->add_action(GAction::create("Lay out horizontally", load_png("/res/icons/16x16/layout-horizontally.png"), [this](auto&) {
  39. if (auto* widget = single_selected_widget()) {
  40. dbg() << "Giving " << *widget->gwidget() << " a horizontal box layout";
  41. widget->gwidget()->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  42. }
  43. }));
  44. m_context_menu->add_action(GAction::create("Lay out vertically", load_png("/res/icons/16x16/layout-vertically.png"), [this](auto&) {
  45. if (auto* widget = single_selected_widget()) {
  46. dbg() << "Giving " << *widget->gwidget() << " a vertical box layout";
  47. widget->gwidget()->set_layout(make<GBoxLayout>(Orientation::Vertical));
  48. }
  49. }));
  50. m_context_menu->add_separator();
  51. m_context_menu->add_action(GCommonActions::make_delete_action([this](auto&) {
  52. delete_selected_widgets();
  53. }));
  54. }
  55. void VBForm::context_menu_event(GContextMenuEvent& event)
  56. {
  57. m_context_menu->popup(event.screen_position());
  58. }
  59. void VBForm::insert_widget(VBWidgetType type)
  60. {
  61. auto* insertion_parent = single_selected_widget();
  62. auto widget = VBWidget::create(type, *this, insertion_parent);
  63. Point insertion_position = m_next_insertion_position;
  64. if (insertion_parent)
  65. insertion_position.move_by(insertion_parent->gwidget()->window_relative_rect().location());
  66. widget->set_rect({ insertion_position, { m_grid_size * 10 + 1, m_grid_size * 5 + 1 } });
  67. m_next_insertion_position.move_by(m_grid_size, m_grid_size);
  68. m_widgets.append(move(widget));
  69. }
  70. VBForm::~VBForm()
  71. {
  72. }
  73. void VBForm::paint_event(GPaintEvent& event)
  74. {
  75. GPainter painter(*this);
  76. painter.add_clip_rect(event.rect());
  77. for (int y = 0; y < height(); y += m_grid_size) {
  78. for (int x = 0; x < width(); x += m_grid_size) {
  79. painter.set_pixel({ x, y }, Color::from_rgb(0x404040));
  80. }
  81. }
  82. }
  83. void VBForm::second_paint_event(GPaintEvent& event)
  84. {
  85. GPainter painter(*this);
  86. painter.add_clip_rect(event.rect());
  87. for (auto& widget : m_widgets) {
  88. if (widget.is_selected()) {
  89. for_each_direction([&](auto direction) {
  90. bool in_layout = widget.is_in_layout();
  91. auto grabber_rect = widget.grabber_rect(direction);
  92. painter.fill_rect(grabber_rect, in_layout ? Color::White : Color::Black);
  93. if (in_layout)
  94. painter.draw_rect(grabber_rect, Color::Black);
  95. });
  96. }
  97. }
  98. }
  99. bool VBForm::is_selected(const VBWidget& widget) const
  100. {
  101. // FIXME: Fix HashTable and remove this const_cast.
  102. return m_selected_widgets.contains(const_cast<VBWidget*>(&widget));
  103. }
  104. VBWidget* VBForm::widget_at(const Point& position)
  105. {
  106. auto result = hit_test(position, GWidget::ShouldRespectGreediness::No);
  107. if (!result.widget)
  108. return nullptr;
  109. auto* gwidget = result.widget;
  110. while (gwidget) {
  111. if (auto* widget = m_gwidget_map.get(gwidget).value_or(nullptr))
  112. return widget;
  113. gwidget = gwidget->parent_widget();
  114. }
  115. return nullptr;
  116. }
  117. void VBForm::grabber_mousedown_event(GMouseEvent& event, Direction grabber)
  118. {
  119. m_transform_event_origin = event.position();
  120. for_each_selected_widget([](auto& widget) { widget.capture_transform_origin_rect(); });
  121. m_resize_direction = grabber;
  122. }
  123. void VBForm::keydown_event(GKeyEvent& event)
  124. {
  125. if (event.key() == KeyCode::Key_Delete) {
  126. delete_selected_widgets();
  127. return;
  128. }
  129. if (event.key() == KeyCode::Key_Tab) {
  130. if (m_widgets.is_empty())
  131. return;
  132. if (m_selected_widgets.is_empty()) {
  133. set_single_selected_widget(&m_widgets.first());
  134. update();
  135. return;
  136. }
  137. int selected_widget_index = 0;
  138. for (; selected_widget_index < m_widgets.size(); ++selected_widget_index) {
  139. if (&m_widgets[selected_widget_index] == *m_selected_widgets.begin())
  140. break;
  141. }
  142. ++selected_widget_index;
  143. if (selected_widget_index == m_widgets.size())
  144. selected_widget_index = 0;
  145. set_single_selected_widget(&m_widgets[selected_widget_index]);
  146. update();
  147. return;
  148. }
  149. if (!m_selected_widgets.is_empty()) {
  150. switch (event.key()) {
  151. case KeyCode::Key_Up:
  152. update();
  153. for_each_selected_widget([this](auto& widget) {
  154. if (widget.is_in_layout())
  155. return;
  156. widget.gwidget()->move_by(0, -m_grid_size);
  157. });
  158. break;
  159. case KeyCode::Key_Down:
  160. update();
  161. for_each_selected_widget([this](auto& widget) {
  162. if (widget.is_in_layout())
  163. return;
  164. widget.gwidget()->move_by(0, m_grid_size);
  165. });
  166. break;
  167. case KeyCode::Key_Left:
  168. update();
  169. for_each_selected_widget([this](auto& widget) {
  170. if (widget.is_in_layout())
  171. return;
  172. widget.gwidget()->move_by(-m_grid_size, 0);
  173. });
  174. break;
  175. case KeyCode::Key_Right:
  176. update();
  177. for_each_selected_widget([this](auto& widget) {
  178. if (widget.is_in_layout())
  179. return;
  180. widget.gwidget()->move_by(m_grid_size, 0);
  181. });
  182. break;
  183. }
  184. return;
  185. }
  186. }
  187. void VBForm::set_single_selected_widget(VBWidget* widget)
  188. {
  189. if (!widget) {
  190. if (!m_selected_widgets.is_empty()) {
  191. m_selected_widgets.clear();
  192. on_widget_selected(nullptr);
  193. update();
  194. }
  195. return;
  196. }
  197. m_selected_widgets.clear();
  198. m_selected_widgets.set(widget);
  199. on_widget_selected(m_selected_widgets.size() == 1 ? widget : nullptr);
  200. update();
  201. }
  202. void VBForm::add_to_selection(VBWidget& widget)
  203. {
  204. m_selected_widgets.set(&widget);
  205. update();
  206. }
  207. void VBForm::remove_from_selection(VBWidget& widget)
  208. {
  209. m_selected_widgets.remove(&widget);
  210. update();
  211. }
  212. void VBForm::mousedown_event(GMouseEvent& event)
  213. {
  214. if (m_resize_direction == Direction::None) {
  215. bool hit_grabber = false;
  216. for_each_selected_widget([&](auto& widget) {
  217. if (widget.is_in_layout())
  218. return;
  219. auto grabber = widget.grabber_at(event.position());
  220. if (grabber != Direction::None) {
  221. hit_grabber = true;
  222. return grabber_mousedown_event(event, grabber);
  223. }
  224. });
  225. if (hit_grabber)
  226. return;
  227. }
  228. auto* widget = widget_at(event.position());
  229. if (!widget) {
  230. set_single_selected_widget(nullptr);
  231. return;
  232. }
  233. if (event.button() == GMouseButton::Left || event.button() == GMouseButton::Right) {
  234. m_transform_event_origin = event.position();
  235. if (event.modifiers() == Mod_Ctrl)
  236. remove_from_selection(*widget);
  237. else if (event.modifiers() == Mod_Shift)
  238. add_to_selection(*widget);
  239. else if (!m_selected_widgets.contains(widget))
  240. set_single_selected_widget(widget);
  241. for_each_selected_widget([](auto& widget) { widget.capture_transform_origin_rect(); });
  242. on_widget_selected(single_selected_widget());
  243. }
  244. }
  245. void VBForm::mousemove_event(GMouseEvent& event)
  246. {
  247. if (event.buttons() & GMouseButton::Left) {
  248. if (m_resize_direction == Direction::None) {
  249. update();
  250. auto delta = event.position() - m_transform_event_origin;
  251. for_each_selected_widget([&](auto& widget) {
  252. if (widget.is_in_layout())
  253. return;
  254. auto new_rect = widget.transform_origin_rect().translated(delta);
  255. new_rect.set_x(new_rect.x() - (new_rect.x() % m_grid_size));
  256. new_rect.set_y(new_rect.y() - (new_rect.y() % m_grid_size));
  257. widget.set_rect(new_rect);
  258. });
  259. return;
  260. }
  261. int diff_x = event.x() - m_transform_event_origin.x();
  262. int diff_y = event.y() - m_transform_event_origin.y();
  263. int change_x = 0;
  264. int change_y = 0;
  265. int change_w = 0;
  266. int change_h = 0;
  267. switch (m_resize_direction) {
  268. case Direction::DownRight:
  269. change_w = diff_x;
  270. change_h = diff_y;
  271. break;
  272. case Direction::Right:
  273. change_w = diff_x;
  274. break;
  275. case Direction::UpRight:
  276. change_w = diff_x;
  277. change_y = diff_y;
  278. change_h = -diff_y;
  279. break;
  280. case Direction::Up:
  281. change_y = diff_y;
  282. change_h = -diff_y;
  283. break;
  284. case Direction::UpLeft:
  285. change_x = diff_x;
  286. change_w = -diff_x;
  287. change_y = diff_y;
  288. change_h = -diff_y;
  289. break;
  290. case Direction::Left:
  291. change_x = diff_x;
  292. change_w = -diff_x;
  293. break;
  294. case Direction::DownLeft:
  295. change_x = diff_x;
  296. change_w = -diff_x;
  297. change_h = diff_y;
  298. break;
  299. case Direction::Down:
  300. change_h = diff_y;
  301. break;
  302. default:
  303. ASSERT_NOT_REACHED();
  304. }
  305. update();
  306. for_each_selected_widget([&](auto& widget) {
  307. if (widget.is_in_layout())
  308. return;
  309. auto new_rect = widget.transform_origin_rect();
  310. Size minimum_size { 5, 5 };
  311. new_rect.set_x(new_rect.x() + change_x);
  312. new_rect.set_y(new_rect.y() + change_y);
  313. new_rect.set_width(max(minimum_size.width(), new_rect.width() + change_w));
  314. new_rect.set_height(max(minimum_size.height(), new_rect.height() + change_h));
  315. new_rect.set_x(new_rect.x() - (new_rect.x() % m_grid_size));
  316. new_rect.set_y(new_rect.y() - (new_rect.y() % m_grid_size));
  317. new_rect.set_width(new_rect.width() - (new_rect.width() % m_grid_size) + 1);
  318. new_rect.set_height(new_rect.height() - (new_rect.height() % m_grid_size) + 1);
  319. widget.set_rect(new_rect);
  320. });
  321. set_cursor_type_from_grabber(m_resize_direction);
  322. } else {
  323. for (auto& widget : m_selected_widgets) {
  324. if (widget->is_in_layout())
  325. continue;
  326. auto grabber_at = widget->grabber_at(event.position());
  327. set_cursor_type_from_grabber(grabber_at);
  328. if (grabber_at != Direction::None)
  329. break;
  330. }
  331. }
  332. }
  333. void VBForm::load_from_file(const String& path)
  334. {
  335. CFile file(path);
  336. if (!file.open(CIODevice::ReadOnly)) {
  337. GMessageBox::show(String::format("Could not open '%s' for reading", path.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  338. return;
  339. }
  340. auto file_contents = file.read_all();
  341. auto form_json = JsonValue::from_string(file_contents);
  342. if (!form_json.is_object()) {
  343. GMessageBox::show(String::format("Could not parse '%s'", path.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  344. return;
  345. }
  346. m_name = form_json.as_object().get("name").to_string();
  347. auto widgets = form_json.as_object().get("widgets").as_array();
  348. widgets.for_each([&](const JsonValue& widget_value) {
  349. auto& widget_object = widget_value.as_object();
  350. auto widget_class = widget_object.get("class").as_string();
  351. auto widget_type = widget_type_from_class_name(widget_class);
  352. // FIXME: Construct VBWidget within the right parent..
  353. auto vbwidget = VBWidget::create(widget_type, *this, nullptr);
  354. widget_object.for_each_member([&](auto& property_name, const JsonValue& property_value) {
  355. (void)property_name;
  356. (void)property_value;
  357. VBProperty& property = vbwidget->property(property_name);
  358. dbgprintf("Set property %s.%s to '%s'\n", widget_class.characters(), property_name.characters(), property_value.to_string().characters());
  359. property.set_value(property_value);
  360. });
  361. m_widgets.append(vbwidget);
  362. });
  363. }
  364. void VBForm::write_to_file(const String& path)
  365. {
  366. CFile file(path);
  367. if (!file.open(CIODevice::WriteOnly)) {
  368. GMessageBox::show(String::format("Could not open '%s' for writing", path.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  369. return;
  370. }
  371. JsonObject form_object;
  372. form_object.set("name", m_name);
  373. JsonArray widget_array;
  374. for (auto& widget : m_widgets) {
  375. JsonObject widget_object;
  376. widget.for_each_property([&](auto& property) {
  377. if (property.value().is_bool())
  378. widget_object.set(property.name(), property.value().to_bool());
  379. else if (property.value().is_int())
  380. widget_object.set(property.name(), property.value().to_int());
  381. else
  382. widget_object.set(property.name(), property.value().to_string());
  383. });
  384. widget_array.append(widget_object);
  385. }
  386. form_object.set("widgets", widget_array);
  387. file.write(form_object.to_string());
  388. }
  389. void VBForm::dump()
  390. {
  391. dbgprintf("[Form]\n");
  392. dbgprintf("Name=%s\n", m_name.characters());
  393. dbgprintf("\n");
  394. int i = 0;
  395. for (auto& widget : m_widgets) {
  396. dbgprintf("[Widget %d]\n", i++);
  397. widget.for_each_property([](auto& property) {
  398. dbgprintf("%s=%s\n", property.name().characters(), property.value().to_string().characters());
  399. });
  400. dbgprintf("\n");
  401. }
  402. }
  403. void VBForm::mouseup_event(GMouseEvent& event)
  404. {
  405. if (event.button() == GMouseButton::Left) {
  406. m_transform_event_origin = {};
  407. m_resize_direction = Direction::None;
  408. }
  409. }
  410. void VBForm::delete_selected_widgets()
  411. {
  412. Vector<VBWidget*> to_delete;
  413. for_each_selected_widget([&](auto& widget) {
  414. to_delete.append(&widget);
  415. });
  416. if (to_delete.is_empty())
  417. return;
  418. for (auto& widget : to_delete)
  419. m_widgets.remove_first_matching([&widget](auto& entry) { return entry == widget; });
  420. on_widget_selected(single_selected_widget());
  421. update();
  422. }
  423. template<typename Callback>
  424. void VBForm::for_each_selected_widget(Callback callback)
  425. {
  426. for (auto& widget : m_selected_widgets)
  427. callback(*widget);
  428. }
  429. void VBForm::set_cursor_type_from_grabber(Direction grabber)
  430. {
  431. if (grabber == m_mouse_direction_type)
  432. return;
  433. switch (grabber) {
  434. case Direction::Up:
  435. case Direction::Down:
  436. window()->set_override_cursor(GStandardCursor::ResizeVertical);
  437. break;
  438. case Direction::Left:
  439. case Direction::Right:
  440. window()->set_override_cursor(GStandardCursor::ResizeHorizontal);
  441. break;
  442. case Direction::UpLeft:
  443. case Direction::DownRight:
  444. window()->set_override_cursor(GStandardCursor::ResizeDiagonalTLBR);
  445. break;
  446. case Direction::UpRight:
  447. case Direction::DownLeft:
  448. window()->set_override_cursor(GStandardCursor::ResizeDiagonalBLTR);
  449. break;
  450. case Direction::None:
  451. window()->set_override_cursor(GStandardCursor::None);
  452. break;
  453. }
  454. m_mouse_direction_type = grabber;
  455. }
  456. VBWidget* VBForm::single_selected_widget()
  457. {
  458. if (m_selected_widgets.size() != 1)
  459. return nullptr;
  460. return *m_selected_widgets.begin();
  461. }