1 | /* $Id: QILineEdit.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
5 | * QILineEdit class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "QILineEdit.h"
|
---|
21 |
|
---|
22 | /* Qt includes */
|
---|
23 | #include <QStyleOptionFrame>
|
---|
24 |
|
---|
25 | #if defined (Q_WS_WIN32)
|
---|
26 | #include <QWindowsVistaStyle>
|
---|
27 | #include <QLibrary>
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | void QILineEdit::setMinimumWidthByText (const QString &aText)
|
---|
31 | {
|
---|
32 | setMinimumWidth (featTextWidth (aText).width());
|
---|
33 | }
|
---|
34 |
|
---|
35 | void QILineEdit::setFixedWidthByText (const QString &aText)
|
---|
36 | {
|
---|
37 | setFixedWidth (featTextWidth (aText).width());
|
---|
38 | }
|
---|
39 |
|
---|
40 | QSize QILineEdit::featTextWidth (const QString &aText) const
|
---|
41 | {
|
---|
42 | QStyleOptionFrame sof;
|
---|
43 | sof.initFrom (this);
|
---|
44 | sof.rect = contentsRect();
|
---|
45 | sof.lineWidth = hasFrame() ? style()->pixelMetric (QStyle::PM_DefaultFrameWidth) : 0;
|
---|
46 | sof.midLineWidth = 0;
|
---|
47 | sof.state |= QStyle::State_Sunken;
|
---|
48 |
|
---|
49 | /* The margins are based on qlineedit.cpp of Qt. Maybe they where changed
|
---|
50 | * at some time in the future. */
|
---|
51 | QSize sc (fontMetrics().width (aText) + 2*2,
|
---|
52 | fontMetrics().xHeight() + 2*1);
|
---|
53 | QSize sa = style()->sizeFromContents (QStyle::CT_LineEdit, &sof, sc, this);
|
---|
54 |
|
---|
55 | #if defined (Q_WS_WIN32)
|
---|
56 | /* Vista l&f style has a bug where the last parameter of sizeFromContents
|
---|
57 | * function ('widget' what corresponds to 'this' in our class) is ignored.
|
---|
58 | * Due to it QLineEdit processed as QComboBox and size calculation includes
|
---|
59 | * non-existing combo-box button of 23 pix in width. So fixing it here: */
|
---|
60 | if (qobject_cast <QWindowsVistaStyle*> (style()))
|
---|
61 | {
|
---|
62 | /* Check if l&f style theme is really active else painting performed by
|
---|
63 | * Windows Classic theme and there is no such shifting error. */
|
---|
64 | typedef bool (*IsAppThemedFunction)();
|
---|
65 | IsAppThemedFunction isAppThemed =
|
---|
66 | (IsAppThemedFunction) QLibrary::resolve ("uxtheme", "IsAppThemed");
|
---|
67 | if (isAppThemed && isAppThemed()) sa -= QSize (23, 0);
|
---|
68 | }
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | return sa;
|
---|
72 | }
|
---|
73 |
|
---|