[Main Page] [Edit #1] [Edit #2] [Edit #3] [Edit #4] [Edit #5]

An editor - and now for something completely different


The past few exercises we've spent building an editor for ourselves. Laboriously writing load and save code (although not as laboriously as it would have been if we'd used C++), typing line after line of an exceedingly large program, and not even having a simple search-and-replace...

That being thus, I'd like to present an editor with even more functionality (and a few more bugs) in about half the number of lines.


#!/usr/bin/env python
# Import the libraries

import sys
from kdeui import *
from kdecore import *
from qt import *

# Create the application instance

app = KApplication(sys.argv,"PythonKDE Editor")

# Setup the main window

topwidget = KTMainWindow()
editor = KEdit(app,topwidget)
topwidget.setView(editor)

# menu function - can't seem to pass a parameter through a slot

def openFile():
  editor.openFile(editor.OPEN_READWRITE)

# Create the menubar

mn = KMenuBar(topwidget)

file = QPopupMenu()
file.insertItem("&New", editor.newFile)
file.insertItem("&Load...", openFile)
file.insertItem("&Save", editor.doSave)
file.insertItem("Save &As...", editor.saveAs)
file.insertItem("&Insert file...", editor.insertFile)
file.insertSeparator()
file.insertItem("&Quit", app.quit)
mn.insertItem("&File",file)

edit = QPopupMenu()
edit.insertItem("&Copy", editor.copyText)
edit.insertItem("&Insert", editor.paste)
edit.insertItem("C&ut", editor.cut)
edit.insertItem("Select &all", editor.selectAll)
edit.insertSeparator()
edit.insertItem("&Search...", editor.Search)
edit.insertItem("&Replace...", editor.Replace)
edit.insertItem("Repeat &last", editor.repeatSearch)
edit.insertSeparator()
edit.insertItem("&Goto line...", editor.doGotoLine)
mn.insertItem("&Edit", edit)

options = QPopupMenu()
options.insertItem("&Font...", editor.selectFont)
mn.insertItem("&Options", options)

mn.insertSeparator()
mn.insertItem("&Help",app.getHelpMenu(0,"PythonKDE Editor 0.01"))

topwidget.setMenu(mn)

# Create the toolbar

toolbar = KToolBar(topwidget)
toolbar.insertButton(Icon("filenew.xpm")
                    ,1
                    ,SIGNAL("clicked(int)")
                    ,editor.newFile
                    , 1
                    , i18n("New")
                    )
toolbar.insertButton(Icon("fileopen.xpm")
                     ,2
                     ,SIGNAL("clicked(int)")
                     , openFile
                     , 1
                     ,i18n("Load")
                     )
toolbar.insertButton(Icon("filefloppy.xpm")
                    ,3
                    ,SIGNAL("clicked(int)")
                    ,editor.doSave
                    , 1
                    ,i18n("Save")
                    )
topwidget.addToolBar(toolbar)

# Create the statusbar

statusbar = KStatusBar(topwidget)
topwidget.setStatusBar(statusbar)

# Setup and show the main widget

topwidget.show()
app.setMainWidget(topwidget)

# Execute the application

app.exec_loop()

# Clean up

del app


This code is a more-or-less straight translation of the program that was included as an example in Matthias Hoelzer's obsolete KDE bindings.

The reason this program is so small is that we use the KEdit object, which has all kinds of useful functionality itself. Perhaps one day KWrite will become available as an object, too, and then we'd have a syntax highlighting editor widget we could use everywhere. Of course, with the convience come the bugs. Try to open a file and see the open button gently mutate into a save button as soon as you click a file.

The lesson? Know your library. Chances are there's a component with more functionality available, functionality you won't have to write yourself.


Changes