Serenity Operating System
at hosted 109 lines 4.3 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 "VBPropertiesWindow.h" 28#include "VBWidgetPropertyModel.h" 29#include <LibGUI/BoxLayout.h> 30#include <LibGUI/ComboBox.h> 31#include <LibGUI/ModelEditingDelegate.h> 32#include <LibGUI/TableView.h> 33#include <LibGUI/TextBox.h> 34#include <LibGUI/Widget.h> 35 36class BoolValuesModel final : public GUI::Model { 37public: 38 virtual int row_count(const GUI::ModelIndex&) const override { return 2; } 39 virtual int column_count(const GUI::ModelIndex&) const override { return 1; } 40 virtual void update() override {} 41 virtual GUI::Variant data(const GUI::ModelIndex& index, Role role) const override 42 { 43 if (role != Role::Display) 44 return {}; 45 switch (index.row()) { 46 case 0: 47 return "false"; 48 case 1: 49 return "true"; 50 } 51 ASSERT_NOT_REACHED(); 52 } 53}; 54 55class BoolModelEditingDelegate : public GUI::ModelEditingDelegate { 56public: 57 BoolModelEditingDelegate() {} 58 virtual ~BoolModelEditingDelegate() override {} 59 60 virtual RefPtr<GUI::Widget> create_widget() override 61 { 62 auto combo = GUI::ComboBox::construct(); 63 combo->set_only_allow_values_from_model(true); 64 combo->set_model(adopt(*new BoolValuesModel)); 65 combo->on_return_pressed = [this] { commit(); }; 66 combo->on_change = [this](auto&, auto&) { commit(); }; 67 return combo; 68 } 69 virtual GUI::Variant value() const override { return static_cast<const GUI::ComboBox*>(widget())->text() == "true"; } 70 virtual void set_value(const GUI::Variant& value) override { static_cast<GUI::ComboBox*>(widget())->set_text(value.to_string()); } 71 virtual void will_begin_editing() override 72 { 73 auto& combo = *static_cast<GUI::ComboBox*>(widget()); 74 combo.select_all(); 75 combo.open(); 76 } 77}; 78 79VBPropertiesWindow::VBPropertiesWindow() 80{ 81 set_title("Properties"); 82 set_rect(780, 200, 240, 280); 83 84 auto& widget = set_main_widget<GUI::Widget>(); 85 widget.set_fill_with_background_color(true); 86 widget.set_layout<GUI::VerticalBoxLayout>(); 87 widget.layout()->set_margins({ 2, 2, 2, 2 }); 88 89 m_table_view = widget.add<GUI::TableView>(); 90 m_table_view->set_headers_visible(false); 91 m_table_view->set_editable(true); 92 93 m_table_view->aid_create_editing_delegate = [this](auto& index) -> OwnPtr<GUI::ModelEditingDelegate> { 94 if (!m_table_view->model()) 95 return nullptr; 96 auto type_index = m_table_view->model()->index(index.row(), VBWidgetPropertyModel::Column::Type); 97 auto type = m_table_view->model()->data(type_index, GUI::Model::Role::Custom).to_i32(); 98 switch ((GUI::Variant::Type)type) { 99 case GUI::Variant::Type::Bool: 100 return make<BoolModelEditingDelegate>(); 101 default: 102 return make<GUI::StringModelEditingDelegate>(); 103 } 104 }; 105} 106 107VBPropertiesWindow::~VBPropertiesWindow() 108{ 109}