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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
| from PySide.QtCore import QSize, QRect, QRectF, Qt, QFile
from PySide.QtGui import QApplication, QWidget, QPainter, QKeySequence, QColor, \
QCheckBox, QStatusBar, QIcon, QMessageBox, QImage, QRubberBand, QMainWindow, \
QAction, QMenuBar, QFileDialog, QScrollArea, QColorDialog, QPalette, QBrush, \
QPixmap, QGraphicsScene, QGraphicsView, QGraphicsPixmapItem, QPushButton,\
QSlider,QDockWidget,QListWidget, QUndoStack,QUndoView
from Scene.GraphicsScene import GraphicsScene
from View.GraphicsView import GraphicsView
from Tools.AddCommand import AddCommand
from Tools.ColorPickerTool import ColorPickerTool
# We call the class graphicsScene and we import it
class MainWindow(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.setWindowTitle("Rub on your Image")
self._fileName="-1"
self.sliderValue = 0
'''
Declarations of the bool to block an action
when another is selected
FIXME:
Change the layout system suppress the tool bar maybe ..
add square, circle ...
maybe do the trick with the rubber box to change the value.
'''
#Init rubberBool so rub is disabled by def
self.rubberBool = True
#Init selectBool to true so by def it's enabled
self.selectBool = False
#we suppose select by def but can it be rub to test
self._mode = 'select'
'''
TODO:
Use of undofmwork
'''
""" remove that => """
self.setFileName("C:\Users\Guillaume\Qt\workspaceQt\EraserV2\Pics\mol1.bmp")
self.scene = GraphicsScene(self.getFileName())
#Init of the value of the width so that you will see the rub !!!
self.scene.setWidth(1)
self.image = self.scene.img
self.view = GraphicsView(self.scene)
self.setCentralWidget(self.view)
"""
and place it in open
"""
self._color = QColor()
#self.image = QImage()
#Init of the Menu with the menubar and the icon
self._createMenu()
#Init of the toolbar with all tools rubber etc..
self._createToolBar()
#Init of the dockWidget
self._createDockWidget()
#Init of the status bar (at bottom) with ckbox and colorPicker
self._createStatusBar()
def _createMenu(self):
"""
Adding of actions for the tool bar
"""
self.iconToolBar = self.addToolBar("iconBar")
"""
Add of the Menu which contains all the features like of Open/Save/..
and the edit options
"""
menu = QMenuBar()
self.setMenuBar(menu)
# File menu
_file = menu.addMenu('File')
# 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)
#Edit menu
_edit = menu.addMenu("Edit")
'''
FIXME:
another try we ve an another stack
_action = QAction('Undo',_edit,shortcut=QKeySequence.Undo)
_action.triggered.connect(self.__actionUndo)
_edit.addAction(_action
def __actionUndo(self):
self.scene.removePixItem()
=======
undo Fmwk
'''
undoAction = self.scene.pixItem.undoStack.createUndoAction(self,'Undo')
undoAction.setShortcuts(QKeySequence.Undo)
_edit.addAction(undoAction)
redoAction = self.scene.pixItem.undoStack.createRedoAction(self,'Redo')
redoAction.setShortcuts(QKeySequence.Redo)
_edit.addAction(redoAction)
def _createToolBar(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
self._actionRubber = QAction(self)
self._actionRubber.triggered.connect(self.__actionRubber)
self._actionRubber.setIcon(QIcon("Pics\eraser2.png"))
# on/ off by statusTip maybe or with the icon ...
''''
FIXME:
Add the rectangle rubber with the icon ... and a variable to disabled
circle when you have the rect ..
'''
self._actionRubber.setStatusTip("Enable or Disable eraser tool")
self._actionRubber.setText("Rubber Tool")
self.iconToolBar.addAction(self._actionRubber)
#Select
_actionSelect = QAction(self)
_actionSelect.triggered.connect(self.__actionSelect)
_actionSelect.setIcon(QIcon("Pics\selectTool.png"))
# on/ off by statusTip maybe or with the icon ...
''''
FIXME:
Add the rectangle Select with the icon ... and a variable to disabled
circle when you have the rect ..
'''
_actionSelect.setStatusTip("Enable Select Tool")
_actionSelect.setText("SelectTool")
_actionSelect.setEnabled(True)
self.iconToolBar.addAction(_actionSelect)
#ColorPicker
_actionColorPicker = QAction(self)
_actionColorPicker.triggered.connect(self.__actionColorPicker)
_actionColorPicker.setIcon(QIcon("Pics\Palette2.png"))
_actionColorPicker.setStatusTip("Chosen color")
self.iconToolBar.addAction(_actionColorPicker)
#Circle rubber
self._actionCircleRub = QAction(self)
self._actionCircleRub.triggered.connect(self.__actionCircleRub)
self._actionCircleRub.setIcon(QIcon("Pics\circle.jpg"))
self._actionCircleRub.setStatusTip("Circle rub")
self.iconToolBar.addAction(self._actionCircleRub)
#Rectangle
self._actionRectRub = QAction(self)
#_img = QPixmap(16, 16)
# to have the size of the rubber
_img = QPixmap(16,16)
_p = QPainter(_img)
_p.fillRect(_img.rect(), QBrush(Qt.black))
_p.end()
self._actionRectRub.setIcon(QIcon(_img))
self._actionRectRub.triggered.connect(self.__actionRectRub)
self._actionRectRub.setStatusTip("Rect rubber")
self.iconToolBar.addAction(self._actionRectRub)
#Pipette
'''
Au lieu de cliquer a chaque fois pour avoir la couleur, utilise le truc
de la check box pour garder l a pipette
et peut etre passer la couleur a la palette ...
'''
self._actionPipette = QAction(self)
self._actionPipette.triggered.connect(self.__actionPipette)
self._actionPipette.setText('Pipette')
self._actionPipette.setToolTip("Pipette, click to choose your color")
self.iconToolBar.addAction(self._actionPipette)
def _createDockWidget(self):
#self.dockWidget = QDockWidget()
print "Dock widgt"
#self.addDockWidget()
# dock =QDockWidget(self.tr("Customers"), self)
# dock.setAllowedAreas(Qt.TopDockWidgetArea | Qt.BottomDockWidgetArea)
# self.customerList =QListWidget(dock)
# self.customerList.addItems((
# "E"
# ))
# dock.setWidget(self.customerList)
#self.addDockWidget(Qt.TopDockWidgetArea, dock)
def _createStatusBar(self):
#Check Box rub
statusbar = QStatusBar(self)
self.setStatusBar(statusbar)
self._ckbox = QCheckBox("Rubber On/Off")
statusbar.addWidget(self._ckbox)
#Trick so that we can change mode of select or rub
self._ckbox.setVisible(False)
self._ckbox.stateChanged.connect(self.__rubberBoxChanged)
self._pkbox = QCheckBox("Pippette On/Off")
statusbar.addWidget(self._pkbox)
self._pkbox.stateChanged.connect(self.__pipetteBoxChanged)
#Pipette
self._buttonPipette = QPushButton(self)
self._buttonPipette.clicked.connect(self.__actionPipette)
self._buttonPipette.setText('Pipette')
self._buttonPipette.setToolTip("Pipette, click to choose your color")
statusbar.addWidget(self._buttonPipette)
#ColorPicker
self._buttonColorPicker = QPushButton()
self.__setPaletteColor(QColor(Qt.black))
self._buttonColorPicker.clicked.connect(self.__actionColorPicker)
self._buttonColorPicker.setText("Color Picker")
self._buttonColorPicker.setToolTip("Choose your color")
# hide the border of the button
self._buttonColorPicker.setFlat(True)
# Make the button selected or not (appuyer ou non genre Paint)
#self._buttonColorPicker.setDown(True)
#Bottom left
#statusbar.addWidget(self._buttonColorPicker)
#Bottom right
statusbar.addPermanentWidget(self._buttonColorPicker)
#Slider to choose the stroke
self.slider =QSlider(Qt.Horizontal , self)
self.slider.setRange(1, 30)
self.slider.setValue(1)
self.slider.setToolTip("Choose your stroke")
#self.slider.connect()
self.slider.valueChanged.connect(self.__sliderChanged)
statusbar.addWidget(self.slider)
def getFileName(self):
return self._fileName
def setFileName(self,fileName):
self._fileName = fileName
def __actionOpen(self):
self.__fileName = QFileDialog.getOpenFileName(self, "Open Image", "", "Image Files (*.png *.jpg *.bmp)")
#self.setFileName(self._fileName)
if self.__fileName:
'''
self.setFileName(self.__fileName[0])
self.scene = GraphicsScene(self.getFileName())
self.view = GraphicsView(self.scene)
self.setCentralWidget(self.view)
'''
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):
if self._fileName == "-1" :
self.__actionSaveAs()
else :
_result = self.scene.saveImage()
# Test to know if it's working
if _result:
print "Saved successfully"
QMessageBox.information(self, "Saving State",
"""<p> Saved with success </p>""" )
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.scene.saveImageAs(self._fileName[0])
# 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 __actionClose(self):
self.close()
def __actionRubber(self):
if self._ckbox.isChecked():
print"uncheck"
self._ckbox.setChecked(False)
self._actionRectRub.setEnabled(False)
self._actionCircleRub.setEnabled(False)
else :
print "Set check"
self._ckbox.setChecked(self._ckbox.isCheckable())
self._actionRectRub.setEnabled(True)
self._actionCircleRub.setEnabled(True)
def __actionSelect(self):
print "mode action select : ",self._mode
if self._mode =='rub' or self._mode == '-1':
self._mode = 'select'
self.scene.setMode('select')
if self._mode == 'select':
print "mode action select if select ::"
# Wait till' user press anothe button
self._mode = ''
self.selectBool = True
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.__setPaletteColor(dlg.selectedColor())
self.setPaletteColor(dlg.selectedColor())
if self._mode =='pipette':
print 'Action pipette in color picker'
self.__setPaletteColor(QColor(self.scene.pixItem.getPipetteColor()))
def __actionPipette(self):
'''
TODO:
ne pas avoir besoin de cliquer pour afficher la couleur surement combiner avec
le color picker
peut etre avoir un bouton pour la pipette puis avoir un affichage en bas
Have an another class with tool (ColorPicker)
inherits from QObject to connect directly (dir connec connect type) so
that image can be changed every time
'''
if self._pkbox.isChecked():
print"unchecking the pbox"
self._pkbox.setChecked(False)
self._actionRectRub.setEnabled(False)
self._actionCircleRub.setEnabled(False)
else :
print "Set check"
self._mode = 'pipette'
self._pkbox.setChecked(self._pkbox.isCheckable())
self._actionRectRub.setEnabled(True)
self._actionCircleRub.setEnabled(True)
def __actionCircleRub(self):
if self._mode == 'rub':
self.scene.setShape('circle')
def __actionRectRub(self):
print "action Rect rubs"
print "mode :", self._mode
if self._mode == 'rub':
self.scene.setShape('rect')
def __setPaletteColor(self, color):
_img = QPixmap(16, 16)
_p = QPainter(_img)
_p.fillRect(_img.rect(), QBrush(color))
_p.end()
self._buttonColorPicker.setIcon(QIcon(_img))
def __sliderChanged(self,val):
self.scene.setWidth(val)
self.setF
def __rubberBoxChanged(self, state):
print "mode rubberBoxChanged",self._mode
if self._ckbox.isChecked():
self._mode = 'rub'
else:
self._mode = 'select'
self._actionRubber.setEnabled(self.getRubberBool())
# We give the view the mode in order to select the right tool to use
self.scene.setMode(self._mode)
def __pipetteBoxChanged(self,state):
print "mode pippettebox chnged",self._mode
if self._pkbox.isChecked() :
#& self.scene.pixItem.getMousePressBool()
self._mode = 'pipette'
self.scene.pixItem.setMode(self._mode)
_img = QPixmap(16, 16)
_p = QPainter(_img)
'''
FIXME:
connect with a qobject to have the color instantly like PAint
'''
self.pipetteColor = QColor(self.scene.pixItem.getPipetteColor())
#self.pipetteColor = ColorPickerTool.getPipetteColor(self.scene)
_p.fillRect(_img.rect(), QBrush(self.pipetteColor))
_p.end()
self._buttonPipette.setIcon(QIcon(_img))
self.__setPaletteColor(self.pipetteColor)
else :
self._mode = 'select'
def getRubberBool(self):
print "get rubBool",self.rubberBool
return self.rubberBool
def setRubberBool(self,_bool):
self.rubberBool = _bool
def getPaletteColor(self):
return self._color
def setPaletteColor(self,colour):
self._color=colour
self.scene.setColor(colour)
self.update()
def _initStyleFile(self):
styleFile = QFile("QSS\style.qss")
styleFile.open(QFile().ReadOnly)
styleSheet = unicode(styleFile.readAll())
return styleSheet |