10 QMenu *fileMenu =
new QMenu(tr(
"&File"));
12 QAction *saveAction = fileMenu->addAction(tr(
"&Save..."));
13 saveAction->setShortcut(tr(
"Ctrl+S"));
14 QAction *quitAction = fileMenu->addAction(tr(
"E&xit"));
15 quitAction->setShortcut(tr(
"Ctrl+Q"));
17 QMenu *showMenu =
new QMenu(tr(
"&Show"));
19 QAction *showTableAction = showMenu->addAction(tr(
"&Table"));
21 menuBar()->addMenu(fileMenu);
22 menuBar()->addMenu(showMenu);
24 editor =
new QTextEdit();
27 QTextCursor cursor(editor->textCursor());
29 cursor.movePosition(QTextCursor::Start);
36 QTextTableFormat tableFormat;
37 tableFormat.setBackground(QColor(
"#e0e0e0"));
38 QList<QTextLength> constraints;
39 constraints << QTextLength(QTextLength::PercentageLength, 16);
40 constraints << QTextLength(QTextLength::PercentageLength, 28);
41 constraints << QTextLength(QTextLength::PercentageLength, 28);
42 constraints << QTextLength(QTextLength::PercentageLength, 28);
43 tableFormat.setColumnWidthConstraints(constraints);
45 QTextTable *table = cursor.insertTable(rows, columns, tableFormat);
51 QTextCursor cellCursor;
53 QTextCharFormat charFormat;
54 charFormat.setForeground(Qt::black);
57 cell = table->cellAt(0, 0);
58 cellCursor = cell.firstCursorPosition();
59 cellCursor.insertText(tr(
"Week"), charFormat);
63 for (column = 1; column < columns; ++column) {
64 cell = table->cellAt(0, column);
65 cellCursor = cell.firstCursorPosition();
66 cellCursor.insertText(tr(
"Team %1").arg(column), charFormat);
69 for (row = 1; row < rows; ++row) {
70 cell = table->cellAt(row, 0);
71 cellCursor = cell.firstCursorPosition();
72 cellCursor.insertText(tr(
"%1").arg(row), charFormat);
74 for (column = 1; column < columns; ++column) {
75 if ((row-1) % 3 == column-1) {
77 cell = table->cellAt(row, column);
78 QTextCursor cellCursor = cell.firstCursorPosition();
79 cellCursor.insertText(tr(
"On duty"), charFormat);
87 connect(saveAction, &QAction::triggered,
this, &MainWindow::saveFile);
88 connect(quitAction, &QAction::triggered,
this, &MainWindow::close);
89 connect(showTableAction, &QAction::triggered,
this, &MainWindow::showTable);
91 setCentralWidget(editor);
92 setWindowTitle(tr(
"Text Document Tables"));
97 QString fileName = QFileDialog::getSaveFileName(
this,
98 tr(
"Save document as:"),
"", tr(
"XML (*.xml)"));
100 if (!fileName.isEmpty()) {
101 if (writeXml(fileName))
102 setWindowTitle(fileName);
104 QMessageBox::warning(
this, tr(
"Warning"),
105 tr(
"Failed to save the document."), QMessageBox::Cancel,
106 QMessageBox::NoButton);
112 QTextCursor cursor = editor->textCursor();
113 QTextTable *table = cursor.currentTable();
118 QTableWidget *tableWidget =
new QTableWidget(table->rows(), table->columns());
121 for (
int row = 0; row < table->rows(); ++row) {
122 for (
int column = 0; column < table->columns(); ++column) {
123 QTextTableCell tableCell = table->cellAt(row, column);
125 QTextFrame::iterator it;
127 for (it = tableCell.begin(); !(it.atEnd()); ++it) {
128 QTextBlock childBlock = it.currentBlock();
129 if (childBlock.isValid())
130 text += childBlock.text();
132 QTableWidgetItem *newItem =
new QTableWidgetItem(text);
133 tableWidget->setItem(row, column, newItem);
135
136
137
138
145 tableWidget->setWindowTitle(tr(
"Table Contents"));
157void MainWindow::processTable(QTextTable *table)
159 QTextFrame *frame = qobject_cast<QTextFrame *>(table);
161 QTextFrame::iterator it;
162 for (it = frame->begin(); !(it.atEnd()); ++it) {
164 QTextFrame *childFrame = it.currentFrame();
165 QTextBlock childBlock = it.currentBlock();
168 QTextTable *childTable = qobject_cast<QTextTable*>(childFrame);
171 processTable(childTable);
173 processFrame(childFrame);
175 }
else if (childBlock.isValid()) {
176 processBlock(childBlock);