MultiView.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <LibGUI/Action.h>
  28. #include <LibGUI/ActionGroup.h>
  29. #include <LibGUI/Model.h>
  30. #include <LibGUI/MultiView.h>
  31. #include <LibGUI/Window.h>
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. namespace GUI {
  35. MultiView::MultiView()
  36. {
  37. set_active_widget(nullptr);
  38. set_content_margins({ 2, 2, 2, 2 });
  39. m_icon_view = add<IconView>();
  40. m_table_view = add<TableView>();
  41. m_columns_view = add<ColumnsView>();
  42. m_icon_view->on_activation = [&](auto& index) {
  43. if (on_activation)
  44. on_activation(index);
  45. };
  46. m_columns_view->on_activation = [&](auto& index) {
  47. if (on_activation)
  48. on_activation(index);
  49. };
  50. m_table_view->on_activation = [&](auto& index) {
  51. if (on_activation)
  52. on_activation(index);
  53. };
  54. m_table_view->on_selection_change = [this] {
  55. if (on_selection_change)
  56. on_selection_change();
  57. };
  58. m_icon_view->on_selection_change = [this] {
  59. if (on_selection_change)
  60. on_selection_change();
  61. };
  62. m_columns_view->on_selection_change = [this] {
  63. if (on_selection_change)
  64. on_selection_change();
  65. };
  66. m_table_view->on_context_menu_request = [this](auto& index, auto& event) {
  67. if (on_context_menu_request)
  68. on_context_menu_request(index, event);
  69. };
  70. m_icon_view->on_context_menu_request = [this](auto& index, auto& event) {
  71. if (on_context_menu_request)
  72. on_context_menu_request(index, event);
  73. };
  74. m_columns_view->on_context_menu_request = [this](auto& index, auto& event) {
  75. if (on_context_menu_request)
  76. on_context_menu_request(index, event);
  77. };
  78. m_table_view->on_drop = [this](auto& index, auto& event) {
  79. if (on_drop)
  80. on_drop(index, event);
  81. };
  82. m_icon_view->on_drop = [this](auto& index, auto& event) {
  83. if (on_drop)
  84. on_drop(index, event);
  85. };
  86. m_columns_view->on_drop = [this](auto& index, auto& event) {
  87. if (on_drop)
  88. on_drop(index, event);
  89. };
  90. build_actions();
  91. set_view_mode(ViewMode::Icon);
  92. }
  93. MultiView::~MultiView()
  94. {
  95. }
  96. void MultiView::set_view_mode(ViewMode mode)
  97. {
  98. if (m_view_mode == mode)
  99. return;
  100. m_view_mode = mode;
  101. update();
  102. if (mode == ViewMode::Table) {
  103. set_active_widget(m_table_view);
  104. m_view_as_table_action->set_checked(true);
  105. return;
  106. }
  107. if (mode == ViewMode::Columns) {
  108. set_active_widget(m_columns_view);
  109. m_view_as_columns_action->set_checked(true);
  110. return;
  111. }
  112. if (mode == ViewMode::Icon) {
  113. set_active_widget(m_icon_view);
  114. m_view_as_icons_action->set_checked(true);
  115. return;
  116. }
  117. ASSERT_NOT_REACHED();
  118. }
  119. void MultiView::set_model(RefPtr<Model> model)
  120. {
  121. if (m_model == model)
  122. return;
  123. m_model = model;
  124. for_each_view_implementation([&](auto& view) {
  125. view.set_model(model);
  126. });
  127. }
  128. void MultiView::set_model_column(int column)
  129. {
  130. if (m_model_column == column)
  131. return;
  132. m_model_column = column;
  133. m_icon_view->set_model_column(column);
  134. m_columns_view->set_model_column(column);
  135. }
  136. void MultiView::set_column_hidden(int column_index, bool hidden)
  137. {
  138. m_table_view->set_column_hidden(column_index, hidden);
  139. }
  140. void MultiView::build_actions()
  141. {
  142. m_view_as_table_action = Action::create_checkable(
  143. "Table view", Gfx::Bitmap::load_from_file("/res/icons/16x16/table-view.png"), [this](auto&) {
  144. set_view_mode(ViewMode::Table);
  145. });
  146. m_view_as_icons_action = Action::create_checkable(
  147. "Icon view", Gfx::Bitmap::load_from_file("/res/icons/16x16/icon-view.png"), [this](auto&) {
  148. set_view_mode(ViewMode::Icon);
  149. });
  150. m_view_as_columns_action = Action::create_checkable(
  151. "Columns view", Gfx::Bitmap::load_from_file("/res/icons/16x16/columns-view.png"), [this](auto&) {
  152. set_view_mode(ViewMode::Columns);
  153. });
  154. m_view_type_action_group = make<ActionGroup>();
  155. m_view_type_action_group->set_exclusive(true);
  156. m_view_type_action_group->add_action(*m_view_as_table_action);
  157. m_view_type_action_group->add_action(*m_view_as_icons_action);
  158. m_view_type_action_group->add_action(*m_view_as_columns_action);
  159. }
  160. AbstractView::SelectionMode MultiView::selection_mode() const
  161. {
  162. return m_table_view->selection_mode();
  163. }
  164. void MultiView::set_selection_mode(AbstractView::SelectionMode selection_mode)
  165. {
  166. m_table_view->set_selection_mode(selection_mode);
  167. m_icon_view->set_selection_mode(selection_mode);
  168. m_columns_view->set_selection_mode(selection_mode);
  169. }
  170. void MultiView::set_key_column_and_sort_order(int column, SortOrder sort_order)
  171. {
  172. for_each_view_implementation([&](auto& view) {
  173. view.set_key_column_and_sort_order(column, sort_order);
  174. });
  175. }
  176. }