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