MultiView.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/IconView.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. Invalid,
  46. Table,
  47. Columns,
  48. Icon
  49. };
  50. void set_view_mode(ViewMode);
  51. ViewMode view_mode() const { return m_view_mode; }
  52. int model_column() const { return m_model_column; }
  53. void set_model_column(int);
  54. void set_column_hidden(int column_index, bool hidden);
  55. GUI::AbstractView& current_view()
  56. {
  57. switch (m_view_mode) {
  58. case ViewMode::Table:
  59. return *m_table_view;
  60. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  61. case ViewMode::Columns:
  62. return *m_columns_view;
  63. #endif
  64. case ViewMode::Icon:
  65. return *m_icon_view;
  66. default:
  67. ASSERT_NOT_REACHED();
  68. }
  69. }
  70. const ModelSelection& selection() const { return const_cast<MultiView&>(*this).current_view().selection(); }
  71. ModelSelection& selection() { return current_view().selection(); }
  72. template<typename Callback>
  73. void for_each_view_implementation(Callback callback)
  74. {
  75. callback(*m_table_view);
  76. callback(*m_icon_view);
  77. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  78. callback(*m_columns_view);
  79. #endif
  80. }
  81. Model* model() { return m_model; }
  82. const Model* model() const { return m_model; }
  83. void set_model(RefPtr<Model>);
  84. Action& view_as_table_action() { return *m_view_as_table_action; }
  85. Action& view_as_icons_action() { return *m_view_as_icons_action; }
  86. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  87. Action& view_as_columns_action() { return *m_view_as_columns_action; }
  88. #endif
  89. private:
  90. MultiView();
  91. void build_actions();
  92. ViewMode m_view_mode { Invalid };
  93. int m_model_column { 0 };
  94. RefPtr<Model> m_model;
  95. RefPtr<TableView> m_table_view;
  96. RefPtr<IconView> m_icon_view;
  97. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  98. RefPtr<ColumnsView> m_columns_view;
  99. #endif
  100. RefPtr<Action> m_view_as_table_action;
  101. RefPtr<Action> m_view_as_icons_action;
  102. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  103. RefPtr<Action> m_view_as_columns_action;
  104. #endif
  105. OwnPtr<ActionGroup> m_view_type_action_group;
  106. };
  107. }