File indexing completed on 2024-04-28 05:43:24

0001 /***************************************************************************
0002  *   Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>                     *
0003  *   Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>                     *
0004  *   Copyright (C) 2006 David Saxton <david@bluehaze.org>                  *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  ***************************************************************************/
0011 
0012 #include "propertyeditorlist.h"
0013 #include "iteminterface.h"
0014 #include "property.h"
0015 
0016 //#include <klistbox.h>
0017 #include <KLocalizedString>
0018 
0019 #include <QCursor>
0020 #include <QDebug>
0021 //#include <q3hbox.h>
0022 #include <QHBoxLayout>
0023 #include <QInputEvent>
0024 #include <QLineEdit>
0025 #include <QStringList>
0026 #include <QToolButton>
0027 
0028 // BEGIN class PropComboBox
0029 PropComboBox::PropComboBox(QWidget *parent)
0030     : KComboBox(parent)
0031 {
0032     m_eventFilterEnabled = true;
0033 }
0034 
0035 bool PropComboBox::eventFilter(QObject *o, QEvent *e)
0036 {
0037     if (!m_eventFilterEnabled)
0038         return false;
0039 
0040     if (o == lineEdit()) {
0041         if (e->type() == QEvent::KeyPress) {
0042             QKeyEvent *ev = static_cast<QKeyEvent *>(e);
0043             if ((ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down) &&
0044                 (/* ev->state()!=Qt::ControlButton */
0045                  (dynamic_cast<QInputEvent *>(ev))->modifiers() != Qt::ControlModifier)) {
0046                 parentWidget()->eventFilter(o, e);
0047                 return true;
0048             }
0049         }
0050     }
0051 
0052     return KComboBox::eventFilter(o, e);
0053 }
0054 
0055 void PropComboBox::hideList()
0056 {
0057     lineEdit()->setFocus();
0058 }
0059 // END class PropComboBox
0060 
0061 // BEGIN class PropertyEditorList
0062 PropertyEditorList::PropertyEditorList(QWidget *parent, Property *property)
0063     : PropertySubEditor(parent, property)
0064 {
0065     QWidget *box = new QWidget(this);
0066     QHBoxLayout *boxLayout = new QHBoxLayout;
0067     boxLayout->setMargin(0);
0068 
0069     m_combo = new PropComboBox(box);
0070     m_combo->setGeometry(frameGeometry());
0071 
0072     boxLayout->addWidget(m_combo);
0073     box->setLayout(boxLayout);
0074 
0075     bool isEditable = false;
0076     switch (property->type()) {
0077     case Property::Type::Port:
0078     case Property::Type::Pin:
0079     case Property::Type::PenStyle:
0080     case Property::Type::PenCapStyle:
0081     case Property::Type::SevenSegment:
0082     case Property::Type::KeyPad:
0083     case Property::Type::Select:
0084         isEditable = false;
0085         break;
0086 
0087     case Property::Type::String:
0088     case Property::Type::Multiline:
0089     case Property::Type::RichText:
0090     case Property::Type::Combo:
0091     case Property::Type::FileName:
0092     case Property::Type::VarName:
0093         isEditable = true;
0094         break;
0095 
0096     case Property::Type::None:
0097     case Property::Type::Int:
0098     case Property::Type::Raw:
0099     case Property::Type::Double:
0100     case Property::Type::Color:
0101     case Property::Type::Bool:
0102         // not handled by this
0103         break;
0104     }
0105 
0106     m_combo->setEditable(isEditable);
0107 
0108     m_combo->setInsertPolicy(QComboBox::InsertAtBottom);
0109     m_combo->setAutoCompletion(true);
0110     m_combo->setMinimumSize(10, 0); // to allow the combo to be resized to a small size
0111 
0112     m_combo->insertItems(m_combo->count(), m_property->allowed());
0113     // m_combo->setCurrentText( m_property->displayString() ); // 2018.12.07
0114     {
0115         QString text = m_property->displayString();
0116         int i = m_combo->findText(text);
0117         if (i != -1)
0118             m_combo->setCurrentIndex(i);
0119         else if (m_combo->isEditable())
0120             m_combo->setEditText(text);
0121         else
0122             m_combo->setItemText(m_combo->currentIndex(), text);
0123     }
0124 
0125     KCompletion *comp = m_combo->completionObject();
0126     comp->insertItems(m_property->allowed());
0127 
0128     setWidget(box, m_combo->lineEdit());
0129 
0130     connect(m_combo, qOverload<const QString &>(&PropComboBox::textActivated), this, &PropertyEditorList::valueChanged);
0131     connect(m_property, qOverload<const QString &>(&Property::valueChanged), this, [this](const QString &str) { m_combo->setCurrentItem(str); });
0132 }
0133 
0134 void PropertyEditorList::setList(QStringList l)
0135 {
0136     // m_combo->insertStringList(l); // 2018.12.07
0137     m_combo->insertItems(m_combo->count(), l);
0138 }
0139 
0140 void PropertyEditorList::valueChanged(const QString &text)
0141 {
0142     m_property->setValue(text);
0143     ItemInterface::self()->setProperty(m_property);
0144 }
0145 // END class PropertyEditorList
0146 
0147 #include "moc_propertyeditorlist.cpp"