Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
mainwindow.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3#include "mainwindow.h"
4
5#include <QtWidgets>
6
8{
9 QMenu *fileMenu = new QMenu(tr("&File"));
10
11 fileMenu->addAction(tr("&Open..."), QKeySequence(tr("Ctrl+O", "File|Open")),
13
14 QAction *quitAction = fileMenu->addAction(tr("E&xit"), this, &MainWindow::close);
15 quitAction->setShortcut(tr("Ctrl+Q"));
16
17 QMenu *editMenu = new QMenu(tr("&Edit"));
18
19 cutAction = editMenu->addAction(tr("Cu&t"), this, &MainWindow::cutSelection);
20 cutAction->setShortcut(tr("Ctrl+X"));
21 cutAction->setEnabled(false);
22
23 copyAction = editMenu->addAction(tr("&Copy"), this, &MainWindow::copySelection);
24 copyAction->setShortcut(tr("Ctrl+C"));
25 copyAction->setEnabled(false);
26
27 pasteAction = editMenu->addAction(tr("&Paste"), this, &MainWindow::pasteSelection);
28 pasteAction->setShortcut(tr("Ctrl+V"));
29 pasteAction->setEnabled(false);
30
31 QMenu *selectMenu = new QMenu(tr("&Select"));
32 selectMenu->addAction(tr("&Word"), this, &MainWindow::selectWord);
33 selectMenu->addAction(tr("&Line"), this, &MainWindow::selectLine);
34 selectMenu->addAction(tr("&Block"), this, &MainWindow::selectBlock);
35 selectMenu->addAction(tr("&Frame"), this, &MainWindow::selectFrame);
36
37 menuBar()->addMenu(fileMenu);
38 menuBar()->addMenu(editMenu);
39 menuBar()->addMenu(selectMenu);
40
41 editor = new QTextEdit(this);
42 document = new QTextDocument(this);
43 editor->setDocument(document);
44
45 connect(editor, &QTextEdit::selectionChanged,
46 this, &MainWindow::updateMenus);
47
48 setCentralWidget(editor);
49 setWindowTitle(tr("Text Document Writer"));
50}
51
52void MainWindow::openFile()
53{
54 QString fileName = QFileDialog::getOpenFileName(this,
55 tr("Open file"), currentFile, "HTML files (*.html);;Text files (*.txt)");
56
57 if (!fileName.isEmpty()) {
58 QFileInfo info(fileName);
59 if (info.completeSuffix() == "html") {
60 QFile file(fileName);
61
62 if (file.open(QFile::ReadOnly)) {
63 editor->setHtml(QString(file.readAll()));
64 file.close();
65 currentFile = fileName;
66 }
67 } else if (info.completeSuffix() == "txt") {
68 QFile file(fileName);
69
70 if (file.open(QFile::ReadOnly)) {
71 editor->setPlainText(file.readAll());
72 file.close();
73 currentFile = fileName;
74 }
75 }
76 }
77}
78
79void MainWindow::cutSelection()
80{
81 QTextCursor cursor = editor->textCursor();
82 if (cursor.hasSelection()) {
83 selection = cursor.selection();
84 cursor.removeSelectedText();
85 }
86}
87
89{
90 QTextCursor cursor = editor->textCursor();
91 if (cursor.hasSelection()) {
92 selection = cursor.selection();
93 cursor.clearSelection();
94 }
95}
96
98{
99 QTextCursor cursor = editor->textCursor();
100 cursor.insertFragment(selection);
101}
102
104{
105 QTextCursor cursor = editor->textCursor();
106
107//! [0]
108 cursor.beginEditBlock();
109//! [1]
110 cursor.movePosition(QTextCursor::StartOfWord);
111 cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
112//! [1]
113 cursor.endEditBlock();
114//! [0]
115
116 editor->setTextCursor(cursor);
117}
118
120{
121 QTextCursor cursor = editor->textCursor();
122
123 cursor.beginEditBlock();
124 cursor.movePosition(QTextCursor::StartOfLine);
125 cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
126 cursor.endEditBlock();
127
128 editor->setTextCursor(cursor);
129}
130
132{
133 QTextCursor cursor = editor->textCursor();
134
135 cursor.beginEditBlock();
136 cursor.movePosition(QTextCursor::StartOfBlock);
137 cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
138 cursor.endEditBlock();
139
140 editor->setTextCursor(cursor);
141}
142
144{
145 QTextCursor cursor = editor->textCursor();
146 QTextFrame *frame = cursor.currentFrame();
147
148 cursor.beginEditBlock();
149 cursor.setPosition(frame->firstPosition());
150 cursor.setPosition(frame->lastPosition(), QTextCursor::KeepAnchor);
151 cursor.endEditBlock();
152
153 editor->setTextCursor(cursor);
154}
155
157{
158 QTextCursor cursor = editor->textCursor();
159 cutAction->setEnabled(cursor.hasSelection());
160 copyAction->setEnabled(cursor.hasSelection());
161
162 pasteAction->setEnabled(!selection.isEmpty());
163}
void selectBlock()
void copySelection()
void openFile()
void selectLine()
void selectFrame()
void pasteSelection()
void selectWord()
void updateMenus()