openlp/openlp/plugins/songs/forms/editverseform.py

141 lines
6.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2009-12-31 12:52:01 +00:00
# Copyright (c) 2008-2010 Raoul Snyman #
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
2010-03-21 23:58:01 +00:00
# Gorven, Scott Guerrieri, Christian Richter, Maikel Stuivenberg, Martin #
# Thompson, Jon Tibble, Carsten Tinggaard #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
2010-04-03 23:00:05 +00:00
import re
import logging
2009-09-25 00:43:42 +00:00
from PyQt4 import QtCore, QtGui
2010-04-03 23:00:05 +00:00
2010-06-05 20:00:50 +00:00
from openlp.plugins.songs.forms import VerseType
from editversedialog import Ui_EditVerseDialog
2010-04-03 23:00:05 +00:00
log = logging.getLogger(__name__)
class EditVerseForm(QtGui.QDialog, Ui_EditVerseDialog):
"""
This is the form that is used to edit the verses of the song.
"""
def __init__(self, parent=None):
"""
Constructor
"""
QtGui.QDialog.__init__(self, parent)
self.setupUi(self)
2010-04-03 23:00:05 +00:00
QtCore.QObject.connect(
self.InsertButton,
QtCore.SIGNAL(u'clicked()'),
self.onInsertButtonClicked
)
QtCore.QObject.connect(
self.VerseTextEdit,
QtCore.SIGNAL(u'cursorPositionChanged()'),
self.onCursorPositionChanged
)
self.verse_regex = re.compile(r'---\[([-\w]+):([\d]+)\]---')
def insertVerse(self, title, num=1):
2010-03-27 07:01:50 +00:00
if self.VerseTextEdit.textCursor().columnNumber() != 0:
self.VerseTextEdit.insertPlainText(u'\n')
2010-04-03 23:00:05 +00:00
self.VerseTextEdit.insertPlainText(u'---[%s:%s]---\n' % (title, num))
2010-03-26 21:04:50 +00:00
self.VerseTextEdit.setFocus()
2010-04-03 23:00:05 +00:00
def onInsertButtonClicked(self):
if self.VerseTextEdit.textCursor().columnNumber() != 0:
self.VerseTextEdit.insertPlainText(u'\n')
verse_type = self.VerseTypeComboBox.currentIndex()
if verse_type == VerseType.Verse:
2010-06-05 20:00:50 +00:00
self.insertVerse(VerseType.to_string(VerseType.Verse),
self.VerseNumberBox.value())
2010-04-03 23:00:05 +00:00
elif verse_type == VerseType.Chorus:
2010-06-05 20:00:50 +00:00
self.insertVerse(VerseType.to_string(VerseType.Chorus),
self.VerseNumberBox.value())
2010-04-03 23:00:05 +00:00
elif verse_type == VerseType.Bridge:
2010-06-05 20:00:50 +00:00
self.insertVerse(VerseType.to_string(VerseType.Bridge))
2010-04-03 23:00:05 +00:00
elif verse_type == VerseType.PreChorus:
2010-06-06 06:40:08 +00:00
self.insertVerse(VerseType.to_string(VerseType.PreChorus))
2010-04-03 23:00:05 +00:00
elif verse_type == VerseType.Intro:
2010-06-06 06:40:08 +00:00
self.insertVerse(VerseType.to_string(VerseType.Intro))
2010-04-03 23:00:05 +00:00
elif verse_type == VerseType.Ending:
2010-06-06 06:40:08 +00:00
self.insertVerse(VerseType.to_string(VerseType.Ending))
2010-04-03 23:00:05 +00:00
elif verse_type == VerseType.Other:
2010-06-06 06:40:08 +00:00
self.insertVerse(VerseType.to_string(VerseType.Other))
2010-04-03 23:00:05 +00:00
def onCursorPositionChanged(self):
position = self.VerseTextEdit.textCursor().position()
text = unicode(self.VerseTextEdit.toPlainText())
if not text:
return
if text.rfind(u'[', 0, position) > text.rfind(u']', 0, position) and \
text.find(u']', position) < text.find(u'[', position):
return
position = text.rfind(u'---[', 0, position)
if position == -1:
return
text = text[position:]
position = text.find(u']---')
if position == -1:
return
text = text[:position + 4]
match = self.verse_regex.match(text)
if match:
verse_type = match.group(1)
verse_number = int(match.group(2))
verse_type_index = VerseType.from_string(verse_type)
if verse_type_index:
self.VerseTypeComboBox.setCurrentIndex(verse_type_index)
self.VerseNumberBox.setValue(verse_number)
2010-06-06 07:28:07 +00:00
def setVerse(self, text, single=False,
tag=u'%s:1' % VerseType.to_string(VerseType.Verse)):
2009-11-22 20:33:39 +00:00
if single:
2010-04-03 23:00:05 +00:00
verse_type, verse_number = tag.split(u':')
2010-06-08 15:38:09 +00:00
self.VerseTypeComboBox.setCurrentIndex(
VerseType.from_string(verse_type))
2010-04-03 23:00:05 +00:00
self.VerseNumberBox.setValue(int(verse_number))
self.InsertButton.setVisible(False)
2009-11-22 20:33:39 +00:00
else:
2010-04-03 23:00:05 +00:00
if not text:
2010-06-06 06:40:08 +00:00
text = u'---[%s:1]---\n' % VerseType.to_string(VerseType.Verse)
2010-04-03 23:00:05 +00:00
self.VerseTypeComboBox.setCurrentIndex(0)
self.VerseNumberBox.setValue(1)
self.InsertButton.setVisible(True)
2009-11-22 20:33:39 +00:00
self.VerseTextEdit.setPlainText(text)
self.VerseTextEdit.setFocus(QtCore.Qt.OtherFocusReason)
2010-06-05 14:29:06 +00:00
self.VerseTextEdit.moveCursor(QtGui.QTextCursor.End)
def getVerse(self):
2010-04-03 23:00:05 +00:00
return self.VerseTextEdit.toPlainText(), \
VerseType.to_string(self.VerseTypeComboBox.currentIndex()), \
unicode(self.VerseNumberBox.value())
2009-11-26 18:41:39 +00:00
def getVerseAll(self):
2010-02-27 09:55:44 +00:00
text = self.VerseTextEdit.toPlainText()
if not text.startsWith(u'---['):
2010-06-08 15:38:09 +00:00
text = u'---[%s:1]---\n%s' % (VerseType.to_string(VerseType.Verse),
text)
2010-02-27 09:55:44 +00:00
return text