1# This script is licensed CC 0 1.0, so that you can learn from it.
2
3# ------ CC 0 1.0 ---------------
4
5# The person who associated a work with this deed has dedicated the
6# work to the public domain by waiving all of his or her rights to the
7# work worldwide under copyright law, including all related and
8# neighboring rights, to the extent allowed by law.
9
10# You can copy, modify, distribute and perform the work, even for
11# commercial purposes, all without asking permission.
12
13# https://creativecommons.org/publicdomain/zero/1.0/legalcode
14
15from . import colorspacedialog
16from .components import (
17    colordepthcombobox,
18    colormodelcombobox,
19    colorprofilecombobox,
20)
21from PyQt5.QtCore import Qt
22from PyQt5.QtWidgets import (QFormLayout, QListWidget,
23                             QAbstractItemView, QDialogButtonBox,
24                             QVBoxLayout, QFrame, QMessageBox, QPushButton,
25                             QAbstractScrollArea)
26from PyQt5.QtGui import QIcon
27import krita
28
29
30class UIColorSpace(object):
31
32    def __init__(self):
33        self.mainDialog = colorspacedialog.ColorSpaceDialog()
34        self.mainLayout = QVBoxLayout(self.mainDialog)
35        self.formLayout = QFormLayout()
36        self.documentLayout = QVBoxLayout()
37        self.refreshButton = QPushButton(QIcon(':/icons/refresh.svg'),
38                                         i18n("Refresh"))
39        self.widgetDocuments = QListWidget()
40        self.colorModelComboBox = colormodelcombobox.ColorModelComboBox(self)
41        self.colorDepthComboBox = colordepthcombobox.ColorDepthComboBox(self)
42        self.colorProfileComboBox = \
43            colorprofilecombobox.ColorProfileComboBox(self)
44        self.buttonBox = QDialogButtonBox(
45            QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
46
47        self.kritaInstance = krita.Krita.instance()
48        self.documentsList = []
49        self.colorModelsList = []
50        self.colorDepthsList = []
51        self.colorProfilesList = []
52
53        self.refreshButton.clicked.connect(self.refreshButtonClicked)
54        self.buttonBox.accepted.connect(self.confirmButton)
55        self.buttonBox.rejected.connect(self.mainDialog.close)
56
57        self.mainDialog.setWindowModality(Qt.NonModal)
58        self.widgetDocuments.setSelectionMode(QAbstractItemView.MultiSelection)
59        self.widgetDocuments.setSizeAdjustPolicy(
60            QAbstractScrollArea.AdjustToContents)
61
62    def initialize(self):
63        self.loadDocuments()
64        self.loadColorModels()
65        self.loadColorDepths()
66        self.loadColorProfiles()
67
68        self.documentLayout.addWidget(self.widgetDocuments)
69        self.documentLayout.addWidget(self.refreshButton)
70
71        self.formLayout.addRow(i18n("Documents:"), self.documentLayout)
72        self.formLayout.addRow(i18n("Color model:"), self.colorModelComboBox)
73        self.formLayout.addRow(i18n("Color depth:"), self.colorDepthComboBox)
74        self.formLayout.addRow(i18n("Color profile:"),
75                               self.colorProfileComboBox)
76
77        self.line = QFrame()
78        self.line.setFrameShape(QFrame.HLine)
79        self.line.setFrameShadow(QFrame.Sunken)
80
81        self.mainLayout.addLayout(self.formLayout)
82        self.mainLayout.addWidget(self.line)
83        self.mainLayout.addWidget(self.buttonBox)
84
85        self.mainDialog.resize(500, 300)
86        self.mainDialog.setWindowTitle(i18n("Color Space"))
87        self.mainDialog.setSizeGripEnabled(True)
88        self.mainDialog.show()
89        self.mainDialog.activateWindow()
90
91    def loadColorModels(self):
92        self.colorModelsList = sorted(self.kritaInstance.colorModels())
93
94        self.colorModelComboBox.addItems(self.colorModelsList)
95
96    def loadColorDepths(self):
97        self.colorDepthComboBox.clear()
98
99        colorModel = self.colorModelComboBox.currentText()
100        self.colorDepthsList = sorted(
101            self.kritaInstance.colorDepths(colorModel))
102
103        self.colorDepthComboBox.addItems(self.colorDepthsList)
104
105    def loadColorProfiles(self):
106        self.colorProfileComboBox.clear()
107
108        colorModel = self.colorModelComboBox.currentText()
109        colorDepth = self.colorDepthComboBox.currentText()
110        self.colorProfilesList = sorted(
111            self.kritaInstance.profiles(colorModel, colorDepth))
112
113        self.colorProfileComboBox.addItems(self.colorProfilesList)
114
115    def loadDocuments(self):
116        self.widgetDocuments.clear()
117
118        self.documentsList = [
119            document for document in self.kritaInstance.documents()
120            if document.fileName()
121        ]
122
123        for document in self.documentsList:
124            self.widgetDocuments.addItem(document.fileName())
125
126    def refreshButtonClicked(self):
127        self.loadDocuments()
128
129    def confirmButton(self):
130        selectedPaths = [
131            item.text() for item in self.widgetDocuments.selectedItems()]
132        selectedDocuments = [
133            document for document in self.documentsList
134            for path in selectedPaths if path == document.fileName()
135        ]
136
137        self.msgBox = QMessageBox(self.mainDialog)
138        if selectedDocuments:
139            self.convertColorSpace(selectedDocuments)
140            self.msgBox.setText(
141                i18n("The selected documents has been converted."))
142        else:
143            self.msgBox.setText(i18n("Select at least one document."))
144        self.msgBox.exec_()
145
146    def convertColorSpace(self, documents):
147        for document in documents:
148            document.setColorSpace(self.colorModelComboBox.currentText(),
149                                   self.colorDepthComboBox.currentText(),
150                                   self.colorProfileComboBox.currentText())
151