Class: StudyTableModel

Inherits:
Qt::AbstractTableModel
  • Object
show all
Defined in:
lib/tmis/interface/models/study_table_model.rb

Overview

~~~~~~~~~~~~~~~~~~~~~~~~~~

Instance Method Summary collapse

Constructor Details

#initialize(date, parent = nil) ⇒ StudyTableModel

Returns a new instance of StudyTableModel.



15
16
17
18
19
20
21
22
# File 'lib/tmis/interface/models/study_table_model.rb', line 15

def initialize(date, parent = nil)
  super()
  @view = parent
  @date = date
  @studies = setup
  @colors_for_studies = {}
  @colors_for_cabinets = {}
end

Instance Method Details

#cancelColoringObject



314
315
316
317
318
# File 'lib/tmis/interface/models/study_table_model.rb', line 314

def cancelColoring()
  @colors_for_studies.clear
  @colors_for_cabinets.clear
  refresh
end

#columnCount(parent = self) ⇒ Object



49
50
51
# File 'lib/tmis/interface/models/study_table_model.rb', line 49

def columnCount(parent = self)
  @titles.size * 2
end

#data(index, role = Qt::DisplayRole, data = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tmis/interface/models/study_table_model.rb', line 53

def data(index, role = Qt::DisplayRole, data = nil)
  default = Qt::Variant.new
  return default unless index.valid?
  case role
  when Qt::DisplayRole || Qt::EditRole
    begin
      if index.column.even?
        @studies[index.column / 2][1][(index.row / 2) + 1][index.row % 2].to_s.to_v
      else
        @studies[index.column / 2][1][(index.row / 2) + 1][index.row % 2].cabinet.title.to_v
      end
    rescue NoMethodError
      default
    end
    #if index.column.even?
    #  @studies[index.column / 2].try(:at, 1).try(:fetch, (index.row / 2) + 1, nil).try(:at, index.row % 2).try(:to_s)
    #else
    #  @studies[index.column / 2].try(:at, 1).try(:fetch, (index.row / 2) + 1, nil).try(:at, index.row % 2).try(:cabinet).try(:title)
    #end.try(:to_v) || default
  when Qt::UserRole
    begin
      if (st = @studies[index.column / 2])
        if (st = st[1])
          if (st = st[(index.row / 2) + 1])
            if (study = st[index.row % 2])
              Base64.encode64(Marshal.dump(study)).to_v
            else
              group = @groups[index.column / 2]
              Base64.encode64(Marshal.dump(group)).to_v
            end
          else
            group = @groups[index.column / 2]
            Base64.encode64(Marshal.dump(group)).to_v
          end
        else
          group = @groups[index.column / 2]
          Base64.encode64(Marshal.dump(group)).to_v
        end
      elsif (group = @groups[index.column / 2])
        Base64.encode64(Marshal.dump(group)).to_v
      else
        default
      end
    rescue NoMethodError
      default
    end
  when  Qt::BackgroundRole #Qt::TextColorRole
    begin
      if index.column.even?
        study = @studies[index.column / 2][1][(index.row / 2) + 1][index.row % 2]
        if @colors_for_studies[study.id]
          @colors_for_studies[study.id].to_v
        elsif study.color
          Qt::Variant.fromValue(Qt::Color.new(study.color))
        else
          default
        end
      else
        study = @studies[index.column / 2][1][(index.row / 2) + 1][index.row % 2]
        if @colors_for_cabinets[study.id]
          @colors_for_cabinets[study.id].to_v
        elsif study.color
          Qt::Variant.fromValue(Qt::Color.new(study.color))
        else
          default
        end
      end
    rescue NoMethodError
      default
    end
  else
    default
  end
end

#displayMenu(pos) ⇒ Object



298
299
300
301
302
303
304
# File 'lib/tmis/interface/models/study_table_model.rb', line 298

def displayMenu(pos)
  menu = Qt::Menu.new()
  remove = Qt::Action.new('Удалить', menu)
  connect(remove, SIGNAL('triggered()'), self, SLOT('removeData()'))
  menu.addAction(remove)
  menu.exec(@view.viewport.mapToGlobal(pos))
end

#dropMimeData(data, action, row, column, index) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/tmis/interface/models/study_table_model.rb', line 209

def dropMimeData(data, action, row, column, index)
  table_model = ObjectSpace._id2ref(data.data('application/model').data.to_i)
  subject_id = data.data('application/subject').data if data.hasFormat('application/subject')
  lecturer_id = data.data('application/lecturer').data if data.hasFormat('application/lecturer')
  cabinet_id = data.data('application/cabinet').data if data.hasFormat('application/cabinet')
  # date почему-то содержит "application/subject" с каким-то мусором, если drag осуществляется из табицы
  return false if !index.valid? || data.hasFormat('application/empty')
  if data.hasFormat('application/study')
    study = Marshal.load(Base64.decode64(data.data('application/study').data))
    study.number = (1..6).to_a[index.row / 2]
    study.date = @date
    if study.groupable.group?
      study.groupable_id = @groups[index.column / 2].id
    else
      number = study.groupable.number
      study.groupable = @groups[index.column / 2].subgroups.where(number: number).first
    end
    if (studies = @studies[index.column / 2][1][(index.row / 2) + 1]) && (studies[index.row % 2])
      exist_study = studies[index.row % 2]
      exist_study.delete unless exist_study == study
      @studies[index.column / 2][1][(index.row / 2) + 1][index.row % 2] = study
    else
      if @studies[index.column / 2][1][(index.row / 2) + 1]
        @studies[index.column / 2][1][(index.row / 2) + 1][index.row % 2] = study
      else
        array = []
        array[index.row % 2] = study
        @studies[index.column / 2][1][(index.row / 2) + 1] = array
      end
    end
  else
    if (studies = @studies[index.column / 2][1][(index.row / 2) + 1]) && (studies[index.row % 2])
      study = studies[index.row % 2]
      study.subject_id = subject_id if subject_id
      study.lecturer_id = lecturer_id if lecturer_id
      study.cabinet_id = cabinet_id if cabinet_id
    elsif subject_id || lecturer_id || cabinet_id
      study = Study.new
      if (studies = @studies[index.column / 2][1][(index.row / 2) + 1]) && (studies[(index.row % 2) - 1])
        another_study = studies[(index.row % 2) - 1]
        if another_study.groupable.subgroup?
          if another_study.groupable.number == 1
            study.groupable = another_study.groupable.get_group.subgroups.where(number: 2).first
          else
            study.groupable = another_study.groupable.get_group.subgroups.where(number: 1).first
          end
        else
          another_study.groupable = another_study.groupable.get_group.subgroups.where(number: 1).first
          study.groupable = another_study.groupable.get_group.subgroups.where(number: 2).first
        end
      else
        study.groupable_type = 'Group'
        study.groupable_id = @groups[index.column / 2].id
      end
      study.number = (1..6).to_a[index.row / 2]
      study.date = @date
      study.subject_id = subject_id || Subject.where(stub: true).first.id
      study.lecturer_id = lecturer_id || Lecturer.where(stub: true).first.id
      study.cabinet_id = cabinet_id || Cabinet.where(stub: true).first.id
    end
  end
  study.save
  refresh
  table_model.refresh if table_model && table_model != self
  emit refreshTarification(index) if $TARIFICATION_MODE
  emit dataChanged(index, index)
  true
end

#editStudy(index) ⇒ Object



320
321
322
# File 'lib/tmis/interface/models/study_table_model.rb', line 320

def editStudy(index)
  setData(index, nil, Qt::EditRole)
end

#flags(index) ⇒ Object



142
143
144
# File 'lib/tmis/interface/models/study_table_model.rb', line 142

def flags(index)
  Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | super(index) if index.valid?
end

#headerData(section, orientation, role = Qt::DisplayRole) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/tmis/interface/models/study_table_model.rb', line 128

def headerData(section, orientation, role = Qt::DisplayRole)
  invalid = Qt::Variant.new
  return invalid unless role == Qt::DisplayRole
  v = case orientation
      when Qt::Vertical
        (1..6).zip(Array.new(6, '')).flatten[section]
      when Qt::Horizontal
        @titles.zip(Array.new(@titles.size, 'Кабинет')).flatten[section]
      else
        ''
      end
  Qt::Variant.new(v)
end

#insertColumns(column, count, parent) ⇒ Object



290
291
292
# File 'lib/tmis/interface/models/study_table_model.rb', line 290

def insertColumns(column, count, parent )
  false
end

#insertRows(row, count, parent) ⇒ Object



286
287
288
# File 'lib/tmis/interface/models/study_table_model.rb', line 286

def insertRows(row, count, parent )
  false
end

#mimeData(indexes) ⇒ Object

end



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/tmis/interface/models/study_table_model.rb', line 193

def mimeData(indexes)
  index = indexes.first
  mime_data = super indexes # для обхода ошибки сегментации Qt::MimeData создаётся с помощью родительского метода
  if (studies = @studies[index.column / 2][1][(index.row / 2) + 1]) && (studies[index.row % 2])
    study = studies[index.row % 2]
    study_dump = Base64.encode64(Marshal.dump(study))
    byte_study = Qt::ByteArray.new study_dump
    table_model_ref = Qt::ByteArray.new self.object_id.to_s
    mime_data.setData('application/study', byte_study)
    mime_data.setData('application/model', table_model_ref)
  else
    mime_data.setData('application/empty', Qt::ByteArray.new(''))
  end
  mime_data
end

#mimeTypesObject



294
295
296
# File 'lib/tmis/interface/models/study_table_model.rb', line 294

def mimeTypes
  ['application/subject', 'application/lecturer', 'application/cabinet', 'application/study']
end

#refreshObject



40
41
42
43
# File 'lib/tmis/interface/models/study_table_model.rb', line 40

def refresh
  @studies = setup
  emit layoutChanged()
end

#removeDataObject



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/tmis/interface/models/study_table_model.rb', line 172

def removeData
  if @view.hasFocus && (index = @view.currentIndex).valid?
    if (studies = @studies[index.column / 2][1][(index.row / 2) + 1])
      if (study = studies[index.row % 2])
        study.delete
        refresh
        emit refreshTarification(index) if $TARIFICATION_MODE
        emit dataChanged(index, createIndex(index.row, index.column + 1))
      end
    end
  end
end

#rowCount(parent = self) ⇒ Object



45
46
47
# File 'lib/tmis/interface/models/study_table_model.rb', line 45

def rowCount(parent = self)
  12
end

#setColor(id, color) ⇒ Object



306
307
308
# File 'lib/tmis/interface/models/study_table_model.rb', line 306

def setColor(id, color)
  @colors_for_studies[id] = color
end

#setColorCabinet(id, color) ⇒ Object



310
311
312
# File 'lib/tmis/interface/models/study_table_model.rb', line 310

def setColorCabinet(id, color)
  @colors_for_cabinets[id] = color
end

#setData(index, variant, role = Qt::EditRole) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/tmis/interface/models/study_table_model.rb', line 146

def setData(index, variant, role = Qt::EditRole)
  if index.valid? and role == Qt::EditRole
    if (studies = @studies[index.column / 2][1][(index.row / 2) + 1]) && (studies[index.row % 2])
      study = studies[index.row % 2]
      EditStudyDialog.new().setupData(study).exec
      refresh
      emit studySaved(study.date.to_v) unless study.date == @date
    else
      study = Study.new
      study.groupable_type = 'Group'
      study.groupable_id = @groups[index.column / 2].id
      study.number = (1..6).to_a[index.row / 2]
      study.date = @date
      EditStudyDialog.new().setupData(study).exec
      unless study.new_record?
        refresh
        emit studySaved(study.date.to_v) unless study.date == @date
      end
    end
    emit dataChanged(index, index)
    true
  else
    false
  end
end

#setItemData(index, roles) ⇒ Object



278
279
280
# File 'lib/tmis/interface/models/study_table_model.rb', line 278

def setItemData(index, roles)
  false
end

#setupObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tmis/interface/models/study_table_model.rb', line 24

def setup
  @groups = Group.all.sort_by(&:title_for_sort)
  @titles = @groups.map(&:title)
  Hash[ Group.all.map{|g| [g, []]} ].
    merge(Study.of_groups_and_its_subgroups(Group.scoped).
    where(date: @date).
    group_by(&:get_group)).
    sort_by{|k, v| k.title_for_sort}.
    map do |k, v|
      [k, Hash[ v.sort_by(&:number).
                  group_by(&:number).
                  map{|n, ss| [n, ss.sort_by{|s| s.groupable.number}]}
              ] ]
    end
end

#supportedDropActionsObject



282
283
284
# File 'lib/tmis/interface/models/study_table_model.rb', line 282

def supportedDropActions
  Qt::CopyAction
end