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