MultiView.cpp 6.0 KB

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