GAbstractColumnView.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * Copyright (c) 2018-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 <AK/StringBuilder.h>
  27. #include <LibDraw/Palette.h>
  28. #include <LibGUI/GAbstractColumnView.h>
  29. #include <LibGUI/GAction.h>
  30. #include <LibGUI/GMenu.h>
  31. #include <LibGUI/GPainter.h>
  32. #include <LibGUI/GScrollBar.h>
  33. static const int minimum_column_width = 2;
  34. GAbstractColumnView::GAbstractColumnView(GWidget* parent)
  35. : GAbstractView(parent)
  36. {
  37. set_frame_shape(FrameShape::Container);
  38. set_frame_shadow(FrameShadow::Sunken);
  39. set_frame_thickness(2);
  40. set_should_hide_unnecessary_scrollbars(true);
  41. }
  42. GAbstractColumnView::~GAbstractColumnView()
  43. {
  44. }
  45. void GAbstractColumnView::update_column_sizes()
  46. {
  47. if (!m_size_columns_to_fit_content)
  48. return;
  49. if (!model())
  50. return;
  51. auto& model = *this->model();
  52. int column_count = model.column_count();
  53. int row_count = model.row_count();
  54. int key_column = model.key_column();
  55. for (int column = 0; column < column_count; ++column) {
  56. if (is_column_hidden(column))
  57. continue;
  58. int header_width = header_font().width(model.column_name(column));
  59. if (column == key_column)
  60. header_width += font().width(" \xc3\xb6");
  61. int column_width = header_width;
  62. for (int row = 0; row < row_count; ++row) {
  63. auto cell_data = model.data(model.index(row, column));
  64. int cell_width = 0;
  65. if (cell_data.is_bitmap()) {
  66. cell_width = cell_data.as_bitmap().width();
  67. } else {
  68. cell_width = font().width(cell_data.to_string());
  69. }
  70. column_width = max(column_width, cell_width);
  71. }
  72. auto& column_data = this->column_data(column);
  73. column_data.width = max(column_data.width, column_width);
  74. column_data.has_initialized_width = true;
  75. }
  76. }
  77. void GAbstractColumnView::update_content_size()
  78. {
  79. if (!model())
  80. return set_content_size({});
  81. int content_width = 0;
  82. int column_count = model()->column_count();
  83. for (int i = 0; i < column_count; ++i) {
  84. if (!is_column_hidden(i))
  85. content_width += column_width(i) + horizontal_padding() * 2;
  86. }
  87. int content_height = item_count() * item_height();
  88. set_content_size({ content_width, content_height });
  89. set_size_occupied_by_fixed_elements({ 0, header_height() });
  90. }
  91. Rect GAbstractColumnView::header_rect(int column_index) const
  92. {
  93. if (!model())
  94. return {};
  95. if (is_column_hidden(column_index))
  96. return {};
  97. int x_offset = 0;
  98. for (int i = 0; i < column_index; ++i) {
  99. if (is_column_hidden(i))
  100. continue;
  101. x_offset += column_width(i) + horizontal_padding() * 2;
  102. }
  103. return { x_offset, 0, column_width(column_index) + horizontal_padding() * 2, header_height() };
  104. }
  105. void GAbstractColumnView::set_hovered_header_index(int index)
  106. {
  107. if (m_hovered_column_header_index == index)
  108. return;
  109. m_hovered_column_header_index = index;
  110. update_headers();
  111. }
  112. void GAbstractColumnView::paint_headers(GPainter& painter)
  113. {
  114. if (!headers_visible())
  115. return;
  116. int exposed_width = max(content_size().width(), width());
  117. painter.fill_rect({ 0, 0, exposed_width, header_height() }, palette().button());
  118. painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, palette().threed_highlight());
  119. painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, palette().threed_shadow1());
  120. int x_offset = 0;
  121. int column_count = model()->column_count();
  122. for (int column_index = 0; column_index < column_count; ++column_index) {
  123. if (is_column_hidden(column_index))
  124. continue;
  125. int column_width = this->column_width(column_index);
  126. bool is_key_column = model()->key_column() == column_index;
  127. Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, header_height());
  128. bool pressed = column_index == m_pressed_column_header_index && m_pressed_column_header_is_pressed;
  129. bool hovered = column_index == m_hovered_column_header_index && model()->column_metadata(column_index).sortable == GModel::ColumnMetadata::Sortable::True;
  130. StylePainter::paint_button(painter, cell_rect, palette(), ButtonStyle::Normal, pressed, hovered);
  131. String text;
  132. if (is_key_column) {
  133. StringBuilder builder;
  134. builder.append(model()->column_name(column_index));
  135. auto sort_order = model()->sort_order();
  136. if (sort_order == GSortOrder::Ascending)
  137. builder.append(" \xc3\xb6");
  138. else if (sort_order == GSortOrder::Descending)
  139. builder.append(" \xc3\xb7");
  140. text = builder.to_string();
  141. } else {
  142. text = model()->column_name(column_index);
  143. }
  144. auto text_rect = cell_rect.translated(horizontal_padding(), 0);
  145. if (pressed)
  146. text_rect.move_by(1, 1);
  147. painter.draw_text(text_rect, text, header_font(), TextAlignment::CenterLeft, palette().button_text());
  148. x_offset += column_width + horizontal_padding() * 2;
  149. }
  150. }
  151. bool GAbstractColumnView::is_column_hidden(int column) const
  152. {
  153. return !column_data(column).visibility;
  154. }
  155. void GAbstractColumnView::set_column_hidden(int column, bool hidden)
  156. {
  157. auto& column_data = this->column_data(column);
  158. if (column_data.visibility == !hidden)
  159. return;
  160. column_data.visibility = !hidden;
  161. update_content_size();
  162. update();
  163. }
  164. GMenu& GAbstractColumnView::ensure_header_context_menu()
  165. {
  166. // FIXME: This menu needs to be rebuilt if the model is swapped out,
  167. // or if the column count/names change.
  168. if (!m_header_context_menu) {
  169. ASSERT(model());
  170. m_header_context_menu = GMenu::construct();
  171. for (int column = 0; column < model()->column_count(); ++column) {
  172. auto& column_data = this->column_data(column);
  173. auto name = model()->column_name(column);
  174. column_data.visibility_action = GAction::create(name, [this, column](GAction& action) {
  175. action.set_checked(!action.is_checked());
  176. set_column_hidden(column, !action.is_checked());
  177. });
  178. column_data.visibility_action->set_checkable(true);
  179. column_data.visibility_action->set_checked(true);
  180. m_header_context_menu->add_action(*column_data.visibility_action);
  181. }
  182. }
  183. return *m_header_context_menu;
  184. }
  185. const Font& GAbstractColumnView::header_font()
  186. {
  187. return Font::default_bold_font();
  188. }
  189. void GAbstractColumnView::set_cell_painting_delegate(int column, OwnPtr<GTableCellPaintingDelegate>&& delegate)
  190. {
  191. column_data(column).cell_painting_delegate = move(delegate);
  192. }
  193. void GAbstractColumnView::update_headers()
  194. {
  195. Rect rect { 0, 0, frame_inner_rect().width(), header_height() };
  196. rect.move_by(frame_thickness(), frame_thickness());
  197. update(rect);
  198. }
  199. GAbstractColumnView::ColumnData& GAbstractColumnView::column_data(int column) const
  200. {
  201. if (column >= m_column_data.size())
  202. m_column_data.resize(column + 1);
  203. return m_column_data.at(column);
  204. }
  205. Rect GAbstractColumnView::column_resize_grabbable_rect(int column) const
  206. {
  207. if (!model())
  208. return {};
  209. auto header_rect = this->header_rect(column);
  210. return { header_rect.right() - 1, header_rect.top(), 4, header_rect.height() };
  211. }
  212. int GAbstractColumnView::column_width(int column_index) const
  213. {
  214. if (!model())
  215. return 0;
  216. auto& column_data = this->column_data(column_index);
  217. if (!column_data.has_initialized_width) {
  218. ASSERT(!m_size_columns_to_fit_content);
  219. column_data.has_initialized_width = true;
  220. column_data.width = model()->column_metadata(column_index).preferred_width;
  221. }
  222. return column_data.width;
  223. }
  224. void GAbstractColumnView::mousemove_event(GMouseEvent& event)
  225. {
  226. if (!model())
  227. return GAbstractView::mousemove_event(event);
  228. if (m_in_column_resize) {
  229. auto delta = event.position() - m_column_resize_origin;
  230. int new_width = m_column_resize_original_width + delta.x();
  231. if (new_width <= minimum_column_width)
  232. new_width = minimum_column_width;
  233. ASSERT(m_resizing_column >= 0 && m_resizing_column < model()->column_count());
  234. auto& column_data = this->column_data(m_resizing_column);
  235. if (column_data.width != new_width) {
  236. column_data.width = new_width;
  237. dbg() << "New column width: " << new_width;
  238. update_content_size();
  239. update();
  240. }
  241. return;
  242. }
  243. if (m_pressed_column_header_index != -1) {
  244. auto header_rect = this->header_rect(m_pressed_column_header_index);
  245. if (header_rect.contains(event.position())) {
  246. if (!m_pressed_column_header_is_pressed)
  247. update_headers();
  248. m_pressed_column_header_is_pressed = true;
  249. } else {
  250. if (m_pressed_column_header_is_pressed)
  251. update_headers();
  252. m_pressed_column_header_is_pressed = false;
  253. }
  254. return;
  255. }
  256. if (event.buttons() == 0) {
  257. int column_count = model()->column_count();
  258. bool found_hovered_header = false;
  259. for (int i = 0; i < column_count; ++i) {
  260. if (column_resize_grabbable_rect(i).contains(event.position())) {
  261. window()->set_override_cursor(GStandardCursor::ResizeHorizontal);
  262. set_hovered_header_index(-1);
  263. return;
  264. }
  265. if (header_rect(i).contains(event.position())) {
  266. set_hovered_header_index(i);
  267. found_hovered_header = true;
  268. }
  269. }
  270. if (!found_hovered_header)
  271. set_hovered_header_index(-1);
  272. }
  273. window()->set_override_cursor(GStandardCursor::None);
  274. GAbstractView::mousemove_event(event);
  275. }
  276. void GAbstractColumnView::mouseup_event(GMouseEvent& event)
  277. {
  278. auto adjusted_position = this->adjusted_position(event.position());
  279. if (event.button() == GMouseButton::Left) {
  280. if (m_in_column_resize) {
  281. if (!column_resize_grabbable_rect(m_resizing_column).contains(adjusted_position))
  282. window()->set_override_cursor(GStandardCursor::None);
  283. m_in_column_resize = false;
  284. return;
  285. }
  286. if (m_pressed_column_header_index != -1) {
  287. auto header_rect = this->header_rect(m_pressed_column_header_index);
  288. if (header_rect.contains(event.position())) {
  289. auto new_sort_order = GSortOrder::Ascending;
  290. if (model()->key_column() == m_pressed_column_header_index)
  291. new_sort_order = model()->sort_order() == GSortOrder::Ascending
  292. ? GSortOrder::Descending
  293. : GSortOrder::Ascending;
  294. model()->set_key_column_and_sort_order(m_pressed_column_header_index, new_sort_order);
  295. }
  296. m_pressed_column_header_index = -1;
  297. m_pressed_column_header_is_pressed = false;
  298. update_headers();
  299. return;
  300. }
  301. }
  302. GAbstractView::mouseup_event(event);
  303. }
  304. void GAbstractColumnView::mousedown_event(GMouseEvent& event)
  305. {
  306. if (!model())
  307. return GAbstractView::mousedown_event(event);
  308. if (event.button() != GMouseButton::Left)
  309. return GAbstractView::mousedown_event(event);
  310. if (event.y() < header_height()) {
  311. int column_count = model()->column_count();
  312. for (int i = 0; i < column_count; ++i) {
  313. if (column_resize_grabbable_rect(i).contains(event.position())) {
  314. m_resizing_column = i;
  315. m_in_column_resize = true;
  316. m_column_resize_original_width = column_width(i);
  317. m_column_resize_origin = event.position();
  318. return;
  319. }
  320. auto header_rect = this->header_rect(i);
  321. auto column_metadata = model()->column_metadata(i);
  322. if (header_rect.contains(event.position()) && column_metadata.sortable == GModel::ColumnMetadata::Sortable::True) {
  323. m_pressed_column_header_index = i;
  324. m_pressed_column_header_is_pressed = true;
  325. update_headers();
  326. return;
  327. }
  328. }
  329. return;
  330. }
  331. bool is_toggle;
  332. auto index = index_at_event_position(event.position(), is_toggle);
  333. if (index.is_valid() && is_toggle && model()->row_count(index)) {
  334. toggle_index(index);
  335. return;
  336. }
  337. GAbstractView::mousedown_event(event);
  338. }
  339. GModelIndex GAbstractColumnView::index_at_event_position(const Point& position, bool& is_toggle) const
  340. {
  341. is_toggle = false;
  342. if (!model())
  343. return {};
  344. auto adjusted_position = this->adjusted_position(position);
  345. for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
  346. if (!row_rect(row).contains(adjusted_position))
  347. continue;
  348. for (int column = 0, column_count = model()->column_count(); column < column_count; ++column) {
  349. if (!content_rect(row, column).contains(adjusted_position))
  350. continue;
  351. return model()->index(row, column);
  352. }
  353. return model()->index(row, 0);
  354. }
  355. return {};
  356. }
  357. GModelIndex GAbstractColumnView::index_at_event_position(const Point& position) const
  358. {
  359. bool is_toggle;
  360. auto index = index_at_event_position(position, is_toggle);
  361. return is_toggle ? GModelIndex() : index;
  362. }
  363. int GAbstractColumnView::item_count() const
  364. {
  365. if (!model())
  366. return 0;
  367. return model()->row_count();
  368. }
  369. void GAbstractColumnView::keydown_event(GKeyEvent& event)
  370. {
  371. if (!model())
  372. return;
  373. auto& model = *this->model();
  374. if (event.key() == KeyCode::Key_Return) {
  375. activate_selected();
  376. return;
  377. }
  378. if (event.key() == KeyCode::Key_Up) {
  379. GModelIndex new_index;
  380. if (!selection().is_empty()) {
  381. auto old_index = selection().first();
  382. new_index = model.index(old_index.row() - 1, old_index.column());
  383. } else {
  384. new_index = model.index(0, 0);
  385. }
  386. if (model.is_valid(new_index)) {
  387. selection().set(new_index);
  388. scroll_into_view(new_index, Orientation::Vertical);
  389. update();
  390. }
  391. return;
  392. }
  393. if (event.key() == KeyCode::Key_Down) {
  394. GModelIndex new_index;
  395. if (!selection().is_empty()) {
  396. auto old_index = selection().first();
  397. new_index = model.index(old_index.row() + 1, old_index.column());
  398. } else {
  399. new_index = model.index(0, 0);
  400. }
  401. if (model.is_valid(new_index)) {
  402. selection().set(new_index);
  403. scroll_into_view(new_index, Orientation::Vertical);
  404. update();
  405. }
  406. return;
  407. }
  408. if (event.key() == KeyCode::Key_PageUp) {
  409. int items_per_page = visible_content_rect().height() / item_height();
  410. auto old_index = selection().first();
  411. auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
  412. if (model.is_valid(new_index)) {
  413. selection().set(new_index);
  414. scroll_into_view(new_index, Orientation::Vertical);
  415. update();
  416. }
  417. return;
  418. }
  419. if (event.key() == KeyCode::Key_PageDown) {
  420. int items_per_page = visible_content_rect().height() / item_height();
  421. auto old_index = selection().first();
  422. auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
  423. if (model.is_valid(new_index)) {
  424. selection().set(new_index);
  425. scroll_into_view(new_index, Orientation::Vertical);
  426. update();
  427. }
  428. return;
  429. }
  430. return GWidget::keydown_event(event);
  431. }
  432. void GAbstractColumnView::scroll_into_view(const GModelIndex& index, Orientation orientation)
  433. {
  434. auto rect = row_rect(index.row()).translated(0, -header_height());
  435. GScrollableWidget::scroll_into_view(rect, orientation);
  436. }
  437. void GAbstractColumnView::doubleclick_event(GMouseEvent& event)
  438. {
  439. if (!model())
  440. return;
  441. if (event.button() == GMouseButton::Left) {
  442. if (event.y() < header_height())
  443. return;
  444. if (!selection().is_empty()) {
  445. if (is_editable())
  446. begin_editing(selection().first());
  447. else
  448. activate_selected();
  449. }
  450. }
  451. }
  452. void GAbstractColumnView::context_menu_event(GContextMenuEvent& event)
  453. {
  454. if (!model())
  455. return;
  456. if (event.position().y() < header_height()) {
  457. ensure_header_context_menu().popup(event.screen_position());
  458. return;
  459. }
  460. bool is_toggle;
  461. auto index = index_at_event_position(event.position(), is_toggle);
  462. if (index.is_valid()) {
  463. if (!selection().contains(index))
  464. selection().set(index);
  465. } else {
  466. selection().clear();
  467. }
  468. if (on_context_menu_request)
  469. on_context_menu_request(index, event);
  470. }
  471. void GAbstractColumnView::leave_event(CEvent&)
  472. {
  473. window()->set_override_cursor(GStandardCursor::None);
  474. set_hovered_header_index(-1);
  475. }
  476. Rect GAbstractColumnView::content_rect(int row, int column) const
  477. {
  478. auto row_rect = this->row_rect(row);
  479. int x = 0;
  480. for (int i = 0; i < column; ++i)
  481. x += column_width(i) + horizontal_padding() * 2;
  482. return { row_rect.x() + x, row_rect.y(), column_width(column) + horizontal_padding() * 2, item_height() };
  483. }
  484. Rect GAbstractColumnView::content_rect(const GModelIndex& index) const
  485. {
  486. return content_rect(index.row(), index.column());
  487. }
  488. Rect GAbstractColumnView::row_rect(int item_index) const
  489. {
  490. return { 0, header_height() + (item_index * item_height()), max(content_size().width(), width()), item_height() };
  491. }
  492. Point GAbstractColumnView::adjusted_position(const Point& position) const
  493. {
  494. return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  495. }
  496. void GAbstractColumnView::did_update_model()
  497. {
  498. GAbstractView::did_update_model();
  499. update_column_sizes();
  500. update_content_size();
  501. update();
  502. }