Add form validation - start of

bzr-revno: 283
This commit is contained in:
Tim Bentley 2009-01-12 19:31:29 +00:00
parent cc0ca326c3
commit 7022a05316
2 changed files with 65 additions and 13 deletions

View File

@ -65,6 +65,7 @@ class AuthorsForm(QDialog, Ui_AuthorsDialog):
if self.currentrow > c: # incase we have delete the last row of the table if self.currentrow > c: # incase we have delete the last row of the table
self.currentrow = c self.currentrow = c
self.AuthorListView.selectRow(self.currentrow) # set selected row to previous selected row self.AuthorListView.selectRow(self.currentrow) # set selected row to previous selected row
self._validate_form()
@pyqtSignature("") @pyqtSignature("")
def on_DeleteButton_clicked(self): def on_DeleteButton_clicked(self):
@ -73,8 +74,12 @@ class AuthorsForm(QDialog, Ui_AuthorsDialog):
""" """
self.songmanager.delete_author(self.author.id) self.songmanager.delete_author(self.author.id)
self.on_ClearButton_clicked() self.on_ClearButton_clicked()
self.load_form() self.load_form()
@pyqtSignature("")
def on_DisplayEdit_lostFocus(self):
self._validate_form()
@pyqtSignature("") @pyqtSignature("")
def on_AddUpdateButton_clicked(self): def on_AddUpdateButton_clicked(self):
""" """
@ -88,6 +93,7 @@ class AuthorsForm(QDialog, Ui_AuthorsDialog):
self.songmanager.save_author(self.author) self.songmanager.save_author(self.author)
self.on_ClearButton_clicked() self.on_ClearButton_clicked()
self.load_form() self.load_form()
self._validate_form()
@pyqtSignature("") @pyqtSignature("")
@ -99,8 +105,9 @@ class AuthorsForm(QDialog, Ui_AuthorsDialog):
self.FirstNameEdit.setText("") self.FirstNameEdit.setText("")
self.LastNameEdit.setText("") self.LastNameEdit.setText("")
self.MessageLabel.setText("") self.MessageLabel.setText("")
self.DeleteButton.setEnabled(True) self.DeleteButton.setEnabled(False)
self.author = None self.author = None
self._validate_form()
@pyqtSignature("QTableWidgetItem*") @pyqtSignature("QTableWidgetItem*")
def on_AuthorListView_itemClicked(self, item): def on_AuthorListView_itemClicked(self, item):
@ -122,3 +129,10 @@ class AuthorsForm(QDialog, Ui_AuthorsDialog):
else: else:
self.MessageLabel.setText("Author is not used") self.MessageLabel.setText("Author is not used")
self.DeleteButton.setEnabled(True) self.DeleteButton.setEnabled(True)
self._validate_form()
def _validate_form(self):
if len(self.DisplayEdit.displayText()) == 0: # We need at lease a display name
self.AddUpdateButton.setEnabled(False)
else:
self.AddUpdateButton.setEnabled(True)

View File

@ -17,7 +17,7 @@ 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 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA Place, Suite 330, Boston, MA 02111-1307 USA
""" """
from PyQt4 import QtCore, QtGui from PyQt4 import Qt, QtCore, QtGui
from PyQt4.QtGui import QWidget from PyQt4.QtGui import QWidget
from PyQt4.QtCore import pyqtSignature from PyQt4.QtCore import pyqtSignature
@ -51,6 +51,7 @@ class EditSongForm(QWidget, Ui_EditSongDialog):
self.AuthorsListView.setShowGrid(False) self.AuthorsListView.setShowGrid(False)
self.AuthorsListView.setSortingEnabled(False) self.AuthorsListView.setSortingEnabled(False)
self.AuthorsListView.setAlternatingRowColors(True) self.AuthorsListView.setAlternatingRowColors(True)
self.savebutton = self.ButtonBox.button(QtGui.QDialogButtonBox.Save)
def initialise(self): def initialise(self):
@ -60,18 +61,20 @@ class EditSongForm(QWidget, Ui_EditSongDialog):
self.AuthorsSelectionComboItem.addItem( i.display_name) self.AuthorsSelectionComboItem.addItem( i.display_name)
def load_song(self, songid): def load_song(self, songid):
self.songid = songid if songid == None: # We have a new Song
song = self.songmanager.get_song(songid) self.song = Song()
self.TitleEditItem.setText(song.title) else:
self.LyricsTextEdit.setText(song.lyrics) self.songid = songid
self.CopyrightEditItem.setText(song.copyright) self.song = self.songmanager.get_song(songid)
self.TitleEditItem.setText(self.song.title)
self.LyricsTextEdit.setText(self.song.lyrics)
self.CopyrightEditItem.setText(self.song.copyright)
self.AuthorsListView.clear() # clear the results self.AuthorsListView.clear() # clear the results
self.AuthorsListView.setHorizontalHeaderLabels(QtCore.QStringList(["","Author"])) self.AuthorsListView.setHorizontalHeaderLabels(QtCore.QStringList(["","Author"]))
self.AuthorsListView.setVerticalHeaderLabels(QtCore.QStringList([""])) self.AuthorsListView.setVerticalHeaderLabels(QtCore.QStringList([""]))
self.AuthorsListView.setRowCount(0) self.AuthorsListView.setRowCount(0)
for author in self.song.authors:
for author in song.authors:
c = self.AuthorsListView.rowCount() c = self.AuthorsListView.rowCount()
self.AuthorsListView.setRowCount(c+1) self.AuthorsListView.setRowCount(c+1)
twi = QtGui.QTableWidgetItem(str(author.id)) twi = QtGui.QTableWidgetItem(str(author.id))
@ -79,7 +82,7 @@ class EditSongForm(QWidget, Ui_EditSongDialog):
twi = QtGui.QTableWidgetItem(str(author.display_name)) twi = QtGui.QTableWidgetItem(str(author.display_name))
self.AuthorsListView.setItem(c , 1, twi) self.AuthorsListView.setItem(c , 1, twi)
self.AuthorsListView.setRowHeight(c, 20) self.AuthorsListView.setRowHeight(c, 20)
self._validate_song()
@pyqtSignature("") @pyqtSignature("")
def on_AddAuthorsButton_clicked(self): def on_AddAuthorsButton_clicked(self):
@ -103,4 +106,39 @@ class EditSongForm(QWidget, Ui_EditSongDialog):
Slot documentation goes here. Slot documentation goes here.
""" """
self.song_book_form.load_form() self.song_book_form.load_form()
self.song_book_form.show() self.song_book_form.show()
def _validate_song(self):
"""
Check the validity of the form. Only display the 'save' if the data can be saved.
"""
valid = True # Lets be nice and assume the data is correct.
if len(self.TitleEditItem.displayText()) == 0: #Song title missing
valid = False
self._color_widget(self.TitleEditItem, True)
else:
self._color_widget(self.TitleEditItem, False)
if len(self.CopyrightEditItem.displayText()) == 0: #Song title missing
valid = False
self._color_widget(self.CopyrightEditItem, True)
else:
self._color_widget(self.CopyrightEditItem, False)
if valid:
self.ButtonBox.addButton(self.savebutton, QtGui.QDialogButtonBox.AcceptRole) # hide the save button tile screen is valid
else:
self.ButtonBox.removeButton(self.savebutton) # hide the save button tile screen is valid
def _color_widget(self, slot, invalid):
r = Qt.QPalette(slot.palette())
if invalid == True:
r.setColor(Qt.QPalette.Base, Qt.QColor('darkRed'))
else:
r.setColor(Qt.QPalette.Base, Qt.QColor('white'))
slot.setPalette(r)
slot.setAutoFillBackground(True)
def on_TitleEditItem_lostFocus(self):
self._validate_song()
def on_CopyrightEditItem_lostFocus(self):
self._validate_song()