Class: ExportGroupTimetableDialog

Inherits:
Qt::Dialog
  • Object
show all
Defined in:
lib/tmis/interface/forms/export_group_timetable.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(initial_date, parent = nil) ⇒ ExportGroupTimetableDialog

Returns a new instance of ExportGroupTimetableDialog.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tmis/interface/forms/export_group_timetable.rb', line 22

def initialize(initial_date, parent = nil)
  super parent
  @ui = Ui::ExportGroupTimetableDialog.new
  @ui.setup_ui self
  @ui.dayDateEdit.setDate(Qt::Date.fromString(initial_date.to_s, Qt::ISODate))
  @ui.progressBar.visible = false
  Group.all.each do |g|
    item = Qt::ListWidgetItem.new(g.to_s, @ui.groupsListWidget)
    item.setData(Qt::UserRole, Qt::Variant.new(g.id))
    item.checkState = Qt::Unchecked
  end
end

Instance Method Details

#export(dates) ⇒ Object



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
127
128
129
130
131
132
133
# File 'lib/tmis/interface/forms/export_group_timetable.rb', line 70

def export(dates)
  # TODO распараллелить
  # TODO progressBar в процентах
  @ui.progressBar.visible = true
  @ui.progressBar.setRange(0, @ui.groupsListWidget.count)
  if @ui.saveCheckBox.checkState == Qt::Checked and @ui.mailCheckBox.checkState == Qt::Checked
    @ui.groupsListWidget.count.times do |i|
      @ui.progressBar.setValue(i)
      Qt::Application::processEvents
      if @ui.groupsListWidget.item(i).checkState == Qt::Checked
        id = @ui.groupsListWidget.item(i).data(Qt::UserRole)
        group = Group.where(id: id.to_i).first
        path = @ui.folderPathLineEdit.text.force_encoding('UTF-8')
        if File.writable? path
          filename = File.join(path, "#{group.title}_timetable.xls")
          if File.exist? filename
            File.delete filename
            spreadsheet = SpreadsheetCreater.create filename
          else
            spreadsheet = SpreadsheetCreater.create filename
          end
          TimetableExporter.new(spreadsheet, GroupTimetableExportStratagy.new(dates, group)).export.save
          mail(group, filename)
        end
      end
    end
  elsif @ui.saveCheckBox.checkState == Qt::Checked
    @ui.groupsListWidget.count.times do |i|
      @ui.progressBar.setValue(i)
      Qt::Application::processEvents
      if @ui.groupsListWidget.item(i).checkState == Qt::Checked
        id = @ui.groupsListWidget.item(i).data(Qt::UserRole)
        group = Group.where(id: id.to_i).first
        path = @ui.folderPathLineEdit.text.force_encoding('UTF-8')
        if File.writable? path
          filename = File.join(path, "#{group.title}_timetable.xls")
          if File.exist? filename
            File.delete filename
            spreadsheet = SpreadsheetCreater.create filename
          else
            spreadsheet = SpreadsheetCreater.create filename
          end
          TimetableExporter.new(spreadsheet, GroupTimetableExportStratagy.new(dates, group)).export.save
          mail(group, filename)
        end
      end
    end
  elsif @ui.mailCheckBox.checkState == Qt::Checked
    @ui.groupsListWidget.count.times do |i|
      @ui.progressBar.setValue(i)
      Qt::Application::processEvents
      if @ui.groupsListWidget.item(i).checkState == Qt::Checked
        id = @ui.groupsListWidget.item(i).data(Qt::UserRole)
        group = Group.where(id: id.to_i).first
        filename = Dir.mktmpdir('tmis') + "/#{group.title}_timetable.xls"
        spreadsheet = SpreadsheetCreater.create filename
        TimetableExporter.new(spreadsheet, GroupTimetableExportStratagy.new(dates, group)).export.save
        mail(group, filename)
      end
    end
  end
  # thread.join
  @ui.progressBar.visible = false
end

#mail(group, filename) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/tmis/interface/forms/export_group_timetable.rb', line 135

def mail(group, filename)
  group.emails.select(&:email_valid?).each do |email|
    text = "Здравствуйте, куратор группы #{group.to_s}!\n" +
           "В прикреплённой электронной таблице находится расписание для вашей группы.\n"
    Mailer.new(Settings[:mailer, :email], Settings[:mailer, :password]) do
      from     '[email protected]'
      to       email.email
      subject  'Расписание'
      body     text
      add_file filename
    end.send!
  end
end

#on_deselectAllPushButton_pressedObject



55
56
57
# File 'lib/tmis/interface/forms/export_group_timetable.rb', line 55

def on_deselectAllPushButton_pressed
  @ui.groupsListWidget.count.times{|i| @ui.groupsListWidget.item(i).setCheckState(Qt::Unchecked) }
end

#on_exportButtonBox_acceptedObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/tmis/interface/forms/export_group_timetable.rb', line 59

def on_exportButtonBox_accepted
  if @ui.modeComboBox.currentIndex == 0
    date = Date.parse(@ui.dayDateEdit.date.toString(Qt::ISODate)).monday
    export(date..date + 5)
    close
  elsif @ui.modeComboBox.currentIndex == 1
    export([Date.parse(@ui.dayDateEdit.date.toString(Qt::ISODate))])
    close
  end
end

#on_exportButtonBox_rejectedObject



149
150
151
# File 'lib/tmis/interface/forms/export_group_timetable.rb', line 149

def on_exportButtonBox_rejected
  close
end

#on_saveCheckBox_stateChangedObject



35
36
37
# File 'lib/tmis/interface/forms/export_group_timetable.rb', line 35

def on_saveCheckBox_stateChanged
  checkbox_actions(sender, chk, unchk)
end

#on_saveCheckBox_toggled(checked) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tmis/interface/forms/export_group_timetable.rb', line 39

def on_saveCheckBox_toggled(checked)
  if checked
    if (path = Qt::FileDialog::getExistingDirectory(self, 'Open Directory', Dir.home, Qt::FileDialog::ShowDirsOnly | Qt::FileDialog::DontResolveSymlinks))
      @ui.folderPathLineEdit.text = path # force_encoding doesn't help because Qt changes the encoding to ASCII anyway
    else
      @ui.folderPathLineEdit.text = Dir.home
    end
  else
     @ui.folderPathLineEdit.text = ''
  end
end

#on_selectAllPushButton_pressedObject



51
52
53
# File 'lib/tmis/interface/forms/export_group_timetable.rb', line 51

def on_selectAllPushButton_pressed
  @ui.groupsListWidget.count.times{|i| @ui.groupsListWidget.item(i).setCheckState(Qt::Checked) }
end

#show_message(text) ⇒ Object



153
154
155
156
157
# File 'lib/tmis/interface/forms/export_group_timetable.rb', line 153

def show_message(text)
  box = Qt::MessageBox.new
  box.setText text
  box.exec
end