1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
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.
|
---|
25 | **
|
---|
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."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | #include "mainwindow.h"
|
---|
44 | #include "xmlwriter.h"
|
---|
45 |
|
---|
46 | MainWindow::MainWindow()
|
---|
47 | {
|
---|
48 | QMenu *fileMenu = new QMenu(tr("&File"));
|
---|
49 |
|
---|
50 | QAction *saveAction = fileMenu->addAction(tr("&Save..."));
|
---|
51 | saveAction->setShortcut(tr("Ctrl+S"));
|
---|
52 | QAction *quitAction = fileMenu->addAction(tr("E&xit"));
|
---|
53 | quitAction->setShortcut(tr("Ctrl+Q"));
|
---|
54 |
|
---|
55 | QMenu *showMenu = new QMenu(tr("&Show"));
|
---|
56 |
|
---|
57 | QAction *showTableAction = showMenu->addAction(tr("&Table"));
|
---|
58 |
|
---|
59 | menuBar()->addMenu(fileMenu);
|
---|
60 | menuBar()->addMenu(showMenu);
|
---|
61 |
|
---|
62 | editor = new QTextEdit();
|
---|
63 |
|
---|
64 | //! [0] //! [1]
|
---|
65 | QTextCursor cursor(editor->textCursor());
|
---|
66 | //! [0]
|
---|
67 | cursor.movePosition(QTextCursor::Start);
|
---|
68 | //! [1]
|
---|
69 |
|
---|
70 | int rows = 11;
|
---|
71 | int columns = 4;
|
---|
72 |
|
---|
73 | //! [2]
|
---|
74 | QTextTableFormat tableFormat;
|
---|
75 | tableFormat.setBackground(QColor("#e0e0e0"));
|
---|
76 | QVector<QTextLength> constraints;
|
---|
77 | constraints << QTextLength(QTextLength::PercentageLength, 16);
|
---|
78 | constraints << QTextLength(QTextLength::PercentageLength, 28);
|
---|
79 | constraints << QTextLength(QTextLength::PercentageLength, 28);
|
---|
80 | constraints << QTextLength(QTextLength::PercentageLength, 28);
|
---|
81 | tableFormat.setColumnWidthConstraints(constraints);
|
---|
82 | //! [3]
|
---|
83 | QTextTable *table = cursor.insertTable(rows, columns, tableFormat);
|
---|
84 | //! [2] //! [3]
|
---|
85 |
|
---|
86 | int column;
|
---|
87 | int row;
|
---|
88 | QTextTableCell cell;
|
---|
89 | QTextCursor cellCursor;
|
---|
90 |
|
---|
91 | QTextCharFormat charFormat;
|
---|
92 | charFormat.setForeground(Qt::black);
|
---|
93 |
|
---|
94 | //! [4]
|
---|
95 | cell = table->cellAt(0, 0);
|
---|
96 | cellCursor = cell.firstCursorPosition();
|
---|
97 | cellCursor.insertText(tr("Week"), charFormat);
|
---|
98 | //! [4]
|
---|
99 |
|
---|
100 | //! [5]
|
---|
101 | for (column = 1; column < columns; ++column) {
|
---|
102 | cell = table->cellAt(0, column);
|
---|
103 | cellCursor = cell.firstCursorPosition();
|
---|
104 | cellCursor.insertText(tr("Team %1").arg(column), charFormat);
|
---|
105 | }
|
---|
106 |
|
---|
107 | for (row = 1; row < rows; ++row) {
|
---|
108 | cell = table->cellAt(row, 0);
|
---|
109 | cellCursor = cell.firstCursorPosition();
|
---|
110 | cellCursor.insertText(tr("%1").arg(row), charFormat);
|
---|
111 |
|
---|
112 | for (column = 1; column < columns; ++column) {
|
---|
113 | if ((row-1) % 3 == column-1) {
|
---|
114 | //! [5] //! [6]
|
---|
115 | cell = table->cellAt(row, column);
|
---|
116 | QTextCursor cellCursor = cell.firstCursorPosition();
|
---|
117 | cellCursor.insertText(tr("On duty"), charFormat);
|
---|
118 | }
|
---|
119 | //! [6] //! [7]
|
---|
120 | }
|
---|
121 | //! [7] //! [8]
|
---|
122 | }
|
---|
123 | //! [8]
|
---|
124 |
|
---|
125 | connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile()));
|
---|
126 | connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
|
---|
127 | connect(showTableAction, SIGNAL(triggered()), this, SLOT(showTable()));
|
---|
128 |
|
---|
129 | setCentralWidget(editor);
|
---|
130 | setWindowTitle(tr("Text Document Tables"));
|
---|
131 | }
|
---|
132 |
|
---|
133 | void MainWindow::saveFile()
|
---|
134 | {
|
---|
135 | QString fileName = QFileDialog::getSaveFileName(this,
|
---|
136 | tr("Save document as:"), "", tr("XML (*.xml)"));
|
---|
137 |
|
---|
138 | if (!fileName.isEmpty()) {
|
---|
139 | if (writeXml(fileName))
|
---|
140 | setWindowTitle(fileName);
|
---|
141 | else
|
---|
142 | QMessageBox::warning(this, tr("Warning"),
|
---|
143 | tr("Failed to save the document."), QMessageBox::Cancel,
|
---|
144 | QMessageBox::NoButton);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | void MainWindow::showTable()
|
---|
149 | {
|
---|
150 | QTextCursor cursor = editor->textCursor();
|
---|
151 | QTextTable *table = cursor.currentTable();
|
---|
152 |
|
---|
153 | if (!table)
|
---|
154 | return;
|
---|
155 |
|
---|
156 | QTableWidget *tableWidget = new QTableWidget(table->rows(), table->columns());
|
---|
157 |
|
---|
158 | //! [9]
|
---|
159 | for (int row = 0; row < table->rows(); ++row) {
|
---|
160 | for (int column = 0; column < table->columns(); ++column) {
|
---|
161 | QTextTableCell tableCell = table->cellAt(row, column);
|
---|
162 | //! [9]
|
---|
163 | QTextFrame::iterator it;
|
---|
164 | QString text;
|
---|
165 | for (it = tableCell.begin(); !(it.atEnd()); ++it) {
|
---|
166 | QTextBlock childBlock = it.currentBlock();
|
---|
167 | if (childBlock.isValid())
|
---|
168 | text += childBlock.text();
|
---|
169 | }
|
---|
170 | QTableWidgetItem *newItem = new QTableWidgetItem(text);
|
---|
171 | tableWidget->setItem(row, column, newItem);
|
---|
172 | /*
|
---|
173 | //! [10]
|
---|
174 | processTableCell(tableCell);
|
---|
175 | //! [10]
|
---|
176 | */
|
---|
177 | //! [11]
|
---|
178 | }
|
---|
179 | //! [11] //! [12]
|
---|
180 | }
|
---|
181 | //! [12]
|
---|
182 |
|
---|
183 | tableWidget->setWindowTitle(tr("Table Contents"));
|
---|
184 | tableWidget->show();
|
---|
185 | }
|
---|
186 |
|
---|
187 | bool MainWindow::writeXml(const QString &fileName)
|
---|
188 | {
|
---|
189 | XmlWriter documentWriter(editor->document());
|
---|
190 |
|
---|
191 | QDomDocument *domDocument = documentWriter.toXml();
|
---|
192 | QFile file(fileName);
|
---|
193 |
|
---|
194 | if (file.open(QFile::WriteOnly)) {
|
---|
195 | QTextStream textStream(&file);
|
---|
196 | textStream.setCodec(QTextCodec::codecForName("UTF-8"));
|
---|
197 |
|
---|
198 | textStream << domDocument->toString(1).toUtf8();
|
---|
199 | file.close();
|
---|
200 | return true;
|
---|
201 | }
|
---|
202 | else
|
---|
203 | return false;
|
---|
204 | }
|
---|