root / py / maugis / scenic / dialogs.py @ 3db5a977
History | View | Annotate | Download (4.3 kB)
| 1 | 34e654f6 | Alexandre Quessy | #!/usr/bin/env python
|
|---|---|---|---|
| 2 | 34e654f6 | Alexandre Quessy | # -*- coding: utf-8 -*-
|
| 3 | 34e654f6 | Alexandre Quessy | #
|
| 4 | 34e654f6 | Alexandre Quessy | # Scenic
|
| 5 | 34e654f6 | Alexandre Quessy | # Copyright (C) 2008 Société des arts technologiques (SAT)
|
| 6 | 34e654f6 | Alexandre Quessy | # http://www.sat.qc.ca
|
| 7 | 34e654f6 | Alexandre Quessy | # All rights reserved.
|
| 8 | 34e654f6 | Alexandre Quessy | #
|
| 9 | 34e654f6 | Alexandre Quessy | # This file is free software: you can redistribute it and/or modify
|
| 10 | 34e654f6 | Alexandre Quessy | # it under the terms of the GNU General Public License as published by
|
| 11 | 34e654f6 | Alexandre Quessy | # the Free Software Foundation, either version 2 of the License, or
|
| 12 | 34e654f6 | Alexandre Quessy | # (at your option) any later version.
|
| 13 | 34e654f6 | Alexandre Quessy | #
|
| 14 | 34e654f6 | Alexandre Quessy | # Scenic is distributed in the hope that it will be useful,
|
| 15 | 34e654f6 | Alexandre Quessy | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 16 | 34e654f6 | Alexandre Quessy | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 17 | 34e654f6 | Alexandre Quessy | # GNU General Public License for more details.
|
| 18 | 34e654f6 | Alexandre Quessy | #
|
| 19 | 34e654f6 | Alexandre Quessy | # You should have received a copy of the GNU General Public License
|
| 20 | 34e654f6 | Alexandre Quessy | # along with Scenic. If not, see <http://www.gnu.org/licenses/>.
|
| 21 | 34e654f6 | Alexandre Quessy | """
|
| 22 | 34e654f6 | Alexandre Quessy | GTK Dialogs well integrated with Twisted. |
| 23 | 34e654f6 | Alexandre Quessy | * Error dialog |
| 24 | 34e654f6 | Alexandre Quessy | * Yes/No dialog |
| 25 | 34e654f6 | Alexandre Quessy | """
|
| 26 | 34e654f6 | Alexandre Quessy | if __name__ == "__main__": |
| 27 | 34e654f6 | Alexandre Quessy | from twisted.internet import gtk2reactor |
| 28 | 34e654f6 | Alexandre Quessy | gtk2reactor.install() # has to be done before importing reactor
|
| 29 | 34e654f6 | Alexandre Quessy | from twisted.internet import reactor |
| 30 | 34e654f6 | Alexandre Quessy | from twisted.internet import defer |
| 31 | 34e654f6 | Alexandre Quessy | import gtk |
| 32 | 34e654f6 | Alexandre Quessy | |
| 33 | 34e654f6 | Alexandre Quessy | class ErrorDialog(object): |
| 34 | 34e654f6 | Alexandre Quessy | """
|
| 35 | 34e654f6 | Alexandre Quessy | Error dialog. Fires the deferred given to it once done. |
| 36 | 34e654f6 | Alexandre Quessy | Use the create static method as a factory. |
| 37 | 34e654f6 | Alexandre Quessy | """ |
| 38 | e0ebd025 | Alexandre Quessy | def __init__(self, deferred, message, parent=None): |
| 39 | 34e654f6 | Alexandre Quessy | """
|
| 40 | 34e654f6 | Alexandre Quessy | @param deferred: L{Deferred} |
| 41 | 34e654f6 | Alexandre Quessy | @param message: str |
| 42 | 34e654f6 | Alexandre Quessy | """ |
| 43 | 34e654f6 | Alexandre Quessy | self.deferredResult = deferred
|
| 44 | 34e654f6 | Alexandre Quessy | error_dialog = gtk.MessageDialog( |
| 45 | e0ebd025 | Alexandre Quessy | parent=parent, |
| 46 | 34e654f6 | Alexandre Quessy | flags=0,
|
| 47 | 34e654f6 | Alexandre Quessy | type=gtk.MESSAGE_ERROR, |
| 48 | 34e654f6 | Alexandre Quessy | buttons=gtk.BUTTONS_CLOSE, |
| 49 | 34e654f6 | Alexandre Quessy | message_format=message) |
| 50 | 8bf4f99b | Tristan Matthews | error_dialog.set_modal(True)
|
| 51 | 34e654f6 | Alexandre Quessy | error_dialog.connect("close", self.on_close) |
| 52 | 34e654f6 | Alexandre Quessy | error_dialog.connect("response", self.on_response) |
| 53 | 34e654f6 | Alexandre Quessy | error_dialog.show() |
| 54 | 34e654f6 | Alexandre Quessy | |
| 55 | 34e654f6 | Alexandre Quessy | @staticmethod
|
| 56 | e0ebd025 | Alexandre Quessy | def create(message, parent=None): |
| 57 | 34e654f6 | Alexandre Quessy | """
|
| 58 | 34e654f6 | Alexandre Quessy | Returns a Deferred which will be called with a True result. |
| 59 | 34e654f6 | Alexandre Quessy | @param message: str |
| 60 | 34e654f6 | Alexandre Quessy | @rettype: L{Deferred} |
| 61 | 34e654f6 | Alexandre Quessy | """ |
| 62 | 34e654f6 | Alexandre Quessy | d = defer.Deferred() |
| 63 | e0ebd025 | Alexandre Quessy | dialog = ErrorDialog(d, message, parent) |
| 64 | 34e654f6 | Alexandre Quessy | return d
|
| 65 | 34e654f6 | Alexandre Quessy | |
| 66 | 34e654f6 | Alexandre Quessy | def on_close(self, dialog, *params): |
| 67 | 34e654f6 | Alexandre Quessy | print("on_close %s %s" % (dialog, params))
|
| 68 | 34e654f6 | Alexandre Quessy | |
| 69 | 34e654f6 | Alexandre Quessy | def on_response(self, dialog, response_id, *params): |
| 70 | 34e654f6 | Alexandre Quessy | #print("on_response %s %s %s" % (dialog, response_id, params))
|
| 71 | 34e654f6 | Alexandre Quessy | if response_id == gtk.RESPONSE_DELETE_EVENT:
|
| 72 | 34e654f6 | Alexandre Quessy | print("Deleted")
|
| 73 | 34e654f6 | Alexandre Quessy | elif response_id == gtk.RESPONSE_CANCEL:
|
| 74 | 34e654f6 | Alexandre Quessy | print("Cancelled")
|
| 75 | 34e654f6 | Alexandre Quessy | elif response_id == gtk.RESPONSE_OK:
|
| 76 | 34e654f6 | Alexandre Quessy | print("Accepted")
|
| 77 | 34e654f6 | Alexandre Quessy | self.terminate(dialog)
|
| 78 | 34e654f6 | Alexandre Quessy | |
| 79 | 34e654f6 | Alexandre Quessy | def terminate(self, dialog): |
| 80 | 34e654f6 | Alexandre Quessy | dialog.destroy() |
| 81 | 34e654f6 | Alexandre Quessy | self.deferredResult.callback(True) |
| 82 | 34e654f6 | Alexandre Quessy | |
| 83 | 34e654f6 | Alexandre Quessy | class YesNoDialog(object): |
| 84 | 34e654f6 | Alexandre Quessy | """
|
| 85 | 34e654f6 | Alexandre Quessy | Yes/no confirmation dialog. |
| 86 | 34e654f6 | Alexandre Quessy | Use the create static method as a factory. |
| 87 | 34e654f6 | Alexandre Quessy | """ |
| 88 | e0ebd025 | Alexandre Quessy | def __init__(self, deferred, message, parent=None): |
| 89 | 34e654f6 | Alexandre Quessy | self.deferredResult = deferred
|
| 90 | 8bf4f99b | Tristan Matthews | yes_no_dialog = gtk.MessageDialog( |
| 91 | e0ebd025 | Alexandre Quessy | parent=parent, |
| 92 | 34e654f6 | Alexandre Quessy | flags=0,
|
| 93 | 34e654f6 | Alexandre Quessy | type=gtk.MESSAGE_QUESTION, |
| 94 | 34e654f6 | Alexandre Quessy | buttons=gtk.BUTTONS_YES_NO, |
| 95 | 34e654f6 | Alexandre Quessy | message_format=message) |
| 96 | 8bf4f99b | Tristan Matthews | yes_no_dialog.set_modal(True)
|
| 97 | 8bf4f99b | Tristan Matthews | yes_no_dialog.connect("close", self.on_close) |
| 98 | 8bf4f99b | Tristan Matthews | yes_no_dialog.connect("response", self.on_response) |
| 99 | 8bf4f99b | Tristan Matthews | yes_no_dialog.show() |
| 100 | 34e654f6 | Alexandre Quessy | |
| 101 | 34e654f6 | Alexandre Quessy | @staticmethod
|
| 102 | e0ebd025 | Alexandre Quessy | def create(message, parent=None): |
| 103 | 34e654f6 | Alexandre Quessy | """
|
| 104 | 34e654f6 | Alexandre Quessy | Returns a Deferred which will be called with a boolean result. |
| 105 | 34e654f6 | Alexandre Quessy | @param message: str |
| 106 | 34e654f6 | Alexandre Quessy | @rettype: L{Deferred} |
| 107 | 34e654f6 | Alexandre Quessy | """ |
| 108 | 34e654f6 | Alexandre Quessy | d = defer.Deferred() |
| 109 | e0ebd025 | Alexandre Quessy | dialog = YesNoDialog(d, message, parent) |
| 110 | 34e654f6 | Alexandre Quessy | return d
|
| 111 | 34e654f6 | Alexandre Quessy | |
| 112 | 34e654f6 | Alexandre Quessy | def on_close(self, dialog, *params): |
| 113 | 34e654f6 | Alexandre Quessy | print("on_close %s %s" % (dialog, params))
|
| 114 | 34e654f6 | Alexandre Quessy | |
| 115 | 34e654f6 | Alexandre Quessy | def on_response(self, dialog, response_id, *params): |
| 116 | 34e654f6 | Alexandre Quessy | print("on_response %s %s %s" % (dialog, response_id, params))
|
| 117 | 34e654f6 | Alexandre Quessy | if response_id == gtk.RESPONSE_DELETE_EVENT:
|
| 118 | 34e654f6 | Alexandre Quessy | print("Deleted")
|
| 119 | 34e654f6 | Alexandre Quessy | self.terminate(dialog, False) |
| 120 | 34e654f6 | Alexandre Quessy | elif response_id == gtk.RESPONSE_NO:
|
| 121 | 34e654f6 | Alexandre Quessy | print("Cancelled")
|
| 122 | 34e654f6 | Alexandre Quessy | self.terminate(dialog, False) |
| 123 | 34e654f6 | Alexandre Quessy | elif response_id == gtk.RESPONSE_YES:
|
| 124 | 34e654f6 | Alexandre Quessy | print("Accepted")
|
| 125 | 34e654f6 | Alexandre Quessy | self.terminate(dialog, True) |
| 126 | 34e654f6 | Alexandre Quessy | |
| 127 | 34e654f6 | Alexandre Quessy | def terminate(self, dialog, answer): |
| 128 | 34e654f6 | Alexandre Quessy | dialog.destroy() |
| 129 | 34e654f6 | Alexandre Quessy | self.deferredResult.callback(answer)
|
| 130 | 34e654f6 | Alexandre Quessy | |
| 131 | 8bf4f99b | Tristan Matthews | |
| 132 | 8bf4f99b | Tristan Matthews | if __name__ == '__main__': |
| 133 | 8bf4f99b | Tristan Matthews | d = ErrorDialog.create('BOBBBBBBBBBB')
|
| 134 | 8bf4f99b | Tristan Matthews | d.addCallback(lambda result: reactor.stop())
|
| 135 | 8bf4f99b | Tristan Matthews | reactor.run() |
