1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
# -*- coding: utf-8 -*-
from PySide.QtGui import QApplication, QWidget, QPainter, QKeySequence,QColor,QCheckBox,QStatusBar,QIcon,\
QMessageBox,QImage, QRubberBand, QMainWindow, QAction,\
QMenuBar, QFileDialog, QScrollArea,QColorDialog,QPalette,QBrush,QPixmap,\
QPushButton
from PySide.QtCore import QSize, QRect,QRectF,Qt
from ImageView import ImageView
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("Rub your Image")
self._color = QColor()
self._fileName=""
self.image = ImageView(QImage(),self.getPaletteColor())
self.resize(500, 300)
#Init of the Menu with the menubar and the icon
self._initMenu()
#Init of the Scrollarea
self._initScrollArea()
#Init of the status bar (at bottom) with ckbox and colorPicker
self._initStatusBar()
def _initMenu(self):
"""
Adding of actions for the tool bar
"""
self.iconToolBar = self.addToolBar("iconBar")
#About
_actionAbout = QAction(self)
_actionAbout.triggered.connect(self.__actionAbout)
_actionAbout.setIcon(QIcon("Pics\info-icon.jpg"))
_actionAbout.setStatusTip("Pop up the About dialog.")
self.iconToolBar.addAction(_actionAbout)
#Rubber
_actionRubber = QAction(self)
_actionRubber.triggered.connect(self.__actionRubber)
_actionRubber.setIcon(QIcon("Pics\eraser2.png"))
_actionRubber.setStatusTip("Enable or Disable eraser tool")
self.iconToolBar.addAction(_actionRubber)
"""
Add of the Menu which contains all the features like of Open/Save/..
and the edit options
"""
menu = QMenuBar()
self.setMenuBar(menu)
_file = menu.addMenu('File')
_edit = menu.addMenu("Edit")
# Menu Open
_action = QAction('Open', _file,shortcut=QKeySequence.Open)
_action.triggered.connect(self.__actionOpen)
_file.addAction(_action)
# Menu Save
_action = QAction('Save ', _file,shortcut=QKeySequence.Save)
_action.triggered.connect(self.__actionSave)
_file.addAction(_action)
# Menu Save As
_action = QAction('Save image As', _file,shortcut=QKeySequence(Qt.CTRL+Qt.SHIFT+Qt.Key_S))
_action.triggered.connect(self.__actionSaveAs)
_file.addAction(_action)
# Menu Close
_action = QAction('Close', _file,shortcut=QKeySequence.Close)
_action.triggered.connect(self.__actionClose)
_file.addAction(_action)
#Menu Edit
def _initScrollArea(self):
"""
Add of scroll bars to see properly the images
"""
self.area = QScrollArea(self)
self.area.setWidget(self.image)
self.area.setWidgetResizable(True)
self.setCentralWidget(self.area)
def _initStatusBar(self):
#Check Box rub
statusbar = QStatusBar(self)
self.setStatusBar(statusbar)
self._ckbox = QCheckBox("Rubber On/Off")
statusbar.addWidget(self._ckbox)
if self._ckbox.isChecked():
self._mode = "rub"
else :
self._mode = "select"
#Init of the boolean to know whether we select or rub
self.image.mode = self._mode
self._ckbox.stateChanged.connect(self.__rubberBoxChanged)
#ColorPicker
self._buttonColorPicker = QPushButton()
self.__setColor(QColor(255,255,255))
self._buttonColorPicker.clicked.connect(self.__actionColorPicker)
self._buttonColorPicker.setText("Color Picker")
self._buttonColorPicker.setToolTip("Choose your color")
#Bottom left
statusbar.addWidget(self._buttonColorPicker)
#Bottom right
#statusbar.addPermanentWidget(self._buttonColorPicker)
def __actionOpen(self):
self._fileName = QFileDialog.getOpenFileName(self, "Open Image", "", "Image Files (*.png *.jpg *.bmp)")
if self._fileName:
self.image.setWorkingImage(QImage(self._fileName[0]))
else:
print "Invalid Image"
QMessageBox.about(self, "Opening State",
""" <p>Opening this image failed </p>
<p> Check your extension and make sure it is .bmp </p>""" )
def __actionSave(self):
_result = self.image.workingImage().save(self. _fileName[0], "BMP", -1)
# Test to know if it's working
if _result:
print "Saved successfully"
else :
print "Saving failed"
def __actionSaveAs(self):
self._fileName = QFileDialog.getSaveFileName(parent=None, caption="Save image as")
"""
Must check if the .bmp is well written if not message box to show an error
"""
_result = self.image.workingImage().save(self. _fileName[0], "BMP", -1)
# Test to know if it's working
if _result:
print "Saved successfully"
QMessageBox.about(self, "Saving State",
"""<p> Saved with success </p>""" )
else :
print "Saving failed"
QMessageBox.about(self, "Saving State",
""" <p>Saving this image failed </p>
<p> Check your extension and make sure it is .bmp </p>""" )
def getFileName(self):
return self._fileName
def setFileName(self,newFileName):
self._fileName = newFileName
def __actionClose(self):
self.close()
def __actionRubber(self):
if self._ckbox.isChecked():
print"uncheck"
self._ckbox.setChecked(False)
else :
print "Set check"
self._ckbox.setChecked(self._ckbox.isCheckable())
def __actionAbout(self):
'''Popup a box with about message.'''
QMessageBox.about(self, "About PySide, Platform and the like",
"""<b> About this program </b>
<p>Copyright 2012 Maugin Guillaume.
All rights reserved in accordance with
GPL v2 or later
<p>This application can be used for
displaying OS and platform details.
<p>Python %s - PySide version %s - Qt version %s on %s""" )
def __actionColorPicker(self):
dlg = QColorDialog(self)
if dlg.exec_():
self.__setColor(dlg.selectedColor())
self.setPaletteColor(dlg.selectedColor())
def __setColor(self, color):
_img = QPixmap(16, 16)
_p = QPainter(_img)
_p.fillRect(_img.rect(), QBrush(color))
self.setPaletteColor(color)
_p.end()
self._buttonColorPicker.setIcon(QIcon(_img))
def getPaletteColor(self):
return self._color
def setPaletteColor(self,colour):
self._color=colour
self.update()
#Init of the property for mode
color = property(getPaletteColor, setPaletteColor)
def __rubberBoxChanged(self, state):
if self._ckbox.isChecked():
self._mode = 'rub'
else:
self._mode = 'select'
self.image.mode = self._mode
if __name__ == '__main__':
app = QApplication([])
win = MainWindow()
win.show()
app.exec_() |