#!/usr/bin/env python

#############################################################################
##
## Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
##
## This file is part of the example classes of the Qt Toolkit.
##
## This file may be used under the terms of the GNU General Public
## License version 2.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of
## this file.  Please review the following information to ensure GNU
## General Public Licensing requirements will be met:
## http://www.trolltech.com/products/qt/opensource.html
##
## If you are unsure which license is appropriate for your use, please
## review the following information:
## http://www.trolltech.com/products/qt/licensing.html or contact the
## sales department at sales@trolltech.com.
##
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
##
#############################################################################

import sys
from PyQt4 import QtCore, QtGui

import configdialog_rc


class ConfigurationPage(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        configGroup = QtGui.QGroupBox(self.tr("Server configuration"))

        serverLabel = QtGui.QLabel(self.tr("Server:"))
        serverCombo = QtGui.QComboBox()
        serverCombo.addItem(self.tr("Trolltech (Australia)"))
        serverCombo.addItem(self.tr("Trolltech (Norway)"))
        serverCombo.addItem(self.tr("Trolltech (People's Republic of China)"))
        serverCombo.addItem(self.tr("Trolltech (USA)"))
    
        serverLayout = QtGui.QHBoxLayout()
        serverLayout.addWidget(serverLabel)
        serverLayout.addWidget(serverCombo)
    
        configLayout = QtGui.QVBoxLayout()
        configLayout.addLayout(serverLayout)
        configGroup.setLayout(configLayout)
    
        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(configGroup)
        mainLayout.addStretch(1)

        self.setLayout(mainLayout)


class UpdatePage(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        
        updateGroup = QtGui.QGroupBox(self.tr("Package selection"))
        systemCheckBox = QtGui.QCheckBox(self.tr("Update system"))
        appsCheckBox = QtGui.QCheckBox(self.tr("Update applications"))
        docsCheckBox = QtGui.QCheckBox(self.tr("Update documentation"))
    
        packageGroup = QtGui.QGroupBox(self.tr("Existing packages"))
    
        packageList = QtGui.QListWidget()
        qtItem = QtGui.QListWidgetItem(packageList)
        qtItem.setText(self.tr("Qt"))
        qsaItem = QtGui.QListWidgetItem(packageList)
        qsaItem.setText(self.tr("QSA"))
        teamBuilderItem = QtGui.QListWidgetItem(packageList)
        teamBuilderItem.setText(self.tr("Teambuilder"))
    
        startUpdateButton = QtGui.QPushButton(self.tr("Start update"))
    
        updateLayout = QtGui.QVBoxLayout()
        updateLayout.addWidget(systemCheckBox)
        updateLayout.addWidget(appsCheckBox)
        updateLayout.addWidget(docsCheckBox)
        updateGroup.setLayout(updateLayout)
    
        packageLayout = QtGui.QVBoxLayout()
        packageLayout.addWidget(packageList)
        packageGroup.setLayout(packageLayout)
    
        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(updateGroup)
        mainLayout.addWidget(packageGroup)
        mainLayout.addSpacing(12)
        mainLayout.addWidget(startUpdateButton)
        mainLayout.addStretch(1)

        self.setLayout(mainLayout)


class QueryPage(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        
        packagesGroup = QtGui.QGroupBox(self.tr("Look for packages"))

        nameLabel = QtGui.QLabel(self.tr("Name:"))
        nameEdit = QtGui.QLineEdit()
    
        dateLabel = QtGui.QLabel(self.tr("Released after:"))
        dateEdit = QtGui.QDateTimeEdit(QtCore.QDate.currentDate())
    
        releasesCheckBox = QtGui.QCheckBox(self.tr("Releases"))
        upgradesCheckBox = QtGui.QCheckBox(self.tr("Upgrades"))
    
        hitsSpinBox = QtGui.QSpinBox()
        hitsSpinBox.setPrefix(self.tr("Return up to "))
        hitsSpinBox.setSuffix(self.tr(" results"))
        hitsSpinBox.setSpecialValueText(self.tr("Return only the first result"))
        hitsSpinBox.setMinimum(1)
        hitsSpinBox.setMaximum(100)
        hitsSpinBox.setSingleStep(10)
    
        startQueryButton = QtGui.QPushButton(self.tr("Start query"))
    
        packagesLayout = QtGui.QGridLayout()
        packagesLayout.addWidget(nameLabel, 0, 0)
        packagesLayout.addWidget(nameEdit, 0, 1)
        packagesLayout.addWidget(dateLabel, 1, 0)
        packagesLayout.addWidget(dateEdit, 1, 1)
        packagesLayout.addWidget(releasesCheckBox, 2, 0)
        packagesLayout.addWidget(upgradesCheckBox, 3, 0)
        packagesLayout.addWidget(hitsSpinBox, 4, 0, 1, 2)
        packagesGroup.setLayout(packagesLayout)
    
        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(packagesGroup)
        mainLayout.addSpacing(12)
        mainLayout.addWidget(startQueryButton)
        mainLayout.addStretch(1)

        self.setLayout(mainLayout)


class ConfigDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)

        self.contentsWidget = QtGui.QListWidget()
        self.pagesWidget = QtGui.QStackedWidget()
        
        self.contentsWidget.setViewMode(QtGui.QListView.IconMode)
        self.contentsWidget.setIconSize(QtCore.QSize(96, 84))
        self.contentsWidget.setMovement(QtGui.QListView.Static)
        self.contentsWidget.setMaximumWidth(128)
        self.contentsWidget.setSpacing(12)
    
        self.pagesWidget.addWidget(ConfigurationPage())
        self.pagesWidget.addWidget(UpdatePage())
        self.pagesWidget.addWidget(QueryPage())
    
        closeButton = QtGui.QPushButton(self.tr("Close"))
    
        self.createIcons()
        self.contentsWidget.setCurrentRow(0)
    
        self.connect(closeButton, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("close()"))
    
        horizontalLayout = QtGui.QHBoxLayout()
        horizontalLayout.addWidget(self.contentsWidget)
        horizontalLayout.addWidget(self.pagesWidget, 1)
    
        buttonsLayout = QtGui.QHBoxLayout()
        buttonsLayout.addStretch(1)
        buttonsLayout.addWidget(closeButton)
    
        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addLayout(horizontalLayout)
        mainLayout.addStretch(1)
        mainLayout.addSpacing(12)
        mainLayout.addLayout(buttonsLayout)
        
        self.setLayout(mainLayout)
    
        self.setWindowTitle(self.tr("Config Dialog"))

    def changePage(self, current, previous):
        if not current:
            current = previous

        self.pagesWidget.setCurrentIndex(self.contentsWidget.row(current))

    def createIcons(self):
        configButton = QtGui.QListWidgetItem(self.contentsWidget)
        configButton.setIcon(QtGui.QIcon("images/config.png"))
        configButton.setText(self.tr("Configuration"))
        configButton.setTextAlignment(QtCore.Qt.AlignHCenter)
        configButton.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
    
        updateButton = QtGui.QListWidgetItem(self.contentsWidget)
        updateButton.setIcon(QtGui.QIcon("images/update.png"))
        updateButton.setText(self.tr("Update"))
        updateButton.setTextAlignment(QtCore.Qt.AlignHCenter)
        updateButton.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
    
        queryButton = QtGui.QListWidgetItem(self.contentsWidget)
        queryButton.setIcon(QtGui.QIcon("images/query.png"))
        queryButton.setText(self.tr("Query"))
        queryButton.setTextAlignment(QtCore.Qt.AlignHCenter)
        queryButton.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
    
        self.connect(self.contentsWidget, 
                     QtCore.SIGNAL("currentItemChanged(QListWidgetItem *, QListWidgetItem *)"), self.changePage)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    dialog = ConfigDialog()
    sys.exit(dialog.exec_())    
