123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #include "MixWithComboxInput.h"
- #include <QComboBox>
- #include <QLabel>
- #include <QHBoxLayout>
- #include <QPainter>
- #include <QAbstractItemView>
- #include <QKeyEvent>
- class MixWithComboxInput::Private
- {
- public:
- QComboBox *combo;
- QLabel *label;
- bool inside;
- bool isFlat;
- };
- MixWithComboxInput::MixWithComboxInput(QWidget *parent) : QStackedWidget(parent), d(new Private)
- {
- #ifdef Q_OS_MACOS
- setAttribute(Qt::WA_MacMiniSize, true);
- #endif
- QWidget *first = new QWidget(this);
- QHBoxLayout *layout = new QHBoxLayout(first);
- layout->setSpacing(0);
- layout->setMargin(0);
- d->label = new QLabel(first);
- d->label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
- layout->addWidget(d->label, 10);
- QLabel *icon = new QLabel(first);
- QStyleOption option;
- option.state = QStyle::State_Enabled;
- QPixmap pixmap(16, 16);
- pixmap.fill(QColor(255, 255, 255, 0));
- QPainter painter(&pixmap);
- painter.translate(8, 8);
- style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &option, &painter);
- icon->setPixmap(pixmap);
- layout->addWidget(icon);
- addWidget(first);
- d->combo = new QComboBox(this);
- d->combo->setMaxVisibleItems(15);
- d->combo->setEditable(true);
- d->combo->installEventFilter(this);
- addWidget(d->combo);
- d->inside = false;
- d->isFlat = true;
- connect(d->combo, SIGNAL(activated(QString)), this, SIGNAL(inputLevelChanged(QString)));
- }
- MixWithComboxInput::~MixWithComboxInput()
- {
- delete d;
- }
- void MixWithComboxInput::enterEvent(QEvent *event)
- {
- Q_UNUSED(event);
- d->inside = true;
- if (!isFlat()) {
- return;
- }
- if (d->combo->view()) {
- d->combo->view()->removeEventFilter(this);
- }
- setCurrentIndex(1);
- }
- void MixWithComboxInput::leaveEvent(QEvent *event)
- {
- Q_UNUSED(event);
- d->inside = false;
- if (!isFlat()) {
- return;
- }
- if (d->combo->view() && d->combo->view()->isVisible()) {
- d->combo->view()->installEventFilter(this);
- return;
- }
- if (!d->combo->hasFocus())
- setCurrentIndex(0);
- }
- void MixWithComboxInput::keyPressEvent(QKeyEvent *event)
- {
- if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
- focusNextChild();
- }
- }
- bool MixWithComboxInput::isFlat() const
- {
- return d->isFlat;
- }
- void MixWithComboxInput::setFlat(bool flat)
- {
- if (flat == d->isFlat) {
- return;
- }
- d->isFlat = flat;
- if (flat) {
- d->combo->installEventFilter(this);
- if (d->inside) {
- enterEvent(nullptr);
- } else {
- leaveEvent(nullptr);
- }
- d->combo->setEditable(true);
- } else {
- d->combo->removeEventFilter(this);
- if (d->combo->view()) {
- d->combo->view()->removeEventFilter(this);
- }
- d->combo->setCurrentIndex(d->combo->findText(d->label->text()));
- d->combo->setEditable(false);
- setCurrentIndex(1);
- }
- }
- void MixWithComboxInput::setInputLevels(const QStringList &levels)
- {
- d->combo->clear();
- d->combo->addItems(levels);
- }
- QString MixWithComboxInput::currentInputLevel() const
- {
- return d->label->text();
- }
- void MixWithComboxInput::setCurrentInputLevel(const QString &level)
- {
- d->combo->setCurrentIndex(d->combo->findText(level));
- d->label->setText(level);
- }
- bool MixWithComboxInput::eventFilter(QObject *watched, QEvent *event)
- {
- if (watched == d->combo->view() && event->type() == QEvent::Hide) {
- focusNextChild();
- setCurrentIndex(0);
- } else if (watched == d->combo && event->type() == QEvent::FocusOut
- && (d->combo->view() && !d->combo->view()->hasFocus()) && !d->inside) {
- setCurrentIndex(0);
- }
- return false;
- }
|