summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/web/ImageListDialog.py
blob: 156ea150f74f216ca6ffa5e11d9df0cdc1e181b9 (plain)
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
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you under the Apache
#   License, Version 2.0 (the "License"); you may not use this file
#   except in compliance with the License. You may obtain a copy of
#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
#
import uno

from ..ui.UnoDialog2 import UnoDialog2
from ..ui.ImageList import ImageList
from ..common.HelpIds import HelpIds
from ..common.PropertyNames import PropertyNames
from ..common.IRenderer import IRenderer

from com.sun.star.awt import FontDescriptor
from com.sun.star.awt.PushButtonType import OK, CANCEL, HELP, STANDARD
from com.sun.star.awt import Size

'''
This class is a general implementation for a
dialog which displays a choice of images.
The model and the renderer are
still abstract in this class.
To use the class one should extend it,
in the constructor then set the imageList
properties (member name il) like image size, grid size,
model renderer aso, and then call "build".
This class uses a counter renderer which
gets an expression which contains %START %END %TOTAL and replaces
them with the actual numbers.
<BR/>
two buttons: "other" and "none" which are there
to enable choosing images that are not in the list and
"no image" respectivley, are optional, with default to True,
so dialogs which do not need those, should set the corresponding
members showDeselectButton and/or showOtherButton to false.
<br/>
the consturctor should receive, among others, an Array of String resources
see constructor documentation for details.

@author rpiterman
'''

class ImageListDialog(UnoDialog2):
    RES_TITLE = 0
    RES_LABEL = 1
    RES_OK = 2
    RES_CANCEL = 3
    RES_HELP = 4
    RES_DESELECT = 5
    RES_OTHER = 6
    RES_COUNTER = 7
    START = "%START"
    END = "%END"
    TOTAL = "%TOTAL"

    '''
    @param xmsf
    @param resources_ a string array with the following strings :
    dialog title, label text, ok, cancel, help, deselect, other.
    <br/> if "deselect" and "other" are not displayed,
    the array can also be shorter. but if "other" is displayed
    and "deselect" not, both must be there :-(
    '''

    def __init__(self, xmsf, hid_, resources_):
        super(ImageListDialog, self).__init__(xmsf)
        self.hid = hid_
        self.cutFilename = 0
        self.showDeselectButton = True
        self.showOtherButton = True
        self.fontDescriptor1 = FontDescriptor()
        self.resources = resources_
        self.il = ImageList()
        self.il.counterRenderer = self.ARenderer(
            self.resources[ImageListDialog.RES_COUNTER])

    '''
    adds the controls to the dialog, depending on
    the size of the image list.
    This method should be called by subclasses after setting
    the il ImageList member properties
    '''

    def build(self):
        #set dialog properties...
        ilWidth = (self.il.imageSize.Width + self.il.gap.Width) \
            * self.il.cols + self.il.gap.Width
        ilHeight = (self.il.imageSize.Height + self.il.gap.Height) \
            * self.il.rows + self.il.gap.Height
        dialogWidth = 6 + ilWidth + 6 + 50 + 6
        dialogHeight = 3 + 16 + 3 + (ilHeight + 8 + 14) + 6
        uno.invoke(self.xDialogModel, "setPropertyValues",(
            ("Closeable",
                PropertyNames.PROPERTY_HEIGHT,
                PropertyNames.PROPERTY_HELPURL,
                "Moveable",
                PropertyNames.PROPERTY_NAME,
                PropertyNames.PROPERTY_POSITION_X,
                PropertyNames.PROPERTY_POSITION_Y,
                PropertyNames.PROPERTY_STEP,
                "Title",
                PropertyNames.PROPERTY_WIDTH),
            (True, dialogHeight, HelpIds.getHelpIdString(self.hid), True,
                "imgDialog", 59, 24, 1,
                self.resources[ImageListDialog.RES_TITLE],dialogWidth)))
        #Set member- FontDescriptors...
        self.fontDescriptor1.Weight = 150
        PROPNAMES = ("DefaultButton",
            PropertyNames.PROPERTY_HEIGHT,
            PropertyNames.PROPERTY_HELPURL,
            PropertyNames.PROPERTY_LABEL,
            PropertyNames.PROPERTY_NAME,
            PropertyNames.PROPERTY_POSITION_X,
            PropertyNames.PROPERTY_POSITION_Y,
            "PushButtonType",
            PropertyNames.PROPERTY_TABINDEX,
            PropertyNames.PROPERTY_WIDTH)
        iButtonsX = 6 + ilWidth + 6
        self.btnOK = self.insertButton(
            "btnOK", None, PROPNAMES,
            (True, 14, HelpIds.getHelpIdString(self.hid + 3),
                self.resources[ImageListDialog.RES_OK], "btnOK",
                iButtonsX, 22, uno.Any("short", OK), 7, 50), self)
        self.btnCancel = self.insertButton(
            "btnCancel", None, PROPNAMES,
            (False, 14, HelpIds.getHelpIdString(self.hid + 4),
                self.resources[ImageListDialog.RES_CANCEL], "btnCancel",
                iButtonsX, 41, uno.Any("short", CANCEL), 8, 50), self)
        self.btnHelp = self.insertButton(
            "btnHelp", None, PROPNAMES,
            (False, 14, "", self.resources[ImageListDialog.RES_HELP],
                "CommandButton3", iButtonsX, 71,
                uno.Any("short", HELP), 9, 50), self)
        if self.showOtherButton:
            aux = 0
            if self.showDeselectButton:
                aux = 19
            otherY = 22 + ilHeight - 14 - aux
            self.btnOther = self.insertButton(
                "btnOther", "other", PROPNAMES,
                (False, 14, HelpIds.getHelpIdString(self.hid + 1),
                    self.resources[ImageListDialog.RES_OTHER], "btnOther",
                    iButtonsX, otherY,
                    uno.Any("short", STANDARD), 5, 50), self)

        if self.showDeselectButton:
            deselectY = 22 + ilHeight - 14
            self.btnDeselect = self.insertButton(
                "btnNoImage", "deselect", PROPNAMES,
                (False, 14, HelpIds.getHelpIdString(self.hid + 2),
                    self.resources[ImageListDialog.RES_DESELECT], "btnNoImage",
                    iButtonsX, deselectY,
                    uno.Any("short", STANDARD), 4, 50), self)

        self.il.step = 1
        self.il.pos = Size(6, 22)
        self.il.helpURL = self.hid + 5
        self.il.tabIndex = 1
        self.il.create(self)
        self.lblTitle = self.insertLabel("lblTitle",
            ("FontDescriptor",
                PropertyNames.PROPERTY_HEIGHT,
                PropertyNames.PROPERTY_LABEL,
                PropertyNames.PROPERTY_NAME,
                PropertyNames.PROPERTY_POSITION_X,
                PropertyNames.PROPERTY_POSITION_Y,
                PropertyNames.PROPERTY_STEP,
                PropertyNames.PROPERTY_TABINDEX,
                PropertyNames.PROPERTY_WIDTH),
            (self.fontDescriptor1, 8,
                self.resources[ImageListDialog.RES_LABEL],
                "lblTitle", 6, 6, 1, 4, 216))

    '''
    is called when the user clicks "none"
    '''

    def deselect(self):
        self.il.setSelected(-1)

    '''
    @return the currently elected object.
    '''

    def getSelected(self):
        return self.il.getSelectedObject()

    '''
    sets the currently selected object.
    @param obj the object (out of the model) to be selected.
    '''

    def setSelected(self, obj):
        self.il.setSelected(obj)
        self.il.showSelected()

    '''
    The counter renderer, which uses a template.
    The template replaces the Strings "%START", "%END" and
    "%TOTAL" with the respective values.
    @author rpiterman
    '''
    class ARenderer(IRenderer):

        '''
        @param aTempalte a template for this renderer.
        The strings %START, %END ,%TOTAL will be replaced
        with the actual values.
        '''

        def __init__(self, aTemplate):
            self.template = aTemplate

        def render(self, counter):
            s = self.template.replace(ImageListDialog.START, "" + \
                str(counter.start))
            s = s.replace(ImageListDialog.END, "" + str(counter.end))
            s = s.replace(ImageListDialog.TOTAL, "" + str(counter.max))
            return s