By PATX on Jul 18, 2009

to install (via wget) and use:
$ python p-edit.py

for more:
e-mail patx: patx44 at gmail dot com
website: http://bitbucket.org/patx/p-edit

# p-Edit by PATX.
# With help from:
# Carson J. Q. Farmer
# & rowinggolfer

Copyright (c) 2009 Harrison Erd

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

    1. The origin of this software must not be misrepresented; you must not
    claim that you wrote the original software. If you use this software
    in a product, an acknowledgment in the product documentation would be
    appreciated but is not required.

    2. Altered source versions must be plainly marked as such, and must not be
    misrepresented as being the original software.

    3. This notice may not be removed or altered from any source

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import QtCore, QtGui

class MyHighlighter( QSyntaxHighlighter ):

    def __init__( self, parent, theme ):
      QSyntaxHighlighter.__init__( self, parent )
      self.parent = parent
      keyword = QTextCharFormat()
      reservedClasses = QTextCharFormat()
      assignmentOperator = QTextCharFormat()
      delimiter = QTextCharFormat()
      specialConstant = QTextCharFormat()
      boolean = QTextCharFormat()
      number = QTextCharFormat()
      comment = QTextCharFormat()
      string = QTextCharFormat()
      singleQuotedString = QTextCharFormat()

      self.highlightingRules = []

      # keyword
      brush = QBrush( Qt.darkBlue, Qt.SolidPattern )
      keyword.setForeground( brush )
      keyword.setFontWeight( QFont.Bold )
      keywords = QStringList( [ "break", "else", "for", "if", "in", 
                                "next", "repeat", "return", "switch", 
                                "try", "while", "self" ] )
      for word in keywords:
        pattern = QRegExp("\\b" + word + "\\b")
        rule = HighlightingRule( pattern, keyword )
        self.highlightingRules.append( rule )

      # reservedClasses
      reservedClasses.setForeground( brush )
      reservedClasses.setFontWeight( QFont.Bold )
      keywords = QStringList( [ "array", "character", "complex", 
                                "data.frame", "double", "factor", 
                                "function", "integer", "list", 
                                "logical", "matrix", "numeric", 
                                "vector" ] )
      for word in keywords:
        pattern = QRegExp("\\b" + word + "\\b")
        rule = HighlightingRule( pattern, reservedClasses )
        self.highlightingRules.append( rule )

      # assignmentOperator
      brush = QBrush( Qt.yellow, Qt.SolidPattern )
      pattern = QRegExp( "(<){1,2}-" )
      assignmentOperator.setForeground( brush )
      assignmentOperator.setFontWeight( QFont.Bold )
      rule = HighlightingRule( pattern, assignmentOperator )
      self.highlightingRules.append( rule )

      # delimiter
      pattern = QRegExp( "[\)\(]+|[\{\}]+|[][]+" )
      delimiter.setForeground( brush )
      delimiter.setFontWeight( QFont.Bold )
      rule = HighlightingRule( pattern, delimiter )
      self.highlightingRules.append( rule )

      # specialConstant
      brush = QBrush( Qt.green, Qt.SolidPattern )
      specialConstant.setForeground( brush )
      keywords = QStringList( [ "Inf", "NA", "NaN", "NULL" ] )
      for word in keywords:
        pattern = QRegExp("\\b" + word + "\\b")
        rule = HighlightingRule( pattern, specialConstant )
        self.highlightingRules.append( rule )

      # boolean
      boolean.setForeground( brush )
      keywords = QStringList( [ "TRUE", "FALSE" ] )
      for word in keywords:
        pattern = QRegExp("\\b" + word + "\\b")
        rule = HighlightingRule( pattern, boolean )
        self.highlightingRules.append( rule )

      # number
      pattern = QRegExp( "[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?" )
      pattern.setMinimal( True )
      number.setForeground( brush )
      rule = HighlightingRule( pattern, number )
      self.highlightingRules.append( rule )

      # comment
      brush = QBrush( Qt.blue, Qt.SolidPattern )
      pattern = QRegExp( "#[^\n]*" )
      comment.setForeground( brush )
      rule = HighlightingRule( pattern, comment )
      self.highlightingRules.append( rule )

      # string
      brush = QBrush( Qt.red, Qt.SolidPattern )
      pattern = QRegExp( "\".*\"" )
      pattern.setMinimal( True )
      string.setForeground( brush )
      rule = HighlightingRule( pattern, string )
      self.highlightingRules.append( rule )

      # singleQuotedString
      pattern = QRegExp( "\'.*\'" )
      pattern.setMinimal( True )
      singleQuotedString.setForeground( brush )
      rule = HighlightingRule( pattern, singleQuotedString )
      self.highlightingRules.append( rule )

    def highlightBlock( self, text ):
      for rule in self.highlightingRules:
        expression = QRegExp( rule.pattern )
        index = expression.indexIn( text )
        while index >= 0:
          length = expression.matchedLength()
          self.setFormat( index, length, rule.format )
          index = text.indexOf( expression, index + length )
      self.setCurrentBlockState( 0 )

class HighlightingRule():
  def __init__( self, pattern, format ):
    self.pattern = pattern
    self.format = format

class pedit(QtGui.QWidget):

    def Save(self):
        save_file = QtGui.QFileDialog.getSaveFileName(self,'Save File','')    

    def Open(self):
        open_file = QtGui.QFileDialog.getOpenFileName(self,'Open File','')
        open_the_file = open(open_file, 'r')

    def __init__(self):
        self.pb1 = QtGui.QPushButton('Save')
        self.pb2 = QtGui.QPushButton('Open')
        self.txt = QtGui.QTextEdit()
    highlighter = MyHighlighter( self.txt, "Classic" )
        tab = QtGui.QGridLayout()
        tab.addWidget(self.txt, 2, 0, 1, 2)
        tab.addWidget(self.pb1, 0, 0)
        tab.addWidget(self.pb2, 0, 1)
        self.txt.setStyleSheet('font-family: monospace')
        self.connect(self.pb1, QtCore.SIGNAL('clicked()'),
        self.connect(self.pb2, QtCore.SIGNAL('clicked()'),

if __name__ == "__main__":
  app = QApplication( sys.argv )
  window = pedit()
  sys.exit( app.exec_() )


Sign in to comment.
PATX   -  Jul 21, 2009

Ok thanks!

Hawkee   -  Jul 21, 2009

If you want to have buttons at the top of the window you could use icons. That's a fairly common practice, but I think it's important to have the standard File/Edit menus anyway. Something to consider for your next app at least.

PATX   -  Jul 21, 2009

Yes I can. However I myself find that if it only has two buttons it would look kind of weird :/. However if you want I can make a "sub-version" of p-Edit?

Hawkee   -  Jul 21, 2009

No problem, I appreciate the Python snippets. It's great to see people posting in other languages on the site, they inspire me to learn new languages and platforms. The new button location is an improvement, but I think what would be ideal are application menus like File, Edit, etc. I don't know if you can do this with PyQt, but it would be nice.

PATX   -  Jul 20, 2009

@Hawkee I have changed it so there Open and Save buttons (the only buttons) are on the top. Once again thanks for your feedback on these GUIs I have made most of them (this and the twitter client) when I was bored... It really helps having a person comment on these that way they can improve and more importantly i can improve. Thanks again -- PATX

Hawkee   -  Jul 18, 2009

Very basic, but it gets the job done. I think it would feel more consistent to have the buttons on top like most apps though.

PATX   -  Jul 18, 2009

there a no know bugs atm :)

ok, basicly this is just a simple text editor, to open edit and save files. i have added python syntax highlighting.

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.