HeaderView.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/AbstractTableView.h>
  7. #include <LibGUI/Action.h>
  8. #include <LibGUI/HeaderView.h>
  9. #include <LibGUI/Menu.h>
  10. #include <LibGUI/Model.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGUI/Window.h>
  13. #include <LibGfx/FontDatabase.h>
  14. #include <LibGfx/Palette.h>
  15. #include <LibGfx/StylePainter.h>
  16. namespace GUI {
  17. static constexpr int minimum_column_size = 2;
  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. HeaderView::~HeaderView()
  30. {
  31. }
  32. void HeaderView::set_section_size(int section, int size)
  33. {
  34. auto& data = section_data(section);
  35. if (data.size == size)
  36. return;
  37. data.size = size;
  38. data.has_initialized_size = true;
  39. data.size = size;
  40. m_table_view.header_did_change_section_size({}, m_orientation, section, size);
  41. }
  42. int HeaderView::section_size(int section) const
  43. {
  44. return section_data(section).size;
  45. }
  46. HeaderView::SectionData& HeaderView::section_data(int section) const
  47. {
  48. VERIFY(model());
  49. if (static_cast<size_t>(section) >= m_section_data.size()) {
  50. m_section_data.resize(section_count());
  51. }
  52. return m_section_data.at(section);
  53. }
  54. Gfx::IntRect HeaderView::section_rect(int section) const
  55. {
  56. if (!model())
  57. return {};
  58. auto& data = section_data(section);
  59. if (!data.visibility)
  60. return {};
  61. int offset = 0;
  62. for (int i = 0; i < section; ++i) {
  63. if (!is_section_visible(i))
  64. continue;
  65. offset += section_data(i).size;
  66. if (orientation() == Gfx::Orientation::Horizontal)
  67. offset += m_table_view.horizontal_padding() * 2;
  68. }
  69. if (orientation() == Gfx::Orientation::Horizontal)
  70. return { offset, 0, section_size(section) + m_table_view.horizontal_padding() * 2, height() };
  71. return { 0, offset, width(), section_size(section) };
  72. }
  73. HeaderView::VisibleSectionRange HeaderView::visible_section_range() const
  74. {
  75. auto section_count = this->section_count();
  76. auto is_horizontal = m_orientation == Orientation::Horizontal;
  77. auto rect = m_table_view.visible_content_rect();
  78. auto start = is_horizontal ? rect.top_left().x() : rect.top_left().y();
  79. auto end = is_horizontal ? rect.top_right().x() : rect.bottom_left().y();
  80. auto offset = 0;
  81. VisibleSectionRange range;
  82. for (; range.end < section_count; ++range.end) {
  83. auto& section = section_data(range.end);
  84. int section_size = section.size;
  85. if (orientation() == Gfx::Orientation::Horizontal)
  86. section_size += m_table_view.horizontal_padding() * 2;
  87. if (offset + section_size < start) {
  88. if (section.visibility)
  89. offset += section_size;
  90. ++range.start;
  91. range.start_offset = offset;
  92. continue;
  93. }
  94. if (offset >= end)
  95. break;
  96. if (section.visibility)
  97. offset += section_size;
  98. }
  99. return range;
  100. }
  101. Gfx::IntRect HeaderView::section_resize_grabbable_rect(int section) const
  102. {
  103. if (!model())
  104. return {};
  105. // FIXME: Support resizable rows.
  106. if (m_orientation == Gfx::Orientation::Vertical)
  107. return {};
  108. auto rect = section_rect(section);
  109. return { rect.right() - 1, rect.top(), 4, rect.height() };
  110. }
  111. int HeaderView::section_count() const
  112. {
  113. if (!model())
  114. return 0;
  115. return m_orientation == Gfx::Orientation::Horizontal ? model()->column_count() : model()->row_count();
  116. }
  117. void HeaderView::doubleclick_event(MouseEvent& event)
  118. {
  119. if (!model())
  120. return;
  121. auto range = visible_section_range();
  122. for (int i = range.start; i < range.end; ++i) {
  123. if (section_resize_grabbable_rect(i).contains(event.position())) {
  124. if (on_resize_doubleclick)
  125. on_resize_doubleclick(i);
  126. }
  127. }
  128. }
  129. void HeaderView::mousedown_event(MouseEvent& event)
  130. {
  131. if (event.button() != GUI::MouseButton::Left)
  132. return;
  133. if (!model())
  134. return;
  135. auto& model = *this->model();
  136. auto range = visible_section_range();
  137. for (int i = range.start; i < range.end; ++i) {
  138. if (section_resize_grabbable_rect(i).contains(event.position())) {
  139. m_resizing_section = i;
  140. m_in_section_resize = true;
  141. m_section_resize_original_width = section_size(i);
  142. m_section_resize_origin = event.position();
  143. return;
  144. }
  145. auto rect = this->section_rect(i);
  146. if (rect.contains(event.position()) && model.is_column_sortable(i)) {
  147. m_pressed_section = i;
  148. m_pressed_section_is_pressed = true;
  149. update();
  150. return;
  151. }
  152. }
  153. }
  154. void HeaderView::mousemove_event(MouseEvent& event)
  155. {
  156. if (!model())
  157. return;
  158. if (m_in_section_resize) {
  159. auto delta = event.position() - m_section_resize_origin;
  160. int new_size = m_section_resize_original_width + delta.primary_offset_for_orientation(m_orientation);
  161. if (new_size <= minimum_column_size)
  162. new_size = minimum_column_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::Left) {
  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. if (orientation() == Gfx::Orientation::Horizontal && size < minimum_column_size)
  358. size = minimum_column_size;
  359. auto& data = section_data(section);
  360. if (data.default_size == size)
  361. return;
  362. data.default_size = size;
  363. data.has_initialized_default_size = true;
  364. }
  365. int HeaderView::default_section_size(int section) const
  366. {
  367. return section_data(section).default_size;
  368. }
  369. bool HeaderView::is_default_section_size_initialized(int section) const
  370. {
  371. return section_data(section).has_initialized_default_size;
  372. }
  373. bool HeaderView::is_section_visible(int section) const
  374. {
  375. return section_data(section).visibility;
  376. }
  377. void HeaderView::set_hovered_section(int section)
  378. {
  379. if (m_hovered_section == section)
  380. return;
  381. m_hovered_section = section;
  382. update();
  383. }
  384. Model* HeaderView::model()
  385. {
  386. return m_table_view.model();
  387. }
  388. const Model* HeaderView::model() const
  389. {
  390. return m_table_view.model();
  391. }
  392. }