1 | /* $Id: QILineEdit.cpp 76606 2019-01-02 05:40:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Qt extensions: QILineEdit class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /* GUI includes: */
|
---|
19 | #include "QILineEdit.h"
|
---|
20 |
|
---|
21 | /* Qt includes: */
|
---|
22 | #include <QStyleOptionFrame>
|
---|
23 |
|
---|
24 |
|
---|
25 | void QILineEdit::setMinimumWidthByText(const QString &strText)
|
---|
26 | {
|
---|
27 | setMinimumWidth(featTextWidth(strText).width());
|
---|
28 | }
|
---|
29 |
|
---|
30 | void QILineEdit::setFixedWidthByText(const QString &strText)
|
---|
31 | {
|
---|
32 | setFixedWidth(featTextWidth(strText).width());
|
---|
33 | }
|
---|
34 |
|
---|
35 | QSize QILineEdit::featTextWidth(const QString &strText) const
|
---|
36 | {
|
---|
37 | QStyleOptionFrame sof;
|
---|
38 | sof.initFrom(this);
|
---|
39 | sof.rect = contentsRect();
|
---|
40 | sof.lineWidth = hasFrame() ? style()->pixelMetric(QStyle::PM_DefaultFrameWidth) : 0;
|
---|
41 | sof.midLineWidth = 0;
|
---|
42 | sof.state |= QStyle::State_Sunken;
|
---|
43 |
|
---|
44 | /** @todo make it wise.. */
|
---|
45 | // WORKAROUND:
|
---|
46 | // The margins are based on qlineedit.cpp of Qt.
|
---|
47 | // Maybe they where changed at some time in the future.
|
---|
48 | QSize sc(fontMetrics().width(strText) + 2 * 2,
|
---|
49 | fontMetrics().xHeight() + 2 * 1);
|
---|
50 | const QSize sa = style()->sizeFromContents(QStyle::CT_LineEdit, &sof, sc, this);
|
---|
51 |
|
---|
52 | return sa;
|
---|
53 | }
|
---|