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 <QLayout> 00027 #include <QLabel> 00028 00029 class KNameAndUrlInputDialogPrivate 00030 { 00031 public: 00032 KNameAndUrlInputDialogPrivate(KNameAndUrlInputDialog* qq) : m_fileNameEdited(false), q(qq) {} 00033 00034 void _k_slotClear(); 00035 void _k_slotNameTextChanged(const QString&); 00036 void _k_slotURLTextChanged(const QString&); 00037 00041 KLineEdit *m_leName; 00045 KUrlRequester *m_urlRequester; 00049 bool m_fileNameEdited; 00050 KNameAndUrlInputDialog* q; 00051 }; 00052 00053 KNameAndUrlInputDialog::KNameAndUrlInputDialog(const QString& nameLabel, const QString& urlLabel, const KUrl& startDir, QWidget *parent) 00054 : KDialog(parent), d(new KNameAndUrlInputDialogPrivate(this)) 00055 { 00056 setButtons(Ok | Cancel | User1); 00057 setButtonGuiItem(User1, KStandardGuiItem::clear()); 00058 00059 QFrame* plainPage = new QFrame(this); 00060 setMainWidget(plainPage); 00061 00062 QVBoxLayout* topLayout = new QVBoxLayout(plainPage); 00063 topLayout->setMargin(0); 00064 topLayout->setSpacing(spacingHint()); 00065 00066 // First line: filename 00067 KHBox* fileNameBox = new KHBox(plainPage); 00068 topLayout->addWidget(fileNameBox); 00069 00070 QLabel* label = new QLabel(nameLabel, fileNameBox); 00071 d->m_leName = new KLineEdit(fileNameBox); 00072 d->m_leName->setMinimumWidth(d->m_leName->sizeHint().width() * 3); 00073 label->setBuddy(d->m_leName); 00074 d->m_leName->setSelection(0, d->m_leName->text().length()); // autoselect 00075 connect(d->m_leName, SIGNAL(textChanged(QString)), 00076 SLOT(_k_slotNameTextChanged(QString))); 00077 00078 // Second line: url 00079 KHBox* urlBox = new KHBox(plainPage); 00080 topLayout->addWidget(urlBox); 00081 label = new QLabel(urlLabel, urlBox); 00082 d->m_urlRequester = new KUrlRequester(urlBox); 00083 d->m_urlRequester->setStartDir(startDir); 00084 d->m_urlRequester->setMode(KFile::File | KFile::Directory); 00085 00086 d->m_urlRequester->setMinimumWidth(d->m_urlRequester->sizeHint().width() * 3); 00087 connect(d->m_urlRequester->lineEdit(), SIGNAL(textChanged(QString)), 00088 SLOT(_k_slotURLTextChanged(QString))); 00089 label->setBuddy(d->m_urlRequester); 00090 00091 connect(this, SIGNAL(user1Clicked()), this, SLOT(_k_slotClear())); 00092 d->m_fileNameEdited = false; 00093 } 00094 00095 KNameAndUrlInputDialog::~KNameAndUrlInputDialog() 00096 { 00097 delete d; 00098 } 00099 00100 KUrl KNameAndUrlInputDialog::url() const 00101 { 00102 if (result() == QDialog::Accepted) { 00103 return d->m_urlRequester->url(); 00104 } 00105 else 00106 return KUrl(); 00107 } 00108 00109 QString KNameAndUrlInputDialog::name() const 00110 { 00111 if (result() == QDialog::Accepted) 00112 return d->m_leName->text(); 00113 else 00114 return QString(); 00115 } 00116 00117 void KNameAndUrlInputDialogPrivate::_k_slotClear() 00118 { 00119 m_leName->clear(); 00120 m_urlRequester->clear(); 00121 m_fileNameEdited = false; 00122 } 00123 00124 void KNameAndUrlInputDialogPrivate::_k_slotNameTextChanged(const QString&) 00125 { 00126 m_fileNameEdited = true; 00127 q->enableButtonOk(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty()); 00128 } 00129 00130 void KNameAndUrlInputDialogPrivate::_k_slotURLTextChanged(const QString&) 00131 { 00132 if (!m_fileNameEdited) { 00133 // use URL as default value for the filename 00134 // (we copy only its filename if protocol supports listing, 00135 // but for HTTP we don't want tons of index.html links) 00136 KUrl url(m_urlRequester->url()); 00137 if (KProtocolManager::supportsListing(url) && !url.fileName().isEmpty()) 00138 m_leName->setText(url.fileName()); 00139 else 00140 m_leName->setText(url.url()); 00141 m_fileNameEdited = false; // slotNameTextChanged set it to true erroneously 00142 } 00143 q->enableButtonOk(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty()); 00144 } 00145 00146 void KNameAndUrlInputDialog::setSuggestedName(const QString& name) 00147 { 00148 d->m_leName->setText(name); 00149 d->m_urlRequester->setFocus(); 00150 } 00151 00152 void KNameAndUrlInputDialog::setSuggestedUrl(const KUrl& url) 00153 { 00154 d->m_urlRequester->setUrl(url); 00155 } 00156 00157 #include "knameandurlinputdialog.moc"
KDE 4.6 API Reference