info_viewer.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 QTextBrowser, QApplication
  20. from qgis.utils import OverrideCursor
  21. from .db_plugins.plugin import BaseError, DbError, DBPlugin, Schema, Table
  22. from .dlg_db_error import DlgDbError
  23. class InfoViewer(QTextBrowser):
  24. def __init__(self, parent=None):
  25. QTextBrowser.__init__(self, parent)
  26. self.setOpenLinks(False)
  27. self.item = None
  28. self.dirty = False
  29. self._clear()
  30. self._showPluginInfo()
  31. self.anchorClicked.connect(self._linkClicked)
  32. def _linkClicked(self, url):
  33. if self.item is None:
  34. return
  35. if url.scheme() == "action":
  36. with OverrideCursor(Qt.WaitCursor):
  37. try:
  38. if self.item.runAction(url.path()):
  39. self.refresh()
  40. except BaseError as e:
  41. DlgDbError.showError(e, self)
  42. def refresh(self):
  43. self.setDirty(True)
  44. self.showInfo(self.item)
  45. def showInfo(self, item):
  46. if item == self.item and not self.dirty:
  47. return
  48. self._clear()
  49. if item is None:
  50. return
  51. if isinstance(item, DBPlugin):
  52. self._showDatabaseInfo(item)
  53. elif isinstance(item, Schema):
  54. self._showSchemaInfo(item)
  55. elif isinstance(item, Table):
  56. self._showTableInfo(item)
  57. else:
  58. return
  59. self.item = item
  60. item.aboutToChange.connect(self.setDirty)
  61. def setDirty(self, val=True):
  62. self.dirty = val
  63. def _clear(self):
  64. if self.item is not None:
  65. # skip exception on RuntimeError fixes #6892
  66. try:
  67. self.item.aboutToChange.disconnect(self.setDirty)
  68. except RuntimeError:
  69. pass
  70. self.item = None
  71. self.dirty = False
  72. self.item = None
  73. self.setHtml("")
  74. def _showPluginInfo(self):
  75. from .db_plugins import getDbPluginErrors
  76. html = '<div style="background-color:rgba(255,255,95,0.3);"><h1>&nbsp;' + self.tr("DB Manager") + '</h1></div>'
  77. html += '<div style="margin-left:8px;">'
  78. for msg in getDbPluginErrors():
  79. html += "<p>%s" % msg
  80. self.setHtml(html)
  81. def _showDatabaseInfo(self, connection):
  82. html = '<div style="background-color:rgba(120,255,100,0.3);"><h1>&nbsp;%s</h1></div>' % connection.connectionName()
  83. html += '<div style="margin-left:8px;">'
  84. try:
  85. if connection.database() is None:
  86. html += connection.info().toHtml()
  87. else:
  88. html += connection.database().info().toHtml()
  89. except DbError as e:
  90. html += '<p style="color:red">%s</p>' % str(e).replace('\n', '<br>')
  91. html += '</div>'
  92. self.setHtml(html)
  93. def _showSchemaInfo(self, schema):
  94. html = '<div style="background-color:rgba(255,100,100,0.3);"><h1>&nbsp;%s</h1></div>' % schema.name
  95. html += '<div style="margin-left:8px;">'
  96. try:
  97. html += schema.info().toHtml()
  98. except DbError as e:
  99. html += '<p style="color:red">%s</p>' % str(e).replace('\n', '<br>')
  100. html += "</div>"
  101. self.setHtml(html)
  102. def _showTableInfo(self, table):
  103. html = '<div style="background-color:rgba(100,100,255,0.3)"><h1>&nbsp;%s</h1></div>' % table.name
  104. html += '<div style="margin-left:8px;">'
  105. try:
  106. html += table.info().toHtml()
  107. except DbError as e:
  108. html += '<p style="color:red">%s</p>' % str(e).replace('\n', '<br>')
  109. html += '</div>'
  110. self.setHtml(html)
  111. return True
  112. def setHtml(self, html):
  113. # convert special tags :)
  114. html = str(html).replace('<warning>', '<img src=":/db_manager/warning">&nbsp;&nbsp; ')
  115. # add default style
  116. html = """
  117. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  118. <html>
  119. <head>
  120. <style type="text/css">
  121. .section { margin-top: 25px; }
  122. table th { background-color: palette(midlight); color: palette(shadow); }
  123. table td { background-color: palette(light); }
  124. table th, table td { padding: 0px 10px; }
  125. table td { padding-right: 20px; }
  126. .underline { text-decoration:underline; }
  127. </style>
  128. </head>
  129. <body>
  130. %s <br>
  131. </body>
  132. </html>
  133. """ % html
  134. # print(">>>>>\n", html, "\n<<<<<<")
  135. return QTextBrowser.setHtml(self, html)