HeaderView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/AbstractTableView.h>
  27. #include <LibGUI/Action.h>
  28. #include <LibGUI/HeaderView.h>
  29. #include <LibGUI/Menu.h>
  30. #include <LibGUI/Model.h>
  31. #include <LibGUI/Painter.h>
  32. #include <LibGUI/Window.h>
  33. #include <LibGfx/Palette.h>
  34. #include <LibGfx/StylePainter.h>
  35. namespace GUI {
  36. static constexpr int minimum_column_size = 2;
  37. HeaderView::HeaderView(AbstractTableView& table_view, Gfx::Orientation orientation)
  38. : m_table_view(table_view)
  39. , m_orientation(orientation)
  40. {
  41. set_font(Gfx::Font::default_bold_font());
  42. if (m_orientation == Gfx::Orientation::Horizontal) {
  43. set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  44. set_preferred_size(0, 16);
  45. } else {
  46. set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  47. set_preferred_size(40, 0);
  48. }
  49. }
  50. HeaderView::~HeaderView()
  51. {
  52. }
  53. void HeaderView::set_section_size(int section, int size)
  54. {
  55. auto& data = section_data(section);
  56. if (data.size == size)
  57. return;
  58. data.size = size;
  59. data.has_initialized_size = true;
  60. data.size = size;
  61. m_table_view.header_did_change_section_size({}, m_orientation, section, size);
  62. }
  63. int HeaderView::section_size(int section) const
  64. {
  65. return section_data(section).size;
  66. }
  67. HeaderView::SectionData& HeaderView::section_data(int section) const
  68. {
  69. if (static_cast<size_t>(section) >= m_section_data.size())
  70. m_section_data.resize(section + 1);
  71. return m_section_data.at(section);
  72. }
  73. Gfx::IntRect HeaderView::section_rect(int section) const
  74. {
  75. if (!model())
  76. return {};
  77. auto& data = section_data(section);
  78. if (!data.visibility)
  79. return {};
  80. int offset = 0;
  81. for (int i = 0; i < section; ++i) {
  82. if (!is_section_visible(i))
  83. continue;
  84. offset += section_data(i).size + horizontal_padding() * 2;
  85. }
  86. if (orientation() == Gfx::Orientation::Horizontal)
  87. return { offset, 0, section_size(section) + horizontal_padding() * 2, height() };
  88. return { 0, offset, width(), section_size(section) };
  89. }
  90. Gfx::IntRect HeaderView::section_resize_grabbable_rect(int section) const
  91. {
  92. if (!model())
  93. return {};
  94. auto rect = section_rect(section);
  95. return { rect.right() - 1, rect.top(), 4, rect.height() };
  96. }
  97. int HeaderView::section_count() const
  98. {
  99. if (!model())
  100. return 0;
  101. return m_orientation == Gfx::Orientation::Horizontal ? model()->column_count() : model()->row_count();
  102. }
  103. void HeaderView::mousedown_event(MouseEvent& event)
  104. {
  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. ASSERT(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. window()->set_override_cursor(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. window()->set_override_cursor(StandardCursor::None);
  172. }
  173. void HeaderView::mouseup_event(MouseEvent& event)
  174. {
  175. Gfx::IntPoint horizontally_adjusted_position(event.x(), event.y());
  176. if (event.button() == MouseButton::Left) {
  177. if (m_in_section_resize) {
  178. if (!section_resize_grabbable_rect(m_resizing_section).contains(horizontally_adjusted_position))
  179. window()->set_override_cursor(StandardCursor::None);
  180. m_in_section_resize = false;
  181. return;
  182. }
  183. if (m_pressed_section != -1) {
  184. auto header_rect = this->section_rect(m_pressed_section);
  185. if (header_rect.contains(horizontally_adjusted_position)) {
  186. auto new_sort_order = SortOrder::Ascending;
  187. if (m_table_view.key_column() == m_pressed_section)
  188. new_sort_order = m_table_view.sort_order() == SortOrder::Ascending
  189. ? SortOrder::Descending
  190. : SortOrder::Ascending;
  191. m_table_view.set_key_column_and_sort_order(m_pressed_section, new_sort_order);
  192. }
  193. m_pressed_section = -1;
  194. m_pressed_section_is_pressed = false;
  195. update();
  196. return;
  197. }
  198. }
  199. }
  200. void HeaderView::paint_event(PaintEvent& event)
  201. {
  202. Painter painter(*this);
  203. painter.add_clip_rect(event.rect());
  204. painter.fill_rect(rect(), palette().button());
  205. painter.draw_line({ 0, 0 }, { rect().right(), 0 }, palette().threed_highlight());
  206. painter.draw_line({ 0, rect().bottom() }, { rect().right(), rect().bottom() }, palette().threed_shadow1());
  207. int x_offset = 0;
  208. int section_count = this->section_count();
  209. for (int section = 0; section < section_count; ++section) {
  210. if (!is_section_visible(section))
  211. continue;
  212. int section_width = section_size(section);
  213. bool is_key_column = m_table_view.key_column() == section;
  214. Gfx::IntRect cell_rect(x_offset, 0, section_width + horizontal_padding() * 2, height());
  215. bool pressed = section == m_pressed_section && m_pressed_section_is_pressed;
  216. bool hovered = section == m_hovered_section && model()->is_column_sortable(section);
  217. Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, pressed, hovered);
  218. String text;
  219. if (is_key_column) {
  220. StringBuilder builder;
  221. builder.append(model()->column_name(section));
  222. if (m_table_view.sort_order() == SortOrder::Ascending)
  223. builder.append(" \xE2\xAC\x86"); // UPWARDS BLACK ARROW
  224. else if (m_table_view.sort_order() == SortOrder::Descending)
  225. builder.append(" \xE2\xAC\x87"); // DOWNWARDS BLACK ARROW
  226. text = builder.to_string();
  227. } else {
  228. text = model()->column_name(section);
  229. }
  230. auto text_rect = cell_rect.shrunken(horizontal_padding() * 2, 0);
  231. if (pressed)
  232. text_rect.move_by(1, 1);
  233. painter.draw_text(text_rect, text, font(), section_alignment(section), palette().button_text());
  234. x_offset += section_width + horizontal_padding() * 2;
  235. }
  236. }
  237. void HeaderView::set_section_visible(int section, bool visible)
  238. {
  239. auto& data = section_data(section);
  240. if (data.visibility == visible)
  241. return;
  242. data.visibility = visible;
  243. if (data.visibility_action) {
  244. data.visibility_action->set_checked(visible);
  245. }
  246. m_table_view.header_did_change_section_visibility({}, m_orientation, section, visible);
  247. update();
  248. }
  249. Menu& HeaderView::ensure_context_menu()
  250. {
  251. // FIXME: This menu needs to be rebuilt if the model is swapped out,
  252. // or if the column count/names change.
  253. if (!m_context_menu) {
  254. ASSERT(model());
  255. m_context_menu = Menu::construct();
  256. int section_count = this->section_count();
  257. for (int section = 0; section < section_count; ++section) {
  258. auto& column_data = this->section_data(section);
  259. // FIXME: Vertical support
  260. ASSERT(m_orientation == Gfx::Orientation::Horizontal);
  261. auto name = model()->column_name(section);
  262. column_data.visibility_action = Action::create_checkable(name, [this, section](auto& action) {
  263. set_section_visible(section, action.is_checked());
  264. });
  265. column_data.visibility_action->set_checked(column_data.visibility);
  266. m_context_menu->add_action(*column_data.visibility_action);
  267. }
  268. }
  269. return *m_context_menu;
  270. }
  271. void HeaderView::context_menu_event(ContextMenuEvent& event)
  272. {
  273. ensure_context_menu().popup(event.screen_position());
  274. }
  275. void HeaderView::leave_event(Core::Event& event)
  276. {
  277. Widget::leave_event(event);
  278. window()->set_override_cursor(StandardCursor::None);
  279. }
  280. Gfx::TextAlignment HeaderView::section_alignment(int section) const
  281. {
  282. return section_data(section).alignment;
  283. }
  284. void HeaderView::set_section_alignment(int section, Gfx::TextAlignment alignment)
  285. {
  286. section_data(section).alignment = alignment;
  287. }
  288. bool HeaderView::is_section_visible(int section) const
  289. {
  290. return section_data(section).visibility;
  291. }
  292. void HeaderView::set_hovered_section(int section)
  293. {
  294. if (m_hovered_section == section)
  295. return;
  296. m_hovered_section = section;
  297. update();
  298. }
  299. Model* HeaderView::model()
  300. {
  301. return m_table_view.model();
  302. }
  303. const Model* HeaderView::model() const
  304. {
  305. return m_table_view.model();
  306. }
  307. }