#!/usr/bin/env python

import os

from PySide import QtCore, QtGui
from PySide.QtGui import QApplication, QMainWindow
from PySide.QtCore import Qt, QSettings, QCoreApplication, QLocale

from juma.qt.helpers import restrainWidgetToScreen

from juma.core import signals
from juma.qt.controls.Menu import MenuManager

def getWindowScreenId(window):
    desktop=QtGui.QApplication.desktop()
    return desktop.screenNumber(window)
    
def moveWindowToCenter(window):
    desktop=QtGui.QApplication.desktop()
    geom=desktop.availableGeometry(window)
    x=(geom.width()-window.width())/2 +geom.x()
    y=(geom.height()-window.height())/2+geom.y()
    window.move(x,y)

##----------------------------------------------------------------##
class MainWindow( QMainWindow ):
    tabChanged = QtCore.Signal( QtGui.QWidget )
    tabRemoved = QtCore.Signal( QtGui.QWidget )

    def __init__(self, parent=None, script=None):
        super(MainWindow, self).__init__(parent)

        self.setWindowTitle( 'JUMA' )

        self.setBaseSize( 800, 600 )
        self.resize( 800, 600 )

        self.setUnifiedTitleAndToolBarOnMac( False )
        self.setDockOptions( QtGui.QMainWindow.AllowNestedDocks | QtGui.QMainWindow.AllowTabbedDocks )
        self.setDockNestingEnabled(True)
        self.setWindowModality(QtCore.Qt.NonModal)
        self.setMouseTracking(True)
        self.setFocusPolicy(QtCore.Qt.ClickFocus)

        self.defaultToolBarIconSize = 32
        font=QtGui.QFont()
        font.setPointSize(11)
        self.setFont(font)

        self.centerTabWidget = QtGui.QTabWidget( None )
        self.centerTabWidget.setTabsClosable( True )
        self.centerTabWidget.setMovable( True )

        self.setCentralWidget( self.centerTabWidget )

        self.centerTabWidget.currentChanged.connect( self.onDocumentTabChanged )
        self.centerTabWidget.tabCloseRequested.connect( self.onTabCloseRequested )


    def moveToCenter(self):
        moveWindowToCenter( self )

    def ensureVisible(self):
        restrainWidgetToScreen( self )

    def requestToolBar( self, name, **options ):
        toolbar = QtGui.QToolBar()
        toolbar.setFloatable( options.get( 'floatable', False ) )
        toolbar.setMovable(   options.get( 'movable',   True ) )        
        toolbar.setObjectName( 'toolbar-%s' % name )