You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pytde/examples/menudemo.py

281 lines
10 KiB

"""
This template constructs an application with menus, toolbar and statusbar.
It uses TDE classes and methods that simplify the task of building and
operating a GUI. It is recommended that this approach be used, rather
than the primitive approach in menuapp1.py
"""
"""
Copyright 2003 Jim Bublitz
Terms and Conditions
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Except as contained in this notice, the name of the copyright holder shall
not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization from the
copyright holder.
"""
import sys
from PyTQt.tqt import TQPopupMenu, SIGNAL, TQLabel, TQIconSet
from tdecore import TDEApplication, TDECmdLineArgs, TDEAboutData, i18n, TDEIcon, TDEIconLoader, TDEShortcut
from tdeui import TDEMainWindow, KMessageBox, KStdAction, TDEAction, TDEToggleAction, TDEFontSizeAction, TDEFontAction, TDERadioAction,\
TDEActionSeparator, TDEActionMenu, KWindowListMenu
STATUSBAR_LEFT = 1
STATUSBAR_MIDDLE = 2
STATUSBAR_RIGHT = 3
class MainWin (TDEMainWindow):
def __init__ (self, *args):
TDEMainWindow.__init__(self, *args)
self.initActions ()
self.initMenus ()
self.initToolBar ()
self.initStatusBar ()
self.saveAction.setEnabled (False)
self.saveAsAction.setEnabled (False)
def initActions (self):
# "File" menu items
self.newAction = KStdAction.openNew (self.slotNew, self.actionCollection ())
self.openAction = KStdAction.open (self.slotOpen, self.actionCollection ())
self.saveAction = KStdAction.save (self.slotSave, self.actionCollection ())
self.saveAsAction = KStdAction.saveAs (self.slotSaveAs, self.actionCollection ())
self.printAction = KStdAction.print_ (self.slotPrint, self.actionCollection ())
self.quitAction = KStdAction.quit (self.slotQuit, self.actionCollection ())
# "Edit" menu items
self.undoAction = KStdAction.undo (self.slotUndo, self.actionCollection ())
self.redoAction = KStdAction.redo (self.slotRedo, self.actionCollection ())
self.cutAction = KStdAction.cut (self.slotCut, self.actionCollection ())
self.copyAction = KStdAction.copy (self.slotCopy, self.actionCollection ())
self.pasteAction = KStdAction.paste (self.slotPaste, self.actionCollection ())
self.findAction = KStdAction.find (self.slotFind, self.actionCollection ())
self.findNextAction = KStdAction.findNext (self.slotFindNext, self.actionCollection ())
self.replaceAction = KStdAction.replace (self.slotReplace, self.actionCollection ())
self.specialAction = TDEAction (i18n ("Special"), TDEShortcut.null (), self.slotSpecial, self.actionCollection (), "special")
# Demo menu items
# TDEToggleAction has an isChecked member and emits the "toggle" signal
self.toggle1Action = TDEToggleAction ("Toggle 1")
self.toggle2Action = TDEToggleAction ("Toggle 2", TDEShortcut.null (), self.slotToggle2, None)
# A separator - create once/use everywhere
self.separateAction = TDEActionSeparator ()
# Font stuff in menus or toolbar
self.fontAction = TDEFontAction ("Font")
self.fontSizeAction = TDEFontSizeAction ("Font Size")
# Need to assign an icon to actionMenu below
icons = TDEIconLoader ()
iconSet = TQIconSet (icons.loadIcon ("viewmag", TDEIcon.Toolbar))
# Nested menus using TDEActions (also nested on toolbar)
self.actionMenu = TDEActionMenu ("Action Menu")
self.actionMenu.setIconSet (iconSet)
self.actionMenu.insert (KStdAction.zoomIn (self.slotZoomIn, self.actionCollection ()))
self.actionMenu.insert (KStdAction.zoomOut (self.slotZoomOut, self.actionCollection ()))
def initMenus (self):
fileMenu = TQPopupMenu (self)
self.newAction.plug (fileMenu)
self.openAction.plug (fileMenu)
fileMenu.insertSeparator ()
self.saveAction.plug (fileMenu)
self.saveAsAction.plug (fileMenu)
fileMenu.insertSeparator ()
self.printAction.plug (fileMenu)
fileMenu.insertSeparator ()
self.quitAction.plug (fileMenu)
self.menuBar ().insertItem (i18n ("&File"), fileMenu)
editMenu = TQPopupMenu (self)
self.undoAction.plug (editMenu)
self.redoAction.plug (editMenu)
editMenu.insertSeparator ()
self.cutAction.plug (editMenu)
self.copyAction.plug (editMenu)
self.pasteAction.plug (editMenu)
editMenu.insertSeparator ()
self.findAction.plug (editMenu)
self.findNextAction.plug (editMenu)
self.replaceAction.plug (editMenu)
editMenu.insertSeparator ()
self.specialAction.plug (editMenu)
self.menuBar ().insertItem (i18n ("&Edit"), editMenu)
demoMenu = TQPopupMenu (self)
self.toggle1Action.plug (demoMenu)
self.toggle2Action.plug (demoMenu)
self.separateAction.plug (demoMenu)
self.fontAction.plug (demoMenu)
self.fontSizeAction.plug (demoMenu)
self.actionMenu.plug (demoMenu)
# self.radio1Action.plug (demoMenu)
# self.radio2Action.plug (demoMenu)
# self.radio3Action.plug (demoMenu)
self.menuBar ().insertItem (i18n ("&Demo"), demoMenu)
# This really belongs in Kicker, not here,
# but it actually works
wlMenu = KWindowListMenu (self)
wlMenu.init ()
self.menuBar ().insertItem (i18n ("&WindowListMenu"), wlMenu)
helpMenu = self.helpMenu ("")
self.menuBar ().insertItem (i18n ("&Help"), helpMenu)
def initToolBar (self):
self.newAction.plug (self.toolBar ())
self.openAction.plug (self.toolBar ())
self.saveAction.plug (self.toolBar ())
self.cutAction.plug (self.toolBar ())
self.copyAction.plug (self.toolBar ())
self.pasteAction.plug (self.toolBar ())
self.separateAction.plug (self.toolBar ())
self.separateAction.plug (self.toolBar ())
self.separateAction.plug (self.toolBar ())
self.fontAction.plug (self.toolBar ())
self.separateAction.plug (self.toolBar ())
self.fontAction.setComboWidth (150)
self.fontSizeAction.plug (self.toolBar ())
self.fontSizeAction.setComboWidth (75)
self.separateAction.plug (self.toolBar ())
# This works, but you have to hold down the
# button in the toolbar and wait a bit
self.actionMenu.plug (self.toolBar ())
# This appears to do nothing
self.actionMenu.setDelayed (False)
# Need this to keep the font comboboxes from stretching
# to the full width of the toolbar when the window is
# maximized (comment out the next two lines to see
# what happens)
stretchlbl = TQLabel ("", self.toolBar ())
self.toolBar ().setStretchableWidget (stretchlbl)
# self.toolBar ().setHorizontalStretchable (False)
def initStatusBar (self):
self.statusBar ().insertItem ("", STATUSBAR_LEFT, 1000, True)
self.statusBar ().insertItem ("", STATUSBAR_MIDDLE, 1000, True)
self.statusBar ().insertItem ("", STATUSBAR_RIGHT, 1000, True)
#-------------------- slots -----------------------------------------------
def slotNew (self, id = -1):
self.notImpl ("New")
def slotOpen(self, id = -1):
self.notImpl ("Open")
def slotSave (self, id = -1):
self.notImpl ("Save")
def slotSaveAs (self):
self.notImpl ("Save As")
def slotPrint (self):
self.notImpl ("Print")
def slotQuit (self):
self.notImpl ("Quit")
def slotUndo (self):
self.notImpl ("Undo")
def slotRedo (self):
self.notImpl ("Redo")
def slotCut (self, id = -1):
self.notImpl ("Cut")
def slotCopy (self, id = -1):
self.notImpl ("Copy")
def slotPaste (self, id = -1):
self.notImpl ("Paste")
def slotFind (self):
self.notImpl ("Find")
def slotFindNext (self):
self.notImpl ("Find Next")
def slotReplace (self):
self.notImpl ("Replace")
def slotSpecial (self):
self.notImpl ("Special")
def slotToggle2 (self):
self.notImpl ("Toggle")
def slotZoomIn (self):
self.notImpl ("Zoom In")
def slotZoomOut (self):
self.notImpl ("Zoom Out")
def notImpl (self, item):
self.statusBar ().changeItem ("%s not implemented" % item, STATUSBAR_LEFT)
KMessageBox.error (self, "%s not implemented" % item, "Not Implemented")
self.statusBar ().changeItem ("", STATUSBAR_LEFT)
#-------------------- main ------------------------------------------------
description = b"A basic application template"
version = b"1.0"
aboutData = TDEAboutData (b"MyApp", b"MyApp",\
version, description, TDEAboutData.License_GPL,\
b"(C) 2003 whoever the author is")
aboutData.addAuthor (b"author1", b"whatever they did", b"email@somedomain")
aboutData.addAuthor (b"author2", b"they did something else", b"another@email.address")
TDECmdLineArgs.init (sys.argv, aboutData)
TDECmdLineArgs.addCmdLineOptions ([(b"+files", b"File to open")])
app = TDEApplication ()
mainWindow = MainWin (None, b"main window")
mainWindow.setGeometry(100, 100, 600, 400)
mainWindow.show()
app.exec_loop()