Class: Ui::RelationConfigModel

Inherits:
Qt::AbstractItemModel
  • Object
show all
Defined in:
lib/roby/gui/relations_view/relations_config.rb

Overview

Manage relations using a two-level tree structure. The relation categories (tasks and events) have a model index of (row, column, nil) while the relations have a model index of (row, column, category), where category is TASK_ROOT_INDEX for task relations and EVENT_ROOT_INDEX for event relations

Constant Summary collapse

COL_NAME =
0
COL_COLOR =
1
TASK_ROOT_INDEX =
0
EVENT_ROOT_INDEX =
1
CATEGORIES =
["Task structure"].freeze
QTRUBY_INDEX_USING_POINTERS =
1
QTRUBY_INDEX_USING_OBJECTIDS =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(display) ⇒ RelationConfigModel

Returns a new instance of RelationConfigModel.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/roby/gui/relations_view/relations_config.rb', line 29

def initialize(display)
    super()

    @current_color = 0
    @display = display
    @relations = []

    relations[TASK_ROOT_INDEX] = Roby::TaskStructure.relations.to_a

    RelationConfigModel.detect_qtruby_behaviour(createIndex(0, 0, 0))
end

Instance Attribute Details

#displayObject (readonly)

Returns the value of attribute display.



27
28
29
# File 'lib/roby/gui/relations_view/relations_config.rb', line 27

def display
  @display
end

#relationsObject (readonly)

Returns the value of attribute relations.



27
28
29
# File 'lib/roby/gui/relations_view/relations_config.rb', line 27

def relations
  @relations
end

Class Method Details

.category_from_index(idx) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/roby/gui/relations_view/relations_config.rb', line 51

def self.category_from_index(idx)
    if @@qtruby_behaviour == QTRUBY_INDEX_USING_POINTERS
        idx.internalPointer
    else
        idx.internalId >> 1
    end
end

.detect_qtruby_behaviour(idx) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/roby/gui/relations_view/relations_config.rb', line 43

def self.detect_qtruby_behaviour(idx)
    if idx.internalPointer == 0
        @@qtruby_behaviour = QTRUBY_INDEX_USING_POINTERS
    elsif idx.internalId == 1
        @@qtruby_behaviour = QTRUBY_INDEX_USING_OBJECTIDS
    end
end

Instance Method Details

#columnCount(parent) ⇒ Object



78
79
80
# File 'lib/roby/gui/relations_view/relations_config.rb', line 78

def columnCount(parent)
    2
end

#data(index, role) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/roby/gui/relations_view/relations_config.rb', line 107

def data(index, role)
    return Qt::Variant.new unless index.valid?

    category = RelationConfigModel.category_from_index(index)
    value = if category == -1
                if index.column == COL_NAME && role == Qt::DisplayRole
                    CATEGORIES[index.row]
                end
            else
                relation = relations[category][index.row]
                if index.column == COL_NAME && role == Qt::CheckStateRole
                    if    display.relation_enabled?(relation) then Qt::Checked.to_i
                    elsif display.layout_relation?(relation)  then Qt::PartiallyChecked.to_i
                    else
                        Qt::Unchecked.to_i
                    end
                elsif index.column == COL_NAME && role == Qt::DisplayRole
                    relation.name.gsub(/.*Structure::/, "")
                elsif index.column == COL_COLOR && role == Qt::DisplayRole
                    display.relation_color(relation)
                end
            end

    if value then Qt::Variant.new(value)
    else
        Qt::Variant.new
    end
end

#event_root_indexObject



19
20
21
# File 'lib/roby/gui/relations_view/relations_config.rb', line 19

def event_root_index
    createIndex(EVENT_ROOT_INDEX, 0, -1)
end

#flags(index) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/roby/gui/relations_view/relations_config.rb', line 155

def flags(index)
    if !index.valid? || RelationConfigModel.category_from_index(index) == -1 then Qt::ItemIsEnabled
    else
        flags = Qt::ItemIsSelectable | Qt::ItemIsTristate | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable
        if index.column == 1
            flags = Qt::ItemIsEditable | flags
        end
        flags
    end
end

#hasChildren(parent) ⇒ Object



82
83
84
# File 'lib/roby/gui/relations_view/relations_config.rb', line 82

def hasChildren(parent)
    !parent.valid? || RelationConfigModel.category_from_index(parent) == -1
end

#headerData(section, orientation, role) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/roby/gui/relations_view/relations_config.rb', line 94

def headerData(section, orientation, role)
    return Qt::Variant.new unless role == Qt::DisplayRole && orientation == Qt::Horizontal

    value =
        if section == 0
            "Relation"
        else
            "Color"
        end

    Qt::Variant.new(value)
end

#index(row, column, parent) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/roby/gui/relations_view/relations_config.rb', line 59

def index(row, column, parent)
    if parent.valid? && RelationConfigModel.category_from_index(parent) == -1
        createIndex(row, column, parent.row)
    elsif row < relations.size
        createIndex(row, column, -1)
    else
        Qt::ModelIndex.new
    end
end

#parent(index) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/roby/gui/relations_view/relations_config.rb', line 69

def parent(index)
    category = RelationConfigModel.category_from_index(index)
    if !index.valid? || category == -1
        Qt::ModelIndex.new
    else
        createIndex(category, 0, -1)
    end
end

#rowCount(parent) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/roby/gui/relations_view/relations_config.rb', line 86

def rowCount(parent)
    if parent.valid?
        relations[parent.row].size
    else
        relations.size
    end
end

#setData(index, value, role) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/roby/gui/relations_view/relations_config.rb', line 136

def setData(index, value, role)
    category = RelationConfigModel.category_from_index(index)
    relation = relations[category][index.row]
    if role == Qt::CheckStateRole
        case value.to_i
        when Qt::Checked.to_i
            display.enable_relation(relation)
        when Qt::PartiallyChecked.to_i
            display.layout_relation(relation)
        else
            display.ignore_relation(relation)
        end
    else
        display.update_relation_color(relation, value.to_string)
    end
    display.update
    emit dataChanged(index, index)
end

#task_root_indexObject



23
24
25
# File 'lib/roby/gui/relations_view/relations_config.rb', line 23

def task_root_index
    createIndex(TASK_ROOT_INDEX, 0, -1)
end