3 #------------------------------------------------------------------------------
8 # Original Author: Andrei "Garoth" Thorp <garoth@gmail.com>
10 # Description: This module makes handling of my multi-widget result viewer
11 # easier. What this class does is track a set of widgets and keeps
12 # them all updated. At any point, the main method can ask the
13 # resulter to return a specific widget so that main can display it.
14 # The Resulter is not a frame or box of any kind that can be
15 # displayed -- it only holds pointers to the objects that it
19 # Indentation: I use tabs only, 4 spaces per tab.
20 #------------------------------------------------------------------------------
24 import gtksourceview2
as gtksourceview
28 #------------------------------------------------------------------------------
30 #------------------------------------------------------------------------------
31 #--------------------------
32 def __init__(self
, parser
):
33 #--------------------------
34 """Initializes the instance variables (and as such, the various
35 gtk widgets that would be used)"""
36 # Given parser, set up with the dbus info
38 # Iterator for the data, able to go row by row
39 self
.iterator
= self
.parser
.getTableIterator()
40 self
.titles
= self
.parser
.getColumnTitles()
41 self
.message
= parser
.getOriginalMessage()
42 self
.dbusMessages
= time
.ctime(time
.time())+"\n\n"+ str(self
.message
)
43 self
.dbusMessages
= self
.__formatDbusMessage
__(self
.dbusMessage
, 100)
45 # Set up these objects
46 self
.__initTableView
__()
47 self
.__initDbusView
__()
48 self
.__initTextView
__()
50 # Reorganize this if you want the program to change through the views
51 # in a different order
52 self
.viewOrder
= [self
.getTableView(),
55 self
.currentView
= self
.viewOrder
[0]
60 """Initializes a blank resulter with no... results. This construct
61 would be used with later calls to the update method."""
62 self
.dbusMessages
= ""
64 #---------------------------
65 def __initTableView__(self
):
66 #---------------------------
67 """Generates a gtk.TreeView based table"""
68 # FIXME: STUPID PANGO MARKDOWN WTH
69 if len(self
.parser
.getColumnTitles()) >= 1:
70 self
.tableViewModel
= gtk
.ListStore(*self
.parser
71 .getColumnTypesAsStrings())
73 while self
.iterator
.hasNext():
74 item
= self
.iterator
.getNext()
75 self
.tableViewModel
.append(item
)
77 self
.tableView
= gtk
.TreeView(self
.tableViewModel
)
78 self
.cellRenderer
= gtk
.CellRendererText()
81 for title
in self
.titles
:
82 treeviewcolumn
= gtk
.TreeViewColumn(title
, self
.cellRenderer
, text
=x
)
83 treeviewcolumn
.set_resizable(True)
84 self
.tableView
.append_column(treeviewcolumn
)
89 #--------------------------
90 def __initDbusView__(self
):
91 #--------------------------
92 """Generates a gtksourceview with the original dbus message inside"""
93 self
.dbusBuffer
= gtksourceview
.Buffer()
94 self
.dbusView
= gtksourceview
.View(self
.dbusBuffer
)
96 self
.configureEditor(self
.dbusView
, self
.dbusBuffer
)
97 self
.dbusView
.set_editable(False)
99 self
.dbusBuffer
.set_text(self
.dbusMessages
)
103 #--------------------------
104 def __initTextView__(self
):
105 #--------------------------
106 """Generates a textual table of the data"""
109 self
.textBuffer
= gtksourceview
.Buffer()
110 self
.textView
= gtksourceview
.View(self
.textBuffer
)
112 self
.configureEditor(self
.textView
, self
.textBuffer
)
113 self
.textView
.set_editable(False)
114 self
.textView
.set_wrap_mode(gtk
.WRAP_NONE
)
116 self
.textBuffer
.set_text(self
.__formatTextTable
__())
120 #-----------------------------
121 def __formatTextTable__(self
):
122 #-----------------------------
123 """Formats the textual table to look nice"""
124 output
= "" # Final Output to send back
125 numColumns
= self
.parser
.numColumns() # Number of columns in table
126 numRows
= self
.parser
.numRows() # Number of rows in the table
127 maxColWidth
= 20 # Max width of 1 column
128 padding
= " | " # The space between columns
129 table
= self
.parser
.getTable() # the full table of values
130 iterator
= self
.parser
.getTableIterator() # iterator for the rows
133 # 1) Get max widths of the columns
135 for y
in range(numColumns
):
136 max = len(self
.titles
[y
])
137 for x
in range(numRows
):
138 if len(table
[x
][y
]) > max:
139 max = len(table
[x
][y
])
140 if max > maxColWidth
:
144 # 2) Generate format string
146 if width
!= widths
[len(widths
)-1]:
147 format
+= r
"%-" + str(width
) + r
"." + str(width
) + r
"s"+padding
149 format
+= r
"%-" + str(width
) + r
"." + str(width
) + r
"s"
152 output
+= format
% tuple(self
.titles
) + os
.linesep
156 for width
in widths
:totalWidth
+= width
157 totalWidth
+= len(padding
* (numColumns
- 1))
158 output
+= "-" * totalWidth
+ os
.linesep
161 while iterator
.hasNext():
162 output
+= format
% tuple(iterator
.getNext()) + os
.linesep
166 #--------------------------------------------
167 def __formatDbusMessage__(self
, text
, width
):
168 #--------------------------------------------
169 """Slices up the Dbus Message so that hopefully gtksourceview doesn't"""
170 return reduce(lambda line
, word
, width
=width
: '%s%s%s' %
172 ' \n'[(len(line
)-line
.rfind('\n')-1
173 + len (word
.split('\n', 1)[0]
180 #-------------------------
181 def __makeScrolls__(self
):
182 #-------------------------
183 """Generates scroll bars for widgets that want them"""
184 scrolls
= gtk
.ScrolledWindow(gtk
.Adjustment(), gtk
.Adjustment())
185 scrolls
.set_policy(gtk
.POLICY_AUTOMATIC
, gtk
.POLICY_AUTOMATIC
)
189 #------------------------
190 def update(self
, parser
):
191 #------------------------
192 """Updates widgets with a new dbus message.
193 * TableView replaces its old content
194 * DbusView appends text to its old content
195 * TextView replaces its old content
196 Note: this will create new objects. The old won't update."""
198 self
.iterator
= self
.parser
.getTableIterator()
199 self
.titles
= self
.parser
.getColumnTitles()
200 self
.message
= parser
.getOriginalMessage()
201 self
.dbusMessages
= time
.ctime(time
.time())+ os
.linesep
+ os
.linesep
+ \
203 self
.dbusMessages
= self
.__formatDbusMessage
__(self
.dbusMessages
, 100)
205 # Set up these objects
206 self
.__initTableView
__()
207 self
.__initDbusView
__()
208 self
.__initTextView
__()
211 self
.viewOrder
= [self
.getTableView(),
214 self
.currentView
= self
.viewOrder
[0]
216 #---------------------------------------------
217 def configureEditor(self
, editor
, textbuffer
):
218 #---------------------------------------------
219 """Sets up a gtksourceview with the common options I want."""
220 languagemanager
= gtksourceview
.LanguageManager()
221 textbuffer
.set_language(languagemanager
.get_language("sql"))
222 textbuffer
.set_highlight_syntax(True)
223 editor
.set_show_line_numbers(True)
224 editor
.set_wrap_mode(gtk
.WRAP_WORD_CHAR
)
225 editor
.modify_font(pango
.FontDescription("monospace 10"))
227 #------------------------
228 def getCurrentView(self
):
229 #------------------------
230 """Returns the view that was most recently retrieved"""
231 return self
.currentView
233 #---------------------
234 def getNextView(self
):
235 #---------------------
236 """Returns the next view that is on the self.viewOrder list"""
237 self
.currentView
= self
.viewOrder
[(self
.viewOrder
238 .index(self
.getCurrentView())+1)%3]
239 return self
.currentView
241 #----------------------
242 def getTableView(self
):
243 #----------------------
244 """Returns self.tableView widget"""
245 scrolls
= self
.__makeScrolls
__()
246 scrolls
.add(self
.tableView
)
247 self
.currentView
= scrolls
250 #---------------------
251 def getDbusView(self
):
252 #---------------------
253 """Returns self.dbusView widget"""
254 scrolls
= self
.__makeScrolls
__()
255 scrolls
.add(self
.dbusView
)
256 self
.currentView
= scrolls
259 #---------------------
260 def getTextView(self
):
261 #---------------------
262 """Returns self.textView widget"""
263 scrolls
= self
.__makeScrolls
__()
264 scrolls
.add(self
.textView
)
265 self
.currentView
= scrolls