Add support for ODBC
This commit is contained in:
parent
55df1aa1bc
commit
ac4aac9b89
|
@ -85,6 +85,7 @@ from com.sun.star.awt import XWindowListener
|
|||
from com.sun.star.awt import XMenuListener
|
||||
from com.sun.star.awt import XKeyListener
|
||||
from com.sun.star.awt import XItemListener
|
||||
from com.sun.star.awt import XFocusListener
|
||||
|
||||
|
||||
try:
|
||||
|
@ -570,10 +571,10 @@ class LODocument(object):
|
|||
|
||||
def _init_values(self):
|
||||
self._type_doc = get_type_doc(self.obj)
|
||||
if self._type_doc == 'base':
|
||||
self._cc = self.obj.DatabaseDocument.getCurrentController()
|
||||
else:
|
||||
self._cc = self.obj.getCurrentController()
|
||||
# ~ if self._type_doc == 'base':
|
||||
# ~ self._cc = self.obj.DatabaseDocument.getCurrentController()
|
||||
# ~ else:
|
||||
self._cc = self.obj.getCurrentController()
|
||||
return
|
||||
|
||||
@property
|
||||
|
@ -1158,6 +1159,10 @@ class LOWriter(LODocument):
|
|||
def cursor(self):
|
||||
return self.text.createTextCursor()
|
||||
|
||||
@property
|
||||
def paragraphs(self):
|
||||
return [LOTextRange(p) for p in self.text]
|
||||
|
||||
@property
|
||||
def selection(self):
|
||||
sel = self.obj.getCurrentSelection()
|
||||
|
@ -1209,16 +1214,40 @@ class LOWriter(LODocument):
|
|||
self.insert_content(cursor, image)
|
||||
return
|
||||
|
||||
def go_start(self):
|
||||
cursor = self._cc.getViewCursor()
|
||||
cursor.gotoStart(False)
|
||||
return cursor
|
||||
|
||||
def go_end(self):
|
||||
cursor = self._cc.getViewCursor()
|
||||
cursor.gotoEnd(False)
|
||||
return cursor
|
||||
|
||||
def select(self, text):
|
||||
self._cc.select(text)
|
||||
return
|
||||
|
||||
|
||||
class LOTextRange(object):
|
||||
|
||||
def __init__(self, obj):
|
||||
self._obj = obj
|
||||
self._is_paragraph = self.obj.ImplementationName == 'SwXParagraph'
|
||||
self._is_table = self.obj.ImplementationName == 'SwXTextTable'
|
||||
|
||||
@property
|
||||
def obj(self):
|
||||
return self._obj
|
||||
|
||||
@property
|
||||
def is_paragraph(self):
|
||||
return self._is_paragraph
|
||||
|
||||
@property
|
||||
def is_table(self):
|
||||
return self._is_table
|
||||
|
||||
@property
|
||||
def string(self):
|
||||
return self.obj.String
|
||||
|
@ -1232,7 +1261,7 @@ class LOTextRange(object):
|
|||
return self.text.createTextCursorByRange(self.obj)
|
||||
|
||||
|
||||
class LOBase(LODocument):
|
||||
class LOBase(object):
|
||||
TYPES = {
|
||||
str: 'setString',
|
||||
int: 'setInt',
|
||||
|
@ -1267,13 +1296,33 @@ class LOBase(LODocument):
|
|||
if not self.exists:
|
||||
self._dbc.registerDatabaseLocation(name, path_url)
|
||||
else:
|
||||
db = self._dbc.getByName(name)
|
||||
self.path = _path_system(self._dbc.getDatabaseLocation(name))
|
||||
super().__init__(db)
|
||||
self._con = db.getConnection('', '')
|
||||
msg = 'Connected to: {}'.format(name)
|
||||
if name.startswith('odbc:'):
|
||||
self._con = self._odbc(name, kwargs)
|
||||
else:
|
||||
db = self._dbc.getByName(name)
|
||||
self.path = _path_system(self._dbc.getDatabaseLocation(name))
|
||||
self._con = db.getConnection('', '')
|
||||
|
||||
if self._con is None:
|
||||
msg = 'Not connected to: {}'.format(name)
|
||||
else:
|
||||
msg = 'Connected to: {}'.format(name)
|
||||
debug(msg)
|
||||
|
||||
def _odbc(self, name, kwargs):
|
||||
dm = create_instance('com.sun.star.sdbc.DriverManager')
|
||||
args = dict_to_property(kwargs)
|
||||
try:
|
||||
con = dm.getConnectionWithInfo('sdbc:{}'.format(name), args)
|
||||
return con
|
||||
except Exception as e:
|
||||
error(str(e))
|
||||
return None
|
||||
|
||||
@property
|
||||
def obj(self):
|
||||
return self._obj
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self._name
|
||||
|
@ -1299,6 +1348,10 @@ class LOBase(LODocument):
|
|||
self._dbc.registerDatabaseLocation(name, _path_url(path))
|
||||
return
|
||||
|
||||
def revoke(self, name):
|
||||
self._dbc.revokeDatabaseLocation(name)
|
||||
return True
|
||||
|
||||
def save(self):
|
||||
# ~ self._db.connection.commit()
|
||||
# ~ self._db.connection.getTables().refresh()
|
||||
|
@ -1337,8 +1390,8 @@ class LOBase(LODocument):
|
|||
else:
|
||||
cursor = self._con.createStatement()
|
||||
cursor.execute(sql)
|
||||
# ~ executeQuery
|
||||
# ~ executeUpdate
|
||||
# ~ resulset = cursor.executeQuery(sql)
|
||||
# ~ rows = cursor.executeUpdate(sql)
|
||||
self.save()
|
||||
return cursor
|
||||
|
||||
|
@ -1639,6 +1692,17 @@ class LOCellRange(object):
|
|||
img.setSize(Size(w, h))
|
||||
return
|
||||
|
||||
def insert_shape(self, tipo, **kwargs):
|
||||
s = self.obj.Size
|
||||
w = kwargs.get('width', s.Width)
|
||||
h = kwargs.get('Height', s.Height)
|
||||
img = self.doc.create_instance('com.sun.star.drawing.{}Shape'.format(tipo))
|
||||
set_properties(img, kwargs)
|
||||
self.draw_page.add(img)
|
||||
img.Anchor = self.obj
|
||||
img.setSize(Size(w, h))
|
||||
return
|
||||
|
||||
def select(self):
|
||||
self.doc._cc.select(self.obj)
|
||||
return
|
||||
|
@ -1837,6 +1901,20 @@ class EventsItemRoadmap(EventsItem):
|
|||
return
|
||||
|
||||
|
||||
class EventsFocus(EventsListenerBase, XFocusListener):
|
||||
|
||||
def __init__(self, controller, name):
|
||||
super().__init__(controller, name)
|
||||
|
||||
def focusGained(self, event):
|
||||
obj = event.Source.Model
|
||||
obj.BackgroundColor = COLOR_ON_FOCUS
|
||||
|
||||
def focusLost(self, event):
|
||||
obj = event.Source.Model
|
||||
obj.BackgroundColor = -1
|
||||
|
||||
|
||||
class EventsKey(EventsListenerBase, XKeyListener):
|
||||
"""
|
||||
event.KeyChar
|
||||
|
@ -2044,6 +2122,13 @@ class UnoBaseObject(object):
|
|||
def step(self, value):
|
||||
self.model.Step = value
|
||||
|
||||
@property
|
||||
def back_color(self):
|
||||
return self.model.BackgroundColor
|
||||
@back_color.setter
|
||||
def back_color(self, value):
|
||||
self.model.BackgroundColor = value
|
||||
|
||||
@property
|
||||
def rules(self):
|
||||
return self._rules
|
||||
|
@ -2301,6 +2386,10 @@ class UnoRoadmap(UnoBaseObject):
|
|||
self.model.insertByIndex(i, opt)
|
||||
return
|
||||
|
||||
def set_enabled(self, index, value):
|
||||
self.model.getByIndex(index).Enabled = value
|
||||
return
|
||||
|
||||
|
||||
def get_custom_class(tipo, obj):
|
||||
classes = {
|
||||
|
@ -2325,6 +2414,7 @@ def add_listeners(events, control, name=''):
|
|||
'addActionListener': EventsButton,
|
||||
'addMouseListener': EventsMouse,
|
||||
'addItemListener': EventsItem,
|
||||
'addFocusListener': EventsFocus,
|
||||
}
|
||||
if hasattr(control, 'obj'):
|
||||
control = contro.obj
|
||||
|
@ -2795,6 +2885,7 @@ class LODialog(object):
|
|||
self._model = self._obj.Model
|
||||
self._init_controls()
|
||||
self._events = None
|
||||
self._color_on_focus = -1
|
||||
return
|
||||
|
||||
def _create(self, properties):
|
||||
|
@ -2833,6 +2924,7 @@ class LODialog(object):
|
|||
'stardiv.Toolkit.UnoButtonControl': 'button',
|
||||
'stardiv.Toolkit.UnoEditControl': 'text',
|
||||
'stardiv.Toolkit.UnoRoadmapControl': 'roadmap',
|
||||
'stardiv.Toolkit.UnoFixedHyperlinkControl': 'link',
|
||||
}
|
||||
return types[name]
|
||||
|
||||
|
@ -2859,6 +2951,15 @@ class LODialog(object):
|
|||
def height(self, value):
|
||||
self.model.Height = value
|
||||
|
||||
@property
|
||||
def color_on_focus(self):
|
||||
return self._color_on_focus
|
||||
@color_on_focus.setter
|
||||
def color_on_focus(self, value):
|
||||
global COLOR_ON_FOCUS
|
||||
COLOR_ON_FOCUS = get_color(value)
|
||||
self._color_on_focus = COLOR_ON_FOCUS
|
||||
|
||||
@property
|
||||
def step(self):
|
||||
return self.model.Step
|
||||
|
@ -2880,7 +2981,6 @@ class LODialog(object):
|
|||
return
|
||||
|
||||
def open(self):
|
||||
# ~ self.model.Step = self.step
|
||||
return self.obj.execute()
|
||||
|
||||
def close(self, value=0):
|
||||
|
@ -3888,6 +3988,11 @@ def end():
|
|||
# ~ Export ok
|
||||
# ~ https://en.wikipedia.org/wiki/Web_colors
|
||||
def get_color(*value):
|
||||
if len(value) == 1 and isinstance(value[0], int):
|
||||
return value[0]
|
||||
if len(value) == 1 and isinstance(value[0], tuple):
|
||||
value = value[0]
|
||||
|
||||
COLORS = {
|
||||
'aliceblue': 15792383,
|
||||
'antiquewhite': 16444375,
|
||||
|
@ -4047,10 +4152,12 @@ def get_color(*value):
|
|||
color = (r << 16) + (g << 8) + b
|
||||
else:
|
||||
color = COLORS.get(value.lower(), -1)
|
||||
|
||||
return color
|
||||
|
||||
|
||||
COLOR_ON_FOCUS = get_color('LightYellow')
|
||||
|
||||
|
||||
# ~ Export ok
|
||||
def render(template, data):
|
||||
s = Template(template)
|
||||
|
@ -4431,3 +4538,28 @@ class LIBOServer(object):
|
|||
else:
|
||||
instance = self._sm.createInstance(name)
|
||||
return instance
|
||||
|
||||
|
||||
|
||||
|
||||
# ~ controls = {
|
||||
# ~ 'CheckBox': 'com.sun.star.awt.UnoControlCheckBoxModel',
|
||||
# ~ 'ComboBox': 'com.sun.star.awt.UnoControlComboBoxModel',
|
||||
# ~ 'CurrencyField': 'com.sun.star.awt.UnoControlCurrencyFieldModel',
|
||||
# ~ 'DateField': 'com.sun.star.awt.UnoControlDateFieldModel',
|
||||
# ~ 'FileControl': 'com.sun.star.awt.UnoControlFileControlModel',
|
||||
# ~ 'FixedLine': 'com.sun.star.awt.UnoControlFixedLineModel',
|
||||
# ~ 'FixedText': 'com.sun.star.awt.UnoControlFixedTextModel',
|
||||
# ~ 'FormattedField': 'com.sun.star.awt.UnoControlFormattedFieldModel',
|
||||
# ~ 'GroupBox': 'com.sun.star.awt.UnoControlGroupBoxModel',
|
||||
# ~ 'ImageControl': 'com.sun.star.awt.UnoControlImageControlModel',
|
||||
# ~ 'ListBox': 'com.sun.star.awt.UnoControlListBoxModel',
|
||||
# ~ 'NumericField': 'com.sun.star.awt.UnoControlNumericFieldModel',
|
||||
# ~ 'PatternField': 'com.sun.star.awt.UnoControlPatternFieldModel',
|
||||
# ~ 'ProgressBar': 'com.sun.star.awt.UnoControlProgressBarModel',
|
||||
# ~ 'ScrollBar': 'com.sun.star.awt.UnoControlScrollBarModel',
|
||||
# ~ 'SimpleAnimation': 'com.sun.star.awt.UnoControlSimpleAnimationModel',
|
||||
# ~ 'SpinButton': 'com.sun.star.awt.UnoControlSpinButtonModel',
|
||||
# ~ 'Throbber': 'com.sun.star.awt.UnoControlThrobberModel',
|
||||
# ~ 'TimeField': 'com.sun.star.awt.UnoControlTimeFieldModel',
|
||||
# ~ }
|
||||
|
|
Loading…
Reference in New Issue