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#include "VBWidgetPropertyModel.h"
28#include "VBProperty.h"
29#include "VBWidget.h"
30#include <LibGfx/Font.h>
31
32VBWidgetPropertyModel::VBWidgetPropertyModel(VBWidget& widget)
33 : m_widget(widget)
34{
35}
36
37VBWidgetPropertyModel::~VBWidgetPropertyModel()
38{
39}
40
41int VBWidgetPropertyModel::row_count(const GUI::ModelIndex&) const
42{
43 return m_widget.m_properties.size();
44}
45
46String VBWidgetPropertyModel::column_name(int column) const
47{
48 switch (column) {
49 case Column::Name:
50 return "Name";
51 case Column::Value:
52 return "Value";
53 case Column::Type:
54 return "Type";
55 default:
56 ASSERT_NOT_REACHED();
57 }
58}
59
60GUI::Model::ColumnMetadata VBWidgetPropertyModel::column_metadata(int column) const
61{
62 UNUSED_PARAM(column);
63 if (column == Column::Name)
64 return { 110, Gfx::TextAlignment::CenterLeft, &Gfx::Font::default_bold_font() };
65 return { 90, Gfx::TextAlignment::CenterLeft };
66}
67
68GUI::Variant VBWidgetPropertyModel::data(const GUI::ModelIndex& index, Role role) const
69{
70 if (role == Role::Custom) {
71 auto& property = m_widget.m_properties[index.row()];
72 if (index.column() == Column::Type)
73 return (int)property.value().type();
74 return {};
75 }
76 if (role == Role::Display) {
77 auto& property = m_widget.m_properties[index.row()];
78 switch (index.column()) {
79 case Column::Name:
80 return property.name();
81 case Column::Value:
82 return property.value();
83 case Column::Type:
84 return to_string(property.value().type());
85 }
86 ASSERT_NOT_REACHED();
87 }
88 if (role == Role::ForegroundColor) {
89 auto& property = m_widget.m_properties[index.row()];
90 switch (index.column()) {
91 case Column::Name:
92 return Color::Black;
93 case Column::Type:
94 return Color::Blue;
95 case Column::Value:
96 return property.is_readonly() ? Color(Color::MidGray) : Color(Color::Black);
97 }
98 ASSERT_NOT_REACHED();
99 }
100 return {};
101}
102
103void VBWidgetPropertyModel::set_data(const GUI::ModelIndex& index, const GUI::Variant& value)
104{
105 ASSERT(index.column() == Column::Value);
106 auto& property = m_widget.m_properties[index.row()];
107 ASSERT(!property.is_readonly());
108 property.set_value(value);
109}
110
111bool VBWidgetPropertyModel::is_editable(const GUI::ModelIndex& index) const
112{
113 if (index.column() != Column::Value)
114 return false;
115 auto& property = m_widget.m_properties[index.row()];
116 return !property.is_readonly();
117}