MultiView.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. void set_key_column_and_sort_order(int column, SortOrder);
  56. GUI::AbstractView& current_view()
  57. {
  58. switch (m_view_mode) {
  59. case ViewMode::Table:
  60. return *m_table_view;
  61. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  62. case ViewMode::Columns:
  63. return *m_columns_view;
  64. #endif
  65. case ViewMode::Icon:
  66. return *m_icon_view;
  67. default:
  68. ASSERT_NOT_REACHED();
  69. }
  70. }
  71. const ModelSelection& selection() const { return const_cast<MultiView&>(*this).current_view().selection(); }
  72. ModelSelection& selection() { return current_view().selection(); }
  73. template<typename Callback>
  74. void for_each_view_implementation(Callback callback)
  75. {
  76. callback(*m_table_view);
  77. callback(*m_icon_view);
  78. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  79. callback(*m_columns_view);
  80. #endif
  81. }
  82. Model* model() { return m_model; }
  83. const Model* model() const { return m_model; }
  84. void set_model(RefPtr<Model>);
  85. Action& view_as_table_action() { return *m_view_as_table_action; }
  86. Action& view_as_icons_action() { return *m_view_as_icons_action; }
  87. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  88. Action& view_as_columns_action() { return *m_view_as_columns_action; }
  89. #endif
  90. bool is_multi_select() const { return m_multi_select; }
  91. void set_multi_select(bool);
  92. private:
  93. MultiView();
  94. void build_actions();
  95. void apply_multi_select();
  96. ViewMode m_view_mode { Invalid };
  97. int m_model_column { 0 };
  98. RefPtr<Model> m_model;
  99. RefPtr<TableView> m_table_view;
  100. RefPtr<IconView> m_icon_view;
  101. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  102. RefPtr<ColumnsView> m_columns_view;
  103. #endif
  104. RefPtr<Action> m_view_as_table_action;
  105. RefPtr<Action> m_view_as_icons_action;
  106. #ifdef MULTIVIEW_WITH_COLUMNSVIEW
  107. RefPtr<Action> m_view_as_columns_action;
  108. #endif
  109. OwnPtr<ActionGroup> m_view_type_action_group;
  110. bool m_multi_select { true };
  111. };
  112. }