HeaderView.cpp 14 KB

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