1#############################################################################
2##
3## Copyright (C) 2010 Hans-Peter Jansen <hpj@urpla.net>.
4## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5## All rights reserved.
6##
7## This file is part of the examples of PyQt.
8##
9## $QT_BEGIN_LICENSE:BSD$
10## You may use this file under the terms of the BSD license as follows:
11##
12## "Redistribution and use in source and binary forms, with or without
13## modification, are permitted provided that the following conditions are
14## met:
15##   * Redistributions of source code must retain the above copyright
16##     notice, this list of conditions and the following disclaimer.
17##   * Redistributions in binary form must reproduce the above copyright
18##     notice, this list of conditions and the following disclaimer in
19##     the documentation and/or other materials provided with the
20##     distribution.
21##   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22##     the names of its contributors may be used to endorse or promote
23##     products derived from this software without specific prior written
24##     permission.
25##
26## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37## $QT_END_LICENSE$
38##
39###########################################################################
40
41
42from PyQt5.QtCore import pyqtSlot, QFile, QRegExp, Qt, QTextStream
43from PyQt5.QtWidgets import (QApplication, QDialog, QFileDialog, QMessageBox,
44        QStyleFactory)
45
46from ui_stylesheeteditor import Ui_StyleSheetEditor
47
48
49class StyleSheetEditor(QDialog):
50    def __init__(self, parent=None):
51        super(StyleSheetEditor, self).__init__(parent)
52
53        self.ui = Ui_StyleSheetEditor()
54        self.ui.setupUi(self)
55
56        regExp = QRegExp(r'.(.*)\+?Style')
57        defaultStyle = QApplication.style().metaObject().className()
58        if regExp.exactMatch(defaultStyle):
59            defaultStyle = regExp.cap(1)
60
61        self.ui.styleCombo.addItems(QStyleFactory.keys())
62        self.ui.styleCombo.setCurrentIndex(
63                self.ui.styleCombo.findText(defaultStyle, Qt.MatchContains))
64
65        self.ui.styleSheetCombo.setCurrentIndex(
66                self.ui.styleSheetCombo.findText('Coffee'))
67
68        self.loadStyleSheet('Coffee')
69
70    @pyqtSlot(str)
71    def on_styleCombo_activated(self, styleName):
72        QApplication.setStyle(styleName)
73        self.ui.applyButton.setEnabled(False)
74
75    @pyqtSlot(str)
76    def on_styleSheetCombo_activated(self, sheetName):
77        self.loadStyleSheet(sheetName)
78
79    def on_styleTextEdit_textChanged(self):
80        self.ui.applyButton.setEnabled(True)
81
82    def on_applyButton_clicked(self):
83        QApplication.instance().setStyleSheet(
84                self.ui.styleTextEdit.toPlainText())
85        self.ui.applyButton.setEnabled(False)
86
87    def on_saveButton_clicked(self):
88        fileName, _ = QFileDialog.getSaveFileName(self)
89        if fileName:
90            self.saveStyleSheet(fileName)
91
92    def loadStyleSheet(self, sheetName):
93        file = QFile(':/qss/%s.qss' % sheetName.lower())
94        file.open(QFile.ReadOnly)
95
96        styleSheet = file.readAll()
97        try:
98            # Python v2.
99            styleSheet = unicode(styleSheet, encoding='utf8')
100        except NameError:
101            # Python v3.
102            styleSheet = str(styleSheet, encoding='utf8')
103
104        self.ui.styleTextEdit.setPlainText(styleSheet)
105        QApplication.instance().setStyleSheet(styleSheet)
106        self.ui.applyButton.setEnabled(False)
107
108    def saveStyleSheet(self, fileName):
109        styleSheet = self.ui.styleTextEdit.toPlainText()
110        file = QFile(fileName)
111        if file.open(QFile.WriteOnly):
112            QTextStream(file) << styleSheet
113        else:
114            QMessageBox.information(self, "Unable to open file",
115                    file.errorString())
116