Top > OOobbs3 > 45

OOobbs3/45 Edit

  • サマリ: 範囲選択ダイアログ - Python
  • 環境: Calc
  • 状態: 投稿
  • 投稿者: はにゃ??
  • 投稿日: 2011-01-31 (月) 00:07:06

質問 Edit

Calc で範囲を選択させる方法はありますが、ダイアログで行おうとするとノンモダルダイアログでなければうまく動作しません。しかし、安定なものを作成するのは結構大変です。

Basic で書くと複数のウィンドウで同時に利用できるようにうまくなりませんでした・・・。

Python で書いたものを置いておきます。

  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# -*- coding: utf-8 -*-
 
# v0.1
import uno
import unohelper
 
from com.sun.star.awt import XActionListener, Rectangle
from com.sun.star.awt.PosSize import POS as PS_POS
from com.sun.star.lang import XComponent
 
 
class DialogBase(unohelper.Base, object):
    """ Helper class to make dialog class. """
    
    def __init__(self, ctx, frame, dialog_url="", string_resource={}):
        self.ctx = ctx
        self._frame = frame
        self.dialog_url = dialog_url
        self.strres = string_resource
        
        self._disposed = False
        self._dialog = None
    
    def _s(self, text):
        """ Translates text. """
        return self.strres.get(text.replace(" ", "_"), text)
    
    # XEventListener
    def disposing(self, ev):
        self.ctx = None
        self._dialog = None
        self._strres = None
        self._frame = None
    
    def _dispose(self):
        """ Destroy the dialog. """
        if self._disposed: return
        self._dialog.dispose()
        self._disposed = True
    
    def centering_dialog(self):
        """ Put the dialog on center of the parent window. """
        if not self._dialog: return
        ps = self._frame.getContainerWindow().getPosSize()
        dps = self._dialog.getPosSize()
        self._dialog.setPosSize(
            (ps.Width - dps.Width)/2, (ps.Height - dps.Height)/2, 0, 0, PS_POS)
        
        x, y = self._dialog.getModel().getPropertyValues(("PositionX", "PositionY"))
        self._dialog.getModel().setPropertyValues(("PositionX", "PositionY"), (x, y))
    
    def _create_dialog(self, x=0, y=0, width=0, height=0, title=""):
        """ Create dialog instance. 
		if dialog_url has passed, the dialog is created from it. 
		Otherwise, runtime dialog creation is started.
		"""
        ctx = self.ctx
        smgr = self.ctx.getServiceManager()
        
        if self.dialog_url:
            dp = smgr.createInstanceWithContext(
                "com.sun.star.awt.DialogProvider", ctx)
            dialog = dp.createDialog(self.dialog_url)
        else:
            dialog = smgr.createInstanceWithContext(
                "com.sun.star.awt.UnoControlDialog", ctx)
            dialog_model = smgr.createInstanceWithContext(
                "com.sun.star.awt.UnoControlDialogModel", ctx)
            dialog_model.setPropertyValues(
                ("Height", "PositionX", "PositionY", "Title", "Width"), 
                (height, x, y, title, width))
            dialog.setModel(dialog_model)
        
        #dialog.setVisible(True)
        #dialog.createPeer(self._parent.getToolkit(), self._parent)
        #dialog.setVisible(False)
        self._dialog = dialog
    
    def _create_peer(self):
        parent = self._frame.getContainerWindow()
        self._dialog.createPeer(parent.getToolkit(), parent)
    
    def _add_control(self, control_type, name, x, y, width, height, names=(), values=()):
        """ Add new control according to passed values. """
        dialog_model = self._dialog.getModel()
        control_model = dialog_model.createInstance(
            control_type if control_type.startswith("com.sun.star.awt") else "com.sun.star.awt.UnoControl" + control_type + "Model")
        dialog_model.insertByName(name, control_model)
        
        control_model.setPropertyValues(
            ("Height", "PositionX", "PositionY", "Width"), 
            (height, x, y, width))
        if len(names) > 0:
            #print(names, values)
            control_model.setPropertyValues(names, values)
        return self._dialog.getControl(name)
    
    def _get_control(self, name):
        """ Get control from dialog. """
        return self._dialog.getControl(name)
        
    
    def get_label(self, label_name):
        """ Get label data from a control. """
        return self._dialog.getControl(label_name).getModel().Label
    
    def set_label(self, label_name, label):
        """ Set label data from a control. """
        self._dialog.getControl(label_name).getModel().Label = label
    
    def get_text(self, edit_name):
        """ Get text data from an edit control. """
        return self._dialog.getControl(edit_name).getModel().Text
    
    def set_text(self, edit_name, text):
        """ Set text data from an edit control. """
        self._dialog.getControl(edit_name).getModel().Text = text
    
    def get_radio_state(self, radio_name):
        """ Get state of radio button. """
        return self._dialog.getControl(radio_name).getModel().State
    
    def get_check_state(self, check_name):
        return self._dialog.getControl(check_name).getModel().State == 1
    
    def set_check_state(self, check_name, state):
        self._dialog.getControl(check_name).getModel().State = 1 if state else 0
    
    def set_focus(self, name):
        """ Set focus on a control. """
        self._dialog.getControl(name).setFocus()
    
    def _set_visible(self, state):
        """ Change visibility of the dialog. """
        if self._disposed: return
        self._dialog.setVisible(state)
    
    def set_state(self, name, state):
        if self._disposed: return
        self._dialog.getControl(name).setEnable(state)
    
    def set_visible(self, name, state):
        self._dialog.getControl(name).setVisible(state)
    
 
class ModalDialog(DialogBase):
    """ Modal dialog base class. """
    def __init__(self, ctx, frame, dialog_url="", string_resource={}):
        DialogBase.__init__(self, ctx, frame, string_resource)
        
        self._result = 0
    
    def execute(self):
        """ Show dialog and returns result of the pushed button. """
        if not self._dialog: return None
        self._set_visible(True)
        self._result = self._dialog.execute()
        return self._result
    
    def dispose_dialog(self):
        """ Destroy dialog. """
        self._dispose()
 
 
from com.sun.star.awt import XTopWindowListener
from com.sun.star.frame import XFrameActionListener
 
from com.sun.star.frame.FrameAction import COMPONENT_DETACHING as FA_COMPONENT_DETACHING
 
 
class ModelessDialog(DialogBase):
    """ Modeless dialog base class. """
    
    class TopWindowListener(unohelper.Base, XTopWindowListener):
        def __init__(self, parent):
            self._parent = parent
        def disposing(self, ev):
            self._parent = None
        # XTopWindowListener
        def windowOpened(self, ev): pass
        def windowClosing(self, ev):
            """ Helps to close if X button pushed on the title bar of the dialog. """
            self._parent.close()
        def windowClosed(self, ev): pass
        def windowMinimized(self, ev): pass
        def windowNormalized(self, ev): pass
        def windowActivated(self, ev): pass
        def windowDeactivated(self, ev): pass
    
    class FrameActionListener(unohelper.Base, XFrameActionListener):
        def __init__(self, parent):
            self._parent = parent
        def disposing(self, ev):
            self._parent = None
        # XFrameActionListener
        def frameAction(self, ev):
            """ Close dialog if the component of the frame has been changed. """
            if ev.Action == FA_COMPONENT_DETACHING:
                self._parent._remove_frame_action_listener(self)
                self._parent.close()
    
    def __init__(self, ctx, frame, dialog_url="", string_resource={}):
        DialogBase.__init__(self, ctx, frame, dialog_url, string_resource={})
        self._frame_listener = None
        self._topwindow_listener = None
    
    def _set_visible(self, state):
        """ Change visibility of the dialog. """
        if self._disposed: return
        self._dialog.setVisible(state)
    
    def _create_dialog(self, x=0, y=0, width=0, height=0, title=""):
        """ Creates dialog with additional listeners. """
        DialogBase._create_dialog(self, x, y, width, height, title)
        
        window_listener = self.TopWindowListener(self)
        frame_listener = self.FrameActionListener(self)
        self._dialog.addTopWindowListener(window_listener)
        self._frame.addFrameActionListener(frame_listener)
        self._topwindow_listener = window_listener
        self._frame_listener = frame_listener
    
    def show(self):
        """ Show dialog. """
        self._set_visible(True)
    
    def hide(self):
        """ Hide dialog. """
        self._set_visible(False)
    
    def close(self):
        """ Close and destroy dialog. """
        self.hide()
        self._dispose()
    
    # XEventListener
    def disposing(self, ev):
        self._remove_frame_action_listener(self._frame_listener)
        DialogBase.disposing(self, ev)
        self._frame_listener = None
        self._topwindow_listener = None
    
    def _remove_frame_action_listener(self, listener):
        self._frame.removeFrameActionListener(listener)
 
 
from com.sun.star.beans.PropertyState import DIRECT_VALUE as PS_DIRECT_VALUE
from com.sun.star.beans import PropertyValue
 
from com.sun.star.sheet import XRangeSelectionListener
 
import re
 
class RangeSelectionDialog(ModelessDialog):
    """ Modeless dialog with range selection functions. """
    
    range_selection_locked = False
    
    RANGE_SELECTION_ACTION_PREFIX = "__RANGE_SELECTION__"
    
    class RangeSelectionOptions(object):
        """ Used to pass range selection options,  
			see css.sheet.RangeSelectionArguments service.
		"""
        def __init__(self, edit_name, title, initial_value="", close_on_release=True, single_mode=False):
            self.edit_name = edit_name
            self.title = title
            self.initial_value = initial_value
            self.close_on_release = close_on_release
            self.single_mode = single_mode
    
    
    class RangeSelectionListenerImpl(unohelper.Base, XRangeSelectionListener):
        """ Tracks range selection changes. """
        def __init__(self, parent, edit_name):
            self._parent = parent
            self.edit_name = edit_name
        # XEventListener
        def disposing(self, ev):
            self._parent = None
        # XRangeSelectionListener
        def done(self, ev):
            desc = ev.RangeDescriptor
            desc = self._parse_descriptor(desc)
            self._parent.end_range_selection(self, self.edit_name, desc)
        
        def aborted(self, ev):
            self._parent.end_range_selection(self)
        
        def _parse_descriptor(self, desc):
            return desc.replace("$", "")
    
    class RangeSelectionButtonListenerImpl(unohelper.Base, XActionListener):
        def __init__(self, parent):
            self._parent = parent
        def disposing(self, ev):
            self._parent = None
        # XActionListener
        def actionPerformed(self, ev):
            command = ev.ActionCommand
            if command.startswith(self._parent.RANGE_SELECTION_ACTION_PREFIX):
                name = command[len(self._parent.RANGE_SELECTION_ACTION_PREFIX):]
                options = self._parent._range_selection_map[name]
                initial_value = options.initial_value if options.initial_value else self._parent.get_text(options.edit_name)
                self._parent.start_range_selection(options, initial_value)
    
    SMALL_BUTTON_WIDTH = 14
    SMALL_BUTTON_HEIGHT = 16
    
    def __init__(self, ctx, frame, dialog_url="", string_resource={}, range_selection_image_url=""):
        """ Initialize dialog. 
		@param ctx component context
		@param frame keeps spreadsheet document
		@param dialog_url dialog url to show if you want to use a dialog 
							created by the dialog editor.
		@param string_resource translation table for the runtime dialog creation
		@param range_selection_image_url image url for range selection buttons which created at runtime
		"""
        ModelessDialog.__init__(self, ctx, frame, dialog_url, string_resource)
        
        self.range_selection_image_url = range_selection_image_url
        self._controller = frame.getController()
        self._range_selection_listener = None
        self._range_selection_button_listener = self.RangeSelectionButtonListenerImpl(self)
        
        self._range_selection_map = {}
    
    def start_range_selection(self, options, initial_value=""):
        """ Starting range selection mode. """
        edit_name = options.edit_name
        if initial_value is None:
            initial_value = self.get_text(edit_name) if edit_name else ""
        args = self._get_range_selection_arguments(options, initial_value)
        #initial_value, title, close_on_release, single_mode)
        
        self.hide()
        
        self._range_selection_listener = self.RangeSelectionListenerImpl(self, edit_name)
        self._controller.addRangeSelectionListener(self._range_selection_listener)
        self._controller.startRangeSelection(args)
        
        self.range_selection_locked = True
    
    def end_range_selection(self, listener, edit_name=None, desc=""):
        """ Ends range selection mode. """
        if edit_name:
            self.set_text(edit_name, desc)
        #print("remove listener")
        self.show() # should be shown before the listener is removed
        self._controller.removeRangeSelectionListener(listener)
        self.range_selection_locked = False
        self._range_selection_listener = None
    
    def _get_range_selection_arguments(self, options, initial_value=""):
        """ Convert options to css.beans.PropertyValues """
        d = PS_DIRECT_VALUE
        return (
            PropertyValue("InitialValue", 0, initial_value, d), 
            PropertyValue("Title", 0, options.title, d), 
            PropertyValue("CloseOnMouseRelease", 0, options.close_on_release, d), 
            PropertyValue("SingleCellMode", 0, options.single_mode, d)
            )
    
    def _add_range_selection_button(self, name, x, y, width=SMALL_BUTTON_WIDTH, height=SMALL_BUTTON_HEIGHT):
        """ Adds button allows to start range selection. """
        return self._add_control("Button", name, x, y, width, height, 
            ("ImageURL", ), (self.range_selection_image_url,))
    
    def _add_edit(self, name, x, y, width, height, names=(), values=()):
        """ Adds edit control. """
        self._add_control("Edit", name, x, y, width, height, names, values)
    
    def _add_range_selection_set(self, edit_name, button_name, x, y, width, height, label_name="", 
        title="", title_from_label=True, initial_value="", on_mouse_release=True, 
        single_mode=False, named_range=False, range_names=None):
        """ Adds a set of range selection which has an edit control 
			shows range address and a push button to start range selection.
		
		"""
        V_SHIFT = (self.SMALL_BUTTON_HEIGHT - height) / 2
        if named_range and isinstance(range_names, tuple):
            range_edit = self._add_control("ComboBox", edit_name, x, y, width - self.SMALL_BUTTON_WIDTH - 1, height, 
            ("Dropdown", "LineCount"), (True, 7))
            range_edit.addItems(range_names, 0)
        else:
            self._add_edit(edit_name, x, y, width - self.SMALL_BUTTON_WIDTH - 1, height)
        btn = self._add_range_selection_button(button_name, x + width - self.SMALL_BUTTON_WIDTH, y - V_SHIFT)
        
        self._range_selection_registration(btn, edit_name, button_name, label_name, 
            title, title_from_label, initial_value, on_mouse_release, single_mode)
    
    def _register_range_selection_set(self, edit_name, button_name, label_name="", 
        title="", title_from_label=True, initial_value="", on_mouse_release=True, 
        single_mode=False, named_range=False, range_names=None):
        """ Register a set of controls allows to do range selection. """
        
        if named_range and isinstance(range_names, tuple):
            range_edit = self._get_control(edit_name)
            range_edit.addItems(range_names, 0)
        
        self._range_selection_registration(edit_name, button_name, label_name, 
            title, title_from_label, initial_value, on_mouse_release, single_mode)
        
    
    def _range_selection_registration(self, edit_name, button_name, label_name, 
        title, title_from_label, initial_value, on_mouse_release, single_mode):
        btn = self._get_control(button_name)
        btn.addActionListener(self._range_selection_button_listener)
        btn.setActionCommand(self.RANGE_SELECTION_ACTION_PREFIX + button_name)
        
        if title_from_label and label_name:
            title = self._get_label_as_title(label_name)
        options = self.RangeSelectionOptions(edit_name, title, initial_value, on_mouse_release, single_mode)
        
        self._range_selection_map[button_name] = options
    
    def _get_label_as_title(self, name):
        label = self.get_label(name)
        label_b = re.sub("\(~\S\)", "", label).replace("~", "")
        return label_b
    
    def messagebox(self, message, title, message_type, buttons):
        parent = self._frame.getContainerWindow()
        toolkit = parent.getToolkit()
        
        msgbox = toolkit.createMessageBox(parent, Rectangle(), message_type, buttons, title, message)
        return msgbox.execute()
    
    def message(self, message, title=""):
        """ Shows information. """
        return self.messagebox(message, title, "messbox", 1)
    
    def warning(self, message, title=""):
        """ Shows warning message. """
        return self.messagebox(message, title, "warningbox", 1)
    
    def error(self, message, title=""):
        """ Shows error message. """
        return self.messagebox(message, title, "errorbox", 1)

Edit

上記のコードを利用して次のようにします。

  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from dialog import RangeSelectionDialog
 
from com.sun.star.awt import XActionListener
 
class CustomSelectionDialog(RangeSelectionDialog, XActionListener):
    """ Example dialog uses RangeSelectionDialog class.
		
		The dialog created by the dialog editor and it has the following controls:
			buttonOk: OK button to start processing
			buttonCancel: Cancel button
			labelRange1: label for the range selection 1
			editRange1: first edit field shows in the single selection mode
			buttonRange1: button to start range selection
			labelRange2: label for the range selection 2
			editRange1: the combo box shows range selected and combo box keeps list of named ranges
			buttonRange2: to start
	"""
    
    def __init__(self, manager, ctx, frame):
        dialog_url = "vnd.sun.star.script:Standard.Dialog6?location=application"
        self.image_url = "file:///home/asuka/Documents/images/se.png"
        RangeSelectionDialog.__init__(self, ctx, frame, dialog_url=dialog_url)
        self.manager = manager
    
    def _initialize_dialog(self):
        """ set listeners on the dialog controls. """
        dialog = self._dialog
        gc = self._get_control
        
        # set listeners to buttons
        buttonOk = gc("buttonOk")
        buttonCancel = gc("buttonCancel")
        buttonOk.addActionListener(self)
        buttonCancel.addActionListener(self)
        buttonOk.setActionCommand("ok")
        buttonCancel.setActionCommand("cancel")
        
        gc("buttonRange1").getModel().ImageURL = self.image_url
        gc("buttonRange2").getModel().ImageURL = self.image_url
        
        range_names = self.manager.get_named_range_names()
        
        """ editRange1 is used in single selection mode
		"""
        self._register_range_selection_set("editRange1", "buttonRange1", label_name="labelRange1", 
            title="", title_from_label=True, initial_value="", 
            on_mouse_release=True, single_mode=True)
        
        """ editRange2 is the combo box and it shows named range
		"""
        self._register_range_selection_set("editRange2", "buttonRange2", label_name="labelRange2", 
            title="", title_from_label=True, initial_value="", 
            on_mouse_release=True, single_mode=False, named_range=True, range_names=range_names)
        
    
    def actionPerformed(self, ev):
        cmd = ev.ActionCommand
        if cmd == "ok":
            # if you want to check the values on the dialog, try in here
            # validation...
            # call hide method if you want to hide the dialog before any processing
            self.hide()
            self.manager.do_something()
            self.close()
        elif cmd == "cancel":
            # close and destroy the dialog, dispose method of the dialog is called also
            self.close()
    
    def get_selection1(self):
        """ Get first selected. """
        return self.get_text("editRange1")
    
    def get_selection2(self):
        """ Get secondary selected. """
        return self.get_text("editRange2")
    
    def start(self):
        """ Show dialog. """
        if not self._dialog:
            self._create_dialog() # DialogBase method
            self._initialize_dialog()
        self.show()
 
 
class Manager(object):
    """ Simple test application with the dialog. """
    def __init__(self, ctx, frame):
        self._dialog = CustomSelectionDialog(self, ctx, frame)
        self._frame = frame
        self.ctx = ctx
    
    def start(self):
        """ Start this. """
        self._dialog.start()
    
    def do_something(self):
        """ OK button pushed on the dialog. """
        message = "Range 1: %s\nRange 2: %s" % (
            self._dialog.get_selection1(), self._dialog.get_selection2())
        
        self._dialog.message(message)
    
    def get_named_range_names(self):
        """ list of named range is taken from the dialog. """
        doc = self._get_document()
        return doc.NamedRanges.getElementNames()
    
    def _get_document(self):
        return self._frame.getController().getModel()
 
 
def modeless_dialog_test(*args):
    """ Just an example to use range selection dialog. """
    ctx = XSCRIPTCONTEXT.getComponentContext()
    doc = XSCRIPTCONTEXT.getDocument()
    frame = doc.getCurrentController().getFrame()
    # should be check the document type is spreadsheet document
    
    manager = Manager(ctx, frame)
    manager.start()

回答 Edit


感想,コメント,メモ Edit



Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup   Help   RSS of recent changes