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