diff --git a/egon.db b/egon.db index ce317e8..0433129 100644 Binary files a/egon.db and b/egon.db differ diff --git a/egon.py b/egon.py index 6eb04ec..3dd9017 100755 --- a/egon.py +++ b/egon.py @@ -14,10 +14,7 @@ import time import random from PyQt4.QtCore import * from PyQt4.QtGui import * -#from PyQt4 import QtGui -#from PyQt4 import QtCore from pysqlite2 import dbapi2 as sqlite -#import qrc_resources from qrc_resources import * @@ -31,12 +28,13 @@ courses = [] coursesString = [] books = [] booksString = [] -chosenDay = QDate() colors = [] termList = None +### The main window class MainWindow(QMainWindow): + # Initialize the main window def __init__(self, parent=None): super(MainWindow, self).__init__(parent) @@ -83,13 +81,11 @@ class MainWindow(QMainWindow): semesters = getSemestersFromDB() semester = self.getLatestSemester(semesters) if semester: - print semester.getTerm(), semester.getYear() self.load(semester) # Actions 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") - filePrintAction = self.createAction("&Print", self.filePrint, QKeySequence.Print, "fileprint", "Print plan") fileQuitAction = self.createAction("&Quit", self.close, "Ctrl+Q", "filequit", "Quit program") editAddCourse = self.createAction("Add &course", self.addCourse, "Ctrl+C", None, "Add a new course") editAddBook = self.createAction("Add &book", self.addBook, "Ctrl+B", None, "Add a new book") @@ -105,7 +101,7 @@ class MainWindow(QMainWindow): self.helpMenu = self.menuBar().addMenu("&Help") # Add actions to the menus - self.addActions(self.fileMenu, (fileNewAction, fileOpenAction, None, filePrintAction, None, fileQuitAction)) + self.addActions(self.fileMenu, (fileNewAction, fileOpenAction, None, fileQuitAction)) self.addActions(self.editMenu, (editAddCourse, editAddBook, None, editAddAssignment, None, editAddReading, None, editAddLesson, None, editShowCalendar)) self.addActions(self.helpMenu, (helpAboutScheduler, None)) @@ -130,7 +126,7 @@ class MainWindow(QMainWindow): # The toolbars fileToolbar = self.addToolBar("File") fileToolbar.setObjectName("FileToolBar") - self.addActions(fileToolbar, (fileNewAction, fileOpenAction, None, filePrintAction, None, fileQuitAction)) + self.addActions(fileToolbar, (fileNewAction, fileOpenAction, None, fileQuitAction)) editToolbar = self.addToolBar("Edit") editToolbar.setObjectName("EditToolBar") self.addActions(editToolbar, (editAddCourse, editAddBook, None, editShowCalendar)) @@ -138,24 +134,43 @@ class MainWindow(QMainWindow): # Set the title self.setMainWindowTitle() + # The courses courses = getCourses() makeCoursesString() + # This window global main main = self - def addCourse(self):##, code, title, short): - self.acdlg = AddCourseDlg() - self.acdlg.show() - - def addBook(self):##, isbn, title, author, edition, course): - self.abdlg = AddBookDlg() - self.abdlg.show() + # Semester + ## Return the latest semester + def getLatestSemester(self, semesters): + if len(semesters) == 0: + return None + global termList + max = semesters[0] + for s in semesters: + 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 + + ## Open the Add Assignment dialog def addAssignment(self): - self.aadlg = AddAssignmentDlg() - self.aadlg.show() + self.adlg = AssignmentDlg() + self.adlg.show() + ## Delete the assignment from the database and the table def deleteAssignment(self): course, number = self.getCourseAndNumber() table, row = self.getAssignmentTableAndRow() @@ -164,6 +179,7 @@ class MainWindow(QMainWindow): table.removeRow(row) table.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents) + ## Update the assignment's completion level to the specified level def completeAssignment(self, completionLevel): course, number = self.getCourseAndNumber() table, row = self.getAssignmentTableAndRow() @@ -173,11 +189,13 @@ class MainWindow(QMainWindow): table.setItem(row, 4, item) table.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents) + ## Return the assignment table and its current row def getAssignmentTableAndRow(self): table = self.assignment.assignmentTable row = table.currentRow() return table, row + ## Return the course and number of the current assignment def getCourseAndNumber(self): table, row = self.getAssignmentTableAndRow() courseItem = table.item(row, 1) @@ -187,10 +205,14 @@ class MainWindow(QMainWindow): course = getCourseCode(courseFull) return course, number - def addReading(self): - self.ardlg = AddReadingDlg() - self.ardlg.show() + # Reading + ## Open the Add Reading dialog + def addReading(self): + self.rdlg = ReadingDlg() + self.rdlg.show() + + ## Delete the reading from the database and the table def deleteReading(self): week, course, book = self.getWeekCourseAndBook() table, row = self.getReadingTableAndRow() @@ -199,6 +221,7 @@ class MainWindow(QMainWindow): table.removeRow(row) table.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents) + ## Mark the reading as done def doneReading(self): table, row = self.getReadingTableAndRow() if table.item(row, 6).text().compare(QString(self.trUtf8("Not done"))) == 0: @@ -219,11 +242,13 @@ class MainWindow(QMainWindow): bookIsbn = book.getIsbn() updateReadingDone(week, courseCode, bookIsbn, True) + ## Return the reading table and its current row def getReadingTableAndRow(self): table = self.reading.readingTable row = table.currentRow() return table, row + ## Return the week, course and book of the current reading def getWeekCourseAndBook(self): table, row = self.getReadingTableAndRow() weekItem = table.item(row, 0) @@ -235,10 +260,14 @@ class MainWindow(QMainWindow): book = getBookWithTitleFromDB(bookItem.text()) return week, courseCode, book.getIsbn() + # Schedule + + ## Open the Add Lesson dialog def addLesson(self): - self.asdlg = AddScheduleDlg() - self.asdlg.show() + self.sdlg = ScheduleDlg() + self.sdlg.show() + ## Delete the lesson from the database and the table def deleteLesson(self): table, row, column = self.getScheduleTableRowAndColumn() day, course, time = self.getDayCourseAndTime() @@ -246,6 +275,14 @@ class MainWindow(QMainWindow): removeLessonFromDB(day, course, time) table.setItem(row, column, QTableWidgetItem()) + ## 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 def getDayCourseAndTime(self): table, row, column = self.getScheduleTableRowAndColumn() item = table.item(row, column) @@ -257,32 +294,37 @@ class MainWindow(QMainWindow): time = row + 8 return day, coursecode, time - def getScheduleTableRowAndColumn(self): - table = self.schedule.scheduleTable - row = table.currentRow() - column = table.currentColumn() - return table, row, column - + ## Return the day belonging to the specified column def getDayFromTable(self, column): return self.days[column] - def getLatestSemester(self, semesters): - if len(semesters) == 0: - return None - global termList - max = semesters[0] - for s in semesters: - if s.getYear() > max.getYear(): - max = s - if s.getYear() == max.getYear(): - if s.getTerm() == self.trUtf8("Autumn"): - max = s - return max + # Course - def getTermList(self): - global termList - return termList + ## 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 + + ## Make and open the About dialog def helpAbout(self): QMessageBox.about(self, "About %s" % self.title, u""" %s v %s @@ -292,34 +334,24 @@ class MainWindow(QMainWindow):

Python %s - Qt %s - PyQt %s on %s

Developer: Tiril Anette Langfeldt Rødland, tirilane@pvv.ntnu.no """ % (self.title, __version__, platform.python_version(), QT_VERSION_STR, PYQT_VERSION_STR, platform.system())) - - def addActions(self, target, actions): - for action in actions: - if action is None: - target.addSeparator() - else: - target.addAction(action) - - def changeDay(self): - global chosenDay - chosenDay = self.calendar + ## 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() - def filePrint(self): - pass - + ## Updates the File menu 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 + ## Create and return the action specified by the input 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: @@ -336,13 +368,15 @@ class MainWindow(QMainWindow): action.setCheckable(True) return action - def showCalendar(self): - if self.calendarDockWidget.isVisible(): - self.removeDockWidget(self.calendarDockWidget) - else: - self.addDockWidget(Qt.BottomDockWidgetArea, self.calendarDockWidget) - self.calendarDockWidget.setVisible(True) - + ## 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 def setMainWindowTitle(self): global semester if semester: @@ -350,76 +384,243 @@ class MainWindow(QMainWindow): else: self.setWindowTitle(self.title) + ## Load the assignments, readings and schedule from the specified semester def load(self, semester): self.assignment.updateTable(semester) self.reading.updateTable(semester) self.schedule.updateTable(semester) +### 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")) + + # Widgets + self.termEdit = QComboBox() + self.yearEdit = QSpinBox() + self.termEdit.addItems(getMain().getTermList()) + self.yearEdit.setRange(2000, 2050) + self.yearEdit.setSingleStep(1) + self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) + + # Layout + 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")) + + ## Accept the dialog and add the specified semester + def accept(self): + term = unicode(self.termEdit.currentText()) + year = self.yearEdit.value() + global semester + semester = SemesterModel(term, year) + addNewSemesterToDB(term, year) + 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() + layout.addWidget(self.buttonBox) + 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")) + + ## 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 class AssignmentTab(QWidget): + ## Initialize the Assignment tab def __init__(self, parent=None): super(AssignmentTab, self).__init__(parent) + # Widgets self.addAssignmentButton = QPushButton("Add assignment") self.completeAssignmentBox = QComboBox() 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) + # Make the table self.makeTable() + # Layout 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) + ## Make an empty assignment table def makeTable(self, current=None): self.assignmentTable = QTableWidget(0, 5, self) self.assignmentTable.clear() + 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) self.assignmentTable.setAlternatingRowColors(True) self.assignmentTable.setEditTriggers(QAbstractItemView.NoEditTriggers) self.assignmentTable.setSelectionBehavior(QAbstractItemView.SelectRows) self.assignmentTable.setSelectionMode(QAbstractItemView.SingleSelection) - selected = None + + selected = None + ## Add the assignments of the semester to the table def updateTable(self, semester, current=None): self.assignments = getAssignmentsFromDB(semester) rows = len(self.assignments) self.assignmentTable.setRowCount(rows) + for row in range(rows): self.addAssignmentToTable(row) - self.assignmentTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents) + + self.assignmentTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents) self.assignmentTable.sortItems(0, Qt.AscendingOrder) + ## Add new assignment to Table def addAssignmentToTable(self, row, assignment=None): if assignment == None: assignment = self.assignments[row] - complete = QString(assignment.getComplete()) + + complete = QString(assignment.getComplete()) brush = self.makeBrush(complete) - self.assignmentTable.setItem(row, 0, QTableWidgetItem(QString(assignment.getDate().toString("yyyy-MM-dd hh:mm, ddd")))) + + self.assignmentTable.setItem(row, 0, QTableWidgetItem(QString(assignment.getDate().toString("yyyy-MM-dd hh:mm, ddd")))) 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()))) - completeItem = QTableWidgetItem(complete) + + completeItem = QTableWidgetItem(complete) completeItem.setBackground(brush) self.assignmentTable.setItem(row, 4, completeItem) + ## Set the right brush and color for the assignment, depending on level of completion def makeBrush(self, complete): brush = QBrush(Qt.NoBrush) if complete.compare(QString(self.trUtf8("Available"))) == 0: @@ -445,17 +646,21 @@ class AssignmentTab(QWidget): return brush +### The Add Assignment dialog class AssignmentDlg(QDialog): + ## Initialize the Add Assignment dialog def __init__(self, parent=None): super(AssignmentDlg, self).__init__(parent) + # Labels self.dateLabel = QLabel(self.trUtf8("&Date")) self.courseLabel = QLabel(self.trUtf8("&Course")) self.numberLabel = QLabel(self.trUtf8("&Number")) self.descriptionLabel = QLabel(self.trUtf8("De&scription")) self.completeLabel = QLabel(self.trUtf8("&Complete")) + # Widgets self.dateEdit = QLineEdit("DD.MM.YYYY HH:MM") self.dateEdit.setSelection(0, 16) self.courseEdit = QComboBox() @@ -466,13 +671,16 @@ class AssignmentDlg(QDialog): self.completeEdit = QComboBox() 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) + self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) + # Buddies self.dateLabel.setBuddy(self.dateEdit) self.courseLabel.setBuddy(self.courseEdit) self.numberLabel.setBuddy(self.numberEdit) self.descriptionLabel.setBuddy(self.descriptionEdit) self.completeLabel.setBuddy(self.completeEdit) + # Layout self.layout = QGridLayout() self.layout.addWidget(self.dateLabel, 0, 0) self.layout.addWidget(self.courseLabel, 1, 0) @@ -484,8 +692,17 @@ class AssignmentDlg(QDialog): self.layout.addWidget(self.numberEdit, 2, 1) self.layout.addWidget(self.descriptionEdit, 3, 1) self.layout.addWidget(self.completeEdit, 4, 1) + self.layout.addWidget(self.buttonBox, 5, 0, 1, 2) self.setLayout(self.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("Add new assignment")) + + ## Return an array with the values of the widgets def getValues(self): assignment = [] assignment.append(unicode(self.dateEdit.text())) @@ -495,21 +712,7 @@ class AssignmentDlg(QDialog): assignment.append(unicode(self.completeEdit.currentText())) return assignment - -class AddAssignmentDlg(AssignmentDlg): - - def __init__(self, parent=None): - super(AddAssignmentDlg, self).__init__(parent) - - buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) - - self.layout.addWidget(buttonBox, 5, 0, 1, 2) - - self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()")) - self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) - - self.setWindowTitle(self.trUtf8("Add new assignment")) - + ## Accept the dialog and add the specified assignment def accept(self): assignmentList = self.getValues() dateString = assignmentList[0] @@ -528,7 +731,6 @@ class AddAssignmentDlg(AssignmentDlg): 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) course = getCourseFromDB(getCourseCode(courseFull)) @@ -546,35 +748,7 @@ class AddAssignmentDlg(AssignmentDlg): self.close() -class EditAssignmentDlg(AssignmentDlg): - - def __init__(self, parent=None): - super(EditAssignmentDlg, self).__init__(parent) - self.setAttribute(Qt.WA_DeleteOnClose) - - buttonBox = QDialogButtonBox(QDialogButtonBox.Apply|QDialogButtonBox.Close) - - self.layout.addWidget(buttonBox, 8, 0, 1, 2) - - self.connect(buttonBox.button(QDialogButtonBox.Apply), SIGNAL("clicked()"), self.apply) - self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) - - self.setWindowTitle(self.trUtf8("Edit assignment")) - - assignment = self.getValues() - self.dateEdit.setText(assignment[0]) -# self.courseEdit.setText(assignment[1]) - self.numberEdit.setValue(assignment[2]) - self.descriptionEdit.setText(assignment[3]) -# self.completeEdit.setText(assignment[4]) - - def apply(self): - assignmentList = self.getValues() - - -# updateAssignment(assignment, assignmentList[0], assignmentList[1], assignmentList[2], assignmentList[3], assignmentList[4]) - - +### The assignment model class AssignmentModel(): date = None @@ -583,6 +757,7 @@ class AssignmentModel(): description = "" complete = "" + ## Initialize the assignment model def __init__(self, date, course, number, description, complete): self.date = date self.course = course @@ -590,61 +765,54 @@ class AssignmentModel(): self.description = description self.complete = complete + ## Return the date the assignment is due def getDate(self): return self.date - def setDate(self, date): - self.date = date - + ## Return the course the assignment is given in def getCourse(self): return self.course - def setCourse(self, course): - self.course = course - + ## Return the number of the assignment def getNumber(self): return self.number - - def setNumber(self, number): - self.number = number + ## Return the description of the assignment def getDescription(self): return self.description - def setDescription(self, description): - self.description = description - + ## Return the level of completion for the assignment def getComplete(self): return self.complete - - def setComplete(self, complete): - self.complete = complete +### The contents of the Reading tab class ReadingTab(QWidget): + ## Initialize the Reading tab def __init__(self, parent=None): super(ReadingTab, self).__init__(parent) + # Widgets self.addReadingButton = QPushButton(self.trUtf8("Add pages to read")) self.deleteReadingButton = QPushButton(self.trUtf8("Delete pages")) self.readingDoneButton = QPushButton(self.trUtf8("Done")) + # Make the table self.makeTable() + # Layout 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) + ## Make an empty reading table def makeTable(self, current=None): self.readingTable = QTableWidget(0, 7, self) self.readingTable.clear() @@ -663,6 +831,7 @@ class ReadingTab(QWidget): self.readingTable.setSelectionMode(QAbstractItemView.SingleSelection) selected = None + ## Add the readings of the semester to the table def updateTable(self, semester): self.readings = getReadingsFromDB(semester) rows = len(self.readings) @@ -672,6 +841,7 @@ class ReadingTab(QWidget): self.readingTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents) self.readingTable.sortItems(0, Qt.AscendingOrder) + ## Add a new reading to the table def addReadingToTable(self, row, reading=None): if reading == None: reading = self.readings[row] @@ -692,22 +862,27 @@ class ReadingTab(QWidget): self.readingTable.setItem(row, 3, QTableWidgetItem(QString(reading.getChapter()))) self.readingTable.setItem(row, 4, QTableWidgetItem(QString(reading.getPages()))) self.readingTable.setItem(row, 5, QTableWidgetItem(QString("%i" % reading.getNumberOfPages()))) - item = QTableWidgetItem(QString(doneString)) + + item = QTableWidgetItem(QString(doneString)) item.setBackground(brush) self.readingTable.setItem(row, 6, item) +### The Add Reading dialog class ReadingDlg(QDialog): + ## Initialize the Add Reading dialog def __init__(self, parent=None): super(ReadingDlg, self).__init__(parent) + # Labels 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")) + # Widgets self.weekEdit = QSpinBox() self.weekEdit.setRange(1, 52) self.courseEdit = QComboBox() @@ -724,13 +899,16 @@ class ReadingDlg(QDialog): self.bookEdit.addItems(booksStringList) self.chapterEdit = QLineEdit() self.pagesEdit = QLineEdit() + self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) + # Buddies 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) + # Layout self.layout = QGridLayout() self.layout.addWidget(self.weekLabel, 0, 0) self.layout.addWidget(self.courseLabel, 1, 0) @@ -742,23 +920,17 @@ class ReadingDlg(QDialog): self.layout.addWidget(self.bookEdit, 2, 1) self.layout.addWidget(self.chapterEdit, 3, 1) self.layout.addWidget(self.pagesEdit, 4, 1) + self.layout.addWidget(self.buttonBox, 5, 0, 1, 2) self.setLayout(self.layout) - -class AddReadingDlg(ReadingDlg): - - def __init__(self, parent=None): - super(AddReadingDlg, self).__init__(parent) - - buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) - - self.layout.addWidget(buttonBox, 5, 0, 1, 2) - - self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()")) - self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) - + # 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("Add new reading")) + ## Accept the dialog and add the specified reading def accept(self): week = unicode(self.weekEdit.value()) courseFull = unicode(self.courseEdit.currentText()) @@ -782,25 +954,7 @@ class AddReadingDlg(ReadingDlg): table.sortItems(0, Qt.AscendingOrder) -class EditReadingDlg(ReadingDlg): - - def __init__(self, parent=None): - super(EditReadingDlg, self).__init__(parent) - self.setAttribute(Qt.WA_DeleteOnClose) - - buttonBox = QDialogButtonBox(QDialogButtonBox.Apply|QDialogButtonBox.Close) - - self.layout.addWidget(buttonBox, 5, 0, 1, 2) - - self.connect(buttonBox.button(QDialogButtonBox.Apply), SIGNAL("clicked()"), self.apply) - self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) - - self.setWindowTitle(self.trUtf8("Edit reading")) - - def apply(self): - pass - - +### The reading model class ReadingModel(): week = 0 @@ -811,6 +965,7 @@ class ReadingModel(): numberOfPages = 0 done = False + ## Initialize the reading model def __init__(self, week, course, book, chapter, pages, done=False): self.week = week self.course = course @@ -820,36 +975,27 @@ class ReadingModel(): self.numberOfPages = self.getNumberOfPages() self.done = done + ## Return the week the reading shall be done def getWeek(self): return self.week - - def setWeek(self, week): - self.week = week + ## Return the course the reading is in def getCourse(self): return self.course - def setCourse(self, course): - self.course = course - + ## Return the book to be read in def getBook(self): return self.book - def setBook(self, book): - self.book = book - + ## Return the chapter to be read def getChapter(self): return self.chapter - def setChapter(self, chapter): - self.chapter = chapter - + ## Return the pages to be read def getPages(self): return self.pages - def setPages(self, pages): - self.pages = pages - + ## Return the number of pages to be read def getNumberOfPages(self): pages = self.getPages() pagesArray = pages.split(",") @@ -862,37 +1008,39 @@ class ReadingModel(): sum += int(n[1]) - int(n[0]) return sum + ## Return whether the reading has been done or not def getDone(self): return self.done - def setDone(self, done): - self.done = done - +### The contents of the Schedule tab class ScheduleTab(QWidget): + ## Initialize the Schedule tab def __init__(self, parent=None): super(ScheduleTab, self).__init__(parent) + # Widgets self.addScheduleButton = QPushButton("Add lesson") self.deleteScheduleButton = QPushButton("Delete lesson") + # Make the table self.makeTable() + # Layout self.vlayout = QVBoxLayout() self.hlayout = QHBoxLayout() - self.hlayout.addWidget(self.addScheduleButton) self.hlayout.addWidget(self.deleteScheduleButton) self.hlayout.addStretch() - self.vlayout.addWidget(self.scheduleTable) self.vlayout.addLayout(self.hlayout) - self.setLayout(self.vlayout) + ## Make an empty schedule table def makeTable(self, current=None): self.scheduleTable = QTableWidget(12, 5, self) + self.scheduleTable.setRowCount(11) self.scheduleTable.clear() self.scheduleHorizontalHeaderList = QStringList() self.scheduleHorizontalHeaderList.append(self.trUtf8("Monday")) @@ -905,31 +1053,42 @@ class ScheduleTab(QWidget): self.scheduleVerticalHeaderList.append(self.trUtf8("%i" % i)) self.scheduleTable.setHorizontalHeaderLabels(self.scheduleHorizontalHeaderList) self.scheduleTable.setVerticalHeaderLabels(self.scheduleVerticalHeaderList) - self.scheduleTable.setAlternatingRowColors(True) + self.scheduleTable.setAlternatingRowColors(False) self.scheduleTable.setEditTriggers(QAbstractItemView.NoEditTriggers) self.scheduleTable.setSelectionBehavior(QAbstractItemView.SelectItems) self.scheduleTable.setSelectionMode(QAbstractItemView.SingleSelection) selected = None + ## Add the lessons of the semester to the table def updateTable(self, semester): self.schedule = getLessonsFromDB(semester) rows = len(self.schedule) - self.scheduleTable.setRowCount(rows) for l in range(rows): self.addLessonToTable(self.schedule[l]) self.scheduleTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents) self.scheduleTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents) + ## Add a new lesson to the table def addLessonToTable(self, lesson): row = lesson.getTime() - 8 column = self.getColumn(lesson.getDay()) course = lesson.getCourse().getFull() type = lesson.getType() room = lesson.getRoom() - item = QTableWidgetItem(QString("%s\n%s\n%s" % (course, type, room))) - item.setBackground(self.getBackground(QString("%s" % type), lesson.getCourse())) - self.scheduleTable.setItem(row, column, item) + olditem = self.scheduleTable.item(row, column) + newtext = "%s\n%s\n%s" % (course, type, room) + if olditem: + oldtext = olditem.text() + text = QString(oldtext + "\n" + newtext) + collision = True + else: + text = QString(newtext) + collision = False + item = QTableWidgetItem(text) + item.setBackground(self.getBackground(QString("%s" % type), lesson.getCourse(), collision)) + self.scheduleTable.setItem(row, column, item) + ## Return the column specified by the day def getColumn(self, dayString): day = QString("%s" % dayString) if day.compare(QString(self.trUtf8("Monday"))) == 0: @@ -945,31 +1104,37 @@ class ScheduleTab(QWidget): else: return -1 - def getBackground(self, type, course): + ## Set the right brush and color for the lesson, depending on type of lesson + def getBackground(self, type, course, collision): if type.compare(QString(self.trUtf8("Lecture"))) == 0: - brush = QBrush(Qt.HorPattern) + brush = QBrush(Qt.SolidPattern) elif type.compare(QString(self.trUtf8("Assignment lecture"))) == 0: - brush = QBrush(Qt.CrossPattern) + brush = QBrush(Qt.Dense2Pattern) elif type.compare(QString(self.trUtf8("Assignment help"))) == 0: - brush = QBrush(Qt.VerPattern) + brush = QBrush(Qt.Dense3Pattern) elif type.compare(QString(self.trUtf8("Lab"))) == 0: - brush = QBrush(Qt.BDiagPattern) + brush = QBrush(Qt.Dense1Pattern) elif type.compare(QString(self.trUtf8("Seminar"))) == 0: - brush = QBrush(Qt.FDiagPattern) + brush = QBrush(Qt.Dense4Pattern) elif type.compare(QString(self.trUtf8("Other"))) == 0: - brush = QBrush(Qt.DiagCrossPattern) + brush = QBrush(Qt.Dense5Pattern) else: - brush = QBrush(Qt.NoBrush) - brush.setColor(course.getColor()) - + brush = QBrush(Qt.NoBrush) + if not collision: + brush.setColor(course.getColor()) + else: + brush.setColor(Qt.red) return brush +### The Add Lesson dialog class ScheduleDlg(QDialog): + ## Initialize the Add Lesson dialog def __init__(self, parent=None): super(ScheduleDlg, self).__init__(parent) + # Labels self.dayLabel = QLabel(self.trUtf8("&Day")) self.fromLabel = QLabel(self.trUtf8("&From")) self.toLabel = QLabel(self.trUtf8("&To")) @@ -977,6 +1142,7 @@ class ScheduleDlg(QDialog): self.typeLabel = QLabel(self.trUtf8("Ty&pe")) self.roomLabel = QLabel(self.trUtf8("&Room")) + # Widgets self.dayEdit = QComboBox() self.dayEdit.addItems(getMain().days) self.fromEdit = QSpinBox() @@ -995,7 +1161,9 @@ class ScheduleDlg(QDialog): 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) self.roomEdit = QLineEdit() + self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) + # Buddies self.dayLabel.setBuddy(self.dayEdit) self.fromLabel.setBuddy(self.fromEdit) self.toLabel.setBuddy(self.toEdit) @@ -1003,6 +1171,7 @@ class ScheduleDlg(QDialog): self.typeLabel.setBuddy(self.typeEdit) self.roomLabel.setBuddy(self.roomEdit) + # Layout self.layout = QGridLayout() self.layout.addWidget(self.dayLabel, 0, 0) self.layout.addWidget(self.fromLabel, 1, 0) @@ -1016,23 +1185,17 @@ class ScheduleDlg(QDialog): self.layout.addWidget(self.courseEdit, 3, 1) self.layout.addWidget(self.typeEdit, 4, 1) self.layout.addWidget(self.roomEdit, 5, 1) + self.layout.addWidget(self.buttonBox, 6, 0, 1, 2) self.setLayout(self.layout) - -class AddScheduleDlg(ScheduleDlg): - - def __init__(self, parent=None): - super(AddScheduleDlg, self).__init__(parent) - - buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) - - self.layout.addWidget(buttonBox, 6, 0, 1, 2) - + # Connect statements self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()")) self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) + # Set the title self.setWindowTitle(self.trUtf8("Add new lesson")) + ## Accept the dialog and add the specified lesson def accept(self): day = unicode(self.dayEdit.currentText()) fromtime = self.fromEdit.value() @@ -1051,24 +1214,7 @@ class AddScheduleDlg(ScheduleDlg): self.close() -class EditScheduleDlg(ScheduleDlg): - - def __init__(self, parent=None): - super(EditScheduleDlg, self).__init__(parent) - - buttonBox = QDialogButtonBox(QDialogButtonBox.Apply|QDialogButtonBox.Close) - - self.layout.addWidget(buttonBox, 6, 0, 1, 2) - - self.connect(buttonBox.button(QDialogButtonBox.Apply), SIGNAL("clicked()"), self.apply) - self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) - - self.setWindowTitle(self.trUtf8("Edit lesson")) - - def apply(self): - pass - - +### The schedule model class ScheduleModel(): day = "" @@ -1077,6 +1223,7 @@ class ScheduleModel(): type = "" room = "" + ## Initialize the schedule model def __init__(self, day, time, course, type, room): self.day = day self.time = time @@ -1084,65 +1231,60 @@ class ScheduleModel(): self.type = type self.room = room + ## Return the day of the lesson def getDay(self): return self.day - - def setDay(self, day): - self.day = day - + + ## Return the start time of the lesson def getTime(self): return self.time - def setTime(self, fromTime): - self.time = time - + ## Return the course of the lesson def getCourse(self): return self.course - def setCourse(self, course): - self.course = course - + ## Return the type of the lesson def getType(self): return self.type - def setType(self, type): - self.type = type - + ## Return the room the lesson is in def getRoom(self): return self.room - def setRoom(self, room): - self.room = room - +### The Add Course dialog class CourseDlg(QDialog): + ## Initialize the Add Course dialog def __init__(self, parent=None): super(CourseDlg, self).__init__(parent) + # The books self.books = [] + # Labels 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")) + # Widgets self.codeEdit = QLineEdit() self.titleEdit = QLineEdit() self.shortEdit = QLineEdit() self.booksEdit = QListWidget() self.updateList() self.booksEdit.setSelectionMode(QAbstractItemView.ExtendedSelection) - self.newBook = QPushButton("Add new book") - self.connect(self.newBook, SIGNAL("pressed()"), getMain().addBook) - self.connect(getMain(), SIGNAL("newBook"), self.updateList) + self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) + # Buddies self.codeLabel.setBuddy(self.codeEdit) self.titleLabel.setBuddy(self.titleEdit) self.shortLabel.setBuddy(self.shortEdit) self.booksLabel.setBuddy(self.booksEdit) + # Layout self.layout = QGridLayout() self.layout.addWidget(self.codeLabel, 0, 0) self.layout.addWidget(self.titleLabel, 1, 0) @@ -1153,8 +1295,19 @@ class CourseDlg(QDialog): self.layout.addWidget(self.shortEdit, 2, 1) self.layout.addWidget(self.booksEdit, 3, 1) self.layout.addWidget(self.newBook, 4, 0) + self.layout.addWidget(self.buttonBox, 4, 1, 1, 2) self.setLayout(self.layout) + # Connect statements + self.connect(self.newBook, SIGNAL("pressed()"), getMain().addBook) + 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()")) + + # Set the title + self.setWindowTitle(self.trUtf8("Add new course")) + + ## Update the book list def updateList(self): self.booksEdit.clear() booksDB = getBooksFromDB() @@ -1164,25 +1317,12 @@ class CourseDlg(QDialog): self.booksEdit.addItems(booksStringList) self.booksEdit.sortItems() + ## Add a new book to the course def addNewBookToCourse(self): book = getBookWithTitleFromDB(booktitle) self.books.append(book) - -class AddCourseDlg(CourseDlg): - - def __init__(self, parent=None): - super(AddCourseDlg, self).__init__(parent) - - buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) - - self.layout.addWidget(buttonBox, 4, 1, 1, 2) - - self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()")) - self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) - - self.setWindowTitle(self.trUtf8("Add new course")) - + ## Accept the dialog and add the specified course def accept(self): courseCode = unicode(self.codeEdit.text()) courseTitle = unicode(self.titleEdit.text()) @@ -1201,6 +1341,7 @@ class AddCourseDlg(CourseDlg): self.close() +### The course model class CourseModel(): code = "" @@ -1209,6 +1350,7 @@ class CourseModel(): full = "" books = [] + ## Initialize the course model def __init__(self, code, title, short, color, books): self.code = code self.title = title @@ -1217,52 +1359,67 @@ class CourseModel(): self.color = color self.books = books + ## Return the code of the course def getCode(self): return self.code + ## Return the title of the course def getTitle(self): return self.title + ## Return the short form of the course def getShort(self): return self.short + ## Set the full form of the course def setFull(self, code, title): self.full = code + ' ' + title + ## Return the full form of the course def getFull(self): return self.full + ## Return the color of the course def getColor(self): return self.color + ## Add a book to the course def addBook(self, book): books.append(book) + ## Return the books of the course def getBooks(self): return self.books +### The Add Book dialog class BookDlg(QDialog): + ## Initialize the Add Book dialog def __init__(self, parent=None): super(BookDlg, self).__init__(parent) + # Labels self.titleLabel = QLabel(self.trUtf8("&Title")) self.authorLabel = QLabel(self.trUtf8("&Author")) self.editionLabel = QLabel(self.trUtf8("&Edition")) self.isbnLabel = QLabel(self.trUtf8("&ISBN")) + # Widgets self.titleEdit = QLineEdit() self.authorEdit = QLineEdit() self.editionEdit = QSpinBox() self.editionEdit.setRange(1, 50) self.isbnEdit = QLineEdit() + self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) + # Buddies self.titleLabel.setBuddy(self.titleEdit) self.authorLabel.setBuddy(self.authorEdit) self.editionLabel.setBuddy(self.editionEdit) self.isbnLabel.setBuddy(self.isbnEdit) + # Layout self.layout = QGridLayout() self.layout.addWidget(self.titleLabel, 0, 0) self.layout.addWidget(self.authorLabel, 1, 0) @@ -1272,23 +1429,17 @@ class BookDlg(QDialog): 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) - -class AddBookDlg(BookDlg): - - def __init__(self, parent=None): - super(AddBookDlg, self).__init__(parent) - - buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) - - self.layout.addWidget(buttonBox, 4, 0, 1, 2) - - self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()")) - self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) + # 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("Add new book")) + # Accept the dialog and add the specified book def accept(self): bookTitle = unicode(self.titleEdit.text()) bookAuthor = unicode(self.authorEdit.text()) @@ -1299,6 +1450,7 @@ class AddBookDlg(BookDlg): self.close() +### The book model class BookModel(): title = "" @@ -1306,214 +1458,41 @@ class BookModel(): edition = 0 isbn = "" + ## Initialize the book model def __init__(self, isbn, title, author, edition): self.isbn = isbn self.title = title self.author = author self.edition = edition + # Return the ISBN number of the book def getIsbn(self): return self.isbn + # Return the title of the book def getTitle(self): return self.title + # Return the author(s) of the book def getAuthor(self): return self.author + # Return the edition of the book def getEdition(self): return self.edition -class SemesterDlg(QDialog): - - def __init__(self, parent=None): - super(SemesterDlg, self).__init__(parent) - - self.oldLabel = QLabel(self.trUtf8("Earlier semesters:")) - self.newLabel = QLabel(self.trUtf8("New semester:")) - self.newTermLabel = QLabel(self.trUtf8("Term:")) - self.newYearLabel = QLabel(self.trUtf8("Year:")) - - global semester, semesters, main - - self.oldEdit = QListWidget() - self.oldEdit.addItems(semesters) - self.newTermEdit = QComboBox() - self.newTermEdit.addItems(getMain().getTermList()) - self.newYearEdit = QSpinBox() - self.newYearEdit.setRange(2000, 2050) - self.newYearEdit.setSingleStep(1) - - self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) - - layout = QGridLayout() - layout.addWidget(self.oldLabel, 0, 0) - layout.addWidget(self.oldEdit, 0, 1, 1, 2) - layout.addWidget(self.newLabel, 1, 0) - layout.addWidget(self.newTermLabel, 1, 1) - layout.addWidget(self.newTermEdit, 2, 1) - layout.addWidget(self.newYearLabel, 1, 2) - layout.addWidget(self.newYearEdit, 2, 2) - layout.addWidget(self.buttonBox, 3, 0, 1, 3) - layout.setColumnStretch(0, 0) - layout.setColumnStretch(1, 1) - layout.setColumnStretch(2, 1) - self.setLayout(layout) - - self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()")) - self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) - - self.setWindowTitle(self.trUtf8("Choose a semester or create a new")) - - def accept(self): - pass - - -class NewSemesterDlg(QDialog): - - def __init__(self, parent=None): - super(NewSemesterDlg, self).__init__(parent) - - self.termLabel = QLabel(self.trUtf8("Term")) - self.yearLabel = QLabel(self.trUtf8("Year")) - self.termEdit = QComboBox() - self.yearEdit = QSpinBox() - self.termEdit.addItems(getMain().getTermList()) - self.yearEdit.setRange(2000, 2050) - self.yearEdit.setSingleStep(1) - self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) - - 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) - - self.connect(self.buttonBox, SIGNAL("accepted()"), self, SLOT("accept()")) - self.connect(self.buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) - - self.setWindowTitle(self.trUtf8("New semester")) - - def accept(self): - term = unicode(self.termEdit.currentText()) - year = self.yearEdit.value() - global semester - semester = SemesterModel(term, year) - addNewSemesterToDB(term, year) - getMain().load(semester) - getMain().setMainWindowTitle() - self.close() - - -class OpenSemesterDlg(QDialog): - - def __init__(self, parent=None): - super(OpenSemesterDlg, self).__init__(parent) - - 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 = QVBoxLayout() - layout.addWidget(self.semesterList) - layout.addStretch() - layout.addWidget(self.buttonBox) - self.setLayout(layout) - - self.connect(self.buttonBox, SIGNAL("accepted()"), self, SLOT("accept()")) - self.connect(self.buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) - - self.setWindowTitle(self.trUtf8("Open 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)) - print semester.getAssignments() - print semester.getReadings() - print semester.getLessons() - getMain().load(semester) - getMain().setMainWindowTitle() - self.close() - - -class SemesterModel(): - - term = "" - year = 0 - assignments = [] - readings = [] - lessons = [] - - def __init__(self, term, year): - self.term = term - self.year = year - - def getTerm(self): - return self.term - - def getYear(self): - return self.year - - def addAssignment(self, assignment): - self.assignments.append(assignment) - - def setAssignments(self, assignments): - self.assignments = assignments - - def getAssignments(self): - return self.assignments - - def addReading(self, reading): - self.readings.append(reading) - - def setReadings(self, readings): - self.readings = readings - - def getReadings(self): - return self.readings - - def addLesson(self, lesson): - self.lessons.append(lesson) - - def setLessons(self, lessons): - self.lessons = lessons - - def getLessons(self): - return self.lessons - - -class CalendarTab(QWidget): - - def __init__(self, parent=None): - super(CalendarTab, self).__init__(parent) - - calendar = QCalendarWidget() - calendar.setFirstDayOfWeek(Qt.Monday) - - layout = QVBoxLayout() - layout.addWidget(calendar) - self.setLayout(layout) +# Database +# Initialization +## Connect to the database and return the cursor and connection def initDB(): conn = sqlite.connect('egon.db') curs = conn.cursor() return curs, conn +## Initialize the tables def initNewDB(): cursor, conn = initDB() initSemesterDB(cursor) @@ -1525,6 +1504,9 @@ def initNewDB(): initCourseUsesBook(cursor) exitDB(conn) +# Create the database + +## Create the Semester table def initSemesterDB(cursor): cursor.execute(''' CREATE TABLE Semester ( @@ -1534,6 +1516,7 @@ def initSemesterDB(cursor): ) ''') +## Create the Assignment table def initAssignmentDB(cursor): cursor.execute(''' CREATE TABLE Assignment ( @@ -1548,6 +1531,7 @@ def initAssignmentDB(cursor): ) ''') +## Create the Reading table def initReadingDB(cursor): cursor.execute(''' CREATE TABLE Reading ( @@ -1563,6 +1547,7 @@ def initReadingDB(cursor): ) ''') +## Create the Lesson table def initScheduleDB(cursor): cursor.execute(''' CREATE TABLE Lesson ( @@ -1577,16 +1562,7 @@ def initScheduleDB(cursor): ) ''') -def initBookDB(cursor): - cursor.execute(''' - CREATE TABLE Book ( - isbn TEXT PRIMARY KEY, - title TEXT, - author TEXT, - edition INTEGER - ) - ''') - +## Create the Course table def initCourseDB(cursor): cursor.execute(''' CREATE TABLE Course ( @@ -1599,6 +1575,18 @@ def initCourseDB(cursor): ) ''') +## 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 def initCourseUsesBook(cursor): cursor.execute(''' CREATE TABLE CourseUsesBook ( @@ -1608,6 +1596,9 @@ def initCourseUsesBook(cursor): ) ''') +# Add things to the database + +## Add new semester to the database def addNewSemesterToDB(term, year): cursor, conn = initDB() @@ -1618,6 +1609,7 @@ def addNewSemesterToDB(term, year): exitDB(conn) +## Add new assignment to the database def addNewAssignmentToDB(semester, datetime, course, number, description, complete): cursor, conn = initDB() @@ -1637,26 +1629,35 @@ def addNewAssignmentToDB(semester, datetime, course, number, description, comple exitDB(conn) +## Add new reading to the database def addNewReadingToDB(semester, week, course, book, chapter, pages, done): cursor, conn = initDB() + term = "%s" % semester.getTerm() + year = semester.getYear() + cursor.execute(''' INSERT INTO Reading (week, course, book, chapter, pages, done, term, year) VALUES (?, ?, ?, ?, ?, ?, ?, ?) - ''', (week, course.getCode(), book.getIsbn(), chapter, pages, done, semester.getTerm(), semester.getYear())) + ''', (week, course.getCode(), book.getIsbn(), chapter, pages, done, term, year)) exitDB(conn) +## Add new lesson to the database def addNewLessonToDB(semester, day, time, course, type, room): cursor, conn = initDB() + term = "%s" % semester.getTerm() + year = semester.getYear() + cursor.execute(''' INSERT INTO Lesson (day, time, course, type, room, term, year) VALUES (?, ?, ?, ?, ?, ?, ?) - ''', (day, time, course.getCode(), type, room, semester.getTerm(), semester.getYear())) + ''', (day, time, course.getCode(), type, room, term, year)) exitDB(conn) +## Add new course to the database def addNewCourseToDB(code, title, short, color, books): cursor, conn = initDB() @@ -1677,6 +1678,7 @@ def addNewCourseToDB(code, title, short, color, books): exitDB(conn) +## Add new book to the database def addNewBookToDB(isbn, title, author, edition): cursor, conn = initDB() @@ -1687,6 +1689,26 @@ def addNewBookToDB(isbn, title, author, edition): exitDB(conn) +# 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 def getAssignmentsFromDB(semester): cursor, conn = initDB() @@ -1708,24 +1730,7 @@ def getAssignmentsFromDB(semester): return assignments -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 - +## Get the readings of the specified semester from the database def getReadingsFromDB(semester): cursor, conn = initDB() @@ -1747,6 +1752,7 @@ def getReadingsFromDB(semester): return readings +## Get the lessons of the specified semester from the database def getLessonsFromDB(semester): cursor, conn = initDB() @@ -1768,6 +1774,7 @@ def getLessonsFromDB(semester): return lessons +## Get all the courses from the database def getCoursesFromDB(): cursor, conn = initDB() @@ -1798,6 +1805,7 @@ def getCoursesFromDB(): return courses +## Get the course specified by the course code from the database def getCourseFromDB(courseCode): cursor, conn = initDB() @@ -1831,6 +1839,7 @@ def getCourseFromDB(courseCode): return course +## Get all the books from the database def getBooksFromDB(): cursor, conn = initDB() @@ -1847,6 +1856,7 @@ def getBooksFromDB(): return books +## Get the book specified by the ISBN number from the database def getBookFromDB(isbn): cursor, conn = initDB() @@ -1863,6 +1873,7 @@ def getBookFromDB(isbn): return book +## Get the book specified by the title from the database def getBookWithTitleFromDB(booktitle): cursor, conn = initDB() @@ -1881,22 +1892,9 @@ def getBookWithTitleFromDB(booktitle): return book -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 +# Update things in the database +## Update the completion level of the specified assignment def updateAssignmentCompletion(coursecode, num, completion): cursor, conn = initDB() @@ -1913,6 +1911,7 @@ def updateAssignmentCompletion(coursecode, num, completion): exitDB(conn) +## Update whether the specified reading is done or not def updateReadingDone(week, coursecode, bookisbn, done): cursor, conn = initDB() @@ -1929,6 +1928,9 @@ def updateReadingDone(week, coursecode, bookisbn, done): exitDB(conn) +# Remove things from the database + +## Remove the specified assignment from the database def removeAssignmentFromDB(coursecode, num): cursor, conn = initDB() @@ -1944,6 +1946,7 @@ def removeAssignmentFromDB(coursecode, num): exitDB(conn) +## Remove the specified reading from the database def removeReadingFromDB(week, coursecode, bookisbn): cursor, conn = initDB() @@ -1960,6 +1963,7 @@ def removeReadingFromDB(week, coursecode, bookisbn): exitDB(conn) +## Remove the specified lesson from the database def removeLessonFromDB(daystring, coursecode, fromTime): cursor, conn = initDB() @@ -1977,27 +1981,37 @@ def removeLessonFromDB(daystring, coursecode, fromTime): exitDB(conn) +# Exit the database + +## Commit and close the connection def exitDB(conn): conn.commit() conn.close() +# Course + +## Get the course code from the full name of the course def getCourseCode(courseFull): course = courseFull.split(' ') return course[0] +## Add a new course to the list of courses def addNewCourse(course): courses.append(course) addNewCourseToDB(course.getCode(), course.getTitle(), course.getShort(), course.getColor()) addNewCourseString(course) +## Return the list of courses def getCourses(): global courses return courses +## Add the course as a course string def addNewCourseString(course): global coursesString coursesString.append(course.getFull()) +## Make the courses string list and return it def makeCoursesString(): emptyCourses() cs = getCoursesFromDB() @@ -2007,23 +2021,37 @@ def makeCoursesString(): s = getCoursesString() return s +## Return the courses string list def getCoursesString(): global coursesString return coursesString +## 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 def addNewBook(book): books.append(book) addNewBookToDB(book.getISBN(), book.getTitle(), book.getAuthor(), book.getEdition(), book.getCourse()) addNewBookString(book) +## Return the list of books def getBooks(): global books return books +## Add the book as a book string def addNewBookString(book): global booksString booksString.append(book.getTitle()) +## Make the books string list and return it def makeBooksString(): emptyBooks() bs = getBooksFromDB() @@ -2033,32 +2061,55 @@ def makeBooksString(): s = getBooksString() return s +## Return the books string list def getBooksString(): global booksString return booksString -def emptyCourses(): - global courses - global coursesString - courses = [] - coursesString = [] - +## Empty the books list and the books string list def emptyBooks(): global books global booksString books = [] booksString = [] +# Color + +## Return a random color, RGB def getRandomColor(): return QColor(getRandomColorPart(), getRandomColorPart(), getRandomColorPart()) +## Return a random number between 0 and 255 def getRandomColorPart(): return random.random()*256 +# 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 def getMain(): global main return main +## Run the program def main(): app = QApplication(sys.argv) form = MainWindow()