1
/****************************************************************************
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include "mainwindow.h"
33
MainWindow::MainWindow()
35
selectedDate = QDate::currentDate();
38
QWidget *centralWidget = new QWidget;
40
QLabel *dateLabel = new QLabel(tr("Date:"));
41
QComboBox *monthCombo = new QComboBox;
43
for (int month = 1; month <= 12; ++month)
44
monthCombo->addItem(QDate::longMonthName(month));
46
QDateTimeEdit *yearEdit = new QDateTimeEdit;
47
yearEdit->setDisplayFormat("yyyy");
48
yearEdit->setDateRange(QDate(1753, 1, 1), QDate(8000, 1, 1));
50
monthCombo->setCurrentIndex(selectedDate.month() - 1);
51
yearEdit->setDate(selectedDate);
53
QLabel *fontSizeLabel = new QLabel(tr("Font size:"));
54
QSpinBox *fontSizeSpinBox = new QSpinBox;
55
fontSizeSpinBox->setRange(1, 64);
56
fontSizeSpinBox->setValue(10);
58
editor = new QTextBrowser;
61
connect(monthCombo, SIGNAL(activated(int)), this, SLOT(setMonth(int)));
62
connect(yearEdit, SIGNAL(dateChanged(QDate)), this, SLOT(setYear(QDate)));
63
connect(fontSizeSpinBox, SIGNAL(valueChanged(int)),
64
this, SLOT(setFontSize(int)));
66
QHBoxLayout *controlsLayout = new QHBoxLayout;
67
controlsLayout->addWidget(dateLabel);
68
controlsLayout->addWidget(monthCombo);
69
controlsLayout->addWidget(yearEdit);
70
controlsLayout->addSpacing(24);
71
controlsLayout->addWidget(fontSizeLabel);
72
controlsLayout->addWidget(fontSizeSpinBox);
73
controlsLayout->addStretch(1);
75
QVBoxLayout *centralLayout = new QVBoxLayout;
76
centralLayout->addLayout(controlsLayout);
77
centralLayout->addWidget(editor, 1);
78
centralWidget->setLayout(centralLayout);
80
setCentralWidget(centralWidget);
83
void MainWindow::insertCalendar()
86
QTextCursor cursor = editor->textCursor();
88
QDate date(selectedDate.year(), selectedDate.month(), 1);
90
QTextTableFormat tableFormat;
91
tableFormat.setAlignment(Qt::AlignHCenter);
92
tableFormat.setBackground(QColor("#e0e0e0"));
93
tableFormat.setCellPadding(2);
94
tableFormat.setCellSpacing(4);
95
QVector<QTextLength> constraints;
96
constraints << QTextLength(QTextLength::PercentageLength, 14)
97
<< QTextLength(QTextLength::PercentageLength, 14)
98
<< QTextLength(QTextLength::PercentageLength, 14)
99
<< QTextLength(QTextLength::PercentageLength, 14)
100
<< QTextLength(QTextLength::PercentageLength, 14)
101
<< QTextLength(QTextLength::PercentageLength, 14)
102
<< QTextLength(QTextLength::PercentageLength, 14);
103
tableFormat.setColumnWidthConstraints(constraints);
105
QTextTable *table = cursor.insertTable(1, 7, tableFormat);
107
QTextFrame *frame = cursor.currentFrame();
108
QTextFrameFormat frameFormat = frame->frameFormat();
109
frameFormat.setBorder(1);
110
frame->setFrameFormat(frameFormat);
112
QTextCharFormat format = cursor.charFormat();
113
format.setFontPointSize(fontSize);
115
QTextCharFormat boldFormat = format;
116
boldFormat.setFontWeight(QFont::Bold);
118
QTextCharFormat highlightedFormat = boldFormat;
119
highlightedFormat.setBackground(Qt::yellow);
121
for (int weekDay = 1; weekDay <= 7; ++weekDay) {
122
QTextTableCell cell = table->cellAt(0, weekDay-1);
123
QTextCursor cellCursor = cell.firstCursorPosition();
124
cellCursor.insertText(QString("%1").arg(QDate::longDayName(weekDay)),
128
table->insertRows(table->rows(), 1);
130
while (date.month() == selectedDate.month()) {
131
int weekDay = date.dayOfWeek();
132
QTextTableCell cell = table->cellAt(table->rows()-1, weekDay-1);
133
QTextCursor cellCursor = cell.firstCursorPosition();
135
if (date == QDate::currentDate())
136
cellCursor.insertText(QString("%1").arg(date.day()), highlightedFormat);
138
cellCursor.insertText(QString("%1").arg(date.day()), format);
140
date = date.addDays(1);
141
if (weekDay == 7 && date.month() == selectedDate.month())
142
table->insertRows(table->rows(), 1);
145
setWindowTitle(tr("Calendar for %1 %2"
146
).arg(QDate::longMonthName(selectedDate.month())
147
).arg(selectedDate.year()));
150
void MainWindow::setFontSize(int size)
156
void MainWindow::setMonth(int month)
158
selectedDate = QDate(selectedDate.year(), month + 1, selectedDate.day());
162
void MainWindow::setYear(QDate date)
164
selectedDate = QDate(date.year(), selectedDate.month(), selectedDate.day());