File indexing completed on 2024-04-21 08:30:11

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
0003    Copyright (C) 2004  Alexander Dymo <cloudtemple@mskat.net>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "symbolcombo.h"
0022 
0023 #include <QLineEdit>
0024 #include <QPushButton>
0025 #include <QPainter>
0026 #include <QVariant>
0027 #include <QHBoxLayout>
0028 
0029 class Q_DECL_HIDDEN KPropertySymbolComboEditor::Private
0030 {
0031 public:
0032     Private() {
0033     }
0034     QLineEdit *edit;
0035     QPushButton *select;
0036 };
0037 
0038 KPropertySymbolComboEditor::KPropertySymbolComboEditor(KProperty *property, QWidget *parent)
0039         : Widget(property, parent), d(new Private)
0040 {
0041     setHasBorders(false);
0042     QHBoxLayout *l = new QHBoxLayout(this);
0043 
0044     d->edit = new QLineEdit(this);
0045     d->edit->setReadOnly(true);
0046     d->edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
0047     d->edit->setMinimumHeight(5);
0048     d->edit->setMaxLength(1);
0049     l->addWidget(d->edit);
0050     d->select = new QPushButton(this);
0051     Utils::setupDotDotDotButton(d->select, tr("Select symbol"),
0052         tr("Selects a symbol"));
0053     d->select->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::MinimumExpanding);
0054     d->select->setMinimumHeight(5);
0055     l->addWidget(d->select);
0056 
0057     connect(d->select, SIGNAL(clicked()), this, SLOT(selectChar()));
0058     connect(d->edit, SIGNAL(textChanged(const QString&)), this, SLOT(slotValueChanged(const QString&)));
0059 }
0060 
0061 KPropertySymbolComboEditor::~KPropertySymbolComboEditor()
0062 {
0063     delete d;
0064 }
0065 
0066 QVariant
0067 KPropertySymbolComboEditor::value() const
0068 {
0069     if (!(d->edit->text().isNull()))
0070         return d->edit->text().at(0).unicode();
0071     else
0072         return 0;
0073 }
0074 
0075 void
0076 KPropertySymbolComboEditor::setValue(const QVariant &value, bool emitChange)
0077 {
0078     if (!(value.isNull()))
0079     {
0080         d->edit->blockSignals(true);
0081         d->edit->setText(QChar(value.toInt()));
0082         d->edit->blockSignals(false);
0083         if (emitChange)
0084             emit valueChanged(this);
0085     }
0086 }
0087 
0088 void
0089 KPropertySymbolComboEditor::drawViewer(QPainter *p, const QColorGroup &cg, const QRect &r, const QVariant &value)
0090 {
0091     Widget::drawViewer(p, cg, r, QString(QChar(value.toInt())));
0092 }
0093 
0094 void
0095 KPropertySymbolComboEditor::selectChar()
0096 {
0097     QDialog dialog(this->topLevelWidget());
0098     dialog.setWindowTitle(tr("Select Character", "Window title"));
0099     dialog.setObjectName("charselect_dialog");
0100     dialog.setButtons(QDialog::Ok | QDialog::Cancel);
0101     dialog.setDefaultButton(QDialog::Ok);
0102     dialog.setModal(false);
0103 
0104     KCharSelect *select = new KCharSelect(&dialog);
0105     dialog.setObjectName("select_char");
0106 //PORTING: Verify that widget was added to mainLayout:     dialog.setMainWidget(select);
0107 // Add mainLayout->addWidget(select); if necessary
0108 
0109     if (!(d->edit->text().isNull()))
0110         select->setCurrentChar(d->edit->text().at(0));
0111 
0112     if (dialog.exec() == QDialog::Accepted)
0113         d->edit->setText(select->currentChar());
0114 }
0115 
0116 void
0117 KPropertySymbolComboEditor::slotValueChanged(const QString&)
0118 {
0119     emit valueChanged(this);
0120 }
0121 
0122 void
0123 KPropertySymbolComboEditor::setReadOnlyInternal(bool readOnly)
0124 {
0125     d->select->setEnabled(!readOnly);
0126 }