KProperty

KPropertyUrlEditor.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3 Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
4 Copyright (C) 2016-2018 Jarosław Staniek <staniek@kde.org>
5 Copyright (C) 2018 Dmitry Baryshev <dmitrymq@gmail.com>
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
34KPropertyUrlEditor::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
43KPropertyUrlEditor::~KPropertyUrlEditor() {}
44
45QUrl KPropertyUrlEditor::value() const
46{
47 return d->value.toUrl();
48}
49
50void KPropertyUrlEditor::setValue(const QUrl &value)
51{
52 d->setValue(value);
53 const KPropertyUrlDelegate delegate;
54 d->updateLineEdit(delegate.valueToString(d->value, locale()));
55}
56
57void 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
66bool KPropertyUrlEditor::eventFilter(QObject *o, QEvent *event)
67{
68 d->processEvent(o, event);
70}
71
72// ----
73
74KPropertyUrlDelegate::KPropertyUrlDelegate() {}
75
76QWidget *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
93QString 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}
Editor for ComposedUrl type.
A data structure that composes absolute and relative URLs.
QUrl absoluteUrl() const
Returns absolute URL that has been set for this object.
QString relativePath() const
Returns relative path that has been set for this object.
bool isValid() const
Return true if the URL value that can be computed (from base URL, absolute URL and relative path) is ...
A base class for use by editors that have widget on the left and "..." select button on the right.
Delegate for Url and ComposedUrl types.
Editor for Url type.
static QString valueToLocalizedString(const QVariant &value)
The base class representing a single property.
Definition KProperty.h:96
@ ComposedUrl
composed URL
Definition KProperty.h:155
QString toNativeSeparators(const QString &pathName)
QLocale::Language language() const const
virtual bool eventFilter(QObject *watched, QEvent *event)
bool isEmpty() const const
bool isLocalFile() const const
bool isRelative() const const
bool isValid() const const
void setPath(const QString &path, QUrl::ParsingMode mode)
QString toLocalFile() const const
QString toString(QUrl::FormattingOptions options) const const
bool canConvert(int targetTypeId) const const
QUrl toUrl() const const
T value() const const
virtual bool event(QEvent *event) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:41:55 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.