KProperty

KPropertyUrlEditor.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2004 Cedric Pasteur <[email protected]>
3  Copyright (C) 2004 Alexander Dymo <[email protected]>
4  Copyright (C) 2016-2018 Jarosław Staniek <[email protected]>
5  Copyright (C) 2018 Dmitry Baryshev <[email protected]>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21 */
22 
23 #include "KPropertyUrlEditor.h"
24 #include "KPropertyComposedUrl.h"
25 #include "KPropertyComposedUrlEditor.h"
26 #include "KPropertyEditorView.h"
27 #include "KPropertyUrlEditor_p.h"
28 #include "KPropertyUtils.h"
29 #include "kproperty_debug.h"
30 
31 #include <QDir>
32 #include <QLineEdit>
33 
34 KPropertyUrlEditor::KPropertyUrlEditor(const KProperty &property, QWidget *parent)
36  , d(new KPropertyUrlEditorPrivate(this, property))
37 {
38  setMainWidget(d->lineEdit);
39  connect(d.data(), &KPropertyUrlEditorPrivate::commitData, this,
40  [this] { emit commitData(this); });
41 }
42 
43 KPropertyUrlEditor::~KPropertyUrlEditor() {}
44 
45 QUrl KPropertyUrlEditor::value() const
46 {
47  return d->value.toUrl();
48 }
49 
50 void KPropertyUrlEditor::setValue(const QUrl &value)
51 {
52  d->setValue(value);
53  const KPropertyUrlDelegate delegate;
54  d->updateLineEdit(delegate.valueToString(d->value, locale()));
55 }
56 
57 void KPropertyUrlEditor::selectButtonClicked()
58 {
59  QUrl url = d->getUrl();
60  if (url.isValid() && d->checkAndUpdate(&url)) {
61  setValue(url);
62  emit commitData(this);
63  }
64 }
65 
66 bool KPropertyUrlEditor::eventFilter(QObject *o, QEvent *event)
67 {
68  d->processEvent(o, event);
70 }
71 
72 // ----
73 
74 KPropertyUrlDelegate::KPropertyUrlDelegate() {}
75 
76 QWidget *KPropertyUrlDelegate::createEditor(int type, QWidget *parent,
77  const QStyleOptionViewItem &option,
78  const QModelIndex &index) const
79 {
80  Q_UNUSED(option)
81 
82  const KProperty *prop = KPropertyUtils::propertyForIndex(index);
83 
84  if (type == KProperty::Url) {
85  return new KPropertyUrlEditor(prop ? *prop : KProperty(), parent);
86  } else if (type == KProperty::ComposedUrl) {
87  return new KPropertyComposedUrlEditor(prop ? *prop : KProperty(), parent);
88  }
89 
90  return nullptr;
91 }
92 
93 QString KPropertyUrlDelegate::valueToString(const QVariant &value, const QLocale &locale) const
94 {
95  QUrl url;
96 
97  if (value.canConvert<QUrl>()) {
98  url = value.toUrl();
99  } else if (value.canConvert<KPropertyComposedUrl>()) {
100  const KPropertyComposedUrl composedUrl = value.value<KPropertyComposedUrl>();
101 
102  if (composedUrl.isValid()) {
103  if (!composedUrl.relativePath().isEmpty()) {
104  QUrl urlWithPath;
105  urlWithPath.setPath(composedUrl.relativePath());
106  url = urlWithPath;
107  } else {
108  url = composedUrl.absoluteUrl();
109  }
110  } else {
111  return QString();
112  }
113  } else {
114  return QString();
115  }
116 
117  QString s;
118 
119  if (url.isLocalFile()) {
121  // TODO this assumes local files only
122  } else if (url.isRelative()) {
124  } else {
125  s = url.toString();
126  }
127 
128  if (locale.language() == QLocale::C) {
129  return s;
130  }
131  return valueToLocalizedString(s);
132 }
bool canConvert(int targetTypeId) const const
QUrl toUrl() const const
QString toNativeSeparators(const QString &pathName)
composed URL
Definition: KProperty.h:155
T value() const const
QString toString(QUrl::FormattingOptions options) const const
QString relativePath() const
Returns relative path that has been set for this object.
virtual bool event(QEvent *e)
void setPath(const QString &path, QUrl::ParsingMode mode)
bool isEmpty() const const
QUrl absoluteUrl() const
Returns absolute URL that has been set for this object.
QLocale::Language language() const const
virtual bool eventFilter(QObject *watched, QEvent *event)
Editor for Url type.
QString toLocalFile() const const
The base class representing a single property.
Definition: KProperty.h:95
bool isValid() const const
bool isRelative() const const
Delegate for Url and ComposedUrl types.
bool isValid() const
Return true if the URL value that can be computed (from base URL, absolute URL and relative path) is ...
Editor for ComposedUrl type.
A data structure that composes absolute and relative URLs.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
A base class for use by editors that have widget on the left and "..." select button on the right...
bool isLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 23:10:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.