MultiView.cpp 5.8 KB

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