eric6/QScintilla/MarkupProviders/ImageMarkupDialog.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2017 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to enter data for an image markup.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import pyqtSlot, QSize
13 from PyQt5.QtGui import QImage, QImageReader
14 from PyQt5.QtWidgets import QDialog, QDialogButtonBox
15
16 from E5Gui.E5PathPicker import E5PathPickerModes
17
18 from .Ui_ImageMarkupDialog import Ui_ImageMarkupDialog
19
20
21 class ImageMarkupDialog(QDialog, Ui_ImageMarkupDialog):
22 """
23 Class implementing a dialog to enter data for an image markup.
24 """
25 HtmlMode = 0
26 MarkDownMode = 1
27 RestMode = 2
28
29 def __init__(self, mode, parent=None):
30 """
31 Constructor
32
33 @param mode mode of the dialog
34 @type int
35 @param parent reference to the parent widget
36 @type QWidget
37 """
38 super(ImageMarkupDialog, self).__init__(parent)
39 self.setupUi(self)
40
41 if mode == ImageMarkupDialog.MarkDownMode:
42 self.sizeCheckBox.setEnabled(False)
43 self.aspectRatioCheckBox.setEnabled(False)
44 self.widthSpinBox.setEnabled(False)
45 self.heightSpinBox.setEnabled(False)
46 elif mode == ImageMarkupDialog.RestMode:
47 self.titleEdit.setEnabled(False)
48
49 self.__mode = mode
50 self.__originalImageSize = QSize()
51
52 filters = {
53 'bmp': self.tr("Windows Bitmap File (*.bmp)"),
54 'cur': self.tr("Windows Cursor File (*.cur)"),
55 'dds': self.tr("DirectDraw-Surface File (*.dds)"),
56 'gif': self.tr("Graphic Interchange Format File (*.gif)"),
57 'icns': self.tr("Apple Icon File (*.icns)"),
58 'ico': self.tr("Windows Icon File (*.ico)"),
59 'jp2': self.tr("JPEG2000 File (*.jp2)"),
60 'jpg': self.tr("JPEG File (*.jpg)"),
61 'jpeg': self.tr("JPEG File (*.jpeg)"),
62 'mng': self.tr("Multiple-Image Network Graphics File (*.mng)"),
63 'pbm': self.tr("Portable Bitmap File (*.pbm)"),
64 'pcx': self.tr("Paintbrush Bitmap File (*.pcx)"),
65 'pgm': self.tr("Portable Graymap File (*.pgm)"),
66 'png': self.tr("Portable Network Graphics File (*.png)"),
67 'ppm': self.tr("Portable Pixmap File (*.ppm)"),
68 'sgi': self.tr("Silicon Graphics Image File (*.sgi)"),
69 'svg': self.tr("Scalable Vector Graphics File (*.svg)"),
70 'svgz': self.tr("Compressed Scalable Vector Graphics File"
71 " (*.svgz)"),
72 'tga': self.tr("Targa Graphic File (*.tga)"),
73 'tif': self.tr("TIFF File (*.tif)"),
74 'tiff': self.tr("TIFF File (*.tiff)"),
75 'wbmp': self.tr("WAP Bitmap File (*.wbmp)"),
76 'webp': self.tr("WebP Image File (*.webp)"),
77 'xbm': self.tr("X11 Bitmap File (*.xbm)"),
78 'xpm': self.tr("X11 Pixmap File (*.xpm)"),
79 }
80
81 inputFormats = []
82 readFormats = QImageReader.supportedImageFormats()
83 for readFormat in readFormats:
84 try:
85 inputFormats.append(filters[bytes(readFormat).decode()])
86 except KeyError:
87 pass
88 inputFormats.sort()
89 inputFormats.append(self.tr("All Files (*)"))
90 if filters["png"] in inputFormats:
91 inputFormats.remove(filters["png"])
92 inputFormats.insert(0, filters["png"])
93 self.imagePicker.setFilters(';;'.join(inputFormats))
94 self.imagePicker.setMode(E5PathPickerModes.OpenFileMode)
95
96 self.sizeCheckBox.setChecked(True)
97 self.aspectRatioCheckBox.setChecked(True)
98
99 msh = self.minimumSizeHint()
100 self.resize(max(self.width(), msh.width()), msh.height())
101
102 self.__updateOkButton()
103
104 def __updateOkButton(self):
105 """
106 Private slot to set the state of the OK button.
107 """
108 enable = bool(self.imagePicker.text())
109 if self.__mode == ImageMarkupDialog.MarkDownMode:
110 enable = enable and bool(self.altTextEdit.text())
111
112 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enable)
113
114 @pyqtSlot(str)
115 def on_imagePicker_textChanged(self, address):
116 """
117 Private slot handling changes of the image path.
118
119 @param address image address (URL or local path)
120 @type str
121 """
122 if address and "://" not in address:
123 image = QImage(address)
124 # load the file to set the size spin boxes
125 if image.isNull():
126 self.widthSpinBox.setValue(0)
127 self.heightSpinBox.setValue(0)
128 self.__originalImageSize = QSize()
129 self.__aspectRatio = 1
130 else:
131 self.widthSpinBox.setValue(image.width())
132 self.heightSpinBox.setValue(image.height())
133 self.__originalImageSize = image.size()
134 self.__aspectRatio = \
135 float(self.__originalImageSize.height()) / \
136 self.__originalImageSize.width()
137 else:
138 self.widthSpinBox.setValue(0)
139 self.heightSpinBox.setValue(0)
140 self.__originalImageSize = QSize()
141 self.__aspectRatio = 1
142
143 self.__updateOkButton()
144
145 @pyqtSlot(str)
146 def on_altTextEdit_textChanged(self, txt):
147 """
148 Private slot handling changes of the alternative text.
149
150 @param txt alternative text
151 @type str
152 """
153 self.__updateOkButton()
154
155 @pyqtSlot(bool)
156 def on_sizeCheckBox_toggled(self, checked):
157 """
158 Private slot to reset the width and height spin boxes.
159
160 @param checked flag indicating the state of the check box
161 @type bool
162 """
163 if checked:
164 self.widthSpinBox.setValue(self.__originalImageSize.width())
165 self.heightSpinBox.setValue(self.__originalImageSize.height())
166
167 @pyqtSlot(bool)
168 def on_aspectRatioCheckBox_toggled(self, checked):
169 """
170 Private slot to adjust the height to match the original aspect ratio.
171
172 @param checked flag indicating the state of the check box
173 @type bool
174 """
175 if checked and self.__originalImageSize.isValid():
176 height = self.widthSpinBox.value() * self.__aspectRatio
177 self.heightSpinBox.setValue(height)
178
179 @pyqtSlot(int)
180 def on_widthSpinBox_valueChanged(self, width):
181 """
182 Private slot to adjust the height spin box.
183
184 @param width width for the image
185 @type int
186 """
187 if self.aspectRatioCheckBox.isChecked() and \
188 self.widthSpinBox.hasFocus():
189 height = width * self.__aspectRatio
190 self.heightSpinBox.setValue(height)
191
192 @pyqtSlot(int)
193 def on_heightSpinBox_valueChanged(self, height):
194 """
195 Private slot to adjust the width spin box.
196
197 @param height height for the image
198 @type int
199 """
200 if self.aspectRatioCheckBox.isChecked() and \
201 self.heightSpinBox.hasFocus():
202 width = height / self.__aspectRatio
203 self.widthSpinBox.setValue(width)
204
205 def getData(self):
206 """
207 Public method to get the entered data.
208
209 @return tuple containing the image address, alternative text,
210 title text, flag to keep the original size, width and height
211 @rtype tuple of (str, str, str, bool, int, int)
212 """
213 return (
214 self.imagePicker.text(),
215 self.altTextEdit.text(),
216 self.titleEdit.text(),
217 self.sizeCheckBox.isChecked(),
218 self.widthSpinBox.value(),
219 self.heightSpinBox.value(),
220 )

eric ide

mercurial