File indexing completed on 2024-04-21 05:51:39

0001 /*
0002  *  SPDX-FileCopyrightText: 2002-2003 Jesper K. Pedersen <blackie@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-only
0005  **/
0006 
0007 #include "selectablelineedit.h"
0008 
0009 SelectableLineEdit::SelectableLineEdit(RegExpWidget *owner, QWidget *parent, const QString &name)
0010     : QLineEdit(parent)
0011     , _owner(owner)
0012 {
0013     setObjectName(name);
0014     connect(this, &QLineEdit::textChanged, this, &SelectableLineEdit::slotKeyPressed);
0015     setAcceptDrops(false);
0016 }
0017 
0018 void SelectableLineEdit::setSelected(bool selected)
0019 {
0020     if (selected) {
0021         QPalette pal = palette();
0022         pal.setBrush(QPalette::Base, Qt::gray);
0023         setPalette(pal);
0024     } else {
0025         setPalette(QPalette());
0026     }
0027     repaint();
0028 }
0029 
0030 QSize SelectableLineEdit::sizeHint() const
0031 {
0032     int frameWidth = hasFrame() ? 12 : 6; // from QLineEdit source
0033     QFontMetrics metrics = fontMetrics();
0034     int actualSize = metrics.boundingRect(text()).width();
0035     int charWidth = metrics.maxWidth();
0036     int height = QLineEdit::sizeHint().height();
0037 
0038     int width;
0039     if (hasFocus()) {
0040         width = actualSize + 6 * charWidth + frameWidth;
0041     } else {
0042         width = qMax(actualSize, charWidth) + frameWidth;
0043     }
0044 
0045     return QSize(width, height);
0046 }
0047 
0048 void SelectableLineEdit::slotKeyPressed()
0049 {
0050     int frameWidth = hasFrame() ? 8 : 4; // from QLineEdit source
0051 
0052     QFontMetrics metrics = fontMetrics();
0053     int actualSize = metrics.boundingRect(text()).width();
0054 
0055     if (actualSize > size().width() - frameWidth) {
0056         updateGeometry();
0057         Q_EMIT parentPleaseUpdate();
0058     }
0059 }
0060 
0061 #include "moc_selectablelineedit.cpp"