1 | /* $Id: QILineEdit.cpp 104983 2024-06-20 13:33:10Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Qt extensions: QILineEdit class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2024 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 <QApplication>
|
---|
30 | #include <QClipboard>
|
---|
31 | #include <QContextMenuEvent>
|
---|
32 | #include <QLabel>
|
---|
33 | #include <QMenu>
|
---|
34 | #include <QStyleOptionFrame>
|
---|
35 |
|
---|
36 | /* GUI includes: */
|
---|
37 | #include "QILineEdit.h"
|
---|
38 | #include "UIDesktopWidgetWatchdog.h"
|
---|
39 | #include "UIIconPool.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | QILineEdit::QILineEdit(QWidget *pParent /* = 0 */)
|
---|
43 | : QLineEdit(pParent)
|
---|
44 | , m_fAllowToCopyContentsWhenDisabled(false)
|
---|
45 | , m_pCopyAction(0)
|
---|
46 | , m_fMarkable(false)
|
---|
47 | , m_fMarkForError(false)
|
---|
48 | , m_pLabelIcon(0)
|
---|
49 | , m_iIconMargin(0)
|
---|
50 | {
|
---|
51 | prepare();
|
---|
52 | }
|
---|
53 |
|
---|
54 | QILineEdit::QILineEdit(const QString &strText, QWidget *pParent /* = 0 */)
|
---|
55 | : QLineEdit(strText, pParent)
|
---|
56 | , m_fAllowToCopyContentsWhenDisabled(false)
|
---|
57 | , m_pCopyAction(0)
|
---|
58 | , m_fMarkable(false)
|
---|
59 | , m_fMarkForError(false)
|
---|
60 | , m_pLabelIcon(0)
|
---|
61 | , m_iIconMargin(0)
|
---|
62 | {
|
---|
63 | prepare();
|
---|
64 | }
|
---|
65 |
|
---|
66 | void QILineEdit::setAllowToCopyContentsWhenDisabled(bool fAllow)
|
---|
67 | {
|
---|
68 | m_fAllowToCopyContentsWhenDisabled = fAllow;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void QILineEdit::setMinimumWidthByText(const QString &strText)
|
---|
72 | {
|
---|
73 | setMinimumWidth(fitTextWidth(strText).width());
|
---|
74 | }
|
---|
75 |
|
---|
76 | void QILineEdit::setFixedWidthByText(const QString &strText)
|
---|
77 | {
|
---|
78 | setFixedWidth(fitTextWidth(strText).width());
|
---|
79 | }
|
---|
80 |
|
---|
81 | void QILineEdit::setMarkable(bool fMarkable)
|
---|
82 | {
|
---|
83 | /* Sanity check: */
|
---|
84 | if (m_fMarkable == fMarkable)
|
---|
85 | return;
|
---|
86 |
|
---|
87 | /* Save new value, show/hide label accordingly: */
|
---|
88 | m_fMarkable = fMarkable;
|
---|
89 | if (m_pLabelIcon)
|
---|
90 | m_pLabelIcon->setVisible(fMarkable);
|
---|
91 |
|
---|
92 | /* Update label position on visibility changes: */
|
---|
93 | moveIconLabel();
|
---|
94 | }
|
---|
95 |
|
---|
96 | void QILineEdit::mark(bool fError, const QString &strErrorMessage, const QString &strNoErrorMessage)
|
---|
97 | {
|
---|
98 | /* Sanity check: */
|
---|
99 | if ( !m_pLabelIcon
|
---|
100 | || !m_fMarkable)
|
---|
101 | return;
|
---|
102 |
|
---|
103 | /* Assign corresponding icon: */
|
---|
104 | const QIcon icon = fError ? UIIconPool::iconSet(":/status_error_16px.png") : UIIconPool::iconSet(":/status_check_16px.png");
|
---|
105 | const int iIconMetric = qMin((int)(QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize) * .625), height());
|
---|
106 | const qreal fDevicePixelRatio = gpDesktop->devicePixelRatio(m_pLabelIcon);
|
---|
107 | const QString strToolTip = fError ? strErrorMessage : strNoErrorMessage;
|
---|
108 | const QPixmap iconPixmap = icon.pixmap(QSize(iIconMetric, iIconMetric), fDevicePixelRatio);
|
---|
109 | m_pLabelIcon->setPixmap(iconPixmap);
|
---|
110 | m_pLabelIcon->resize(m_pLabelIcon->minimumSizeHint());
|
---|
111 | m_pLabelIcon->setToolTip(strToolTip);
|
---|
112 | m_iIconMargin = (height() - m_pLabelIcon->height()) / 2;
|
---|
113 |
|
---|
114 | /* Update label position on icon changes: */
|
---|
115 | moveIconLabel();
|
---|
116 | }
|
---|
117 |
|
---|
118 | bool QILineEdit::event(QEvent *pEvent)
|
---|
119 | {
|
---|
120 | /* Depending on event type: */
|
---|
121 | switch (pEvent->type())
|
---|
122 | {
|
---|
123 | case QEvent::ContextMenu:
|
---|
124 | {
|
---|
125 | /* For disabled widget if requested: */
|
---|
126 | if (!isEnabled() && m_fAllowToCopyContentsWhenDisabled)
|
---|
127 | {
|
---|
128 | /* Create a context menu for the copy to clipboard action: */
|
---|
129 | QContextMenuEvent *pContextMenuEvent = static_cast<QContextMenuEvent*>(pEvent);
|
---|
130 | QMenu menu;
|
---|
131 | m_pCopyAction->setText(tr("&Copy"));
|
---|
132 | menu.addAction(m_pCopyAction);
|
---|
133 | menu.exec(pContextMenuEvent->globalPos());
|
---|
134 | pEvent->accept();
|
---|
135 | }
|
---|
136 | break;
|
---|
137 | }
|
---|
138 | case QEvent::Move:
|
---|
139 | case QEvent::Resize:
|
---|
140 | {
|
---|
141 | /* Update label position on each move/resize: */
|
---|
142 | moveIconLabel();
|
---|
143 | break;
|
---|
144 | }
|
---|
145 | default:
|
---|
146 | break;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* Call to base-class: */
|
---|
150 | return QLineEdit::event(pEvent);
|
---|
151 | }
|
---|
152 |
|
---|
153 | void QILineEdit::copy()
|
---|
154 | {
|
---|
155 | /* Copy the current text to the global and selection clipboards: */
|
---|
156 | QApplication::clipboard()->setText(text(), QClipboard::Clipboard);
|
---|
157 | QApplication::clipboard()->setText(text(), QClipboard::Selection);
|
---|
158 | }
|
---|
159 |
|
---|
160 | void QILineEdit::prepare()
|
---|
161 | {
|
---|
162 | /* Prepare invisible copy action: */
|
---|
163 | m_pCopyAction = new QAction(this);
|
---|
164 | if (m_pCopyAction)
|
---|
165 | {
|
---|
166 | m_pCopyAction->setShortcut(QKeySequence(QKeySequence::Copy));
|
---|
167 | m_pCopyAction->setShortcutContext(Qt::WidgetShortcut);
|
---|
168 | connect(m_pCopyAction, &QAction::triggered, this, &QILineEdit::copy);
|
---|
169 | addAction(m_pCopyAction);
|
---|
170 | }
|
---|
171 |
|
---|
172 | /* Prepare icon label: */
|
---|
173 | m_pLabelIcon = new QLabel(this);
|
---|
174 | if (m_pLabelIcon)
|
---|
175 | m_pLabelIcon->hide();
|
---|
176 | }
|
---|
177 |
|
---|
178 | QSize QILineEdit::fitTextWidth(const QString &strText) const
|
---|
179 | {
|
---|
180 | QStyleOptionFrame sof;
|
---|
181 | sof.initFrom(this);
|
---|
182 | sof.rect = contentsRect();
|
---|
183 | sof.lineWidth = hasFrame() ? style()->pixelMetric(QStyle::PM_DefaultFrameWidth) : 0;
|
---|
184 | sof.midLineWidth = 0;
|
---|
185 | sof.state |= QStyle::State_Sunken;
|
---|
186 |
|
---|
187 | /** @todo make it wise.. */
|
---|
188 | // WORKAROUND:
|
---|
189 | // The margins are based on qlineedit.cpp of Qt.
|
---|
190 | // Maybe they where changed at some time in the future.
|
---|
191 | QSize sc(fontMetrics().horizontalAdvance(strText) + 2 * 2,
|
---|
192 | fontMetrics().xHeight() + 2 * 1);
|
---|
193 | const QSize sa = style()->sizeFromContents(QStyle::CT_LineEdit, &sof, sc, this);
|
---|
194 |
|
---|
195 | return sa;
|
---|
196 | }
|
---|
197 |
|
---|
198 | void QILineEdit::moveIconLabel()
|
---|
199 | {
|
---|
200 | /* Sanity check: */
|
---|
201 | if ( !m_pLabelIcon
|
---|
202 | || !m_fMarkable)
|
---|
203 | return;
|
---|
204 |
|
---|
205 | /* We do it cause we have manual layout for the label: */
|
---|
206 | m_pLabelIcon->move(width() - m_pLabelIcon->width() - m_iIconMargin, m_iIconMargin);
|
---|
207 | update();
|
---|
208 | }
|
---|