File: lineeditwidget.cpp

package info (click to toggle)
psi-plugins 1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,368 kB
  • sloc: cpp: 42,063; xml: 714; ansic: 84; makefile: 61; sh: 12
file content (129 lines) | stat: -rw-r--r-- 2,571 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <QBoxLayout>
#include <QApplication>
#include <QDesktopWidget>
#include <QEvent>
#include <QRegExpValidator>

#include "lineeditwidget.h"

LineEditWidget::LineEditWidget(QWidget *parent)
	: QLineEdit(parent)
	, _layout(new QHBoxLayout())
	, _popup(0)
	, _optimalLength(0)
{
	_layout->setSpacing(0);
	_layout->setContentsMargins(1, 3, 2, 3);
	_layout->addWidget(new QWidget());

	setLayout(_layout);
	setContentsMargins(0, 0, 0, 0);
	installEventFilter(this);
}

LineEditWidget::~LineEditWidget()
{
	_toolbuttons.clear();
}


QSize LineEditWidget::sizeHint() const
{
	QSize size;
	size = QLineEdit::sizeHint();

	int width = 0;

	if(_optimalLength) {
		width += fontMetrics().width("0") * _optimalLength;
	}
	else {
		width += size.width();
	}
	width += textMargins().right();
	size.setWidth(width);
	return size;
}

void LineEditWidget::showEvent(QShowEvent *e)
{
	// Width of standard QLineEdit plus extended tool buttons
	int width = 0;
	for(int i = _toolbuttons.size() - 1; i >= 0; i--) {
		width += _toolbuttons[i]->width();
	}

	setTextMargins(0, 0, width, 0);
	QLineEdit::showEvent(e);
}

bool LineEditWidget::eventFilter(QObject *o, QEvent *e)
{
	return QLineEdit::eventFilter(o, e);
}

void LineEditWidget::setRxValidator(const QString &str)
{
	_rxValidator = str;
	if (str.isEmpty()) {
		return;
	}

	QRegExp rx(str);
	QRegExpValidator *validator = new QRegExpValidator(rx, this);
	setValidator(validator);
}

void LineEditWidget::addWidget(QWidget *w)
{
	_toolbuttons << w;
	_layout->addWidget(w);
}

void LineEditWidget::setPopup(QWidget *w)
{
	if(_popup) {
		delete _popup;
		_popup = 0;
	}

	_popup = new QFrame(this);
	_popup->setWindowFlags(Qt::Popup);
	_popup->setFrameStyle(QFrame::StyledPanel);
	_popup->setAttribute(Qt::WA_WindowPropagation);
	_popup->setAttribute(Qt::WA_X11NetWmWindowTypeCombo);

	QBoxLayout *layout = new QBoxLayout(QBoxLayout::TopToBottom);
	layout->setSpacing(0);
	layout->setMargin(0);
	layout->setContentsMargins(0, 0, 0, 0);
	layout->addWidget(w);
	_popup->setLayout(layout);
}

void LineEditWidget::showPopup()
{
	_popup->adjustSize();
	_popup->move(mapToGlobal(QPoint(width() - _popup->geometry().width(), height())));
	QSize size = qApp->desktop()->size();
	QRect rect = _popup->geometry();

	// if widget is beyond edge of display
	if(rect.right() > size.width()) {
		rect.moveRight(size.width());
	}

	if(rect.bottom() > size.height()) {
		rect.moveBottom(size.height());
	}

	_popup->move(rect.topLeft());
	_popup->show();
}

void LineEditWidget::hidePopup()
{
	if (_popup->isVisible()) {
		_popup->hide();
	}
}