Ejemplo n.º 1
0
    def on_mapAct_triggered(self):
        # some languages have more info in single character
        # ratios are from http://www.sonasphere.com/blog/?p=1319
        ratio = {
            QLocale.Chinese: 1,
            QLocale.English: 4,
            QLocale.Japanese: 1.5,
        }.get(QLocale().language(), 1.6)
        logging.debug('HeatMap got length ratio %s' % ratio)
        ds = [
            '0',
            '< %d' % (200 * ratio),
            '< %d' % (550 * ratio),
            '>= %d' % (550 * ratio)
        ]
        descriptions = [
            i + ' ' + qApp.translate('HeatMap', '(characters)') for i in ds
        ]

        def colorFunc(data, cellColors):
            if data == 0:
                return cellColors[0]
            elif data < 200 * ratio:
                return cellColors[1]
            elif data < 550 * ratio:
                return cellColors[2]
            else:
                return cellColors[3]

        # iter through model once and cache result.
        cached = {}
        for diary in self.diaryList.originModel.getAll():
            dt, length = diary[DiaryModel.DATETIME], diary[DiaryModel.LENGTH]
            year, month, last = dt.split('-')
            cached[(int(year), int(month), int(last[:2]))] = length

        try:
            self.heatMap.activateWindow()
        except (AttributeError, RuntimeError):
            self.heatMap = HeatMap(self,
                                   objectName='heatMap',
                                   font=font.datetime)
            self.heatMap.closeSc = QShortcut(QKeySequence(Qt.Key_Escape),
                                             self.heatMap,
                                             activated=self.heatMap.close)
            self.heatMap.setColorFunc(colorFunc)
            self.heatMap.setDataFunc(lambda y, m, d: cached.get((y, m, d), 0))
            self.heatMap.sample.setDescriptions(descriptions)
            self.heatMap.setAttribute(Qt.WA_DeleteOnClose)
            self.heatMap.resize(self.size())
            self.heatMap.setWindowFlags(Qt.Window | Qt.WindowTitleHint)
            self.heatMap.setWindowTitle(self.tr('Heat Map'))
            self.heatMap.move(self.pos() + QPoint(12, 12) * scaleRatio)
            self.heatMap.show()