#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#       calmanager.py
#       
#       Copyright 2011 Zonov Valerij <corvinalive@list.ru>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
   
import struct, sys, os, logging
from PySide import QtCore, QtGui

sys.path.append(os.path.join(sys.path[0],"modules"))

from mymainwindow import Ui_MyMainWindow
from temp_unit import TempForm
from press_unit import PressForm
from options_unit import OptionsForm
from update_unit import UpdateForm
import commondata

class MyMainWindow(QtGui.QMainWindow):
           
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.ui = Ui_MyMainWindow()
        self.ui.setupUi(self)
        self.setCentralWidget(self.ui.mdiArea);
        self.connect(self.ui.Tempaction, QtCore.SIGNAL("triggered()"), self.pushButtonTemp)
        self.connect(self.ui.Pressaction, QtCore.SIGNAL("triggered()"), self.pushButtonPress)
        self.connect(self.ui.Aboutaction, QtCore.SIGNAL("triggered()"), self.aboutButton)
        self.connect(self.ui.Setupaction, QtCore.SIGNAL("triggered()"), self.setupButton)
        self.connect(self.ui.Updateaction, QtCore.SIGNAL("triggered()"), self.updateButton)
        self.Commondata = commondata.Commondata()
        self.setWindowTitle(QtGui.QApplication.translate("MyMainWindow", "CalManager "+self.Commondata.version, None, QtGui.QApplication.UnicodeUTF8))
      
    def pushButtonTemp(self):
        tempform1 = TempForm(self.Commondata,self)
        self.ui.mdiArea.addSubWindow(tempform1)
        tempform1.show()
    def pushButtonPress(self):
        pressform1 = PressForm(self.Commondata,self)
        self.ui.mdiArea.addSubWindow(pressform1)
        pressform1.show()
    def aboutButton(self):
        QtGui.QMessageBox.about(self, u"Поверка датчиков давления и температуры",
                u"Программа для обработки данных и формирования свидетельств и протоколов поверки датчиков давления и \
                температуры.\n\nАвтор: Зонов В. М.\n\ne-mail: corvinalive@list.ru\n\nЛицензия: GPL v.2\n\n\
                Исходники: https://corvinalive@github.com/corvinalive/CalManager.git\n\nВерсия: "+self.Commondata.version)
    def setupButton(self):
        of = OptionsForm(self.Commondata,self)
        of.Prepare()
        of.exec_()
            
    def updateButton(self):
        of = UpdateForm(self.Commondata,None)
        #of.Prepare()
        of.exec_()

def str_mode_to_int(mode):
    l = len(mode)
    stepen = 1
    value = 0
    for i in range(l):
        q=int(mode[l-i-1])
        value+=q*stepen
        stepen = stepen*8
    return value
    
def CreateDesktop(Commondata):
    f = open('Calmanager.desktop','wb')
    f.write(u'[Desktop Entry]\n')
    f.write(u'Version='+str(Commondata.version)+u'\n')
    f.write(u'Encoding=UTF-8\n')
    f.write(u'Name=Calmanager\n')
    f.write(u'Type=Application\n')
    ee= os.path.join(unicode(sys.path[0],"utf-8"),u'calmanager')
    logging.debug(ee)
    zz=u'Exec=python \"'+ee+'\"\n'
    logging.debug(zz)
    f.write(zz.encode('utf-8'))
    zz=u'Path='+Commondata.apppath+'\n'
    f.write(zz.encode('utf-8'))
    zz='Icon='+os.path.join(Commondata.apppath,'modules/document_4244.ico\n')
    f.write(zz.encode('utf-8'))
    f.write(u'X-GNOME-DocPath=\n')
    f.write(u'Terminal=false\n')
    f.write(u'Comment=CalManager\n')
    f.write(u'Name[ru]=Calmanager\n')
    f.close()
    os.chmod('Calmanager.desktop',str_mode_to_int('744'))


def main():
    sys.path.append(os.path.join(sys.path[0],"modules"))
       
    app = QtGui.QApplication(sys.argv)
    myapp = MyMainWindow()

    if sys.platform.startswith('win')==False :
        CreateDesktop(myapp.Commondata)
            
    myapp.setWindowIcon(QtGui.QIcon("modules/document_4244.ico"))
    myapp.show()
    result=app.exec_()
    sys.exit(result)
    return 0

if __name__ == '__main__':
	main()

