lib

urledit.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 "urledit.h"
00022 
00023 #include <qlayout.h>
00024 #include <qvariant.h>
00025 
00026 #ifndef QT_ONLY
00027 #include <kurlrequester.h>
00028 #include <klineedit.h>
00029 #else
00030 #include <qpushbutton.h>
00031 #include <qlineedit.h>
00032 #include <qfiledialog.h>
00033 #endif
00034 
00035 #include "property.h"
00036 
00037 
00038 using namespace KoProperty;
00039 
00040 URLEdit::URLEdit(Property *property, QWidget *parent, const char *name)
00041  : Widget(property, parent, name)
00042 {
00043     QHBoxLayout *l = new QHBoxLayout(this, 0, 0);
00044 #ifndef QT_ONLY
00045     m_edit = new KURLRequester(this);
00046     m_edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
00047     m_edit->setMinimumHeight(5);
00048     l->addWidget(m_edit);
00049 
00050     setProperty(property);
00051 
00052     connect(m_edit, SIGNAL(textChanged(const QString&)), this, SLOT(slotValueChanged(const QString&)));
00053 #else
00054     m_edit = new QLineEdit(this);
00055     m_select = new QPushButton("...",this);
00056     l->addWidget(m_edit);
00057     l->addWidget(m_select);
00058     connect( m_select, SIGNAL(clicked()),this,SLOT(selectFile()));
00059     connect(m_edit, SLOT(textChanged(const QString&)), this, slotValueChanged(const QString&));
00060 #endif
00061     m_edit->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
00062 }
00063 
00064 URLEdit::~URLEdit()
00065 {}
00066 
00067 QVariant
00068 URLEdit::value() const
00069 {
00070 #ifdef QT_ONLY
00071     return m_edit->text();
00072 #else
00073     return m_edit->url();
00074 #endif
00075 }
00076 
00077 void
00078 URLEdit::setValue(const QVariant &value, bool emitChange)
00079 {
00080     m_edit->blockSignals(true);
00081 #ifdef QT_ONLY
00082     m_edit->setText(value.toString());
00083 #else
00084     m_edit->setURL(value.toString());
00085 #endif
00086     m_edit->blockSignals(false);
00087     if (emitChange)
00088         emit valueChanged(this);
00089 }
00090 
00091 void
00092 URLEdit::slotValueChanged(const QString&)
00093 {
00094     emit valueChanged(this);
00095 }
00096 
00097 void
00098 URLEdit::setProperty(Property *property)
00099 {
00100 #ifndef QT_ONLY
00101     int  mode;
00102     if(property) {
00103         switch(property->type()) {
00104             case DirectoryURL:  mode = KFile::Directory|KFile::ExistingOnly;  break;
00105             case FileURL: case PictureFileURL: default: mode = KFile::File|KFile::ExistingOnly;
00106         }
00107         m_edit->setMode(mode);
00108     }
00109 #endif
00110 
00111     Widget::setProperty(property);
00112 }
00113 
00114 void
00115 URLEdit::selectFile()
00116 {
00117 #ifdef QT_ONLY
00118     QString path;
00119     if(m_property->type() == DirectoryURL)
00120         path = QFileDialog::getExistingDirectory(path,this);
00121     else if(m_property->type() == FileURL)
00122         path = QFileDialog::getOpenFileName(path, QString::null, this);
00123     //slotValueChanged(m_url);
00124     m_edit->setText(path);
00125 #endif
00126 }
00127 
00128 void
00129 URLEdit::setReadOnlyInternal(bool readOnly)
00130 {
00131 #ifndef QT_ONLY
00132     m_edit->lineEdit()->setReadOnly(readOnly);
00133     m_edit->button()->setEnabled(!readOnly);
00134 #else
00135     m_edit->setReadOnly(readOnly);
00136     m_select->setEnabled(!readOnly);
00137 #endif
00138 }
00139 
00140 #include "urledit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys