VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/widgets/UILineTextEdit.cpp

Last change on this file was 104358, checked in by vboxsync, 4 weeks ago

FE/Qt. bugref:10622. More refactoring around the retranslation functionality.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1/* $Id: UILineTextEdit.cpp 104358 2024-04-18 05:33:40Z 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#include "UITranslationEventListener.h"
42
43////////////////////////////////////////////////////////////////////////////////
44// UITextEditor
45
46UITextEditor::UITextEditor(QWidget *pParent /* = NULL */)
47 : 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 sltRetranslateUI();
70 connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
71 this, &UITextEditor::sltRetranslateUI);
72}
73
74void UITextEditor::setText(const QString& strText)
75{
76 m_pTextEdit->setText(strText);
77}
78
79QString UITextEditor::text() const
80{
81 return m_pTextEdit->toPlainText();
82}
83
84void UITextEditor::sltRetranslateUI()
85{
86 setWindowTitle(tr("Edit text"));
87 m_pOpenButton->setText(tr("&Replace..."));
88 m_pOpenButton->setToolTip(tr("Replaces the current text with the content of a file."));
89}
90
91void UITextEditor::open()
92{
93 QString fileName = QIFileDialog::getOpenFileName(uiCommon().documentsPath(), tr("Text (*.txt);;All (*.*)"), this, tr("Select a file to open..."));
94 if (!fileName.isEmpty())
95 {
96 QFile file(fileName);
97 if (file.open(QFile::ReadOnly))
98 {
99 QTextStream in(&file);
100 m_pTextEdit->setPlainText(in.readAll());
101 }
102 }
103
104}
105
106////////////////////////////////////////////////////////////////////////////////
107// UILineTextEdit
108
109UILineTextEdit::UILineTextEdit(QWidget *pParent /* = NULL */)
110 : QPushButton(pParent)
111{
112 connect(this, &UILineTextEdit::clicked,
113 this, &UILineTextEdit::edit);
114
115 /* Don't interpret the Enter Key. */
116 setAutoDefault(false);
117 setDefault(false);
118
119 setFocusPolicy(Qt::StrongFocus);
120 sltRetranslateUI();
121 connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
122 this, &UILineTextEdit::sltRetranslateUI);
123}
124
125void UILineTextEdit::sltRetranslateUI()
126{
127 QPushButton::setText(tr("&Edit"));
128}
129
130void UILineTextEdit::edit()
131{
132 UITextEditor te(this);
133 te.setText(m_strText);
134 if (te.exec() == QDialog::Accepted)
135 {
136 m_strText = te.text();
137 /* Notify listener(s) about we finished: */
138 emit sigFinished(this);
139 }
140}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use