File indexing completed on 2024-05-12 16:35:16

0001 /* This file is part of the KDE project
0002    Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
0003    Copyright (C) 2007, 2009 Thomas Zander <zander@kde.org>
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 "CharacterSelectDialog.h"
0022 
0023 #include <QLayout>
0024 #include <QGridLayout>
0025 #include <QPointer>
0026 
0027 #include <KLocalizedString>
0028 #include <kcharselect.h>
0029 #include <kstandardguiitem.h>
0030 
0031 using namespace Calligra::Sheets;
0032 
0033 class CharacterSelectDialog::Private
0034 {
0035 public:
0036     Private() : charSelect(0) {}
0037     KCharSelect *charSelect;
0038 };
0039 
0040 /******************************************************************/
0041 /* class CharacterSelectDialog                                           */
0042 /******************************************************************/
0043 
0044 CharacterSelectDialog::CharacterSelectDialog(QWidget *parent, const QString &name, const QChar &_chr, const QString &_font, bool _modal)
0045         : KoDialog(parent),
0046         d(new Private())
0047 {
0048     setCaption(i18n("Select Character"));
0049     setModal(_modal);
0050     setButtons(Ok | Cancel);
0051     setDefaultButton(Ok);
0052     setObjectName(name);
0053 
0054     initDialog(_chr, _font);
0055 
0056     KGuiItem okItem = KStandardGuiItem::ok(); // start from std item to keep the OK icon...
0057     okItem.setText(i18n("&Insert"));
0058     okItem.setWhatsThis(i18n("Insert the selected character in the text"));
0059     setButtonGuiItem(KoDialog::Ok, okItem);
0060 }
0061 
0062 CharacterSelectDialog::CharacterSelectDialog(QWidget *parent, const QString &name, const QString &_font, const QChar &_chr, bool _modal)
0063         : KoDialog(parent),
0064         d(new Private())
0065 {
0066     setCaption(i18n("Select Character"));
0067     setModal(_modal);
0068     setButtons(User1 | Close);
0069     setDefaultButton(User1);
0070     setObjectName(name);
0071 
0072     initDialog(_chr, _font);
0073 
0074     setButtonText(User1, i18n("&Insert"));
0075     setButtonToolTip(User1, i18n("Insert the selected character in the text"));
0076     connect(this, SIGNAL(user1Clicked()), this, SLOT(slotUser1()));
0077 }
0078 
0079 void CharacterSelectDialog::initDialog(const QChar &_chr, const QString &_font)
0080 {
0081     QWidget *page = mainWidget()/*plainPage()*/;
0082 
0083     QGridLayout *grid = new QGridLayout(page);
0084     grid->setMargin(0);
0085 
0086     d->charSelect = new KCharSelect(page, 0);
0087     d->charSelect->setCurrentChar(_chr);
0088     d->charSelect->setCurrentFont(QFont(_font));
0089     connect(d->charSelect, SIGNAL(charSelected(QChar)), this, SLOT(slotDoubleClicked()));
0090     d->charSelect->resize(d->charSelect->sizeHint());
0091 //     d->charSelect->enableFontCombo( true );
0092     grid->addWidget(d->charSelect, 0, 0);
0093 
0094     grid->addItem(new QSpacerItem(d->charSelect->width(), 0), 0, 0);
0095     grid->addItem(new QSpacerItem(0, d->charSelect->height()), 0, 0);
0096     grid->setRowStretch(0, 0);
0097     d->charSelect->setFocus();
0098 }
0099 
0100 CharacterSelectDialog::~CharacterSelectDialog()
0101 {
0102     delete d;
0103 }
0104 
0105 void CharacterSelectDialog::closeDialog()
0106 {
0107     KoDialog::close();
0108 }
0109 
0110 // static
0111 bool CharacterSelectDialog::selectChar(QString &_font, QChar &_chr, QWidget* parent, const char* name)
0112 {
0113     bool res = false;
0114 
0115     QPointer<CharacterSelectDialog> dlg = new CharacterSelectDialog(parent, name, _chr, _font);
0116     dlg->setFocus();
0117     if (dlg->exec() == Accepted) {
0118         _font = dlg->font();
0119         _chr = dlg->chr();
0120         res = true;
0121     }
0122 
0123     delete dlg;
0124 
0125     return res;
0126 }
0127 
0128 QChar CharacterSelectDialog::chr() const
0129 {
0130     return d->charSelect->currentChar();
0131 }
0132 
0133 QString CharacterSelectDialog::font() const
0134 {
0135     return d->charSelect->font().family();
0136 }
0137 
0138 void CharacterSelectDialog::slotUser1()
0139 {
0140     emit insertChar(chr(), font());
0141 }
0142 
0143 void CharacterSelectDialog::slotDoubleClicked()
0144 {
0145     emit insertChar(chr(), font());
0146 }