HeaderView.cpp 15 KB

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