-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSmartDemon.py
More file actions
278 lines (241 loc) · 7.43 KB
/
SmartDemon.py
File metadata and controls
278 lines (241 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#coding:utf-8
##########################################
#Author:rswofnd
#Vision:1.0
#DateTime:2012-6-7
##########################################
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import sys,os,datetime
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt,QTimer, QTime
############################################
#初始化Qt界面及行为
#向第三方类及模块暴露API接口
class SmartDemon(QtGui.QWidget):
"""
docstring for SmartDemon(精灵)
"""
def __init__(self,side):
super(SmartDemon, self).__init__()
self.side = side
self.w = (self.side-20)/8
self.initui()
self.handleChange()
self.init_Plugins()
self.timer = QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(100)
self.show()
def initui(self):
"""
初始化UI界面
"""
self.setGeometry(300,300,self.side,self.side)
self.popMenu()
sizeGrip=QtGui.QSizeGrip(self)
self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint|Qt.SubWindow|Qt.WA_Moved)
self.trans=False #True?
self.set_transparency(True) #设置窗口透明
def popMenu(self):
"""
定义弹出菜单
"""
setupAction = QtGui.QAction(QtGui.QIcon('SetUp.png'), '&SetUp', self) #定义动作抽象
setupAction.triggered.connect(self.setUp)
quitAction = QtGui.QAction( 'Quit', self)
quitAction.triggered.connect(QtGui.qApp.quit)
self.popMenu= QtGui.QMenu() #定义右键弹出菜单
self.popMenu.addAction(setupAction)
self.popMenu.addAction(quitAction)
self.rightButton=False
def set_transparency(self, enabled):
"""
窗口透明度显示实现
"""
if enabled:
self.setAttribute(Qt.WA_Moved, True)
self.setAutoFillBackground(False)
else:
self.setAttribute(Qt.WA_NoSystemBackground, False)
# 下面这种方式好像不行
# pal=QtGui.QPalette()
# pal.setColor(QtGui.QPalette.Background, QColor(127, 127,10,120))
# self.setPalette(pal)
self.setAttribute(Qt.WA_TranslucentBackground, enabled)
self.update()
# self.repaint() no better than self.update()
def resizeEvent(self,e):
"""
重新实现尺寸拖拽事件
"""
self.handleChange()
def handleChange(self):
"""
改变控件大小
"""
self.side = min(self.width(), self.height())
self.w = (self.side-20)/8
def mouseReleaseEvent(self,e):
"""
鼠标释放事件
"""
if self.rightButton == True:
self.rightButton=False
self.popMenu.popup(e.globalPos())
def mouseMoveEvent(self, e):
"""
鼠标移动事件
"""
if e.buttons() & Qt.LeftButton:
self.move(e.globalPos()-self.dragPos)
e.accept()
def mousePressEvent(self, e):
"""
鼠标按下事件
"""
if e.button() == Qt.LeftButton:
self.dragPos=e.globalPos()-self.frameGeometry().topLeft()
e.accept()
if e.button() == Qt.RightButton and self.rightButton == False:
self.rightButton=True
def closeEvent(self,event):
"""
应用于窗口关闭弹出菜单
是对系统默认关闭事件的‘重新实现’
"""
reply = QtGui.QMessageBox.question(self,'Message','sure to quit?',
QtGui.QMessageBox.Yes|QtGui.QMessageBox.No,QtGui.QMessageBox.No)
if reply==QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
def paintEvent(self,event):
"""
重新实现组件绘制事件
"""
qp = QtGui.QPainter()
#################################
#时钟API接口
timeAPI = TimeM(0,0,0)
self.h = timeAPI.hour
self.m = timeAPI.minute
self.s = timeAPI.second
self.ms = timeAPI.m_second
#时,分,秒显示
self.hour = -((15.0 * ((self.h + self.m/ 60.0)))*16)
self.minute = -((6.0 * (self.m + ((self.s+self.ms/1000)/ 60.0)))*16)
self.second = -(6.0*(self.s+float(self.ms)/1000)*16)
qp.begin(self)
#开始绘制
qp.setRenderHint(QtGui.QPainter.Antialiasing) #开启抗锯齿
pen = QtGui.QPen(QtGui.QColor(112,166,18),self.w*0.382,QtCore.Qt.SolidLine)
qp.setPen(pen) #设置画笔颜色,粗细和线型
qp.drawArc((10+self.w),(10+self.w),(self.side-2*(10+self.w)),(self.side-2*(10+self.w)),90*16,self.second) #绘制弧线段,x,y,w,h,a,alen
pen = QtGui.QPen(QtGui.QColor(34,113,204),self.w*0.618,QtCore.Qt.SolidLine)
qp.setPen(pen) #设置画笔颜色,粗细和线型
qp.drawArc((10+2*self.w),(10+2*self.w),(self.side-2*(10+2*self.w)),(self.side-2*(10+2*self.w)),90*16,self.minute) #绘制弧线段,x,y,w,h,a,alen
pen = QtGui.QPen(QtGui.QColor(219,92,29),self.w*0.382,QtCore.Qt.SolidLine)
qp.setPen(pen) #设置画笔颜色,粗细和线型
qp.drawArc((10+3*self.w),(10+3*self.w),(self.side-2*(10+3*self.w)),(self.side-2*(10+3*self.w)),90*16,self.hour) #绘制弧线段,x,y,w,h,a,alen
#结束绘制
qp.end()
def setUp(self):
"""
定时,任务,插件等参数设置
"""
pass
def init_Plugins(self):
"""
自动加载插件
管理和运行插件
"""
self.plugin = Platform()
self.plugins = self.plugin.plugins
for x in self.plugins:
Action = QtGui.QAction(QtGui.QIcon('Action.png'),x, self)
Action.triggered.connect(self.plugin.runPlugin)
self.popMenu.addAction(Action)
########################################
#时钟类
#向主类提供时钟API接口
class TimeM(object):
"""
docstring for TimeM
"""
def __init__(self,count_h,count_m,count_s):
super(TimeM, self).__init__()
self.year = 0
self.month = 0
self.day = 0
self.wday = 0
self.yday = 0
self.hour = 0
self.minute = 0
self.second = 0
self.m_second = 0
self.c_h = count_h
self.c_m = count_m
self.c_s = count_s
self.do_What()
def do_What(self):
if (self.c_h==0 and self.c_m==0 and self.c_s==0):
self.now()
else:
self.counter()
def now(self):
t = datetime.datetime.now()
self.year = t.year
self.month = t.month
self.day = t.day
self.hour = t.hour
self.minute = t.minute
self.second = t.second
self.m_second = t.microsecond/1000
self.wday = t.isoweekday()
self.str_time = t.strftime('%Y-%m-%d %H:%M:%S')
def counter(self):
pass
#######################################
#插件平台
#提供插件API接口
class Platform(SmartDemon):
"""
调用plugins目录下非Zip插件包
该目录下必须包含一个__init__.py空文件和相关插件模块
"""
def __init__(self):
self.plugins=[]
self.loadPlugins()
def sayHello(self, from_):
print "hello from %s." % from_
def sayGoodbye(self, from_):
print "goodbye from %s." % from_
def loadPlugins(self):
for filename in os.listdir("plugins"):
if not filename.endswith(".py") or filename.startswith("_"):
continue
self.plugins.append(filename) #添加插件列表
def runPlugin(self, filename):
pluginName=os.path.splitext(filename)[0]
plugin=__import__("plugins."+pluginName, fromlist=[pluginName])
clazz=plugin.getPluginClass()
o=clazz()
o.setPlatform(self)
o.start() #运行插件start()函数
def shutdown(self):
for o in self.plugins:
o.stop() #运行插件stop()函数
o.setPlatform(None)
self.plugins=[] #清空插件列表
#############################################
#模块入口主函数
def main_demon():
app = QtGui.QApplication(sys.argv)
demon = SmartDemon(100)
sys.exit(app.exec_())
#############################################
#测试代码
if __name__ == '__main__':
main_demon()