示例#1
0
文件: window.py 项目: mattpotok/gLog
class Window(QtGui.QWidget):

  """ __init__
      Description: Initializes the class and GUI
      Inputs: None
      Outputs: None
      Return values: None
  """
  def __init__(self):
    self.logger=Logger()
    self.catLen,self.cats=self.logger.getCategories()
    super(Window,self).__init__()
    self.initUI()
    self.setInfoBox()

  """ initUI
      Description: Initializes the GUI and arranges groupboxes in their 
                   appropriate places.
      Inputs: None
      Outputs: None
      Return values: None
  """
  def initUI(self):
    winLayout=QtGui.QGridLayout()

    winLayout.addWidget(self.createCalendarBox(),0,0,2,1)
    winLayout.addWidget(self.createLogBox(),0,1)
    winLayout.addWidget(self.createInfoBox(),1,1)
    winLayout.addLayout(self.createLowerBar(),2,0,1,2)
    
    self.setLayout(winLayout)
    self.setWindowTitle("gLog")
    self.show()

  """ createCalendarBox
      Descriptiion: Creates the calendarbox and all associated elements 
                    inside it
      Inputs: None
      Outputs: None
      Return values: None
  """
  def createCalendarBox(self):
    calendarBox=QtGui.QGroupBox("Calendar")
    calendarLayout=QtGui.QGridLayout()

    self.calendar=QtGui.QCalendarWidget()
    self.calendar.setGridVisible(True)
    self.calendar.clicked[QtCore.QDate].connect(self.showSelDate)
    calendarLayout.addWidget(self.calendar,0,0)

    calendarBox.setLayout(calendarLayout)
    return calendarBox

  """ createLogBox
      Description: Creates the logbox and all associated elements inside it
      Inputs: None
      Outputs: None
      Return values: None
  """
  def createLogBox(self):
    logBox=QtGui.QGroupBox("Log")
    logLayout=QtGui.QGridLayout()
    
    self.logEdit=QtGui.QLineEdit()
    self.logEdit.clear()
    logLayout.addWidget(self.logEdit,0,0)

    self.logBtn=QtGui.QPushButton("Log")
    self.logBtn.clicked.connect(self.logData)
    logLayout.addWidget(self.logBtn,0,1)
    
    logBox.setLayout(logLayout)
    return logBox

  """ createInfoBox
      Description: Creates the infobox and all associated elements inside it
      Inputs: None
      Outputs: None
      Return values: None
  """
  def createInfoBox(self):
    infoBox=QtGui.QGroupBox("Info")
    infoLayout=QtGui.QGridLayout()

    dayLbl=QtGui.QLabel("Day:")
    infoLayout.addWidget(dayLbl,0,0)
    self.dayCnt=QtGui.QLabel()
    infoLayout.addWidget(self.dayCnt,0,1)

    monthLbl=QtGui.QLabel("Month:")
    infoLayout.addWidget(monthLbl,1,0)
    self.monthCnt=QtGui.QLabel()
    infoLayout.addWidget(self.monthCnt,1,1)

    yearLbl=QtGui.QLabel("Year:")
    infoLayout.addWidget(yearLbl,2,0)
    self.yearCnt=QtGui.QLabel()
    infoLayout.addWidget(self.yearCnt,2,1)

    totLbl=QtGui.QLabel("Total:")
    infoLayout.addWidget(totLbl,3,0)
    self.totCnt=QtGui.QLabel()
    infoLayout.addWidget(self.totCnt,3,1)

    infoBox.setLayout(infoLayout)
    return infoBox

  """ createLowerBar
      Description: Creates the bar at the bottom of the program along with all 
                   elements inside it.
      Inputs: None
      Outputs: None
      Return values: None
  """
  def createLowerBar(self):
    barLayout=QtGui.QGridLayout()

    self.selDayLbl=QtGui.QLabel()
    self.selDayLbl.setText(self.calendar.selectedDate().toString())
    barLayout.addWidget(self.selDayLbl,0,1)

    self.catList=QtGui.QComboBox()
    for c in self.cats:
      self.catList.addItem(c)
    self.catList.currentIndexChanged[str].connect(self.setInfoBox)
    barLayout.addWidget(self.catList,0,2)

    self.editBtn=QtGui.QPushButton("Edit")
    self.editBtn.clicked.connect(self.editCategory)
    barLayout.addWidget(self.editBtn,0,3)

    self.addBtn=QtGui.QPushButton("Add")
    self.addBtn.clicked.connect(self.addCategory)
    barLayout.addWidget(self.addBtn,0,4)

    self.removeBtn=QtGui.QPushButton("Remove")
    self.removeBtn.clicked.connect(self.removeCategory)
    barLayout.addWidget(self.removeBtn,0,5)

    return barLayout
  
  """ addCategory
      Description: Handler for the add button. Creates a popup which allows a 
                   user to create a category and update the infobox
      Inputs: None
      Outputs: None
      Return values: None
  """
  def addCategory(self):
    pop=Popup(self.sender().text(),unicode(self.catList.currentText()))
    if pop.exec_():
      newCat=pop.getNewCategory()
      self.cats.append(newCat)
      self.catList.addItem(newCat)
      self.catList.setCurrentIndex(self.catLen)
      self.catLen+=1

  """ editCategory
      Description: Handler for the edit button. Creates a popup which allows a 
                   user to edit an existing category and updates the infobox
      Inputs: None
      Outputs: None
      Return values: None
  """
  def editCategory(self):
    if self.catLen==0:
      return
    if pop.exec_():
      newCat=pop.getNewCategory()
      for i,c in enumerate(self.cats):
        if c==self.catList.currentText():
          self.cats[i]=newCat
      self.catList.setItemText(self.catList.currentIndex(),newCat)

  """ removeCategory
      Description: Handler for the remove button. Removes a category from the
                   list of categories that the user has created and updates
                   infobox
      Inputs: None
      Outputs: None
      Return values: None
  """
  def removeCategory(self):
    if self.catLen==0:
      return
    if QtGui.QMessageBox.warning(self,"Warning","Are you sure you want to delete the selected category?",QtGui.QMessageBox.Yes|QtGui.QMessageBox.No,QtGui.QMessageBox.No)==QtGui.QMessageBox.Yes:
      remCat=unicode(self.catList.currentText())
      self.catLen-=1
      self.catList.removeItem(self.catList.currentIndex())
      self.cats.remove(remCat)
      self.logger.removeCategory(remCat)

  """ showSelDate
      Description: Handler for the calendar widget. Changes the text displaying
                   the date and updates the infobox
      Inputs: date-the selected date in the calendar widget
      Outputs: None
      Return values: None
  """
  def showSelDate(self,date):
    self.selDayLbl.setText(date.toString())
    self.setInfoBox()

  """ logData
      Description: Handler for the log button. Logs the data for the selected
                   date and category to an xml file.
      Inputs: None
      Outputs: None
      Return values: None
  """
  def logData(self):
    if self.catLen==0:
      return
    try:
      data=self.logger.logData(unicode(self.catList.currentText()),str(float(self.logEdit.text())),self.calendar.selectedDate())
      unit=data[0]
      self.dayCnt.setText(data[1]+" "+unit)
      self.monthCnt.setText(data[2]+" "+unit)
      self.yearCnt.setText(data[3]+" "+unit)
      self.totCnt.setText(data[4]+" "+unit)
      self.logEdit.clear()
      self.logEdit.setFocus()
    except ValueError:
      QtGui.QMessageBox.warning(self,"Warning","The data you are trying to log is invalid.\nPlease enter real numbers only")
      self.logEdit.clear()
      self.logEdit.setFocus()

  """ setInfoBox
      Description: Updates the infobox whenever a date or category is changed
      Inputs: None
      Outputs: None
      Return values: None
  """
  def setInfoBox(self):
    if not self.catLen:
      self.dayCnt.clear()
      self.monthCnt.clear()
      self.yearCnt.clear()
      self.totCnt.clear()
      return

    data=self.logger.getData(unicode(self.catList.currentText()),self.calendar.selectedDate())
    unit=data[0]
    if not data[1]==0:
      self.dayCnt.setText(data[1]+" "+unit)
    else:
      self.dayCnt.setText("0.0"+" "+unit)
    if not data[2]==0:
      self.monthCnt.setText(data[2]+" "+unit)
    else:
      self.monthCnt.setText("0.0"+" "+unit)
    if not data[3]==0:
      self.yearCnt.setText(data[3]+" "+unit)
    else:
      self.yearCnt.setText("0.0"+" "+unit)
    if not data[4]==0:
      self.totCnt.setText(data[4]+" "+unit)
    else:
      self.totCnt.setText("0.0"+" "+unit)