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.
koffice/kspread/plugins/scripting/scripts/scripteditor/ScriptEditor.py

1100 lines
37 KiB

"""
Kross Script Editor
Description:
This script provides a in python written script editor.
Author:
Sebastian Sauer <mail@dipe.org>
Copyright:
Dual-licensed under LGPL v2+higher and the BSD license.
"""
import os, sys
try:
from TQt import tqt
except (ImportError):
raise Exception("Failed to import the required PyTQt python module.")
####################################################################################
# Samples.
class Widget(tqt.TQHBox):
def __init__(self, parentwidget, label = None):
self.parentwidget = parentwidget
from TQt import tqt
tqt.TQHBox.__init__(self, parentwidget)
self.setMargin(4)
self.setSpacing(4)
if label != None: tqt.TQLabel(label, self)
def value(self):
return None
class ListWidget(Widget):
def __init__(self, parentwidget, label):
from TQt import tqt
global Widget
Widget.__init__(self, parentwidget, label)
self.combo = tqt.TQComboBox(self)
self.combo.setEditable(True)
self.setStretchFactor(self.combo,1)
def value(self):
return self.combo.currentText()
class EditWidget(Widget):
def __init__(self, parentwidget, label):
from TQt import tqt
global Widget
Widget.__init__(self, parentwidget, label)
self.edit = tqt.TQLineEdit(self)
self.setStretchFactor(self.edit,1)
def value(self):
return self.edit.text()
class FileWidget(Widget):
def __init__(self, parentwidget, label, filtermask, openfiledialog = True):
self.filtermask = filtermask
self.openfiledialog = openfiledialog
from TQt import tqt
global Widget
Widget.__init__(self, parentwidget, label)
self.edit = tqt.TQLineEdit(self)
self.setStretchFactor(self.edit,1)
btn = tqt.TQPushButton("...",self)
tqt.TQObject.connect(btn, tqt.TQ_SIGNAL("clicked()"), self.btnClicked)
def btnClicked(self):
from TQt import tqt
text = str( self.edit.text() )
if self.openfiledialog:
filename = str( tqt.TQFileDialog.getOpenFileName(text, self.filtermask, self.parentwidget) )
else:
filename = tqt.TQFileDialog.getSaveFileName(text, self.filtermask, self.parentwidget)
if filename != "": self.edit.setText( filename )
def value(self):
return self.edit.text()
class Samples:
####################################################################################
# KexiDB
class KexiDB:
def __init__(self, parentwidget):
self.parentwidget = parentwidget
class _ProjectWidget(FileWidget):
def __init__(self, parentwidget):
global FileWidget
FileWidget.__init__(self, parentwidget, "Project File:", "*.kexi *.kexis *.kexic;;*")
class _DriverWidget(ListWidget):
def __init__(self, parentwidget):
global ListWidget
ListWidget.__init__(self, parentwidget, "Driver:")
import krosskexidb
for driver in krosskexidb.DriverManager().driverNames():
self.combo.insertItem(driver)
class _TableWidget(ListWidget):
def __init__(self, parentwidget):
global ListWidget
ListWidget.__init__(self, parentwidget, "Table:")
class PrintDriverDetails:
""" Print a the list of available KexiDB drivers and print details about one of them. """
name = "Details about a driver"
def __init__(self, parent):
self.widgets = {
"DriverName" : Samples.KexiDB._DriverWidget( parent.parentwidget ),
}
def getCode(self):
return (
'import krosskexidb',
'drivermanager = krosskexidb.DriverManager()',
'print("drivernames: %s" % drivermanager.driverNames())',
'',
'driver = drivermanager.driver( \"{DriverName}\" )',
'print("driver: {DriverName}")',
'print("version=%s.%s" % (driver.versionMajor(),driver.versionMinor())ú',
'print("mimetype=%s" % driver.fileDBDriverMimeType()ú',
'print("filedriver=%s" % driver.isFileDriver())',
)
class ConnectWithFile:
""" Connect with a KexiDB database by using a Kexi Connection Project File. """
name = "Connect with file"
def __init__(self, parent):
self.widgets = {
"ProjectFile" : Samples.KexiDB._ProjectWidget( parent.parentwidget ),
}
def getCode(self):
return (
'# Import the KexiDB module.',
'import krosskexidb',
'drivermanager = krosskexidb.DriverManager()',
'',
'# Get the connectiondata from the project file.',
'connectiondata = drivermanager.createConnectionDataByFile( "{ProjectFile}" )',
'print("Connectiondata: %s" % connectiondata.serverInfoString())',
'',
'# Create the driver for the database backend.',
'driver = drivermanager.driver( connectiondata.driverName() )',
'',
'# Create and establish the connection to the database.',
'connection = driver.createConnection(connectiondata)',
'if not connection.isConnected():',
' if not connection.connect():',
' raise Exception("Failed to connect")',
'',
'# Open the database for usage.',
'print("Databases: %s" % connection.databaseNames())',
'if not connection.isDatabaseUsed():',
' if not connection.useDatabase( connectiondata.databaseName() ):',
' if not connection.useDatabase( connectiondata.fileName() ):',
' raise Exception("Failed to use database")',
'',
'# Print some infos.',
'print("All tables: %s" % connection.allTableNames())',
'print("Tables: %s" % connection.tableNames())',
'print("Queries: %s" % connection.queryNames())',
)
class IterateThroughTable:
""" Iterate through a table within a connected KexiDB database. """
name = "Iterate through table"
def __init__(self, parent):
self.widgets = {
"ProjectFile" : Samples.KexiDB._ProjectWidget( parent.parentwidget ),
"TableName" : Samples.KexiDB._TableWidget( parent.parentwidget ),
}
def getCode(self):
return (
'# Import the KexiDB module.',
'import krosskexidb',
'drivermanager = krosskexidb.DriverManager()',
'',
'# Get the connectiondata from the project file.',
'connectiondata = drivermanager.createConnectionDataByFile( "{ProjectFile}" )',
'print("Connectiondata: %s" % connectiondata.serverInfoString())',
'',
'# Create the driver for the database backend.',
'driver = drivermanager.driver( connectiondata.driverName() )',
'',
'# Create and establish the connection to the database.',
'connection = driver.createConnection(connectiondata)',
'if not connection.isConnected():',
' if not connection.connect():',
' raise Exception("Failed to connect")',
'',
'# Open the database for usage.',
'if not connection.isDatabaseUsed():',
' if not connection.useDatabase( connectiondata.databaseName() ):',
' if not connection.useDatabase( connectiondata.fileName() ):',
' raise Exception("Failed to use database")',
'',
'# Get the table and create a query for it.',
'table = connection.tableSchema( \"{TableName}\" )',
'query = table.query()',
'',
'# Create a cursor to walk through the records.',
'cursor = connection.executeQuerySchema( query )',
'if not cursor:',
' raise Exception("Failed to create cursor.")',
'',
'# Iterate through the records.',
'if not cursor.moveFirst():',
' raise Exception("The cursor has no records to read from.")',
'while not cursor.eof():',
' for i in range( cursor.fieldCount() ):',
' print("%s" % cursor.value(i))',
' cursor.moveNext()',
)
####################################################################################
# KSpread
class KSpread:
def __init__(self, parentwidget):
self.parentwidget = parentwidget
class _SheetWidget(ListWidget):
def __init__(self, parentwidget, label = "Sheet:"):
global ListWidget
ListWidget.__init__(self, parentwidget, label)
try:
import krosskspreadcore
document = krosskspreadcore.get("KSpreadDocument")
for sheetname in document.sheetNames():
self.combo.insertItem(sheetname)
except:
import traceback
trace = "".join( traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]) )
print(trace)
class _CellsWidget(ListWidget):
def __init__(self, parentwidget):
global ListWidget
ListWidget.__init__(self, parentwidget, "Cells (col1:row1 - col2:row2):")
self.combo.insertItem( "1:1 - %s:%s" % (5,10) )
self.combo.insertItem( "1:1 - %s:%s" % (256,256) )
self.combo.insertItem( "1:1 - %s:%s" % (32767,32767) )
def value(self):
return [ [ int(i) for i in item.split(':') ] for item in str( ListWidget.value(self) ).split('-') ]
class _ValueWidget(EditWidget):
def __init__(self, parentwidget):
global EditWidget
EditWidget.__init__(self, parentwidget, "Value:")
self.edit.setText("Some text")
class _ColorWidget(EditWidget):
def __init__(self, parentwidget,label,color):
global EditWidget
EditWidget.__init__(self, parentwidget, "%s (RGB):" % label)
self.edit.setText(color)
def value(self):
return "#%s" % EditWidget.value(self)
class SetTextOfCells:
""" Set the text of the defined cells. """
name = "Set text of cells"
def __init__(self, parent):
pass
self.widgets = {
"SheetName" : Samples.KSpread._SheetWidget( parent.parentwidget ),
"Cells" : Samples.KSpread._CellsWidget( parent.parentwidget ),
"Value" : Samples.KSpread._ValueWidget( parent.parentwidget ),
}
def getCode(self):
return (
'# Import the KSpread module.',
'import krosskspreadcore',
'',
'# Get the current document.',
'document = krosskspreadcore.get("KSpreadDocument")',
'',
'# Get the sheet defined by the sheetname.',
'sheet = document.sheetByName( \"{SheetName}\" )',
'if not sheet:',
' raise Exception("No such sheet {SheetName} %s" % document.sheetNames())',
'',
'( (col1,row1),(col2,row2) ) = {Cells}',
'for c in range(col1,col2):',
' for r in range(row1,row2):',
' cell = sheet.cell(c,r)',
' print("cell c=%s r=%s v=%s" % (c,r,cell.value()))',
' cell.setText( \"{Value}\" )',
)
class SetColorsOfCells:
""" Set the colors of the defined cells. """
name = "Set colors of cells"
def __init__(self, parent):
pass
self.widgets = {
"SheetName" : Samples.KSpread._SheetWidget( parent.parentwidget),
"Cells" : Samples.KSpread._CellsWidget( parent.parentwidget ),
"TextColor" : Samples.KSpread._ColorWidget( parent.parentwidget, "Textcolor", "ff0000" ),
"BackgroundColor" : Samples.KSpread._ColorWidget( parent.parentwidget, "Backgroundcolor", "c0c0c0" ),
}
def getCode(self):
return (
'# Import the KSpread module.',
'import krosskspreadcore',
'',
'# Get the current document.',
'document = krosskspreadcore.get("KSpreadDocument")',
'',
'# Get the sheet defined by the sheetname.',
'sheet = document.sheetByName( \"{SheetName}\" )',
'if not sheet:',
' raise Exception("No such sheet {SheetName} %s" % document.sheetNames())',
'',
'( (col1,row1),(col2,row2) ) = {Cells}',
'for c in range(col1,col2):',
' for r in range(col1,col2):',
' cell = sheet.cell(c,r)',
' cell.setTextColor( \"{TextColor}\" )',
' cell.setBackgroundColor( \"{BackgroundColor}\" )',
)
class IterateThroughCellsWithContent:
""" Iterate over all cells in a sheet that have content (aka that are not empty). """
name = "Iterate through cells"
def __init__(self, parent):
pass
self.widgets = {
"SheetName" : Samples.KSpread._SheetWidget( parent.parentwidget ),
}
def getCode(self):
return (
'# Import the KSpread module.',
'import krosskspreadcore',
'',
'# Get the current document.',
'document = krosskspreadcore.get("KSpreadDocument")',
'',
'# Get the sheet defined by the sheetname.',
'sheet = document.sheetByName( \"{SheetName}\" )',
'if not sheet:',
' raise Exception("No such sheet {SheetName} %s" % document.sheetNames())',
'',
'# Iterate through the cells that have content (aka that are not empty).',
'cell = sheet.firstCell()',
'while cell:',
' print("col=%s row=%s value=%s" % (cell.column(),cell.row(),cell.value()))',
' cell = cell.nextCell()',
)
class PrintSheetDetails:
""" Print details about the current sheet. """
name = "Details about a sheet"
def __init__(self, parent):
self.widgets = {
"SheetName" : Samples.KSpread._SheetWidget( parent.parentwidget ),
}
def getCode(self):
return (
'# Import the KSpread module.',
'import krosskspreadcore',
'',
'# Get the current document.',
'document = krosskspreadcore.get("KSpreadDocument")',
'',
'# Get the sheet defined by the sheetname.',
'sheet = document.sheetByName( \"{SheetName}\" )',
'if not sheet:',
' raise Exception("No such sheet {SheetName} %s" % document.sheetNames())',
'',
'print("name=%s" % sheet.name())',
'print("maxcolumns=%s maxrows=%s" % (sheet.maxColumn(),sheet.maxRow()))',
)
class LoadDocFromNativeXML:
""" Load the document from a native XML file. """
name = "Load document from native XML File"
def __init__(self, parent):
global FileWidget
self.widgets = {
"FileName" : FileWidget( parent.parentwidget, "XML File:", "*.xml;;*" ),
}
def getCode(self):
return (
'# Import the PyTQt module.',
'from TQt import tqt',
'',
'def loadFile(filename):',
' # Import the krosskspreadcore module.',
' import krosskspreadcore',
' # Try to read the file.',
' try:',
' file = open(filename, "r")',
' xml = file.read()',
' file.close()',
' except IOError, (errno, strerror):',
' tqt.TQMessageBox.critical(self,"Error","<qt>Failed to read file %s<br><br>%s</qt>" % (filename,strerror))',
' return',
'',
' # Get the current document.',
' document = krosskspreadcore.get("KSpreadDocument")',
' # Load the document from the native XML string.',
' ok = document.loadNativeXML( xml )',
'',
'# Show the openfile dialog',
'filename = "{FileName}"',
'openfilename = tqt.TQFileDialog.getOpenFileName(filename,"*.xml;;*", self)',
'if str(openfilename) != "":',
' loadFile( openfilename )',
)
class SaveDocToNativeXML:
""" Save the document to a native XML file. """
name = "Save document to native XML File"
def __init__(self, parent):
global FileWidget
self.widgets = {
"FileName" : FileWidget( parent.parentwidget, "XML File:", "*.xml;;*", False ),
}
def getCode(self):
return (
'# Import the PyTQt module.',
'from TQt import tqt',
'',
'def saveFile(filename):',
' # Import the krosskspreadcore module.',
' import krosskspreadcore',
' # Try to open the file for writting.',
' try:',
' file = open(filename, "w")',
' except IOError, (errno, strerror):',
' tqt.TQMessageBox.critical(self,"Error","<qt>Failed to create file %s<br><br>%s</qt>" % (filename,strerror))',
' return',
' # Get the current document.',
' document = krosskspreadcore.get("KSpreadDocument")',
' # Get the native XML string.',
' xml = document.saveNativeXML()',
' # Write the XML string to the file.',
' file.write( xml )',
' # Close the file.',
' file.close()',
'',
'# Show the savefile dialog',
'filename = "{FileName}"',
'savefilename = tqt.TQFileDialog.getSaveFileName(filename,"*.xml;;*", self)',
'if str(savefilename) != "":',
' saveFile( savefilename )',
)
class CopySheets:
""" Copy the text-content from one sheet to another. """
name = "Copy sheets"
def __init__(self, parent):
self.widgets = {
"SourceSheet" : Samples.KSpread._SheetWidget( parent.parentwidget, "Source sheet:" ),
"TargetSheet" : Samples.KSpread._SheetWidget( parent.parentwidget, "Target sheet:" ),
}
def getCode(self):
return (
'# Import the KSpread module.',
'import krosskspreadcore',
'# Get the current document.',
'document = krosskspreadcore.get("KSpreadDocument")',
'# Get the source sheet.',
'fromsheet = document.sheetByName( "{SourceSheet}" )',
'if not fromsheet: raise Exception("No such sheet {SourceSheet} %s" % document.sheetNames())',
'# Get the target sheet.',
'tosheet = document.sheetByName( "{TargetSheet}" )',
'if not fromsheet: raise Exception("No such sheet {TargetSheet} %s" % document.sheetNames())',
'# Copy the cells.',
'fromcell = fromsheet.firstCell()',
'while fromcell:',
' tocell = tosheet.cell( fromcell.column(), fromcell.row() )',
' tocell.setText( fromcell.text() )',
' #tocell.setValue( fromcell.value() )',
' fromcell = fromcell.nextCell()',
)
class LoadSheetFromCSV:
""" Load the content of a CSV file into a KSpread sheet. """
name = "Load data from CSV file into sheet"
def __init__(self, parent):
self.widgets = {
"Sheet" : Samples.KSpread._SheetWidget( parent.parentwidget ),
"FileName" : FileWidget( parent.parentwidget, "CSV File:", "*.csv;;*", True ),
}
def getCode(self):
return (
'# Import the KSpread module.',
'import krosskspreadcore',
'# Get the current document and the sheet.',
'document = krosskspreadcore.get("KSpreadDocument")',
'sheet = document.sheetByName( "{Sheet}" )',
'if not sheet: raise Exception("No such sheet {Sheet} %s" % document.sheetNames())',
'',
'filename = "{FileName}"',
'try:',
' file = open(filename, "r")',
'except IOError:',
' raise Exception("Failed to open CSV File: %s" % filename)',
'',
'import csv',
'csvparser = csv.reader(file)',
'row = 1',
'while True:',
' try:',
' record = csvparser.next()',
' except StopIteration:',
' break',
' col = 1',
' for item in record:',
' sheet.cell(col,row).setText( item )',
' col += 1',
' row += 1',
'file.close()',
)
class SaveSheetToCSV:
""" Save the content of a KSpread sheet into a CSV file. """
name = "Save data from a sheet into a CSV file"
def __init__(self, parent):
self.widgets = {
"Sheet" : Samples.KSpread._SheetWidget( parent.parentwidget ),
"FileName" : FileWidget( parent.parentwidget, "CSV File:", "*.csv;;*", False ),
}
def getCode(self):
return (
'filename = "{FileName}"',
'try:',
' file = open(filename, "w")',
'except IOError:',
' raise Exception("Failed to write CSV File: %s" % filename)',
'# Prepare CSV-writer',
'import csv',
'csvwriter = csv.writer(file)',
'# Import the KSpread module.',
'import krosskspreadcore',
'# Get the current document and the sheet.',
'document = krosskspreadcore.get("KSpreadDocument")',
'sheet = document.sheetByName( "{Sheet}" )',
'if not sheet: raise Exception("No such sheet {Sheet} %s" % document.sheetNames())',
'# Iterate over the cells.',
'cell = sheet.firstCell()',
'record = []',
'while cell:',
' record.append( cell.text() )',
' if cell.column() == 1 or not cell.nextCell():',
' csvwriter.writerow( record )',
' record = []',
' cell = cell.nextCell()',
'file.close()',
)
####################################################################################
# PyTQt
class PyTQt:
def __init__(self, parentwidget):
self.parentwidget = parentwidget
class OpenFileDialog:
""" Show the usage of the openfile dialog with TQFileDialog. """
name = "Open File Dialog"
def __init__(self, parent):
pass
self.widgets = {
"FileName" : FileWidget( parent.parentwidget, "Open File:", "*.txt *.html;;*" ),
}
def getCode(self):
return (
'from TQt import tqt',
'openfilename = tqt.TQFileDialog.getOpenFileName("{FileName}","*.txt *.html;;*", self)',
'print("openfile=%s" % openfilename)',
)
class SaveFileDialog:
""" Show the usage of the savefile dialog with TQFileDialog. """
name = "Save File Dialog"
def __init__(self, parent):
pass
self.widgets = {
"FileName" : FileWidget( parent.parentwidget, "Save File:", "*.txt *.html;;*", False ),
}
def getCode(self):
return (
'from TQt import tqt',
'savefilename = tqt.TQFileDialog.getSaveFileName("{FileName}","*.txt *.html;;*", self)',
'print("savefile=%s" % savefilename)',
)
class CustomDialog:
""" Show a custom dialog that inherits a TQDialog. """
name = "Custom Dialog"
def __init__(self, parent):
pass
self.widgets = {
}
def getCode(self):
return (
'from TQt import tqt',
'',
'class MyDialog(tqt.TQDialog):',
' def __init__(self, parent):',
' from TQt import tqt',
' tqt.TQDialog.__init__(self, parent, "MyDialog", 1, tqt.TQt.WDestructiveClose)',
' self.setCaption("My Dialog")',
' btn = tqt.TQPushButton("Click me",self)',
' tqt.TQObject.connect(btn, tqt.TQ_SIGNAL("clicked()"), self.buttonClicked)',
' def buttonClicked(self):',
' from TQt import tqt',
' tqt.TQMessageBox.information(self, "The Caption", "This is the message string.")',
'',
'dialog = MyDialog(self)',
'dialog.exec_loop()',
)
class InputDialog:
""" Show how to use a TQInputDialog. """
name = "Input Dialog"
def __init__(self, parent):
global EditWidget
self.widgets = {
"Caption" : EditWidget( parent.parentwidget, "Caption" ),
"Message" : EditWidget( parent.parentwidget, "Message" ),
}
def getCode(self):
return (
'from TQt import tqt',
'',
'text, ok = tqt.TQInputDialog.getText("{Caption}", "{Message}", tqt.TQLineEdit.Normal, "")',
'if ok:',
' print("Text defined: %s" % text)',
'else:',
' print("Dialog aborted.")',
)
####################################################################################
# DCOP
class DCOP:
def __init__(self, parentwidget):
self.parentwidget = parentwidget
class PrintClipboard:
""" Print the content from the clipper via DCOP. """
name = "Clipboard content"
def __init__(self, parent):
self.widgets = {
}
def getCode(self):
return (
'from TQt import tqt',
'import tdecore, dcop, dcopext',
'dcopclient = tdecore.TDEApplication.dcopClient()',
'apps = [ app for app in dcopclient.registeredApplications() if str(app).startswith("klipper") ]',
'd = dcopext.DCOPApp(apps[0], dcopclient)',
'result,typename,data = d.appclient.call(apps[0],"klipper","getClipboardContents()","")',
'ds = tqt.TQDataStream(data, tqt.IO_ReadOnly)',
'print("Clipboard content:\\n%s" % tdecore.dcop_next(ds, "TQString"))',
)
class AmarokCollectionInfos:
""" Fetch some collection informations from the amarok collection via DCOP. """
name = "amarok collection infos"
def __init__(self, parent):
self.widgets = {
}
def getCode(self):
return (
'from TQt import tqt',
'import tdecore, dcop, dcopext',
'',
'dcopclient = tdecore.TDEApplication.dcopClient()',
'apps = [ app for app in dcopclient.registeredApplications() if str(app).startswith("amarok") ]',
'app = apps[0]',
'd = dcopext.DCOPApp(app, dcopclient)',
'',
'def dataToList(data, types = []):',
' from TQt import tqt',
' import tdecore',
' ds = tqt.TQDataStream(data, tqt.IO_ReadOnly)',
' return [ tdecore.dcop_next(ds, t) for t in types ]',
'',
'for funcname in ["totalAlbums","totalArtists","totalCompilations","totalGenres","totalTracks"]:',
' result,replytype,replydata = d.appclient.call("amarok", "collection", "%s()" % funcname,"")',
' print("%s: %s" % ( funcname, dataToList(replydata,["int"])[0] ))',
)
class KopeteContacts:
""" Print the names of all contacts Kopete knows via DCOP. """
name = "Kopete contacts"
def __init__(self, parent):
self.widgets = {
}
def getCode(self):
return (
'from TQt import tqt',
'import tdecore, dcop, dcopext',
'',
'dcopclient = tdecore.TDEApplication.dcopClient()',
'apps = [ app for app in dcopclient.registeredApplications() if str(app).startswith("kopete") ]',
'app = apps[0]',
'd = dcopext.DCOPApp(app, dcopclient)',
'',
'(state,rtype,rdata) = d.appclient.call("kopete", "KopeteIface", "contacts()","")',
'if not state: raise Exception("Failed to call the kopete contacts-function")',
'',
'ds = tqt.TQDataStream(rdata.data(), tqt.IO_ReadOnly)',
'sl = tdecore.dcop_next (ds, "TQStringList")',
'print("contacts=%s" % [ str(s) for s in sl ])',
)
class KWordSelectedText:
""" Get the selected text from a KWord instance via DCOP. """
name = "KWord selected text"
def __init__(self, parent):
self.widgets = {
}
def getCode(self):
return (
'from TQt import tqt',
'import tdecore, dcop, dcopext',
'',
'def dataToList(data, types = []):',
' from TQt import tqt',
' import tdecore',
' ds = tqt.TQDataStream(data, tqt.IO_ReadOnly)',
' return [ tdecore.dcop_next(ds, t) for t in types ]',
'def listToData(listdict):',
' from TQt import tqt',
' import tdecore',
' ba= tqt.TQByteArray()',
' ds = tqt.TQDataStream(ba, tqt.IO_WriteOnly)',
' for (typename,value) in listdict:',
' tdecore.dcop_add (ds, value, typename)',
' return ba',
'',
'# Get the KWord DCOP client.',
'dcopclient = tdecore.TDEApplication.dcopClient()',
'apps = [ app for app in dcopclient.registeredApplications() if str(app).startswith("kword") ]',
'if len(apps) < 1: raise Exception("No KWord instance is running. Please start KWord before!")',
'appname = apps[0]',
'd = dcopext.DCOPApp(appname, dcopclient)',
'',
'# Call the getDocuments() function.',
'(state,rtype,rdata) = d.appclient.call(appname, "KoApplicationIface", "getDocuments()","")',
'if not state: raise Exception("%s: Failed to call getDocuments-function" % appname)',
'documents = dataToList(rdata,["TQValueList<DCOPRef>"])[0]',
'print("documents=%s" % [ str( doc.obj() ) for doc in documents ])',
'document = documents[0] # Let\'s just take the first document.',
'',
'# Get the frameset.',
'ba = listToData( [ ("int",0) ] )',
'(state,rtype,rdata) = d.appclient.call(appname, document.obj(), "textFrameSet(int)", ba)',
'if not state: raise Exception("%s: Failed to call frameSet-function" % appname)',
'frameset = dataToList( rdata,["DCOPRef"] )[0] # Let\'s just take the first textframe.',
'',
'# Get the selected text.',
'(state,rtype,rdata) = d.appclient.call(appname, frameset.obj(), "selectedText()", "")',
'print("Selected Text: %s" % dataToList( rdata,["TQString"] )[0])',
)
####################################################################################
# Dialog implementations.
class SampleDialog(tqt.TQDialog):
def __init__(self, parent, sampleclazz, samplechildclazz):
from TQt import tqt
tqt.TQDialog.__init__(self, parent, "SampleDialog", 1)
layout = tqt.TQVBoxLayout(self)
box = tqt.TQVBox(self)
box.setMargin(4)
box.setSpacing(10)
layout.addWidget(box)
self.scrollview = tqt.TQScrollView(box)
self.scrollview.setResizePolicy(tqt.TQScrollView.AutoOne)
#self.scrollview.setFrameStyle(tqt.TQFrame.NoFrame);
self.scrollview.setResizePolicy(tqt.TQScrollView.AutoOneFit);
self.scrollview.viewport().setPaletteBackgroundColor(self.paletteBackgroundColor())
mainbox = tqt.TQVBox( self.scrollview.viewport() )
mainbox.setMargin(6)
mainbox.setSpacing(6)
desclabel = tqt.TQLabel(mainbox)
tqt.TQFrame(mainbox).setFrameStyle( tqt.TQFrame.HLine | tqt.TQFrame.Sunken )
self.sample = sampleclazz( mainbox )
self.samplechild = samplechildclazz( self.sample )
desclabel.setText( "<qt>%s</qt>" % self.samplechild.__doc__ )
mainbox.setStretchFactor(tqt.TQWidget(mainbox), 1)
mainbox.show()
self.scrollview.addChild(mainbox)
btnbox = tqt.TQHBox(box)
btnbox.setMargin(6)
btnbox.setSpacing(6)
okbtn = tqt.TQPushButton(btnbox)
okbtn.setText("Ok")
tqt.TQObject.connect(okbtn, tqt.TQ_SIGNAL("clicked()"), self.okClicked)
cancelbtn = tqt.TQPushButton(btnbox)
cancelbtn.setText("Cancel")
tqt.TQObject.connect(cancelbtn, tqt.TQ_SIGNAL("clicked()"), self.close)
self.setCaption(self.samplechild.name)
box.setMinimumSize(tqt.TQSize(500,340))
def okClicked(self):
self.code = self.samplechild.getCode()
self.close()
def getCode(self):
if not hasattr(self,"code"): return None
code = "\n".join( self.code )
for widgetname in list(self.samplechild.widgets.keys()):
print(".............. %s" % widgetname)
widget = self.samplechild.widgets[widgetname]
value = widget.value()
if value != None:
code = code.replace("{%s}" % widgetname, str(value))
return code
class MainDialog(tqt.TQDialog):
def __init__(self, scriptpath, parent):
self.scriptpath = scriptpath
if not hasattr(__main__,"scripteditorfilename"):
__main__.scripteditorfilename = self.getFileName("myscript.py")
import krosskspreadcore
self.doc = krosskspreadcore.get("KSpreadDocument")
import os
from TQt import tqt
tqt.TQDialog.__init__(self, parent, "MainDialog", 1, tqt.TQt.WDestructiveClose)
self.setCaption("Script Editor")
layout = tqt.TQVBoxLayout(self)
box = tqt.TQVBox(self)
box.setMargin(4)
box.setSpacing(10)
layout.addWidget(box)
menu = tqt.TQMenuBar(box)
splitter = tqt.TQSplitter(box)
splitter.setOrientation(tqt.TQt.Vertical)
self.scripttext = tqt.TQMultiLineEdit(splitter)
self.scripttext.setWordWrap( tqt.TQTextEdit.NoWrap )
self.scripttext.setTextFormat( tqt.TQt.PlainText )
tqt.TQObject.connect(self.scripttext, tqt.TQ_SIGNAL("cursorPositionChanged(int,int)"),self.cursorPositionChanged)
self.console = tqt.TQTextBrowser(splitter)
splitter.setResizeMode(self.console, tqt.TQSplitter.KeepSize)
statusbar = tqt.TQStatusBar(box)
self.messagestatus = tqt.TQLabel(statusbar)
statusbar.addWidget(self.messagestatus,1)
self.cursorstatus = tqt.TQLabel(statusbar)
statusbar.addWidget(self.cursorstatus)
self.cursorPositionChanged()
box.setMinimumSize( tqt.TQSize(680,540) )
filemenu = tqt.TQPopupMenu(menu)
menu.insertItem("&File", filemenu)
newaction = tqt.TQAction("New", tqt.TQKeySequence("CTRL+N"), self)
tqt.TQObject.connect(newaction, tqt.TQ_SIGNAL("activated()"), self.newFile)
newaction.addTo(filemenu)
openaction = tqt.TQAction("Open...", tqt.TQKeySequence("CTRL+O"), self)
tqt.TQObject.connect(openaction, tqt.TQ_SIGNAL("activated()"), self.openFileAs)
openaction.addTo(filemenu)
saveaction = tqt.TQAction("Save", tqt.TQKeySequence("CTRL+S"), self)
tqt.TQObject.connect(saveaction, tqt.TQ_SIGNAL("activated()"), self.saveFile)
saveaction.addTo(filemenu)
saveasaction = tqt.TQAction("Save as...", tqt.TQKeySequence("CTRL+A"), self)
tqt.TQObject.connect(saveasaction, tqt.TQ_SIGNAL("activated()"), self.saveFileAs)
saveasaction.addTo(filemenu)
filemenu.insertSeparator()
quitaction = tqt.TQAction("Quit", tqt.TQKeySequence("CTRL+Q"), self)
tqt.TQObject.connect(quitaction, tqt.TQ_SIGNAL("activated()"), self.close)
quitaction.addTo(filemenu)
editmenu = tqt.TQPopupMenu(menu)
menu.insertItem("&Edit", editmenu)
undoaction = tqt.TQAction("Undo", tqt.TQKeySequence("CTRL+Z"), self)
tqt.TQObject.connect(undoaction, tqt.TQ_SIGNAL("activated()"), self.scripttext.undo)
undoaction.addTo(editmenu)
redoaction = tqt.TQAction("Redo", tqt.TQKeySequence("CTRL+Shift+Z"), self)
tqt.TQObject.connect(redoaction, tqt.TQ_SIGNAL("activated()"), self.scripttext.redo)
redoaction.addTo(editmenu)
editmenu.insertSeparator()
cutaction = tqt.TQAction("Cut", tqt.TQKeySequence("CTRL+X"), self)
tqt.TQObject.connect(cutaction, tqt.TQ_SIGNAL("activated()"), self.scripttext.cut)
cutaction.addTo(editmenu)
copyaction = tqt.TQAction("Copy", tqt.TQKeySequence("CTRL+C"), self)
tqt.TQObject.connect(copyaction, tqt.TQ_SIGNAL("activated()"), self.scripttext.copy)
copyaction.addTo(editmenu)
pasteaction = tqt.TQAction("Paste", tqt.TQKeySequence("CTRL+V"), self)
tqt.TQObject.connect(pasteaction, tqt.TQ_SIGNAL("activated()"), self.scripttext.paste)
pasteaction.addTo(editmenu)
clearaction = tqt.TQAction("Clear", tqt.TQKeySequence("CTRL+Shift+X"), self)
tqt.TQObject.connect(clearaction, tqt.TQ_SIGNAL("activated()"), self.scripttext.clear)
clearaction.addTo(editmenu)
editmenu.insertSeparator()
selallaction = tqt.TQAction("Select All", 0, self)
tqt.TQObject.connect(selallaction, tqt.TQ_SIGNAL("activated()"), self.scripttext.selectAll)
selallaction.addTo(editmenu)
scriptmenu = tqt.TQPopupMenu(menu)
menu.insertItem("&Script", scriptmenu)
compileaction = tqt.TQAction("Compile", tqt.TQKeySequence("F9"), self)
tqt.TQObject.connect(compileaction, tqt.TQ_SIGNAL("activated()"), self.compileScript)
compileaction.addTo(scriptmenu)
executeaction = tqt.TQAction("Execute", tqt.TQKeySequence("F10"), self)
tqt.TQObject.connect(executeaction, tqt.TQ_SIGNAL("activated()"), self.executeScript)
executeaction.addTo(scriptmenu)
self.samplemenu = tqt.TQPopupMenu(menu)
menu.insertItem("&Samples", self.samplemenu)
itemid = 500
global Samples
for samplename in dir(Samples):
if samplename.startswith("_"): continue
itemid += 1
menu = tqt.TQPopupMenu(self.samplemenu)
tqt.TQObject.connect(menu, tqt.TQ_SIGNAL("activated(int)"), self.sampleActivated)
self.samplemenu.insertItem(samplename, menu, -1, self.samplemenu.count() - 1)
attr = getattr(Samples,samplename)
for a in dir(attr):
if a.startswith("_"): continue
itemid += 1
child = getattr(attr,a)
itemid = menu.insertItem(child.name, itemid)
menu.setWhatsThis(itemid,"%s/%s" % (samplename,a))
if os.path.exists(__main__.scripteditorfilename):
self.openFile(__main__.scripteditorfilename)
def getFileName(self, filename):
import os
try:
homepath = os.getenv("HOME")
if not homepath:
import pwd
user = os.getenv("USER") or os.getenv("LOGNAME")
if not user:
pwent = pwd.getpwuid(os.getuid())
else:
pwent = pwd.getpwnam(user)
homepath = pwent[6]
except (KeyError, ImportError):
homepath = os.curdir
return os.path.join(homepath, filename)
def cursorPositionChanged(self,para = 0,pos = 0):
self.cursorstatus.setText( "Line: %s Col: %s" % (para+1,pos+1) )
def sampleActivated(self, index):
global Samples
sampleid = str( self.sender().whatsThis(index) )
sampleidlist = sampleid.split('/')
sampleclazz = getattr( Samples,sampleidlist[0] )
samplechildclazz = getattr( sampleclazz, sampleidlist[1] )
global SampleDialog
dialog = SampleDialog(self, sampleclazz, samplechildclazz)
dialog.exec_loop()
code = dialog.getCode()
if code != None:
self.scripttext.append( code )
def execCode(self,function):
import sys, io
codeOut = io.StringIO()
codeErr = io.StringIO()
sys.stdout = codeOut
sys.stderr = codeErr
try:
function(self)
except:
import traceback
trace = "".join( traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]) )
sys.stderr.write(trace)
try:
# this is a bit tricky. we need to go to steps back to know where the exception really happened.
tb = sys.exc_info()[2]
while hasattr(tb,"tb_next") and tb.tb_next:
tb = tb.tb_next
lineno = tb.tb_lineno
print("EXCEPTION: lineno=%s" % lineno)
self.scripttext.setCursorPosition( lineno - 1, 0 )
except:
pass
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
s = codeErr.getvalue()
if s:
print("ERROR:\n%s\n" % s)
self.console.append(s)
s = codeOut.getvalue()
if s:
print(s)
self.console.append(s)
codeOut.close()
codeErr.close()
def compileScript(self):
self.console.clear()
code = str( self.scripttext.text() )
def docompile(self):
compile(code, __main__.scripteditorfilename, 'exec')
self.execCode(docompile)
self.console.append("<b>Compiled!</b>")
def executeScript(self):
self.console.clear()
def doexecute(self):
code = str( self.scripttext.text() )
exec(code, globals(), locals())
self.execCode(doexecute)
self.console.append("<b>Execution done!</b>")
def newFile(self):
self.console.clear()
#if tqt.TQMessageBox.warning(self,"Remove?","Remove the selected item?",tqt.TQMessageBox.Yes,tqt.TQMessageBox.Cancel) != tqt.TQMessageBox.Yes:
self.scripttext.clear()
def openFile(self, filename):
__main__.scripteditorfilename = None
try:
file = open(filename, "r")
self.scripttext.setText( str( file.read() ) )
file.close()
__main__.scripteditorfilename = filename
except IOError as xxx_todo_changeme:
(errno, strerror) = xxx_todo_changeme.args
tqt.TQMessageBox.critical(self,"Error","<qt>Failed to open script file \"%s\"<br><br>%s</qt>" % (filename,strerror))
def openFileAs(self):
from TQt import tqt
self.console.clear()
filename = str( tqt.TQFileDialog.getOpenFileName(__main__.scripteditorfilename,"*.py;;*", self) )
if filename == "": return
self.openFile(filename)
def saveFile(self):
try:
file = open(__main__.scripteditorfilename, "w")
file.write( str( self.scripttext.text() ) )
file.close()
except IOError as xxx_todo_changeme1:
(errno, strerror) = xxx_todo_changeme1.args
tqt.TQMessageBox.critical(self,"Error","<qt>Failed to open script file \"%s\"<br><br>%s</qt>" % (__main__.scripteditorfilename,strerror))
def saveFileAs(self):
from TQt import tqt
filename = str( tqt.TQFileDialog.getSaveFileName(__main__.scripteditorfilename,"*.py;;*", self) )
if filename == "": return
__main__.scripteditorfilename = filename
self.saveFile()
####################################################################################
# Show the main dialog.
if __name__ == "__main__":
scriptpath = os.getcwd()
tqtapp = tqt.TQApplication(sys.argv)
else:
scriptpath = os.path.dirname(__name__)
tqtapp = tqt.tqApp
dialog = MainDialog(scriptpath, tqtapp.mainWidget())
dialog.exec_loop()