File indexing completed on 2024-05-19 15:46:45

0001 /*
0002     SPDX-FileCopyrightText: 2013 Sven Brauch <svenbrauch@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef PROPERTYPREVIEWWIDGET_H
0008 #define PROPERTYPREVIEWWIDGET_H
0009 
0010 #include <QWidget>
0011 #include <QMultiHash>
0012 
0013 #include <language/duchain/declaration.h>
0014 #include <interfaces/idocument.h>
0015 
0016 #include <KTextEditor/Range>
0017 
0018 using namespace KDevelop;
0019 class QQuickWidget;
0020 
0021 // Describes one supported property, such as "width"
0022 struct SupportedProperty {
0023     explicit SupportedProperty(const QUrl& qmlfile,
0024                       const QString &typeContains = QString(),
0025                       const QString &classContains = QString())
0026     : qmlfile(qmlfile),
0027       typeContains(typeContains),
0028       classContains(classContains)
0029     {
0030     }
0031 
0032     // the absolute (!) URL to the qml file to load when creating a widget
0033     // for this property
0034     QUrl qmlfile;
0035     // A string that must be contained into the string representation of the
0036     // type of the key being matched.
0037     QString typeContains;
0038     // A string that must be contained into the name of the class in which the
0039     // key is declared
0040     QString classContains;
0041 };
0042 
0043 Q_DECLARE_TYPEINFO(SupportedProperty, Q_MOVABLE_TYPE);
0044 
0045 
0046 // This class is responsible for creating the property widgets for editing QML properties
0047 // with e.g. sliders. It knows which properties are supported, and creates a widget from
0048 // a QML file for each supported property when requested. For the actual implementations
0049 // of the widgets, see the propertywidgets/ subfolder, especially the README file.
0050 class PropertyPreviewWidget : public QWidget {
0051 Q_OBJECT
0052 public:
0053     // Constructs a widget operating on the given document if the given key is in the list
0054     // of supported properties.
0055     // key and value must be the key and the current value of the property in question,
0056     // without spaces or other extra characters.
0057     // The ranges must encompass the text which should be replaced by the new values
0058     // selected by the user.
0059     // Returns 0 when the property is not supported, which tells kdevplatform not to
0060     // display any widget when returned from e.g. specialLanguageObjectNavigationWidget.
0061     static QWidget* constructIfPossible(KTextEditor::Document* doc,
0062                                         const KTextEditor::Range& keyRange,
0063                                         const KTextEditor::Range& valueRange,
0064                                         Declaration* decl,
0065                                         const QString& key,
0066                                         const QString& value);
0067     ~PropertyPreviewWidget() override;
0068 
0069 private:
0070     // private because you should use the static constructIfPossible function to create instances,
0071     // to make sure you don't have widgets which operate on unsupported properties.
0072     explicit PropertyPreviewWidget(KTextEditor::Document* doc,
0073                                    const KTextEditor::Range& keyRange, const KTextEditor::Range& valueRange,
0074                                    const SupportedProperty& property, const QString& value);
0075     static QMultiHash<QString, SupportedProperty> supportedProperties;
0076 
0077     QQuickWidget* view;
0078 
0079     // the document the widget replaces text in
0080     KTextEditor::Document* document;
0081     // the range of the key
0082     KTextEditor::Range const keyRange;
0083     // the range of the value to be modified. Not const because the range might change
0084     // if the newly inserted text is smaller or larger than what was there before
0085     // (e.g. 9 -> 10)
0086     KTextEditor::Range valueRange;
0087     // the SupportedProperty instance for this widget
0088     SupportedProperty const property;
0089 
0090 private Q_SLOTS:
0091     // updates the text in the document to contain the new value in valueRange
0092     void updateValue();
0093 };
0094 
0095 #endif