2008-06-13 16:51:42 +02:00
#!/usr/bin/env python
#coding: utf-8
# Copyright 2008, Tiril Anette Langfeldt Rødland
# 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, either version 3 of the License, or (at your option) any later version. 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, see <http://www.gnu.org/licenses/>.
import os
import platform
import sys
import string
import datetime
import time
2008-06-14 19:48:48 +02:00
import random
2008-06-13 16:51:42 +02:00
from PyQt4 . QtCore import *
from PyQt4 . QtGui import *
from pysqlite2 import dbapi2 as sqlite
from qrc_resources import *
2008-06-19 16:25:51 +02:00
__version__ = " 1.0.1 "
2008-06-13 16:51:42 +02:00
main = None
2008-06-17 15:39:39 +02:00
semesters = [ ]
semester = None
2008-06-13 16:51:42 +02:00
courses = [ ]
coursesString = [ ]
books = [ ]
booksString = [ ]
2008-06-14 19:48:48 +02:00
colors = [ ]
2008-06-17 15:39:39 +02:00
termList = None
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
### The main window
2008-06-13 16:51:42 +02:00
class MainWindow ( QMainWindow ) :
2008-06-18 15:41:03 +02:00
# Initialize the main window
2008-06-13 16:51:42 +02:00
def __init__ ( self , parent = None ) :
super ( MainWindow , self ) . __init__ ( parent )
# The program name
self . title = " Egon "
2008-06-15 21:21:39 +02:00
# The days
self . days = [ self . trUtf8 ( " Monday " ) , self . trUtf8 ( " Tuesday " ) , self . trUtf8 ( " Wednesday " ) , self . trUtf8 ( " Thursday " ) , self . trUtf8 ( " Friday " ) ]
2008-06-13 16:51:42 +02:00
# The tabs
self . assignment = AssignmentTab ( )
self . reading = ReadingTab ( )
self . schedule = ScheduleTab ( )
# Set the tabs in the middle
self . tabs = QTabWidget ( )
self . tabs . addTab ( self . assignment , " &Assignments " )
self . tabs . addTab ( self . reading , " &Reading List " )
self . tabs . addTab ( self . schedule , " &Schedule " )
self . tabs . setTabShape ( QTabWidget . Rounded )
self . tabs . setTabPosition ( QTabWidget . North )
self . setCentralWidget ( self . tabs )
# The calendar
self . calendarFrame = QFrame ( )
self . calendarFrame . setFrameStyle ( QFrame . StyledPanel | QFrame . Sunken )
self . calendarDockWidget = QDockWidget ( " Calendar " , self )
self . calendarDockWidget . setVisible ( False )
self . calendarDockWidget . setObjectName ( " CalendarDockWidget " )
self . calendarDockWidget . setAllowedAreas ( Qt . LeftDockWidgetArea | Qt . RightDockWidgetArea | Qt . BottomDockWidgetArea )
self . calendar = QCalendarWidget ( )
2008-06-18 21:26:48 +02:00
self . calendar . setFirstDayOfWeek ( Qt . Monday )
2008-06-13 16:51:42 +02:00
self . calendarLayout = QVBoxLayout ( )
self . calendarLayout . addWidget ( self . calendar )
self . calendarFrame . setLayout ( self . calendarLayout )
self . calendarFrame . hide ( )
self . calendarDockWidget . setWidget ( self . calendarFrame )
2008-06-17 15:39:39 +02:00
# The semesters
global semester , semesters , termList
2008-06-18 21:26:48 +02:00
termList = QStringList ( )
termList . append ( self . trUtf8 ( " Spring " ) )
2008-06-17 15:39:39 +02:00
termList . append ( self . trUtf8 ( " Autumn " ) )
semesters = getSemestersFromDB ( )
semester = self . getLatestSemester ( semesters )
if semester :
self . load ( semester )
2008-06-13 16:51:42 +02:00
# Actions
2008-06-15 22:03:05 +02:00
fileNewAction = self . createAction ( " &New " , self . fileNew , QKeySequence . New , " filenew " , " Create a new semester plan " )
fileOpenAction = self . createAction ( " &Open " , self . fileOpen , QKeySequence . Open , " fileopen " , " Open an existing semester plan " )
2008-06-13 16:51:42 +02:00
fileQuitAction = self . createAction ( " &Quit " , self . close , " Ctrl+Q " , " filequit " , " Quit program " )
2008-06-13 20:53:23 +02:00
editAddCourse = self . createAction ( " Add &course " , self . addCourse , " Ctrl+C " , None , " Add a new course " )
2008-06-13 16:51:42 +02:00
editAddBook = self . createAction ( " Add &book " , self . addBook , " Ctrl+B " , None , " Add a new book " )
editAddAssignment = self . createAction ( " Add &assignment " , self . addAssignment , " Ctrl+A " , None , " Add a new assignment " )
editAddReading = self . createAction ( " Add &reading " , self . addReading , " Ctrl+R " , None , " Add a new reading " )
editAddLesson = self . createAction ( " Add &lesson " , self . addLesson , " Ctrl+L " , None , " Add a new lesson " )
2008-06-13 20:53:23 +02:00
editShowCalendar = self . createAction ( " Cal&endar " , self . showCalendar , " Ctrl+E " , None , " Show the calendar " )
2008-06-13 16:51:42 +02:00
helpAboutScheduler = self . createAction ( " &About %s " % self . title )
# Menus
self . fileMenu = self . menuBar ( ) . addMenu ( " &File " )
self . editMenu = self . menuBar ( ) . addMenu ( " &Edit " )
self . helpMenu = self . menuBar ( ) . addMenu ( " &Help " )
# Add actions to the menus
2008-06-18 15:41:03 +02:00
self . addActions ( self . fileMenu , ( fileNewAction , fileOpenAction , None , fileQuitAction ) )
2008-06-13 16:51:42 +02:00
self . addActions ( self . editMenu , ( editAddCourse , editAddBook , None , editAddAssignment , None , editAddReading , None , editAddLesson , None , editShowCalendar ) )
self . addActions ( self . helpMenu , ( helpAboutScheduler , None ) )
# Connect statements
self . connect ( editAddCourse , SIGNAL ( " triggered() " ) , self . addCourse )
self . connect ( editAddBook , SIGNAL ( " triggered() " ) , self . addBook )
self . connect ( editAddAssignment , SIGNAL ( " triggered() " ) , self . addAssignment )
self . connect ( editAddReading , SIGNAL ( " triggered() " ) , self . addReading )
self . connect ( editAddLesson , SIGNAL ( " triggered() " ) , self . addLesson )
self . connect ( editShowCalendar , SIGNAL ( " pressed() " ) , self . showCalendar )
self . connect ( helpAboutScheduler , SIGNAL ( " triggered() " ) , self . helpAbout )
self . connect ( self . assignment . addAssignmentButton , SIGNAL ( " pressed() " ) , self . addAssignment )
self . connect ( self . assignment . deleteAssignmentButton , SIGNAL ( " pressed() " ) , self . deleteAssignment )
self . connect ( self . assignment . completeAssignmentBox , SIGNAL ( " currentIndexChanged(QString) " ) , self . completeAssignment )
self . connect ( self . reading . addReadingButton , SIGNAL ( " pressed() " ) , self . addReading )
self . connect ( self . reading . deleteReadingButton , SIGNAL ( " pressed() " ) , self . deleteReading )
self . connect ( self . reading . readingDoneButton , SIGNAL ( " pressed() " ) , self . doneReading )
self . connect ( self . schedule . addScheduleButton , SIGNAL ( " pressed() " ) , self . addLesson )
2008-06-13 20:53:23 +02:00
self . connect ( self . schedule . deleteScheduleButton , SIGNAL ( " pressed() " ) , self . deleteLesson )
2008-06-13 16:51:42 +02:00
# The toolbars
fileToolbar = self . addToolBar ( " File " )
fileToolbar . setObjectName ( " FileToolBar " )
2008-06-18 15:41:03 +02:00
self . addActions ( fileToolbar , ( fileNewAction , fileOpenAction , None , fileQuitAction ) )
2008-06-13 16:51:42 +02:00
editToolbar = self . addToolBar ( " Edit " )
editToolbar . setObjectName ( " EditToolBar " )
self . addActions ( editToolbar , ( editAddCourse , editAddBook , None , editShowCalendar ) )
2008-06-17 17:03:11 +02:00
# Set the title
self . setMainWindowTitle ( )
2008-06-18 21:26:48 +02:00
# The courses
2008-06-13 16:51:42 +02:00
courses = getCourses ( )
makeCoursesString ( )
2008-06-18 15:41:03 +02:00
# This window
2008-06-13 16:51:42 +02:00
global main
main = self
2008-06-18 21:26:48 +02:00
2008-06-18 15:41:03 +02:00
# Semester
2008-06-18 21:26:48 +02:00
## Open the New dialog
def fileNew ( self ) :
self . nsdlg = NewSemesterDlg ( )
self . nsdlg . show ( )
## Open the Open dialog
def fileOpen ( self ) :
self . osdlg = OpenSemesterDlg ( )
self . osdlg . show ( )
2008-06-18 15:41:03 +02:00
## Return the latest semester
def getLatestSemester ( self , semesters ) :
if len ( semesters ) == 0 :
return None
global termList
max = semesters [ 0 ]
2008-06-18 21:26:48 +02:00
for s in semesters :
2008-06-18 15:41:03 +02:00
if s . getYear ( ) > max . getYear ( ) :
max = s
if s . getYear ( ) == max . getYear ( ) :
if s . getTerm ( ) == self . trUtf8 ( " Autumn " ) :
max = s
return max
# Return the list of terms
def getTermList ( self ) :
global termList
return termList
# Assignment
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
## Open the Add Assignment dialog
2008-06-13 16:51:42 +02:00
def addAssignment ( self ) :
2008-06-18 15:41:03 +02:00
self . adlg = AssignmentDlg ( )
self . adlg . show ( )
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
## Delete the assignment from the database and the table
2008-06-13 16:51:42 +02:00
def deleteAssignment ( self ) :
course , number = self . getCourseAndNumber ( )
table , row = self . getAssignmentTableAndRow ( )
2008-06-17 15:39:39 +02:00
global semester
2008-06-13 16:51:42 +02:00
removeAssignmentFromDB ( course , number )
table . removeRow ( row )
table . horizontalHeader ( ) . setResizeMode ( QHeaderView . ResizeToContents )
2008-06-18 15:41:03 +02:00
## Update the assignment's completion level to the specified level
2008-06-13 16:51:42 +02:00
def completeAssignment ( self , completionLevel ) :
course , number = self . getCourseAndNumber ( )
table , row = self . getAssignmentTableAndRow ( )
2008-06-18 21:26:48 +02:00
updateAssignmentCompletion ( course , number , completionLevel )
2008-06-13 16:51:42 +02:00
item = QTableWidgetItem ( QString ( completionLevel ) )
item . setBackground ( self . assignment . makeBrush ( completionLevel ) )
2008-06-18 21:26:48 +02:00
table . setItem ( row , 4 , item )
2008-06-13 16:51:42 +02:00
table . horizontalHeader ( ) . setResizeMode ( QHeaderView . ResizeToContents )
2008-06-18 15:41:03 +02:00
## Return the assignment table and its current row
2008-06-13 16:51:42 +02:00
def getAssignmentTableAndRow ( self ) :
table = self . assignment . assignmentTable
row = table . currentRow ( )
return table , row
2008-06-18 15:41:03 +02:00
## Return the course and number of the current assignment
2008-06-13 16:51:42 +02:00
def getCourseAndNumber ( self ) :
table , row = self . getAssignmentTableAndRow ( )
courseItem = table . item ( row , 1 )
numberItem = table . item ( row , 2 )
courseFull = courseItem . text ( )
number = numberItem . text ( )
2008-06-18 21:26:48 +02:00
course = getCourseCode ( courseFull )
2008-06-13 16:51:42 +02:00
return course , number
2008-06-18 15:41:03 +02:00
# Reading
## Open the Add Reading dialog
2008-06-13 16:51:42 +02:00
def addReading ( self ) :
2008-06-18 15:41:03 +02:00
self . rdlg = ReadingDlg ( )
self . rdlg . show ( )
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
## Delete the reading from the database and the table
2008-06-13 16:51:42 +02:00
def deleteReading ( self ) :
week , course , book = self . getWeekCourseAndBook ( )
table , row = self . getReadingTableAndRow ( )
2008-06-17 15:39:39 +02:00
global semester
2008-06-13 16:51:42 +02:00
removeReadingFromDB ( week , course , book )
table . removeRow ( row )
table . horizontalHeader ( ) . setResizeMode ( QHeaderView . ResizeToContents )
2008-06-18 15:41:03 +02:00
## Mark the reading as done
2008-06-13 16:51:42 +02:00
def doneReading ( self ) :
table , row = self . getReadingTableAndRow ( )
2008-06-13 20:53:23 +02:00
if table . item ( row , 6 ) . text ( ) . compare ( QString ( self . trUtf8 ( " Not done " ) ) ) == 0 :
done = True
item = QTableWidgetItem ( self . trUtf8 ( " Done " ) )
item . setBackground ( QBrush ( Qt . green , Qt . SolidPattern ) )
else :
done = False
item = QTableWidgetItem ( self . trUtf8 ( " Not done " ) )
item . setBackground ( QBrush ( Qt . red , Qt . SolidPattern ) )
2008-06-13 16:51:42 +02:00
table . setItem ( row , 6 , item )
2008-06-13 20:53:23 +02:00
table . horizontalHeader ( ) . setResizeMode ( QHeaderView . ResizeToContents )
2008-06-13 16:51:42 +02:00
week = ( table . item ( row , 0 ) . text ( ) . toInt ( ) ) [ 0 ]
2008-06-18 21:26:48 +02:00
courseFull = table . item ( row , 1 ) . text ( )
courseCode = QString ( getCourseCode ( courseFull ) )
2008-06-13 16:51:42 +02:00
bookTitle = table . item ( row , 2 ) . text ( )
2008-06-18 21:26:48 +02:00
book = getBookWithTitleFromDB ( bookTitle )
bookIsbn = book . getIsbn ( )
2008-06-13 16:51:42 +02:00
updateReadingDone ( week , courseCode , bookIsbn , True )
2008-06-18 15:41:03 +02:00
## Return the reading table and its current row
2008-06-13 16:51:42 +02:00
def getReadingTableAndRow ( self ) :
table = self . reading . readingTable
row = table . currentRow ( )
return table , row
2008-06-18 15:41:03 +02:00
## Return the week, course and book of the current reading
2008-06-13 16:51:42 +02:00
def getWeekCourseAndBook ( self ) :
table , row = self . getReadingTableAndRow ( )
weekItem = table . item ( row , 0 )
courseItem = table . item ( row , 1 )
bookItem = table . item ( row , 2 )
week = ( weekItem . text ( ) . toInt ( ) ) [ 0 ]
courseFull = courseItem . text ( )
courseCode = getCourseCode ( courseFull )
book = getBookWithTitleFromDB ( bookItem . text ( ) )
return week , courseCode , book . getIsbn ( )
2008-06-18 15:41:03 +02:00
# Schedule
## Open the Add Lesson dialog
2008-06-13 16:51:42 +02:00
def addLesson ( self ) :
2008-06-18 15:41:03 +02:00
self . sdlg = ScheduleDlg ( )
self . sdlg . show ( )
2008-06-13 20:53:23 +02:00
2008-06-18 15:41:03 +02:00
## Delete the lesson from the database and the table
2008-06-13 20:53:23 +02:00
def deleteLesson ( self ) :
2008-06-15 21:21:39 +02:00
table , row , column = self . getScheduleTableRowAndColumn ( )
day , course , time = self . getDayCourseAndTime ( )
2008-06-17 15:39:39 +02:00
global semester
2008-06-15 21:21:39 +02:00
removeLessonFromDB ( day , course , time )
table . setItem ( row , column , QTableWidgetItem ( ) )
2008-06-18 15:41:03 +02:00
## Return the schedule table and the current row and column
def getScheduleTableRowAndColumn ( self ) :
table = self . schedule . scheduleTable
row = table . currentRow ( )
column = table . currentColumn ( )
return table , row , column
## Return the day, course and time of the current lesson
2008-06-15 21:21:39 +02:00
def getDayCourseAndTime ( self ) :
table , row , column = self . getScheduleTableRowAndColumn ( )
item = table . item ( row , column )
text = item . text ( )
textlist = text . split ( ' \n ' )
courseFull = textlist [ 0 ]
coursecode = getCourseCode ( courseFull )
day = self . getDayFromTable ( column )
time = row + 8
return day , coursecode , time
2008-06-18 15:41:03 +02:00
## Return the day belonging to the specified column
2008-06-15 21:21:39 +02:00
def getDayFromTable ( self , column ) :
return self . days [ column ]
2008-06-13 20:53:23 +02:00
2008-06-18 15:41:03 +02:00
# Course
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
## Open the Add Course dialog
def addCourse ( self ) :
self . cdlg = CourseDlg ( )
self . cdlg . show ( )
# Book
## Open the Add Book dialog
def addBook ( self ) :
self . bdlg = BookDlg ( )
self . bdlg . show ( )
# Calendar
## Show the calendar
def showCalendar ( self ) :
if self . calendarDockWidget . isVisible ( ) :
self . removeDockWidget ( self . calendarDockWidget )
else :
self . addDockWidget ( Qt . BottomDockWidgetArea , self . calendarDockWidget )
self . calendarDockWidget . setVisible ( True )
# General
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
## Make and open the About dialog
2008-06-13 16:51:42 +02:00
def helpAbout ( self ) :
2008-06-13 20:53:23 +02:00
QMessageBox . about ( self , " About %s " % self . title , u """
2008-06-18 21:26:48 +02:00
< b > % s < / b > v % s
< p > Copyright & copy ; 2008 Tiril Anette Langfeldt Rødland . No rights reserved .
< p > You may modify and redistribute the program under the terms of the GPL . The license can be found here : http : / / www . gnu . org / licenses / gpl . html
2008-06-13 20:53:23 +02:00
< p > This application is mainly for use by students , and can be used to keep track of assignments , planned readings and the schedule .
< p > Python % s - Qt % s - PyQt % s on % s
2008-06-18 21:26:48 +02:00
< p > Developer : Tiril Anette Langfeldt Rødland , tirilane @pvv.ntnu.no
""" % (self.title, __version__, platform.python_version(), QT_VERSION_STR, PYQT_VERSION_STR, platform.system()))
2008-06-18 15:41:03 +02:00
## Updates the File menu
2008-06-13 16:51:42 +02:00
def updateFileMenu ( self ) :
self . fileMenu . clear ( )
self . addActions ( self . fileMenu , self . fileMenuActions [ : - 1 ] )
current = QString ( self . filename ) if self . filename is not None else None
2008-06-18 15:41:03 +02:00
## Create and return the action specified by the input
2008-06-13 16:51:42 +02:00
def createAction ( self , text , slot = None , shortcut = None , icon = None , tip = None , checkable = False , signal = " triggered() " ) :
action = QAction ( text , self )
if icon is not None :
iconfile = " :/ %s .png " % icon
action . setIcon ( QIcon ( iconfile ) )
if shortcut is not None :
action . setShortcut ( shortcut )
if tip is not None :
action . setToolTip ( tip )
action . setStatusTip ( tip )
if slot is not None :
self . connect ( action , SIGNAL ( signal ) , slot )
if checkable :
action . setCheckable ( True )
return action
2008-06-18 15:41:03 +02:00
## Add action to the target
def addActions ( self , target , actions ) :
for action in actions :
if action is None :
target . addSeparator ( )
else :
target . addAction ( action )
## Set the title on the main window, depending on the chosen semester
2008-06-17 17:03:11 +02:00
def setMainWindowTitle ( self ) :
global semester
if semester :
self . setWindowTitle ( " %s : %s %i " % ( self . title , semester . getTerm ( ) , semester . getYear ( ) ) )
else :
self . setWindowTitle ( self . title )
2008-06-18 15:41:03 +02:00
## Load the assignments, readings and schedule from the specified semester
2008-06-17 15:39:39 +02:00
def load ( self , semester ) :
self . assignment . updateTable ( semester )
self . reading . updateTable ( semester )
self . schedule . updateTable ( semester )
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
### The New Semester dialog
class NewSemesterDlg ( QDialog ) :
## Initialize the New Semester dialog
def __init__ ( self , parent = None ) :
super ( NewSemesterDlg , self ) . __init__ ( parent )
# Labels
self . termLabel = QLabel ( self . trUtf8 ( " Term " ) )
self . yearLabel = QLabel ( self . trUtf8 ( " Year " ) )
2008-06-18 21:26:48 +02:00
# Widgets
self . termEdit = QComboBox ( )
2008-06-18 15:41:03 +02:00
self . yearEdit = QSpinBox ( )
self . termEdit . addItems ( getMain ( ) . getTermList ( ) )
self . yearEdit . setRange ( 2000 , 2050 )
self . yearEdit . setSingleStep ( 1 )
self . buttonBox = QDialogButtonBox ( QDialogButtonBox . Ok | QDialogButtonBox . Cancel )
2008-06-18 21:26:48 +02:00
# Layout
2008-06-18 15:41:03 +02:00
layout = QGridLayout ( )
layout . addWidget ( self . termLabel , 0 , 0 )
layout . addWidget ( self . termEdit , 0 , 1 )
layout . addWidget ( self . yearLabel , 1 , 0 )
layout . addWidget ( self . yearEdit , 1 , 1 )
layout . addWidget ( self . buttonBox , 2 , 1 )
self . setLayout ( layout )
# Connect statements
self . connect ( self . buttonBox , SIGNAL ( " accepted() " ) , self , SLOT ( " accept() " ) )
self . connect ( self . buttonBox , SIGNAL ( " rejected() " ) , self , SLOT ( " reject() " ) )
# Set the title
self . setWindowTitle ( self . trUtf8 ( " New semester " ) )
2008-06-18 21:26:48 +02:00
2008-06-18 15:41:03 +02:00
## Accept the dialog and add the specified semester
def accept ( self ) :
term = unicode ( self . termEdit . currentText ( ) )
year = self . yearEdit . value ( )
2008-06-18 21:26:48 +02:00
global semester
semester = SemesterModel ( term , year )
addNewSemesterToDB ( term , year )
2008-06-18 15:41:03 +02:00
getMain ( ) . load ( semester )
getMain ( ) . setMainWindowTitle ( )
self . close ( )
### The Open Semester dialog
class OpenSemesterDlg ( QDialog ) :
## Initialize the Open Semester dialog
def __init__ ( self , parent = None ) :
super ( OpenSemesterDlg , self ) . __init__ ( parent )
# Widgets
self . semesterList = QListWidget ( )
semesters = getSemestersFromDB ( )
semesterlist = QStringList ( )
for semester in semesters :
semesterlist . append ( " %s %i " % ( semester . getTerm ( ) , semester . getYear ( ) ) )
self . semesterList . addItems ( semesterlist )
self . buttonBox = QDialogButtonBox ( QDialogButtonBox . Ok | QDialogButtonBox . Cancel )
# Layout
layout = QVBoxLayout ( )
layout . addWidget ( self . semesterList )
layout . addStretch ( )
2008-06-18 21:26:48 +02:00
layout . addWidget ( self . buttonBox )
2008-06-18 15:41:03 +02:00
self . setLayout ( layout )
# Connect statements
self . connect ( self . buttonBox , SIGNAL ( " accepted() " ) , self , SLOT ( " accept() " ) )
self . connect ( self . buttonBox , SIGNAL ( " rejected() " ) , self , SLOT ( " reject() " ) )
# Set the title
self . setWindowTitle ( self . trUtf8 ( " Open semester " ) )
2008-06-18 21:26:48 +02:00
2008-06-18 15:41:03 +02:00
## Accept the dialog and open the specified semester
def accept ( self ) :
text = self . semesterList . currentItem ( ) . text ( )
textlist = text . split ( ' ' )
term = textlist [ 0 ]
year = textlist [ 1 ] . toInt ( ) [ 0 ]
global semester
semester = SemesterModel ( term , year )
semester . setAssignments ( getAssignmentsFromDB ( semester ) )
semester . setReadings ( getReadingsFromDB ( semester ) )
semester . setLessons ( getLessonsFromDB ( semester ) )
getMain ( ) . load ( semester )
getMain ( ) . setMainWindowTitle ( )
self . close ( )
### The semester model
class SemesterModel ( ) :
term = " "
year = 0
assignments = [ ]
readings = [ ]
lessons = [ ]
## Initialize the semester model
def __init__ ( self , term , year ) :
self . term = term
self . year = year
## Return the term of the semester
def getTerm ( self ) :
return self . term
## Return the year of the semester
def getYear ( self ) :
return self . year
## Add an assignment to the semester's list of assignments
def addAssignment ( self , assignment ) :
self . assignments . append ( assignment )
## Set the list of assignments
def setAssignments ( self , assignments ) :
self . assignments = assignments
## Return the list of assignments
def getAssignments ( self ) :
return self . assignments
## Add a reading to the semester's list of readings
def addReading ( self , reading ) :
self . readings . append ( reading )
## Set the list of readings
def setReadings ( self , readings ) :
self . readings = readings
## Return the list of readings
def getReadings ( self ) :
return self . readings
## Add a lesson to the semester's list of lessons
def addLesson ( self , lesson ) :
self . lessons . append ( lesson )
## Set the list of lessons
def setLessons ( self , lessons ) :
self . lessons = lessons
## Return the list of lessons
def getLessons ( self ) :
return self . lessons
### The contents of the Assignment tab
2008-06-13 16:51:42 +02:00
class AssignmentTab ( QWidget ) :
2008-06-18 15:41:03 +02:00
## Initialize the Assignment tab
2008-06-13 16:51:42 +02:00
def __init__ ( self , parent = None ) :
super ( AssignmentTab , self ) . __init__ ( parent )
2008-06-18 15:41:03 +02:00
# Widgets
2008-06-13 16:51:42 +02:00
self . addAssignmentButton = QPushButton ( " Add assignment " )
2008-06-18 21:26:48 +02:00
self . completeAssignmentBox = QComboBox ( )
2008-06-13 16:51:42 +02:00
self . deleteAssignmentButton = QPushButton ( " Delete assignment " )
completeTypes = [ self . trUtf8 ( " Not available " ) , self . trUtf8 ( " Available " ) , self . trUtf8 ( " Begun " ) , self . trUtf8 ( " Finished " ) , self . trUtf8 ( " Delivered " ) , self . trUtf8 ( " Approved " ) , self . trUtf8 ( " Not approved " ) ]
self . completeAssignmentBox . addItems ( completeTypes )
2008-06-18 15:41:03 +02:00
# Make the table
2008-06-18 21:26:48 +02:00
self . makeTable ( )
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
# Layout
2008-06-17 15:39:39 +02:00
self . vlayout = QVBoxLayout ( )
self . hlayout = QHBoxLayout ( )
self . hlayout . addWidget ( self . addAssignmentButton )
self . hlayout . addWidget ( self . deleteAssignmentButton )
self . hlayout . addWidget ( self . completeAssignmentBox )
self . hlayout . addStretch ( )
self . vlayout . addWidget ( self . assignmentTable )
self . vlayout . addLayout ( self . hlayout )
self . setLayout ( self . vlayout )
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
## Make an empty assignment table
2008-06-13 16:51:42 +02:00
def makeTable ( self , current = None ) :
2008-06-17 15:39:39 +02:00
self . assignmentTable = QTableWidget ( 0 , 5 , self )
2008-06-13 16:51:42 +02:00
self . assignmentTable . clear ( )
2008-06-18 15:41:03 +02:00
2008-06-18 21:26:48 +02:00
self . assignmentHeaderList = QStringList ( )
self . assignmentHeaderList . append ( self . trUtf8 ( " Date " ) )
self . assignmentHeaderList . append ( self . trUtf8 ( " Course " ) )
self . assignmentHeaderList . append ( self . trUtf8 ( " Number " ) )
self . assignmentHeaderList . append ( self . trUtf8 ( " Description " ) )
self . assignmentHeaderList . append ( self . trUtf8 ( " Complete " ) )
self . assignmentTable . setHorizontalHeaderLabels ( self . assignmentHeaderList )
2008-06-13 16:51:42 +02:00
self . assignmentTable . setAlternatingRowColors ( True )
self . assignmentTable . setEditTriggers ( QAbstractItemView . NoEditTriggers )
self . assignmentTable . setSelectionBehavior ( QAbstractItemView . SelectRows )
self . assignmentTable . setSelectionMode ( QAbstractItemView . SingleSelection )
2008-06-18 15:41:03 +02:00
2008-06-18 21:26:48 +02:00
selected = None
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
## Add the assignments of the semester to the table
2008-06-17 15:39:39 +02:00
def updateTable ( self , semester , current = None ) :
self . assignments = getAssignmentsFromDB ( semester )
rows = len ( self . assignments )
2008-06-18 21:26:48 +02:00
self . assignmentTable . setRowCount ( rows )
for row in range ( rows ) :
2008-06-13 16:51:42 +02:00
self . addAssignmentToTable ( row )
2008-06-18 15:41:03 +02:00
2008-06-18 21:26:48 +02:00
self . assignmentTable . horizontalHeader ( ) . setResizeMode ( QHeaderView . ResizeToContents )
2008-06-13 16:51:42 +02:00
self . assignmentTable . sortItems ( 0 , Qt . AscendingOrder )
2008-06-18 15:41:03 +02:00
## Add new assignment to Table
2008-06-13 16:51:42 +02:00
def addAssignmentToTable ( self , row , assignment = None ) :
if assignment == None :
assignment = self . assignments [ row ]
2008-06-18 15:41:03 +02:00
2008-06-18 21:26:48 +02:00
complete = QString ( assignment . getComplete ( ) )
2008-06-13 16:51:42 +02:00
brush = self . makeBrush ( complete )
2008-06-18 15:41:03 +02:00
2008-06-18 21:26:48 +02:00
self . assignmentTable . setItem ( row , 0 , QTableWidgetItem ( QString ( assignment . getDate ( ) . toString ( " yyyy-MM-dd hh:mm, ddd " ) ) ) )
2008-06-13 16:51:42 +02:00
self . assignmentTable . setItem ( row , 1 , QTableWidgetItem ( QString ( assignment . getCourse ( ) . getFull ( ) ) ) )
self . assignmentTable . setItem ( row , 2 , QTableWidgetItem ( QString ( " %i " % assignment . getNumber ( ) ) ) )
self . assignmentTable . setItem ( row , 3 , QTableWidgetItem ( QString ( " %s " % assignment . getDescription ( ) ) ) )
2008-06-18 15:41:03 +02:00
2008-06-18 21:26:48 +02:00
completeItem = QTableWidgetItem ( complete )
2008-06-13 16:51:42 +02:00
completeItem . setBackground ( brush )
self . assignmentTable . setItem ( row , 4 , completeItem )
2008-06-18 15:41:03 +02:00
## Set the right brush and color for the assignment, depending on level of completion
2008-06-13 16:51:42 +02:00
def makeBrush ( self , complete ) :
brush = QBrush ( Qt . NoBrush )
if complete . compare ( QString ( self . trUtf8 ( " Available " ) ) ) == 0 :
brush . setStyle ( Qt . Dense7Pattern )
brush . setColor ( QColor ( Qt . cyan ) )
elif complete . compare ( QString ( self . trUtf8 ( " Begun " ) ) ) == 0 :
brush . setStyle ( Qt . Dense5Pattern )
brush . setColor ( QColor ( Qt . cyan ) )
elif complete . compare ( QString ( self . trUtf8 ( " Finished " ) ) ) == 0 :
brush . setStyle ( Qt . Dense3Pattern )
brush . setColor ( QColor ( Qt . cyan ) )
elif complete . compare ( QString ( self . trUtf8 ( " Delivered " ) ) ) == 0 :
brush . setStyle ( Qt . Dense1Pattern )
brush . setColor ( QColor ( Qt . cyan ) )
elif complete . compare ( QString ( self . trUtf8 ( " Approved " ) ) ) == 0 :
brush . setStyle ( Qt . SolidPattern )
brush . setColor ( QColor ( Qt . green ) )
elif complete . compare ( QString ( self . trUtf8 ( " Not approved " ) ) ) == 0 :
brush . setStyle ( Qt . SolidPattern )
brush . setColor ( QColor ( Qt . red ) )
else :
brush . setStyle ( Qt . NoBrush )
2008-06-18 21:26:48 +02:00
return brush
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
### The Add Assignment dialog
2008-06-13 16:51:42 +02:00
class AssignmentDlg ( QDialog ) :
2008-06-18 15:41:03 +02:00
## Initialize the Add Assignment dialog
2008-06-13 16:51:42 +02:00
def __init__ ( self , parent = None ) :
super ( AssignmentDlg , self ) . __init__ ( parent )
2008-06-18 21:26:48 +02:00
# Labels
2008-06-13 16:51:42 +02:00
self . dateLabel = QLabel ( self . trUtf8 ( " &Date " ) )
self . courseLabel = QLabel ( self . trUtf8 ( " &Course " ) )
2008-06-18 21:26:48 +02:00
self . numberLabel = QLabel ( self . trUtf8 ( " &Number " ) )
2008-06-13 16:51:42 +02:00
self . descriptionLabel = QLabel ( self . trUtf8 ( " De&scription " ) )
self . completeLabel = QLabel ( self . trUtf8 ( " &Complete " ) )
2008-06-18 15:41:03 +02:00
# Widgets
2008-06-18 21:26:48 +02:00
self . dateEdit = QLineEdit ( " DD.MM.YYYY HH:MM " )
self . dateEdit . setSelection ( 0 , 16 )
2008-06-13 16:51:42 +02:00
self . courseEdit = QComboBox ( )
2008-06-18 21:26:48 +02:00
self . courseEdit . addItems ( makeCoursesString ( ) )
self . numberEdit = QSpinBox ( )
self . numberEdit . setRange ( 1 , 20 )
2008-06-13 16:51:42 +02:00
self . descriptionEdit = QLineEdit ( )
self . completeEdit = QComboBox ( )
2008-06-18 21:26:48 +02:00
completeTypes = [ self . trUtf8 ( " Not available " ) , self . trUtf8 ( " Available " ) , self . trUtf8 ( " Begun " ) , self . trUtf8 ( " Finished " ) , self . trUtf8 ( " Delivered " ) , self . trUtf8 ( " Approved " ) , self . trUtf8 ( " Not approved " ) ]
self . completeEdit . addItems ( completeTypes )
2008-06-18 15:41:03 +02:00
self . buttonBox = QDialogButtonBox ( QDialogButtonBox . Ok | QDialogButtonBox . Cancel )
2008-06-13 16:51:42 +02:00
2008-06-18 21:26:48 +02:00
# Buddies
2008-06-13 16:51:42 +02:00
self . dateLabel . setBuddy ( self . dateEdit )
self . courseLabel . setBuddy ( self . courseEdit )
2008-06-18 21:26:48 +02:00
self . numberLabel . setBuddy ( self . numberEdit )
2008-06-13 16:51:42 +02:00
self . descriptionLabel . setBuddy ( self . descriptionEdit )
self . completeLabel . setBuddy ( self . completeEdit )
2008-06-18 21:26:48 +02:00
# Layout
2008-06-13 16:51:42 +02:00
self . layout = QGridLayout ( )
self . layout . addWidget ( self . dateLabel , 0 , 0 )
self . layout . addWidget ( self . courseLabel , 1 , 0 )
2008-06-18 21:26:48 +02:00
self . layout . addWidget ( self . numberLabel , 2 , 0 )
2008-06-13 16:51:42 +02:00
self . layout . addWidget ( self . descriptionLabel , 3 , 0 )
self . layout . addWidget ( self . completeLabel , 4 , 0 )
self . layout . addWidget ( self . dateEdit , 0 , 1 )
self . layout . addWidget ( self . courseEdit , 1 , 1 )
2008-06-18 21:26:48 +02:00
self . layout . addWidget ( self . numberEdit , 2 , 1 )
2008-06-13 16:51:42 +02:00
self . layout . addWidget ( self . descriptionEdit , 3 , 1 )
self . layout . addWidget ( self . completeEdit , 4 , 1 )
2008-06-18 15:41:03 +02:00
self . layout . addWidget ( self . buttonBox , 5 , 0 , 1 , 2 )
2008-06-13 16:51:42 +02:00
self . setLayout ( self . layout )
2008-06-18 21:26:48 +02:00
# Connect statements
2008-06-18 15:41:03 +02:00
self . connect ( self . buttonBox , SIGNAL ( " accepted() " ) , self , SLOT ( " accept() " ) )
2008-06-18 21:26:48 +02:00
self . connect ( self . buttonBox , SIGNAL ( " rejected() " ) , self , SLOT ( " reject() " ) )
2008-06-18 15:41:03 +02:00
2008-06-18 21:26:48 +02:00
# Set the title
2008-06-18 15:41:03 +02:00
self . setWindowTitle ( self . trUtf8 ( " Add new assignment " ) )
## Return an array with the values of the widgets
2008-06-13 16:51:42 +02:00
def getValues ( self ) :
assignment = [ ]
assignment . append ( unicode ( self . dateEdit . text ( ) ) )
assignment . append ( unicode ( self . courseEdit . currentText ( ) ) )
assignment . append ( self . numberEdit . value ( ) )
assignment . append ( unicode ( self . descriptionEdit . text ( ) ) )
assignment . append ( unicode ( self . completeEdit . currentText ( ) ) )
return assignment
2008-06-18 15:41:03 +02:00
## Accept the dialog and add the specified assignment
2008-06-13 16:51:42 +02:00
def accept ( self ) :
assignmentList = self . getValues ( )
dateString = assignmentList [ 0 ]
courseFull = assignmentList [ 1 ]
number = assignmentList [ 2 ]
description = assignmentList [ 3 ]
complete = assignmentList [ 4 ]
2008-06-18 21:26:48 +02:00
regex = QRegExp ( r " [01-31].[01-12].[2000-2050] [00-23]:[00-60] " )
validator = QRegExpValidator ( regex , self )
valid = validator . validate ( dateString , 16 )
if valid == QValidator . Invalid :
regexMessage = QErrorMessage ( )
regexMessage . showMessage ( QString ( self . trUtf8 ( " The date is not in a correct format. " ) ) )
2008-06-13 16:51:42 +02:00
if len ( dateString ) < = 11 :
dateList = dateString . split ( ' . ' )
timeList = [ ' 00 ' , ' 00 ' ]
else :
dateTime = dateString . split ( )
dateList = dateTime [ 0 ] . split ( ' . ' )
timeList = dateTime [ 1 ] . split ( ' : ' )
2008-06-18 21:26:48 +02:00
print dateList , timeList
if dateList [ 1 ] > ' 13 ' :
dateMessage = QErrorMessage ( )
dateMessage . showMessage ( QString ( self . trUtf8 ( " The month is not valid. Please enter a valid date. " ) ) )
print dateList [ 1 ]
elif dateList [ 0 ] > ' 31 ' :
dateMessage = QErrorMessage ( )
dateMessage . showMessage ( QString ( self . trUtf8 ( " The day is not valid. Please enter a valid date. " ) ) )
print dateList [ 0 ]
elif timeList [ 0 ] > ' 23 ' :
dateMessage = QErrorMessage ( )
dateMessage . showMessage ( QString ( self . trUtf8 ( " The hour is not valid. Please enter a valid time. " ) ) )
print timeList [ 0 ]
elif timeList [ 1 ] > ' 59 ' :
dateMessage = QErrorMessage ( )
dateMessage . showMessage ( QString ( self . trUtf8 ( " The minutes are not valid. Please enter a valid time. " ) ) )
print timeList [ 1 ]
else :
try :
date = QDate ( string . atoi ( dateList [ 2 ] ) , string . atoi ( dateList [ 1 ] ) , string . atoi ( dateList [ 0 ] ) )
time = QTime ( string . atoi ( timeList [ 0 ] ) , string . atoi ( timeList [ 1 ] ) )
datetime = QDateTime ( date , time )
except ValueError , e :
valueMessage = QErrorMessage ( )
valueMessage . showMessage ( QString ( self . trUtf8 ( " The date is not valid. Please enter a date on the DD.MM.YYYY HH:MM form. " ) ) )
course = getCourseFromDB ( getCourseCode ( courseFull ) )
try :
global semester
addNewAssignmentToDB ( semester , datetime , course , number , description , complete )
assignment = AssignmentModel ( datetime , course , number , description , complete )
except UnboundLocalError , e :
pass
table = getMain ( ) . assignment . assignmentTable
row = table . rowCount ( )
table . insertRow ( row )
getMain ( ) . assignment . addAssignmentToTable ( row , assignment )
table . sortItems ( 0 , Qt . AscendingOrder )
2008-06-13 16:51:42 +02:00
self . close ( )
2008-06-18 15:41:03 +02:00
### The assignment model
2008-06-13 16:51:42 +02:00
class AssignmentModel ( ) :
date = None
course = None
number = 0
description = " "
complete = " "
2008-06-18 15:41:03 +02:00
## Initialize the assignment model
2008-06-13 16:51:42 +02:00
def __init__ ( self , date , course , number , description , complete ) :
self . date = date
self . course = course
self . number = number
self . description = description
self . complete = complete
2008-06-18 15:41:03 +02:00
## Return the date the assignment is due
2008-06-13 16:51:42 +02:00
def getDate ( self ) :
return self . date
2008-06-18 15:41:03 +02:00
## Return the course the assignment is given in
2008-06-13 16:51:42 +02:00
def getCourse ( self ) :
return self . course
2008-06-18 15:41:03 +02:00
## Return the number of the assignment
2008-06-13 16:51:42 +02:00
def getNumber ( self ) :
return self . number
2008-06-18 15:41:03 +02:00
## Return the description of the assignment
2008-06-13 16:51:42 +02:00
def getDescription ( self ) :
return self . description
2008-06-18 15:41:03 +02:00
## Return the level of completion for the assignment
2008-06-13 16:51:42 +02:00
def getComplete ( self ) :
return self . complete
2008-06-18 15:41:03 +02:00
### The contents of the Reading tab
2008-06-13 16:51:42 +02:00
class ReadingTab ( QWidget ) :
2008-06-18 15:41:03 +02:00
## Initialize the Reading tab
2008-06-13 16:51:42 +02:00
def __init__ ( self , parent = None ) :
super ( ReadingTab , self ) . __init__ ( parent )
2008-06-18 15:41:03 +02:00
# Widgets
2008-06-13 16:51:42 +02:00
self . addReadingButton = QPushButton ( self . trUtf8 ( " Add pages to read " ) )
self . deleteReadingButton = QPushButton ( self . trUtf8 ( " Delete pages " ) )
self . readingDoneButton = QPushButton ( self . trUtf8 ( " Done " ) )
2008-06-18 21:26:48 +02:00
# Make the table
2008-06-13 16:51:42 +02:00
self . makeTable ( )
2008-06-18 15:41:03 +02:00
# Layout
2008-06-17 15:39:39 +02:00
self . vlayout = QVBoxLayout ( )
self . hlayout = QHBoxLayout ( )
self . hlayout . addWidget ( self . addReadingButton )
self . hlayout . addWidget ( self . deleteReadingButton )
self . hlayout . addWidget ( self . readingDoneButton )
self . hlayout . addStretch ( )
self . vlayout . addWidget ( self . readingTable )
self . vlayout . addLayout ( self . hlayout )
self . setLayout ( self . vlayout )
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
## Make an empty reading table
2008-06-13 16:51:42 +02:00
def makeTable ( self , current = None ) :
2008-06-17 15:39:39 +02:00
self . readingTable = QTableWidget ( 0 , 7 , self )
2008-06-13 16:51:42 +02:00
self . readingTable . clear ( )
2008-06-18 21:26:48 +02:00
self . readingHeaderList = QStringList ( )
self . readingHeaderList . append ( self . trUtf8 ( " Week " ) )
self . readingHeaderList . append ( self . trUtf8 ( " Course " ) )
self . readingHeaderList . append ( self . trUtf8 ( " Book " ) )
self . readingHeaderList . append ( self . trUtf8 ( " Chapter " ) )
self . readingHeaderList . append ( self . trUtf8 ( " Pages " ) )
self . readingHeaderList . append ( self . trUtf8 ( " Number of pages " ) )
2008-06-13 16:51:42 +02:00
self . readingHeaderList . append ( self . trUtf8 ( " Done " ) )
2008-06-18 21:26:48 +02:00
self . readingTable . setHorizontalHeaderLabels ( self . readingHeaderList )
2008-06-13 16:51:42 +02:00
self . readingTable . setAlternatingRowColors ( True )
self . readingTable . setEditTriggers ( QAbstractItemView . NoEditTriggers )
self . readingTable . setSelectionBehavior ( QAbstractItemView . SelectRows )
self . readingTable . setSelectionMode ( QAbstractItemView . SingleSelection )
selected = None
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
## Add the readings of the semester to the table
2008-06-17 15:39:39 +02:00
def updateTable ( self , semester ) :
self . readings = getReadingsFromDB ( semester )
rows = len ( self . readings )
self . readingTable . setRowCount ( rows )
for row in range ( rows ) :
2008-06-13 16:51:42 +02:00
self . addReadingToTable ( row )
self . readingTable . horizontalHeader ( ) . setResizeMode ( QHeaderView . ResizeToContents )
self . readingTable . sortItems ( 0 , Qt . AscendingOrder )
2008-06-18 15:41:03 +02:00
## Add a new reading to the table
2008-06-13 16:51:42 +02:00
def addReadingToTable ( self , row , reading = None ) :
if reading == None :
reading = self . readings [ row ]
brush = QBrush ( Qt . NoBrush )
2008-06-13 20:53:23 +02:00
brush . setStyle ( Qt . SolidPattern )
2008-06-13 16:51:42 +02:00
if reading . getDone ( ) :
doneString = self . trUtf8 ( " Done " )
2008-06-18 21:26:48 +02:00
brush . setColor ( Qt . green )
2008-06-13 16:51:42 +02:00
else :
2008-06-13 20:53:23 +02:00
doneString = self . trUtf8 ( " Not done " )
brush . setColor ( Qt . red )
2008-06-13 16:51:42 +02:00
2008-06-13 20:53:23 +02:00
self . readingTable . setItem ( row , 0 , QTableWidgetItem ( QString ( " %02s " % reading . getWeek ( ) ) ) )
2008-06-13 16:51:42 +02:00
self . readingTable . setItem ( row , 1 , QTableWidgetItem ( QString ( reading . getCourse ( ) . getFull ( ) ) ) )
self . readingTable . setItem ( row , 2 , QTableWidgetItem ( QString ( reading . getBook ( ) . getTitle ( ) ) ) )
2008-06-18 21:26:48 +02:00
self . readingTable . setItem ( row , 3 , QTableWidgetItem ( QString ( reading . getChapter ( ) ) ) )
2008-06-13 16:51:42 +02:00
self . readingTable . setItem ( row , 4 , QTableWidgetItem ( QString ( reading . getPages ( ) ) ) )
2008-06-18 21:26:48 +02:00
self . readingTable . setItem ( row , 5 , QTableWidgetItem ( QString ( " %i " % reading . getNumberOfPages ( ) ) ) )
2008-06-18 15:41:03 +02:00
2008-06-18 21:26:48 +02:00
item = QTableWidgetItem ( QString ( doneString ) )
item . setBackground ( brush )
2008-06-13 16:51:42 +02:00
self . readingTable . setItem ( row , 6 , item )
2008-06-18 15:41:03 +02:00
### The Add Reading dialog
2008-06-13 16:51:42 +02:00
class ReadingDlg ( QDialog ) :
2008-06-18 15:41:03 +02:00
## Initialize the Add Reading dialog
2008-06-13 16:51:42 +02:00
def __init__ ( self , parent = None ) :
super ( ReadingDlg , self ) . __init__ ( parent )
2008-06-18 21:26:48 +02:00
# Labels
2008-06-13 16:51:42 +02:00
self . weekLabel = QLabel ( self . trUtf8 ( " &Week " ) )
self . courseLabel = QLabel ( self . trUtf8 ( " &Course " ) )
self . bookLabel = QLabel ( self . trUtf8 ( " &Book " ) )
self . chapterLabel = QLabel ( self . trUtf8 ( " Cha&pter " ) )
self . pagesLabel = QLabel ( self . trUtf8 ( " &Pages " ) )
2008-06-18 21:26:48 +02:00
# Widgets
2008-06-13 16:51:42 +02:00
self . weekEdit = QSpinBox ( )
self . weekEdit . setRange ( 1 , 52 )
self . courseEdit = QComboBox ( )
coursesStringList = QStringList ( )
courses = makeCoursesString ( )
for course in courses :
coursesStringList . append ( course )
self . courseEdit . addItems ( coursesStringList )
self . bookEdit = QComboBox ( )
booksStringList = QStringList ( )
books = makeBooksString ( )
for book in books :
booksStringList . append ( book )
2008-06-18 21:26:48 +02:00
self . bookEdit . addItems ( booksStringList )
2008-06-13 16:51:42 +02:00
self . chapterEdit = QLineEdit ( )
self . pagesEdit = QLineEdit ( )
2008-06-18 15:41:03 +02:00
self . buttonBox = QDialogButtonBox ( QDialogButtonBox . Ok | QDialogButtonBox . Cancel )
2008-06-13 16:51:42 +02:00
2008-06-18 21:26:48 +02:00
# Buddies
2008-06-13 16:51:42 +02:00
self . weekLabel . setBuddy ( self . weekEdit )
self . courseLabel . setBuddy ( self . courseEdit )
self . bookLabel . setBuddy ( self . bookEdit )
self . chapterLabel . setBuddy ( self . chapterEdit )
self . pagesLabel . setBuddy ( self . pagesEdit )
2008-06-18 21:26:48 +02:00
# Layout
2008-06-13 16:51:42 +02:00
self . layout = QGridLayout ( )
self . layout . addWidget ( self . weekLabel , 0 , 0 )
self . layout . addWidget ( self . courseLabel , 1 , 0 )
self . layout . addWidget ( self . bookLabel , 2 , 0 )
self . layout . addWidget ( self . chapterLabel , 3 , 0 )
self . layout . addWidget ( self . pagesLabel , 4 , 0 )
self . layout . addWidget ( self . weekEdit , 0 , 1 )
self . layout . addWidget ( self . courseEdit , 1 , 1 )
self . layout . addWidget ( self . bookEdit , 2 , 1 )
self . layout . addWidget ( self . chapterEdit , 3 , 1 )
self . layout . addWidget ( self . pagesEdit , 4 , 1 )
2008-06-18 15:41:03 +02:00
self . layout . addWidget ( self . buttonBox , 5 , 0 , 1 , 2 )
2008-06-13 16:51:42 +02:00
self . setLayout ( self . layout )
2008-06-18 15:41:03 +02:00
# Connect statements
self . connect ( self . buttonBox , SIGNAL ( " accepted() " ) , self , SLOT ( " accept() " ) )
self . connect ( self . buttonBox , SIGNAL ( " rejected() " ) , self , SLOT ( " reject() " ) )
# Set the title
2008-06-13 16:51:42 +02:00
self . setWindowTitle ( self . trUtf8 ( " Add new reading " ) )
2008-06-18 15:41:03 +02:00
## Accept the dialog and add the specified reading
2008-06-13 16:51:42 +02:00
def accept ( self ) :
week = unicode ( self . weekEdit . value ( ) )
courseFull = unicode ( self . courseEdit . currentText ( ) )
bookTitle = unicode ( self . bookEdit . currentText ( ) )
chapter = unicode ( self . chapterEdit . text ( ) )
pages = unicode ( self . pagesEdit . text ( ) )
2008-06-18 21:26:48 +02:00
self . close ( )
2008-06-13 16:51:42 +02:00
course = getCourseFromDB ( getCourseCode ( courseFull ) )
2008-06-18 21:26:48 +02:00
book = getBookWithTitleFromDB ( bookTitle )
2008-06-13 16:51:42 +02:00
2008-06-17 15:39:39 +02:00
global semester
2008-06-18 21:26:48 +02:00
addNewReadingToDB ( semester , week , course , book , chapter , pages , False )
2008-06-13 16:51:42 +02:00
reading = ReadingModel ( week , course , book , chapter , pages , False )
table = getMain ( ) . reading . readingTable
row = table . rowCount ( )
2008-06-18 21:26:48 +02:00
table . insertRow ( row )
getMain ( ) . reading . addReadingToTable ( row , reading )
table . sortItems ( 0 , Qt . AscendingOrder )
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
### The reading model
2008-06-13 16:51:42 +02:00
class ReadingModel ( ) :
week = 0
course = None
book = None
chapter = " "
pages = " "
numberOfPages = 0
done = False
2008-06-18 15:41:03 +02:00
## Initialize the reading model
2008-06-13 16:51:42 +02:00
def __init__ ( self , week , course , book , chapter , pages , done = False ) :
self . week = week
self . course = course
self . book = book
self . chapter = chapter
self . pages = pages
self . numberOfPages = self . getNumberOfPages ( )
self . done = done
2008-06-18 15:41:03 +02:00
## Return the week the reading shall be done
2008-06-13 16:51:42 +02:00
def getWeek ( self ) :
return self . week
2008-06-18 15:41:03 +02:00
## Return the course the reading is in
2008-06-13 16:51:42 +02:00
def getCourse ( self ) :
return self . course
2008-06-18 15:41:03 +02:00
## Return the book to be read in
2008-06-13 16:51:42 +02:00
def getBook ( self ) :
return self . book
2008-06-18 15:41:03 +02:00
## Return the chapter to be read
2008-06-13 16:51:42 +02:00
def getChapter ( self ) :
return self . chapter
2008-06-18 15:41:03 +02:00
## Return the pages to be read
2008-06-13 16:51:42 +02:00
def getPages ( self ) :
return self . pages
2008-06-18 15:41:03 +02:00
## Return the number of pages to be read
2008-06-13 16:51:42 +02:00
def getNumberOfPages ( self ) :
pages = self . getPages ( )
pagesArray = pages . split ( " , " )
nextArray = [ ]
sum = 0
for p in pagesArray :
p . strip ( )
nextArray . append ( p . split ( " - " ) )
for n in nextArray :
sum + = int ( n [ 1 ] ) - int ( n [ 0 ] )
return sum
2008-06-18 15:41:03 +02:00
## Return whether the reading has been done or not
2008-06-13 16:51:42 +02:00
def getDone ( self ) :
return self . done
2008-06-18 15:41:03 +02:00
### The contents of the Schedule tab
2008-06-13 16:51:42 +02:00
class ScheduleTab ( QWidget ) :
2008-06-18 15:41:03 +02:00
## Initialize the Schedule tab
2008-06-13 16:51:42 +02:00
def __init__ ( self , parent = None ) :
super ( ScheduleTab , self ) . __init__ ( parent )
2008-06-18 21:26:48 +02:00
# Widgets
2008-06-13 16:51:42 +02:00
self . addScheduleButton = QPushButton ( " Add lesson " )
2008-06-13 20:53:23 +02:00
self . deleteScheduleButton = QPushButton ( " Delete lesson " )
2008-06-13 16:51:42 +02:00
2008-06-18 21:26:48 +02:00
# Make the table
2008-06-13 16:51:42 +02:00
self . makeTable ( )
2008-06-18 15:41:03 +02:00
# Layout
2008-06-17 15:39:39 +02:00
self . vlayout = QVBoxLayout ( )
self . hlayout = QHBoxLayout ( )
self . hlayout . addWidget ( self . addScheduleButton )
self . hlayout . addWidget ( self . deleteScheduleButton )
2008-06-18 21:26:48 +02:00
self . hlayout . addStretch ( )
2008-06-17 15:39:39 +02:00
self . vlayout . addWidget ( self . scheduleTable )
self . vlayout . addLayout ( self . hlayout )
self . setLayout ( self . vlayout )
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
## Make an empty schedule table
2008-06-13 16:51:42 +02:00
def makeTable ( self , current = None ) :
self . scheduleTable = QTableWidget ( 12 , 5 , self )
2008-06-18 15:41:03 +02:00
self . scheduleTable . setRowCount ( 11 )
2008-06-13 16:51:42 +02:00
self . scheduleTable . clear ( )
self . scheduleHorizontalHeaderList = QStringList ( )
self . scheduleHorizontalHeaderList . append ( self . trUtf8 ( " Monday " ) )
self . scheduleHorizontalHeaderList . append ( self . trUtf8 ( " Tuesday " ) )
self . scheduleHorizontalHeaderList . append ( self . trUtf8 ( " Wednesday " ) )
self . scheduleHorizontalHeaderList . append ( self . trUtf8 ( " Thursday " ) )
self . scheduleHorizontalHeaderList . append ( self . trUtf8 ( " Friday " ) )
self . scheduleVerticalHeaderList = QStringList ( )
for i in range ( 8 , 20 ) :
self . scheduleVerticalHeaderList . append ( self . trUtf8 ( " %i " % i ) )
self . scheduleTable . setHorizontalHeaderLabels ( self . scheduleHorizontalHeaderList )
self . scheduleTable . setVerticalHeaderLabels ( self . scheduleVerticalHeaderList )
2008-06-18 15:41:03 +02:00
self . scheduleTable . setAlternatingRowColors ( False )
2008-06-13 16:51:42 +02:00
self . scheduleTable . setEditTriggers ( QAbstractItemView . NoEditTriggers )
self . scheduleTable . setSelectionBehavior ( QAbstractItemView . SelectItems )
self . scheduleTable . setSelectionMode ( QAbstractItemView . SingleSelection )
selected = None
2008-06-18 21:26:48 +02:00
2008-06-18 15:41:03 +02:00
## Add the lessons of the semester to the table
2008-06-17 15:39:39 +02:00
def updateTable ( self , semester ) :
self . schedule = getLessonsFromDB ( semester )
rows = len ( self . schedule )
for l in range ( rows ) :
2008-06-13 20:53:23 +02:00
self . addLessonToTable ( self . schedule [ l ] )
2008-06-13 16:51:42 +02:00
self . scheduleTable . horizontalHeader ( ) . setResizeMode ( QHeaderView . ResizeToContents )
self . scheduleTable . verticalHeader ( ) . setResizeMode ( QHeaderView . ResizeToContents )
2008-06-18 15:41:03 +02:00
## Add a new lesson to the table
2008-06-13 20:53:23 +02:00
def addLessonToTable ( self , lesson ) :
2008-06-15 21:21:39 +02:00
row = lesson . getTime ( ) - 8
2008-06-13 20:53:23 +02:00
column = self . getColumn ( lesson . getDay ( ) )
2008-06-18 21:26:48 +02:00
course = lesson . getCourse ( ) . getFull ( )
type = lesson . getType ( )
room = lesson . getRoom ( )
olditem = self . scheduleTable . item ( row , column )
2008-06-18 15:41:03 +02:00
newtext = " %s \n %s \n %s " % ( course , type , room )
if olditem :
oldtext = olditem . text ( )
2008-06-18 21:26:48 +02:00
text = QString ( oldtext + " \n " + newtext )
collision = True
2008-06-18 15:41:03 +02:00
else :
text = QString ( newtext )
2008-06-18 21:26:48 +02:00
collision = False
2008-06-18 15:41:03 +02:00
item = QTableWidgetItem ( text )
2008-06-18 21:26:48 +02:00
item . setBackground ( self . getBackground ( QString ( " %s " % type ) , lesson . getCourse ( ) , collision ) )
self . scheduleTable . setItem ( row , column , item )
2008-06-13 20:53:23 +02:00
2008-06-18 15:41:03 +02:00
## Return the column specified by the day
2008-06-13 20:53:23 +02:00
def getColumn ( self , dayString ) :
day = QString ( " %s " % dayString )
if day . compare ( QString ( self . trUtf8 ( " Monday " ) ) ) == 0 :
return 0
elif day . compare ( QString ( self . trUtf8 ( " Tuesday " ) ) ) == 0 :
return 1
elif day . compare ( QString ( self . trUtf8 ( " Wednesday " ) ) ) == 0 :
return 2
elif day . compare ( QString ( self . trUtf8 ( " Thursday " ) ) ) == 0 :
return 3
elif day . compare ( QString ( self . trUtf8 ( " Friday " ) ) ) == 0 :
return 4
else :
return - 1
2008-06-18 15:41:03 +02:00
## Set the right brush and color for the lesson, depending on type of lesson
def getBackground ( self , type , course , collision ) :
2008-06-13 20:53:23 +02:00
if type . compare ( QString ( self . trUtf8 ( " Lecture " ) ) ) == 0 :
2008-06-18 15:41:03 +02:00
brush = QBrush ( Qt . SolidPattern )
2008-06-13 20:53:23 +02:00
elif type . compare ( QString ( self . trUtf8 ( " Assignment lecture " ) ) ) == 0 :
2008-06-18 15:41:03 +02:00
brush = QBrush ( Qt . Dense2Pattern )
2008-06-13 20:53:23 +02:00
elif type . compare ( QString ( self . trUtf8 ( " Assignment help " ) ) ) == 0 :
2008-06-18 15:41:03 +02:00
brush = QBrush ( Qt . Dense3Pattern )
2008-06-13 20:53:23 +02:00
elif type . compare ( QString ( self . trUtf8 ( " Lab " ) ) ) == 0 :
2008-06-18 15:41:03 +02:00
brush = QBrush ( Qt . Dense1Pattern )
2008-06-13 20:53:23 +02:00
elif type . compare ( QString ( self . trUtf8 ( " Seminar " ) ) ) == 0 :
2008-06-18 15:41:03 +02:00
brush = QBrush ( Qt . Dense4Pattern )
2008-06-13 20:53:23 +02:00
elif type . compare ( QString ( self . trUtf8 ( " Other " ) ) ) == 0 :
2008-06-18 15:41:03 +02:00
brush = QBrush ( Qt . Dense5Pattern )
2008-06-13 20:53:23 +02:00
else :
2008-06-18 15:41:03 +02:00
brush = QBrush ( Qt . NoBrush )
if not collision :
brush . setColor ( course . getColor ( ) )
else :
brush . setColor ( Qt . red )
2008-06-13 20:53:23 +02:00
return brush
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
### The Add Lesson dialog
2008-06-13 16:51:42 +02:00
class ScheduleDlg ( QDialog ) :
2008-06-18 15:41:03 +02:00
## Initialize the Add Lesson dialog
2008-06-13 16:51:42 +02:00
def __init__ ( self , parent = None ) :
super ( ScheduleDlg , self ) . __init__ ( parent )
2008-06-18 21:26:48 +02:00
# Labels
2008-06-13 16:51:42 +02:00
self . dayLabel = QLabel ( self . trUtf8 ( " &Day " ) )
self . fromLabel = QLabel ( self . trUtf8 ( " &From " ) )
self . toLabel = QLabel ( self . trUtf8 ( " &To " ) )
self . courseLabel = QLabel ( self . trUtf8 ( " &Course " ) )
self . typeLabel = QLabel ( self . trUtf8 ( " Ty&pe " ) )
self . roomLabel = QLabel ( self . trUtf8 ( " &Room " ) )
2008-06-18 21:26:48 +02:00
# Widgets
2008-06-13 16:51:42 +02:00
self . dayEdit = QComboBox ( )
2008-06-15 21:21:39 +02:00
self . dayEdit . addItems ( getMain ( ) . days )
2008-06-13 16:51:42 +02:00
self . fromEdit = QSpinBox ( )
self . fromEdit . setRange ( 08.15 , 18.15 )
self . fromEdit . setSingleStep ( 01.00 )
self . toEdit = QSpinBox ( )
self . toEdit . setRange ( 09.00 , 19.00 )
self . toEdit . setSingleStep ( 01.00 )
self . courseEdit = QComboBox ( )
courses = makeCoursesString ( )
2008-06-18 21:26:48 +02:00
coursesStringList = QStringList ( )
2008-06-13 16:51:42 +02:00
for course in courses :
coursesStringList . append ( course )
self . courseEdit . addItems ( coursesStringList )
self . typeEdit = QComboBox ( )
2008-06-18 21:26:48 +02:00
types = [ self . trUtf8 ( " Lecture " ) , self . trUtf8 ( " Assignment lecture " ) , self . trUtf8 ( " Assignment help " ) , self . trUtf8 ( " Lab " ) , self . trUtf8 ( " Seminar " ) , self . trUtf8 ( " Other " ) ]
self . typeEdit . addItems ( types )
2008-06-13 16:51:42 +02:00
self . roomEdit = QLineEdit ( )
2008-06-18 15:41:03 +02:00
self . buttonBox = QDialogButtonBox ( QDialogButtonBox . Ok | QDialogButtonBox . Cancel )
2008-06-13 16:51:42 +02:00
2008-06-18 21:26:48 +02:00
# Buddies
2008-06-13 16:51:42 +02:00
self . dayLabel . setBuddy ( self . dayEdit )
self . fromLabel . setBuddy ( self . fromEdit )
self . toLabel . setBuddy ( self . toEdit )
self . courseLabel . setBuddy ( self . courseEdit )
self . typeLabel . setBuddy ( self . typeEdit )
self . roomLabel . setBuddy ( self . roomEdit )
2008-06-18 21:26:48 +02:00
# Layout
2008-06-13 16:51:42 +02:00
self . layout = QGridLayout ( )
self . layout . addWidget ( self . dayLabel , 0 , 0 )
self . layout . addWidget ( self . fromLabel , 1 , 0 )
self . layout . addWidget ( self . toLabel , 2 , 0 )
self . layout . addWidget ( self . courseLabel , 3 , 0 )
self . layout . addWidget ( self . typeLabel , 4 , 0 )
self . layout . addWidget ( self . roomLabel , 5 , 0 )
self . layout . addWidget ( self . dayEdit , 0 , 1 )
self . layout . addWidget ( self . fromEdit , 1 , 1 )
self . layout . addWidget ( self . toEdit , 2 , 1 )
self . layout . addWidget ( self . courseEdit , 3 , 1 )
self . layout . addWidget ( self . typeEdit , 4 , 1 )
self . layout . addWidget ( self . roomEdit , 5 , 1 )
2008-06-18 15:41:03 +02:00
self . layout . addWidget ( self . buttonBox , 6 , 0 , 1 , 2 )
2008-06-13 16:51:42 +02:00
self . setLayout ( self . layout )
2008-06-18 15:41:03 +02:00
# Connect statements
2008-06-13 16:51:42 +02:00
self . connect ( buttonBox , SIGNAL ( " accepted() " ) , self , SLOT ( " accept() " ) )
self . connect ( buttonBox , SIGNAL ( " rejected() " ) , self , SLOT ( " reject() " ) )
2008-06-18 21:26:48 +02:00
# Set the title
2008-06-13 16:51:42 +02:00
self . setWindowTitle ( self . trUtf8 ( " Add new lesson " ) )
2008-06-18 15:41:03 +02:00
## Accept the dialog and add the specified lesson
2008-06-13 16:51:42 +02:00
def accept ( self ) :
day = unicode ( self . dayEdit . currentText ( ) )
2008-06-18 21:26:48 +02:00
fromtime = self . fromEdit . value ( )
totime = self . toEdit . value ( )
2008-06-13 16:51:42 +02:00
courseFull = unicode ( self . courseEdit . currentText ( ) )
type = unicode ( self . typeEdit . currentText ( ) )
2008-06-18 21:26:48 +02:00
room = unicode ( self . roomEdit . text ( ) )
2008-06-13 16:51:42 +02:00
course = getCourseFromDB ( getCourseCode ( courseFull ) )
2008-06-17 15:39:39 +02:00
global semester
2008-06-15 21:21:39 +02:00
for t in range ( fromtime , totime ) :
2008-06-17 15:39:39 +02:00
addNewLessonToDB ( semester , day , t , course , type , room )
2008-06-15 21:21:39 +02:00
getMain ( ) . schedule . addLessonToTable ( ScheduleModel ( day , t , course , type , room ) )
2008-06-18 21:26:48 +02:00
2008-06-15 21:21:39 +02:00
self . close ( )
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
### The schedule model
2008-06-13 16:51:42 +02:00
class ScheduleModel ( ) :
day = " "
2008-06-15 21:21:39 +02:00
time = 0
2008-06-13 16:51:42 +02:00
course = None
type = " "
room = " "
2008-06-18 15:41:03 +02:00
## Initialize the schedule model
2008-06-15 21:21:39 +02:00
def __init__ ( self , day , time , course , type , room ) :
2008-06-13 16:51:42 +02:00
self . day = day
2008-06-15 21:21:39 +02:00
self . time = time
2008-06-13 16:51:42 +02:00
self . course = course
self . type = type
self . room = room
2008-06-18 15:41:03 +02:00
## Return the day of the lesson
2008-06-13 16:51:42 +02:00
def getDay ( self ) :
return self . day
2008-06-18 15:41:03 +02:00
## Return the start time of the lesson
2008-06-15 21:21:39 +02:00
def getTime ( self ) :
return self . time
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
## Return the course of the lesson
2008-06-13 16:51:42 +02:00
def getCourse ( self ) :
return self . course
2008-06-18 15:41:03 +02:00
## Return the type of the lesson
2008-06-13 16:51:42 +02:00
def getType ( self ) :
return self . type
2008-06-18 15:41:03 +02:00
## Return the room the lesson is in
2008-06-13 16:51:42 +02:00
def getRoom ( self ) :
2008-06-18 21:26:48 +02:00
return self . room
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
### The Add Course dialog
2008-06-13 16:51:42 +02:00
class CourseDlg ( QDialog ) :
2008-06-18 15:41:03 +02:00
## Initialize the Add Course dialog
2008-06-13 16:51:42 +02:00
def __init__ ( self , parent = None ) :
super ( CourseDlg , self ) . __init__ ( parent )
2008-06-18 21:26:48 +02:00
# The books
2008-06-13 16:51:42 +02:00
self . books = [ ]
2008-06-18 15:41:03 +02:00
# Labels
2008-06-13 16:51:42 +02:00
self . codeLabel = QLabel ( self . trUtf8 ( " &Code " ) )
self . titleLabel = QLabel ( self . trUtf8 ( " &Title " ) )
self . shortLabel = QLabel ( self . trUtf8 ( " &Short form " ) )
self . booksLabel = QLabel ( self . trUtf8 ( " &Books " ) )
2008-06-18 15:41:03 +02:00
# Widgets
2008-06-13 16:51:42 +02:00
self . codeEdit = QLineEdit ( )
self . titleEdit = QLineEdit ( )
self . shortEdit = QLineEdit ( )
2008-06-15 21:21:39 +02:00
self . booksEdit = QListWidget ( )
2008-06-18 21:26:48 +02:00
self . updateList ( )
2008-06-15 21:21:39 +02:00
self . booksEdit . setSelectionMode ( QAbstractItemView . ExtendedSelection )
self . newBook = QPushButton ( " Add new book " )
2008-06-18 15:41:03 +02:00
self . buttonBox = QDialogButtonBox ( QDialogButtonBox . Ok | QDialogButtonBox . Cancel )
2008-06-15 21:21:39 +02:00
2008-06-18 15:41:03 +02:00
# Buddies
2008-06-18 21:26:48 +02:00
self . codeLabel . setBuddy ( self . codeEdit )
2008-06-13 16:51:42 +02:00
self . titleLabel . setBuddy ( self . titleEdit )
self . shortLabel . setBuddy ( self . shortEdit )
self . booksLabel . setBuddy ( self . booksEdit )
2008-06-18 15:41:03 +02:00
# Layout
2008-06-13 16:51:42 +02:00
self . layout = QGridLayout ( )
self . layout . addWidget ( self . codeLabel , 0 , 0 )
self . layout . addWidget ( self . titleLabel , 1 , 0 )
self . layout . addWidget ( self . shortLabel , 2 , 0 )
2008-06-18 21:26:48 +02:00
self . layout . addWidget ( self . booksLabel , 3 , 0 )
2008-06-13 16:51:42 +02:00
self . layout . addWidget ( self . codeEdit , 0 , 1 )
self . layout . addWidget ( self . titleEdit , 1 , 1 )
self . layout . addWidget ( self . shortEdit , 2 , 1 )
2008-06-18 21:26:48 +02:00
self . layout . addWidget ( self . booksEdit , 3 , 1 )
self . layout . addWidget ( self . newBook , 4 , 0 )
2008-06-18 15:41:03 +02:00
self . layout . addWidget ( self . buttonBox , 4 , 1 , 1 , 2 )
2008-06-13 16:51:42 +02:00
self . setLayout ( self . layout )
2008-06-18 15:41:03 +02:00
# Connect statements
2008-06-18 21:26:48 +02:00
self . connect ( self . newBook , SIGNAL ( " pressed() " ) , getMain ( ) . addBook )
2008-06-18 15:41:03 +02:00
self . connect ( getMain ( ) , SIGNAL ( " newBook " ) , self . updateList )
self . connect ( self . buttonBox , SIGNAL ( " accepted() " ) , self , SLOT ( " accept() " ) )
self . connect ( self . buttonBox , SIGNAL ( " rejected() " ) , self , SLOT ( " reject() " ) )
2008-06-18 21:26:48 +02:00
# Set the title
2008-06-18 15:41:03 +02:00
self . setWindowTitle ( self . trUtf8 ( " Add new course " ) )
## Update the book list
2008-06-15 21:21:39 +02:00
def updateList ( self ) :
self . booksEdit . clear ( )
booksDB = getBooksFromDB ( )
booksStringList = QStringList ( )
for book in booksDB :
booksStringList . append ( book . getTitle ( ) )
self . booksEdit . addItems ( booksStringList )
self . booksEdit . sortItems ( )
2008-06-18 15:41:03 +02:00
## Add a new book to the course
2008-06-15 15:28:23 +02:00
def addNewBookToCourse ( self ) :
book = getBookWithTitleFromDB ( booktitle )
self . books . append ( book )
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
## Accept the dialog and add the specified course
2008-06-13 16:51:42 +02:00
def accept ( self ) :
courseCode = unicode ( self . codeEdit . text ( ) )
courseTitle = unicode ( self . titleEdit . text ( ) )
2008-06-14 19:48:48 +02:00
courseShort = unicode ( self . shortEdit . text ( ) )
2008-06-18 21:26:48 +02:00
courseBooks = self . booksEdit . selectedItems ( )
color = getRandomColor ( )
global colors
2008-06-14 19:48:48 +02:00
while color in colors :
color = getRandomColor ( )
colors . append ( color )
2008-06-15 21:21:39 +02:00
books = [ ]
2008-06-18 21:26:48 +02:00
for book in courseBooks :
2008-06-15 21:21:39 +02:00
books . append ( getBookWithTitleFromDB ( " %s " % book . text ( ) ) )
course = CourseModel ( courseCode , courseTitle , courseShort , color , books )
addNewCourseToDB ( courseCode , courseTitle , courseShort , color , books )
2008-06-13 16:51:42 +02:00
self . close ( )
2008-06-18 15:41:03 +02:00
### The course model
2008-06-13 16:51:42 +02:00
class CourseModel ( ) :
2008-06-18 21:26:48 +02:00
2008-06-13 16:51:42 +02:00
code = " "
title = " "
short = " "
full = " "
books = [ ]
2008-06-18 15:41:03 +02:00
## Initialize the course model
2008-06-14 19:48:48 +02:00
def __init__ ( self , code , title , short , color , books ) :
2008-06-13 16:51:42 +02:00
self . code = code
self . title = title
self . short = short
self . setFull ( code , title )
2008-06-15 15:28:23 +02:00
self . color = color
2008-06-13 16:51:42 +02:00
self . books = books
2008-06-18 15:41:03 +02:00
## Return the code of the course
2008-06-13 16:51:42 +02:00
def getCode ( self ) :
return self . code
2008-06-18 15:41:03 +02:00
## Return the title of the course
2008-06-13 16:51:42 +02:00
def getTitle ( self ) :
return self . title
2008-06-18 21:26:48 +02:00
2008-06-18 15:41:03 +02:00
## Return the short form of the course
2008-06-13 16:51:42 +02:00
def getShort ( self ) :
return self . short
2008-06-18 15:41:03 +02:00
## Set the full form of the course
2008-06-13 16:51:42 +02:00
def setFull ( self , code , title ) :
self . full = code + ' ' + title
2008-06-18 15:41:03 +02:00
## Return the full form of the course
2008-06-13 16:51:42 +02:00
def getFull ( self ) :
return self . full
2008-06-18 15:41:03 +02:00
## Return the color of the course
2008-06-14 19:48:48 +02:00
def getColor ( self ) :
return self . color
2008-06-18 15:41:03 +02:00
## Add a book to the course
def addBook ( self , book ) :
books . append ( book )
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
## Return the books of the course
def getBooks ( self ) :
return self . books
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
### The Add Book dialog
class BookDlg ( QDialog ) :
## Initialize the Add Book dialog
2008-06-17 15:39:39 +02:00
def __init__ ( self , parent = None ) :
2008-06-18 15:41:03 +02:00
super ( BookDlg , self ) . __init__ ( parent )
2008-06-18 21:26:48 +02:00
# Labels
2008-06-18 15:41:03 +02:00
self . titleLabel = QLabel ( self . trUtf8 ( " &Title " ) )
self . authorLabel = QLabel ( self . trUtf8 ( " &Author " ) )
self . editionLabel = QLabel ( self . trUtf8 ( " &Edition " ) )
self . isbnLabel = QLabel ( self . trUtf8 ( " &ISBN " ) )
2008-06-18 21:26:48 +02:00
# Widgets
2008-06-18 15:41:03 +02:00
self . titleEdit = QLineEdit ( )
self . authorEdit = QLineEdit ( )
self . editionEdit = QSpinBox ( )
self . editionEdit . setRange ( 1 , 50 )
self . isbnEdit = QLineEdit ( )
2008-06-17 15:39:39 +02:00
self . buttonBox = QDialogButtonBox ( QDialogButtonBox . Ok | QDialogButtonBox . Cancel )
2008-06-18 21:26:48 +02:00
# Buddies
2008-06-18 15:41:03 +02:00
self . titleLabel . setBuddy ( self . titleEdit )
self . authorLabel . setBuddy ( self . authorEdit )
self . editionLabel . setBuddy ( self . editionEdit )
self . isbnLabel . setBuddy ( self . isbnEdit )
2008-06-18 21:26:48 +02:00
# Layout
2008-06-18 15:41:03 +02:00
self . layout = QGridLayout ( )
self . layout . addWidget ( self . titleLabel , 0 , 0 )
self . layout . addWidget ( self . authorLabel , 1 , 0 )
self . layout . addWidget ( self . editionLabel , 2 , 0 )
self . layout . addWidget ( self . isbnLabel , 3 , 0 )
self . layout . addWidget ( self . titleEdit , 0 , 1 )
self . layout . addWidget ( self . authorEdit , 1 , 1 )
self . layout . addWidget ( self . editionEdit , 2 , 1 )
self . layout . addWidget ( self . isbnEdit , 3 , 1 )
self . layout . addWidget ( self . buttonBox , 4 , 0 , 1 , 2 )
self . setLayout ( self . layout )
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
# Connect statements
2008-06-17 15:39:39 +02:00
self . connect ( self . buttonBox , SIGNAL ( " accepted() " ) , self , SLOT ( " accept() " ) )
self . connect ( self . buttonBox , SIGNAL ( " rejected() " ) , self , SLOT ( " reject() " ) )
2008-06-18 15:41:03 +02:00
2008-06-18 21:26:48 +02:00
# Set the title
2008-06-18 15:41:03 +02:00
self . setWindowTitle ( self . trUtf8 ( " Add new book " ) )
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
# Accept the dialog and add the specified book
2008-06-17 15:39:39 +02:00
def accept ( self ) :
2008-06-18 15:41:03 +02:00
bookTitle = unicode ( self . titleEdit . text ( ) )
bookAuthor = unicode ( self . authorEdit . text ( ) )
bookEdition = self . editionEdit . value ( )
bookIsbn = unicode ( self . isbnEdit . text ( ) )
addNewBookToDB ( bookIsbn , bookTitle , bookAuthor , bookEdition )
getMain ( ) . emit ( SIGNAL ( " newBook " ) )
self . close ( )
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
### The book model
class BookModel ( ) :
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
title = " "
author = " "
edition = 0
isbn = " "
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
## Initialize the book model
def __init__ ( self , isbn , title , author , edition ) :
2008-06-18 21:26:48 +02:00
self . isbn = isbn
2008-06-18 15:41:03 +02:00
self . title = title
2008-06-18 21:26:48 +02:00
self . author = author
self . edition = edition
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
# Return the ISBN number of the book
def getIsbn ( self ) :
return self . isbn
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
# Return the title of the book
def getTitle ( self ) :
return self . title
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
# Return the author(s) of the book
def getAuthor ( self ) :
return self . author
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
# Return the edition of the book
def getEdition ( self ) :
return self . edition
2008-06-15 21:21:39 +02:00
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
# Database
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
# Initialization
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
## Connect to the database and return the cursor and connection
2008-06-13 16:51:42 +02:00
def initDB ( ) :
conn = sqlite . connect ( ' egon.db ' )
2008-06-19 16:25:51 +02:00
cursor = conn . cursor ( )
cursor . execute ( '''
SELECT name FROM sqlite_master
WHERE type = ' table '
ORDER BY name
''' )
if cursor . fetchall ( ) == [ ] :
initSemesterDB ( cursor )
initAssignmentDB ( cursor )
initReadingDB ( cursor )
initScheduleDB ( cursor )
initBookDB ( cursor )
initCourseDB ( cursor )
initCourseUsesBook ( cursor )
return cursor , conn
2008-06-13 16:51:42 +02:00
exitDB ( conn )
2008-06-18 15:41:03 +02:00
# Create the database
## Create the Semester table
2008-06-15 21:21:39 +02:00
def initSemesterDB ( cursor ) :
cursor . execute ( '''
CREATE TABLE Semester (
term TEXT ,
year INT ,
PRIMARY KEY ( term , year )
)
''' )
2008-06-18 15:41:03 +02:00
## Create the Assignment table
2008-06-13 16:51:42 +02:00
def initAssignmentDB ( cursor ) :
cursor . execute ( '''
CREATE TABLE Assignment (
date DATETIME ,
2008-06-18 21:26:48 +02:00
course TEXT ,
2008-06-13 16:51:42 +02:00
number INT ,
2008-06-18 21:26:48 +02:00
description TEXT ,
complete TEXT ,
term TEXT ,
2008-06-17 15:39:39 +02:00
year INT ,
2008-06-13 16:51:42 +02:00
PRIMARY KEY ( course , number )
)
''' )
2008-06-18 15:41:03 +02:00
## Create the Reading table
2008-06-13 16:51:42 +02:00
def initReadingDB ( cursor ) :
cursor . execute ( '''
CREATE TABLE Reading (
2008-06-15 21:21:39 +02:00
week INT ,
2008-06-18 21:26:48 +02:00
course TEXT ,
book INT ,
2008-06-13 16:51:42 +02:00
chapter TEXT ,
pages TEXT ,
done BOOLEAN ,
2008-06-17 15:39:39 +02:00
term TEXT ,
year INT ,
2008-06-13 16:51:42 +02:00
PRIMARY KEY ( week , course , book )
)
''' )
2008-06-18 15:41:03 +02:00
## Create the Lesson table
2008-06-13 16:51:42 +02:00
def initScheduleDB ( cursor ) :
cursor . execute ( '''
CREATE TABLE Lesson (
day TEXT ,
2008-06-15 21:21:39 +02:00
time INT ,
2008-06-18 21:26:48 +02:00
course TEXT ,
2008-06-13 16:51:42 +02:00
type TEXT ,
room TEXT ,
2008-06-17 15:39:39 +02:00
term TEXT ,
year INT ,
2008-06-15 21:21:39 +02:00
PRIMARY KEY ( course , day , time )
2008-06-13 16:51:42 +02:00
)
''' )
2008-06-18 15:41:03 +02:00
## Create the Course table
2008-06-13 16:51:42 +02:00
def initCourseDB ( cursor ) :
cursor . execute ( '''
CREATE TABLE Course (
code TEXT PRIMARY KEY ,
title TEXT ,
2008-06-14 19:48:48 +02:00
short TEXT ,
red INT ,
green INT ,
blue INT
2008-06-13 16:51:42 +02:00
)
''' )
2008-06-18 15:41:03 +02:00
## Create the Book table
def initBookDB ( cursor ) :
cursor . execute ( '''
CREATE TABLE Book (
isbn TEXT PRIMARY KEY ,
title TEXT ,
author TEXT ,
edition INTEGER
)
''' )
## Create the Course-Book relation
2008-06-13 16:51:42 +02:00
def initCourseUsesBook ( cursor ) :
cursor . execute ( '''
CREATE TABLE CourseUsesBook (
courseCode TEXT ,
bookIsbn TEXT ,
2008-06-18 21:26:48 +02:00
PRIMARY KEY ( courseCode , bookIsbn )
)
2008-06-13 16:51:42 +02:00
''' )
2008-06-18 15:41:03 +02:00
# Add things to the database
## Add new semester to the database
2008-06-17 15:39:39 +02:00
def addNewSemesterToDB ( term , year ) :
cursor , conn = initDB ( )
cursor . execute ( '''
INSERT INTO Semester ( term , year )
VALUES ( ? , ? )
''' , (term, year))
exitDB ( conn )
2008-06-18 15:41:03 +02:00
## Add new assignment to the database
2008-06-15 21:21:39 +02:00
def addNewAssignmentToDB ( semester , datetime , course , number , description , complete ) :
2008-06-13 16:51:42 +02:00
cursor , conn = initDB ( )
day = datetime . date ( ) . day ( )
month = datetime . date ( ) . month ( )
year = datetime . date ( ) . year ( )
hour = datetime . time ( ) . hour ( )
minute = datetime . time ( ) . minute ( )
timestring = " %02i - %02i - %02i %02i : %02i " % ( year , month , day , hour , minute )
2008-06-17 15:39:39 +02:00
term = " %s " % semester . getTerm ( )
year = semester . getYear ( )
2008-06-13 16:51:42 +02:00
cursor . execute ( '''
2008-06-17 15:39:39 +02:00
INSERT INTO Assignment ( date , course , number , description , complete , term , year )
2008-06-18 21:26:48 +02:00
VALUES ( datetime ( ? ) , ? , ? , ? , ? , ? , ? )
2008-06-17 15:39:39 +02:00
''' , (timestring, course.getCode(), number, description, complete, term, year))
2008-06-13 16:51:42 +02:00
2008-06-15 21:21:39 +02:00
exitDB ( conn )
2008-06-13 16:51:42 +02:00
2008-06-18 15:41:03 +02:00
## Add new reading to the database
2008-06-15 21:21:39 +02:00
def addNewReadingToDB ( semester , week , course , book , chapter , pages , done ) :
2008-06-13 16:51:42 +02:00
cursor , conn = initDB ( )
2008-06-18 15:41:03 +02:00
term = " %s " % semester . getTerm ( )
year = semester . getYear ( )
2008-06-13 16:51:42 +02:00
cursor . execute ( '''
2008-06-17 15:39:39 +02:00
INSERT INTO Reading ( week , course , book , chapter , pages , done , term , year )
VALUES ( ? , ? , ? , ? , ? , ? , ? , ? )
2008-06-18 15:41:03 +02:00
''' , (week, course.getCode(), book.getIsbn(), chapter, pages, done, term, year))
2008-06-15 21:21:39 +02:00
2008-06-13 16:51:42 +02:00
exitDB ( conn )
2008-06-18 15:41:03 +02:00
## Add new lesson to the database
2008-06-15 21:21:39 +02:00
def addNewLessonToDB ( semester , day , time , course , type , room ) :
2008-06-13 16:51:42 +02:00
cursor , conn = initDB ( )
2008-06-18 15:41:03 +02:00
term = " %s " % semester . getTerm ( )
year = semester . getYear ( )
2008-06-13 16:51:42 +02:00
cursor . execute ( '''
2008-06-17 15:39:39 +02:00
INSERT INTO Lesson ( day , time , course , type , room , term , year )
VALUES ( ? , ? , ? , ? , ? , ? , ? )
2008-06-18 15:41:03 +02:00
''' , (day, time, course.getCode(), type, room, term, year))
2008-06-13 16:51:42 +02:00
exitDB ( conn )
2008-06-18 15:41:03 +02:00
## Add new course to the database
2008-06-14 19:48:48 +02:00
def addNewCourseToDB ( code , title , short , color , books ) :
2008-06-13 16:51:42 +02:00
cursor , conn = initDB ( )
2008-06-14 19:48:48 +02:00
red = color . red ( )
green = color . green ( )
blue = color . blue ( )
2008-06-13 16:51:42 +02:00
cursor . execute ( '''
2008-06-14 19:48:48 +02:00
INSERT INTO Course ( code , title , short , red , green , blue )
VALUES ( ? , ? , ? , ? , ? , ? )
''' , (code, title, short, red, green, blue))
2008-06-13 16:51:42 +02:00
for book in books :
cursor . execute ( '''
INSERT INTO CourseUsesBook ( courseCode , bookIsbn )
VALUES ( ? , ? )
''' , (code, book.getIsbn()))
exitDB ( conn )
2008-06-18 15:41:03 +02:00
## Add new book to the database
2008-06-13 16:51:42 +02:00
def addNewBookToDB ( isbn , title , author , edition ) :
cursor , conn = initDB ( )
cursor . execute ( '''
INSERT INTO Book
VALUES ( ? , ? , ? , ? )
''' , (isbn, title, author, edition))
exitDB ( conn )
2008-06-18 15:41:03 +02:00
# Get things from the database
## Get all the semesters form the database
def getSemestersFromDB ( ) :
cursor , conn = initDB ( )
cursor . execute ( '''
SELECT *
FROM Semester
''' )
semesters = [ ]
for row in cursor . fetchall ( ) :
semesters . append ( SemesterModel ( row [ 0 ] , row [ 1 ] ) )
exitDB ( conn )
return semesters
## Get the assignments of the specified semester from the database
2008-06-17 15:39:39 +02:00
def getAssignmentsFromDB ( semester ) :
2008-06-13 16:51:42 +02:00
cursor , conn = initDB ( )
2008-06-17 15:39:39 +02:00
term = " %s " % semester . getTerm ( )
year = ( semester . getYear ( ) )
2008-06-13 16:51:42 +02:00
cursor . execute ( '''
SELECT *
FROM Assignment
2008-06-17 15:39:39 +02:00
WHERE term = ?
AND year = ?
''' , (term, year))
2008-06-13 16:51:42 +02:00
assignments = [ ]
for row in cursor . fetchall ( ) :
assignments . append ( AssignmentModel ( getQDateTime ( row [ 0 ] ) , getCourseFromDB ( row [ 1 ] ) , row [ 2 ] , row [ 3 ] , row [ 4 ] ) )
exitDB ( conn )
return assignments
2008-06-18 15:41:03 +02:00
## Get the readings of the specified semester from the database
2008-06-17 15:39:39 +02:00
def getReadingsFromDB ( semester ) :
2008-06-13 16:51:42 +02:00
cursor , conn = initDB ( )
2008-06-17 15:39:39 +02:00
term = " %s " % semester . getTerm ( )
year = semester . getYear ( )
2008-06-13 16:51:42 +02:00
cursor . execute ( '''
SELECT *
FROM Reading
2008-06-17 15:39:39 +02:00
WHERE term = ?
AND year = ?
''' , (term, year))
2008-06-13 16:51:42 +02:00
readings = [ ]
for row in cursor . fetchall ( ) :
readings . append ( ReadingModel ( row [ 0 ] , getCourseFromDB ( row [ 1 ] ) , getBookFromDB ( row [ 2 ] ) , row [ 3 ] , row [ 4 ] , row [ 5 ] ) )
exitDB ( conn )
return readings
2008-06-18 15:41:03 +02:00
## Get the lessons of the specified semester from the database
2008-06-17 15:39:39 +02:00
def getLessonsFromDB ( semester ) :
2008-06-13 16:51:42 +02:00
cursor , conn = initDB ( )
2008-06-17 15:39:39 +02:00
term = " %s " % semester . getTerm ( )
year = semester . getYear ( )
2008-06-13 16:51:42 +02:00
cursor . execute ( '''
SELECT *
FROM Lesson
2008-06-17 15:39:39 +02:00
WHERE term = ?
AND year = ?
''' , (term, year))
2008-06-13 16:51:42 +02:00
lessons = [ ]
for row in cursor . fetchall ( ) :
2008-06-15 21:21:39 +02:00
lessons . append ( ScheduleModel ( row [ 0 ] , row [ 1 ] , getCourseFromDB ( row [ 2 ] ) , row [ 3 ] , row [ 4 ] ) )
2008-06-13 16:51:42 +02:00
exitDB ( conn )
return lessons
2008-06-18 15:41:03 +02:00
## Get all the courses from the database
2008-06-13 16:51:42 +02:00
def getCoursesFromDB ( ) :
cursor , conn = initDB ( )
cursor . execute ( '''
SELECT *
FROM Course
''' )
courses = [ ]
for row in cursor . fetchall ( ) :
2008-06-18 21:26:48 +02:00
courses . append ( CourseModel ( row [ 0 ] , row [ 1 ] , row [ 2 ] , QColor ( row [ 3 ] , row [ 4 ] , row [ 5 ] ) , [ ] ) )
2008-06-13 16:51:42 +02:00
for course in courses :
cursor . execute ( '''
SELECT bookIsbn
FROM CourseUsesBook
2008-06-18 21:26:48 +02:00
WHERE courseCode = ?
2008-06-13 16:51:42 +02:00
''' , (course.getCode(),))
2008-06-18 21:26:48 +02:00
fetched = cursor . fetchall ( )
2008-06-13 16:51:42 +02:00
for fetchedRow in fetched :
cursor . execute ( '''
SELECT *
FROM Book
WHERE isbn = ?
''' , (fetchedRow[0],))
for row in cursor :
course . addBook ( BookModel ( row [ 0 ] , row [ 1 ] , row [ 2 ] , row [ 3 ] ) )
return courses
2008-06-18 15:41:03 +02:00
## Get the course specified by the course code from the database
2008-06-13 16:51:42 +02:00
def getCourseFromDB ( courseCode ) :
cursor , conn = initDB ( )
cursor . execute ( '''
SELECT *
FROM Course
WHERE code = ?
''' , (courseCode,))
2008-06-15 21:21:39 +02:00
course = None
2008-06-13 16:51:42 +02:00
for row in cursor . fetchall ( ) :
2008-06-14 19:48:48 +02:00
course = CourseModel ( row [ 0 ] , row [ 1 ] , row [ 2 ] , QColor ( row [ 3 ] , row [ 4 ] , row [ 5 ] ) , [ ] )
2008-06-13 16:51:42 +02:00
cursor . execute ( '''
SELECT bookIsbn
FROM CourseUsesBook
WHERE courseCode = ?
''' , (courseCode,))
fetched = cursor . fetchall ( )
for fetchedRow in fetched :
cursor . execute ( '''
SELECT *
FROM Book
WHERE isbn = ?
''' , (fetchedRow[0],))
for row in cursor :
course . addBook ( BookModel ( row [ 0 ] , row [ 1 ] , row [ 2 ] , row [ 3 ] ) )
exitDB ( conn )
return course
2008-06-18 15:41:03 +02:00
## Get all the books from the database
2008-06-13 16:51:42 +02:00
def getBooksFromDB ( ) :
cursor , conn = initDB ( )
cursor . execute ( '''
SELECT *
FROM Book
''' )
books = [ ]
for row in cursor . fetchall ( ) :
2008-06-18 21:26:48 +02:00
books . append ( BookModel ( row [ 0 ] , row [ 1 ] , row [ 2 ] , row [ 3 ] ) )
2008-06-13 16:51:42 +02:00
exitDB ( conn )
return books
2008-06-18 15:41:03 +02:00
## Get the book specified by the ISBN number from the database
2008-06-13 16:51:42 +02:00
def getBookFromDB ( isbn ) :
cursor , conn = initDB ( )
cursor . execute ( '''
SELECT *
FROM Book
WHERE isbn = ?
''' , (isbn,))
for row in cursor . fetchall ( ) :
book = BookModel ( row [ 0 ] , row [ 1 ] , row [ 2 ] , row [ 3 ] )
exitDB ( conn )
return book
2008-06-18 15:41:03 +02:00
## Get the book specified by the title from the database
2008-06-13 16:51:42 +02:00
def getBookWithTitleFromDB ( booktitle ) :
cursor , conn = initDB ( )
book = " %s " % booktitle
cursor . execute ( '''
SELECT *
FROM Book
WHERE title = ?
''' , (book,))
for row in cursor . fetchall ( ) :
book = BookModel ( row [ 0 ] , row [ 1 ] , row [ 2 ] , row [ 3 ] )
exitDB ( conn )
return book
2008-06-18 15:41:03 +02:00
# Update things in the database
2008-06-17 15:39:39 +02:00
2008-06-18 15:41:03 +02:00
## Update the completion level of the specified assignment
2008-06-13 16:51:42 +02:00
def updateAssignmentCompletion ( coursecode , num , completion ) :
cursor , conn = initDB ( )
complete = " %s " % completion
course = " %s " % coursecode
number = ( num . toInt ( ) ) [ 0 ]
cursor . execute ( '''
UPDATE Assignment
SET complete = ?
WHERE course = ?
AND number = ?
''' , (complete, course, number))
exitDB ( conn )
2008-06-18 15:41:03 +02:00
## Update whether the specified reading is done or not
2008-06-13 16:51:42 +02:00
def updateReadingDone ( week , coursecode , bookisbn , done ) :
cursor , conn = initDB ( )
course = " %s " % coursecode
book = " %s " % bookisbn
cursor . execute ( '''
UPDATE Reading
SET done = ?
WHERE week = ?
AND course = ?
AND book = ?
''' , (done, week, course, book))
exitDB ( conn )
2008-06-18 15:41:03 +02:00
# Remove things from the database
## Remove the specified assignment from the database
2008-06-13 16:51:42 +02:00
def removeAssignmentFromDB ( coursecode , num ) :
cursor , conn = initDB ( )
course = " %s " % coursecode
number = ( num . toInt ( ) ) [ 0 ]
cursor . execute ( '''
DELETE
FROM Assignment
WHERE course = ?
AND number = ?
''' , (course, number))
exitDB ( conn )
2008-06-18 15:41:03 +02:00
## Remove the specified reading from the database
2008-06-13 16:51:42 +02:00
def removeReadingFromDB ( week , coursecode , bookisbn ) :
cursor , conn = initDB ( )
course = " %s " % coursecode
book = " %s " % bookisbn
cursor . execute ( '''
DELETE
FROM Reading
WHERE week = ?
AND course = ?
AND book = ?
''' , (week, course, book))
exitDB ( conn )
2008-06-18 15:41:03 +02:00
## Remove the specified lesson from the database
2008-06-15 21:21:39 +02:00
def removeLessonFromDB ( daystring , coursecode , fromTime ) :
cursor , conn = initDB ( )
day = " %s " % daystring
course = " %s " % coursecode
time = " %s " % fromTime
cursor . execute ( '''
DELETE
FROM Lesson
WHERE day = ?
AND course = ?
AND time = ?
''' , (day, course, time))
exitDB ( conn )
2008-06-18 15:41:03 +02:00
# Exit the database
## Commit and close the connection
2008-06-13 16:51:42 +02:00
def exitDB ( conn ) :
conn . commit ( )
conn . close ( )
2008-06-18 15:41:03 +02:00
# Course
## Get the course code from the full name of the course
2008-06-13 16:51:42 +02:00
def getCourseCode ( courseFull ) :
course = courseFull . split ( ' ' )
return course [ 0 ]
2008-06-18 15:41:03 +02:00
## Add a new course to the list of courses
2008-06-13 16:51:42 +02:00
def addNewCourse ( course ) :
courses . append ( course )
2008-06-14 19:48:48 +02:00
addNewCourseToDB ( course . getCode ( ) , course . getTitle ( ) , course . getShort ( ) , course . getColor ( ) )
2008-06-13 16:51:42 +02:00
addNewCourseString ( course )
2008-06-18 15:41:03 +02:00
## Return the list of courses
2008-06-13 16:51:42 +02:00
def getCourses ( ) :
global courses
return courses
2008-06-18 15:41:03 +02:00
## Add the course as a course string
2008-06-13 16:51:42 +02:00
def addNewCourseString ( course ) :
global coursesString
coursesString . append ( course . getFull ( ) )
2008-06-18 15:41:03 +02:00
## Make the courses string list and return it
2008-06-13 16:51:42 +02:00
def makeCoursesString ( ) :
emptyCourses ( )
cs = getCoursesFromDB ( )
if cs :
for c in cs :
addNewCourseString ( c )
s = getCoursesString ( )
return s
2008-06-18 15:41:03 +02:00
## Return the courses string list
2008-06-13 16:51:42 +02:00
def getCoursesString ( ) :
global coursesString
return coursesString
2008-06-18 15:41:03 +02:00
## Empty the courses list and the courses string list
def emptyCourses ( ) :
global courses
global coursesString
courses = [ ]
coursesString = [ ]
# Book
## Add a new book to the list of books
2008-06-13 16:51:42 +02:00
def addNewBook ( book ) :
books . append ( book )
addNewBookToDB ( book . getISBN ( ) , book . getTitle ( ) , book . getAuthor ( ) , book . getEdition ( ) , book . getCourse ( ) )
addNewBookString ( book )
2008-06-18 15:41:03 +02:00
## Return the list of books
2008-06-13 16:51:42 +02:00
def getBooks ( ) :
global books
return books
2008-06-18 15:41:03 +02:00
## Add the book as a book string
2008-06-13 16:51:42 +02:00
def addNewBookString ( book ) :
global booksString
booksString . append ( book . getTitle ( ) )
2008-06-18 15:41:03 +02:00
## Make the books string list and return it
2008-06-13 16:51:42 +02:00
def makeBooksString ( ) :
emptyBooks ( )
bs = getBooksFromDB ( )
if bs :
for b in bs :
addNewBookString ( b )
s = getBooksString ( )
return s
2008-06-18 15:41:03 +02:00
## Return the books string list
2008-06-13 16:51:42 +02:00
def getBooksString ( ) :
global booksString
return booksString
2008-06-18 15:41:03 +02:00
## Empty the books list and the books string list
2008-06-13 16:51:42 +02:00
def emptyBooks ( ) :
global books
global booksString
books = [ ]
booksString = [ ]
2008-06-18 15:41:03 +02:00
# Color
## Return a random color, RGB
2008-06-14 19:48:48 +02:00
def getRandomColor ( ) :
return QColor ( getRandomColorPart ( ) , getRandomColorPart ( ) , getRandomColorPart ( ) )
2008-06-18 15:41:03 +02:00
## Return a random number between 0 and 255
2008-06-14 19:48:48 +02:00
def getRandomColorPart ( ) :
return random . random ( ) * 256
2008-06-18 15:41:03 +02:00
# General
## Convert a SQL timestamp to a QDateTime object
def getQDateTime ( timestamp ) :
if not timestamp :
return None
datetimeList = timestamp . split ( )
dateString = datetimeList [ 0 ]
timeString = datetimeList [ 1 ]
dateList = dateString . split ( ' - ' )
timeList = timeString . split ( ' : ' )
year = string . atoi ( dateList [ 0 ] )
month = string . atoi ( dateList [ 1 ] )
day = string . atoi ( dateList [ 2 ] )
hour = string . atoi ( timeList [ 0 ] )
minute = string . atoi ( timeList [ 1 ] )
date = QDate ( year , month , day )
time = QTime ( hour , minute )
datetime = QDateTime ( date , time )
return datetime
## Return the main window
2008-06-13 16:51:42 +02:00
def getMain ( ) :
global main
return main
2008-06-18 15:41:03 +02:00
## Run the program
2008-06-13 16:51:42 +02:00
def main ( ) :
app = QApplication ( sys . argv )
form = MainWindow ( )
app . setOrganizationName ( " PVV " )
app . setOrganizationDomain ( " pvv.ntnu.no " )
app . setApplicationName ( form . title )
form . show ( )
app . exec_ ( )
main ( )
#initNewDB()