KFile
knameandurlinputdialog.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright (c) 1998, 2008, 2009 David Faure <faure@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU Lesser General Public License as published by 00006 the Free Software Foundation; either version 2 of the License or (at 00007 your option) version 3 or, at the discretion of KDE e.V. (which shall 00008 act as a proxy as in section 14 of the GPLv3), 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 Lesser 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 "knameandurlinputdialog.h" 00022 00023 #include <klineedit.h> 00024 #include <kurlrequester.h> 00025 #include <kprotocolmanager.h> 00026 #include <QFormLayout> 00027 #include <QLabel> 00028 00029 class KNameAndUrlInputDialogPrivate 00030 { 00031 public: 00032 KNameAndUrlInputDialogPrivate(KNameAndUrlInputDialog* qq) : m_fileNameEdited(false), q(qq) {} 00033 00034 void _k_slotNameTextChanged(const QString&); 00035 void _k_slotURLTextChanged(const QString&); 00036 00040 KLineEdit *m_leName; 00044 KUrlRequester *m_urlRequester; 00048 bool m_fileNameEdited; 00049 KNameAndUrlInputDialog* q; 00050 }; 00051 00052 KNameAndUrlInputDialog::KNameAndUrlInputDialog(const QString& nameLabel, const QString& urlLabel, const KUrl& startDir, QWidget *parent) 00053 : KDialog(parent), d(new KNameAndUrlInputDialogPrivate(this)) 00054 { 00055 setButtons(Ok | Cancel); 00056 00057 QWidget* plainPage = new QWidget(this); 00058 setMainWidget(plainPage); 00059 00060 QFormLayout* topLayout = new QFormLayout(plainPage); 00061 topLayout->setMargin(0); 00062 00063 // First line: filename 00064 d->m_leName = new KLineEdit; 00065 d->m_leName->setMinimumWidth(d->m_leName->sizeHint().width() * 3); 00066 d->m_leName->setSelection(0, d->m_leName->text().length()); // autoselect 00067 connect(d->m_leName, SIGNAL(textChanged(QString)), 00068 SLOT(_k_slotNameTextChanged(QString))); 00069 topLayout->addRow(nameLabel, d->m_leName); 00070 00071 // Second line: url 00072 d->m_urlRequester = new KUrlRequester; 00073 d->m_urlRequester->setStartDir(startDir); 00074 d->m_urlRequester->setMode(KFile::File | KFile::Directory); 00075 00076 d->m_urlRequester->setMinimumWidth(d->m_urlRequester->sizeHint().width() * 3); 00077 connect(d->m_urlRequester->lineEdit(), SIGNAL(textChanged(QString)), 00078 SLOT(_k_slotURLTextChanged(QString))); 00079 topLayout->addRow(urlLabel, d->m_urlRequester); 00080 00081 d->m_fileNameEdited = false; 00082 enableButtonOk(!d->m_leName->text().isEmpty() && !d->m_urlRequester->url().isEmpty()); 00083 d->m_leName->setFocus(); 00084 } 00085 00086 KNameAndUrlInputDialog::~KNameAndUrlInputDialog() 00087 { 00088 delete d; 00089 } 00090 00091 KUrl KNameAndUrlInputDialog::url() const 00092 { 00093 if (result() == QDialog::Accepted) { 00094 return d->m_urlRequester->url(); 00095 } 00096 else 00097 return KUrl(); 00098 } 00099 00100 QString KNameAndUrlInputDialog::name() const 00101 { 00102 if (result() == QDialog::Accepted) 00103 return d->m_leName->text(); 00104 else 00105 return QString(); 00106 } 00107 00108 void KNameAndUrlInputDialogPrivate::_k_slotNameTextChanged(const QString&) 00109 { 00110 m_fileNameEdited = true; 00111 q->enableButtonOk(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty()); 00112 } 00113 00114 void KNameAndUrlInputDialogPrivate::_k_slotURLTextChanged(const QString&) 00115 { 00116 if (!m_fileNameEdited) { 00117 // use URL as default value for the filename 00118 // (we copy only its filename if protocol supports listing, 00119 // but for HTTP we don't want tons of index.html links) 00120 KUrl url(m_urlRequester->url()); 00121 if (KProtocolManager::supportsListing(url) && !url.fileName().isEmpty()) 00122 m_leName->setText(url.fileName()); 00123 else 00124 m_leName->setText(url.url()); 00125 m_fileNameEdited = false; // slotNameTextChanged set it to true erroneously 00126 } 00127 q->enableButtonOk(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty()); 00128 } 00129 00130 void KNameAndUrlInputDialog::setSuggestedName(const QString& name) 00131 { 00132 d->m_leName->setText(name); 00133 d->m_urlRequester->setFocus(); 00134 } 00135 00136 void KNameAndUrlInputDialog::setSuggestedUrl(const KUrl& url) 00137 { 00138 d->m_urlRequester->setUrl(url); 00139 } 00140 00141 #include "knameandurlinputdialog.moc"
KDE 4.7 API Reference