viewgit/index.php:465 Only variables should be passed by reference [2048]

viewgit/index.php:466 Non-static method GeSHi::get_language_name_from_extension() should not be called statically [2048]

  1. #!/usr/bin/env python
  2.  
  3. import re,sys, os
  4. sys.path.append("./libs")
  5. sys.path.append("./ui")
  6. # Questo e' solo un trucchetto per fare in modo che il programm
  7. # trovi le mie librerie anche se viene lanciato da strani posti :)
  8. os.chdir("".join(sys.argv[0].rsplit("songbook-editor.py",1)))
  9. from PyQt4 import QtGui, QtCore
  10. from interface import *
  11. from options import *
  12. import syntax_highlighter
  13. from song import *
  14. from latex_manager import *
  15.  
  16.  
  17.  
  18. class interface(QtGui.QMainWindow):
  19. def __init__(self, lm, parent=None):
  20. super(interface, self).__init__(parent)
  21. self.ui = Ui_MainWindow()
  22. self.ui.setupUi(self)
  23.  
  24. # options Database
  25. self.opt = {}
  26. self.set_default_options()
  27.  
  28. # Create an empty database of songs...
  29. self.song_db = []
  30.  
  31. # Our format separator
  32. self.sep = ":::"
  33. self.sep_song = "***"
  34.  
  35. # Song edit part
  36. self.connect(self.ui.btn_create_pdf, QtCore.SIGNAL("clicked()"), self.export_to_PDF)
  37. self.connect(self.ui.btn_savesong, QtCore.SIGNAL("clicked()"), self.savesong)
  38.  
  39. # About the list
  40. self.connect(self.ui.list_songs, QtCore.SIGNAL("currentTextChanged ( QString )"), self.item_selected)
  41. self.connect(self.ui.btn_new_song, QtCore.SIGNAL("clicked()"), self.new_song)
  42. self.connect(self.ui.btn_delete_song, QtCore.SIGNAL("clicked()"), self.delete_item_from_list)
  43. self.connect(self.ui.btn_list_move_down, QtCore.SIGNAL("clicked()"), self.list_move_down)
  44. self.connect(self.ui.btn_list_move_up, QtCore.SIGNAL("clicked()"), self.list_move_up)
  45.  
  46. # Menu File
  47. self.connect(self.ui.actionSalva, QtCore.SIGNAL("activated()"), self.save_songbook)
  48. self.connect(self.ui.actionApri, QtCore.SIGNAL("activated()"), self.load_songbook)
  49.  
  50. # Menu Canzone
  51. self.connect(self.ui.actionSalva_canzone, QtCore.SIGNAL("activated()"), self.save_song_to_file)
  52. self.connect(self.ui.actionImporta_canzone, QtCore.SIGNAL("activated()"), self.import_song_from_file)
  53. self.connect(self.ui.actionEsporta_in_DVI, QtCore.SIGNAL("activated()"), self.export_to_DVI)
  54. self.connect(self.ui.actionEsporta_in_PDF, QtCore.SIGNAL("activated()"), self.export_to_PDF)
  55.  
  56. # Menu Canzoniere
  57. self.connect(self.ui.actionEsporta_in_LaTeX, QtCore.SIGNAL("activated()"), self.export_songbook)
  58. self.connect(self.ui.actionOpzioni_LaTeX, QtCore.SIGNAL("activated()"), self.options)
  59. self.connect(self.ui.actionOrdina_Canzoni, QtCore.SIGNAL("activated()"), self.sort_list)
  60.  
  61. self.sh = syntax_highlighter.syntax_highlighter(self.ui.te_body.document())
  62.  
  63. # Functions to manage events
  64.  
  65. # Reset the song screen on right and start working on a new song
  66. def new_song(self):
  67. s = song("")
  68. self.set_active_song(s)
  69.  
  70. def compare_songs(self, song_1, song_2):
  71. if(song_1.title > song_2.title):
  72. return 1
  73. elif(song_1.title < song_2.title):
  74. return -1
  75. else:
  76. return 0
  77.  
  78. def sort_list(self):
  79. self.song_db.sort(self.compare_songs)
  80. self.list_update()
  81.  
  82. # Delete the selected song from the list AND from the song_db
  83. def delete_item_from_list(self):
  84. song = self.get_active_song()
  85. item = self.ui.list_songs.currentRow()
  86. self.ui.list_songs.takeItem(item)
  87. self.song_db.remove(song)
  88.  
  89. # Get the song object of the active song
  90. def get_active_song(self):
  91. for newsong in self.song_db:
  92. if( newsong.title == unicode(self.ui.le_title.text()) ):
  93. return newsong
  94.  
  95. # If no song is found means that we need to create a new one
  96. newtitle = unicode(self.ui.le_title.text())
  97. newmauthor = unicode(self.ui.le_mauthor.text())
  98. newtauthor = unicode(self.ui.le_tauthor.text())
  99. newyear = unicode(self.ui.le_year.text())
  100. newtone = unicode(self.ui.le_tone.text())
  101. newsong = song(newtitle, [], newmauthor, newtauthor, newtone, newyear)
  102.  
  103. newbody = unicode(self.ui.te_body.toPlainText())
  104. # Just debug
  105. # print newbody
  106. newbody = re.split("\n\n+", newbody)
  107. for paragraph in newbody:
  108. if(len(paragraph) < 3):
  109. break
  110. if( (paragraph[0] == 'R') & (paragraph[1] == ':') ):
  111. newsong.add_chorus(paragraph.split("R:")[1])
  112. else:
  113. newsong.add_verse(paragraph)
  114.  
  115. return newsong
  116.  
  117.  
  118. def get_active_song_in_list(self):
  119. newsong = self.get_active_song()
  120. for song in self.song_db:
  121. if( (song.title == newsong.title) ):
  122. return song
  123.  
  124. # Add a new song in the db, and update the list
  125. def add_song_to_db(self, song):
  126. self.song_db.append(song)
  127. self.list_update()
  128.  
  129. # Save (and/or overwrite) the active song in the songbook
  130. def savesong(self):
  131. need_new_song = True
  132. song_to_save = self.get_active_song()
  133.  
  134. # Check if we have already saved this song or if it's a new entry
  135. for song in self.song_db:
  136. if(song.title == song_to_save.title):
  137. # Questa linea di codice e' orribile, ma non capisco come dovrei fare per salvare la canzone
  138. self.song_db[self.song_db.index(song)] = song_to_save
  139. need_new_song = False
  140. break
  141. if(need_new_song):
  142. self.add_song_to_db(song_to_save)
  143.  
  144. def get_item_from_song(self, song):
  145. self.list_update()
  146. ind = self.song_db.index(song)
  147. return self.ui.list_songs.item(ind)
  148.  
  149. def list_move_up(self):
  150. song = self.get_active_song_in_list()
  151. ind = self.song_db.index(song)
  152. if(ind == 0):
  153. return
  154. self.song_db.pop(ind)
  155. self.song_db.insert(ind-1, song)
  156. self.list_update()
  157. self.ui.list_songs.setItemSelected(self.get_item_from_song(song), 1)
  158.  
  159. def list_move_down(self):
  160. song = self.get_active_song_in_list()
  161. ind = self.song_db.index(song)
  162. if(ind == len(self.song_db)):
  163. return
  164. self.song_db.pop(ind)
  165. self.song_db.insert(ind+1, song)
  166. self.list_update()
  167. self.ui.list_songs.setItemSelected(self.get_item_from_song(song), 1)
  168.  
  169.  
  170.  
  171. # Export song to a rcs file
  172. def save_song_to_file(self):
  173. filename = QtGui.QFileDialog.getSaveFileName(self, "Salva Canzone", "", "Canzoni di RobolCanzoniere (*.rcs)")
  174. output = self.create_song_file()
  175. handle = open(filename, 'w')
  176. handle.write(output.encode("utf-8"))
  177. handle.close()
  178.  
  179. # Export songbook to a rcc file
  180. def save_songbook(self):
  181. # Default format is concatenated song files, with self.song_sep to
  182. # separate them, so
  183. saving = ""
  184. for song in self.song_db:
  185. saving += self.create_song_file(song) + self.sep_song
  186. filename = QtGui.QFileDialog.getSaveFileName(self, "Salva Canzoniere", "", "Canzoniere di RobolCanzoniere (*.rcc)")
  187. if(filename != ""):
  188. handle = open(filename, 'w')
  189. handle.write(saving.encode("utf-8"))
  190. handle.close()
  191.  
  192. # Load songbook from a rcc file
  193. def load_songbook(self):
  194. filename = QtGui.QFileDialog.getOpenFileName(self, "Apri Canzoniere", "", "Canzoniere di RobolCanzoniere (*.rcc)")
  195. if(filename != ""):
  196. handle = open(filename, 'r')
  197. buf = handle.read().decode("utf-8")
  198. handle.close()
  199. else:
  200. return
  201. song_list = buf.split(self.sep_song)
  202. self.song_db = []
  203. for raw_song in song_list:
  204. if(raw_song != ""):
  205. self.add_song_to_db(self.file_to_song(raw_song))
  206. # Update the list view
  207. # self.list_update() This is not necessary anymore, because of
  208. # the add_song_to_db function managing the widget update :)
  209. # Less fast, but more readable!
  210.  
  211.  
  212. # Update the list view. This function is used from all the other functions that
  213. # edit the self.song_db to make the list widget respecting it
  214. def list_update(self):
  215. self.ui.list_songs.clear()
  216. for song in self.song_db:
  217. self.ui.list_songs.addItem(song.title)
  218.  
  219.  
  220. # Create the rcs file of song_to_save (non interactive function) that returns it
  221. def create_song_file(self, song_to_save=''):
  222. sep = unicode(self.sep)
  223. if(song_to_save == ''):
  224. song_to_save = self.get_active_song()
  225. # filename = QtGui.QFileDialog.getSaveFileName(self, "Salva canzone", "", "Canzoni di RobolCanzoniere (*.rcs)")
  226. # handle = open(filename, 'w')
  227. output = unicode()
  228. output += song_to_save.title + sep + song_to_save.mauthor + sep + song_to_save.tauthor + sep + song_to_save.tone + sep + song_to_save.year
  229. for par in song_to_save.body:
  230. if(par.is_chorus()):
  231. rit = "R:"
  232. else:
  233. rit = ""
  234. output += sep + rit + par.content()
  235. return output
  236.  
  237. # Convert a file (rcs) to a song object
  238. def file_to_song(self, file_content):
  239. buf = file_content.split(self.sep)
  240. newsong = song(buf[0], [], buf[1], buf[2], buf[3], buf[4])
  241. for j in range(5,1024):
  242. try:
  243. if( (buf[j][0] == 'R') & (buf[j][1] == ':') ):
  244. newsong.add_chorus(buf[j].split("R:")[1])
  245. else:
  246. newsong.add_verse(buf[j])
  247. except:
  248. break
  249. return newsong
  250.  
  251. # import a song from a file on the local hard drive, the function is interactive!
  252. def import_song_from_file(self):
  253. filetoimport = QtGui.QFileDialog.getOpenFileName(self, "Importa canzone", "", "Canzoni di RobolCanzoniere (*.rcs)")
  254. handle = open(filetoimport, 'r')
  255. buf = handle.read().decode("utf-8")
  256. handle.close()
  257. self.set_active_song(self.file_to_song(buf))
  258.  
  259. def set_active_song(self, newsong):
  260. self.ui.le_title.setText(newsong.title)
  261. self.ui.le_mauthor.setText(newsong.mauthor)
  262. self.ui.le_tauthor.setText(newsong.tauthor)
  263. self.ui.le_year.setText(newsong.year)
  264. self.ui.le_tone.setText(newsong.tone)
  265. output = unicode()
  266. for paragraph in newsong.body:
  267. if(paragraph.is_chorus()):
  268. output += "R:"
  269. output += paragraph.content()
  270. output += "\n\n"
  271. self.ui.te_body.setDocument(QtGui.QTextDocument(output, self.ui.te_body))
  272. self.sh = syntax_highlighter.syntax_highlighter(self.ui.te_body.document())
  273.  
  274.  
  275. def item_selected(self, item_text):
  276. newsong = ""
  277. for song in self.song_db:
  278. if(song.title == unicode(item_text)):
  279. newsong = song
  280. if(newsong!=""):
  281. self.set_active_song(newsong)
  282. else:
  283. self.new_song
  284.  
  285. # Converte una canzone in un file LaTeX non autosufficiente, che puo' essere inserito in un altro canzoniere latex.
  286. def create_latex_song(self):
  287. song = self.get_active_song()
  288. filetowrite = lm.create_song(song)
  289. filename = QtGui.QFileDialog.getSaveFileName(self, "Salva file latex", "/home/leonardo", "LaTeX Source File (*.tex)")
  290. if(filename == ''):
  291. # We do not have to do nothing, the user clicked Cancel
  292. return 0
  293. handle = open(filename, 'w')
  294. # Just remember that filetowrite is an "unicode" object, treat it
  295. # as it just deserve
  296. handle.write(filetowrite.encode("utf-8"))
  297. handle.close()
  298.  
  299.  
  300. # Esporta il canzoniere in LaTeX, in ogni caso dovra' essere esportato
  301. def export_songbook(self):
  302. # Chiedo al latex manager di farlo.. :)
  303. sbk = lm.export_songbook(self.song_db, widget.opt)
  304.  
  305. # Apriamo un file
  306. filename = QtGui.QFileDialog.getSaveFileName(self, "Salva file latex", "/home/leonardo", "LaTeX Source File (*.tex)")
  307. if(filename == ''):
  308. # We do not have to do nothing, the user clicked Cancel
  309. return 0
  310. handle = open(filename, 'w')
  311. handle.write(sbk.encode("utf-8"))
  312. handle.close()
  313.  
  314. # Esporta il canzoniere in DVI
  315. def export_to_DVI(self):
  316. # Otteniamo il codice LaTeX
  317. sbk = lm.export_songbook(self.song_db, widget.opt)
  318. # Compiliamo il DVI
  319. dvifile = lm.latex_compile(sbk, widget.opt)
  320. # Apriamo un file
  321. filename = QtGui.QFileDialog.getSaveFileName(self, "Salva file DVI", "", "DVI file (*.dvi)")
  322. if(filename == ''):
  323. # We do not have to do nothing, the user clicked Cancel
  324. return 0
  325. handle = open(filename, 'wb')
  326. handle.write(dvifile)
  327. handle.close()
  328.  
  329. # Esporta il canzoniere in PDF
  330. def export_to_PDF(self):
  331. pdf_file = lm.create_pdf_from_songbook(self.song_db, widget.opt)
  332. # Apriamo un file
  333. filename = QtGui.QFileDialog.getSaveFileName(self, "Salva file PDF", "", "PDF file (*.pdf)")
  334. if(filename == ''):
  335. # We do not have to do nothing, the user clicked Cancel
  336. return 0
  337. handle = open(filename, 'wb')
  338. handle.write(pdf_file)
  339. handle.close()
  340.  
  341.  
  342.  
  343. def options(self):
  344. option.show()
  345.  
  346. def set_default_options(self):
  347. self.opt["paper_size"] = "a5"
  348. self.opt["title"] = "Canzoniere"
  349. self.opt["subtitle"] = "Autore Sconosciuto"
  350. self.opt["type"] = "chordbk"
  351.  
  352.  
  353. # This class manage the Dialog that prompt the user for
  354. # common options...
  355. class option_interface(QtGui.QWidget):
  356. def __init__(self, parent=None):
  357. super(option_interface,self).__init__(parent)
  358. self.ui = Ui_options()
  359. self.ui.setupUi(self)
  360.  
  361. # Options dictionary
  362. self.paper_size_dic = {0:"a4", 1:"a5"}
  363. self.type_dic = {0:"chordbk", 1:"wordbk", 2:"overhead"}
  364.  
  365.  
  366. QtCore.QObject.connect(self.ui.buttonBox, QtCore.SIGNAL("accepted()"), self.ok)
  367. self.connect(self.ui.buttonBox, QtCore.SIGNAL("rejected()"), self.cancel)
  368.  
  369. def cancel(self):
  370. self.hide()
  371.  
  372. def ok(self):
  373. # Set paper size
  374. widget.opt["paper_size"] = self.paper_size_dic[self.ui.paper_size.currentIndex()]
  375.  
  376. # Set songbook type
  377. widget.opt["type"] = self.type_dic[self.ui.type.currentIndex()]
  378.  
  379. # Set songbook title
  380. widget.opt["title"] = unicode(self.ui.title.text())
  381. # Set songbook subtitle
  382. widget.opt["subtitle"] = unicode(self.ui.subtitle.text())
  383.  
  384. # Hide options
  385. self.hide()
  386.  
  387.  
  388.  
  389. if __name__ == "__main__":
  390. app = QtGui.QApplication(sys.argv)
  391. option = option_interface()
  392. lm = latex_manager()
  393. widget = interface(lm)
  394. widget.show()
  395. app.exec_()