HeaderView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/AbstractTableView.h>
  8. #include <LibGUI/Action.h>
  9. #include <LibGUI/HeaderView.h>
  10. #include <LibGUI/Menu.h>
  11. #include <LibGUI/Model.h>
  12. #include <LibGUI/Painter.h>
  13. #include <LibGUI/Window.h>
  14. #include <LibGfx/Font/FontDatabase.h>
  15. #include <LibGfx/Palette.h>
  16. #include <LibGfx/StylePainter.h>
  17. namespace GUI {
  18. HeaderView::HeaderView(AbstractTableView& table_view, Gfx::Orientation orientation)
  19. : m_table_view(table_view)
  20. , m_orientation(orientation)
  21. {
  22. set_font(Gfx::FontDatabase::default_font().bold_variant());
  23. if (m_orientation == Gfx::Orientation::Horizontal) {
  24. set_fixed_height(16);
  25. } else {
  26. set_fixed_width(40);
  27. }
  28. }
  29. void HeaderView::set_section_size(int section, int size)
  30. {
  31. auto& data = section_data(section);
  32. if (data.size == size)
  33. return;
  34. data.size = size;
  35. data.has_initialized_size = true;
  36. data.size = size;
  37. m_table_view.header_did_change_section_size({}, m_orientation, section, size);
  38. }
  39. int HeaderView::section_size(int section) const
  40. {
  41. return section_data(section).size;
  42. }
  43. HeaderView::SectionData& HeaderView::section_data(int section) const
  44. {
  45. VERIFY(model());
  46. if (static_cast<size_t>(section) >= m_section_data.size()) {
  47. m_section_data.resize(section_count());
  48. }
  49. return m_section_data.at(section);
  50. }
  51. Gfx::IntRect HeaderView::section_rect(int section) const
  52. {
  53. if (!model())
  54. return {};
  55. auto& data = section_data(section);
  56. if (!data.visibility)
  57. return {};
  58. int offset = 0;
  59. for (int i = 0; i < section; ++i) {
  60. if (!is_section_visible(i))
  61. continue;
  62. offset += section_data(i).size;
  63. if (orientation() == Gfx::Orientation::Horizontal)
  64. offset += m_table_view.horizontal_padding() * 2;
  65. }
  66. if (orientation() == Gfx::Orientation::Horizontal)
  67. return { offset, 0, section_size(section) + m_table_view.horizontal_padding() * 2, height() };
  68. return { 0, offset, width(), section_size(section) };
  69. }
  70. HeaderView::VisibleSectionRange HeaderView::visible_section_range() const
  71. {
  72. auto section_count = this->section_count();
  73. auto is_horizontal = m_orientation == Orientation::Horizontal;
  74. auto rect = m_table_view.visible_content_rect();
  75. auto start = is_horizontal ? rect.top_left().x() : rect.top_left().y();
  76. auto end = is_horizontal ? (rect.top_left().x() + m_table_view.content_width()) : rect.bottom_left().y();
  77. auto offset = 0;
  78. VisibleSectionRange range;
  79. for (; range.end < section_count; ++range.end) {
  80. auto& section = section_data(range.end);
  81. int section_size = section.size;
  82. if (orientation() == Gfx::Orientation::Horizontal)
  83. section_size += m_table_view.horizontal_padding() * 2;
  84. if (offset + section_size < start) {
  85. if (section.visibility)
  86. offset += section_size;
  87. ++range.start;
  88. range.start_offset = offset;
  89. continue;
  90. }
  91. if (offset >= end)
  92. break;
  93. if (section.visibility)
  94. offset += section_size;
  95. }
  96. return range;
  97. }
  98. Gfx::IntRect HeaderView::section_resize_grabbable_rect(int section) const
  99. {
  100. if (!model())
  101. return {};
  102. // FIXME: Support resizable rows.
  103. if (m_orientation == Gfx::Orientation::Vertical)
  104. return {};
  105. auto rect = section_rect(section);
  106. return { rect.right() - 1, rect.top(), 4, rect.height() };
  107. }
  108. int HeaderView::section_count() const
  109. {
  110. if (!model())
  111. return 0;
  112. return m_orientation == Gfx::Orientation::Horizontal ? model()->column_count() : model()->row_count();
  113. }
  114. void HeaderView::doubleclick_event(MouseEvent& event)
  115. {
  116. if (!model())
  117. return;
  118. auto range = visible_section_range();
  119. for (int i = range.start; i < range.end; ++i) {
  120. if (section_resize_grabbable_rect(i).contains(event.position())) {
  121. if (on_resize_doubleclick)
  122. on_resize_doubleclick(i);
  123. }
  124. }
  125. }
  126. void HeaderView::mousedown_event(MouseEvent& event)
  127. {
  128. if (event.button() != GUI::MouseButton::Primary)
  129. return;
  130. if (!model())
  131. return;
  132. auto& model = *this->model();
  133. auto range = visible_section_range();
  134. for (int i = range.start; i < range.end; ++i) {
  135. if (section_resize_grabbable_rect(i).contains(event.position())) {
  136. m_resizing_section = i;
  137. m_in_section_resize = true;
  138. m_section_resize_original_width = section_size(i);
  139. m_section_resize_origin = event.position();
  140. return;
  141. }
  142. auto rect = this->section_rect(i);
  143. if (rect.contains(event.position()) && model.is_column_sortable(i)) {
  144. m_pressed_section = i;
  145. m_pressed_section_is_pressed = true;
  146. update();
  147. return;
  148. }
  149. }
  150. }
  151. void HeaderView::mousemove_event(MouseEvent& event)
  152. {
  153. if (!model())
  154. return;
  155. if (m_in_section_resize) {
  156. auto delta = event.position() - m_section_resize_origin;
  157. int new_size = m_section_resize_original_width + delta.primary_offset_for_orientation(m_orientation);
  158. auto minimum_size = orientation() == Orientation::Horizontal
  159. ? m_table_view.minimum_column_width(m_resizing_section)
  160. : m_table_view.minimum_row_height(m_resizing_section);
  161. if (new_size <= minimum_size)
  162. new_size = minimum_size;
  163. VERIFY(m_resizing_section >= 0 && m_resizing_section < model()->column_count());
  164. set_section_size(m_resizing_section, new_size);
  165. return;
  166. }
  167. if (m_pressed_section != -1) {
  168. auto header_rect = this->section_rect(m_pressed_section);
  169. if (header_rect.contains(event.position())) {
  170. set_hovered_section(m_pressed_section);
  171. if (!m_pressed_section_is_pressed)
  172. update();
  173. m_pressed_section_is_pressed = true;
  174. } else {
  175. set_hovered_section(-1);
  176. if (m_pressed_section_is_pressed)
  177. update();
  178. m_pressed_section_is_pressed = false;
  179. }
  180. return;
  181. }
  182. if (event.buttons() == 0) {
  183. bool found_hovered_header = false;
  184. auto range = visible_section_range();
  185. for (int i = range.start; i < range.end; ++i) {
  186. if (section_resize_grabbable_rect(i).contains(event.position())) {
  187. set_override_cursor(Gfx::StandardCursor::ResizeColumn);
  188. set_hovered_section(-1);
  189. return;
  190. }
  191. if (section_rect(i).contains(event.position())) {
  192. set_hovered_section(i);
  193. found_hovered_header = true;
  194. }
  195. }
  196. if (!found_hovered_header)
  197. set_hovered_section(-1);
  198. }
  199. set_override_cursor(Gfx::StandardCursor::None);
  200. }
  201. void HeaderView::mouseup_event(MouseEvent& event)
  202. {
  203. if (event.button() == MouseButton::Primary) {
  204. if (m_in_section_resize) {
  205. if (!section_resize_grabbable_rect(m_resizing_section).contains(event.position()))
  206. set_override_cursor(Gfx::StandardCursor::None);
  207. m_in_section_resize = false;
  208. return;
  209. }
  210. if (m_pressed_section != -1) {
  211. if (m_orientation == Gfx::Orientation::Horizontal && section_rect(m_pressed_section).contains(event.position())) {
  212. auto new_sort_order = m_table_view.sort_order();
  213. if (m_table_view.key_column() == m_pressed_section)
  214. new_sort_order = m_table_view.sort_order() == SortOrder::Ascending
  215. ? SortOrder::Descending
  216. : SortOrder::Ascending;
  217. m_table_view.set_key_column_and_sort_order(m_pressed_section, new_sort_order);
  218. }
  219. m_pressed_section = -1;
  220. m_pressed_section_is_pressed = false;
  221. update();
  222. return;
  223. }
  224. }
  225. }
  226. void HeaderView::paint_horizontal(Painter& painter)
  227. {
  228. painter.draw_line({ 0, 0 }, { rect().right(), 0 }, palette().threed_highlight());
  229. painter.draw_line({ 0, rect().bottom() }, { rect().right(), rect().bottom() }, palette().threed_shadow1());
  230. auto range = visible_section_range();
  231. int x_offset = range.start_offset;
  232. for (int section = range.start; section < range.end; ++section) {
  233. auto& section_data = this->section_data(section);
  234. if (!section_data.visibility)
  235. continue;
  236. int section_width = section_data.size;
  237. bool is_key_column = m_table_view.key_column() == section;
  238. Gfx::IntRect cell_rect(x_offset, 0, section_width + m_table_view.horizontal_padding() * 2, height());
  239. bool pressed = section == m_pressed_section && m_pressed_section_is_pressed;
  240. bool hovered = section == m_hovered_section && model()->is_column_sortable(section);
  241. Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, pressed, hovered);
  242. String text;
  243. if (is_key_column) {
  244. StringBuilder builder;
  245. builder.append(model()->column_name(section));
  246. if (m_table_view.sort_order() == SortOrder::Ascending)
  247. builder.append(" \xE2\xAC\x86"); // UPWARDS BLACK ARROW
  248. else if (m_table_view.sort_order() == SortOrder::Descending)
  249. builder.append(" \xE2\xAC\x87"); // DOWNWARDS BLACK ARROW
  250. text = builder.to_string();
  251. } else {
  252. text = model()->column_name(section);
  253. }
  254. auto text_rect = cell_rect.shrunken(m_table_view.horizontal_padding() * 2, 0);
  255. if (pressed)
  256. text_rect.translate_by(1, 1);
  257. painter.draw_text(text_rect, text, font(), section_data.alignment, palette().button_text());
  258. x_offset += section_width + m_table_view.horizontal_padding() * 2;
  259. }
  260. if (x_offset < rect().right()) {
  261. Gfx::IntRect cell_rect(x_offset, 0, width() - x_offset, height());
  262. Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, false, false);
  263. }
  264. }
  265. void HeaderView::paint_vertical(Painter& painter)
  266. {
  267. painter.draw_line(rect().top_left(), rect().bottom_left(), palette().threed_highlight());
  268. painter.draw_line(rect().top_right(), rect().bottom_right(), palette().threed_shadow1());
  269. auto range = visible_section_range();
  270. int y_offset = range.start_offset;
  271. for (int section = range.start; section < range.end; ++section) {
  272. auto& section_data = this->section_data(section);
  273. if (!section_data.visibility)
  274. continue;
  275. int section_size = section_data.size;
  276. Gfx::IntRect cell_rect(0, y_offset, width(), section_size);
  277. bool pressed = section == m_pressed_section && m_pressed_section_is_pressed;
  278. bool hovered = false;
  279. Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, pressed, hovered);
  280. String text = String::number(section);
  281. auto text_rect = cell_rect.shrunken(m_table_view.horizontal_padding() * 2, 0);
  282. if (pressed)
  283. text_rect.translate_by(1, 1);
  284. painter.draw_text(text_rect, text, font(), section_data.alignment, palette().button_text());
  285. y_offset += section_size;
  286. }
  287. if (y_offset < rect().bottom()) {
  288. Gfx::IntRect cell_rect(0, y_offset, width(), height() - y_offset);
  289. Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, false, false);
  290. }
  291. }
  292. void HeaderView::paint_event(PaintEvent& event)
  293. {
  294. Painter painter(*this);
  295. painter.add_clip_rect(event.rect());
  296. painter.fill_rect(rect(), palette().button());
  297. if (orientation() == Gfx::Orientation::Horizontal)
  298. paint_horizontal(painter);
  299. else
  300. paint_vertical(painter);
  301. }
  302. void HeaderView::set_section_visible(int section, bool visible)
  303. {
  304. auto& data = section_data(section);
  305. if (data.visibility == visible)
  306. return;
  307. data.visibility = visible;
  308. if (data.visibility_action) {
  309. data.visibility_action->set_checked(visible);
  310. }
  311. m_table_view.header_did_change_section_visibility({}, m_orientation, section, visible);
  312. update();
  313. }
  314. Menu& HeaderView::ensure_context_menu()
  315. {
  316. // FIXME: This menu needs to be rebuilt if the model is swapped out,
  317. // or if the column count/names change.
  318. if (!m_context_menu) {
  319. VERIFY(model());
  320. m_context_menu = Menu::construct();
  321. if (m_orientation == Gfx::Orientation::Vertical) {
  322. dbgln("FIXME: Support context menus for vertical GUI::HeaderView");
  323. return *m_context_menu;
  324. }
  325. int section_count = this->section_count();
  326. for (int section = 0; section < section_count; ++section) {
  327. auto& column_data = this->section_data(section);
  328. auto name = model()->column_name(section);
  329. column_data.visibility_action = Action::create_checkable(name, [this, section](auto& action) {
  330. set_section_visible(section, action.is_checked());
  331. });
  332. column_data.visibility_action->set_checked(column_data.visibility);
  333. m_context_menu->add_action(*column_data.visibility_action);
  334. }
  335. }
  336. return *m_context_menu;
  337. }
  338. void HeaderView::context_menu_event(ContextMenuEvent& event)
  339. {
  340. ensure_context_menu().popup(event.screen_position());
  341. }
  342. void HeaderView::leave_event(Core::Event& event)
  343. {
  344. Widget::leave_event(event);
  345. set_hovered_section(-1);
  346. }
  347. Gfx::TextAlignment HeaderView::section_alignment(int section) const
  348. {
  349. return section_data(section).alignment;
  350. }
  351. void HeaderView::set_section_alignment(int section, Gfx::TextAlignment alignment)
  352. {
  353. section_data(section).alignment = alignment;
  354. }
  355. void HeaderView::set_default_section_size(int section, int size)
  356. {
  357. auto minimum_column_width = m_table_view.minimum_column_width(section);
  358. if (orientation() == Gfx::Orientation::Horizontal && size < minimum_column_width)
  359. size = minimum_column_width;
  360. auto& data = section_data(section);
  361. if (data.default_size == size)
  362. return;
  363. data.default_size = size;
  364. data.has_initialized_default_size = true;
  365. }
  366. int HeaderView::default_section_size(int section) const
  367. {
  368. return section_data(section).default_size;
  369. }
  370. bool HeaderView::is_default_section_size_initialized(int section) const
  371. {
  372. return section_data(section).has_initialized_default_size;
  373. }
  374. bool HeaderView::is_section_visible(int section) const
  375. {
  376. return section_data(section).visibility;
  377. }
  378. void HeaderView::set_hovered_section(int section)
  379. {
  380. if (m_hovered_section == section)
  381. return;
  382. m_hovered_section = section;
  383. update();
  384. }
  385. Model* HeaderView::model()
  386. {
  387. return m_table_view.model();
  388. }
  389. Model const* HeaderView::model() const
  390. {
  391. return m_table_view.model();
  392. }
  393. }