annotate python/ide/logview.py @ 343:11c5a8a70c02 devel

Fix ide
author Windel Bouwman
date Sat, 01 Mar 2014 16:27:52 +0100
parents dcae6574c974
children
rev   line source
281
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
1 #!/usr/bin/python
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
2
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
3 import sys
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
4 import os
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
5 import logging
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
6 import datetime
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
7
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
8 from qtwrapper import QtGui, QtCore, QtWidgets, Qt
281
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
9
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
10 def formatTime(t):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
11 t2 = datetime.datetime.fromtimestamp(t)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
12 return t2.strftime('%H:%M:%S')
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
13
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
14
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
15 class LogModel(QtCore.QAbstractTableModel):
281
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
16 def __init__(self):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
17 super().__init__()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
18 self.entries = []
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
19 self.headers = ['Time', 'Level', 'Logger', 'Message']
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
20 self.txts = []
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
21 self.txts.append(lambda e: str(formatTime(e.created)))
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
22 self.txts.append(lambda e: str(e.levelname))
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
23 self.txts.append(lambda e: str(e.name))
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
24 self.txts.append(lambda e: str(e.msg))
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
25
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
26 def rowCount(self, parent):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
27 return len(self.entries)
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
28
281
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
29 def columnCount(self, parent):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
30 return len(self.headers)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
31
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
32 def data(self, index, role):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
33 if not index.isValid():
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
34 return
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
35 row, col = index.row(), index.column()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
36 if role == Qt.DisplayRole:
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
37 le = self.entries[row]
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
38 return self.txts[col](le)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
39
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
40 def headerData(self, section, orientation, role):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
41 if orientation == Qt.Horizontal and role == Qt.DisplayRole:
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
42 return self.headers[section]
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
43
281
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
44 def newLog(self, x):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
45 self.entries.append(x)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
46 self.modelReset.emit()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
47
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
48
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
49 class LogView(QtWidgets.QWidget):
281
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
50 """ Log view component """
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
51 def __init__(self, parent=None):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
52 super().__init__(parent)
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
53 l = QtWidgets.QVBoxLayout(self)
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
54 self.tv = QtWidgets.QTableView(self)
281
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
55 self.tv.horizontalHeader().setStretchLastSection(True)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
56 l.addWidget(self.tv)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
57 self.lm = LogModel()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
58 self.tv.setModel(self.lm)
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
59
281
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
60 class MyHandler(logging.Handler):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
61 def emit(self2, x):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
62 self.lm.newLog(x)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
63 self.tv.scrollToBottom()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
64 for i in range(3):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
65 self.tv.resizeColumnToContents(i)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
66
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
67 logging.getLogger().addHandler(MyHandler())
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
68
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
69
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
70 if __name__ == '__main__':
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
71 app = QApplication(sys.argv)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
72 lv = LogView()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
73 lv.show()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
74 lv.resize(600, 200)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
75 logging.error('Error!!!')
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
76 logging.warn('Warn here!')
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
77 app.exec_()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
78