File indexing completed on 2024-04-14 04:36:11
0001 /* This file is part of the KDE project 0002 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr> 0003 Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net> 0004 Copyright (C) 2016-2018 Jarosław Staniek <staniek@kde.org> 0005 Copyright (C) 2018 Dmitry Baryshev <dmitrymq@gmail.com> 0006 0007 This library is free software; you can redistribute it and/or 0008 modify it under the terms of the GNU Library General Public 0009 License as published by the Free Software Foundation; either 0010 version 2 of the License, or (at your option) any later version. 0011 0012 This library is distributed in the hope that it will be useful, 0013 but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 Library General Public License for more details. 0016 0017 You should have received a copy of the GNU Library General Public License 0018 along with this library; see the file COPYING.LIB. If not, write to 0019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0020 * Boston, MA 02110-1301, USA. 0021 */ 0022 0023 #include "KPropertyUrlEditor.h" 0024 #include "KPropertyComposedUrl.h" 0025 #include "KPropertyComposedUrlEditor.h" 0026 #include "KPropertyEditorView.h" 0027 #include "KPropertyUrlEditor_p.h" 0028 #include "KPropertyUtils.h" 0029 #include "kproperty_debug.h" 0030 0031 #include <QDir> 0032 #include <QLineEdit> 0033 0034 KPropertyUrlEditor::KPropertyUrlEditor(const KProperty &property, QWidget *parent) 0035 : KPropertyGenericSelectionEditor(parent) 0036 , d(new KPropertyUrlEditorPrivate(this, property)) 0037 { 0038 setMainWidget(d->lineEdit); 0039 connect(d.data(), &KPropertyUrlEditorPrivate::commitData, this, 0040 [this] { emit commitData(this); }); 0041 } 0042 0043 KPropertyUrlEditor::~KPropertyUrlEditor() {} 0044 0045 QUrl KPropertyUrlEditor::value() const 0046 { 0047 return d->value.toUrl(); 0048 } 0049 0050 void KPropertyUrlEditor::setValue(const QUrl &value) 0051 { 0052 d->setValue(value); 0053 const KPropertyUrlDelegate delegate; 0054 d->updateLineEdit(delegate.valueToString(d->value, locale())); 0055 } 0056 0057 void KPropertyUrlEditor::selectButtonClicked() 0058 { 0059 QUrl url = d->getUrl(); 0060 if (url.isValid() && d->checkAndUpdate(&url)) { 0061 setValue(url); 0062 emit commitData(this); 0063 } 0064 } 0065 0066 bool KPropertyUrlEditor::eventFilter(QObject *o, QEvent *event) 0067 { 0068 d->processEvent(o, event); 0069 return KPropertyGenericSelectionEditor::eventFilter(o, event); 0070 } 0071 0072 // ---- 0073 0074 KPropertyUrlDelegate::KPropertyUrlDelegate() {} 0075 0076 QWidget *KPropertyUrlDelegate::createEditor(int type, QWidget *parent, 0077 const QStyleOptionViewItem &option, 0078 const QModelIndex &index) const 0079 { 0080 Q_UNUSED(option) 0081 0082 const KProperty *prop = KPropertyUtils::propertyForIndex(index); 0083 0084 if (type == KProperty::Url) { 0085 return new KPropertyUrlEditor(prop ? *prop : KProperty(), parent); 0086 } else if (type == KProperty::ComposedUrl) { 0087 return new KPropertyComposedUrlEditor(prop ? *prop : KProperty(), parent); 0088 } 0089 0090 return nullptr; 0091 } 0092 0093 QString KPropertyUrlDelegate::valueToString(const QVariant &value, const QLocale &locale) const 0094 { 0095 QUrl url; 0096 0097 if (value.canConvert<QUrl>()) { 0098 url = value.toUrl(); 0099 } else if (value.canConvert<KPropertyComposedUrl>()) { 0100 const KPropertyComposedUrl composedUrl = value.value<KPropertyComposedUrl>(); 0101 0102 if (composedUrl.isValid()) { 0103 if (!composedUrl.relativePath().isEmpty()) { 0104 QUrl urlWithPath; 0105 urlWithPath.setPath(composedUrl.relativePath()); 0106 url = urlWithPath; 0107 } else { 0108 url = composedUrl.absoluteUrl(); 0109 } 0110 } else { 0111 return QString(); 0112 } 0113 } else { 0114 return QString(); 0115 } 0116 0117 QString s; 0118 0119 if (url.isLocalFile()) { 0120 s = QDir::toNativeSeparators(url.toLocalFile()); 0121 // TODO this assumes local files only 0122 } else if (url.isRelative()) { 0123 s = QDir::toNativeSeparators(url.toString()); 0124 } else { 0125 s = url.toString(); 0126 } 0127 0128 if (locale.language() == QLocale::C) { 0129 return s; 0130 } 0131 return valueToLocalizedString(s); 0132 }