class ExportGroupTimetableDialog

include Contracts ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Public Class Methods

new(initial_date, parent = nil) click to toggle source
Calls superclass method
# 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

Public Instance Methods

export(dates) click to toggle source
# 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) click to toggle source
# 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     'tmis@kp11.ru'
      to       email.email
      subject  'Расписание'
      body     text
      add_file filename
    end.send!
  end
end
on_deselectAllPushButton_pressed() click to toggle source
# 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_accepted() click to toggle source
# 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_rejected() click to toggle source
# File lib/tmis/interface/forms/export_group_timetable.rb, line 149
def on_exportButtonBox_rejected
  close
end
on_saveCheckBox_stateChanged() click to toggle source
# 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) click to toggle source
# 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_pressed() click to toggle source
# 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) click to toggle source
# 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