1 # Copyright (c) 2010 Citrix Systems, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from OVEStandard import *
16 from OVEConfig import *
17 from OVEFetch import *
18 from OVELogger import *
21 from OVECommonWindow import *
23 from Ui_MainWindow import *
25 class OVEMainWindow(QtGui.QMainWindow, OVECommonWindow):
26 LOAD_KEY = 'MainWindow/window'
29 def __init__(self, app, loadIndex = None):
30 QtGui.QMainWindow.__init__(self)
31 self.ui = Ui_MainWindow()
32 OVECommonWindow.__init__(self, app, loadIndex)
34 def xon_tabWidget_currentChanged(self, value):
35 self.deleteCurrentTable()
36 OVECommonWindow.xon_tabWidget_currentChanged(self, value)
38 def updateTable(self):
39 if self.hostUuid == '':
40 self.setWindowTitle('OVS Database')
41 self.deleteCurrentTable()
42 self.statusBar().showMessage('No host selected. Choose File->Preferences to add a host')
44 config = OVEConfig.Inst().hostFromUuid(self.hostUuid)
45 self.setWindowTitle('OVS Database - '+config.get('address', ''))
46 self.invalidateCurrentTable('Fetching data...')
47 tabName = self.ui.tabWidget.currentWidget().objectName()
51 OVEFetch.Inst(self.hostUuid).getTable(self, tabName, self.currentRef)
53 OVELog("Error fetching data: "+str(e))
54 self.invalidateCurrentTable(str(e))
56 def timerEvent(self, event):
57 OVECommonWindow.timerEvent(self, event)
59 def customEvent(self, event):
60 OVECommonWindow.customEvent(self, event)
62 def handleFetchEvent(self, ref, values):
63 tabName = self.ui.tabWidget.currentWidget().objectName()
64 self.structToTable(getattr(self.ui, str(tabName)+'Table'), values)
66 def handleFetchFailEvent(self, ref, message):
67 self.invalidateCurrentTable(str(message))
69 def structToTable(self, table, values):
71 table.setUpdatesEnabled(False)
72 table.setSortingEnabled(False)
76 table.setRowCount(len(result['rows']))
77 for row in result['rows']:
78 table.setColumnCount(len(row))
80 for k in sorted(row.keys()):
82 headerItem = QtGui.QTableWidgetItem(k)
83 table.setHorizontalHeaderItem(colNum, headerItem)
84 text = OVEUtil.paramToString(v)
85 item = QtGui.QTableWidgetItem(text)
86 longText = OVEUtil.paramToLongString(v)
87 item.setToolTip(longText)
89 table.setItem(rowNum, colNum, item)
94 for i in range(0, table.columnCount()):
95 table.resizeColumnToContents(i)
96 for i in range(0, table.rowCount()):
97 table.resizeRowToContents(i)
99 # table.setSortingEnabled(True)
100 table.setUpdatesEnabled(True)
102 message = 'Updated at '+str(QtCore.QTime.currentTime().toString())
103 if table.rowCount() == 0:
104 message += ' - Table is empty'
105 self.statusBar().showMessage(message)
107 def invalidateCurrentTable(self, message):
108 tabName = self.ui.tabWidget.currentWidget().objectName()
109 self.invalidateTable(getattr(self.ui, str(tabName)+'Table'), message)
111 def invalidateTable(self, table, message):
112 table.setUpdatesEnabled(False)
113 table.setSortingEnabled(False)
115 for rowNum in range(0, table.rowCount()):
116 for colNum in range(0, table.columnCount()):
117 item = table.takeItem(rowNum, colNum)
119 item.setForeground(Qt.darkGray)
120 table.setItem(rowNum, colNum, item)
121 self.statusBar().showMessage(message)
122 # table.setSortingEnabled(True)
123 table.setUpdatesEnabled(True)
125 def deleteCurrentTable(self):
126 tabName = self.ui.tabWidget.currentWidget().objectName()
127 self.deleteTable(getattr(self.ui, str(tabName)+'Table'))
129 def deleteTable(self, table):
132 table.setColumnCount(0)
134 def saveSettings(self, index):
135 settings = OVECommonWindow.saveSettings(self, index)
137 def loadSettings(self, index):
138 settings = OVECommonWindow.loadSettings(self, index)