module MyFUNCTIONS app = Application.instance class MyKEYBINDING_IMPOR_EXPORT def initialize(app) @app = app @mw = @app.main_window @menu = @mw.menu end def setUp() root = File.dirname(File.dirname(__FILE__)) + "/KEYBOARD_SHORTCUTS" puts root icon_file = root + "/PIC.png" icon = File.expand_path(icon_file) #################################################################################################### # # MENU ITEM DEFINITIONS # #################################################################################################### # TOOLBAR ITEM 1 tooltip = "----------------------------------------------------------------------\n" tooltip += "Keyboard Shortcuts Import/Expor module\n" tooltip += "----------------------------------------------------------------------\n" tooltip += "Export/Import Keyboard Shortcuts\n\n" tooltip += "Default directory is " + root @action = RBA::Action.new @action.title = "Keyboard" @action.tool_tip = tooltip @action.icon = icon @action.icon_text = "ShortCuts" @action.on_triggered do puts "Keyboard" # qt_main_window = RBA::QApplication.topLevelWidgets.select { |w| w.class.to_s == "RBA::MainWindow" } [0] myDialog = KeyboardDialog .new(nil, root) puts "class created" myDialog.show end # triggered do puts "action defined" @menu.insert_item("@toolbar.end","KEYBOARD_Action",@action) end end class KeyboardDialog < RBA::QDialog def initialize(parent = nil, root) @root = root @inFile = '' @kb_data = [] font_l = RBA::QFont.new( 'Times', 12, RBA::QFont::Bold ) font_s = RBA::QFont.new( 'Times', 10, RBA::QFont::Bold ) font_s2 = RBA::QFont.new( 'Times', 8, RBA::QFont::Bold ) font_an = RBA::QFont.new( 'Times', 12, RBA::QFont::Bold ) font_cb = RBA::QFont.new('Consolas', 8, RBA::QFont::Bold ) font_an.setBold(false) font_an.setItalic(true) super(parent) setWindowTitle("IMPORT/EXPORT") resize(400, 120) layout = RBA::QVBoxLayout.new setLayout(layout) @image = RBA::QLabel.new("Keyboard Shortcuts IMPORT/EXPORT", self) @image.setFont(font_l) layout.addWidget(@image) button_import = RBA::QPushButton.new('IMPORT', self) button_export = RBA::QPushButton.new('EXPORT', self) button_import.setFont(font_l) button_export.setFont(font_l) layout.addWidget(button_import) layout.addWidget(button_export) resize_layout(layout) #################################################################### # Listeners #################################################################### button_import.clicked do import_all_settings() puts "=========================================DONE=========================================" self.destroy end button_export.clicked do export_all_settings() # show_config() puts "=========================================DONE=========================================" self.destroy end self.rejected do puts "=========================================REJECTED=========================================" end #################################################################### end #################################################################### #################################################################### #################################################################### def import_all_settings() thisFile = RBA::FileDialog::ask_open_file_name("Load kbg File", @root, "File (*.kbg)") if thisFile!=nil @kb_data = '' @inFile = thisFile openFile = File.open(@inFile, "r") openFile.each_line do |line| @kb_data += line #puts data end openFile.close if @kb_data!=nil RBA::Application.instance.set_config('key-bindings', @kb_data) show_config(true) end end end #################################################################### def show_config(showAll=false) config_names = RBA::Application.instance.get_config_names config_keybindings_str = RBA::Application.instance.get_config('key-bindings') if config_names!=nil if config_names.length>0 puts "====================== CONFIG NAMES ======================" config_names.each do |config_name| # puts "NAME = " + config_name + ", VALUE = #{RBA::Application.instance.get_config(config_name)}" end end end if config_keybindings_str!=nil if config_keybindings_str.length>0 config_keybindings = config_keybindings_str.split(';') # Print all current ones out config_keybindings.each do |keybinding| keybinding_pair = keybinding.split(':') thisName = keybinding_pair[0] thisValue = keybinding_pair[1] if showAll thisPair = "#{thisName}, #{thisValue}" puts thisPair elsif !thisValue.empty? and !thisName.empty? thisPair = "#{thisName.gsub("'","")}, #{thisValue.gsub("'","")}" puts thisPair end end end end end #################################################################### def export_all_settings() thisFile = RBA::FileDialog::ask_save_file_name("Load CSV File", @root, "File (*.kbg)") if thisFile!=nil @inFile = thisFile puts @inFile config_keybindings_str = RBA::Application.instance.get_config('key-bindings') myFile = File.new(thisFile,File::CREAT|File::TRUNC|File::RDWR, 0644) myFile.write(config_keybindings_str); myFile.close export_all_settings_csv(@inFile + ".csv", config_keybindings_str) end end #################################################################### def export_all_settings_csv(thisFile, config_keybindings_str) if thisFile!=nil puts thisFile csvData = '' config_keybindings = config_keybindings_str.split(';') # Print all current ones out config_keybindings.each do |keybinding| keybinding_pair = keybinding.split(':') thisName = keybinding_pair[0] thisValue = keybinding_pair[1] if !thisValue.empty? and !thisName.empty? thisPair = "#{thisName.gsub("'","")}, #{thisValue.gsub("'","")}" puts thisPair csvData += thisPair + "\n" end end myFile = File.new(thisFile,File::CREAT|File::TRUNC|File::RDWR, 0644) myFile.write(csvData); myFile.close end end #################################################################### def resize_layout(layout) w = layout.sizeHint().width(); h = layout.sizeHint().height() resize(w,h) temp_window = RBA::QWindow.new() screen = temp_window.screen(); screenSize = screen.size() sW = screenSize.width() ; sH = screenSize.height() temp_window.close() puts "screenW = #{sW}, screenH = #{sH}" yNew = (sH - h)/2 setGeometry(10,yNew,w,h) end end $kb = MyKEYBINDING_IMPOR_EXPORT.new(app) if !defined?($kb) $kb = MyKEYBINDING_IMPOR_EXPORT.new(app) if $kb==nil $kb.setUp() end #module