lib

stringlistedit.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
00003    Copyright (C) 2004  Alexander Dymo <cloudtemple@mskat.net>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "stringlistedit.h"
00022 
00023 #include <qlineedit.h>
00024 #include <qlayout.h>
00025 #include <qdialog.h>
00026 #include <qpainter.h>
00027 #include <qvariant.h>
00028 #include <qpushbutton.h>
00029 
00030 #ifndef QT_ONLY
00031 #include <keditlistbox.h>
00032 #include <kdialogbase.h>
00033 #include <kstdguiitem.h>
00034 #include <klocale.h>
00035 #include <kdebug.h>
00036 #else
00037 #include "qeditlistbox.h"
00038 #include <compat_tools.h>
00039 #endif
00040 
00041 #include "property.h"
00042 
00043 using namespace KoProperty;
00044 
00045 StringListEdit::StringListEdit(Property *property, QWidget *parent, const char *name)
00046  : Widget(property, parent, name)
00047 {
00048     setHasBorders(false);
00049     QHBoxLayout *l = new QHBoxLayout(this, 0, 0);
00050 
00051     m_edit = new QLineEdit(this);
00052     m_edit->setLineWidth(0);
00053     m_edit->setReadOnly(true);
00054     m_edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
00055     m_edit->setMinimumHeight(5);
00056     l->addWidget(m_edit);
00057 
00058     m_selectButton = new QPushButton("...", this);
00059     m_selectButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
00060     l->addWidget(m_selectButton);
00061     setFocusWidget(m_selectButton);
00062 
00063     connect(m_selectButton, SIGNAL(clicked()), this, SLOT(showEditor()));
00064 }
00065 
00066 StringListEdit::~StringListEdit()
00067 {}
00068 
00069 QVariant
00070 StringListEdit::value() const
00071 {
00072     return m_list;
00073 }
00074 
00075 void
00076 StringListEdit::setValue(const QVariant &value, bool emitChange)
00077 {
00078     m_list = value.toStringList();
00079     m_edit->setText(value.toStringList().join(", "));
00080     if(emitChange)
00081         emit valueChanged(this);
00082 }
00083 
00084 void
00085 StringListEdit::drawViewer(QPainter *p, const QColorGroup &cg, const QRect &r, const QVariant &value)
00086 {
00087 //  p->eraseRect(r);
00088 //  p->drawText(r, Qt::AlignLeft | Qt::AlignVCenter | Qt::SingleLine, value.toStringList().join(", "));
00089     Widget::drawViewer(p, cg, r, value.toStringList().join(", "));
00090 }
00091 
00092 void
00093 StringListEdit::showEditor()
00094 {
00095 #ifdef QT_ONLY
00096     QDialog* dia = new QDialog(this, "stringlist_dialog", true);
00097     QVBoxLayout *dv = new QVBoxLayout(dia, 2);
00098     QEditListBox *select = new QEditListBox(dia, "select_char");
00099     dv->addWidget(select);
00100 
00101     QHBoxLayout *dh = new QHBoxLayout(dv, 6);
00102     QPushButton *pbOk = new QPushButton(i18n("Ok"), dia);
00103     QPushButton *pbCancel = new QPushButton(i18n("Cancel"), dia);
00104 
00105     QSpacerItem *si = new QSpacerItem(30, 0, QSizePolicy::Expanding, QSizePolicy::Expanding);
00106 
00107     connect(pbOk, SIGNAL(clicked()), dia, SLOT(accept()));
00108     connect(pbCancel, SIGNAL(clicked()), dia, SLOT(reject()));
00109 
00110     dh->addItem(si);
00111     dh->addWidget(pbOk);
00112     dh->addWidget(pbCancel);
00113 
00114     select->insertStringList(m_list);
00115 
00116     if (dia->exec() == QDialog::Accepted) {
00117         m_list = select->items();
00118         m_edit->setText(select->items().join(", "));
00119         emit valueChanged(this);
00120     }
00121     delete dia;
00122 
00123 #else
00124 
00125     KDialogBase dialog(this->topLevelWidget(), "stringlist_dialog", true, i18n("Edit List of Items"),
00126         KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, false);
00127 
00128     KEditListBox *edit = new KEditListBox(i18n("Contents of %1").arg(property()->caption()), &dialog, "editlist");
00129     dialog.setMainWidget(edit);
00130     edit->insertStringList(m_list);
00131 
00132     if(dialog.exec() == QDialog::Accepted)
00133     {
00134         m_list = edit->items();
00135         m_edit->setText(m_list.join(", "));
00136         emit valueChanged(this);
00137     }
00138 
00139 #endif
00140 }
00141 
00142 void
00143 StringListEdit::setReadOnlyInternal(bool readOnly)
00144 {
00145     m_selectButton->setEnabled(!readOnly);
00146 }
00147 
00148 #include "stringlistedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys