table_viewer.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """
  2. /***************************************************************************
  3. Name : DB Manager
  4. Description : Database manager plugin for QGIS
  5. Date : May 23, 2011
  6. copyright : (C) 2011 by Giuseppe Sucameli
  7. email : brush.tyler@gmail.com
  8. ***************************************************************************/
  9. /***************************************************************************
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. ***************************************************************************/
  17. """
  18. from qgis.PyQt.QtCore import Qt
  19. from qgis.PyQt.QtWidgets import QTableView, QAbstractItemView, QApplication, QAction
  20. from qgis.PyQt.QtGui import QKeySequence, QCursor, QClipboard
  21. from qgis.utils import OverrideCursor
  22. from .db_plugins.plugin import DbError, Table
  23. from .dlg_db_error import DlgDbError
  24. class TableViewer(QTableView):
  25. def __init__(self, parent=None):
  26. QTableView.__init__(self, parent)
  27. self.setSelectionBehavior(QAbstractItemView.SelectRows)
  28. self.setSelectionMode(QAbstractItemView.ExtendedSelection)
  29. self.item = None
  30. self.dirty = False
  31. # allow copying results
  32. copyAction = QAction(QApplication.translate("DBManagerPlugin", "Copy"), self)
  33. self.addAction(copyAction)
  34. copyAction.setShortcuts(QKeySequence.Copy)
  35. copyAction.triggered.connect(self.copySelectedResults)
  36. self._clear()
  37. def refresh(self):
  38. self.dirty = True
  39. self.loadData(self.item)
  40. def loadData(self, item):
  41. if item == self.item and not self.dirty:
  42. return
  43. self._clear()
  44. if item is None:
  45. return
  46. if isinstance(item, Table):
  47. self._loadTableData(item)
  48. else:
  49. return
  50. self.item = item
  51. self.item.aboutToChange.connect(self.setDirty)
  52. def setDirty(self, val=True):
  53. self.dirty = val
  54. def _clear(self):
  55. if self.item is not None:
  56. try:
  57. self.item.aboutToChange.disconnect(self.setDirty)
  58. except:
  59. # do not raise any error if self.item was deleted
  60. pass
  61. self.item = None
  62. self.dirty = False
  63. # delete the old model
  64. model = self.model()
  65. self.setModel(None)
  66. if model:
  67. model.deleteLater()
  68. def _loadTableData(self, table):
  69. with OverrideCursor(Qt.WaitCursor):
  70. try:
  71. # set the new model
  72. self.setModel(table.tableDataModel(self))
  73. except DbError as e:
  74. DlgDbError.showError(e, self)
  75. else:
  76. self.update()
  77. def copySelectedResults(self):
  78. if len(self.selectedIndexes()) <= 0:
  79. return
  80. model = self.model()
  81. # convert to string using tab as separator
  82. text = model.headerToString("\t")
  83. for idx in self.selectionModel().selectedRows():
  84. text += "\n" + model.rowToString(idx.row(), "\t")
  85. QApplication.clipboard().setText(text, QClipboard.Selection)
  86. QApplication.clipboard().setText(text, QClipboard.Clipboard)