# -*- coding: utf-8 -*-

# Copyright (c) 2006 - 2007 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the Viewmanager configuration page.
"""

import os

from PyQt4.QtCore import QStringList, pyqtSignature
from PyQt4.QtGui import QPixmap

from ViewManager.Config import ConfigViewManagersDispStrings, ConfigViewManagers

from ConfigurationPageBase import ConfigurationPageBase
from Ui_ViewmanagerPage import Ui_ViewmanagerPage

import Preferences

from eric4config import getConfig

class ViewmanagerPage(ConfigurationPageBase, Ui_ViewmanagerPage):
    """
    Class implementing the Viewmanager configuration page.
    """
    def __init__(self):
        """
        Constructor
        """
        ConfigurationPageBase.__init__(self)
        self.setupUi(self)
        self.setObjectName("ViewmanagerPage")
        
        # set initial values
        self.windowComboBox.clear()
        try:
            currentIndex = ConfigViewManagers.index(Preferences.getViewManager())
        except:
            currentIndex = 0
        viewManagers = QStringList()
        for vm in ConfigViewManagersDispStrings:
            viewManagers.append(self.trUtf8(vm))
        self.windowComboBox.addItems(viewManagers)
        self.windowComboBox.setCurrentIndex(currentIndex)
        self.on_windowComboBox_activated(currentIndex)
        
        tvIndex = ConfigViewManagers.index("tabview")
        self.tabViewGroupBox.setTitle(ConfigViewManagersDispStrings[tvIndex])
        self.filenameLengthSpinBox.setValue(\
            Preferences.getUI("TabViewManagerFilenameLength"))
        
    def save(self):
        """
        Public slot to save the Viewmanager configuration.
        """
        Preferences.setViewManager(\
            ConfigViewManagers[self.windowComboBox.currentIndex()])
        Preferences.setUI("TabViewManagerFilenameLength",
            self.filenameLengthSpinBox.value())
        
    @pyqtSignature("int")
    def on_windowComboBox_activated(self, index):
        """
        Private slot to show a preview of the selected workspace view type.
        
        @param index index of selected workspace view type (integer)
        """
        workspace = ConfigViewManagers[index]
        workspacePixmap = unicode(workspace) + ".png"
        pixmap = QPixmap(os.path.join("pixmaps", workspacePixmap))
        if pixmap.isNull():
            pixmap = QPixmap(os.path.join(getConfig('ericPixDir'), workspacePixmap))
        self.previewPixmap.setPixmap(pixmap)
        self.tabViewGroupBox.setEnabled(workspace == "tabview")
    
def create(dlg):
    """
    Module function to create the configuration page.
    
    @param dlg reference to the configuration dialog
    """
    page = ViewmanagerPage()
    return page
