MultiView.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #pragma once
  27. #include <LibGUI/Action.h>
  28. #include <LibGUI/ColumnsView.h>
  29. #include <LibGUI/ItemView.h>
  30. #include <LibGUI/StackWidget.h>
  31. #include <LibGUI/TableView.h>
  32. //#define MULTIVIEW_WITH_COLUMNSVIEW
  33. namespace GUI {
  34. class MultiView final : public GUI::StackWidget {
  35. C_OBJECT(MultiView)
  36. public:
  37. virtual ~MultiView() override;
  38. void refresh();
  39. Function<void()> on_selection_change;
  40. Function<void(const ModelIndex&)> on_activation;
  41. Function<void(const ModelIndex&)> on_selection;
  42. Function<void(const ModelIndex&, const ContextMenuEvent&)> on_context_menu_request;
  43. Function<void(const ModelIndex&, const DropEvent&)> on_drop;
  44. enum ViewMode {
  45. List,
  46. Columns,
  47. Icon
  48. };
  49. void set_view_mode(ViewMode);
  50. ViewMode view_mode() const { return m_view_mode; }
  51. int model_column() const { return m_model_column; }
  52. void set_model_column(int);
  53. void set_column_hidden(int column_index, bool hidden);
  54. GUI::AbstractView& current_view()
  55. {
  56. switch (m_view_mode) {
  57. case ViewMode::List:
  58. return *m_table_view;
  59. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  60. case ViewMode::Columns:
  61. return *m_columns_view;
  62. #endif
  63. case ViewMode::Icon:
  64. return *m_item_view;
  65. default:
  66. ASSERT_NOT_REACHED();
  67. }
  68. }
  69. const ModelSelection& selection() const { return const_cast<MultiView&>(*this).current_view().selection(); }
  70. ModelSelection& selection() { return current_view().selection(); }
  71. template<typename Callback>
  72. void for_each_view_implementation(Callback callback)
  73. {
  74. callback(*m_table_view);
  75. callback(*m_item_view);
  76. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  77. callback(*m_columns_view);
  78. #endif
  79. }
  80. Model* model() { return m_model; }
  81. const Model* model() const { return m_model; }
  82. void set_model(RefPtr<Model>);
  83. Action& view_as_table_action() { return *m_view_as_table_action; }
  84. Action& view_as_icons_action() { return *m_view_as_icons_action; }
  85. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  86. Action& view_as_columns_action() { return *m_view_as_columns_action; }
  87. #endif
  88. private:
  89. MultiView();
  90. void build_actions();
  91. ViewMode m_view_mode { Icon };
  92. int m_model_column { 0 };
  93. RefPtr<Model> m_model;
  94. RefPtr<TableView> m_table_view;
  95. RefPtr<ItemView> m_item_view;
  96. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  97. RefPtr<ColumnsView> m_columns_view;
  98. #endif
  99. RefPtr<Action> m_view_as_table_action;
  100. RefPtr<Action> m_view_as_icons_action;
  101. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  102. RefPtr<Action> m_view_as_columns_action;
  103. #endif
  104. OwnPtr<ActionGroup> m_view_type_action_group;
  105. };
  106. }