~ubuntu-branches/ubuntu/lucid/anki/lucid-updates

« back to all changes in this revision

Viewing changes to ankiqt/ui/utils.py

  • Committer: Bazaar Package Importer
  • Author(s): Mackenzie Morgan
  • Date: 2010-05-31 15:55:50 UTC
  • mfrom: (7.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100531155550-wj3tag8bvp6fwhpo
Tags: 0.9.9.8.6-2~lucid1
Backport from maverick to fix FTBFS (LP: #550145)

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
def openWikiLink(page):
18
18
    openLink(ankiqt.appWiki + page)
19
19
 
20
 
def showWarning(text, parent=None):
 
20
def showWarning(text, parent=None, help=""):
21
21
    "Show a small warning with an OK button."
22
 
    if not parent:
23
 
        parent = ankiqt.mw
24
 
    QMessageBox.warning(parent, "Anki", text)
25
 
 
26
 
def showInfo(text, parent=None, help=""):
 
22
    return showInfo(text, parent, help, QMessageBox.warning)
 
23
 
 
24
def showCritical(text, parent=None, help=""):
 
25
    "Show a small critical error with an OK button."
 
26
    return showInfo(text, parent, help, QMessageBox.critical)
 
27
 
 
28
def showInfo(text, parent=None, help="", func=None):
27
29
    "Show a small info window with an OK button."
28
30
    if not parent:
29
31
        parent = ankiqt.mw
 
32
    if not func:
 
33
        func = QMessageBox.information
30
34
    sb = QMessageBox.Ok
31
35
    if help:
32
36
        sb |= QMessageBox.Help
33
37
    while 1:
34
 
        ret = QMessageBox.information(parent, "Anki", text, sb)
 
38
        ret = func(parent, "Anki", text, sb)
35
39
        if ret == QMessageBox.Help:
36
40
            openWikiLink(help)
37
41
        else:
166
170
    if ankiqt.mw.config.get(key):
167
171
        widget.restoreGeometry(ankiqt.mw.config[key])
168
172
 
 
173
def saveState(widget, key):
 
174
    key += "State"
 
175
    ankiqt.mw.config[key] = widget.saveState()
 
176
 
 
177
def restoreState(widget, key):
 
178
    key += "State"
 
179
    if ankiqt.mw.config.get(key):
 
180
        widget.restoreState(ankiqt.mw.config[key])
 
181
 
169
182
def saveSplitter(widget, key):
170
183
    key += "Splitter"
171
184
    ankiqt.mw.config[key] = widget.saveState()
187
200
def mungeQA(deck, txt):
188
201
    txt = renderLatex(deck, txt)
189
202
    txt = stripSounds(txt)
 
203
    # webkit currently doesn't handle bold/underline properly
 
204
    txt = txt.replace("font-weight: 600;",
 
205
                      "font-weight: 900;")
 
206
    txt = txt.replace("text-decoration: underline;",
 
207
                      "border-bottom: 1px solid #000;")
190
208
    return txt
191
209
 
192
 
def getBase(deck):
193
 
    if deck and deck.mediaDir():
194
 
        if sys.platform.startswith("win32"):
195
 
            prefix = u"file:///"
196
 
        else:
197
 
            prefix = u"file://"
198
 
        base = prefix + unicode(
199
 
            urllib.quote(deck.mediaDir().encode("utf-8")),
200
 
            "utf-8")
201
 
        return '<base href="%s/">' % base
 
210
def applyStyles(widget):
 
211
    try:
 
212
        styleFile = open(os.path.join(ankiqt.mw.config.configPath,
 
213
                                      "style.css"))
 
214
        widget.setStyleSheet(styleFile.read())
 
215
    except (IOError, OSError):
 
216
        pass
 
217
 
 
218
def getBase(deck, card):
 
219
    base = None
 
220
    if deck and card:
 
221
        if deck.getBool("remoteImages") and card.fact.model.features:
 
222
            base = card.fact.model.features
 
223
        elif deck.mediaDir():
 
224
            if sys.platform.startswith("win32"):
 
225
                prefix = u"file:///"
 
226
            else:
 
227
                prefix = u"file://"
 
228
            base = prefix + unicode(
 
229
                urllib.quote(deck.mediaDir().encode("utf-8")),
 
230
                "utf-8") + "/"
 
231
    if base:
 
232
        return '<base href="%s">' % base
202
233
    else:
203
234
        return ""
204
235
 
212
243
        self.diag.setCancelButton(None)
213
244
        self.diag.setAutoClose(False)
214
245
        self.diag.setAutoReset(False)
215
 
        self.diag.setMinimumDuration(0)
216
 
        self.diag.show()
 
246
        # qt doesn't seem to honour this consistently, and it's not triggered
 
247
        # by the db progress handler, so we set it high and use maybeShow() below
 
248
        self.diag.setMinimumDuration(100000)
217
249
        self.counter = min
218
250
        self.min = min
219
251
        self.max = max
 
252
        self.firstTime = time.time()
220
253
        self.lastTime = time.time()
221
254
        self.app = QApplication.instance()
222
255
        if max == 0:
223
256
            self.diag.setLabelText(_("Processing..."))
224
 
        self.app.processEvents()
225
 
 
226
 
    def update(self, label=None, value=None):
227
 
        self.app.processEvents()
 
257
 
 
258
    def maybeShow(self):
 
259
        if time.time() - self.firstTime > 2:
 
260
            self.diag.show()
 
261
 
 
262
    def update(self, label=None, value=None, process=True):
228
263
        #print self.min, self.counter, self.max, label, time.time() - self.lastTime
 
264
        self.maybeShow()
229
265
        self.lastTime = time.time()
230
266
        if label:
231
267
            self.diag.setLabelText(label)
234
270
            self.counter += 1
235
271
        else:
236
272
            self.counter = value + 1
237
 
        self.diag.setValue(value)
238
 
        self.app.processEvents()
 
273
        if self.max:
 
274
            self.diag.setValue(value)
 
275
        if process:
 
276
            self.app.processEvents()
239
277
 
240
278
    def finish(self):
241
279
        if self.max:
243
281
            self.app.processEvents()
244
282
            time.sleep(0.1)
245
283
        self.diag.cancel()
 
284
 
 
285
import PyQt4.pyqtconfig as PyConf;
 
286
pyQtBroken = PyConf.Configuration().pyqt_version_str.startswith("4.6")