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

Embellishments


Nothing really new in this part, I'm afraid. We've got to do the menubar, the buttonbar and the statusbar, and we already know how to do them, except perhaps for the statusbar. Such is the drudgery every programmer has to go through.


#!/usr/bin/env python

import sys
from kdeui import *
from kdecore import *

#
# Constants
#

TRUE=1
FALSE=0

# Generic command codes

ID_OPEN=100
ID_NEW=101
ID_SAVE=102
ID_SAVEAS=103
ID_CLOSE=104
ID_NEWWINDOW=105
ID_CLOSEWINDOW=106
ID_COPY=107
ID_CUT=108
ID_PASTE=109
ID_OPTIONS=110
ID_EXIT=111
ID_HELPCONTENTS=112
ID_ABOUT=113
ID_HINTTEXT=300

class Edit(KTMainWindow):
  
  def __init__ (self):
    KTMainWindow.__init__(self)

    self.initMenuBar()
    self.initToolBar()
    self.initStatusBar()

    self.view=QMultiLineEdit(self, "Main View")
    self.setView(self.view)
    self.show()

  def initMenuBar(self):
    self.file=QPopupMenu()
    self.file.insertItem(i18n("&Open..."), ID_OPEN)
    self.file.insertItem(i18n("&New..."), ID_NEW)
    self.file.insertItem(i18n("&Save"), ID_SAVE)    
    self.file.insertItem(i18n("Save &As..."), ID_SAVEAS)
    self.file.insertSeparator()
    self.file.insertItem(i18n("&Exit"), ID_EXIT)

    self.help=QPopupMenu()
    self.help.insertItem(i18n("&Contents..."), ID_HELPCONTENTS)
    self.help.insertItem(i18n("&About..."), ID_ABOUT)
    
    self.menu=KMenuBar(self)
    self.menu.insertItem(i18n("&File"), self.file)
    self.menu.insertItem(i18n("&Help"), self.help)
    self.menu.show()
    self.setMenu(self.menu)
    
  def initToolBar(self):
    self.toolbar=KToolBar(self)
    self.toolbar.insertButton(Icon("filenew.xpm")
                             ,ID_NEW, TRUE
                             ,i18n("Create a new file")
                             )
    self.toolbar.insertButton(Icon("fileopen.xpm")
                             ,ID_OPEN, FALSE
                             ,i18n("Open a file")
                             )
    self.toolbar.insertButton(Icon("filefloppy.xpm")
                             ,ID_SAVE, FALSE
                             ,i18n("Save the current file")
                             )    
    self.addToolBar(self.toolbar)
    self.toolbar.show()
    
  def initStatusBar(self):
    self.statusbar=KStatusBar(self)
    self.statusbar.insertItem("Welcome to Edit",ID_HINTTEXT)
    self.statusbar.show()
    self.setStatusBar(self.statusbar)

 
app=KApplication(sys.argv,"EditApp")
toplevel=Edit()
app.setMainWidget(toplevel)
toplevel.show()
app.exec_loop()

There's an unholy row of constants at the top of the program. Those constants we will use to identify actions from either the menubar or the buttonbar. The last constant will be used to identify the statusbar text so we can change it at will. The icons we use are the standard KDE ones - look in /opt/kde/share/toolbar for inspiration.

There's also an important bug in our implementation of the menus - no, not a bug that will prevent the program from running, but a flagrant violation of standards. Spotted it?

Now run it. Look what the FALSE does to the buttons on the toolbar.


Changes