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.left() : rect.top();
  76. auto end = is_horizontal ? (rect.left() + m_table_view.content_width()) : rect.bottom() - 1;
  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() - 2, 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() - 1, 0 }, palette().threed_highlight());
  229. painter.draw_line({ 0, rect().bottom() - 1 }, { rect().right() - 1, rect().bottom() - 1 }, 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. auto text = model()->column_name(section);
  243. auto text_rect = cell_rect.shrunken(m_table_view.horizontal_padding() * 2, 0);
  244. if (pressed)
  245. text_rect.translate_by(1, 1);
  246. painter.draw_text(text_rect, text, font(), section_data.alignment, palette().button_text());
  247. if (is_key_column && (m_table_view.sort_order() != SortOrder::None)) {
  248. Gfx::IntPoint offset { text_rect.x() + font().width_rounded_up(text) + sorting_arrow_offset, sorting_arrow_offset };
  249. auto coordinates = m_table_view.sort_order() == SortOrder::Ascending
  250. ? ascending_arrow_coordinates.span()
  251. : descending_arrow_coordinates.span();
  252. painter.draw_triangle(offset, coordinates, palette().button_text());
  253. }
  254. x_offset += section_width + m_table_view.horizontal_padding() * 2;
  255. }
  256. if (x_offset < rect().right() - 1) {
  257. Gfx::IntRect cell_rect(x_offset, 0, width() - x_offset, height());
  258. Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, false, false);
  259. }
  260. }
  261. void HeaderView::paint_vertical(Painter& painter)
  262. {
  263. painter.draw_line(rect().top_left(), rect().bottom_left().moved_up(1), palette().threed_highlight());
  264. painter.draw_line(rect().top_right().moved_left(1), rect().bottom_right().translated(-1), palette().threed_shadow1());
  265. auto range = visible_section_range();
  266. int y_offset = range.start_offset;
  267. for (int section = range.start; section < range.end; ++section) {
  268. auto& section_data = this->section_data(section);
  269. if (!section_data.visibility)
  270. continue;
  271. int section_size = section_data.size;
  272. Gfx::IntRect cell_rect(0, y_offset, width(), section_size);
  273. bool pressed = section == m_pressed_section && m_pressed_section_is_pressed;
  274. bool hovered = false;
  275. Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, pressed, hovered);
  276. DeprecatedString text = DeprecatedString::number(section);
  277. auto text_rect = cell_rect.shrunken(m_table_view.horizontal_padding() * 2, 0);
  278. if (pressed)
  279. text_rect.translate_by(1, 1);
  280. painter.draw_text(text_rect, text, font(), section_data.alignment, palette().button_text());
  281. y_offset += section_size;
  282. }
  283. if (y_offset < rect().bottom() - 1) {
  284. Gfx::IntRect cell_rect(0, y_offset, width(), height() - y_offset);
  285. Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, false, false);
  286. }
  287. }
  288. void HeaderView::paint_event(PaintEvent& event)
  289. {
  290. Painter painter(*this);
  291. painter.add_clip_rect(event.rect());
  292. painter.fill_rect(rect(), palette().button());
  293. if (orientation() == Gfx::Orientation::Horizontal)
  294. paint_horizontal(painter);
  295. else
  296. paint_vertical(painter);
  297. }
  298. void HeaderView::set_section_visible(int section, bool visible)
  299. {
  300. auto& data = section_data(section);
  301. if (data.visibility == visible)
  302. return;
  303. data.visibility = visible;
  304. if (data.visibility_action) {
  305. data.visibility_action->set_checked(visible);
  306. }
  307. m_table_view.header_did_change_section_visibility({}, m_orientation, section, visible);
  308. update();
  309. }
  310. Menu& HeaderView::ensure_context_menu()
  311. {
  312. // FIXME: This menu needs to be rebuilt if the model is swapped out,
  313. // or if the column count/names change.
  314. if (!m_context_menu) {
  315. VERIFY(model());
  316. m_context_menu = Menu::construct();
  317. if (m_orientation == Gfx::Orientation::Vertical) {
  318. dbgln("FIXME: Support context menus for vertical GUI::HeaderView");
  319. return *m_context_menu;
  320. }
  321. int section_count = this->section_count();
  322. for (int section = 0; section < section_count; ++section) {
  323. auto& column_data = this->section_data(section);
  324. auto name = model()->column_name(section).to_deprecated_string();
  325. column_data.visibility_action = Action::create_checkable(name, [this, section](auto& action) {
  326. set_section_visible(section, action.is_checked());
  327. });
  328. column_data.visibility_action->set_checked(column_data.visibility);
  329. m_context_menu->add_action(*column_data.visibility_action);
  330. }
  331. }
  332. return *m_context_menu;
  333. }
  334. void HeaderView::context_menu_event(ContextMenuEvent& event)
  335. {
  336. ensure_context_menu().popup(event.screen_position());
  337. }
  338. void HeaderView::leave_event(Core::Event& event)
  339. {
  340. Widget::leave_event(event);
  341. set_hovered_section(-1);
  342. }
  343. Gfx::TextAlignment HeaderView::section_alignment(int section) const
  344. {
  345. return section_data(section).alignment;
  346. }
  347. void HeaderView::set_section_alignment(int section, Gfx::TextAlignment alignment)
  348. {
  349. section_data(section).alignment = alignment;
  350. }
  351. void HeaderView::set_default_section_size(int section, int size)
  352. {
  353. auto minimum_column_width = m_table_view.minimum_column_width(section);
  354. if (orientation() == Gfx::Orientation::Horizontal && size < minimum_column_width)
  355. size = minimum_column_width;
  356. auto& data = section_data(section);
  357. if (data.default_size == size)
  358. return;
  359. data.default_size = size;
  360. data.has_initialized_default_size = true;
  361. }
  362. int HeaderView::default_section_size(int section) const
  363. {
  364. return section_data(section).default_size;
  365. }
  366. bool HeaderView::is_default_section_size_initialized(int section) const
  367. {
  368. return section_data(section).has_initialized_default_size;
  369. }
  370. bool HeaderView::is_section_visible(int section) const
  371. {
  372. return section_data(section).visibility;
  373. }
  374. void HeaderView::set_hovered_section(int section)
  375. {
  376. if (m_hovered_section == section)
  377. return;
  378. m_hovered_section = section;
  379. update();
  380. }
  381. Model* HeaderView::model()
  382. {
  383. return m_table_view.model();
  384. }
  385. Model const* HeaderView::model() const
  386. {
  387. return m_table_view.model();
  388. }
  389. }