MultiView.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_item_view = add<ItemView>();
  41. m_table_view = add<TableView>();
  42. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  43. m_columns_view = add<ColumnsView>();
  44. #endif
  45. m_item_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_item_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_item_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_item_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. set_view_mode(ViewMode::Icon);
  102. build_actions();
  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::List) {
  114. set_active_widget(m_table_view);
  115. return;
  116. }
  117. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  118. if (mode == ViewMode::Columns) {
  119. set_active_widget(m_columns_view);
  120. return;
  121. }
  122. #endif
  123. if (mode == ViewMode::Icon) {
  124. set_active_widget(m_item_view);
  125. return;
  126. }
  127. ASSERT_NOT_REACHED();
  128. }
  129. void MultiView::set_model(RefPtr<Model> model)
  130. {
  131. if (m_model == model)
  132. return;
  133. m_model = model;
  134. for_each_view_implementation([&](auto& view) {
  135. view.set_model(model);
  136. });
  137. }
  138. void MultiView::set_model_column(int column)
  139. {
  140. if (m_model_column == column)
  141. return;
  142. m_model_column = column;
  143. m_item_view->set_model_column(column);
  144. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  145. m_columns_view->set_model_column(column);
  146. #endif
  147. }
  148. void MultiView::set_column_hidden(int column_index, bool hidden)
  149. {
  150. m_table_view->set_column_hidden(column_index, hidden);
  151. }
  152. void MultiView::build_actions()
  153. {
  154. m_view_as_table_action = Action::create_checkable(
  155. "Table view", Gfx::Bitmap::load_from_file("/res/icons/16x16/table-view.png"), [this](auto&) {
  156. set_view_mode(ViewMode::List);
  157. });
  158. m_view_as_icons_action = Action::create_checkable(
  159. "Icon view", Gfx::Bitmap::load_from_file("/res/icons/16x16/icon-view.png"), [this](auto&) {
  160. set_view_mode(ViewMode::Icon);
  161. });
  162. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  163. m_view_as_columns_action = Action::create_checkable(
  164. "Columns view", Gfx::Bitmap::load_from_file("/res/icons/16x16/columns-view.png"), [this](auto&) {
  165. set_view_mode(ViewMode::Columns);
  166. });
  167. #endif
  168. m_view_type_action_group = make<ActionGroup>();
  169. m_view_type_action_group->set_exclusive(true);
  170. m_view_type_action_group->add_action(*m_view_as_table_action);
  171. m_view_type_action_group->add_action(*m_view_as_icons_action);
  172. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  173. m_view_type_action_group->add_action(*m_view_as_columns_action);
  174. #endif
  175. }
  176. }