editentrydialog.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
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 version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include "editentrydialog.h"
00021 
00022 #include <qlineedit.h>
00023 #include <qcheckbox.h>
00024 #include <qspinbox.h>
00025 #include <qcombobox.h>
00026 #include <qlabel.h>
00027 #include <qheader.h>
00028 #include <klistview.h>
00029 #include <qlayout.h>
00030 #include <qwidgetstack.h>
00031 #include <klocale.h>
00032 #include <kiconloader.h>
00033 
00034 EditEntryDialog::EditEntryDialog(PrintcapEntry *entry, QWidget *parent, const char *name)
00035 : KDialogBase(parent, name, true, QString::null, Ok|Cancel)
00036 {
00037     QWidget *w = new QWidget(this);
00038     setMainWidget(w);
00039 
00040     QLabel  *lab0 = new QLabel(i18n("Aliases:"), w);
00041     m_aliases = new QLineEdit(w);
00042     m_view = new KListView(w);
00043     m_view->addColumn("");
00044     m_view->header()->hide();
00045     m_type = new QComboBox(w);
00046     m_type->insertItem(i18n("String"));
00047     m_type->insertItem(i18n("Number"));
00048     m_type->insertItem(i18n("Boolean"));
00049     m_stack = new QWidgetStack(w);
00050     m_boolean = new QCheckBox(i18n("Enabled"), m_stack);
00051     m_string = new QLineEdit(m_stack);
00052     m_number = new QSpinBox(0, 9999, 1, m_stack);
00053     m_stack->addWidget(m_string, 0);
00054     m_stack->addWidget(m_boolean, 2);
00055     m_stack->addWidget(m_number, 1);
00056     m_name = new QLineEdit(w);
00057 
00058     QVBoxLayout *l0 = new QVBoxLayout(w, 0, 10);
00059     QHBoxLayout *l1 = new QHBoxLayout(0, 0, 10);
00060     QHBoxLayout *l2 = new QHBoxLayout(0, 0, 5);
00061     l0->addLayout(l1);
00062     l1->addWidget(lab0);
00063     l1->addWidget(m_aliases);
00064     l0->addWidget(m_view);
00065     l0->addLayout(l2);
00066     l2->addWidget(m_name, 0);
00067     l2->addWidget(m_type, 0);
00068     l2->addWidget(m_stack, 1);
00069 
00070     if (entry)
00071     {
00072         setCaption(i18n("Printcap Entry: %1").arg(entry->name));
00073         m_fields = entry->fields;
00074         m_aliases->setText(entry->aliases.join("|"));
00075         QListViewItem   *root = new QListViewItem(m_view, entry->name), *item = 0;
00076         root->setSelectable(false);
00077         root->setOpen(true);
00078         root->setPixmap(0, SmallIcon("fileprint"));
00079         for (QMap<QString,Field>::ConstIterator it=m_fields.begin(); it!=m_fields.end(); ++it)
00080             item = new QListViewItem(root, item, (*it).toString(), it.key());
00081     }
00082 
00083     m_block = true;
00084     enableButton(Ok, false);
00085     slotItemSelected(NULL);
00086     slotTypeChanged(0);
00087     m_block = false;
00088 
00089     connect(m_view, SIGNAL(selectionChanged(QListViewItem*)), SLOT(slotItemSelected(QListViewItem*)));
00090     connect(m_string, SIGNAL(textChanged(const QString&)), SLOT(slotChanged()));
00091     connect(m_boolean, SIGNAL(toggled(bool)), SLOT(slotChanged()));
00092     connect(m_number, SIGNAL(valueChanged(int)), SLOT(slotChanged()));
00093     connect(m_type, SIGNAL(activated(int)), SLOT(slotTypeChanged(int)));
00094     connect(m_name, SIGNAL(textChanged(const QString&)), SLOT(slotChanged()));
00095 
00096     resize(500,400);
00097 }
00098 
00099 Field EditEntryDialog::createField()
00100 {
00101     Field   f;
00102     f.name = m_name->text();
00103     f.type = (Field::Type)(m_type->currentItem());
00104     switch (f.type)
00105     {
00106         case Field::String: f.value = m_string->text(); break;
00107         case Field::Integer: f.value = m_number->cleanText(); break;
00108         case Field::Boolean: f.value = (m_boolean->isChecked() ? "1" : "0"); break;
00109     }
00110     return f;
00111 }
00112 
00113 void EditEntryDialog::slotChanged()
00114 {
00115     if (!m_block && m_view->currentItem())
00116     {
00117         Field   f = createField();
00118         if (f.name != m_current)
00119             m_fields.remove(m_current);
00120         m_fields[f.name] = f;
00121         m_view->currentItem()->setText(0, f.toString());
00122     }
00123 }
00124 
00125 void EditEntryDialog::slotItemSelected(QListViewItem *item)
00126 {
00127     m_stack->setEnabled(item);
00128     m_name->setEnabled(item);
00129     m_type->setEnabled(item);
00130     if (item)
00131     {
00132         m_block = true;
00133         m_current = item->text(1);
00134         Field   f = m_fields[m_current];
00135         m_name->setText(f.name);
00136         m_type->setCurrentItem(f.type);
00137         slotTypeChanged(f.type);
00138         m_string->setText(f.value);
00139         m_number->setValue(f.value.toInt());
00140         m_boolean->setChecked(f.value.toInt() == 1);
00141         m_block = false;
00142     }
00143 }
00144 
00145 void EditEntryDialog::fillEntry(PrintcapEntry *entry)
00146 {
00147     entry->aliases = QStringList::split('|', m_aliases->text(), false);
00148     entry->fields = m_fields;
00149 }
00150 
00151 void EditEntryDialog::slotTypeChanged(int ID)
00152 {
00153     m_stack->raiseWidget(ID);
00154     slotChanged();
00155 }
00156 
00157 #include "editentrydialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys