source: trunk/examples/richtext/calendar/mainwindow.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 6.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "mainwindow.h"
45
46//! [0]
47MainWindow::MainWindow()
48{
49 selectedDate = QDate::currentDate();
50 fontSize = 10;
51
52 QWidget *centralWidget = new QWidget;
53//! [0]
54
55//! [1]
56 QLabel *dateLabel = new QLabel(tr("Date:"));
57 QComboBox *monthCombo = new QComboBox;
58
59 for (int month = 1; month <= 12; ++month)
60 monthCombo->addItem(QDate::longMonthName(month));
61
62 QDateTimeEdit *yearEdit = new QDateTimeEdit;
63 yearEdit->setDisplayFormat("yyyy");
64 yearEdit->setDateRange(QDate(1753, 1, 1), QDate(8000, 1, 1));
65//! [1]
66
67 monthCombo->setCurrentIndex(selectedDate.month() - 1);
68 yearEdit->setDate(selectedDate);
69
70//! [2]
71 QLabel *fontSizeLabel = new QLabel(tr("Font size:"));
72 QSpinBox *fontSizeSpinBox = new QSpinBox;
73 fontSizeSpinBox->setRange(1, 64);
74 fontSizeSpinBox->setValue(10);
75
76 editor = new QTextBrowser;
77 insertCalendar();
78//! [2]
79
80//! [3]
81 connect(monthCombo, SIGNAL(activated(int)), this, SLOT(setMonth(int)));
82 connect(yearEdit, SIGNAL(dateChanged(QDate)), this, SLOT(setYear(QDate)));
83 connect(fontSizeSpinBox, SIGNAL(valueChanged(int)),
84 this, SLOT(setFontSize(int)));
85//! [3]
86
87//! [4]
88 QHBoxLayout *controlsLayout = new QHBoxLayout;
89 controlsLayout->addWidget(dateLabel);
90 controlsLayout->addWidget(monthCombo);
91 controlsLayout->addWidget(yearEdit);
92 controlsLayout->addSpacing(24);
93 controlsLayout->addWidget(fontSizeLabel);
94 controlsLayout->addWidget(fontSizeSpinBox);
95 controlsLayout->addStretch(1);
96
97 QVBoxLayout *centralLayout = new QVBoxLayout;
98 centralLayout->addLayout(controlsLayout);
99 centralLayout->addWidget(editor, 1);
100 centralWidget->setLayout(centralLayout);
101
102 setCentralWidget(centralWidget);
103//! [4]
104}
105
106//! [5]
107void MainWindow::insertCalendar()
108{
109 editor->clear();
110 QTextCursor cursor = editor->textCursor();
111 cursor.beginEditBlock();
112
113 QDate date(selectedDate.year(), selectedDate.month(), 1);
114//! [5]
115
116//! [6]
117 QTextTableFormat tableFormat;
118 tableFormat.setAlignment(Qt::AlignHCenter);
119 tableFormat.setBackground(QColor("#e0e0e0"));
120 tableFormat.setCellPadding(2);
121 tableFormat.setCellSpacing(4);
122//! [6] //! [7]
123 QVector<QTextLength> constraints;
124 constraints << 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 << QTextLength(QTextLength::PercentageLength, 14);
131 tableFormat.setColumnWidthConstraints(constraints);
132//! [7]
133
134//! [8]
135 QTextTable *table = cursor.insertTable(1, 7, tableFormat);
136//! [8]
137
138//! [9]
139 QTextFrame *frame = cursor.currentFrame();
140 QTextFrameFormat frameFormat = frame->frameFormat();
141 frameFormat.setBorder(1);
142 frame->setFrameFormat(frameFormat);
143//! [9]
144
145//! [10]
146 QTextCharFormat format = cursor.charFormat();
147 format.setFontPointSize(fontSize);
148
149 QTextCharFormat boldFormat = format;
150 boldFormat.setFontWeight(QFont::Bold);
151
152 QTextCharFormat highlightedFormat = boldFormat;
153 highlightedFormat.setBackground(Qt::yellow);
154//! [10]
155
156//! [11]
157 for (int weekDay = 1; weekDay <= 7; ++weekDay) {
158 QTextTableCell cell = table->cellAt(0, weekDay-1);
159//! [11] //! [12]
160 QTextCursor cellCursor = cell.firstCursorPosition();
161 cellCursor.insertText(QString("%1").arg(QDate::longDayName(weekDay)),
162 boldFormat);
163 }
164//! [12]
165
166//! [13]
167 table->insertRows(table->rows(), 1);
168//! [13]
169
170 while (date.month() == selectedDate.month()) {
171 int weekDay = date.dayOfWeek();
172 QTextTableCell cell = table->cellAt(table->rows()-1, weekDay-1);
173 QTextCursor cellCursor = cell.firstCursorPosition();
174
175 if (date == QDate::currentDate())
176 cellCursor.insertText(QString("%1").arg(date.day()), highlightedFormat);
177 else
178 cellCursor.insertText(QString("%1").arg(date.day()), format);
179
180 date = date.addDays(1);
181 if (weekDay == 7 && date.month() == selectedDate.month())
182 table->insertRows(table->rows(), 1);
183 }
184
185 cursor.endEditBlock();
186//! [14]
187 setWindowTitle(tr("Calendar for %1 %2"
188 ).arg(QDate::longMonthName(selectedDate.month())
189 ).arg(selectedDate.year()));
190}
191//! [14]
192
193//! [15]
194void MainWindow::setFontSize(int size)
195{
196 fontSize = size;
197 insertCalendar();
198}
199//! [15]
200
201//! [16]
202void MainWindow::setMonth(int month)
203{
204 selectedDate = QDate(selectedDate.year(), month + 1, selectedDate.day());
205 insertCalendar();
206}
207//! [16]
208
209//! [17]
210void MainWindow::setYear(QDate date)
211{
212 selectedDate = QDate(date.year(), selectedDate.month(), selectedDate.day());
213 insertCalendar();
214}
215//! [17]
Note: See TracBrowser for help on using the repository browser.