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

« back to all changes in this revision

Viewing changes to doc/html/richtext-calendar.html

  • 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
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/calendar.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Calendar Example</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Calendar Example</h1>
 
21
<p>Files:</p>
 
22
<ul>
 
23
<li><a href="richtext-calendar-mainwindow-cpp.html">richtext/calendar/mainwindow.cpp</a></li>
 
24
<li><a href="richtext-calendar-mainwindow-h.html">richtext/calendar/mainwindow.h</a></li>
 
25
<li><a href="richtext-calendar-main-cpp.html">richtext/calendar/main.cpp</a></li>
 
26
</ul>
 
27
<p>The Calendar example shows how to create rich text content and display it using a rich text editor.</p>
 
28
<center><img src="images/calendar-example.png" /></center><p>Specifically, the example demonstrates the following:</p>
 
29
<ul>
 
30
<li>Use of a text editor with a text document</li>
 
31
<li>Insertion of tables and frames into a document</li>
 
32
<li>Navigation within a table</li>
 
33
<li>Insert text in different styles</li>
 
34
</ul>
 
35
<p>The rich text editor used to display the document is used within a main window application.</p>
 
36
<a name="mainwindow-class-definition"></a>
 
37
<h2>MainWindow Class Definition</h2>
 
38
<p>The <tt>MainWindow</tt> class provides a text editor widget and some controls to allow the user to change the month and year shown. The font size used for the text can also be adjusted.</p>
 
39
<pre>&nbsp;   class MainWindow : public QMainWindow
 
40
    {
 
41
        Q_OBJECT
 
42
 
 
43
    public:
 
44
        MainWindow();
 
45
 
 
46
    public slots:
 
47
        void setFontSize(int size);
 
48
        void setMonth(int month);
 
49
        void setYear(QDate date);
 
50
 
 
51
    private:
 
52
        void insertCalendar();
 
53
 
 
54
        int fontSize;
 
55
        QDate selectedDate;
 
56
        QTextBrowser *editor;
 
57
    };</pre>
 
58
<p>The private <tt>insertCalendar()</tt> function performs most of the work, relying on the <tt>fontSize</tt> and <tt>selectedDate</tt> variables to write useful information to the <tt>editor</tt>.</p>
 
59
<a name="mainwindow-class-implementation"></a>
 
60
<h2>MainWindow Class Implementation</h2>
 
61
<p>The <tt>MainWindow</tt> constructor sets up the user interface and initializes variables used to generate a calendar for each month.</p>
 
62
<pre>&nbsp;   MainWindow::MainWindow()
 
63
    {
 
64
        selectedDate = QDate::currentDate();
 
65
        fontSize = 10;
 
66
 
 
67
        QWidget *centralWidget = new QWidget;</pre>
 
68
<p>We begin by setting default values for the selected date that will be highlighted in the calendar and the font size to be used. Since we are using a <a href="qmainwindow.html">QMainWindow</a> for the user interface, we construct a widget for use as the central widget.</p>
 
69
<p>The user interface will include a line of controls above the generated calendar; we construct a label and a combobox to allow the month to be selected, and a spin box for the year. These widgets are configured to provide a reasonable range of values for the user to try:</p>
 
70
<pre>&nbsp;       QLabel *dateLabel = new QLabel(tr(&quot;Date:&quot;));
 
71
        QComboBox *monthCombo = new QComboBox;
 
72
 
 
73
        for (int month = 1; month &lt;= 12; ++month)
 
74
            monthCombo-&gt;addItem(QDate::longMonthName(month));
 
75
 
 
76
        QDateTimeEdit *yearEdit = new QDateTimeEdit;
 
77
        yearEdit-&gt;setDisplayFormat(&quot;yyyy&quot;);
 
78
        yearEdit-&gt;setDateRange(QDate(1753, 1, 1), QDate(8000, 1, 1));</pre>
 
79
<p>We use the <tt>selectedDate</tt> object to obtain the current month and year, and we set these in the combobox and spin box:</p>
 
80
<p>The font size is displayed in a spin box which we restrict to a sensible range of values:</p>
 
81
<pre>&nbsp;       QLabel *fontSizeLabel = new QLabel(tr(&quot;Font size:&quot;));
 
82
        QSpinBox *fontSizeSpinBox = new QSpinBox;
 
83
        fontSizeSpinBox-&gt;setRange(1, 64);
 
84
        fontSizeSpinBox-&gt;setValue(10);
 
85
 
 
86
        editor = new QTextBrowser;
 
87
        insertCalendar();</pre>
 
88
<p>We construct an editor and use the <tt>insertCalendar()</tt> function to create a calendar for it. Each calendar is displayed in the same text editor; in this example we use a <a href="qtextbrowser.html">QTextBrowser</a> since we do not allow the calendar to be edited.</p>
 
89
<p>The controls used to set the month, year, and font size will not have any effect on the appearance of the calendar unless we make some signal-slot connections:</p>
 
90
<pre>&nbsp;       connect(monthCombo, SIGNAL(activated(int)), this, SLOT(setMonth(int)));
 
91
        connect(yearEdit, SIGNAL(dateChanged(QDate)), this, SLOT(setYear(QDate)));
 
92
        connect(fontSizeSpinBox, SIGNAL(valueChanged(int)),
 
93
                this, SLOT(setFontSize(int)));</pre>
 
94
<p>The signals are connected to some simple slots in the <tt>MainWindow</tt> class which we will describe later.</p>
 
95
<p>We create layouts to manage the widgets we constructed:</p>
 
96
<pre>&nbsp;       QHBoxLayout *controlsLayout = new QHBoxLayout;
 
97
        controlsLayout-&gt;addWidget(dateLabel);
 
98
        controlsLayout-&gt;addWidget(monthCombo);
 
99
        controlsLayout-&gt;addWidget(yearEdit);
 
100
        controlsLayout-&gt;addSpacing(24);
 
101
        controlsLayout-&gt;addWidget(fontSizeLabel);
 
102
        controlsLayout-&gt;addWidget(fontSizeSpinBox);
 
103
        controlsLayout-&gt;addStretch(1);
 
104
 
 
105
        QVBoxLayout *centralLayout = new QVBoxLayout;
 
106
        centralLayout-&gt;addLayout(controlsLayout);
 
107
        centralLayout-&gt;addWidget(editor, 1);
 
108
        centralWidget-&gt;setLayout(centralLayout);
 
109
 
 
110
        setCentralWidget(centralWidget);</pre>
 
111
<p>Finally, the central widget is set for the window.</p>
 
112
<p>Each calendar is created for the editor by the <tt>insertCalendar()</tt> function which uses the date and font size, defined by the private <i>selectedDate</i> and <tt>fontSize</tt> variables, to produce a suitable plan for the specified month and year.</p>
 
113
<pre>&nbsp;   void MainWindow::insertCalendar()
 
114
    {
 
115
        editor-&gt;clear();
 
116
        QTextCursor cursor = editor-&gt;textCursor();
 
117
 
 
118
        QDate date(selectedDate.year(), selectedDate.month(), 1);</pre>
 
119
<p>We begin by clearing the editor's rich text document, and obtain a text cursor from the editor that we will use to add content. We also create a <a href="qdate.html">QDate</a> object based on the currently selected date.</p>
 
120
<p>The calendar is made up of a table with a gray background color that contains seven columns: one for each day of the week. It is placed in the center of the page with equal space to the left and right of it. All of these properties are set in a <a href="qtexttableformat.html">QTextTableFormat</a> object:</p>
 
121
<pre>&nbsp;       QTextTableFormat tableFormat;
 
122
        tableFormat.setAlignment(Qt::AlignHCenter);
 
123
        tableFormat.setBackground(QColor(&quot;#e0e0e0&quot;));
 
124
        tableFormat.setCellPadding(2);
 
125
        tableFormat.setCellSpacing(4);</pre>
 
126
<p>Each cell in the table will be padded and spaced to make the text easier to read.</p>
 
127
<p>We want the columns to have equal widths, so we provide a vector containing percentage widths for each of them and set the constraints in the <a href="qtexttableformat.html">QTextTableFormat</a>:</p>
 
128
<pre>&nbsp;       QVector&lt;QTextLength&gt; constraints;
 
129
        constraints &lt;&lt; QTextLength(QTextLength::PercentageLength, 14)
 
130
                    &lt;&lt; QTextLength(QTextLength::PercentageLength, 14)
 
131
                    &lt;&lt; QTextLength(QTextLength::PercentageLength, 14)
 
132
                    &lt;&lt; QTextLength(QTextLength::PercentageLength, 14)
 
133
                    &lt;&lt; QTextLength(QTextLength::PercentageLength, 14)
 
134
                    &lt;&lt; QTextLength(QTextLength::PercentageLength, 14)
 
135
                    &lt;&lt; QTextLength(QTextLength::PercentageLength, 14);
 
136
        tableFormat.setColumnWidthConstraints(constraints);</pre>
 
137
<p>The constraints used for the column widths are only useful if the table has an appropriate number of columns. With the format for the table defined, we construct a new table with one row and seven columns at the current cursor position:</p>
 
138
<pre>&nbsp;       QTextTable *table = cursor.insertTable(1, 7, tableFormat);</pre>
 
139
<p>We only need one row to start with; more can be added as we need them. Using this approach means that we do not need to perform any date calculations until we add cells to the table.</p>
 
140
<p>When inserting objects into a document with the cursor's insertion functions, the cursor is automatically moved inside the newly inserted object. This means that we can immediately start modifying the table from within:</p>
 
141
<pre>&nbsp;       QTextFrame *frame = cursor.currentFrame();
 
142
        QTextFrameFormat frameFormat = frame-&gt;frameFormat();
 
143
        frameFormat.setBorder(1);
 
144
        frame-&gt;setFrameFormat(frameFormat);</pre>
 
145
<p>Since the table has an outer frame, we obtain the frame and its format so that we can customize it. After making the changes we want, we set the frame's format using the modified format object. We have given the table an outer border one pixel wide.</p>
 
146
<pre>&nbsp;       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);</pre>
 
154
<p>In a similar way, we obtain the cursor's current character format and create customized formats based on it.</p>
 
155
<p>We do not set the format on the cursor because this would change the default character format; instead, we use the customized formats explicitly when we insert text. The following loop inserts the days of the week into the table as bold text:</p>
 
156
<pre>&nbsp;       for (int weekDay = 1; weekDay &lt;= 7; ++weekDay) {
 
157
            QTextTableCell cell = table-&gt;cellAt(0, weekDay-1);</pre>
 
158
<p>For each day of the week, we obtain an existing table cell in the first row (row 0) using the table's <a href="qtexttable.html#cellAt">cellAt()</a> function. Since we start counting the days of the week at day 1 (Monday), we subtract 1 from <tt>weekDay</tt> to ensure that we obtain the cell for the correct column of the table.</p>
 
159
<p>Before text can be inserted into a cell, we must obtain a cursor with the correct position in the document. The cell provides a function for this purpose, and we use this cursor to insert text using the <tt>boldFormat</tt> character format that we created earlier:</p>
 
160
<pre>&nbsp;           QTextCursor cellCursor = cell.firstCursorPosition();
 
161
            cellCursor.insertText(QString(&quot;%1&quot;).arg(QDate::longDayName(weekDay)),
 
162
                                  boldFormat);
 
163
        }</pre>
 
164
<p>Inserting text into document objects usually follows the same pattern. Each object can provide a new cursor that corresponds to the first valid position within itself, and this can be used to insert new content. We continue to use this pattern as we insert the days of the month into the table.</p>
 
165
<p>Since every month has more than seven days, we insert a single row to begin and add days until we reach the end of the month. If the current date is encountered, it is inserted with a special format (created earlier) that makes it stand out:</p>
 
166
<pre>&nbsp;       table-&gt;insertRows(table-&gt;rows(), 1);</pre>
 
167
<p>We add a new row to the table at the end of each week only if the next week falls within the currently selected month.</p>
 
168
<p>For each calendar that we create, we change the window title to reflect the currently selected month and year:</p>
 
169
<pre>&nbsp;       setWindowTitle(tr(&quot;Calendar for %1 %2&quot;
 
170
            ).arg(QDate::longMonthName(selectedDate.month())
 
171
            ).arg(selectedDate.year()));
 
172
    }</pre>
 
173
<p>The <tt>insertCalendar()</tt> function relies on up-to-date values for the month, year, and font size. These are set in the following slots:</p>
 
174
<pre>&nbsp;   void MainWindow::setFontSize(int size)
 
175
    {
 
176
        fontSize = size;
 
177
        insertCalendar();
 
178
    }</pre>
 
179
<p>The <tt>setFontSize()</tt> function simply changes the private <tt>fontSize</tt> variable before updating the calendar.</p>
 
180
<pre>&nbsp;   void MainWindow::setMonth(int month)
 
181
    {
 
182
        selectedDate = QDate(selectedDate.year(), month + 1, selectedDate.day());
 
183
        insertCalendar();
 
184
    }</pre>
 
185
<p>The <tt>setMonth</tt> slot is called when the <a href="qcombobox.html">QComboBox</a> used to select the month is updated. The value supplied is the currently selected row in the combobox. We add 1 to this value to obtain a valid month number, and create a new <a href="qdate.html">QDate</a> based on the existing one. The calendar is then updated to use this new date.</p>
 
186
<pre>&nbsp;   void MainWindow::setYear(QDate date)
 
187
    {
 
188
        selectedDate = QDate(date.year(), selectedDate.month(), selectedDate.day());
 
189
        insertCalendar();
 
190
    }</pre>
 
191
<p>The <tt>setYear()</tt> slot is called when the <a href="qdatetimeedit.html">QDateTimeEdit</a> used to select the year is updated. The value supplied is a <a href="qdate.html">QDate</a> object; this makes the construction of a new value for <tt>selectedDate</tt> simple. We update the calendar afterwards to use this new date.</p>
 
192
<p /><address><hr /><div align="center">
 
193
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
194
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
195
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
196
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
197
</tr></table></div></address></body>
 
198
</html>