Class: Smartdict::Gui::Controller

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload, Forwardable
Defined in:
lib/smartdict/gui/controller.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

HISTORY_SIZE_ON_START =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeController

Returns a new instance of Controller.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/smartdict/gui/controller.rb', line 15

def initialize
  @translator = Smartdict::Translator.new(
    :from_lang => config.from_lang,
    :to_lang   => config.to_lang,
    :log       => true
  )

  @main_window      = MainWindow.new(self)
  @word_entry       = WordEntry.new(self)
  @translate_button = TranslateButton.new(self)
  @menu_bar         = MenuBar.new(self)
  @text_view        = TextView.new(self)
  @status_icon      = StatusIcon.new(self)
  @status_bar       = StatusBar.new(self)
  @from_lang_combo_box = LangComboBox.new(self, config.from_lang) do |lang|
    @translator.default_opts[:from_lang] = lang
  end
  @to_lang_combo_box = LangComboBox.new(self, config.to_lang) do |lang|
    @translator.default_opts[:to_lang] = lang
  end
  @interchange_button = InterchangeButton.new(self)
  @word_list          = WordList.new(self)

  @db_mutex = Mutex.new

  load_history
end

Instance Attribute Details

#from_lang_combo_boxObject (readonly)

Returns the value of attribute from_lang_combo_box.



10
11
12
# File 'lib/smartdict/gui/controller.rb', line 10

def from_lang_combo_box
  @from_lang_combo_box
end

#interchange_buttonObject (readonly)

Returns the value of attribute interchange_button.



10
11
12
# File 'lib/smartdict/gui/controller.rb', line 10

def interchange_button
  @interchange_button
end

#main_windowObject (readonly)

Returns the value of attribute main_window.



10
11
12
# File 'lib/smartdict/gui/controller.rb', line 10

def main_window
  @main_window
end

Returns the value of attribute menu_bar.



10
11
12
# File 'lib/smartdict/gui/controller.rb', line 10

def menu_bar
  @menu_bar
end

#status_barObject (readonly)

Returns the value of attribute status_bar.



10
11
12
# File 'lib/smartdict/gui/controller.rb', line 10

def status_bar
  @status_bar
end

#status_iconObject (readonly)

Returns the value of attribute status_icon.



10
11
12
# File 'lib/smartdict/gui/controller.rb', line 10

def status_icon
  @status_icon
end

#text_viewObject (readonly)

Returns the value of attribute text_view.



10
11
12
# File 'lib/smartdict/gui/controller.rb', line 10

def text_view
  @text_view
end

#to_lang_combo_boxObject (readonly)

Returns the value of attribute to_lang_combo_box.



10
11
12
# File 'lib/smartdict/gui/controller.rb', line 10

def to_lang_combo_box
  @to_lang_combo_box
end

#translate_buttonObject (readonly)

Returns the value of attribute translate_button.



10
11
12
# File 'lib/smartdict/gui/controller.rb', line 10

def translate_button
  @translate_button
end

#word_entryObject (readonly)

Returns the value of attribute word_entry.



10
11
12
# File 'lib/smartdict/gui/controller.rb', line 10

def word_entry
  @word_entry
end

#word_listObject (readonly)

Returns the value of attribute word_list.



10
11
12
# File 'lib/smartdict/gui/controller.rb', line 10

def word_list
  @word_list
end

Instance Method Details

#export_translations(format_name, file_path, options = {}) ⇒ Object



91
92
93
94
95
96
# File 'lib/smartdict/gui/controller.rb', line 91

def export_translations(format_name, file_path, options = {})
  format = Smartdict::Core::FormatManager.find(format_name)
  translations = Smartdict::Log.fetch(options)
  content = format.format_list(translations)
  File.open(file_path, 'w') { |file| file.write content }
end

#focus_word_entryObject



102
103
104
# File 'lib/smartdict/gui/controller.rb', line 102

def focus_word_entry
  @word_entry.grab_focus
end

#interchange_langsObject



98
99
100
# File 'lib/smartdict/gui/controller.rb', line 98

def interchange_langs
  @to_lang_combo_box.active, @from_lang_combo_box.active = @from_lang_combo_box.active, @to_lang_combo_box.active
end

#open_about_windowObject



83
84
85
# File 'lib/smartdict/gui/controller.rb', line 83

def open_about_window
  AboutWindow.new
end

#open_export_dialogObject



87
88
89
# File 'lib/smartdict/gui/controller.rb', line 87

def open_export_dialog
  ExportDialog.new(self)
end

#quitObject



49
50
51
# File 'lib/smartdict/gui/controller.rb', line 49

def quit
  ::Gtk.main_quit
end

#runObject



43
44
45
46
47
# File 'lib/smartdict/gui/controller.rb', line 43

def run
  @main_window.draw_window
  @main_window.show_all
  Gtk.main
end

#translateObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/smartdict/gui/controller.rb', line 53

def translate
  word      = @word_entry.text.strip.downcase
  from_lang = @translator.default_opts[:from_lang]
  to_lang   = @translator.default_opts[:to_lang]
  status_message = "Looking for \"#{word}\" ..."

  safe_concurrent_run do
    do_with_status(status_message) do
      with_reported_exception do
        if add_to_history?(word, from_lang, to_lang)
          translation = @translator.translate(word)
          add_to_history(translation)
        else
          translation = @translator.translate(word, :log => false)
        end
        @text_view.show_translation(translation)
        @word_list.scroll_up
      end
    end
  end
end

#translate_selected_word(word, from_lang, to_lang) ⇒ Object

Parameters:

  • word (String)
  • from_lang (String)

    language code

  • to_lang (String)

    language code



78
79
80
81
# File 'lib/smartdict/gui/controller.rb', line 78

def translate_selected_word(word, from_lang, to_lang)
  translation = @translator.translate(word, :log => false)
  @text_view.show_translation(translation)
end