import os import random import glob from PyQt4 import QtGui, QtCore, QtWebKit from core import CCP4Utils, CCP4Modules class CTipsOfTheDay(QtGui.QDialog): def __init__(self,parent=None): QtGui.QDialog.__init__(self,parent) self.setWindowTitle("Tip of the Day") layout = QtGui.QVBoxLayout() headLayout = QtGui.QHBoxLayout() self.setLayout(layout) lightBulb = QtGui.QLabel() lightBulbPixmap = QtGui.QPixmap(os.path.join(CCP4Utils.getCCP4I2Dir(), 'qticons', "lightbulb.svg")) lightBulb.setPixmap(lightBulbPixmap.scaledToHeight(50)) headLayout.addWidget(lightBulb) didYouKnow = QtGui.QLabel("Did you know ...?") didYouKnow.setStyleSheet("QLabel { font-size: 32pt;}") headLayout.addWidget(didYouKnow) layout.addLayout(headLayout) tipWindow = QtWebKit.QWebView() tipWindow.setMaximumWidth(530) layout.addWidget(tipWindow) tipWindow.setContextMenuPolicy(QtCore.Qt.NoContextMenu) showCheck = QtGui.QCheckBox("Show Tips on Startup") layout.addWidget(showCheck) showTipsOfTheDay = CCP4Modules.PREFERENCES().SHOW_TIPS_OF_THE_DAY if showTipsOfTheDay: showCheck.setChecked(True) showCheck.clicked.connect(showTipsOfTheDay.set) dialogButtonBox = QtGui.QDialogButtonBox() layout.addWidget(dialogButtonBox) cb = dialogButtonBox.addButton(QtGui.QDialogButtonBox.Close) pb = dialogButtonBox.addButton("Previous tip",QtGui.QDialogButtonBox.ActionRole) nb = dialogButtonBox.addButton("Next tip",QtGui.QDialogButtonBox.ActionRole) nb.setDefault(True) def numList(elem): return int(os.path.basename(elem).split(".")[0]) numTips = int(os.path.basename(sorted(glob.glob(os.path.join(CCP4Utils.getCCP4I2Dir(),'tipsOfTheDay','*.html')),key=numList)[-1]).split(".")[0]) #In Python2 we need to deal with a list so that we can edit the variable in an inner-scope. (Can use nonlocal in Python3.) day = [int(random.random()*(numTips-1))] def setNextTipText(): day[0] += 1 if day[0] > numTips: day[0] = 1 dayTextFile = os.path.join(CCP4Utils.getCCP4I2Dir(), 'tipsOfTheDay', str(day[0])+".html") self.setWindowTitle(str(day[0])) tipWindow.load(QtCore.QUrl.fromLocalFile(dayTextFile)) def setPreviousTipText(): day[0] -= 1 if day[0] == 0: day[0] = numTips dayTextFile = os.path.join(CCP4Utils.getCCP4I2Dir(), 'tipsOfTheDay', str(day[0])+".html") self.setWindowTitle(str(day[0])) tipWindow.load(QtCore.QUrl.fromLocalFile(dayTextFile)) setNextTipText() cb.clicked.connect(self.accept) nb.clicked.connect(setNextTipText) pb.clicked.connect(setPreviousTipText)