1 #!/usr/bin/python
2 #--*--coding:utf-8--*--
3
4 importsys,re5 from PyQt4 importQtGui6 from PyQt4 importQtCore7 from dic importFecth8 importtime9 classIrregularForm(QtGui.QWidget):10 def __init__(self, parent=None):11 QtGui.QWidget.__init__(self, parent)12 self.parent=parent13
14 mask=QtGui.QPixmap("./icons/search40.png")15 self.setMask(QtGui.QBitmap(mask.mask()))16 p=QtGui.QPalette()17 p.setBrush(QtGui.QPalette.Window, QtGui.QBrush(mask))18 self.setPalette(p)19 self.setGeometry(100, 100, 100, 100)20 self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)21 self.setWindowFlags(QtCore.Qt.FramelessWindowHint)22 self.setWindowIcon(QtGui.QIcon('./icons/search50.png'))23 self.mouseMovePos =QtCore.QPoint(0, 0)24 defmouseMoveEvent(self,event):25 if(self.mouseMovePos !=QtCore.QPoint(0, 0)):26 self.move(self.geometry().x() +event.globalPos().x() \27 -self.mouseMovePos.x(),self.geometry().y() \28 + event.globalPos().y() -self.mouseMovePos.y())29 self.mouseMovePos =event.globalPos()30 defmousePressEvent(self,event):31 self.mouseMovePos =event.globalPos()32
33 defmouseReleaseEvent(self,event):34 self.mouseMovePos =QtCore.QPoint(0, 0)35 defmouseDoubleClickEvent(self, event):36 self.emit(QtCore.SIGNAL('trueVisible()'))37 defkeyPressEvent(self, event):38 if event.key() ==QtCore.Qt.Key_X:39 self.emit(QtCore.SIGNAL('trueVisible()'))40 classDic(QtGui.QWidget):41 def __init__(self, parent=None):42 QtGui.QWidget.__init__(self, parent)43 self.alt=None44 self.irregular=IrregularForm()45 self.irregular.show()46
47 self.curTime=time.strftime("%Y-%m-%d %H:%M", \48 time.localtime(time.time()))49 okButton = QtGui.QPushButton("OK")50 cbbtn = QtGui.QPushButton("clip")51
52 self.edit =QtGui.QLineEdit()53 self.word =QtGui.QLabel( self.curTime)54 rem = QtGui.QLabel('
Word:
')55 self.edit.setStyleSheet("color: blue;"56 "selection-color: yellow;"
57 "selection-background-color: blue;")58 #cbbtn.setStyleSheet("color:red");
59 cbbtn.setStyleSheet("color: blue;border:2px groove gray;"
60 "border-radius:10px;padding:2px 4px;")61 okButton.setStyleSheet("border:2px groove gray;"
62 "border-radius:10px;padding:2px 4px;")63
64 hbox =QtGui.QHBoxLayout()65 hbox.addWidget(rem)66 hbox.addWidget(self.edit)67 hbox.addWidget(okButton)68 hbox.addWidget(cbbtn)69
70 vbox =QtGui.QVBoxLayout()71 vbox.addLayout(hbox)72 vbox.addWidget(self.word)73
74 self.setLayout(vbox)75 self.setWindowOpacity(0.95)76 self.setWindowTitle('Dictionary')77 self.setGeometry(300, 300, 350, 150)78 self.setWindowIcon(QtGui.QIcon('./icons/search50.png'))79 self.connect(okButton, QtCore.SIGNAL('clicked()'), \80 self.okButton)81 self.connect(cbbtn, QtCore.SIGNAL('clicked()'), \82 self.clipboardBotton)83 self.connect(self.irregular, QtCore.SIGNAL('trueVisible()'), \84 self,QtCore.SLOT('trueVisible()') )85 #self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
86 self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)87 #self.fe=Fecth()
88 @QtCore.pyqtSlot()89 deftrueVisible(self):90 self.setVisible(True)91 self.irregular.setVisible(False)92
93 defclipboardBotton(self):94 clipboard =QtGui.QApplication.clipboard()95 printclipboard.text()96 self.edit.setText(clipboard.text())97 self.buttonClicked(clipboard.text())98 defokButton(self):99 src=self.edit.text()100 self.buttonClicked(src)101 defsetColor(self,string,key):102 if key=='red':103 return ''+string+''+'
'
104 elif key=='blue':105 return ''+string+''+'
'
106 else:107 return ''+string+''+'
'
108 defbuttonClicked(self,src):109
110 g=re.match('[a-zA-Z]+',src)111 if notg:112 self.word.setText(self.setColor('
Not a word!
','red'))113 return114 src=g.group()115 self.fe=Fecth()116 rows=self.fe.searchDB(src,None)117 trans=''
118 ifrows:119 for row inrows:120 trans = self.setColor(row[0],'red')121 if row[1]!='':122 trans+=self.setColor(row[1]+','+row[2],'blue')123 trans+= self.setColor(row[3],'green')124 self.word.setText('
'+trans+'
')125 else:126127 trans=self.setColor(src,'red')128 zh=self.fe.fecth('qt')129 print zh[0],zh[1]130 trans+=self.setColor(zh[0],'blue')131 tmp=zh[1]132 if tmp=='':133 tmp='Fetch fail!'
134 trans+= self.setColor(tmp,'green')135 self.word.setText('
'+trans+'
')136 self.fe.close()137 defkeyReleaseEvent(self, event):138139 if event.key() ==QtCore.Qt.Key_Return:140 self.okButton()141 elif event.key() ==QtCore.Qt.Key_Shift:142 self.clipboardBotton()143 elif event.key() ==QtCore.Qt.Key_Alt:144 self.alt=QtCore.Qt.Key_Alt145 self.setVisible(False)146 self.irregular.setVisible(True)147 elif event.key() ==QtCore.Qt.Key_Space:148 self.edit.setText('')149 defcloseEvent(self, event):150
151 QtGui.qApp.quit()152 #self.irregular.destroy()
153 defleaveEvent(self,evt):154 if self.alt==QtCore.Qt.Key_Alt:155 self.alt=-1
156 return
157 cx,cy=QtGui.QCursor.pos().x(),QtGui.QCursor.pos().y()158 if(cx >= self.x() and cx <= self.x()+self.width()159 and cy >= self.y() and cy <=self.y()+self.height()):160 pass
161 else:162
163 self.irregular.setVisible(True)164 self.setVisible(False)165 #self.setWindowOpacity(0.3)
166
167 app =QtGui.QApplication(sys.argv)168 dic =Dic()169 dic.show()170 sys.exit(app.exec_())