From 13baf7336d5b628c0a62d7c8832bd3e9eebdd55d Mon Sep 17 00:00:00 2001 From: Amit Date: Tue, 17 Sep 2013 15:21:55 +0600 Subject: [PATCH] New changes! -> now can save settings -> now can load settings as well! -> small fix in UI --- src/init.py | 110 ++++++++++++++++++++++++++++++++++++++- src/ui/mainwindow.py | 6 +-- src/ui/mainwindow.ui | 4 +- src/util/read_config.py | 33 ++++++++++++ src/util/write_config.py | 69 ++++++++++++++++++++++++ 5 files changed, 215 insertions(+), 7 deletions(-) create mode 100644 src/util/read_config.py create mode 100644 src/util/write_config.py diff --git a/src/init.py b/src/init.py index 961fad8..82eca6c 100755 --- a/src/init.py +++ b/src/init.py @@ -9,7 +9,10 @@ from ui.help import Ui_Form as Help_Ui_Form import util.executor as executor import util.check_displays as cdisplay +import util.write_config as w_config +import util.read_config as r_config import sys +from os import path as path class MyApplication(QtGui.QMainWindow): @@ -87,6 +90,8 @@ def connect_handlers(self): user_interface.actionExit.triggered.connect(self.close) user_interface.actionHelp.triggered.connect(self.show_help) user_interface.actionLicense.triggered.connect(self.show_license) + user_interface.actionSave.triggered.connect(self.save_settings) + user_interface.actionLoad.triggered.connect(self.load_settings) def enable_secondary_widgets(self, boolean): ''' @@ -241,8 +246,6 @@ def combo_activated(self, text): self.change_primary_sliders(rgb) if self.no_of_connected_dev == 2: self.change_secondary_sliders(rgb) - #time.sleep(5) - #executor.execute_command('eog troll_face_problem-2555px_2.png') elif text == '2600K 40W Tungsten': rgb = [255, 197, 143] self.change_primary_sliders(rgb) @@ -301,6 +304,7 @@ def change_secondary_sliders(self, rgb): ''' rgb - based on the rgb array, assign values to secondary display sliders and in turn changes secondary display color + rgb is given in array from a range of 0 to 255 ''' slider_r = int((rgb[0] * 100 ) / 255) slider_g = int((rgb[1] * 100 ) / 255) @@ -310,6 +314,36 @@ def change_secondary_sliders(self, rgb): self.ui.secondary_green.setValue(slider_g) self.ui.secondary_blue.setValue(slider_b) + def change_secondary_sliders_in_rgb_0_99(self, br_rgb): + ''' + change slider values in rgb from a range of 0 to 99 value + for secondary monitor slider + ''' + self.ui.secondary_brightness.setValue(br_rgb[0]) + self.ui.secondary_red.setValue(br_rgb[1]) + self.ui.secondary_green.setValue(br_rgb[2]) + self.ui.secondary_blue.setValue(br_rgb[3]) + + def primary_sliders_in_rgb_0_99(self, br_rgb): + ''' + change slider values in rgb from a range of 0 to 99 value + for primary monitor sliders + ''' + self.ui.primary_brightness.setValue(br_rgb[0]) + self.ui.primary_red.setValue(br_rgb[1]) + self.ui.primary_green.setValue(br_rgb[2]) + self.ui.primary_blue.setValue(br_rgb[3]) + + def secondary_sliders_in_rgb_0_99(self, br_rgb): + ''' + change slider values in rgb from a range of 0 to 99 value + for primary monitor sliders + ''' + self.ui.secondary_brightness.setValue(br_rgb[0]) + self.ui.secondary_red.setValue(br_rgb[1]) + self.ui.secondary_green.setValue(br_rgb[2]) + self.ui.secondary_blue.setValue(br_rgb[3]) + def show_about(self): ''' Shows the About widget''' self.about_widget.show() @@ -320,6 +354,78 @@ def show_help(self): ''' Shows the Help Widget''' self.help_widget.show() + def save_settings(self): + ''' save current primary and secondary display settings''' + file_path = QtGui.QFileDialog.getSaveFileName()[0] + if path.exists(file_path): + if self.no_of_connected_dev == 1: + print 'here I am in primary' + w_config.write_primary_display( + self.return_current_primary_settings(), + file_path + ) + elif self.no_of_connected_dev == 2: + print 'here I am in secondary' + w_config.write_both_display( + self.return_current_primary_settings(), + self.return_current_secondary_settings(), + self.ui.checkBox.isChecked(), + file_path + ) + + def load_settings(self): + ''' + Load current primary and secondary display settings + ''' + file_path = QtGui.QFileDialog.getOpenFileName()[0] + if path.exists(file_path): + loaded_settings = r_config.read_configuration(file_path) + if len(loaded_settings) == 4: + self.primary_sliders_in_rgb_0_99(loaded_settings) + elif len(loaded_settings) == 9: + # checks just in case saved settings are for two displays, + # but loads when only one display is connected + if self.no_of_connected_dev == 1: + + self.primary_sliders_in_rgb_0_99((loaded_settings[0], + loaded_settings[1], + loaded_settings[2], + loaded_settings[3])) + return + # sets reverse control + self.ui.checkBox.setChecked(loaded_settings[8]) + self.primary_sliders_in_rgb_0_99((loaded_settings[0], + loaded_settings[1], + loaded_settings[2], + loaded_settings[3])) + self.secondary_sliders_in_rgb_0_99((loaded_settings[4], + loaded_settings[5], + loaded_settings[6], + loaded_settings[7])) + + def return_current_primary_settings(self): + ''' + return p_br_rgb(primary_brightness, + primary_red, primary_green, primary_blue) + ''' + p_br_rgb = [] + p_br_rgb.append(self.ui.primary_brightness.value()) + p_br_rgb.append(self.ui.primary_red.value()) + p_br_rgb.append(self.ui.primary_green.value()) + p_br_rgb.append(self.ui.primary_blue.value()) + return p_br_rgb + def return_current_secondary_settings(self): + ''' + return s_br_rgb(secondary_brightness, + secondary_red, secondary_green, secondary_blue) + ''' + s_br_rgb = [] + s_br_rgb.append(self.ui.secondary_brightness.value()) + s_br_rgb.append(self.ui.secondary_red.value()) + s_br_rgb.append(self.ui.secondary_green.value()) + s_br_rgb.append(self.ui.secondary_blue.value()) + return s_br_rgb + class LicenseForm(QtGui.QWidget): '''License Form widget initialization''' def __init__(self, parent=None): diff --git a/src/ui/mainwindow.py b/src/ui/mainwindow.py index a47d428..e1ceba8 100644 --- a/src/ui/mainwindow.py +++ b/src/ui/mainwindow.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'mainwindow.ui' # -# Created: Wed Aug 28 19:24:43 2013 +# Created: Tue Sep 17 11:56:47 2013 # by: pyside-uic 0.2.14 running on PySide 1.1.2 # # WARNING! All changes made in this file will be lost! @@ -204,7 +204,7 @@ def retranslateUi(self, MainWindow): self.actionHelp.setText(QtGui.QApplication.translate("MainWindow", "&Help", None, QtGui.QApplication.UnicodeUTF8)) self.actionAbout.setText(QtGui.QApplication.translate("MainWindow", "&About", None, QtGui.QApplication.UnicodeUTF8)) self.actionLicense.setText(QtGui.QApplication.translate("MainWindow", "&License", None, QtGui.QApplication.UnicodeUTF8)) - self.actionSave.setText(QtGui.QApplication.translate("MainWindow", "Save Color Profile", None, QtGui.QApplication.UnicodeUTF8)) - self.actionLoad.setText(QtGui.QApplication.translate("MainWindow", "Load Color Profile", None, QtGui.QApplication.UnicodeUTF8)) + self.actionSave.setText(QtGui.QApplication.translate("MainWindow", "&Save current settings", None, QtGui.QApplication.UnicodeUTF8)) + self.actionLoad.setText(QtGui.QApplication.translate("MainWindow", "&Load settings", None, QtGui.QApplication.UnicodeUTF8)) self.actionCheck_for_update.setText(QtGui.QApplication.translate("MainWindow", "&Check for Update", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/src/ui/mainwindow.ui b/src/ui/mainwindow.ui index 6d10465..b32f9c5 100644 --- a/src/ui/mainwindow.ui +++ b/src/ui/mainwindow.ui @@ -401,12 +401,12 @@ - Save Color Profile + &Save current settings - Load Color Profile + &Load settings diff --git a/src/util/read_config.py b/src/util/read_config.py new file mode 100644 index 0000000..a253e32 --- /dev/null +++ b/src/util/read_config.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +import ConfigParser + +def read_configuration(file_path): + ''' + reads configuration from given file path + For two displays: + return (p_brightness, p_red, p_green, p_blue, + s_brightness, s_red, s_green, s_blue, s_reversed) + for one display: + return (p_brightness, p_red, p_green, p_blue) + ''' + config = ConfigParser.RawConfigParser() + config.read(file_path) + p_brightness = config.getint('primary', 'brightness') + p_red = config.getint('primary', 'red') + p_green = config.getint('primary', 'green') + p_blue = config.getint('primary', 'blue') + + if config.getboolean('primary', 'has_secondary'): + s_brightness = config.getint('secondary', 'brightness') + s_red = config.getint('secondary', 'red') + s_green = config.getint('secondary', 'green') + s_blue = config.getint('secondary', 'blue') + s_reversed = config.getboolean('secondary', 'reversed') + print (p_brightness, p_red, p_green, p_blue, + s_brightness, s_red, s_green, s_blue, s_reversed) + return (p_brightness, p_red, p_green, p_blue, + s_brightness, s_red, s_green, s_blue, s_reversed) + else: + return (p_brightness, p_red, p_green, p_blue) + \ No newline at end of file diff --git a/src/util/write_config.py b/src/util/write_config.py new file mode 100644 index 0000000..7eab6f9 --- /dev/null +++ b/src/util/write_config.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python + +import ConfigParser + +def write_primary_display(p_br_rgb, path): + ''' + writes the configuration file as set in brightness controller + p_br_rgb - (int primary_brightness, int primary_red, + int primary_green, int primary_blue) + ''' + config = ConfigParser.RawConfigParser() + config.add_section('primary') + config.set('primary', 'has_secondary', False) + if p_br_rgb is None: + config.set('primary', 'brightness', 99) + config.set('primary', 'red', 99) + config.set('primary', 'green', 99) + config.set('primary', 'blue', 99) + else: + config.set('primary', 'brightness', p_br_rgb[0]) + config.set('primary', 'red', p_br_rgb[1]) + config.set('primary', 'green', p_br_rgb[2]) + config.set('primary', 'blue', p_br_rgb[3]) + + with open(path, 'wb') as configfile: + config.write(configfile) + +def write_both_display(p_br_rgb, s_br_rgb, is_control_reversed, path): + ''' + writes the configuration file as set in brightness controller + p_br_rgb - (int primary_brightness, int primary_red, + int primary_green, int primary_blue) + s_br_rgb - (int secondary_brightness, int secondary_red, + int secondary_green, int secondary_blue) + is_control_reversed - returns if reverse control was checked + path - the save file path + ''' + config = ConfigParser.RawConfigParser() + config.add_section('primary') + config.set('primary', 'has_secondary', True) + if p_br_rgb is None: + print 'p_br_rgb is none' + config.set('primary', 'brightness', 99) + config.set('primary', 'red', 99) + config.set('primary', 'green', 99) + config.set('primary', 'blue', 99) + else: + print 'p_br_rgb is not none' + config.set('primary', 'brightness', p_br_rgb[0]) + config.set('primary', 'red', p_br_rgb[1]) + config.set('primary', 'green', p_br_rgb[2]) + config.set('primary', 'blue', p_br_rgb[3]) + config.add_section('secondary') + if s_br_rgb is None: + print 's_br_rgb is none' + config.set('secondary', 'brightness', 99) + config.set('secondary', 'red', 99) + config.set('secondary', 'green', 99) + config.set('secondary', 'blue', 99) + else: + print 's_br_rgb is not none' + config.set('secondary', 'brightness', s_br_rgb[0]) + config.set('secondary', 'red', s_br_rgb[1]) + config.set('secondary', 'green', s_br_rgb[2]) + config.set('secondary', 'blue', s_br_rgb[3]) + config.set('secondary', 'reversed', is_control_reversed) + with open(path, 'wb') as configfile: + config.write(configfile) + \ No newline at end of file