1 /* This file is part of the KDE project
2    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3    Copyright (C) 2004  Alexander Dymo <cloudtemple@mskat.net>
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9 
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public License
16    along with this library; see the file COPYING.LIB.  If not, write to
17    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19 */
20 
21 #include "symbolcombo.h"
22 
23 #include <QLineEdit>
24 #include <QPushButton>
25 #include <QPainter>
26 #include <QVariant>
27 #include <QHBoxLayout>
28 
29 class Q_DECL_HIDDEN KPropertySymbolComboEditor::Private
30 {
31 public:
Private()32     Private() {
33     }
34     QLineEdit *edit;
35     QPushButton *select;
36 };
37 
KPropertySymbolComboEditor(KProperty * property,QWidget * parent)38 KPropertySymbolComboEditor::KPropertySymbolComboEditor(KProperty *property, QWidget *parent)
39         : Widget(property, parent), d(new Private)
40 {
41     setHasBorders(false);
42     QHBoxLayout *l = new QHBoxLayout(this);
43 
44     d->edit = new QLineEdit(this);
45     d->edit->setReadOnly(true);
46     d->edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
47     d->edit->setMinimumHeight(5);
48     d->edit->setMaxLength(1);
49     l->addWidget(d->edit);
50     d->select = new QPushButton(this);
51     Utils::setupDotDotDotButton(d->select, tr("Select symbol"),
52         tr("Selects a symbol"));
53     d->select->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::MinimumExpanding);
54     d->select->setMinimumHeight(5);
55     l->addWidget(d->select);
56 
57     connect(d->select, SIGNAL(clicked()), this, SLOT(selectChar()));
58     connect(d->edit, SIGNAL(textChanged(const QString&)), this, SLOT(slotValueChanged(const QString&)));
59 }
60 
~KPropertySymbolComboEditor()61 KPropertySymbolComboEditor::~KPropertySymbolComboEditor()
62 {
63     delete d;
64 }
65 
66 QVariant
value() const67 KPropertySymbolComboEditor::value() const
68 {
69     if (!(d->edit->text().isNull()))
70         return d->edit->text().at(0).unicode();
71     else
72         return 0;
73 }
74 
75 void
setValue(const QVariant & value,bool emitChange)76 KPropertySymbolComboEditor::setValue(const QVariant &value, bool emitChange)
77 {
78     if (!(value.isNull()))
79     {
80         d->edit->blockSignals(true);
81         d->edit->setText(QChar(value.toInt()));
82         d->edit->blockSignals(false);
83         if (emitChange)
84             emit valueChanged(this);
85     }
86 }
87 
88 void
drawViewer(QPainter * p,const QColorGroup & cg,const QRect & r,const QVariant & value)89 KPropertySymbolComboEditor::drawViewer(QPainter *p, const QColorGroup &cg, const QRect &r, const QVariant &value)
90 {
91     Widget::drawViewer(p, cg, r, QString(QChar(value.toInt())));
92 }
93 
94 void
selectChar()95 KPropertySymbolComboEditor::selectChar()
96 {
97     QDialog dialog(this->topLevelWidget());
98     dialog.setWindowTitle(tr("Select Character", "Window title"));
99     dialog.setObjectName("charselect_dialog");
100     dialog.setButtons(QDialog::Ok | QDialog::Cancel);
101     dialog.setDefaultButton(QDialog::Ok);
102     dialog.setModal(false);
103 
104     KCharSelect *select = new KCharSelect(&dialog);
105     dialog.setObjectName("select_char");
106 //PORTING: Verify that widget was added to mainLayout:     dialog.setMainWidget(select);
107 // Add mainLayout->addWidget(select); if necessary
108 
109     if (!(d->edit->text().isNull()))
110         select->setCurrentChar(d->edit->text().at(0));
111 
112     if (dialog.exec() == QDialog::Accepted)
113         d->edit->setText(select->currentChar());
114 }
115 
116 void
slotValueChanged(const QString &)117 KPropertySymbolComboEditor::slotValueChanged(const QString&)
118 {
119     emit valueChanged(this);
120 }
121 
122 void
setReadOnlyInternal(bool readOnly)123 KPropertySymbolComboEditor::setReadOnlyInternal(bool readOnly)
124 {
125     d->select->setEnabled(!readOnly);
126 }
127