dlg_create_index.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """
  2. /***************************************************************************
  3. Name : DB Manager
  4. Description : Database manager plugin for QGIS
  5. Date : Oct 13, 2011
  6. copyright : (C) 2011 by Giuseppe Sucameli
  7. email : brush.tyler@gmail.com
  8. The content of this file is based on
  9. - PG_Manager by Martin Dobias (GPLv2 license)
  10. ***************************************************************************/
  11. /***************************************************************************
  12. * *
  13. * This program is free software; you can redistribute it and/or modify *
  14. * it under the terms of the GNU General Public License as published by *
  15. * the Free Software Foundation; either version 2 of the License, or *
  16. * (at your option) any later version. *
  17. * *
  18. ***************************************************************************/
  19. """
  20. from qgis.PyQt.QtCore import Qt
  21. from qgis.PyQt.QtWidgets import QDialog, QMessageBox, QApplication
  22. from qgis.utils import OverrideCursor
  23. from .db_plugins.plugin import DbError
  24. from .dlg_db_error import DlgDbError
  25. from .db_plugins.plugin import TableIndex
  26. from .ui.ui_DlgCreateIndex import Ui_DbManagerDlgCreateIndex as Ui_Dialog
  27. class DlgCreateIndex(QDialog, Ui_Dialog):
  28. def __init__(self, parent=None, table=None, db=None):
  29. QDialog.__init__(self, parent)
  30. self.table = table
  31. self.db = self.table.database() if self.table and self.table.database() else db
  32. self.setupUi(self)
  33. self.buttonBox.accepted.connect(self.createIndex)
  34. self.cboColumn.currentIndexChanged.connect(self.columnChanged)
  35. self.populateColumns()
  36. def populateColumns(self):
  37. self.cboColumn.clear()
  38. for fld in self.table.fields():
  39. self.cboColumn.addItem(fld.name)
  40. def columnChanged(self):
  41. self.editName.setText("idx_%s_%s" % (self.table.name, self.cboColumn.currentText()))
  42. def createIndex(self):
  43. idx = self.getIndex()
  44. if idx.name == "":
  45. QMessageBox.critical(self, self.tr("Error"), self.tr("Please enter a name for the index."))
  46. return
  47. # now create the index
  48. with OverrideCursor(Qt.WaitCursor):
  49. try:
  50. self.table.addIndex(idx)
  51. except DbError as e:
  52. DlgDbError.showError(e, self)
  53. return
  54. self.accept()
  55. def getIndex(self):
  56. idx = TableIndex(self.table)
  57. idx.name = self.editName.text()
  58. idx.columns = []
  59. colname = self.cboColumn.currentText()
  60. for fld in self.table.fields():
  61. if fld.name == colname:
  62. idx.columns.append(fld.num)
  63. break
  64. return idx