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