~dsmid/smart/unicodeencodeerror

« back to all changes in this revision

Viewing changes to smart/interfaces/qt/interface.py

  • Committer: Anders F Bjorklund
  • Date: 2010-01-31 14:31:10 UTC
  • mfrom: (837.2.133 qt)
  • Revision ID: afb@users.sourceforge.net-20100131143110-kydp5nmu7zbhbkq6
Implement interfaces/qt as an alternative to interfaces/gtk [#260828]

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (c) 2004 Conectiva, Inc.
 
3
#
 
4
# Written by Gustavo Niemeyer <niemeyer@conectiva.com>
 
5
#
 
6
# This file is part of Smart Package Manager.
 
7
#
 
8
# Smart Package Manager is free software; you can redistribute it and/or
 
9
# modify it under the terms of the GNU General Public License as published
 
10
# by the Free Software Foundation; either version 2 of the License, or (at
 
11
# your option) any later version.
 
12
#
 
13
# Smart Package Manager is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
# General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with Smart Package Manager; if not, write to the Free Software
 
20
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
#
 
22
from smart.interfaces.qt.progress import QtProgress
 
23
from smart.interfaces.qt.changes import QtChanges
 
24
from smart.interfaces.qt.log import QtLog
 
25
from smart.interface import Interface, getScreenWidth
 
26
from smart.fetcher import Fetcher
 
27
from smart.const import DEBUG
 
28
from smart import *
 
29
import qt
 
30
import sys
 
31
 
 
32
 
 
33
app = qt.QApplication(sys.argv)
 
34
 
 
35
class QtInterface(Interface):
 
36
 
 
37
    def _currentThread(self):
 
38
        if hasattr(qt, 'QThread'):
 
39
            return qt.QThread.currentThread()
 
40
        else:
 
41
            return None
 
42
 
 
43
    def __init__(self, ctrl, argv):
 
44
        Interface.__init__(self, ctrl)
 
45
        self._log = QtLog()
 
46
        self._progress = QtProgress(False)
 
47
        self._progress.setMainThread(self._currentThread())
 
48
        self._hassubprogress = QtProgress(True)
 
49
        self._hassubprogress.setMainThread(self._currentThread())
 
50
        self._changes = QtChanges()
 
51
        self._window = None
 
52
        self._sys_excepthook = sys.excepthook
 
53
 
 
54
    def run(self, command=None, argv=None):
 
55
        self.setCatchExceptions(True)
 
56
        result = Interface.run(self, command, argv)
 
57
        self.setCatchExceptions(False)
 
58
        return result
 
59
 
 
60
    def getProgress(self, obj, hassub=False):
 
61
        if hassub:
 
62
            self._progress.hide()
 
63
            fetcher = isinstance(obj, Fetcher) and obj or None
 
64
            self._hassubprogress.setFetcher(fetcher)
 
65
            return self._hassubprogress
 
66
        else:
 
67
            self._hassubprogress.hide()
 
68
            return self._progress
 
69
 
 
70
    def getSubProgress(self, obj):
 
71
        return self._hassubprogress
 
72
 
 
73
    def askYesNo(self, question, default=False):
 
74
        response = qt.QMessageBox.question(self._window,
 
75
                                        _("Question..."),
 
76
                                        question,
 
77
                                        qt.QMessageBox.Yes,
 
78
                                        qt.QMessageBox.No)
 
79
 
 
80
 
 
81
        if response == qt.QMessageBox.Yes:
 
82
            return True
 
83
        elif response == qt.QMessageBox.No:
 
84
            return False
 
85
        else:
 
86
            return default
 
87
 
 
88
    def askContCancel(self, question, default=False):
 
89
        response = qt.QMessageBox.question(self._window,
 
90
                                   _("Question..."),
 
91
                                   question,
 
92
                                   _("Continue"),
 
93
                                   _("Cancel"),
 
94
                                   )
 
95
 
 
96
        #response.setButtonText(QMessageBox.Ok, )
 
97
        
 
98
        if response == 0:
 
99
            return True
 
100
        elif response == 1:
 
101
            return False
 
102
        else:
 
103
            return default
 
104
 
 
105
    def askOkCancel(self, question, default=False):
 
106
        response = qt.QMessageBox.question(self._window,
 
107
                                   _("Question..."),
 
108
                                   question,
 
109
                                   qt.QMessageBox.Ok,
 
110
                                   qt.QMessageBox.Cancel)
 
111
 
 
112
        
 
113
        if response == qt.QMessageBox.Ok:
 
114
            return True
 
115
        elif response == qt.QMessageBox.Cancel:
 
116
            return False
 
117
        else:
 
118
            return default
 
119
 
 
120
    def askInput(self, prompt, message=None, widthchars=40, echo=True):
 
121
        if (message != None):
 
122
            stringToShow = message + "\n" + prompt
 
123
        else:
 
124
            stringToShow = prompt
 
125
        if echo:
 
126
            echoMode = qt.QLineEdit.Normal
 
127
        else:
 
128
            echoMode = qt.QLineEdit.Password
 
129
 
 
130
        text, ok = qt.QInputDialog.getText( _("Input"), stringToShow, echoMode)
 
131
                
 
132
        if (ok and text != None):
 
133
            return text[0:widthchars]
 
134
        else:
 
135
            return ""
 
136
 
 
137
    def insertRemovableChannels(self, channels):
 
138
        question = _("Insert one or more of the following removable "
 
139
                     "channels:\n")
 
140
        question += "\n"
 
141
        for channel in channels:
 
142
            question += "    "
 
143
            question += channel.getName()
 
144
            question += "\n"
 
145
        return self.askOkCancel(question, default=True)
 
146
 
 
147
    def message(self, level, msg):
 
148
        self._log.message(level, msg)
 
149
 
 
150
    def confirmChange(self, oldchangeset, newchangeset, expected=1):
 
151
        changeset = newchangeset.difference(oldchangeset)
 
152
        keep = []
 
153
        for pkg in oldchangeset:
 
154
            if pkg not in newchangeset:
 
155
                keep.append(pkg)
 
156
        if len(keep)+len(changeset) <= expected:
 
157
            return True
 
158
        return self._changes.showChangeSet(changeset, keep=keep, confirm=True)
 
159
 
 
160
    def confirmChangeSet(self, changeset):
 
161
        return self._changes.showChangeSet(changeset, confirm=True)
 
162
 
 
163
    # Non-standard interface methods
 
164
 
 
165
    def _excepthook(self, type, value, tb):
 
166
        if issubclass(type, Error) and not sysconf.get("log-level") is DEBUG:
 
167
            self._hassubprogress.hide()
 
168
            self._progress.hide()
 
169
            iface.error(unicode(value[0]))
 
170
        else:
 
171
            import traceback
 
172
            lines = traceback.format_exception(type, value, tb)
 
173
            iface.error("\n".join(lines))
 
174
 
 
175
    def setCatchExceptions(self, flag):
 
176
        if flag:
 
177
            sys.excepthook = self._excepthook
 
178
        else:
 
179
            sys.excepthook = self._sys_excepthook
 
180
 
 
181
    def hideProgress(self):
 
182
        self._progress.hide()
 
183
        self._hassubprogress.hide()
 
184
 
 
185
 
 
186
# vim:ts=4:sw=4:et