9 QMenu *fileMenu =
new QMenu(tr(
"&File"));
11 fileMenu->addAction(tr(
"&Open..."), QKeySequence(tr(
"Ctrl+O",
"File|Open")),
14 QAction *quitAction = fileMenu->addAction(tr(
"E&xit"),
this, &MainWindow::close);
15 quitAction->setShortcut(tr(
"Ctrl+Q"));
17 QMenu *editMenu =
new QMenu(tr(
"&Edit"));
19 cutAction = editMenu->addAction(tr(
"Cu&t"),
this, &MainWindow::cutSelection);
20 cutAction->setShortcut(tr(
"Ctrl+X"));
21 cutAction->setEnabled(
false);
24 copyAction->setShortcut(tr(
"Ctrl+C"));
25 copyAction->setEnabled(
false);
28 pasteAction->setShortcut(tr(
"Ctrl+V"));
29 pasteAction->setEnabled(
false);
31 QMenu *selectMenu =
new QMenu(tr(
"&Select"));
37 menuBar()->addMenu(fileMenu);
38 menuBar()->addMenu(editMenu);
39 menuBar()->addMenu(selectMenu);
41 editor =
new QTextEdit(
this);
42 document =
new QTextDocument(
this);
43 editor->setDocument(document);
45 connect(editor, &QTextEdit::selectionChanged,
46 this, &MainWindow::updateMenus);
48 setCentralWidget(editor);
49 setWindowTitle(tr(
"Text Document Writer"));
54 QString fileName = QFileDialog::getOpenFileName(
this,
55 tr(
"Open file"), currentFile,
"HTML files (*.html);;Text files (*.txt)");
57 if (!fileName.isEmpty()) {
58 QFileInfo info(fileName);
59 if (info.completeSuffix() ==
"html") {
62 if (file.open(QFile::ReadOnly)) {
63 editor->setHtml(QString(file.readAll()));
65 currentFile = fileName;
67 }
else if (info.completeSuffix() ==
"txt") {
70 if (file.open(QFile::ReadOnly)) {
71 editor->setPlainText(file.readAll());
73 currentFile = fileName;
81 QTextCursor cursor = editor->textCursor();
82 if (cursor.hasSelection()) {
83 selection = cursor.selection();
84 cursor.removeSelectedText();
90 QTextCursor cursor = editor->textCursor();
91 if (cursor.hasSelection()) {
92 selection = cursor.selection();
93 cursor.clearSelection();
99 QTextCursor cursor = editor->textCursor();
100 cursor.insertFragment(selection);
105 QTextCursor cursor = editor->textCursor();
108 cursor.beginEditBlock();
110 cursor.movePosition(QTextCursor::StartOfWord);
111 cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
113 cursor.endEditBlock();
116 editor->setTextCursor(cursor);
121 QTextCursor cursor = editor->textCursor();
123 cursor.beginEditBlock();
124 cursor.movePosition(QTextCursor::StartOfLine);
125 cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
126 cursor.endEditBlock();
128 editor->setTextCursor(cursor);
133 QTextCursor cursor = editor->textCursor();
135 cursor.beginEditBlock();
136 cursor.movePosition(QTextCursor::StartOfBlock);
137 cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
138 cursor.endEditBlock();
140 editor->setTextCursor(cursor);
145 QTextCursor cursor = editor->textCursor();
146 QTextFrame *frame = cursor.currentFrame();
148 cursor.beginEditBlock();
149 cursor.setPosition(frame->firstPosition());
150 cursor.setPosition(frame->lastPosition(), QTextCursor::KeepAnchor);
151 cursor.endEditBlock();
153 editor->setTextCursor(cursor);
158 QTextCursor cursor = editor->textCursor();
159 cutAction->setEnabled(cursor.hasSelection());
160 copyAction->setEnabled(cursor.hasSelection());
162 pasteAction->setEnabled(!selection.isEmpty());