| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "mainwindow.h" |
| 52 | |
| 53 | #include <QtWidgets> |
| 54 | |
| 55 | //! [0] |
| 56 | MainWindow::MainWindow() |
| 57 | { |
| 58 | selectedDate = QDate::currentDate(); |
| 59 | fontSize = 10; |
| 60 | |
| 61 | QWidget *centralWidget = new QWidget; |
| 62 | //! [0] |
| 63 | |
| 64 | //! [1] |
| 65 | QLabel *dateLabel = new QLabel(tr(s: "Date:" )); |
| 66 | QComboBox *monthCombo = new QComboBox; |
| 67 | |
| 68 | for (int month = 1; month <= 12; ++month) |
| 69 | monthCombo->addItem(atext: QLocale::system().monthName(month)); |
| 70 | |
| 71 | QDateTimeEdit *yearEdit = new QDateTimeEdit; |
| 72 | yearEdit->setDisplayFormat("yyyy" ); |
| 73 | yearEdit->setDateRange(min: QDate(1753, 1, 1), max: QDate(8000, 1, 1)); |
| 74 | //! [1] |
| 75 | |
| 76 | monthCombo->setCurrentIndex(selectedDate.month() - 1); |
| 77 | yearEdit->setDate(selectedDate); |
| 78 | |
| 79 | //! [2] |
| 80 | QLabel *fontSizeLabel = new QLabel(tr(s: "Font size:" )); |
| 81 | QSpinBox *fontSizeSpinBox = new QSpinBox; |
| 82 | fontSizeSpinBox->setRange(min: 1, max: 64); |
| 83 | |
| 84 | editor = new QTextBrowser; |
| 85 | insertCalendar(); |
| 86 | //! [2] |
| 87 | |
| 88 | //! [3] |
| 89 | connect(sender: monthCombo, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 90 | receiver: this, slot: &MainWindow::setMonth); |
| 91 | connect(sender: yearEdit, signal: &QDateTimeEdit::dateChanged, |
| 92 | receiver: this, slot: &MainWindow::setYear); |
| 93 | connect(sender: fontSizeSpinBox, signal: QOverload<int>::of(ptr: &QSpinBox::valueChanged), |
| 94 | receiver: this, slot: &MainWindow::setFontSize); |
| 95 | //! [3] |
| 96 | |
| 97 | fontSizeSpinBox->setValue(10); |
| 98 | |
| 99 | //! [4] |
| 100 | QHBoxLayout *controlsLayout = new QHBoxLayout; |
| 101 | controlsLayout->addWidget(dateLabel); |
| 102 | controlsLayout->addWidget(monthCombo); |
| 103 | controlsLayout->addWidget(yearEdit); |
| 104 | controlsLayout->addSpacing(size: 24); |
| 105 | controlsLayout->addWidget(fontSizeLabel); |
| 106 | controlsLayout->addWidget(fontSizeSpinBox); |
| 107 | controlsLayout->addStretch(stretch: 1); |
| 108 | |
| 109 | QVBoxLayout *centralLayout = new QVBoxLayout; |
| 110 | centralLayout->addLayout(layout: controlsLayout); |
| 111 | centralLayout->addWidget(editor, stretch: 1); |
| 112 | centralWidget->setLayout(centralLayout); |
| 113 | |
| 114 | setCentralWidget(centralWidget); |
| 115 | //! [4] |
| 116 | } |
| 117 | |
| 118 | //! [5] |
| 119 | void MainWindow::insertCalendar() |
| 120 | { |
| 121 | editor->clear(); |
| 122 | QTextCursor cursor = editor->textCursor(); |
| 123 | cursor.beginEditBlock(); |
| 124 | |
| 125 | QDate date(selectedDate.year(), selectedDate.month(), 1); |
| 126 | //! [5] |
| 127 | |
| 128 | //! [6] |
| 129 | QTextTableFormat tableFormat; |
| 130 | tableFormat.setAlignment(Qt::AlignHCenter); |
| 131 | tableFormat.setBackground(QColor("#e0e0e0" )); |
| 132 | tableFormat.setCellPadding(2); |
| 133 | tableFormat.setCellSpacing(4); |
| 134 | //! [6] //! [7] |
| 135 | QVector<QTextLength> constraints; |
| 136 | constraints << QTextLength(QTextLength::PercentageLength, 14) |
| 137 | << QTextLength(QTextLength::PercentageLength, 14) |
| 138 | << QTextLength(QTextLength::PercentageLength, 14) |
| 139 | << QTextLength(QTextLength::PercentageLength, 14) |
| 140 | << QTextLength(QTextLength::PercentageLength, 14) |
| 141 | << QTextLength(QTextLength::PercentageLength, 14) |
| 142 | << QTextLength(QTextLength::PercentageLength, 14); |
| 143 | tableFormat.setColumnWidthConstraints(constraints); |
| 144 | //! [7] |
| 145 | |
| 146 | //! [8] |
| 147 | QTextTable *table = cursor.insertTable(rows: 1, cols: 7, format: tableFormat); |
| 148 | //! [8] |
| 149 | |
| 150 | //! [9] |
| 151 | QTextFrame *frame = cursor.currentFrame(); |
| 152 | QTextFrameFormat frameFormat = frame->frameFormat(); |
| 153 | frameFormat.setBorder(1); |
| 154 | frame->setFrameFormat(frameFormat); |
| 155 | //! [9] |
| 156 | |
| 157 | //! [10] |
| 158 | QTextCharFormat format = cursor.charFormat(); |
| 159 | format.setFontPointSize(fontSize); |
| 160 | |
| 161 | QTextCharFormat boldFormat = format; |
| 162 | boldFormat.setFontWeight(QFont::Bold); |
| 163 | |
| 164 | QTextCharFormat highlightedFormat = boldFormat; |
| 165 | highlightedFormat.setBackground(Qt::yellow); |
| 166 | //! [10] |
| 167 | |
| 168 | //! [11] |
| 169 | for (int weekDay = 1; weekDay <= 7; ++weekDay) { |
| 170 | QTextTableCell cell = table->cellAt(row: 0, col: weekDay-1); |
| 171 | //! [11] //! [12] |
| 172 | QTextCursor cellCursor = cell.firstCursorPosition(); |
| 173 | cellCursor.insertText(text: QLocale::system().dayName(weekDay), format: boldFormat); |
| 174 | } |
| 175 | //! [12] |
| 176 | |
| 177 | //! [13] |
| 178 | table->insertRows(pos: table->rows(), num: 1); |
| 179 | //! [13] |
| 180 | |
| 181 | while (date.month() == selectedDate.month()) { |
| 182 | int weekDay = date.dayOfWeek(); |
| 183 | QTextTableCell cell = table->cellAt(row: table->rows()-1, col: weekDay-1); |
| 184 | QTextCursor cellCursor = cell.firstCursorPosition(); |
| 185 | |
| 186 | if (date == QDate::currentDate()) |
| 187 | cellCursor.insertText(text: QString("%1" ).arg(a: date.day()), format: highlightedFormat); |
| 188 | else |
| 189 | cellCursor.insertText(text: QString("%1" ).arg(a: date.day()), format); |
| 190 | |
| 191 | date = date.addDays(days: 1); |
| 192 | if (weekDay == 7 && date.month() == selectedDate.month()) |
| 193 | table->insertRows(pos: table->rows(), num: 1); |
| 194 | } |
| 195 | |
| 196 | cursor.endEditBlock(); |
| 197 | //! [14] |
| 198 | setWindowTitle(tr(s: "Calendar for %1 %2" |
| 199 | ).arg(a: QLocale::system().monthName(selectedDate.month()) |
| 200 | ).arg(a: selectedDate.year())); |
| 201 | } |
| 202 | //! [14] |
| 203 | |
| 204 | //! [15] |
| 205 | void MainWindow::setFontSize(int size) |
| 206 | { |
| 207 | fontSize = size; |
| 208 | insertCalendar(); |
| 209 | } |
| 210 | //! [15] |
| 211 | |
| 212 | //! [16] |
| 213 | void MainWindow::setMonth(int month) |
| 214 | { |
| 215 | selectedDate = QDate(selectedDate.year(), month + 1, selectedDate.day()); |
| 216 | insertCalendar(); |
| 217 | } |
| 218 | //! [16] |
| 219 | |
| 220 | //! [17] |
| 221 | void MainWindow::setYear(QDate date) |
| 222 | { |
| 223 | selectedDate = QDate(date.year(), selectedDate.month(), selectedDate.day()); |
| 224 | insertCalendar(); |
| 225 | } |
| 226 | //! [17] |
| 227 | |