#!/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
Copyright © 2008 Tiril Anette Langfeldt Rødland. Ingen rettigheter reservert.
Programmet kan brukes under GPL-lisens. Programmet kan endres og redistribueres under vilkårene gitt i GPL. Lisensen finnes her: http://www.gnu.org/licenses/gpl.html
Dette programmet er hovedsakelig for studenter, og kan brukes til å holde oversikt over øvinger, lesing og timeplan.
Python %s - Qt %s - PyQt %s på %s
Utvikler: Tiril Anette Langfeldt Rødland, tirilane@pvv.ntnu.no """ % (self.title, __version__, platform.python_version(), QT_VERSION_STR, PYQT_VERSION_STR, platform.system())) ## 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: 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 ## 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: self.setWindowTitle("%s : %s %i" % (self.title, semester.getTerm(), semester.getYear())) 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("Semester")) self.yearLabel = QLabel(self.trUtf8("År")) # 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("Nytt 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("Åpne 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(self.trUtf8("Ny øving")) self.completeAssignmentBox = QComboBox() self.deleteAssignmentButton = QPushButton(self.trUtf8("Slett øving")) completeTypes = [self.trUtf8("Ikke tilgjengelig"), self.trUtf8("Tilgjengelig"), self.trUtf8("Påbegynt"), self.trUtf8("Ferdig"), self.trUtf8("Levert"), self.trUtf8("Godkjent"), self.trUtf8("Underkjent")] 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("Dato")) self.assignmentHeaderList.append(self.trUtf8("Fag")) self.assignmentHeaderList.append(self.trUtf8("Nummer")) self.assignmentHeaderList.append(self.trUtf8("Beskrivelse")) self.assignmentHeaderList.append(self.trUtf8("Kompletthet")) 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 ## 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.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()) brush = self.makeBrush(complete) 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.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("Tilgjengelig"))) == 0: brush.setStyle(Qt.Dense7Pattern) brush.setColor(QColor(Qt.cyan)) elif complete.compare(QString(self.trUtf8("Påbegynt"))) == 0: brush.setStyle(Qt.Dense5Pattern) brush.setColor(QColor(Qt.cyan)) elif complete.compare(QString(self.trUtf8("Ferdig"))) == 0: brush.setStyle(Qt.Dense3Pattern) brush.setColor(QColor(Qt.cyan)) elif complete.compare(QString(self.trUtf8("Levert"))) == 0: brush.setStyle(Qt.Dense1Pattern) brush.setColor(QColor(Qt.cyan)) elif complete.compare(QString(self.trUtf8("Godkjent"))) == 0: brush.setStyle(Qt.SolidPattern) brush.setColor(QColor(Qt.green)) elif complete.compare(QString(self.trUtf8("Underkjent"))) == 0: brush.setStyle(Qt.SolidPattern) brush.setColor(QColor(Qt.red)) else: brush.setStyle(Qt.NoBrush) 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("&Dato")) self.courseLabel = QLabel(self.trUtf8("&Fag")) self.numberLabel = QLabel(self.trUtf8("&Nummer")) self.descriptionLabel = QLabel(self.trUtf8("&Beskrivelse")) self.completeLabel = QLabel(self.trUtf8("Kompletthet")) # Widgets self.dateEdit = QLineEdit("DD.MM.YYYY HH:MM") self.dateEdit.setSelection(0, 16) self.courseEdit = QComboBox() self.courseEdit.addItems(makeCoursesString()) self.numberEdit = QSpinBox() self.numberEdit.setRange(1, 20) self.descriptionEdit = QLineEdit() self.completeEdit = QComboBox() completeTypes = [self.trUtf8("Ikke tilgjengelig"), self.trUtf8("Tilgjengelig"), self.trUtf8("Påbegynt"), self.trUtf8("Ferdig"), self.trUtf8("Levert"), self.trUtf8("Godkjent"), self.trUtf8("Underkjent")] 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) self.layout.addWidget(self.numberLabel, 2, 0) 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) 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("Ny øving")) ## Return an array with the values of the widgets 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 ## Accept the dialog and add the specified assignment def accept(self): assignmentList = self.getValues() dateString = assignmentList[0] courseFull = assignmentList[1] number = assignmentList[2] description = assignmentList[3] complete = assignmentList[4] 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("Datoen har ikke et korrekt format."))) if len(dateString) <= 11: dateList = dateString.split('.') timeList = ['00', '00'] else: dateTime = dateString.split() dateList = dateTime[0].split('.') timeList = dateTime[1].split(':') if dateList[1] > '13': dateMessage = QErrorMessage() elif dateList[0] > '31': dateMessage = QErrorMessage() dateMessage.showMessage(QString(self.trUtf8("Dagen er ikke gyldig. Vennligst skriv inn en korrekt dato."))) elif timeList[0] > '23': dateMessage = QErrorMessage() dateMessage.showMessage(QString(self.trUtf8("Timen er ikke gyldig. Vennligst skriv inn et korrekt klokkeslett."))) elif timeList[1] > '59': dateMessage = QErrorMessage() dateMessage.showMessage(QString(self.trUtf8("Minuttene er ikke gyldig. Vennligst skriv inn et korrekt klokkeslett."))) 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("Datoen er ikke gyldig. Vennligst skriv inn en dato på formatet DD.MM.YYYY HH:MM."))) 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) self.close() ### The assignment model class AssignmentModel(): date = None course = None number = 0 description = "" complete = "" ## Initialize the assignment model def __init__(self, date, course, number, description, complete): self.date = date self.course = course self.number = number self.description = description self.complete = complete ## Return the date the assignment is due def getDate(self): return self.date ## Return the course the assignment is given in def getCourse(self): return self.course ## Return the number of the assignment def getNumber(self): return self.number ## Return the description of the assignment def getDescription(self): return self.description ## Return the level of completion for the assignment def getComplete(self): return self.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("Legg til leselekse")) self.deleteReadingButton = QPushButton(self.trUtf8("Slett leselekse")) self.readingDoneButton = QPushButton(self.trUtf8("Ferdig")) # 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() self.readingHeaderList = QStringList() self.readingHeaderList.append(self.trUtf8("Uke")) self.readingHeaderList.append(self.trUtf8("Fag")) self.readingHeaderList.append(self.trUtf8("Bok")) self.readingHeaderList.append(self.trUtf8("Kapittel")) self.readingHeaderList.append(self.trUtf8("Sider")) self.readingHeaderList.append(self.trUtf8("Antall sider")) self.readingHeaderList.append(self.trUtf8("Ferdig")) self.readingTable.setHorizontalHeaderLabels(self.readingHeaderList) self.readingTable.setAlternatingRowColors(True) self.readingTable.setEditTriggers(QAbstractItemView.NoEditTriggers) self.readingTable.setSelectionBehavior(QAbstractItemView.SelectRows) 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) self.readingTable.setRowCount(rows) for row in range(rows): self.addReadingToTable(row) 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] brush = QBrush(Qt.NoBrush) brush.setStyle(Qt.SolidPattern) if reading.getDone(): doneString = self.trUtf8("Ferdig") brush.setColor(Qt.green) else: doneString = self.trUtf8("Ikke ferdig") brush.setColor(Qt.red) self.readingTable.setItem(row, 0, QTableWidgetItem(QString("%02s" % reading.getWeek()))) self.readingTable.setItem(row, 1, QTableWidgetItem(QString(reading.getCourse().getFull()))) self.readingTable.setItem(row, 2, QTableWidgetItem(QString(reading.getBook().getTitle()))) 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.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("&Uke")) self.courseLabel = QLabel(self.trUtf8("&Fag")) self.bookLabel = QLabel(self.trUtf8("&Bok")) self.chapterLabel = QLabel(self.trUtf8("&Kapittel")) self.pagesLabel = QLabel(self.trUtf8("&Sider")) # Widgets 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) 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) 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) 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("Ny leselekse")) ## Accept the dialog and add the specified reading 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()) self.close() course = getCourseFromDB(getCourseCode(courseFull)) book = getBookWithTitleFromDB(bookTitle) global semester addNewReadingToDB(semester, week, course, book, chapter, pages, False) reading = ReadingModel(week, course, book, chapter, pages, False) table = getMain().reading.readingTable row = table.rowCount() table.insertRow(row) getMain().reading.addReadingToTable(row, reading) table.sortItems(0, Qt.AscendingOrder) ### The reading model class ReadingModel(): week = 0 course = None book = None chapter = "" pages = "" numberOfPages = 0 done = False ## Initialize the reading model 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 ## Return the week the reading shall be done def getWeek(self): return self.week ## Return the course the reading is in def getCourse(self): return self.course ## Return the book to be read in def getBook(self): return self.book ## Return the chapter to be read def getChapter(self): return self.chapter ## Return the pages to be read def getPages(self): return self.pages ## Return the number of pages to be read 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]) + 1 return sum ## Return whether the reading has been done or not def getDone(self): return self.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("Ny time") self.deleteScheduleButton = QPushButton("Slett time") # 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(12) self.scheduleTable.clear() self.updateHeaders() self.scheduleTable.setAlternatingRowColors(False) self.scheduleTable.setEditTriggers(QAbstractItemView.NoEditTriggers) self.scheduleTable.setSelectionBehavior(QAbstractItemView.SelectItems) self.scheduleTable.setSelectionMode(QAbstractItemView.SingleSelection) selected = None def updateHeaders(self): self.scheduleHorizontalHeaderList = QStringList() self.scheduleHorizontalHeaderList.append(self.trUtf8("Mandag")) self.scheduleHorizontalHeaderList.append(self.trUtf8("Tirsdag")) self.scheduleHorizontalHeaderList.append(self.trUtf8("Onsdag")) self.scheduleHorizontalHeaderList.append(self.trUtf8("Torsdag")) self.scheduleHorizontalHeaderList.append(self.trUtf8("Fredag")) 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) ## Add the lessons of the semester to the table def updateTable(self, semester): self.schedule = getLessonsFromDB(semester) rows = len(self.schedule) self.scheduleTable.clear() for l in range(rows): self.addLessonToTable(self.schedule[l]) self.scheduleTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents) self.scheduleTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents) self.updateHeaders() ## Add a new lesson to the table def addLessonToTable(self, lesson): row = lesson.getTime() - 8 column = self.getColumn(lesson.getDay()) code = lesson.getCourse().getCode() title = lesson.getCourse().getTitle() type = lesson.getType() room = lesson.getRoom() olditem = self.scheduleTable.item(row, column) newtext = "%s\n%s\n%s\n%s" % (code, title, 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(u"Mandag")) == 0: return 0 elif day.compare(QString(u"Tirsdag")) == 0: return 1 elif day.compare(QString(u"Onsdag")) == 0: return 2 elif day.compare(QString(u"Torsdag")) == 0: return 3 elif day.compare(QString(u"Fredag")) == 0: return 4 else: return -1 ## 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("Forelesning"))) == 0: brush = QBrush(Qt.SolidPattern) elif type.compare(QString(self.trUtf8("Øvingsforelesning"))) == 0: brush = QBrush(Qt.Dense2Pattern) elif type.compare(QString(self.trUtf8("Øving"))) == 0: brush = QBrush(Qt.Dense3Pattern) elif type.compare(QString(self.trUtf8("Lab"))) == 0: brush = QBrush(Qt.Dense1Pattern) elif type.compare(QString(self.trUtf8("Seminar"))) == 0: brush = QBrush(Qt.Dense4Pattern) elif type.compare(QString(self.trUtf8("Annet"))) == 0: brush = QBrush(Qt.Dense5Pattern) else: 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("&Dag")) self.fromLabel = QLabel(self.trUtf8("F&ra")) self.toLabel = QLabel(self.trUtf8("&Til")) self.courseLabel = QLabel(self.trUtf8("&Fag")) self.typeLabel = QLabel(self.trUtf8("T&ype")) self.roomLabel = QLabel(self.trUtf8("&Rom")) # Widgets self.dayEdit = QComboBox() self.dayEdit.addItems(getMain().days) 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() coursesStringList = QStringList() for course in courses: coursesStringList.append(course) self.courseEdit.addItems(coursesStringList) self.typeEdit = QComboBox() types = [self.trUtf8("Forelesning"), self.trUtf8("Øvingsforelesning"), self.trUtf8("Øving"), self.trUtf8("Lab"), self.trUtf8("Seminar"), self.trUtf8("Annet")] 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) self.courseLabel.setBuddy(self.courseEdit) 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) 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) self.layout.addWidget(self.buttonBox, 6, 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("Ny time")) ## Accept the dialog and add the specified lesson def accept(self): day = unicode(self.dayEdit.currentText()) fromtime = self.fromEdit.value() totime = self.toEdit.value() courseFull = unicode(self.courseEdit.currentText()) type = unicode(self.typeEdit.currentText()) room = unicode(self.roomEdit.text()) course = getCourseFromDB(getCourseCode(courseFull)) global semester for t in range(fromtime, totime): addNewLessonToDB(semester, day, t, course, type, room) getMain().schedule.addLessonToTable(ScheduleModel(day, t, course, type, room)) self.close() ### The schedule model class ScheduleModel(): day = "" time = 0 course = None type = "" room = "" ## Initialize the schedule model def __init__(self, day, time, course, type, room): self.day = day self.time = time self.course = course self.type = type self.room = room ## Return the day of the lesson def getDay(self): return self.day ## Return the start time of the lesson def getTime(self): return self.time ## Return the course of the lesson def getCourse(self): return self.course ## Return the type of the lesson def getType(self): return self.type ## Return the room the lesson is in def getRoom(self): return self.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("&Kode")) self.titleLabel = QLabel(self.trUtf8("&Tittel")) self.shortLabel = QLabel(self.trUtf8("Kort&form")) self.booksLabel = QLabel(self.trUtf8("&Bøker")) # Widgets self.codeEdit = QLineEdit() self.titleEdit = QLineEdit() self.shortEdit = QLineEdit() self.booksEdit = QListWidget() self.updateList() self.booksEdit.setSelectionMode(QAbstractItemView.ExtendedSelection) self.newBook = QPushButton("Ny bok") 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) self.layout.addWidget(self.shortLabel, 2, 0) self.layout.addWidget(self.booksLabel, 3, 0) self.layout.addWidget(self.codeEdit, 0, 1) self.layout.addWidget(self.titleEdit, 1, 1) 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("Nytt fag")) ## Update the book list def updateList(self): self.booksEdit.clear() booksDB = getBooksFromDB() booksStringList = QStringList() for book in booksDB: booksStringList.append(book.getAuthor() + u" : " + book.getTitle() + u", " + str(book.getEdition()) + u". utgave") self.booksEdit.addItems(booksStringList) self.booksEdit.sortItems() ## Add a new book to the course def addNewBookToCourse(self): book = getBookWithTitleFromDB(booktitle) self.books.append(book) ## Accept the dialog and add the specified course def accept(self): courseCode = unicode(self.codeEdit.text()) courseTitle = unicode(self.titleEdit.text()) courseShort = unicode(self.shortEdit.text()) courseBooks = self.booksEdit.selectedItems() color = getRandomColor() global colors while color in colors: color = getRandomColor() colors.append(color) books = [] for book in courseBooks: books.append(getBookWithTitleFromDB("%s" % book.text())) course = CourseModel(courseCode, courseTitle, courseShort, color, books) addNewCourseToDB(courseCode, courseTitle, courseShort, color, books) self.close() ### The course model class CourseModel(): code = "" title = "" short = "" full = "" books = [] ## Initialize the course model def __init__(self, code, title, short, color, books): self.code = code self.title = title self.short = short self.setFull(code, title) 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("&Tittel")) self.authorLabel = QLabel(self.trUtf8("&Forfatter")) self.editionLabel = QLabel(self.trUtf8("&Utgave")) 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) 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) # 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("Ny bok")) # Accept the dialog and add the specified book def accept(self): 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() ### The book model class BookModel(): title = "" author = "" 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 # Database # Initialization ## Connect to the database and return the cursor and connection def initDB(): if 'HOME' in os.environ: home = os.environ['HOME'] else: home = "" path = home+"/.egon/egon_no.db" conn = sqlite.connect(path) 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 exitDB(conn) # Create the database ## Create the Semester table def initSemesterDB(cursor): cursor.execute(''' CREATE TABLE Semester ( term TEXT, year INT, PRIMARY KEY (term, year) ) ''') ## Create the Assignment table def initAssignmentDB(cursor): cursor.execute(''' CREATE TABLE Assignment ( date DATETIME, course TEXT, number INT, description TEXT, complete TEXT, term TEXT, year INT, PRIMARY KEY (course, number) ) ''') ## Create the Reading table def initReadingDB(cursor): cursor.execute(''' CREATE TABLE Reading ( week INT, course TEXT, book INT, chapter TEXT, pages TEXT, done BOOLEAN, term TEXT, year INT, PRIMARY KEY (week, course, book, chapter) ) ''') ## Create the Lesson table def initScheduleDB(cursor): cursor.execute(''' CREATE TABLE Lesson ( day TEXT, time INT, course TEXT, type TEXT, room TEXT, term TEXT, year INT, PRIMARY KEY (course, day, time) ) ''') ## Create the Course table def initCourseDB(cursor): cursor.execute(''' CREATE TABLE Course ( code TEXT PRIMARY KEY, title TEXT, short TEXT, red INT, green INT, blue INT ) ''') ## 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 ( courseCode TEXT, bookIsbn TEXT, PRIMARY KEY (courseCode, bookIsbn) ) ''') # Add things to the database ## Add new semester to the database def addNewSemesterToDB(term, year): cursor, conn = initDB() cursor.execute(''' INSERT INTO Semester (term, year) VALUES (?, ?) ''', (term, year)) exitDB(conn) ## Add new assignment to the database def addNewAssignmentToDB(semester, datetime, course, number, description, complete): 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) term = "%s" % semester.getTerm() year = semester.getYear() cursor.execute(''' INSERT INTO Assignment (date, course, number, description, complete, term, year) VALUES (datetime(?), ?, ?, ?, ?, ?, ?) ''', (timestring, course.getCode(), number, description, complete, term, year)) 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, 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, term, year)) exitDB(conn) ## Add new course to the database def addNewCourseToDB(code, title, short, color, books): cursor, conn = initDB() red = color.red() green = color.green() blue = color.blue() cursor.execute(''' INSERT INTO Course (code, title, short, red, green, blue) VALUES (?, ?, ?, ?, ?, ?) ''', (code, title, short, red, green, blue)) for book in books: cursor.execute(''' INSERT INTO CourseUsesBook (courseCode, bookIsbn) VALUES (?, ?) ''', (code, book)) exitDB(conn) ## Add new book to the database def addNewBookToDB(isbn, title, author, edition): cursor, conn = initDB() cursor.execute(''' INSERT INTO Book VALUES (?, ?, ?, ?) ''', (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() term = "%s" % semester.getTerm() year = (semester.getYear()) cursor.execute(''' SELECT * FROM Assignment WHERE term = ? AND year = ? ''', (term, year)) 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 ## Get the readings of the specified semester from the database def getReadingsFromDB(semester): cursor, conn = initDB() term = "%s" % semester.getTerm() year = semester.getYear() cursor.execute(''' SELECT * FROM Reading WHERE term = ? AND year = ? ''', (term, year)) 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 ## Get the lessons of the specified semester from the database def getLessonsFromDB(semester): cursor, conn = initDB() term = "%s" % semester.getTerm() year = semester.getYear() cursor.execute(''' SELECT * FROM Lesson WHERE term = ? AND year = ? ''', (term, year)) lessons = [] for row in cursor.fetchall(): lessons.append(ScheduleModel(row[0], row[1], getCourseFromDB(row[2]), row[3], row[4])) exitDB(conn) return lessons ## Get all the courses from the database def getCoursesFromDB(): cursor, conn = initDB() cursor.execute(''' SELECT * FROM Course ''') courses = [] for row in cursor.fetchall(): courses.append(CourseModel(row[0], row[1], row[2], QColor(row[3], row[4], row[5]), [])) for course in courses: cursor.execute(''' SELECT bookIsbn FROM CourseUsesBook WHERE courseCode = ? ''', (course.getCode(),)) 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])) return courses ## Get the course specified by the course code from the database def getCourseFromDB(courseCode): cursor, conn = initDB() cursor.execute(''' SELECT * FROM Course WHERE code = ? ''', (courseCode,)) course = None for row in cursor.fetchall(): course = CourseModel(row[0], row[1], row[2], QColor(row[3], row[4], row[5]), []) 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 ## Get all the books from the database def getBooksFromDB(): cursor, conn = initDB() cursor.execute(''' SELECT * FROM Book ''') books = [] for row in cursor.fetchall(): books.append(BookModel(row[0], row[1], row[2], row[3])) exitDB(conn) return books ## Get the book specified by the ISBN number from the database 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 ## Get the book specified by the title from the database 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 # Update things in the database ## Update the completion level of the specified assignment 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) ## Update whether the specified reading is done or not def updateReadingDone(week, coursecode, bookisbn, chapter, done): cursor, conn = initDB() course = "%s" % coursecode book = "%s" % bookisbn ch = "%s" % chapter cursor.execute(''' UPDATE Reading SET done = ? WHERE week = ? AND course = ? AND book = ? AND chapter = ? ''', (done, week, course, book, ch)) exitDB(conn) # Remove things from the database ## Remove the specified assignment from the database 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) ## Remove the specified reading from the database def removeReadingFromDB(week, coursecode, bookisbn, chapter): cursor, conn = initDB() course = "%s" % coursecode book = "%s" % bookisbn ch = "%s" % chapter cursor.execute(''' DELETE FROM Reading WHERE week = ? AND course = ? AND book = ? AND chapter = ? ''', (week, course, book, ch)) exitDB(conn) ## Remove the specified lesson from the database 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) # 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(), course.getBooks()) 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() if cs: for c in cs: addNewCourseString(c) 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() if bs: for b in bs: addNewBookString(b) s = getBooksString() return s ## Return the books string list def getBooksString(): global booksString return booksString ## 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() app.setOrganizationName("PVV") app.setOrganizationDomain("pvv.ntnu.no") app.setApplicationName(form.title) form.show() app.exec_() main() #initNewDB()