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
| # -*- 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
from PySide.QtCore import QSize, QRect,QRectF,Qt
from ImageView.ImageView import ImageView
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("Rub your Image")
self._color = QColor()
self._colorPicker = QImage()
self._pix = QPixmap()
self._rectColorPicker = QRectF(10,10,10,10)
self.image = ImageView(QImage(),self._color)
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 checkbox with the rubber on box
self._initCkBox()
def _initMenu(self):
"""
Adding of actions for the tool bar
"""
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.")
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")
iconToolBar.addAction(_actionRubber)
#Palette
_actionPalette = QAction(self)
_actionPalette.triggered.connect(self.__actionPalette)
_actionPalette.setIcon(QIcon("Pics\Palette.png"))
_actionPalette.setStatusTip("Choose your color")
iconToolBar.addAction(_actionPalette)
#ColorPicker
_actionColorPicker = QAction(self)
_actionColorPicker.triggered.connect(self.__actionColorPicker)
_actionColorPicker.setIcon(QIcon(self._pix))
_actionColorPicker.setStatusTip("Chosen color")
iconToolBar.addAction(_actionColorPicker)
"""
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 image As', _file,shortcut=QKeySequence.Save)
_action.triggered.connect(self.__actionSave)
_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 _initCkBox(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)
def __actionOpen(self):
_file = QFileDialog.getOpenFileName(self, "Open Image", "", "Image Files (*.png *.jpg *.bmp)")
if _file:
self.image.setWorkingImage(QImage(_file[0]))
else:
print "Invalid Image"
def __actionSave(self):
_file = QFileDialog.getSaveFileName(parent=None, caption="Save image as")
_result = self.image.workingImage().save(_file[0], "BMP", -1)
# Test to know if it's working
if _result:
print "Saved successfully"
else :
print "Saving failed"
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 __actionPalette(self):
self._color = QColorDialog.getColor()
self.palette = QPalette()
self.palette.setColor(QPalette().NoRole,self._color)
#We give the imageView the color of the pen/rub
self.image.setPenColor(self._color)
def __actionColorPicker(self):
# self.painter = QPainter()
# self.painter.fillRect(self._rectColorPicker,self.getPaletteColor())
#self.painter.fillRect(self._colorPicker,self.getPaletteColor())
self._pix.fill(self.getPaletteColor())
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 __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 __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_() |
Partager