1 | /* $Id: UILineTextEdit.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UILineTextEdit class definitions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QDialogButtonBox>
|
---|
30 | #include <QFile>
|
---|
31 | #include <QLineEdit>
|
---|
32 | #include <QPushButton>
|
---|
33 | #include <QTextEdit>
|
---|
34 | #include <QTextStream>
|
---|
35 | #include <QVBoxLayout>
|
---|
36 |
|
---|
37 | /* GUI includes: */
|
---|
38 | #include "QIFileDialog.h"
|
---|
39 | #include "UICommon.h"
|
---|
40 | #include "UILineTextEdit.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | ////////////////////////////////////////////////////////////////////////////////
|
---|
44 | // UITextEditor
|
---|
45 |
|
---|
46 | UITextEditor::UITextEditor(QWidget *pParent /* = NULL */)
|
---|
47 | : QIWithRetranslateUI<QIDialog>(pParent)
|
---|
48 | {
|
---|
49 | QVBoxLayout *pMainLayout = new QVBoxLayout(this);
|
---|
50 | pMainLayout->setContentsMargins(12, 12, 12, 12);
|
---|
51 |
|
---|
52 | /* We need a text editor */
|
---|
53 | m_pTextEdit = new QTextEdit(this);
|
---|
54 | pMainLayout->addWidget(m_pTextEdit);
|
---|
55 | /* and some buttons to interact with */
|
---|
56 | m_pButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
|
---|
57 | m_pOpenButton = new QPushButton(this);
|
---|
58 | m_pButtonBox->addButton(m_pOpenButton, QDialogButtonBox::ActionRole);
|
---|
59 | pMainLayout->addWidget(m_pButtonBox);
|
---|
60 | /* Connect the buttons so that they are useful */
|
---|
61 | connect(m_pButtonBox, &QDialogButtonBox::accepted,
|
---|
62 | this, &UITextEditor::accept);
|
---|
63 | connect(m_pButtonBox, &QDialogButtonBox::rejected,
|
---|
64 | this, &UITextEditor::reject);
|
---|
65 | connect(m_pOpenButton, &QPushButton::clicked,
|
---|
66 | this, &UITextEditor::open);
|
---|
67 |
|
---|
68 | /* Applying language settings */
|
---|
69 | retranslateUi();
|
---|
70 | }
|
---|
71 |
|
---|
72 | void UITextEditor::setText(const QString& strText)
|
---|
73 | {
|
---|
74 | m_pTextEdit->setText(strText);
|
---|
75 | }
|
---|
76 |
|
---|
77 | QString UITextEditor::text() const
|
---|
78 | {
|
---|
79 | return m_pTextEdit->toPlainText();
|
---|
80 | }
|
---|
81 |
|
---|
82 | void UITextEditor::retranslateUi()
|
---|
83 | {
|
---|
84 | setWindowTitle(tr("Edit text"));
|
---|
85 | m_pOpenButton->setText(tr("&Replace..."));
|
---|
86 | m_pOpenButton->setToolTip(tr("Replaces the current text with the content of a file."));
|
---|
87 | }
|
---|
88 |
|
---|
89 | void UITextEditor::open()
|
---|
90 | {
|
---|
91 | QString fileName = QIFileDialog::getOpenFileName(uiCommon().documentsPath(), tr("Text (*.txt);;All (*.*)"), this, tr("Select a file to open..."));
|
---|
92 | if (!fileName.isEmpty())
|
---|
93 | {
|
---|
94 | QFile file(fileName);
|
---|
95 | if (file.open(QFile::ReadOnly))
|
---|
96 | {
|
---|
97 | QTextStream in(&file);
|
---|
98 | m_pTextEdit->setPlainText(in.readAll());
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | }
|
---|
103 |
|
---|
104 | ////////////////////////////////////////////////////////////////////////////////
|
---|
105 | // UILineTextEdit
|
---|
106 |
|
---|
107 | UILineTextEdit::UILineTextEdit(QWidget *pParent /* = NULL */)
|
---|
108 | : QIWithRetranslateUI<QPushButton>(pParent)
|
---|
109 | {
|
---|
110 | connect(this, &UILineTextEdit::clicked,
|
---|
111 | this, &UILineTextEdit::edit);
|
---|
112 |
|
---|
113 | /* Don't interpret the Enter Key. */
|
---|
114 | setAutoDefault(false);
|
---|
115 | setDefault(false);
|
---|
116 |
|
---|
117 | setFocusPolicy(Qt::StrongFocus);
|
---|
118 | retranslateUi();
|
---|
119 | }
|
---|
120 |
|
---|
121 | void UILineTextEdit::retranslateUi()
|
---|
122 | {
|
---|
123 | QPushButton::setText(tr("&Edit"));
|
---|
124 | }
|
---|
125 |
|
---|
126 | void UILineTextEdit::edit()
|
---|
127 | {
|
---|
128 | UITextEditor te(this);
|
---|
129 | te.setText(m_strText);
|
---|
130 | if (te.exec() == QDialog::Accepted)
|
---|
131 | {
|
---|
132 | m_strText = te.text();
|
---|
133 | /* Notify listener(s) about we finished: */
|
---|
134 | emit sigFinished(this);
|
---|
135 | }
|
---|
136 | }
|
---|