1#!/usr/bin/env python 2 3 4############################################################################# 5## 6## Copyright (C) 2013 Riverbank Computing Limited. 7## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 8## All rights reserved. 9## 10## This file is part of the examples of PyQt. 11## 12## $QT_BEGIN_LICENSE:BSD$ 13## You may use this file under the terms of the BSD license as follows: 14## 15## "Redistribution and use in source and binary forms, with or without 16## modification, are permitted provided that the following conditions are 17## met: 18## * Redistributions of source code must retain the above copyright 19## notice, this list of conditions and the following disclaimer. 20## * Redistributions in binary form must reproduce the above copyright 21## notice, this list of conditions and the following disclaimer in 22## the documentation and/or other materials provided with the 23## distribution. 24## * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor 25## the names of its contributors may be used to endorse or promote 26## products derived from this software without specific prior written 27## permission. 28## 29## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 40## $QT_END_LICENSE$ 41## 42############################################################################# 43 44 45from PyQt5.QtCore import QDate, Qt 46from PyQt5.QtGui import (QColor, QFont, QTextCharFormat, QTextLength, 47 QTextTableFormat) 48from PyQt5.QtWidgets import (QApplication, QComboBox, QDateTimeEdit, 49 QHBoxLayout, QLabel, QMainWindow, QSpinBox, QTextBrowser, QVBoxLayout, 50 QWidget) 51 52 53class MainWindow(QMainWindow): 54 def __init__(self): 55 super(MainWindow, self).__init__() 56 57 self.selectedDate = QDate.currentDate() 58 self.fontSize = 10 59 60 centralWidget = QWidget() 61 62 dateLabel = QLabel("Date:") 63 monthCombo = QComboBox() 64 65 for month in range(1, 13): 66 monthCombo.addItem(QDate.longMonthName(month)) 67 68 yearEdit = QDateTimeEdit() 69 yearEdit.setDisplayFormat('yyyy') 70 yearEdit.setDateRange(QDate(1753, 1, 1), QDate(8000, 1, 1)) 71 72 monthCombo.setCurrentIndex(self.selectedDate.month() - 1) 73 yearEdit.setDate(self.selectedDate) 74 75 self.fontSizeLabel = QLabel("Font size:") 76 self.fontSizeSpinBox = QSpinBox() 77 self.fontSizeSpinBox.setRange(1, 64) 78 self.fontSizeSpinBox.setValue(10) 79 80 self.editor = QTextBrowser() 81 self.insertCalendar() 82 83 monthCombo.activated.connect(self.setMonth) 84 yearEdit.dateChanged.connect(self.setYear) 85 self.fontSizeSpinBox.valueChanged.connect(self.setfontSize) 86 87 controlsLayout = QHBoxLayout() 88 controlsLayout.addWidget(dateLabel) 89 controlsLayout.addWidget(monthCombo) 90 controlsLayout.addWidget(yearEdit) 91 controlsLayout.addSpacing(24) 92 controlsLayout.addWidget(self.fontSizeLabel) 93 controlsLayout.addWidget(self.fontSizeSpinBox) 94 controlsLayout.addStretch(1) 95 96 centralLayout = QVBoxLayout() 97 centralLayout.addLayout(controlsLayout) 98 centralLayout.addWidget(self.editor, 1) 99 centralWidget.setLayout(centralLayout) 100 101 self.setCentralWidget(centralWidget) 102 103 def insertCalendar(self): 104 self.editor.clear() 105 cursor = self.editor.textCursor() 106 cursor.beginEditBlock() 107 108 date = QDate(self.selectedDate.year(), self.selectedDate.month(), 1) 109 110 tableFormat = QTextTableFormat() 111 tableFormat.setAlignment(Qt.AlignHCenter) 112 tableFormat.setBackground(QColor('#e0e0e0')) 113 tableFormat.setCellPadding(2) 114 tableFormat.setCellSpacing(4) 115 constraints = [QTextLength(QTextLength.PercentageLength, 14), 116 QTextLength(QTextLength.PercentageLength, 14), 117 QTextLength(QTextLength.PercentageLength, 14), 118 QTextLength(QTextLength.PercentageLength, 14), 119 QTextLength(QTextLength.PercentageLength, 14), 120 QTextLength(QTextLength.PercentageLength, 14), 121 QTextLength(QTextLength.PercentageLength, 14)] 122 123 tableFormat.setColumnWidthConstraints(constraints) 124 125 table = cursor.insertTable(1, 7, tableFormat) 126 127 frame = cursor.currentFrame() 128 frameFormat = frame.frameFormat() 129 frameFormat.setBorder(1) 130 frame.setFrameFormat(frameFormat) 131 132 format = cursor.charFormat() 133 format.setFontPointSize(self.fontSize) 134 135 boldFormat = QTextCharFormat(format) 136 boldFormat.setFontWeight(QFont.Bold) 137 138 highlightedFormat = QTextCharFormat(boldFormat) 139 highlightedFormat.setBackground(Qt.yellow) 140 141 for weekDay in range(1, 8): 142 cell = table.cellAt(0, weekDay-1) 143 cellCursor = cell.firstCursorPosition() 144 cellCursor.insertText(QDate.longDayName(weekDay), boldFormat) 145 146 table.insertRows(table.rows(), 1) 147 148 while date.month() == self.selectedDate.month(): 149 weekDay = date.dayOfWeek() 150 cell = table.cellAt(table.rows()-1, weekDay-1) 151 cellCursor = cell.firstCursorPosition() 152 153 if date == QDate.currentDate(): 154 cellCursor.insertText(str(date.day()), highlightedFormat) 155 else: 156 cellCursor.insertText(str(date.day()), format) 157 158 date = date.addDays(1) 159 160 if weekDay == 7 and date.month() == self.selectedDate.month(): 161 table.insertRows(table.rows(), 1) 162 163 cursor.endEditBlock() 164 165 self.setWindowTitle("Calendar for %s %d" % (QDate.longMonthName(self.selectedDate.month()), self.selectedDate.year())) 166 167 def setfontSize(self, size): 168 self.fontSize = size 169 self.insertCalendar() 170 171 def setMonth(self, month): 172 self.selectedDate = QDate(self.selectedDate.year(), month + 1, 173 self.selectedDate.day()) 174 self.insertCalendar() 175 176 def setYear(self, date): 177 self.selectedDate = QDate(date.year(), self.selectedDate.month(), 178 self.selectedDate.day()) 179 self.insertCalendar() 180 181 182if __name__ == '__main__': 183 184 import sys 185 186 app = QApplication(sys.argv) 187 window = MainWindow() 188 window.resize(640, 256) 189 window.show() 190 sys.exit(app.exec_()) 191