SHARE
TWEET

Alvin Delagon

a guest Apr 17th, 2009 311 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. @name: qt-textedit.py
  3. @summary: A sample text editor using PyQt4
  4. @author: Alvin Delagon
  5. """
  6.  
  7. import sys
  8. from PyQt4 import QtGui, QtCore
  9.  
  10. def PutToCenter(widget):
  11.     """
  12.    I'm a helper function that move a widget to the
  13.    center of the screen
  14.  
  15.    @param: QtGui.QWidget
  16.    @return: None
  17.    """
  18.     screen = QtGui.QDesktopWidget().availableGeometry()
  19.     size = widget.geometry()
  20.     widget.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
  21.  
  22. def Maximize(widget):
  23.     """
  24.    I'm a helper function that can set the widget's size
  25.    to maximum screen size
  26.    
  27.    @param: QtGui.QWidget
  28.    @return: None
  29.    """
  30.     widget.showMaximized()
  31.    
  32. class TextEditor(QtGui.QMainWindow):
  33.     def __init__(self, parent=None):
  34.         self.filename = "untitled.txt"
  35.         self.modified = False
  36.         self.saved = False
  37.         QtGui.QMainWindow.__init__(self)
  38.         self.setWindowTitle ("Text Editor 1.0.0 - %s" % (self.filename))
  39.         self.textedit = QtGui.QTextEdit()
  40.         self.setCentralWidget(self.textedit)
  41.         self.connect(self.textedit, QtCore.SIGNAL('textChanged()'), self.onTextChanged)
  42.         self.buildMenuBar()
  43.         self.buildAboutBox()
  44.         Maximize(self)
  45.        
  46.     def buildMenuBar(self):
  47.         """
  48.        Build, Populate, and set Signals for the Menu bar
  49.  
  50.        @param: self
  51.        @return: None
  52.        """
  53.         open = QtGui.QAction('&Open', self)
  54.         new = QtGui.QAction('&New', self)
  55.         save = QtGui.QAction('&Save', self)
  56.         saveas = QtGui.QAction('Save As', self)
  57.         exit = QtGui.QAction('E&xit', self)
  58.         credits = QtGui.QAction('Credits', self)
  59.         self.menu = self.menuBar()
  60.         file = self.menu.addMenu('&File')
  61.         file.addAction(open)
  62.         file.addAction(new)
  63.         file.addAction(save)
  64.         file.addAction(saveas)
  65.         file.addAction(exit)
  66.         about = self.menu.addMenu('&About')
  67.         about.addAction(credits)
  68.         self.connect(open, QtCore.SIGNAL('triggered()'), self.onOpen)
  69.         self.connect(new, QtCore.SIGNAL('triggered()'), self.onNew)
  70.         self.connect(save, QtCore.SIGNAL('triggered()'), self.onSave)
  71.         self.connect(saveas, QtCore.SIGNAL('triggered()'), self.onSaveAs)
  72.         self.connect(exit, QtCore.SIGNAL('triggered()'), self.onExit)
  73.         self.connect(credits, QtCore.SIGNAL('triggered()'), self.onCredits)
  74.  
  75.     def buildAboutBox(self):
  76.         """
  77.        Build the AboutBox
  78.  
  79.        @param: self
  80.        @return: None
  81.        """
  82.         self.credits_window = QtGui.QWidget()
  83.         self.credits_window.setGeometry(100, 50, 250, 150)
  84.         self.credits_window.setWindowTitle("About")
  85.         PutToCenter(self.credits_window)
  86.  
  87.     def onTextChanged(self):
  88.         """
  89.        Trigger for the textchanged() signal. My job is to detect if the
  90.        buffer has been modified
  91.  
  92.        @param: self
  93.        @return: None
  94.        """
  95.         self.modified = True
  96.         self.setWindowTitle("Text Editor 1.0.0 - %s [modified]" % (self.filename))
  97.                                            
  98.     def onOpen(self):
  99.         """
  100.        I'm the trigger for the Menu-Open signal. Reads
  101.        the chosen file and loads it to textedit widget
  102.  
  103.        @param: self
  104.        @return: None
  105.        """
  106.         v = self.checkUnsaved("Warning!")
  107.         if v == "cancel":
  108.             return
  109.         self.filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File')
  110.         file = open(self.filename)
  111.         self.textedit.setText(file.read())
  112.         self.setWindowTitle("Text Editor 1.0.0 - %s" % (self.filename))
  113.         self.saved = True
  114.         self.modified = False
  115.  
  116.     def onNew(self):
  117.         """
  118.        I'm the trigger for the Menu-New signal. Clears
  119.        the textedit widget
  120.  
  121.        @param: self
  122.        @return: None
  123.        """
  124.         v = self.checkUnsaved("Warning!")
  125.         if v == "cancel":
  126.             return
  127.         self.saved = False
  128.         self.modified = False
  129.         self.filename = "untitled.txt"
  130.         self.textedit.setText("")
  131.         self.setWindowTitle("Text Editor 1.0.0 - %s" % (self.filename))
  132.        
  133.     def onSave(self):
  134.         """
  135.        I'm the trigger for the Menu-Save signal. Saves
  136.        the textedit contents to the chosen filename
  137.  
  138.        @param: self
  139.        @return: None
  140.        """
  141.         if self.saved == False:
  142.             self.filename = QtGui.QFileDialog.getSaveFileName(self, 'Save As')
  143.         if len(self.filename) == 0:
  144.             return
  145.         file = open(self.filename, 'w')
  146.         file.write(self.textedit.toPlainText())
  147.         file.close()
  148.         self.modified = False
  149.         self.saved = True
  150.         self.setWindowTitle("Text Editor 1.0.0 - %s" % (self.filename))
  151.  
  152.     def onSaveAs(self):
  153.         """
  154.        I'm the trigger for the Menu-SaveAs signal. Saves the
  155.        buffer in a different file
  156.  
  157.        @param: self
  158.        @returns: None
  159.        """
  160.         self.filename = QtGui.QFileDialog.getSaveFileName(self, 'Save As')
  161.         file = open(self.filename, 'w')
  162.         file.write(self.textedit.toPlainText())
  163.         file.close()
  164.         self.modified = False
  165.         self.saved = True
  166.         self.setWindowTitle("Text Editor 1.0.0 - %s" % (self.filename))
  167.        
  168.     def onExit(self):
  169.         """
  170.        I'm the trigger for the Menu-Exit signal. Triggers
  171.        application close
  172.  
  173.        @param: self
  174.        @returns: None
  175.        """
  176.         v = self.checkUnsaved("Really Quit?")
  177.         if v == "cancel":
  178.             return
  179.         self.close()
  180.  
  181.     def onCredits(self):
  182.         """
  183.        I'm the triggers for the Menu-Credits signal. Shows
  184.        the About Window
  185.        """
  186.         self.credits_window.show()
  187.  
  188.     def checkUnsaved(self, title):
  189.         """
  190.        Checks if the buffer has been modified and unsaved.
  191.  
  192.        @param: title (String)
  193.        @return: String
  194.        """
  195.         v = ""
  196.         if self.modified and not(len(self.textedit.toPlainText()) == 0):
  197.             reply = QtGui.QMessageBox.question(self,
  198.                                                title,
  199.                                                "You have some unsaved work!",
  200.                                                QtGui.QMessageBox.Cancel,
  201.                                                QtGui.QMessageBox.Save,
  202.                                                QtGui.QMessageBox.Discard)
  203.             if reply == QtGui.QMessageBox.Cancel:
  204.                 return ("cancel")
  205.             elif reply == QtGui.QMessageBox.Save:
  206.                 self.onSave()
  207.                 return ("save")
  208.             else:
  209.                 return ("discard")
  210.         return (v)
  211.  
  212.  
  213. app = QtGui.QApplication(sys.argv)
  214. qts = TextEditor()
  215. qts.show()
  216. sys.exit(app.exec_())
RAW Paste Data
Top