~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/richtext/calendar/mainwindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtGui>
 
30
 
 
31
#include "mainwindow.h"
 
32
 
 
33
MainWindow::MainWindow()
 
34
{
 
35
    selectedDate = QDate::currentDate();
 
36
    fontSize = 10;
 
37
 
 
38
    QWidget *centralWidget = new QWidget;
 
39
 
 
40
    QLabel *dateLabel = new QLabel(tr("Date:"));
 
41
    QComboBox *monthCombo = new QComboBox;
 
42
 
 
43
    for (int month = 1; month <= 12; ++month)
 
44
        monthCombo->addItem(QDate::longMonthName(month));
 
45
 
 
46
    QDateTimeEdit *yearEdit = new QDateTimeEdit;
 
47
    yearEdit->setDisplayFormat("yyyy");
 
48
    yearEdit->setDateRange(QDate(1753, 1, 1), QDate(8000, 1, 1));
 
49
 
 
50
    monthCombo->setCurrentIndex(selectedDate.month() - 1);
 
51
    yearEdit->setDate(selectedDate);
 
52
 
 
53
    QLabel *fontSizeLabel = new QLabel(tr("Font size:"));
 
54
    QSpinBox *fontSizeSpinBox = new QSpinBox;
 
55
    fontSizeSpinBox->setRange(1, 64);
 
56
    fontSizeSpinBox->setValue(10);
 
57
 
 
58
    editor = new QTextBrowser;
 
59
    insertCalendar();
 
60
 
 
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)));
 
65
 
 
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);
 
74
 
 
75
    QVBoxLayout *centralLayout = new QVBoxLayout;
 
76
    centralLayout->addLayout(controlsLayout);
 
77
    centralLayout->addWidget(editor, 1);
 
78
    centralWidget->setLayout(centralLayout);
 
79
 
 
80
    setCentralWidget(centralWidget);
 
81
}
 
82
 
 
83
void MainWindow::insertCalendar()
 
84
{
 
85
    editor->clear();
 
86
    QTextCursor cursor = editor->textCursor();
 
87
 
 
88
    QDate date(selectedDate.year(), selectedDate.month(), 1);
 
89
 
 
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);
 
104
 
 
105
    QTextTable *table = cursor.insertTable(1, 7, tableFormat);
 
106
 
 
107
    QTextFrame *frame = cursor.currentFrame();
 
108
    QTextFrameFormat frameFormat = frame->frameFormat();
 
109
    frameFormat.setBorder(1);
 
110
    frame->setFrameFormat(frameFormat);
 
111
 
 
112
    QTextCharFormat format = cursor.charFormat();
 
113
    format.setFontPointSize(fontSize);
 
114
 
 
115
    QTextCharFormat boldFormat = format;
 
116
    boldFormat.setFontWeight(QFont::Bold);
 
117
 
 
118
    QTextCharFormat highlightedFormat = boldFormat;
 
119
    highlightedFormat.setBackground(Qt::yellow);
 
120
 
 
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)),
 
125
                              boldFormat);
 
126
    }
 
127
 
 
128
    table->insertRows(table->rows(), 1);
 
129
 
 
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();
 
134
 
 
135
        if (date == QDate::currentDate())
 
136
            cellCursor.insertText(QString("%1").arg(date.day()), highlightedFormat);
 
137
        else
 
138
            cellCursor.insertText(QString("%1").arg(date.day()), format);
 
139
 
 
140
        date = date.addDays(1);
 
141
        if (weekDay == 7 && date.month() == selectedDate.month())
 
142
            table->insertRows(table->rows(), 1);
 
143
    }
 
144
 
 
145
    setWindowTitle(tr("Calendar for %1 %2"
 
146
        ).arg(QDate::longMonthName(selectedDate.month())
 
147
        ).arg(selectedDate.year()));
 
148
}
 
149
 
 
150
void MainWindow::setFontSize(int size)
 
151
{
 
152
    fontSize = size;
 
153
    insertCalendar();
 
154
}
 
155
 
 
156
void MainWindow::setMonth(int month)
 
157
{
 
158
    selectedDate = QDate(selectedDate.year(), month + 1, selectedDate.day());
 
159
    insertCalendar();
 
160
}
 
161
 
 
162
void MainWindow::setYear(QDate date)
 
163
{
 
164
    selectedDate = QDate(date.year(), selectedDate.month(), selectedDate.day());
 
165
    insertCalendar();
 
166
}