dialogs.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. """!
  2. @package dbmgr.dialogs
  3. @brief DBM-related dialogs
  4. List of classes:
  5. - dialogs::DisplayAttributesDialog
  6. - dialogs::ModifyTableRecord
  7. - dialogs::AddColumnDialog
  8. (C) 2007-2012 by the GRASS Development Team
  9. This program is free software under the GNU General Public License
  10. (>=v2). Read the file COPYING that comes with GRASS for details.
  11. @author Martin Landa <landa.martin gmail.com>
  12. @author Refactoring by Stepan Turek <stepan.turek seznam.cz> (GSoC 2012, mentor: Martin Landa)
  13. """
  14. import os
  15. import types
  16. from core import globalvar
  17. import wx
  18. import wx.lib.scrolledpanel as scrolled
  19. from core.gcmd import RunCommand, GError
  20. from core.debug import Debug
  21. from core.settings import UserSettings
  22. from dbmgr.vinfo import VectorDBInfo
  23. from gui_core.widgets import IntegerValidator, FloatValidator
  24. class DisplayAttributesDialog(wx.Dialog):
  25. def __init__(self, parent, map,
  26. query = None, cats = None, line = None,
  27. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
  28. pos = wx.DefaultPosition,
  29. action = "add", ignoreError = False):
  30. """!Standard dialog used to add/update/display attributes linked
  31. to the vector map.
  32. Attribute data can be selected based on layer and category number
  33. or coordinates.
  34. @param parent
  35. @param map vector map
  36. @param query query coordinates and distance (used for v.edit)
  37. @param cats {layer: cats}
  38. @param line feature id (requested for cats)
  39. @param style
  40. @param pos
  41. @param action (add, update, display)
  42. @param ignoreError True to ignore errors
  43. """
  44. self.parent = parent # mapdisplay.BufferedWindow
  45. self.map = map
  46. self.action = action
  47. # ids/cats of selected features
  48. # fid : {layer : cats}
  49. self.cats = {}
  50. self.fid = -1 # feature id
  51. # get layer/table/column information
  52. self.mapDBInfo = VectorDBInfo(self.map)
  53. layers = self.mapDBInfo.layers.keys() # get available layers
  54. # check if db connection / layer exists
  55. if len(layers) <= 0:
  56. if not ignoreError:
  57. dlg = wx.MessageDialog(parent = self.parent,
  58. message = _("No attribute table found.\n\n"
  59. "Do you want to create a new attribute table "
  60. "and defined a link to vector map <%s>?") % self.map,
  61. caption = _("Create table?"),
  62. style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  63. if dlg.ShowModal() == wx.ID_YES:
  64. lmgr = self.parent.lmgr
  65. lmgr.OnShowAttributeTable(event = None, selection = 'layers')
  66. dlg.Destroy()
  67. self.mapDBInfo = None
  68. wx.Dialog.__init__(self, parent = self.parent, id = wx.ID_ANY,
  69. title = "", style = style, pos = pos)
  70. # dialog body
  71. mainSizer = wx.BoxSizer(wx.VERTICAL)
  72. # notebook
  73. self.notebook = wx.Notebook(parent = self, id = wx.ID_ANY, style = wx.BK_DEFAULT)
  74. self.closeDialog = wx.CheckBox(parent = self, id = wx.ID_ANY,
  75. label = _("Close dialog on submit"))
  76. self.closeDialog.SetValue(True)
  77. if self.action == 'display':
  78. self.closeDialog.Enable(False)
  79. # feature id (text/choice for duplicates)
  80. self.fidMulti = wx.Choice(parent = self, id = wx.ID_ANY,
  81. size = (150, -1))
  82. self.fidMulti.Bind(wx.EVT_CHOICE, self.OnFeature)
  83. self.fidText = wx.StaticText(parent = self, id = wx.ID_ANY)
  84. self.noFoundMsg = wx.StaticText(parent = self, id = wx.ID_ANY,
  85. label = _("No attributes found"))
  86. self.UpdateDialog(query = query, cats = cats)
  87. # set title
  88. if self.action == "update":
  89. self.SetTitle(_("Update attributes"))
  90. elif self.action == "add":
  91. self.SetTitle(_("Define attributes"))
  92. else:
  93. self.SetTitle(_("Display attributes"))
  94. # buttons
  95. btnCancel = wx.Button(self, wx.ID_CANCEL)
  96. btnReset = wx.Button(self, wx.ID_UNDO, _("&Reload"))
  97. btnSubmit = wx.Button(self, wx.ID_OK, _("&Submit"))
  98. if self.action == 'display':
  99. btnSubmit.Enable(False)
  100. btnSizer = wx.StdDialogButtonSizer()
  101. btnSizer.AddButton(btnCancel)
  102. btnSizer.AddButton(btnReset)
  103. btnSizer.SetNegativeButton(btnReset)
  104. btnSubmit.SetDefault()
  105. btnSizer.AddButton(btnSubmit)
  106. btnSizer.Realize()
  107. mainSizer.Add(item = self.noFoundMsg, proportion = 0,
  108. flag = wx.EXPAND | wx.ALL, border = 5)
  109. mainSizer.Add(item = self.notebook, proportion = 1,
  110. flag = wx.EXPAND | wx.ALL, border = 5)
  111. fidSizer = wx.BoxSizer(wx.HORIZONTAL)
  112. fidSizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY,
  113. label = _("Feature id:")),
  114. proportion = 0, border = 5,
  115. flag = wx.ALIGN_CENTER_VERTICAL)
  116. fidSizer.Add(item = self.fidMulti, proportion = 0,
  117. flag = wx.EXPAND | wx.ALL, border = 5)
  118. fidSizer.Add(item = self.fidText, proportion = 0,
  119. flag = wx.EXPAND | wx.ALL, border = 5)
  120. mainSizer.Add(item = fidSizer, proportion = 0,
  121. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 5)
  122. mainSizer.Add(item = self.closeDialog, proportion = 0, flag = wx.EXPAND | wx.LEFT | wx.RIGHT,
  123. border = 5)
  124. mainSizer.Add(item = btnSizer, proportion = 0,
  125. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  126. # bindigs
  127. btnReset.Bind(wx.EVT_BUTTON, self.OnReset)
  128. btnSubmit.Bind(wx.EVT_BUTTON, self.OnSubmit)
  129. btnCancel.Bind(wx.EVT_BUTTON, self.OnCancel)
  130. self.SetSizer(mainSizer)
  131. mainSizer.Fit(self)
  132. # set min size for dialog
  133. w, h = self.GetBestSize()
  134. w += 50
  135. if h < 200:
  136. self.SetMinSize((w, 200))
  137. else:
  138. self.SetMinSize((w, h))
  139. if self.notebook.GetPageCount() == 0:
  140. Debug.msg(2, "DisplayAttributesDialog(): Nothing found!")
  141. ### self.mapDBInfo = None
  142. def OnSQLStatement(self, event):
  143. """!Update SQL statement"""
  144. pass
  145. def IsFound(self):
  146. """!Check for status
  147. @return True on attributes found
  148. @return False attributes not found
  149. """
  150. return bool(self.mapDBInfo and self.notebook.GetPageCount() > 0)
  151. def GetSQLString(self, updateValues = False):
  152. """!Create SQL statement string based on self.sqlStatement
  153. Show error message when invalid values are entered.
  154. If updateValues is True, update dataFrame according to values
  155. in textfields.
  156. """
  157. sqlCommands = []
  158. # find updated values for each layer/category
  159. for layer in self.mapDBInfo.layers.keys(): # for each layer
  160. table = self.mapDBInfo.GetTable(layer)
  161. key = self.mapDBInfo.GetKeyColumn(layer)
  162. columns = self.mapDBInfo.GetTableDesc(table)
  163. for idx in range(len(columns[key]['values'])): # for each category
  164. updatedColumns = []
  165. updatedValues = []
  166. for name in columns.keys():
  167. if name == key:
  168. cat = columns[name]['values'][idx]
  169. continue
  170. ctype = columns[name]['ctype']
  171. value = columns[name]['values'][idx]
  172. id = columns[name]['ids'][idx]
  173. try:
  174. newvalue = self.FindWindowById(id).GetValue()
  175. except:
  176. newvalue = self.FindWindowById(id).GetLabel()
  177. if newvalue:
  178. try:
  179. if ctype == int:
  180. newvalue = int(newvalue)
  181. elif ctype == float:
  182. newvalue = float(newvalue)
  183. except ValueError:
  184. GError(parent = self,
  185. message = _("Column <%(col)s>: Value '%(value)s' needs to be entered as %(type)s.") % \
  186. {'col' : name,
  187. 'value' : str(newvalue),
  188. 'type' : columns[name]['type'].lower()},
  189. showTraceback = False)
  190. sqlCommands.append(None)
  191. continue
  192. else:
  193. if self.action == 'add':
  194. continue
  195. if newvalue != value:
  196. updatedColumns.append(name)
  197. if not newvalue:
  198. updatedValues.append('NULL')
  199. else:
  200. if ctype != str:
  201. updatedValues.append(str(newvalue))
  202. else:
  203. updatedValues.append("'" + str(newvalue) + "'")
  204. columns[name]['values'][idx] = newvalue
  205. if self.action != "add" and len(updatedValues) == 0:
  206. continue
  207. if self.action == "add":
  208. sqlString = "INSERT INTO %s (%s," % (table, key)
  209. else:
  210. sqlString = "UPDATE %s SET " % table
  211. for idx in range(len(updatedColumns)):
  212. name = updatedColumns[idx]
  213. if self.action == "add":
  214. sqlString += name + ","
  215. else:
  216. sqlString += name + "=" + updatedValues[idx] + ","
  217. sqlString = sqlString[:-1] # remove last comma
  218. if self.action == "add":
  219. sqlString += ") VALUES (%s," % cat
  220. for value in updatedValues:
  221. sqlString += str(value) + ","
  222. sqlString = sqlString[:-1] # remove last comma
  223. sqlString += ")"
  224. else:
  225. sqlString += " WHERE %s=%s" % (key, cat)
  226. sqlCommands.append(sqlString)
  227. # for each category
  228. # for each layer END
  229. Debug.msg(3, "DisplayAttributesDialog.GetSQLString(): %s" % sqlCommands)
  230. return sqlCommands
  231. def OnReset(self, event = None):
  232. """!Reset form"""
  233. for layer in self.mapDBInfo.layers.keys():
  234. table = self.mapDBInfo.layers[layer]["table"]
  235. key = self.mapDBInfo.layers[layer]["key"]
  236. columns = self.mapDBInfo.tables[table]
  237. for idx in range(len(columns[key]['values'])):
  238. for name in columns.keys():
  239. type = columns[name]['type']
  240. value = columns[name]['values'][idx]
  241. if value is None:
  242. value = ''
  243. try:
  244. id = columns[name]['ids'][idx]
  245. except IndexError:
  246. id = wx.NOT_FOUND
  247. if name != key and id != wx.NOT_FOUND:
  248. self.FindWindowById(id).SetValue(str(value))
  249. def OnCancel(self, event):
  250. """!Cancel button pressed
  251. """
  252. frame = self.parent.parent
  253. frame.dialogs['attributes'] = None
  254. if hasattr(self, "digit"):
  255. self.parent.digit.GetDisplay().SetSelected([])
  256. if frame.IsAutoRendered():
  257. self.parent.UpdateMap(render = False)
  258. elif frame.IsAutoRendered():
  259. frame.RemoveQueryLayer()
  260. self.parent.UpdateMap(render = True)
  261. self.Close()
  262. def OnSubmit(self, event):
  263. """!Submit records"""
  264. layer = 1
  265. close = True
  266. enc = UserSettings.Get(group = 'atm', key = 'encoding', subkey = 'value')
  267. if not enc and 'GRASS_DB_ENCODING' in os.environ:
  268. enc = os.environ['GRASS_DB_ENCODING']
  269. for sql in self.GetSQLString(updateValues = True):
  270. if not sql:
  271. close = False
  272. continue
  273. if enc:
  274. sql = sql.encode(enc)
  275. driver, database = self.mapDBInfo.GetDbSettings(layer)
  276. Debug.msg(1, "SQL: %s" % sql)
  277. RunCommand('db.execute',
  278. parent = self,
  279. quiet = True,
  280. input = '-',
  281. stdin = sql,
  282. driver = driver,
  283. database = database)
  284. layer += 1
  285. if close and self.closeDialog.IsChecked():
  286. self.OnCancel(event)
  287. def OnFeature(self, event):
  288. self.fid = int(event.GetString())
  289. self.UpdateDialog(cats = self.cats, fid = self.fid)
  290. def GetCats(self):
  291. """!Get id of selected vector object or 'None' if nothing selected
  292. @param id if true return ids otherwise cats
  293. """
  294. if self.fid < 0:
  295. return None
  296. return self.cats[self.fid]
  297. def GetFid(self):
  298. """!Get selected feature id"""
  299. return self.fid
  300. def UpdateDialog(self, map = None, query = None, cats = None, fid = -1,
  301. action = None):
  302. """!Update dialog
  303. @param map name of vector map
  304. @param query
  305. @param cats
  306. @param fid feature id
  307. @param action add, update, display or None
  308. @return True if updated
  309. @return False
  310. """
  311. if action:
  312. self.action = action
  313. if action == 'display':
  314. enabled = False
  315. else:
  316. enabled = True
  317. self.closeDialog.Enable(enabled)
  318. self.FindWindowById(wx.ID_OK).Enable(enabled)
  319. if map:
  320. self.map = map
  321. # get layer/table/column information
  322. self.mapDBInfo = VectorDBInfo(self.map)
  323. if not self.mapDBInfo:
  324. return False
  325. self.mapDBInfo.Reset()
  326. layers = self.mapDBInfo.layers.keys() # get available layers
  327. # id of selected line
  328. if query: # select by position
  329. data = self.mapDBInfo.SelectByPoint(query[0],
  330. query[1])
  331. self.cats = {}
  332. if data and 'Layer' in data:
  333. idx = 0
  334. for layer in data['Layer']:
  335. layer = int(layer)
  336. if data['Id'][idx] is not None:
  337. tfid = int(data['Id'][idx])
  338. else:
  339. tfid = 0 # Area / Volume
  340. if not tfid in self.cats:
  341. self.cats[tfid] = {}
  342. if not layer in self.cats[tfid]:
  343. self.cats[tfid][layer] = []
  344. cat = int(data['Category'][idx])
  345. self.cats[tfid][layer].append(cat)
  346. idx += 1
  347. else:
  348. self.cats = cats
  349. if fid > 0:
  350. self.fid = fid
  351. elif len(self.cats.keys()) > 0:
  352. self.fid = self.cats.keys()[0]
  353. else:
  354. self.fid = -1
  355. if len(self.cats.keys()) == 1:
  356. self.fidMulti.Show(False)
  357. self.fidText.Show(True)
  358. if self.fid > 0:
  359. self.fidText.SetLabel("%d" % self.fid)
  360. else:
  361. self.fidText.SetLabel(_("Unknown"))
  362. else:
  363. self.fidMulti.Show(True)
  364. self.fidText.Show(False)
  365. choices = []
  366. for tfid in self.cats.keys():
  367. choices.append(str(tfid))
  368. self.fidMulti.SetItems(choices)
  369. self.fidMulti.SetStringSelection(str(self.fid))
  370. # reset notebook
  371. self.notebook.DeleteAllPages()
  372. for layer in layers: # for each layer
  373. if not query: # select by layer/cat
  374. if self.fid > 0 and layer in self.cats[self.fid]:
  375. for cat in self.cats[self.fid][layer]:
  376. nselected = self.mapDBInfo.SelectFromTable(layer,
  377. where = "%s=%d" % \
  378. (self.mapDBInfo.layers[layer]['key'],
  379. cat))
  380. else:
  381. nselected = 0
  382. # if nselected <= 0 and self.action != "add":
  383. # continue # nothing selected ...
  384. if self.action == "add":
  385. if nselected <= 0:
  386. if layer in self.cats[self.fid]:
  387. table = self.mapDBInfo.layers[layer]["table"]
  388. key = self.mapDBInfo.layers[layer]["key"]
  389. columns = self.mapDBInfo.tables[table]
  390. for name in columns.keys():
  391. if name == key:
  392. for cat in self.cats[self.fid][layer]:
  393. self.mapDBInfo.tables[table][name]['values'].append(cat)
  394. else:
  395. self.mapDBInfo.tables[table][name]['values'].append(None)
  396. else: # change status 'add' -> 'update'
  397. self.action = "update"
  398. table = self.mapDBInfo.layers[layer]["table"]
  399. key = self.mapDBInfo.layers[layer]["key"]
  400. columns = self.mapDBInfo.tables[table]
  401. for idx in range(len(columns[key]['values'])):
  402. for name in columns.keys():
  403. if name == key:
  404. cat = int(columns[name]['values'][idx])
  405. break
  406. # use scrolled panel instead (and fix initial max height of the window to 480px)
  407. panel = scrolled.ScrolledPanel(parent = self.notebook, id = wx.ID_ANY,
  408. size = (-1, 150))
  409. panel.SetupScrolling(scroll_x = False)
  410. self.notebook.AddPage(page = panel, text = " %s %d / %s %d" % (_("Layer"), layer,
  411. _("Category"), cat))
  412. # notebook body
  413. border = wx.BoxSizer(wx.VERTICAL)
  414. flexSizer = wx.FlexGridSizer (cols = 3, hgap = 3, vgap = 3)
  415. flexSizer.AddGrowableCol(2)
  416. # columns (sorted by index)
  417. names = [''] * len(columns.keys())
  418. for name in columns.keys():
  419. names[columns[name]['index']] = name
  420. for name in names:
  421. if name == key: # skip key column (category)
  422. continue
  423. vtype = columns[name]['type'].lower()
  424. ctype = columns[name]['ctype']
  425. if columns[name]['values'][idx] is not None:
  426. if columns[name]['ctype'] != types.StringType:
  427. value = str(columns[name]['values'][idx])
  428. else:
  429. value = columns[name]['values'][idx]
  430. else:
  431. value = ''
  432. colName = wx.StaticText(parent = panel, id = wx.ID_ANY,
  433. label = name)
  434. colType = wx.StaticText(parent = panel, id = wx.ID_ANY,
  435. label = "[%s]:" % vtype)
  436. colValue = wx.TextCtrl(parent = panel, id = wx.ID_ANY, value = value)
  437. colValue.SetName(name)
  438. if ctype == int:
  439. colValue.SetValidator(IntegerValidator())
  440. elif ctype == float:
  441. colValue.SetValidator(FloatValidator())
  442. self.Bind(wx.EVT_TEXT, self.OnSQLStatement, colValue)
  443. if self.action == 'display':
  444. colValue.SetWindowStyle(wx.TE_READONLY)
  445. flexSizer.Add(colName, proportion = 0,
  446. flag = wx.ALIGN_CENTER_VERTICAL)
  447. flexSizer.Add(colType, proportion = 0,
  448. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT)
  449. flexSizer.Add(colValue, proportion = 1,
  450. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL)
  451. # add widget reference to self.columns
  452. columns[name]['ids'].append(colValue.GetId()) # name, type, values, id
  453. # for each attribute (including category) END
  454. border.Add(item = flexSizer, proportion = 1, flag = wx.ALL | wx.EXPAND, border = 5)
  455. panel.SetSizer(border)
  456. # for each category END
  457. # for each layer END
  458. if self.notebook.GetPageCount() == 0:
  459. self.noFoundMsg.Show(True)
  460. else:
  461. self.noFoundMsg.Show(False)
  462. self.Layout()
  463. return True
  464. def SetColumnValue(self, layer, column, value):
  465. """!Set attrbute value
  466. @param column column name
  467. @param value value
  468. """
  469. table = self.mapDBInfo.GetTable(layer)
  470. columns = self.mapDBInfo.GetTableDesc(table)
  471. for key, col in columns.iteritems():
  472. if key == column:
  473. col['values'] = [col['ctype'](value),]
  474. break
  475. class ModifyTableRecord(wx.Dialog):
  476. def __init__(self, parent, title, data, keyEditable = (-1, True),
  477. id = wx.ID_ANY, style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER):
  478. """!Dialog for inserting/updating table record
  479. @param data a list: [(column, value)]
  480. @param KeyEditable (id, editable?) indicates if textarea for key column
  481. is editable(True) or not
  482. """
  483. # parent -> VDigitWindow
  484. wx.Dialog.__init__(self, parent, id, title, style = style)
  485. self.CenterOnParent()
  486. self.keyId = keyEditable[0]
  487. box = wx.StaticBox(parent = self, id = wx.ID_ANY)
  488. box.Hide()
  489. self.dataPanel = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY,
  490. style = wx.TAB_TRAVERSAL)
  491. self.dataPanel.SetupScrolling(scroll_x = False)
  492. # buttons
  493. self.btnCancel = wx.Button(self, wx.ID_CANCEL)
  494. self.btnSubmit = wx.Button(self, wx.ID_OK, _("&Submit"))
  495. self.btnSubmit.SetDefault()
  496. # data area
  497. self.widgets = []
  498. cId = 0
  499. self.usebox = False
  500. self.cat = None
  501. winFocus = False
  502. for column, ctype, ctypeStr, value in data:
  503. if self.keyId == cId:
  504. self.cat = int(value)
  505. if not keyEditable[1]:
  506. self.usebox = True
  507. box.SetLabel(" %s %d " % (_("Category"), self.cat))
  508. box.Show()
  509. self.boxSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  510. cId += 1
  511. continue
  512. else:
  513. valueWin = wx.SpinCtrl(parent = self.dataPanel, id = wx.ID_ANY,
  514. value = value, min = -1e9, max = 1e9, size = (250, -1))
  515. else:
  516. valueWin = wx.TextCtrl(parent = self.dataPanel, id = wx.ID_ANY,
  517. value = value, size = (250, -1))
  518. if ctype == int:
  519. valueWin.SetValidator(IntegerValidator())
  520. elif ctype == float:
  521. valueWin.SetValidator(FloatValidator())
  522. if not winFocus:
  523. wx.CallAfter(valueWin.SetFocus)
  524. winFocus = True
  525. label = wx.StaticText(parent = self.dataPanel, id = wx.ID_ANY,
  526. label = column)
  527. ctype = wx.StaticText(parent = self.dataPanel, id = wx.ID_ANY,
  528. label = "[%s]:" % ctypeStr.lower())
  529. self.widgets.append((label.GetId(), ctype.GetId(), valueWin.GetId()))
  530. cId += 1
  531. self._layout()
  532. def _layout(self):
  533. """!Do layout"""
  534. sizer = wx.BoxSizer(wx.VERTICAL)
  535. # data area
  536. dataSizer = wx.FlexGridSizer(cols = 3, hgap = 3, vgap = 3)
  537. dataSizer.AddGrowableCol(2)
  538. for labelId, ctypeId, valueId in self.widgets:
  539. label = self.FindWindowById(labelId)
  540. ctype = self.FindWindowById(ctypeId)
  541. value = self.FindWindowById(valueId)
  542. dataSizer.Add(label, proportion = 0,
  543. flag = wx.ALIGN_CENTER_VERTICAL)
  544. dataSizer.Add(ctype, proportion = 0,
  545. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT)
  546. dataSizer.Add(value, proportion = 0,
  547. flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL)
  548. self.dataPanel.SetAutoLayout(True)
  549. self.dataPanel.SetSizer(dataSizer)
  550. dataSizer.Fit(self.dataPanel)
  551. if self.usebox:
  552. self.boxSizer.Add(item = self.dataPanel, proportion = 1,
  553. flag = wx.EXPAND | wx.ALL, border = 5)
  554. # buttons
  555. btnSizer = wx.StdDialogButtonSizer()
  556. btnSizer.AddButton(self.btnCancel)
  557. btnSizer.AddButton(self.btnSubmit)
  558. btnSizer.Realize()
  559. if not self.usebox:
  560. sizer.Add(item = self.dataPanel, proportion = 1,
  561. flag = wx.EXPAND | wx.ALL, border = 5)
  562. else:
  563. sizer.Add(item = self.boxSizer, proportion = 1,
  564. flag = wx.EXPAND | wx.ALL, border = 5)
  565. sizer.Add(item = btnSizer, proportion = 0,
  566. flag = wx.EXPAND | wx.ALL, border = 5)
  567. framewidth = self.GetBestSize()[0] + 25
  568. self.SetMinSize((framewidth, 250))
  569. self.SetAutoLayout(True)
  570. self.SetSizer(sizer)
  571. sizer.Fit(self)
  572. self.Layout()
  573. def GetValues(self, columns = None):
  574. """!Return list of values (casted to string).
  575. If columns is given (list), return only values of given columns.
  576. """
  577. valueList = []
  578. for labelId, ctypeId, valueId in self.widgets:
  579. column = self.FindWindowById(labelId).GetLabel().replace(':', '')
  580. if columns is None or column in columns:
  581. value = str(self.FindWindowById(valueId).GetValue())
  582. valueList.append(value)
  583. # add key value
  584. if self.usebox:
  585. valueList.insert(self.keyId, str(self.cat))
  586. return valueList
  587. class AddColumnDialog(wx.Dialog):
  588. def __init__(self, parent, title, id = wx.ID_ANY,
  589. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER):
  590. """!Dialog for adding column into table
  591. """
  592. wx.Dialog.__init__(self, parent, id, title, style = style)
  593. self.CenterOnParent()
  594. self.data = {}
  595. self.data['addColName'] = wx.TextCtrl(parent = self, id = wx.ID_ANY, value = '',
  596. size = (150, -1), style = wx.TE_PROCESS_ENTER)
  597. self.data['addColType'] = wx.Choice (parent = self, id = wx.ID_ANY,
  598. choices = ["integer",
  599. "double",
  600. "varchar",
  601. "date"]) # FIXME
  602. self.data['addColType'].SetSelection(0)
  603. self.data['addColType'].Bind(wx.EVT_CHOICE, self.OnTableChangeType)
  604. self.data['addColLength'] = wx.SpinCtrl(parent = self, id = wx.ID_ANY, size = (65, -1),
  605. initial = 250,
  606. min = 1, max = 1e6)
  607. self.data['addColLength'].Enable(False)
  608. # buttons
  609. self.btnCancel = wx.Button(self, wx.ID_CANCEL)
  610. self.btnOk = wx.Button(self, wx.ID_OK)
  611. self.btnOk.SetDefault()
  612. self._layout()
  613. def _layout(self):
  614. sizer = wx.BoxSizer(wx.VERTICAL)
  615. addSizer = wx.BoxSizer(wx.HORIZONTAL)
  616. addSizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY, label = _("Column")),
  617. flag = wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT,
  618. border = 5)
  619. addSizer.Add(item = self.data['addColName'], proportion = 1,
  620. flag = wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT,
  621. border = 5)
  622. addSizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY, label = _("Type")),
  623. flag = wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT,
  624. border = 5)
  625. addSizer.Add(item = self.data['addColType'],
  626. flag = wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT,
  627. border = 5)
  628. addSizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY, label = _("Length")),
  629. flag = wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT,
  630. border = 5)
  631. addSizer.Add(item = self.data['addColLength'],
  632. flag = wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT,
  633. border = 5)
  634. sizer.Add(item = addSizer, proportion = 0,
  635. flag = wx.ALIGN_RIGHT | wx.ALL, border = 5)
  636. btnSizer = wx.StdDialogButtonSizer()
  637. btnSizer.AddButton(self.btnCancel)
  638. btnSizer.AddButton(self.btnOk)
  639. btnSizer.Realize()
  640. sizer.Add(item = btnSizer, proportion = 0,
  641. flag = wx.ALIGN_RIGHT | wx.ALL, border = 5)
  642. self.SetSizer(sizer)
  643. self.Fit()
  644. def GetData(self):
  645. """!Get inserted data from dialog's widgets"""
  646. values = {}
  647. values['name'] = self.data['addColName'].GetValue()
  648. values['ctype'] = self.data['addColType'].GetStringSelection()
  649. values['length'] = int(self.data['addColLength'].GetValue())
  650. return values
  651. def OnTableChangeType(self, event):
  652. """!Data type for new column changed. Enable or disable
  653. data length widget"""
  654. if event.GetString() == "varchar":
  655. self.data['addColLength'].Enable(True)
  656. else:
  657. self.data['addColLength'].Enable(False)