HeaderView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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_bold_font());
  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. Gfx::IntRect HeaderView::section_resize_grabbable_rect(int section) const
  74. {
  75. if (!model())
  76. return {};
  77. // FIXME: Support resizable rows.
  78. if (m_orientation == Gfx::Orientation::Vertical)
  79. return {};
  80. auto rect = section_rect(section);
  81. return { rect.right() - 1, rect.top(), 4, rect.height() };
  82. }
  83. int HeaderView::section_count() const
  84. {
  85. if (!model())
  86. return 0;
  87. return m_orientation == Gfx::Orientation::Horizontal ? model()->column_count() : model()->row_count();
  88. }
  89. void HeaderView::doubleclick_event(MouseEvent& event)
  90. {
  91. if (!model())
  92. return;
  93. int section_count = this->section_count();
  94. for (int i = 0; i < section_count; ++i) {
  95. if (section_resize_grabbable_rect(i).contains(event.position())) {
  96. if (on_resize_doubleclick)
  97. on_resize_doubleclick(i);
  98. }
  99. }
  100. }
  101. void HeaderView::mousedown_event(MouseEvent& event)
  102. {
  103. if (event.button() != GUI::MouseButton::Left)
  104. return;
  105. if (!model())
  106. return;
  107. auto& model = *this->model();
  108. int section_count = this->section_count();
  109. for (int i = 0; i < section_count; ++i) {
  110. if (section_resize_grabbable_rect(i).contains(event.position())) {
  111. m_resizing_section = i;
  112. m_in_section_resize = true;
  113. m_section_resize_original_width = section_size(i);
  114. m_section_resize_origin = event.position();
  115. return;
  116. }
  117. auto rect = this->section_rect(i);
  118. if (rect.contains(event.position()) && model.is_column_sortable(i)) {
  119. m_pressed_section = i;
  120. m_pressed_section_is_pressed = true;
  121. update();
  122. return;
  123. }
  124. }
  125. }
  126. void HeaderView::mousemove_event(MouseEvent& event)
  127. {
  128. if (!model())
  129. return;
  130. if (m_in_section_resize) {
  131. auto delta = event.position() - m_section_resize_origin;
  132. int new_size = m_section_resize_original_width + delta.primary_offset_for_orientation(m_orientation);
  133. if (new_size <= minimum_column_size)
  134. new_size = minimum_column_size;
  135. VERIFY(m_resizing_section >= 0 && m_resizing_section < model()->column_count());
  136. set_section_size(m_resizing_section, new_size);
  137. return;
  138. }
  139. if (m_pressed_section != -1) {
  140. auto header_rect = this->section_rect(m_pressed_section);
  141. if (header_rect.contains(event.position())) {
  142. set_hovered_section(m_pressed_section);
  143. if (!m_pressed_section_is_pressed)
  144. update();
  145. m_pressed_section_is_pressed = true;
  146. } else {
  147. set_hovered_section(-1);
  148. if (m_pressed_section_is_pressed)
  149. update();
  150. m_pressed_section_is_pressed = false;
  151. }
  152. return;
  153. }
  154. if (event.buttons() == 0) {
  155. int section_count = this->section_count();
  156. bool found_hovered_header = false;
  157. for (int i = 0; i < section_count; ++i) {
  158. if (section_resize_grabbable_rect(i).contains(event.position())) {
  159. set_override_cursor(Gfx::StandardCursor::ResizeColumn);
  160. set_hovered_section(-1);
  161. return;
  162. }
  163. if (section_rect(i).contains(event.position())) {
  164. set_hovered_section(i);
  165. found_hovered_header = true;
  166. }
  167. }
  168. if (!found_hovered_header)
  169. set_hovered_section(-1);
  170. }
  171. set_override_cursor(Gfx::StandardCursor::None);
  172. }
  173. void HeaderView::mouseup_event(MouseEvent& event)
  174. {
  175. if (event.button() == MouseButton::Left) {
  176. if (m_in_section_resize) {
  177. if (!section_resize_grabbable_rect(m_resizing_section).contains(event.position()))
  178. set_override_cursor(Gfx::StandardCursor::None);
  179. m_in_section_resize = false;
  180. return;
  181. }
  182. if (m_pressed_section != -1) {
  183. if (m_orientation == Gfx::Orientation::Horizontal && section_rect(m_pressed_section).contains(event.position())) {
  184. auto new_sort_order = m_table_view.sort_order();
  185. if (m_table_view.key_column() == m_pressed_section)
  186. new_sort_order = m_table_view.sort_order() == SortOrder::Ascending
  187. ? SortOrder::Descending
  188. : SortOrder::Ascending;
  189. m_table_view.set_key_column_and_sort_order(m_pressed_section, new_sort_order);
  190. }
  191. m_pressed_section = -1;
  192. m_pressed_section_is_pressed = false;
  193. update();
  194. return;
  195. }
  196. }
  197. }
  198. void HeaderView::paint_horizontal(Painter& painter)
  199. {
  200. painter.draw_line({ 0, 0 }, { rect().right(), 0 }, palette().threed_highlight());
  201. painter.draw_line({ 0, rect().bottom() }, { rect().right(), rect().bottom() }, palette().threed_shadow1());
  202. int x_offset = 0;
  203. int section_count = this->section_count();
  204. for (int section = 0; section < section_count; ++section) {
  205. if (!is_section_visible(section))
  206. continue;
  207. int section_width = section_size(section);
  208. bool is_key_column = m_table_view.key_column() == section;
  209. Gfx::IntRect cell_rect(x_offset, 0, section_width + m_table_view.horizontal_padding() * 2, height());
  210. bool pressed = section == m_pressed_section && m_pressed_section_is_pressed;
  211. bool hovered = section == m_hovered_section && model()->is_column_sortable(section);
  212. Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, pressed, hovered);
  213. String text;
  214. if (is_key_column) {
  215. StringBuilder builder;
  216. builder.append(model()->column_name(section));
  217. if (m_table_view.sort_order() == SortOrder::Ascending)
  218. builder.append(" \xE2\xAC\x86"); // UPWARDS BLACK ARROW
  219. else if (m_table_view.sort_order() == SortOrder::Descending)
  220. builder.append(" \xE2\xAC\x87"); // DOWNWARDS BLACK ARROW
  221. text = builder.to_string();
  222. } else {
  223. text = model()->column_name(section);
  224. }
  225. auto text_rect = cell_rect.shrunken(m_table_view.horizontal_padding() * 2, 0);
  226. if (pressed)
  227. text_rect.translate_by(1, 1);
  228. painter.draw_text(text_rect, text, font(), section_alignment(section), palette().button_text());
  229. x_offset += section_width + m_table_view.horizontal_padding() * 2;
  230. }
  231. if (x_offset < rect().right()) {
  232. Gfx::IntRect cell_rect(x_offset, 0, width() - x_offset, height());
  233. Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, false, false);
  234. }
  235. }
  236. void HeaderView::paint_vertical(Painter& painter)
  237. {
  238. painter.draw_line(rect().top_left(), rect().bottom_left(), palette().threed_highlight());
  239. painter.draw_line(rect().top_right(), rect().bottom_right(), palette().threed_shadow1());
  240. int y_offset = 0;
  241. int section_count = this->section_count();
  242. for (int section = 0; section < section_count; ++section) {
  243. if (!is_section_visible(section))
  244. continue;
  245. int section_size = this->section_size(section);
  246. Gfx::IntRect cell_rect(0, y_offset, width(), section_size);
  247. bool pressed = section == m_pressed_section && m_pressed_section_is_pressed;
  248. bool hovered = false;
  249. Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, pressed, hovered);
  250. String text = String::number(section);
  251. auto text_rect = cell_rect.shrunken(m_table_view.horizontal_padding() * 2, 0);
  252. if (pressed)
  253. text_rect.translate_by(1, 1);
  254. painter.draw_text(text_rect, text, font(), section_alignment(section), palette().button_text());
  255. y_offset += section_size;
  256. }
  257. if (y_offset < rect().bottom()) {
  258. Gfx::IntRect cell_rect(0, y_offset, width(), height() - y_offset);
  259. Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, false, false);
  260. }
  261. }
  262. void HeaderView::paint_event(PaintEvent& event)
  263. {
  264. Painter painter(*this);
  265. painter.add_clip_rect(event.rect());
  266. painter.fill_rect(rect(), palette().button());
  267. if (orientation() == Gfx::Orientation::Horizontal)
  268. paint_horizontal(painter);
  269. else
  270. paint_vertical(painter);
  271. }
  272. void HeaderView::set_section_visible(int section, bool visible)
  273. {
  274. auto& data = section_data(section);
  275. if (data.visibility == visible)
  276. return;
  277. data.visibility = visible;
  278. if (data.visibility_action) {
  279. data.visibility_action->set_checked(visible);
  280. }
  281. m_table_view.header_did_change_section_visibility({}, m_orientation, section, visible);
  282. update();
  283. }
  284. Menu& HeaderView::ensure_context_menu()
  285. {
  286. // FIXME: This menu needs to be rebuilt if the model is swapped out,
  287. // or if the column count/names change.
  288. if (!m_context_menu) {
  289. VERIFY(model());
  290. m_context_menu = Menu::construct();
  291. if (m_orientation == Gfx::Orientation::Vertical) {
  292. dbgln("FIXME: Support context menus for vertical GUI::HeaderView");
  293. return *m_context_menu;
  294. }
  295. int section_count = this->section_count();
  296. for (int section = 0; section < section_count; ++section) {
  297. auto& column_data = this->section_data(section);
  298. auto name = model()->column_name(section);
  299. column_data.visibility_action = Action::create_checkable(name, [this, section](auto& action) {
  300. set_section_visible(section, action.is_checked());
  301. });
  302. column_data.visibility_action->set_checked(column_data.visibility);
  303. m_context_menu->add_action(*column_data.visibility_action);
  304. }
  305. }
  306. return *m_context_menu;
  307. }
  308. void HeaderView::context_menu_event(ContextMenuEvent& event)
  309. {
  310. ensure_context_menu().popup(event.screen_position());
  311. }
  312. void HeaderView::leave_event(Core::Event& event)
  313. {
  314. Widget::leave_event(event);
  315. set_hovered_section(-1);
  316. }
  317. Gfx::TextAlignment HeaderView::section_alignment(int section) const
  318. {
  319. return section_data(section).alignment;
  320. }
  321. void HeaderView::set_section_alignment(int section, Gfx::TextAlignment alignment)
  322. {
  323. section_data(section).alignment = alignment;
  324. }
  325. void HeaderView::set_default_section_size(int section, int size)
  326. {
  327. if (orientation() == Gfx::Orientation::Horizontal && size < minimum_column_size)
  328. size = minimum_column_size;
  329. auto& data = section_data(section);
  330. if (data.default_size == size)
  331. return;
  332. data.default_size = size;
  333. data.has_initialized_default_size = true;
  334. }
  335. int HeaderView::default_section_size(int section) const
  336. {
  337. return section_data(section).default_size;
  338. }
  339. bool HeaderView::is_default_section_size_initialized(int section) const
  340. {
  341. return section_data(section).has_initialized_default_size;
  342. }
  343. bool HeaderView::is_section_visible(int section) const
  344. {
  345. return section_data(section).visibility;
  346. }
  347. void HeaderView::set_hovered_section(int section)
  348. {
  349. if (m_hovered_section == section)
  350. return;
  351. m_hovered_section = section;
  352. update();
  353. }
  354. Model* HeaderView::model()
  355. {
  356. return m_table_view.model();
  357. }
  358. const Model* HeaderView::model() const
  359. {
  360. return m_table_view.model();
  361. }
  362. }