~lrcshow-x/lrcshow-x/2-series

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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Filename: lrcEditor.py
# Lisence: GPL-2.0

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import os,re,s2t
#from players.playerBase import Status
#from engines.plain_lyrics import getLyrics
#from urllib import unquote
import embedLrc
import ConfigParser


class lrcEditor(QMainWindow):
	
	def __init__(self, parent = None, *args):
		QMainWindow.__init__(self, parent)
		self.setWindowIcon(QIcon('./icon/lrcShow-X.png'))
		self.setWindowTitle(_('lrcShow-X lrc editor'))
		self.setMinimumSize(QSize(500,280))
		self.initMenuBar(args[2], args[3], args[4])
		self.initToolBar()
		self.initCenterWidget()
		self.initStatusBar()
		self.locale = args[0]
		self.di=args[1]
		self.config = ConfigParser.ConfigParser()
		self.config.read(os.path.expanduser('~/.lrcShow-X/lrcShow-X.conf'))
		state = QByteArray.fromBase64(self.config.get('option', 'editorstate'))
		self.restoreState(state)
		
		self.connect(self.lrcArea, SIGNAL("textChanged()"), self.saveEnable)
		self.connect(self.quitAction,SIGNAL("triggered()"),self,SLOT("close()"))
		self.connect(self.saveAction,SIGNAL("triggered()"),self.saveAction_)
		self.connect(self.saveasAction,SIGNAL("triggered()"),self.saveasAction_)
		self.connect(self.lrcArea,SIGNAL("redoAvailable(bool)"),self.redoAction,SLOT("setEnabled(bool)"))
		self.connect(self.lrcArea,SIGNAL("undoAvailable(bool)"),self.undoAction,SLOT("setEnabled(bool)"))
		self.connect(self.redoAction,SIGNAL("triggered()"),self.lrcArea,SLOT("redo()"))
		self.connect(self.undoAction,SIGNAL("triggered()"),self.lrcArea,SLOT("undo()"))
		self.connect(self.selectallAction,SIGNAL("triggered()"),self.lrcArea,SLOT("selectAll()"))
		self.connect(self.lrcArea,SIGNAL("copyAvailable(bool)"),self.copyEnable)
		self.connect(self.copyAction,SIGNAL("triggered()"),self.lrcArea,SLOT("copy()"))
		self.connect(self.pasteAction,SIGNAL("triggered()"),self.lrcArea,SLOT("paste()"))
		self.connect(self.cutAction,SIGNAL("triggered()"),self.lrcArea,SLOT("cut()"))
		self.connect(self.inserttagAction,SIGNAL("triggered()"),self.inserttagAction_)
		self.connect(self.cleartagAction,SIGNAL("triggered()"),self.cleartagAction_)
		self.connect(self.clearlrcAction,SIGNAL("triggered()"),self.clearlrcAction_)
		self.connect(self.clearheadAction,SIGNAL("triggered()"),self.clearheadAction_)
		self.connect(self.cutpointAction,SIGNAL("triggered()"),self.cutpointAction_)
		self.connect(self.cutspaceAction,SIGNAL("triggered()"),self.cutspaceAction_)
		self.connect(self.sttransferAction,SIGNAL("triggered()"),self.sttransferAction_)
		#self.connect(self.downloadAction,SIGNAL("triggered()"),self.downloadAction_)
	
	#def downloadAction_(self):
		#if(self.artist != '' and self.title != ''):
			#lyrics,timeout = getLyrics(unicode(self.artist, self.locale).encode("utf-8"),unicode(self.title, self.locale).encode("utf-8"), self.proxy, True)
			#if timeout or not lyrics:
				#lyrics = ""
		#else:
			#lyrics = ""
		#self.lrcArea.setText('[ti:%s]\n[ar:%s]\n[al:]\n[by:%s]\n%s' % (unicode(self.title, self.locale),unicode(self.artist, self.locale), os.getenv("USER"), lyrics))
	
	def sttransferAction_(self):
		p=s2t.s2t(str(self.lrcArea.toPlainText().toUtf8()))
		content=p.transfer()
		self.lrcArea.selectAll()
		self.lrcArea.insertPlainText(content.decode('utf8'))
		self.lrcArea.moveCursor(QTextCursor.Start)
		
	def cutspaceAction_(self):
		lrc_before = str(self.lrcArea.toPlainText().toUtf8())
		self.lrcArea.selectAll()
		lrc_before = re.sub('^\s{1,}|\s{1,}$','',lrc_before)
		lrc_after = re.sub('\s{0,}\n\s{0,}','\n',lrc_before)
		self.lrcArea.insertPlainText(lrc_after.decode('utf8'))
		self.lrcArea.moveCursor(QTextCursor.Start)
		
	def cutpointAction_(self):
		lrc_before = str(self.lrcArea.toPlainText().toUtf8())
		self.lrcArea.selectAll()
		lrc_after = re.sub('\.\d\d\]',']',lrc_before)
		self.lrcArea.insertPlainText(lrc_after.decode('utf8'))
		self.lrcArea.moveCursor(QTextCursor.Start)
		
	def clearheadAction_(self):
		lrc_before = str(self.lrcArea.toPlainText().toUtf8())
		self.lrcArea.selectAll()
		lrc_after = re.sub('\[[a-z,A-Z]{1,}:.*?\]\n{0,1}','',lrc_before)
		self.lrcArea.insertPlainText(lrc_after.decode('utf8'))
		self.lrcArea.moveCursor(QTextCursor.Start)
	
	def clearlrcAction_(self):
		self.lrcArea.selectAll()
		self.lrcArea.cut()
	
	def cleartagAction_(self):
		content=str(self.lrcArea.toPlainText().toUtf8())
		self.lrcArea.selectAll()
		content=re.sub('\[\d+:\d+.*?\]','',content)
		self.lrcArea.insertPlainText(content.decode('utf8'))
		self.lrcArea.moveCursor(QTextCursor.Start)
	
	def inserttagAction_(self):
		currentTime = int(self.di.getTimePoint())
		time = ["minutes","seconds","ms"]
		time[1],time[2] = divmod(currentTime/10,100)
		time[0],time[1] = divmod(time[1],60)
		for i in range(3):
			time[i] = str(time[i]).zfill(2)
		currentTag = '[%s:%s.%s]' % (time[0],time[1],time[2])
		self.lrcArea.textCursor().insertText(currentTag)
		self.lrcArea.moveCursor(QTextCursor.StartOfLine)
		self.lrcArea.moveCursor(QTextCursor.Down)
	
	def copyEnable(self,bool):
		if(bool == True):
			self.copyAction.setEnabled(True)
			self.cutAction.setEnabled(True)
		else:
			self.copyAction.setEnabled(False)
			self.cutAction.setEnabled(False)

	
	def saveasAction_(self):
		if(self.lrcType == _('local')):
			tmp = self.saveTarget
		else:
			tmp = os.path.join(self.savePath, self.nameForSave)
		filename = QFileDialog.getSaveFileName(self, _('Save as'), tmp.decode(self.locale),_('lrc files(*.lrc)'))
		if(str(filename.toUtf8()) != ''):
			content = str(self.lrcArea.toPlainText().toUtf8())
			try:
				content = unicode(content, 'utf8').encode(self.encoderForSave)
			except UnicodeEncodeError:
				b = QMessageBox(QMessageBox.Critical, _('Encoding error'), _('Current text could not be encoded by the codec for saving\nPlease check your setting'))
				bu = QPushButton(QIcon('./icon/ok.png'), _('Ok'))
				b.addButton(bu, QMessageBox.AcceptRole)
				b.exec_()
			else:
				file = open(str(filename.toLocal8Bit()), 'w')
				file.write(content)
				file.close()
				self.emit(SIGNAL("changeEncoding()"))
	
	def saveEnable(self):
		if((self.lrcType == _('local') or self.lrcType == _('embed')) and not self.saveAction.isEnabled()):
			self.saveAction.setEnabled(True)
	
	def saveAction_(self):
		if(self.lrcType == _('local')):
			content = str(self.lrcArea.toPlainText().toUtf8())
			try:
				content = unicode(content, 'utf8').encode(self.encoderForSave)
			except UnicodeEncodeError:
				b = QMessageBox(QMessageBox.Critical, _('Encoding error'), _('Current text could not be encoded by the codec for saving\nPlease check your setting'))
				bu = QPushButton(QIcon('./icon/ok.png'), _('Ok'))
				b.addButton(bu, QMessageBox.AcceptRole)
				b.exec_()
			else:
				file = open(self.saveTarget, 'w')
				file.write(content)
				file.close()
				self.saveAction.setEnabled(False)
				self.emit(SIGNAL("changeEncoding()"))
		elif(self.lrcType == _('embed')):
			content = self.lrcArea.toPlainText().toUtf8()
			p = embedLrc.embedLrc(self.saveTarget)
			p.deleteLrc(self.lrcTagType)
			lrcTmp = p.detectLrc()
			p.insertLrc(self.lrcTagType, content)
			self.saveAction.setEnabled(False)
		
	
	def initCenterWidget(self):
		self.lrcArea=QTextEdit(self)
		self.lrcArea.setAcceptRichText(False)
		self.setCentralWidget(self.lrcArea)
	
	def initStatusBar(self):
		self.statusLabel = QLabel(None)
		self.statusBar().addWidget(self.statusLabel)
	
	def initToolBar(self):
		toolBarEdit = self.addToolBar(_('ToolBar'))
		toolBarEdit.setFloatable(False)
		toolBarEdit.setObjectName('ToolBarEdit')
		toolBarEdit.addAction(self.saveAction)
		toolBarEdit.addAction(self.saveasAction)
		toolBarEdit.addAction(self.undoAction)
		toolBarEdit.addAction(self.redoAction)
		toolBarEdit.addAction(self.cutAction)
		toolBarEdit.addAction(self.copyAction)
		toolBarEdit.addAction(self.pasteAction)
		toolBarEdit.addAction(self.inserttagAction)
		
		toolBarControl = self.addToolBar(_('Player control'))
		toolBarControl.setFloatable(False)
		toolBarControl.setObjectName('toolBarControl')
		toolBarControl.addAction(self.playorpauseAction)
		toolBarControl.addAction(self.stopAction)
		toolBarControl.addAction(self.prevAction)
		toolBarControl.addAction(self.nextAction)
		
		toolBarSync = self.addToolBar(_('Sync mode'))
		toolBarSync.setFloatable(False)
		toolBarSync.setObjectName('toolBarSync')
		toolBarSync.addAction(self.syncModeAction)
	
	def initMenuBar(self, F5, F6, b):
		menuBar = self.menuBar()
		menuFile = menuBar.addMenu(_('File(&F)'))
		self.saveAction = QAction(QIcon('./icon/save.png'), _('Save(&S)'), self)
		self.saveAction.setShortcut(Qt.CTRL + Qt.Key_S)
		self.saveAction.setEnabled(False)
		menuFile.addAction(self.saveAction)
		self.saveasAction = QAction(QIcon('./icon/saveas.png'), _('Save as(&A)...'), self)
		menuFile.addAction(self.saveasAction)
		self.quitAction = QAction(QIcon('./icon/exit.png'), _('Quit(&Q)'), self)
		self.quitAction.setShortcut(Qt.CTRL + Qt.Key_Q)
		menuFile.addAction(self.quitAction)
		
		menuEdit = menuBar.addMenu(_('Edit(&E)'))
		self.undoAction = QAction(QIcon('./icon/undo.png'), _('Undo(&U)'), self)
		self.undoAction.setShortcut(F6)
		self.undoAction.setEnabled(False)
		menuEdit.addAction(self.undoAction)
		self.redoAction = QAction(QIcon('./icon/redo.png'), _('Redo(&D)'), self)
		self.redoAction.setShortcut(Qt.CTRL + Qt.SHIFT + Qt.Key_Z)
		self.redoAction.setEnabled(False)
		menuEdit.addAction(self.redoAction)
		self.cutAction = QAction(QIcon('./icon/editcut.png'), _('Cut(&T)'), self)
		self.cutAction.setShortcut(Qt.CTRL + Qt.Key_X)
		self.cutAction.setEnabled(False)
		menuEdit.addAction(self.cutAction)
		self.copyAction = QAction(QIcon('./icon/editcopy.png'), _('Copy(&C)'), self)
		self.copyAction.setShortcut(Qt.CTRL + Qt.Key_C)
		self.copyAction.setEnabled(False)
		menuEdit.addAction(self.copyAction)
		self.pasteAction = QAction(QIcon('./icon/editpaste.png'), _('Paste(&P)'), self)
		self.pasteAction.setShortcut(Qt.CTRL + Qt.Key_V)
		menuEdit.addAction(self.pasteAction)
		self.selectallAction = QAction(QIcon('./icon/selectall.png'), _('Select all(&A)'), self)
		self.selectallAction.setShortcut(Qt.CTRL + Qt.Key_A)
		menuEdit.addAction(self.selectallAction)
		
		menuOperation = menuBar.addMenu(_('Operation(&O)'))
		self.inserttagAction = QAction(QIcon('./icon/insert_tag.png'), _('Insert a tag(&I)'), self)
		self.inserttagAction.setShortcut(F5)
		menuOperation.addAction(self.inserttagAction)
		self.cleartagAction = QAction(QIcon(), _('Clear all tags(&R)'), self)
		menuOperation.addAction(self.cleartagAction)
		self.cutpointAction = QAction(QIcon(), _('Tags accuracy to second(&S)'), self)
		menuOperation.addAction(self.cutpointAction)
		self.cutspaceAction = QAction(QIcon(), _('Remove useless characters(&P)'), self)
		menuOperation.addAction(self.cutspaceAction)
		self.clearlrcAction = QAction(QIcon(), _('Clear LRC(&C)'), self)
		menuOperation.addAction(self.clearlrcAction)
		self.clearheadAction = QAction(QIcon(), _('Clear LRC head'), self)
		menuOperation.addAction(self.clearheadAction)
		self.sttransferAction = QAction(QIcon('./icon/st.png'), _('S-T transfer(&T)'), self)
		menuOperation.addAction(self.sttransferAction)
		
		syncMode = menuBar.addMenu(_('Sync mode(&S)'))
		self.syncModeAction = QAction(_('Sync mode'), self)
		self.syncModeAction.setCheckable(True)
		if(b == 'yes'):
			self.syncModeAction.setChecked(True)
			self.syncModeAction.setIcon(QIcon('./icon/sync_unlock.png'))
		else:
			self.syncModeAction.setChecked(False)
			self.syncModeAction.setIcon(QIcon('./icon/sync_lock.png'))
		syncMode.addAction(self.syncModeAction)
		self.syncManually = QAction(QIcon('./icon/reload.png'), _('Sync manually(&M)'), self)
		syncMode.addAction(self.syncManually)
		
		menuPlayerControl = menuBar.addMenu(_('Media player(&M)'))
		self.playorpauseAction = QAction(QIcon('./icon/player_play.png'), _('Play(&P)'),self)
		menuPlayerControl.addAction(self.playorpauseAction)
		self.stopAction = QAction(QIcon('./icon/player_stop.png'), _('Stop(&S)'), self)
		menuPlayerControl.addAction(self.stopAction)
		self.prevAction = QAction(QIcon('./icon/player_back.png'), _('Prev(&V)'), self)
		menuPlayerControl.addAction(self.prevAction)
		self.nextAction = QAction(QIcon('./icon/player_next.png'), _('Next(&N)'), self)
		menuPlayerControl.addAction(self.nextAction)
		
		#menuDownload = menuBar.addMenu(_('Download(&D)'))
		#self.downloadAction = QAction(QIcon('./icon/download.png'), _('Download plain lyrics(&L)'), self)
		#menuDownload.addAction(self.downloadAction)
	
	def closeEvent(self, ev):
		state = self.saveState().toBase64()
		self.config.set('option', 'editorstate', state)
		f = open(os.path.expanduser('~/.lrcShow-X/lrcShow-X.conf'), 'w')
		self.config.write(f)
		f.close()
		ev.accept()

# test code

def main(args):
	app=QApplication(args)
	win=lrcEditor()
	win.show()
	app.connect(app, SIGNAL("lastWindowClosed()"),app, SLOT("quit()"))
	app.exec_()

if __name__=="__main__":
	main(sys.argv)