You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-console-gui/console/gui/tools.py

499 lines
20 KiB

#-*- coding: utf-8 -*-
# Copyright 2012 Calculate Ltd. http://www.calculate-linux.org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from PySide import QtGui, QtCore
from PySide.QtGui import QComboBox, QColorDialog
from more import LabelWordWrap, FileOpenWgt, show_msg, show_question
class ToolsWidget (QtGui.QWidget):
def __init__(self, parent, ClientObj):
QtGui.QWidget.__init__(self)
self.vlayout = QtGui.QVBoxLayout(self)
self.vlayout.setAlignment(QtCore.Qt.AlignTop)
self.vlayout.setAlignment(QtCore.Qt.AlignRight)
self.move(100+parent.frameGeometry().x(), \
100+parent.frameGeometry().y())
self.setFixedSize(400,300)
self.setWindowTitle(_('Tools'))
self.setWindowIcon(QtGui.QIcon.fromTheme("preferences-other"))
# for clear memory after closed this window
self.setAttribute(QtCore.Qt.WA_ShowModal)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.create(parent, ClientObj)
def create(self, parent, ClientObj):
if self.layout():
for i in range(self.layout().count()):
child = self.layout().takeAt(0)
child.widget().hide()
child.widget().destroy()
del (child)
# Add clear config button
clear_icon = QtGui.QIcon.fromTheme("edit-delete-page")
clear_button = QtGui.QPushButton (clear_icon, _('Clear config'),self)
clear_button.clicked.connect(self.clear_config(parent, ClientObj))
self.vlayout.addWidget(clear_button)
tab = ToolTabWidget(self, ClientObj)
tab.resize(tab.sizeHint())
self.vlayout.addWidget(tab)
# clear_button.setMaximumWidth(self.sizeHint().width() / 2)
clear_button.setMaximumWidth(220)
def clear_config(self, parent, ClientObj):
def wrapper():
try:
# fc = open (ClientObj.user_config, 'w')
# fc.close()
os.remove(ClientObj.user_config)
ClientObj.create_user_config()
ClientObj.read_user_config(ClientObj.user_config)
except Exception, e:
show_msg (e, 'Clear Config Error')
finally:
self.create(parent, ClientObj)
return wrapper
class ToolTabWidget(QtGui.QTabWidget):
def __init__(self, parent, ClientObj):
QtGui.QTabWidget.__init__(self, parent)
self.ClientObj = ClientObj
self.GuiWidget = ToolGui(self, ClientObj)
self.OtherWidget = ToolOther(self, ClientObj)
self.gui_icon = QtGui.QIcon.fromTheme("video-display")
self.other_icon = QtGui.QIcon.fromTheme("preferences-other")
self.addTab(self.GuiWidget, self.gui_icon ,_('Gui Tools'))
self.addTab(self.OtherWidget, self.other_icon, _('Other Tools'))
# message about save
self.cur_tab_num = 0
self.changed_flag = False
self.currentChanged.connect(self.mess)
gui_signal = QtCore.Signal()
other_signal = QtCore.Signal()
def mess(self, tab_num):
tab_list = [_('Gui Tools'),_('Other Tools')]
if self.changed_flag:
text = _('In the %s tab has unsaved changes') \
%tab_list[self.cur_tab_num]
informative_text = _('\tApply them?')
reply = show_question(self.parent(), text, informative_text)
if reply == QtGui.QMessageBox.Yes:
if self.cur_tab_num == 0:
self.gui_signal.connect \
(self.GuiWidget.save_changes(self.ClientObj))
self.gui_signal.emit()
elif self.cur_tab_num == 1:
self.other_signal.connect \
(self.OtherWidget.save_changes(self.ClientObj))
self.other_signal.emit()
self.changed_flag = False
self.removeTab(self.cur_tab_num)
if self.cur_tab_num == 0:
self.GuiWidget = ToolGui(self, self.ClientObj)
self.insertTab(self.cur_tab_num, self.GuiWidget, \
self.gui_icon, _('Gui Tools'))
elif self.cur_tab_num == 1:
self.OtherWidget = ToolOther(self, self.ClientObj)
self.insertTab(self.cur_tab_num, self.OtherWidget, \
self.other_icon, _('Other Tools'))
self.cur_tab_num = tab_num
self.changed_flag = False
# Gui tools in ToolTabWidget
class ToolGui(QtGui.QWidget):
def __init__(self, parent, ClientObj):
QtGui.QWidget.__init__(self, parent)
self.user_config = ClientObj.user_config
self.parent = parent
self.grid = QtGui.QGridLayout(self)
# self.grid.setAlignment(QtCore.Qt.AlignLeft)
# background color settings
self.bg_color = ClientObj.method_background_color
self.color_lbl = LabelWordWrap(_('Select Color'), self)
self.color_lbl.setMaximumWidth(self.color_lbl.sizeHint().width())
color_icon = QtGui.QIcon.fromTheme("fill-color")
self.color_button = QtGui.QPushButton(color_icon, \
ClientObj.method_background_color,self)
# self.color_button.setMaximumWidth (self.color_button.sizeHint().width())
def color_dialog():
def color_select(color):
red = str(color.red())
green = str(color.green())
blue = str(color.blue())
self.bg_color = red + ' ' + green + ' ' + blue
self.color_button.setText(self.bg_color)
self.colordialog = QColorDialog(self)
self.colordialog.setWindowModality(QtCore.Qt.WindowModality.WindowModal)
self.colordialog.colorSelected.connect(color_select)
self.colordialog.show()
self.color_button.clicked.connect(color_dialog)
#add background color settings in grid
self.grid.addWidget(self.color_lbl,0,0)
self.grid.addWidget(self.color_button,0,1)
# add open background image in grid
self.cert_bg_image_lbl = LabelWordWrap(_('Path to bg Image'), self)
self.cert_bg_image_lbl.setMaximumWidth(self.cert_bg_image_lbl.sizeHint().width())
self.fd_bg_image = FileOpenWgt(self, 'file', 'Select Background Image', '~/')
self.grid.addWidget(self.cert_bg_image_lbl, 1, 0)
self.grid.addWidget(self.fd_bg_image, 1, 1)
# repeat background settings
self.repeat_lbl = LabelWordWrap(_('Select repeat background'), self)
self.repeat_lbl.setMaximumWidth(self.repeat_lbl.sizeHint().width())
self.repeat_ComboBox = QComboBox(self)
repeat_dict = {'no-repeat':'no repeat', 'repeat-xy':'repeat for x and y', \
'repeat-x': 'repeat for x','repeat-y': 'repeat for y'}
for repeat in repeat_dict:
self.repeat_ComboBox.addItem(repeat_dict[repeat])
self.repeat_ComboBox.setItemData(self.repeat_ComboBox.count()-1,repeat)
if ClientObj.background_repeat == repeat:
self.repeat_ComboBox.setCurrentIndex(self.repeat_ComboBox.count()-1)
# self.repeat_ComboBox.currentIndexChanged.connect(self.repeat_checked)
#add repeat settings in grid
self.grid.addWidget(self.repeat_lbl,2,0)
self.grid.addWidget(self.repeat_ComboBox,2,1)
# opacity setting
self.opacity_lbl_text = _('Set opacity ')
self.opacity_lbl = LabelWordWrap(self.opacity_lbl_text, self)
self.opacity = QtGui.QSlider(QtCore.Qt.Orientation.Horizontal)
self.opacity.setFocusPolicy(QtCore.Qt.StrongFocus)
self.opacity.setTickPosition(QtGui.QSlider.TicksBothSides)
self.opacity.setTickInterval(20)
self.opacity.setSingleStep(1)
self.opacity.setMaximum(255)
self.opacity.setMinimum(0)
self.opacity.setValue(int(ClientObj.bg_opacity))
self.opacity.valueChanged.connect(self.set_opacity_lbl)
self.set_opacity_lbl(ClientObj.bg_opacity)
#add opacity settings in grid
self.grid.addWidget(self.opacity_lbl,3,0)
self.grid.addWidget(self.opacity,3,1)
# add spacer
self.grid.addItem(QtGui.QSpacerItem( 0, 0, \
QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding ), 4, 0, 1, 2)
# add ok, apply and cancel button
self.botton_layout = QtGui.QHBoxLayout()
self.botton_layout.setAlignment(QtCore.Qt.AlignBottom)
toolOk = QtGui.QPushButton(_('Ok'), self)
toolOk.clicked.connect(self.save_changes(ClientObj))
toolOk.clicked.connect(parent.parent().close)
self.botton_layout.addWidget(toolOk)
toolApply = QtGui.QPushButton(_('Apply'), self)
toolApply.clicked.connect(self.save_changes(ClientObj))
self.botton_layout.addWidget(toolApply)
toolQuit = QtGui.QPushButton(_('Cancel'), self)
toolQuit.clicked.connect(parent.parent().close)
self.botton_layout.addWidget(toolQuit)
self.grid.addLayout(self.botton_layout, 5, 1)
# connect all with change value slot
self.color_button.clicked.connect(self.changed_val)
self.fd_bg_image.textChanged.connect(self.changed_val)
self.repeat_ComboBox.currentIndexChanged.connect(self.changed_val)
self.opacity.valueChanged.connect(self.changed_val)
def changed_val(self):
self.parent.changed_flag = True
def set_opacity_lbl(self, val):
self.opacity_lbl.setText(self.opacity_lbl_text + str(val))
def check_cfg (self, flag, config, part, param, value):
# if param not exists in config
if not flag:
part_flag = False
temp_cfg = []
for line in config:
temp_cfg.append(line)
#add new line in config
if line.startswith(part):
temp_cfg.append('%s = %s\n' %(param, value))
part_flag = True
config = temp_cfg
# if part not exists
if not part_flag:
config.append('\n')
config.append('%s\n' %part)
config.append('%s = %s\n' %(param, value))
return config
def save_changes(self, ClientObj):
def wrapper():
if not os.path.isfile (self.user_config):
f = open (self.user_config, 'w')
f.close()
fc = open (self.user_config, 'r')
config = fc.readlines()
fc.close()
new_config = []
bg_color_flag = False
bg_image_flag = False
bg_opacity_flag = False
repeat_bg_flag = False
for line in config:
if line.startswith('bg_color '):
bg_color_flag = True
new_config.append('bg_color = %s\n' %self.bg_color)
elif line.startswith('bg_image '):
bg_image_flag = True
if self.fd_bg_image.text().lower() == 'not' \
or os.path.isfile(self.fd_bg_image.text()):
new_config.append('bg_image = %s\n' %self.fd_bg_image.text())
else:
new_config.append(line)
elif line.startswith('bg_repeat '):
repeat_bg_flag = True
new_config.append('bg_repeat = %s\n' \
%self.repeat_ComboBox.itemData \
(self.repeat_ComboBox.currentIndex()))
elif line.startswith('bg_opacity '):
bg_opacity_flag = True
new_config.append('bg_opacity = %s\n' % str(self.opacity.value()))
else:
new_config.append(line)
new_config = self.check_cfg (bg_color_flag, new_config, \
'[gui]', 'bg_color', self.bg_color)
if self.fd_bg_image.text().lower() == 'not' \
or os.path.isfile(self.fd_bg_image.text()):
new_config = self.check_cfg (bg_image_flag, new_config, \
'[gui]', 'bg_image', self.fd_bg_image.text())
new_config = self.check_cfg (repeat_bg_flag, new_config, \
'[gui]', 'bg_repeat', self.repeat_ComboBox.itemData \
(self.repeat_ComboBox.currentIndex()))
new_config = self.check_cfg (bg_opacity_flag, new_config, \
'[gui]', 'bg_opacity', str(self.opacity.value()))
fnc = open(self.user_config, 'w')
for line in new_config:
fnc.write(line)
fnc.close()
# save all without closing application
ClientObj.method_background_color = self.bg_color
ClientObj.bg_opacity = self.opacity.value()
if self.fd_bg_image.text().lower() == 'not' \
or os.path.isfile(self.fd_bg_image.text()):
ClientObj.background_image = self.fd_bg_image.text()
return wrapper
# Other tools in ToolTabWidget
class ToolOther(QtGui.QWidget):
def __init__(self, parent, ClientObj):
QtGui.QWidget.__init__(self, parent)
self.user_config = ClientObj.user_config
self.parent = parent
self.grid = QtGui.QGridLayout(self)
# lang settings
self.lang_lbl = LabelWordWrap(_('Select Language'), self)
self.lang_lbl.setMaximumWidth(self.lang_lbl.sizeHint().width())
self.lang_ComboBox = QComboBox(self)
lang_dict = {'en': 'English','ru': 'Russian'}
for lang in lang_dict:
self.lang_ComboBox.addItem(lang_dict[lang])
self.lang_ComboBox.setItemData(self.lang_ComboBox.count()-1,lang)
if ClientObj.lang == lang:
self.lang_ComboBox.setCurrentIndex(self.lang_ComboBox.count()-1)
#add lang settings in grid
self.grid.addWidget(self.lang_lbl,0,0)
self.grid.addWidget(self.lang_ComboBox,0,1)
# add open file in grid
self.cert_path_lbl = LabelWordWrap(_('Path to Certificates'), self)
self.cert_path_lbl.setMaximumWidth(self.cert_path_lbl.sizeHint().width())
self.fd_cert = FileOpenWgt(self, 'dir', 'Certificate Directory', '~/.calculate', \
hidden = True)
self.grid.addWidget(self.cert_path_lbl, 1, 0)
self.grid.addWidget(self.fd_cert, 1, 1)
# add spacer
self.grid.addItem(QtGui.QSpacerItem( 0, 0, \
QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding ), 2, 0, 1, 2)
# add ok, apply and cancel button
self.botton_layout = QtGui.QHBoxLayout()
self.botton_layout.setAlignment(QtCore.Qt.AlignBottom)
toolOk = QtGui.QPushButton(_('Ok'), self)
toolOk.clicked.connect(self.save_changes(ClientObj))
toolOk.clicked.connect(parent.parent().close)
self.botton_layout.addWidget(toolOk)
toolApply = QtGui.QPushButton(_('Apply'), self)
toolApply.clicked.connect(self.save_changes(ClientObj))
self.botton_layout.addWidget(toolApply)
toolQuit = QtGui.QPushButton(_('Cancel'), self)
toolQuit.clicked.connect(parent.parent().close)
self.botton_layout.addWidget(toolQuit)
self.grid.addLayout(self.botton_layout, 3, 1)
# connect all with change value slot
self.lang_ComboBox.currentIndexChanged.connect(self.changed_val)
self.fd_cert.textChanged.connect(self.changed_val)
def changed_val(self):
self.parent.changed_flag = True
def lang_checked(self, ind):
return
print 'Data', self.lang_ComboBox.itemData(ind)
print 'Text', self.lang_ComboBox.itemText(ind)
def check_cfg (self, flag, config, part, param, value):
# if param not exists in config
if not flag:
part_flag = False
temp_cfg = []
for line in config:
temp_cfg.append(line)
#add new line in config
if line.startswith(part):
temp_cfg.append('%s = %s\n' %(param, value))
part_flag = True
config = temp_cfg
# if part not exists
if not part_flag:
config.append('\n')
config.append('%s\n' %part)
config.append('%s = %s\n' %(param, value))
return config
def save_changes(self, ClientObj):
def wrapper():
if not os.path.isfile (self.user_config):
f = open (self.user_config, 'w')
f.close()
fc = open (self.user_config, 'r')
config = fc.readlines()
fc.close()
new_config = []
lang_flag = False
cert_flag = False
for line in config:
if line.startswith('lang '):
lang_flag = True
new_config.append('lang = %s\n' \
%self.lang_ComboBox.itemData \
(self.lang_ComboBox.currentIndex()))
elif line.startswith('path_to_cert '):
cert_flag = True
if self.fd_cert.text().lower() == 'not' or \
os.path.isdir(self.fd_cert.text()):
new_config.append('path_to_cert = %s\n' %self.fd_cert.text())
else:
new_config.append(line)
elif line.startswith('title_color '):
pass
else:
new_config.append(line)
new_config = self.check_cfg (lang_flag, new_config, \
'[other]', 'lang', self.lang_ComboBox.itemData \
(self.lang_ComboBox.currentIndex()))
if self.fd_cert.text().lower() == 'not' or \
os.path.isdir(self.fd_cert.text()):
new_config = self.check_cfg (cert_flag, new_config, \
'[other]', 'path_to_cert', self.fd_cert.text())
fnc = open(self.user_config, 'w')
for line in new_config:
fnc.write(line)
fnc.close()
return wrapper