Serenity Operating System
at hosted 200 lines 6.2 kB view raw
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#include <AK/FileSystemPath.h> 28#include <AK/StringBuilder.h> 29#include <LibGUI/Action.h> 30#include <LibGUI/ActionGroup.h> 31#include <LibGUI/Model.h> 32#include <LibGUI/MultiView.h> 33#include <LibGUI/Window.h> 34#include <stdio.h> 35#include <unistd.h> 36 37namespace GUI { 38 39MultiView::MultiView() 40{ 41 set_active_widget(nullptr); 42 m_item_view = add<ItemView>(); 43 m_table_view = add<TableView>(); 44 45#ifdef MULTIVIEW_WITH_COLUMNSVIEW 46 m_columns_view = add<ColumnsView>(); 47#endif 48 49 m_item_view->on_activation = [&](auto& index) { 50 if (on_activation) 51 on_activation(index); 52 }; 53#ifdef MULTIVIEW_WITH_COLUMNSVIEW 54 m_columns_view->on_activation = [&](auto& index) { 55 if (on_activation) 56 on_activation(index); 57 }; 58#endif 59 m_table_view->on_activation = [&](auto& index) { 60 if (on_activation) 61 on_activation(index); 62 }; 63 64 m_table_view->on_selection_change = [this] { 65 if (on_selection_change) 66 on_selection_change(); 67 }; 68 m_item_view->on_selection_change = [this] { 69 if (on_selection_change) 70 on_selection_change(); 71 }; 72#ifdef MULTIVIEW_WITH_COLUMNSVIEW 73 m_columns_view->on_selection_change = [this] { 74 if (on_selection_change) 75 on_selection_change(); 76 }; 77#endif 78 79 m_table_view->on_context_menu_request = [this](auto& index, auto& event) { 80 if (on_context_menu_request) 81 on_context_menu_request(index, event); 82 }; 83 m_item_view->on_context_menu_request = [this](auto& index, auto& event) { 84 if (on_context_menu_request) 85 on_context_menu_request(index, event); 86 }; 87#ifdef MULTIVIEW_WITH_COLUMNSVIEW 88 m_columns_view->on_context_menu_request = [this](auto& index, auto& event) { 89 if (on_context_menu_request) 90 on_context_menu_request(index, event); 91 }; 92#endif 93 94 m_table_view->on_drop = [this](auto& index, auto& event) { 95 if (on_drop) 96 on_drop(index, event); 97 }; 98 m_item_view->on_drop = [this](auto& index, auto& event) { 99 if (on_drop) 100 on_drop(index, event); 101 }; 102#ifdef MULTIVIEW_WITH_COLUMNSVIEW 103 m_columns_view->on_drop = [this](auto& index, auto& event) { 104 if (on_drop) 105 on_drop(index, event); 106 }; 107#endif 108 set_view_mode(ViewMode::Icon); 109 110 build_actions(); 111} 112 113MultiView::~MultiView() 114{ 115} 116 117void MultiView::set_view_mode(ViewMode mode) 118{ 119 if (m_view_mode == mode) 120 return; 121 m_view_mode = mode; 122 update(); 123 if (mode == ViewMode::List) { 124 set_active_widget(m_table_view); 125 return; 126 } 127#ifdef MULTIVIEW_WITH_COLUMNSVIEW 128 if (mode == ViewMode::Columns) { 129 set_active_widget(m_columns_view); 130 return; 131 } 132#endif 133 if (mode == ViewMode::Icon) { 134 set_active_widget(m_item_view); 135 return; 136 } 137 ASSERT_NOT_REACHED(); 138} 139 140void MultiView::set_model(RefPtr<Model> model) 141{ 142 if (m_model == model) 143 return; 144 m_model = model; 145 for_each_view_implementation([&](auto& view) { 146 view.set_model(model); 147 }); 148} 149 150void MultiView::set_model_column(int column) 151{ 152 if (m_model_column == column) 153 return; 154 m_model_column = column; 155 m_item_view->set_model_column(column); 156#ifdef MULTIVIEW_WITH_COLUMNSVIEW 157 m_columns_view->set_model_column(column); 158#endif 159} 160 161void MultiView::set_column_hidden(int column_index, bool hidden) 162{ 163 m_table_view->set_column_hidden(column_index, hidden); 164} 165 166void MultiView::build_actions() 167{ 168 m_view_as_table_action = Action::create( 169 "Table view", Gfx::Bitmap::load_from_file("/res/icons/16x16/table-view.png"), [this](auto&) { 170 set_view_mode(ViewMode::List); 171 m_view_as_table_action->set_checked(true); 172 }); 173 m_view_as_table_action->set_checkable(true); 174 175 m_view_as_icons_action = Action::create( 176 "Icon view", Gfx::Bitmap::load_from_file("/res/icons/16x16/icon-view.png"), [this](auto&) { 177 set_view_mode(ViewMode::Icon); 178 m_view_as_icons_action->set_checked(true); 179 }); 180 m_view_as_icons_action->set_checkable(true); 181 182#ifdef MULTIVIEW_WITH_COLUMNSVIEW 183 m_view_as_columns_action = Action::create( 184 "Columns view", Gfx::Bitmap::load_from_file("/res/icons/16x16/columns-view.png"), [this](auto&) { 185 set_view_mode(ViewMode::Columns); 186 m_view_as_columns_action->set_checked(true); 187 }); 188 m_view_as_columns_action->set_checkable(true); 189#endif 190 191 m_view_type_action_group = make<ActionGroup>(); 192 m_view_type_action_group->set_exclusive(true); 193 m_view_type_action_group->add_action(*m_view_as_table_action); 194 m_view_type_action_group->add_action(*m_view_as_icons_action); 195#ifdef MULTIVIEW_WITH_COLUMNSVIEW 196 m_view_type_action_group->add_action(*m_view_as_columns_action); 197#endif 198} 199 200}