MultiView.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. namespace GUI {
  33. class MultiView final : public GUI::StackWidget {
  34. C_OBJECT(MultiView)
  35. public:
  36. virtual ~MultiView() override;
  37. void refresh();
  38. Function<void()> on_selection_change;
  39. Function<void(const ModelIndex&)> on_activation;
  40. Function<void(const ModelIndex&)> on_selection;
  41. Function<void(const ModelIndex&, const ContextMenuEvent&)> on_context_menu_request;
  42. Function<void(const ModelIndex&, const DropEvent&)> on_drop;
  43. enum ViewMode {
  44. Invalid,
  45. Table,
  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. void set_key_column_and_sort_order(int column, SortOrder);
  55. GUI::AbstractView& current_view()
  56. {
  57. switch (m_view_mode) {
  58. case ViewMode::Table:
  59. return *m_table_view;
  60. case ViewMode::Columns:
  61. return *m_columns_view;
  62. case ViewMode::Icon:
  63. return *m_icon_view;
  64. default:
  65. ASSERT_NOT_REACHED();
  66. }
  67. }
  68. const ModelSelection& selection() const { return const_cast<MultiView&>(*this).current_view().selection(); }
  69. ModelSelection& selection() { return current_view().selection(); }
  70. template<typename Callback>
  71. void for_each_view_implementation(Callback callback)
  72. {
  73. callback(*m_table_view);
  74. callback(*m_icon_view);
  75. callback(*m_columns_view);
  76. }
  77. Model* model() { return m_model; }
  78. const Model* model() const { return m_model; }
  79. void set_model(RefPtr<Model>);
  80. Action& view_as_table_action() { return *m_view_as_table_action; }
  81. Action& view_as_icons_action() { return *m_view_as_icons_action; }
  82. Action& view_as_columns_action() { return *m_view_as_columns_action; }
  83. bool is_multi_select() const { return m_multi_select; }
  84. void set_multi_select(bool);
  85. private:
  86. MultiView();
  87. void build_actions();
  88. void apply_multi_select();
  89. ViewMode m_view_mode { Invalid };
  90. int m_model_column { 0 };
  91. RefPtr<Model> m_model;
  92. RefPtr<TableView> m_table_view;
  93. RefPtr<IconView> m_icon_view;
  94. RefPtr<ColumnsView> m_columns_view;
  95. RefPtr<Action> m_view_as_table_action;
  96. RefPtr<Action> m_view_as_icons_action;
  97. RefPtr<Action> m_view_as_columns_action;
  98. OwnPtr<ActionGroup> m_view_type_action_group;
  99. bool m_multi_select { true };
  100. };
  101. }