diff --git a/examples/window.cpp b/examples/window.cpp index b86a0e4..6dcdc2f 100644 --- a/examples/window.cpp +++ b/examples/window.cpp @@ -1,240 +1,258 @@ /* This file is part of the KDE project Copyright (C) 2004 Cedric Pasteur Copyright (C) 2008-2015 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "window.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#include +#include Window::Window() : QWidget() , m_set(this, "example") , m_flatOption("flat", QCoreApplication::translate("main", "Flat display: do not display groups\n(useful for testing)")) , m_fontSizeOption("font-size", QCoreApplication::translate("main", "Set font size to (in points)\n(useful for testing whether editors keep the font settings)"), QCoreApplication::translate("main", "size")) , m_propertyOption("property", QCoreApplication::translate("main", "Display only specified property\n(useful when we want to focus on testing a single\nproperty editor)"), QCoreApplication::translate("main", "name")) , m_roOption("ro", QCoreApplication::translate("main", "Set all properties as read-only:\n(useful for testing read-only mode)")) { setObjectName("kpropertyexamplewindow"); setWindowIcon(QIcon::fromTheme("document-properties")); m_parser.setApplicationDescription(QCoreApplication::translate("main", "An example application for the KProperty library.")); m_parser.addHelpOption(); m_parser.addVersionOption(); parseCommandLine(); const QString singleProperty = m_parser.value(m_propertyOption); bool ok; const int fontSize = m_parser.value(m_fontSizeOption).toInt(&ok); if (fontSize > 0 && ok) { QFont f(font()); f.setPointSize(fontSize); setFont(f); } /* First, create the Set which will hold the properties. */ KProperty *p = 0; m_set.setReadOnly(m_parser.isSet(m_roOption)); QByteArray group; if (!m_parser.isSet(m_flatOption)) { group = "SimpleGroup"; m_set.setGroupDescription(group, "Simple Group"); } if (singleProperty.isEmpty() || singleProperty=="Name") { m_set.addProperty(new KProperty("Name", "Name"), group); m_set["Name"].setAutoSync(1); } if (singleProperty.isEmpty() || singleProperty=="Int") { m_set.addProperty(new KProperty("Int", 2, "Int"), group); } if (singleProperty.isEmpty() || singleProperty=="Double") { m_set.addProperty(p = new KProperty("Double", 3.14159, "Double"), group); p->setOption("precision", 4); // will round to 3.1416 } if (singleProperty.isEmpty() || singleProperty=="cm") { const qreal cm = 1.0; // 28.3465058 points const qreal points = KPropertyUnit(KPropertyUnit::Centimeter).fromUserValue(cm); m_set.addProperty(p = new KProperty("cm", points, "Double cm"), group); p->setOption("unit", "cm"); // default precision == 2 } if (singleProperty.isEmpty() || singleProperty=="Bool") { m_set.addProperty(new KProperty("Bool", QVariant(true), "Bool"), group); } if (singleProperty.isEmpty() || singleProperty=="3-State") { m_set.addProperty(p = new KProperty("3-State", QVariant(), "3 States", QString(), KProperty::Bool), group); p->setOption("3State", true); } if (singleProperty.isEmpty() || singleProperty=="Date") { m_set.addProperty(p = new KProperty("Date", QDate::currentDate(), "Date"), group); p->setIcon("date"); } if (singleProperty.isEmpty() || singleProperty=="Time") { m_set.addProperty(new KProperty("Time", QTime::currentTime(), "Time"), group); } if (singleProperty.isEmpty() || singleProperty=="DateTime") { m_set.addProperty(new KProperty("DateTime", QDateTime::currentDateTime(), "Date/Time"), group); } QStringList name_list; //strings name_list << "My Item" << "Other Item" << "Third Item"; if (singleProperty.isEmpty() || singleProperty=="List") { QStringList list;//keys list << "myitem" << "otheritem" << "3rditem"; m_set.addProperty(new KProperty("List", list, name_list, "otheritem", "List"), group); } if (singleProperty.isEmpty() || singleProperty=="List2") { // A valueFromList property matching strings with ints (could be any type supported by QVariant) QList keys; keys.append(1); keys.append(2); keys.append(3); KPropertyListData *listData = new KPropertyListData(keys, name_list); m_set.addProperty(new KProperty("List2", listData, 3, "List 2"), group); } // Complex if (!m_parser.isSet(m_flatOption)) { group = "ComplexGroup"; m_set.setGroupDescription(group, "Complex Group"); } if (singleProperty.isEmpty() || singleProperty=="Rect") { m_set.addProperty(new KProperty("Rect", QRect(5,11,100,200), "Rect"), group); } if (singleProperty.isEmpty() || singleProperty=="Point") { m_set.addProperty(new KProperty("Point", QPoint(3, 4), "Point"), group); } if (singleProperty.isEmpty() || singleProperty=="Size") { m_set.addProperty(new KProperty("Size", QSize(10, 20), "Size"), group); } if (singleProperty.isEmpty() || singleProperty=="RectF") { m_set.addProperty(new KProperty("RectF", QRectF(0.1, 0.5, 10.72, 18.21), "RectF"), group); } if (singleProperty.isEmpty() || singleProperty=="PointF") { m_set.addProperty(new KProperty("PointF", QPointF(3.14, 4.15), "PointF"), group); } if (singleProperty.isEmpty() || singleProperty=="SizeF") { m_set.addProperty(new KProperty("SizeF", QSizeF(1.1, 2.45), "SizeF"), group); } // Appearance if (!m_parser.isSet(m_flatOption)) { group = "Appearance Group"; m_set.setGroupDescription(group, "Appearance Group"); m_set.setGroupIcon(group, "appearance"); } if (singleProperty.isEmpty() || singleProperty=="Color") { m_set.addProperty(new KProperty("Color", palette().color(QPalette::Active, QPalette::Background), "Color"), group); } if (singleProperty.isEmpty() || singleProperty=="Pixmap") { QPixmap pm(QIcon::fromTheme("network-wired").pixmap(QSize(16,16))); m_set.addProperty(p = new KProperty("Pixmap", pm, "Pixmap"), group); p->setIcon("kpaint"); } if (singleProperty.isEmpty() || singleProperty=="Font") { QFont myFont("Times New Roman", 12); myFont.setUnderline(true); m_set.addProperty(p = new KProperty("Font", myFont, "Font"), group); p->setIcon("fonts"); } if (singleProperty.isEmpty() || singleProperty=="Cursor") { m_set.addProperty(new KProperty("Cursor", QCursor(Qt::WaitCursor), "Cursor"), group); } if (singleProperty.isEmpty() || singleProperty=="LineStyle") { m_set.addProperty(new KProperty("LineStyle", 3, "Line Style", QString(), KProperty::LineStyle), group); } if (singleProperty.isEmpty() || singleProperty=="SizePolicy") { QSizePolicy sp(sizePolicy()); sp.setHorizontalStretch(1); sp.setVerticalStretch(2); m_set.addProperty(new KProperty("SizePolicy", sp, "Size Policy"), group); } if (singleProperty.isEmpty() || singleProperty=="Invisible") { m_set.addProperty(p = new KProperty("Invisible", "I am invisible", "Invisible"), group); p->setVisible(false); } + if (singleProperty.isEmpty() || singleProperty=="Url") { + m_set.addProperty(p = new KProperty("Url", QUrl("https://community.kde.org/KProperty"), "Url"), group); + } + if (singleProperty.isEmpty() || singleProperty=="ExistingFile") { + m_set.addProperty(p = new KProperty("ExistingFile", QUrl::fromLocalFile(QDir::homePath()), "Existing File"), group); + p->setOption("fileMode", "existingFile"); + } + if (singleProperty.isEmpty() || singleProperty=="OverwriteFile") { + m_set.addProperty(p = new KProperty("OverwriteFile", QUrl::fromLocalFile(QDir::homePath()), "Overwrite File"), group); + p->setOption("fileMode", "existingFile"); + p->setOption("confirmOverwrites", true); + } + if (singleProperty.isEmpty() || singleProperty=="Dir") { + m_set.addProperty(p = new KProperty("Dir", QUrl::fromLocalFile(QDir::homePath()), "Dir"), group); + p->setOption("fileMode", "dirsOnly"); + } // qDebug() << m_set.groupNames(); QVBoxLayout *lyr = new QVBoxLayout(this); m_editorView = new KPropertyEditorView(this); lyr->addWidget(m_editorView); m_editorView->changeSet(&m_set, KPropertyEditorView::ExpandChildItems); m_showGrid = new QCheckBox("Show grid"); m_showGrid->setChecked(true); connect(m_showGrid, &QCheckBox::stateChanged, this, &Window::showGrid); QHBoxLayout *hlyr = new QHBoxLayout; lyr->addLayout(hlyr); hlyr->addWidget(m_showGrid); m_showFrame = new QCheckBox("Show frame"); m_showFrame->setChecked(true); connect(m_showFrame, &QCheckBox::stateChanged, this, &Window::showFrame); hlyr->addWidget(m_showFrame); hlyr->addStretch(1); resize(400, qApp->desktop()->height() - 200); m_editorView->setFocus(); qDebug() << m_set; } Window::~Window() { } void Window::parseCommandLine() { m_parser.addOption(m_flatOption); m_parser.addOption(m_fontSizeOption); m_parser.addOption(m_propertyOption); m_parser.addOption(m_roOption); m_parser.process(*(QCoreApplication::instance())); } void Window::showGrid(int state) { m_editorView->setGridLineColor( state == Qt::Checked ? KPropertyEditorView::defaultGridLineColor() : QColor()); } void Window::showFrame(int state) { m_editorView->setFrameStyle(state == Qt::Checked ? QFrame::Box : QFrame::NoFrame); } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5479578..6b063dc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,257 +1,259 @@ # Options configure_file(config-kproperty.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-kproperty.h) set(kpropertycore_LIB_SRCS KProperty.cpp KPropertySet.cpp KPropertyUnit_p.cpp KPropertyFactory.cpp KPropertyCoreUtils.cpp kproperty_debug.cpp ) if(KPROPERTY_WIDGETS) add_subdirectory(editors) set(kpropertywidgets_LIB_SRCS editors/utils.cpp editors/booledit.cpp editors/coloredit.cpp editors/combobox.cpp editors/cursoredit.cpp editors/dateedit.cpp editors/datetimeedit.cpp # editors/dummywidget.cpp editors/fontedit.cpp editors/pixmapedit.cpp editors/pointedit.cpp editors/pointfedit.cpp editors/rectedit.cpp editors/KPropertyRectFEditor.cpp editors/sizeedit.cpp editors/sizefedit.cpp editors/sizepolicyedit.cpp editors/spinbox.cpp + editors/KPropertyGenericSelectionEditor.cpp editors/KPropertyStringEditor.cpp + editors/KPropertyUrlEditor.cpp editors/linestyleedit.cpp # editors/stringlistedit.cpp # editors/symbolcombo.cpp editors/timeedit.cpp #TODO editors/urledit.cpp kproperty_debug.cpp # needed here too KPropertyWidgetsFactory.cpp KDefaultPropertyFactory.cpp KPropertyEditorView.cpp KPropertyEditorDataModel.cpp KPropertyUtils.cpp KPropertyLineStyleSelector_p.cpp KPropertyLineStyleModel_p.cpp KPropertyLineStyleItemDelegate_p.cpp ) ecm_create_qm_loader(kpropertywidgets_LIB_SRCS kpropertywidgets_qt) set(kpropertywidgets_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/editors ) endif() set(kpropertycore_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ) ecm_create_qm_loader(kpropertycore_LIB_SRCS kpropertycore_qt) add_library(KPropertyCore SHARED ${kpropertycore_LIB_SRCS}) target_link_libraries(KPropertyCore Qt5::Core ) generate_export_header(KPropertyCore) target_include_directories(KPropertyCore PUBLIC "$" INTERFACE "$" ) set_target_properties(KPropertyCore PROPERTIES VERSION 1.0 SOVERSION 1.0.0 EXPORT_NAME KPropertyCore ) if(KPROPERTY_WIDGETS) add_library(KPropertyWidgets SHARED ${kpropertywidgets_LIB_SRCS}) target_link_libraries(KPropertyWidgets KPropertyCore ) if(KPROPERTY_KF) target_link_libraries(KPropertyWidgets KF5::WidgetsAddons KF5::GuiAddons ) else() target_link_libraries(KPropertyWidgets Qt5::Widgets ) endif() generate_export_header(KPropertyWidgets) target_include_directories(KPropertyWidgets PUBLIC "$" INTERFACE "$" PRIVATE editors ) set_target_properties(KPropertyWidgets PROPERTIES VERSION 1.0 SOVERSION 1.0.0 EXPORT_NAME KPropertyWidgets ) endif() # Create a Config.cmake and a ConfigVersion.cmake file and install them set(CMAKECONFIG_CORE_INSTALL_DIR "${CMAKECONFIG_INSTALL_PREFIX}/KPropertyCore") # A place for KProperty plugins set(KPROPERTY_PLUGIN_INSTALL_DIR ${PLUGIN_INSTALL_DIR}/kproperty) ecm_setup_version(${KPROPERTY_VERSION} VARIABLE_PREFIX KPROPERTYCORE VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/kproperty_version.h" PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KPropertyCoreConfigVersion.cmake" ) ecm_configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/KPropertyCoreConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/KPropertyCoreConfig.cmake" INSTALL_DESTINATION "${CMAKECONFIG_CORE_INSTALL_DIR}" ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/KPropertyCoreConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/KPropertyCoreConfigVersion.cmake" DESTINATION "${CMAKECONFIG_CORE_INSTALL_DIR}" COMPONENT Devel) if(SAILFISH) set(KDE_INSTALL_TARGETS_DEFAULT_ARGS LIBRARY DESTINATION lib) endif() install(TARGETS KPropertyCore EXPORT KPropertyCoreTargets ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) install(EXPORT KPropertyCoreTargets DESTINATION "${CMAKECONFIG_CORE_INSTALL_DIR}" FILE KPropertyCoreTargets.cmake) if(KPROPERTY_WIDGETS) set(CMAKECONFIG_WIDGETS_INSTALL_DIR "${CMAKECONFIG_INSTALL_PREFIX}/KPropertyWidgets") ecm_setup_version(${KPROPERTY_VERSION} VARIABLE_PREFIX KPROPERTYWIDGETS VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/kproperty_version.h" PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KPropertyWidgetsConfigVersion.cmake" ) ecm_configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/KPropertyWidgetsConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/KPropertyWidgetsConfig.cmake" INSTALL_DESTINATION "${CMAKECONFIG_WIDGETS_INSTALL_DIR}" ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/KPropertyWidgetsConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/KPropertyWidgetsConfigVersion.cmake" DESTINATION "${CMAKECONFIG_WIDGETS_INSTALL_DIR}" COMPONENT Devel) install(TARGETS KPropertyWidgets EXPORT KPropertyWidgetsTargets ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) install(EXPORT KPropertyWidgetsTargets DESTINATION "${CMAKECONFIG_WIDGETS_INSTALL_DIR}" FILE KPropertyWidgetsTargets.cmake) endif() install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR}) if (GENERATE_PRI) ecm_generate_pri_file( BASE_NAME KPropertyCore LIB_NAME KPropertyCore DEPS "core" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${INCLUDE_INSTALL_DIR}/KPropertyCore ) if(KPROPERTY_WIDGETS) ecm_generate_pri_file( BASE_NAME KPropertyWidgets LIB_NAME KPropertyWidgets DEPS "widgets KPropertyCore" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${INCLUDE_INSTALL_DIR}/KPropertyWidgets ) endif() endif() #ecm_install_icons(${DATA_INSTALL_DIR}/kproperty/icons) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/kproperty_version.h" DESTINATION "${INCLUDE_INSTALL_DIR}" COMPONENT Devel) ecm_generate_headers(kpropertycore_FORWARDING_HEADERS REQUIRED_HEADERS kpropertycore_HEADERS ORIGINAL CAMELCASE HEADER_NAMES KProperty KPropertySet KPropertyFactory ) install( FILES ${kpropertycore_HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR}/KPropertyCore COMPONENT Devel ) install( FILES ${kpropertycore_FORWARDING_HEADERS} ${PROJECT_BINARY_DIR}/src/kpropertycore_export.h ${PROJECT_BINARY_DIR}/src/config-kproperty.h DESTINATION ${INCLUDE_INSTALL_DIR}/KPropertyCore COMPONENT Devel ) if(KPROPERTY_WIDGETS) ecm_generate_headers(kpropertywidgets_FORWARDING_HEADERS REQUIRED_HEADERS kpropertywidgets_HEADERS ORIGINAL CAMELCASE HEADER_NAMES KPropertyWidgetsFactory KPropertyUtils KPropertyEditorView KPropertyEditorDataModel ) install( FILES ${kpropertywidgets_HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR}/KPropertyWidgets COMPONENT Devel ) install( FILES ${kpropertywidgets_FORWARDING_HEADERS} ${PROJECT_BINARY_DIR}/src/kpropertywidgets_export.h DESTINATION ${INCLUDE_INSTALL_DIR}/KPropertyWidgets COMPONENT Devel ) endif() enable_testing() diff --git a/src/KDefaultPropertyFactory.cpp b/src/KDefaultPropertyFactory.cpp index ce8a6d5..8a60ca2 100644 --- a/src/KDefaultPropertyFactory.cpp +++ b/src/KDefaultPropertyFactory.cpp @@ -1,79 +1,81 @@ /* This file is part of the KDE project Copyright (C) 2008-2015 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KDefaultPropertyFactory.h" #include "config-kproperty.h" /* #include "customproperty.h"*/ #include "editors/booledit.h" #include "editors/coloredit.h" #include "editors/combobox.h" #include "editors/cursoredit.h" #include "editors/dateedit.h" #include "editors/datetimeedit.h" // #include "editors/dummywidget.h" #include "editors/pixmapedit.h" #include "editors/pointedit.h" #include "editors/pointfedit.h" #include "editors/fontedit.h" #include "editors/rectedit.h" #include "editors/KPropertyRectFEditor.h" #include "editors/sizeedit.h" #include "editors/sizefedit.h" #include "editors/sizepolicyedit.h" #include "editors/spinbox.h" /*#include "stringlistedit.h"*/ #include "editors/linestyleedit.h" #include "editors/KPropertyStringEditor.h" // #include "symbolcombo.h" #include "editors/timeedit.h" +#include "editors/KPropertyUrlEditor.h" // #include "urledit.h" KDefaultPropertyFactory::KDefaultPropertyFactory() : KPropertyWidgetsFactory() { addEditor( KProperty::Bool, new KPropertyBoolDelegate ); //! @todo addEditor( KProperty::ByteArray, new KPropertyByteArrayDelegate ); addEditor( KProperty::Color, new KPropertyColorComboDelegate ); addEditor( KProperty::Cursor, new KPropertyCursorDelegate ); addEditor( KProperty::Date, new KPropertyDateDelegate ); addEditor( KProperty::DateTime, new KPropertyDateTimeDelegate ); addEditor( KProperty::Double, new KPropertyDoubleSpinBoxDelegate ); addEditor( KProperty::Font, new KPropertyFontDelegate ); addEditor( KProperty::Int, new KPropertyIntSpinBoxDelegate ); addEditor( KProperty::LineStyle, new KPropertyLineStyleComboDelegate ); addEditor( KProperty::LongLong, new KPropertyIntSpinBoxDelegate ); //!< @todo add more specialized delegate addEditor( KProperty::Pixmap, new KPropertyPixmapDelegate ); addEditor( KProperty::Point, new KPropertyPointDelegate ); addEditor( KProperty::PointF, new KPropertyPointFDelegate ); addEditor( KProperty::Rect, new KPropertyRectDelegate ); addEditor( KProperty::RectF, new KPropertyRectFDelegate ); addEditor( KProperty::Size, new KPropertySizeDelegate ); addEditor( KProperty::SizeF, new KPropertySizeFDelegate ); addEditor( KProperty::SizePolicy, new KPropertySizePolicyDelegate ); addEditor( KProperty::String, new KPropertyStringDelegate ); addEditor( KProperty::Time, new KPropertyTimeDelegate ); addEditor( KProperty::UInt, new KPropertyIntSpinBoxDelegate ); //!< @todo add more specialized delegate addEditor( KProperty::ULongLong, new KPropertyIntSpinBoxDelegate ); //!< @todo add more specialized delegate + addEditor( KProperty::Url, new KPropertyUrlDelegate ); addEditor( KProperty::ValueFromList, new KPropertyComboBoxDelegate ); } KDefaultPropertyFactory::~KDefaultPropertyFactory() { } diff --git a/src/KPropertyEditorView.h b/src/KPropertyEditorView.h index b0138a7..558518c 100644 --- a/src/KPropertyEditorView.h +++ b/src/KPropertyEditorView.h @@ -1,166 +1,177 @@ /* This file is part of the KDE project Copyright (C) 2008-2009 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KPROPERTY_EDITORVIEW_H #define KPROPERTY_EDITORVIEW_H #include "kpropertywidgets_export.h" #include class KProperty; class KPropertySet; //! @brief A widget for editing properties //! @todo review this ............. /*! Editor widgets use property options using Property::option(const char *) to override default behaviour of editor items. Currently supported options are:
  • min: integer setting for minimum value for integer and double types. The default is 0. Set it to -1, if you want this special value to be allowed.
  • minValueText: user-visible translated string used for integer type to set "specialValueText" widget's property
  • max: integer setting for minimum value the property of integer type. Default is 0xffff.
  • precision: The number of decimals after the decimal point (for double types).
  • step: the size of the step that is taken when the user hits the up or down buttons (for double types).
  • 3State: boolean value used for boolean types; if true, the editor becomes a combobox (instead of checkable button) and accepta the third "null" state.
  • yesName: user-visible translated string used for boolean types (both 2- and 3-state) to display the "true" value. If not present, tr("Yes") is used.
  • noName: user-visible translated string used for boolean types (both 2- and 3-state) to display the "false" value. If not present, tr("No") is used.
  • 3rdStateName: user-visible translated string used for boolean types (both 2- and 3-state) to display the 3rd state "null" value. If not present, tr("None") is used.
  • nullName: user-visible translated string used for boolean types to display the "null" value if and only if the property accepts two states (i.e. when "3State" option is not true). If this option is not present, null values are displayed as false.
  • extraValueAllowed: Allow the user to manually enter a value into a combobox - that is not in the list. The entered text will be returned as opposed to a matching key. + that is not in the list. The entered text will be returned as opposed to a matching key.
  • +
  • fileMode: indicates what the user may select in the file dialog for the url type's editor: +
      +
    • "dirsOnly": only support and display directories + See QFileDialog::ShowDirsOnly and QFileDialog::Directory.
    • +
    • "existingFile": only allow to select one existing file. See QFileDialog::ExistingFile.
    • +
    • For any other value, supports any file, whether it exists or not. See QFileDialog::AnyFile.
    • +
    + @note The line edit does not validate the content. +
  • +
  • confirmOverwrites: if true, user will be asked for file overwriting by url editor; false by default. + @note The line edit does not validate the content.
*/ class KPROPERTYWIDGETS_EXPORT KPropertyEditorView : public QTreeView { Q_OBJECT public: /*! Creates an empty property editor with @a parent as parent widget. */ explicit KPropertyEditorView(QWidget *parent = 0); ~KPropertyEditorView(); //! Options for changeSet(). enum SetOption { NoOptions = 0, PreservePreviousSelection = 1, //!< If used, previously selected editor item //!< will be kept selected. AlphabeticalOrder = 2, //!< Alphabetical order of properties (the default is insert-order) ExpandChildItems = 4 //!< Child property items are expanded (the default is "collapsed") }; Q_DECLARE_FLAGS(SetOptions, SetOption) //! @return grid line color, defaultGridLineColor() by default QColor gridLineColor() const; //! @return default grid line color - Qt::gray static QColor defaultGridLineColor() { return Qt::gray; } public Q_SLOTS: /*! Populates the editor view with items for each property from the @ set set. Child items for composed properties are also created. See SetOption documentation for description of @a options options. If @a preservePreviousSelection is true, previously selected editor item will be kept selected, if present. */ void changeSet(KPropertySet *set, SetOptions options = NoOptions); /*! Populates the editor view with items for each property from the @ set set. Child items for composed properties are also created. If @a propertyToSelect is provided, item for this property name will be selected, if present. */ void changeSet(KPropertySet *set, const QByteArray& propertyToSelect, SetOptions options = NoOptions); /*! If @a enable is true (the default), property values are automatically synced as soon as editor contents change (e.g. every time the user types a character) and the values are written back to the assigned property set. If @a enable is false, property set is updated only when selection within the property editor or user presses Enter/Return key. Each property can overwrite this setting by changing its own autoSync flag. */ void setAutoSync(bool enable); /*! @return value of autoSync flag. */ bool isAutoSync() const; /*! Accepts the changes made to the current editor item (if any) (as if the user had pressed Enter key). */ void acceptInput(); //! Sets color of grid lines. Use invalid color QColor() to hide grid lines. void setGridLineColor(const QColor& color); Q_SIGNALS: /*! Emitted when current property set has been changed. May be 0. */ void propertySetChanged(KPropertySet *set); protected: virtual bool viewportEvent( QEvent * event ); protected Q_SLOTS: virtual void currentChanged( const QModelIndex & current, const QModelIndex & previous ); virtual void commitData( QWidget * editor ); /*! Called when current propertis of this set are about to be cleared. */ void slotSetWillBeCleared(); /*! Called when current property set is about to be destroyed. */ void slotSetWillBeDeleted(); /*! Updates editor widget in the editor.*/ void slotPropertyChanged(KPropertySet& set, KProperty& property); void slotPropertyReset(KPropertySet& set, KProperty& property); private: /*! Used by changeSet(). */ void changeSetInternal(KPropertySet *set, SetOptions options, const QByteArray& propertyToSelect); virtual bool edit( const QModelIndex & index, EditTrigger trigger, QEvent * event ); virtual void drawBranches( QPainter * painter, const QRect & rect, const QModelIndex & index ) const; virtual void mousePressEvent( QMouseEvent * event ); //! @return true if @a x is within the area of the revert button for @a index index. bool withinRevertButtonArea( int x, const QModelIndex& index ) const; //! @return area of revert button, if it is displayed for @a index index. //! Otherwise invalid QRect is returned. QRect revertButtonArea( const QModelIndex& index ) const; //! Updates item for @a index and all its children. void updateSubtree(const QModelIndex &index); /*! Undoes the last change in the property editor.*/ void undo(); class Private; Private * const d; }; Q_DECLARE_OPERATORS_FOR_FLAGS(KPropertyEditorView::SetOptions) #endif diff --git a/src/editors/CMakeLists.txt b/src/editors/CMakeLists.txt index 0bbd1d9..5fed78f 100644 --- a/src/editors/CMakeLists.txt +++ b/src/editors/CMakeLists.txt @@ -1,51 +1,54 @@ set(KPROPERTY_EDITORS_HEADERS Utils BoolEdit ColorEdit ComboBox CursorEdit DateEdit DateTimeEdit #DummyWidget FontEdit LineStyleEdit PixmapEdit PointEdit RectEdit #TODO KPropertyRectFEditor SizeEdit SizePolicyEdit SpinBox #TODO KPropertyStringEditor #StringListEdit #SymbolCombo TimeEdit - #TODO UrlEdit + #TODO KPropertyUrlEditor + #TODO KPropertyGenericSelectionEditor ) ecm_generate_headers(kproperty_editors_FORWARDING_HEADERS REQUIRED_HEADERS kproperty_editors_HEADERS # TODO: rename to camelcase, then uncomment "ORIGINAL CAMELCASE" HEADER_NAMES ${KPROPERTY_EDITORS_HEADERS} ) ecm_generate_headers(kproperty_editors_FORWARDING_HEADERS REQUIRED_HEADERS kproperty_editors_HEADERS ORIGINAL CAMELCASE HEADER_NAMES + KPropertyGenericSelectionEditor KPropertyStringEditor + KPropertyUrlEditor ) install( FILES ${kproperty_editors_HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR}/KPropertyWidgets COMPONENT Devel ) install( FILES ${kproperty_editors_FORWARDING_HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR}/KPropertyWidgets COMPONENT Devel ) diff --git a/src/editors/KPropertyGenericSelectionEditor.cpp b/src/editors/KPropertyGenericSelectionEditor.cpp new file mode 100644 index 0000000..4c60476 --- /dev/null +++ b/src/editors/KPropertyGenericSelectionEditor.cpp @@ -0,0 +1,72 @@ +/* This file is part of the KDE project + Copyright (C) 2016 Jarosław Staniek + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. +*/ + +#include "KPropertyGenericSelectionEditor.h" + +#include +#include + +class KPropertyGenericSelectionEditor::Private +{ +public: + Private() { + } + QHBoxLayout *lyr; + QPushButton *btn; +}; + +KPropertyGenericSelectionEditor::KPropertyGenericSelectionEditor(QWidget *parent) + : QWidget(parent), d(new Private) +{ + setAutoFillBackground(true); + d->lyr = new QHBoxLayout(this); + d->lyr->setContentsMargins(0, 0, 0, 0); + d->lyr->setSpacing(2); + d->btn = new QPushButton(tr("...", "... button")); + d->btn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); + d->btn->setFixedWidth(d->btn->fontMetrics().width(d->btn->text() + QLatin1String(" "))); + d->btn->setFocusPolicy(Qt::NoFocus); + connect(d->btn, &QPushButton::clicked, this, &KPropertyGenericSelectionEditor::selectButtonClicked); + d->lyr->addWidget(d->btn); +} + +KPropertyGenericSelectionEditor::~KPropertyGenericSelectionEditor() +{ +} + +void KPropertyGenericSelectionEditor::setMainWidget(QWidget *widget) +{ + if (d->lyr->count() > 1) { + delete d->lyr->takeAt(0)->widget(); + } + if (widget) { + d->lyr->insertWidget(0, widget); + widget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred); + setFocusProxy(widget); + } +} + +void KPropertyGenericSelectionEditor::setSelectionButtonVisible(bool set) +{ + d->btn->setVisible(set); +} + +void KPropertyGenericSelectionEditor::selectButtonClicked() +{ +} diff --git a/src/editors/urledit.h b/src/editors/KPropertyGenericSelectionEditor.h similarity index 53% rename from src/editors/urledit.h rename to src/editors/KPropertyGenericSelectionEditor.h index fc3a1aa..a46055c 100644 --- a/src/editors/urledit.h +++ b/src/editors/KPropertyGenericSelectionEditor.h @@ -1,57 +1,54 @@ /* This file is part of the KDE project - Copyright (C) 2004 Cedric Pasteur - Copyright (C) 2004 Alexander Dymo + Copyright (C) 2016 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ -#ifndef KPROPERTY_URLEDIT_H -#define KPROPERTY_URLEDIT_H +#ifndef KPROPERTYGENERICSELECTIONEDITOR_H +#define KPROPERTYGENERICSELECTIONEDITOR_H -#include "KPropertyFactory.h" +#include "kpropertywidgets_export.h" -class KUrlRequester; +#include +#include -class KPROPERTYWIDGETS_EXPORT KPropertyUrlEditor : public Widget +//! A base class for use by editors that have widget on the left and "..." select button on the right +class KPROPERTYWIDGETS_EXPORT KPropertyGenericSelectionEditor : public QWidget { - Q_PROPERTY(QVariant value READ value WRITE setValue USER true) Q_OBJECT public: - explicit KPropertyUrlEditor(KProperty *property, QWidget *parent = 0); - virtual ~KPropertyUrlEditor(); + explicit KPropertyGenericSelectionEditor(QWidget *parent = 0); - virtual QVariant value() const; + ~KPropertyGenericSelectionEditor(); - virtual void setProperty(KProperty *property); + //! Sets the visibility of the "..." select button + void setSelectionButtonVisible(bool set); -Q_SIGNALS: - void commitData( QWidget * editor ); - -public Q_SLOTS: - virtual void setValue(const QVariant &value); +protected: + void setMainWidget(QWidget *widget); protected Q_SLOTS: - void slotValueChanged(const QString &url); - -protected: - virtual void setReadOnlyInternal(bool readOnly); + //! Reimplement to react on clicking the "..." select button + virtual void selectButtonClicked(); private: - KUrlRequester *m_edit; + Q_DISABLE_COPY(KPropertyGenericSelectionEditor) + class Private; + QScopedPointer const d; }; #endif diff --git a/src/editors/KPropertyUrlEditor.cpp b/src/editors/KPropertyUrlEditor.cpp new file mode 100644 index 0000000..f0a4ebe --- /dev/null +++ b/src/editors/KPropertyUrlEditor.cpp @@ -0,0 +1,110 @@ +/* This file is part of the KDE project + Copyright (C) 2004 Cedric Pasteur + Copyright (C) 2004 Alexander Dymo + Copyright (C) 2016 Jarosław Staniek + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. +*/ + +#include "KPropertyUrlEditor.h" +#include "KProperty.h" +#include "KPropertyEditorDataModel.h" + +#include +#include + +class KPropertyUrlEditor::Private +{ +public: + Private(const KProperty &property) + : fileMode(property.option("fileMode").toByteArray().toLower()) + , confirmOverwrites(property.option("confirmOverwrites", false).toBool()) + { + } + QUrl value; + QLineEdit *lineEdit; + KPropertyUrlDelegate delegate; + QByteArray fileMode; + bool confirmOverwrites; +}; + +KPropertyUrlEditor::KPropertyUrlEditor(const KProperty &property, QWidget *parent) + : KPropertyGenericSelectionEditor(parent), d(new Private(property)) +{ + d->lineEdit = new QLineEdit; + d->lineEdit->setClearButtonEnabled(true); + setMainWidget(d->lineEdit); +} + +KPropertyUrlEditor::~KPropertyUrlEditor() +{ +} + +QUrl KPropertyUrlEditor::value() const +{ + return d->value; +} + +void KPropertyUrlEditor::setValue(const QUrl &value) +{ + d->value = value; + d->lineEdit->setText(d->delegate.valueToString(d->value, locale())); +} + +void KPropertyUrlEditor::selectButtonClicked() +{ + QUrl url; + QFileDialog::Options options; + if (!d->confirmOverwrites) { + options |= QFileDialog::DontConfirmOverwrite; + } + if (d->fileMode == "existingfile") { + url = QFileDialog::getSaveFileUrl(this, tr("Select File"), d->value, QString(), 0, options); + } else if (d->fileMode == "dirsonly") { + options |= QFileDialog::ShowDirsOnly; + url = QFileDialog::getExistingDirectoryUrl(this, tr("Select Directory"), d->value, options); + } else { + url = QFileDialog::getOpenFileUrl(this, tr("Select File"), d->value, QString(), 0, options); + } + //! @todo filters, more options, supportedSchemes, localFilesOnly? + if (!url.isEmpty()) { + setValue(url); + } +} + +KPropertyUrlDelegate::KPropertyUrlDelegate() +{ +} + +QWidget* KPropertyUrlDelegate::createEditor(int type, QWidget *parent, + const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + Q_UNUSED(type) + Q_UNUSED(option) + const KPropertyEditorDataModel *editorModel = dynamic_cast(index.model()); + const KProperty *prop = editorModel->propertyForItem(index); + return new KPropertyUrlEditor(prop ? *prop : KProperty(), parent); +} + +QString KPropertyUrlDelegate::valueToString(const QVariant& value, const QLocale &locale) const +{ + const QUrl url(value.toUrl()); + QString s(url.isLocalFile() ? url.toLocalFile() : value.toString()); + if (locale.language() == QLocale::C) { + return s; + } + return valueToLocalizedString(s); +} diff --git a/src/editors/KPropertyUrlEditor.h b/src/editors/KPropertyUrlEditor.h new file mode 100644 index 0000000..1e1da68 --- /dev/null +++ b/src/editors/KPropertyUrlEditor.h @@ -0,0 +1,71 @@ +/* This file is part of the KDE project + Copyright (C) 2004 Cedric Pasteur + Copyright (C) 2004 Alexander Dymo + Copyright (C) 2016 Jarosław Staniek + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. +*/ + +#ifndef KPROPERTYURLEDITOR_H +#define KPROPERTYURLEDITOR_H + +#include "KPropertyWidgetsFactory.h" +#include "KPropertyGenericSelectionEditor.h" + +#include + +//! Editor for Url type +class KPROPERTYWIDGETS_EXPORT KPropertyUrlEditor : public KPropertyGenericSelectionEditor +{ + Q_PROPERTY(QUrl value READ value WRITE setValue USER true) + Q_OBJECT + +public: + explicit KPropertyUrlEditor(const KProperty &property, QWidget *parent = 0); + + ~KPropertyUrlEditor(); + + virtual QUrl value() const; + +Q_SIGNALS: + void commitData(QWidget * editor); + +public Q_SLOTS: + virtual void setValue(const QUrl &value); + +protected Q_SLOTS: + virtual void selectButtonClicked(); + +private: + Q_DISABLE_COPY(KPropertyUrlEditor) + class Private; + QScopedPointer const d; +}; + +//! Delegate for Url type +class KPROPERTYWIDGETS_EXPORT KPropertyUrlDelegate : public KPropertyEditorCreatorInterface, + public KPropertyValueDisplayInterface +{ +public: + KPropertyUrlDelegate(); + + virtual QWidget* createEditor(int type, QWidget *parent, + const QStyleOptionViewItem & option, const QModelIndex & index ) const; + + virtual QString valueToString(const QVariant& value, const QLocale &locale) const; +}; + +#endif diff --git a/src/editors/urledit.cpp b/src/editors/urledit.cpp deleted file mode 100644 index db9bacd..0000000 --- a/src/editors/urledit.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2004 Cedric Pasteur - Copyright (C) 2004 Alexander Dymo - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. -*/ - -#include "urledit.h" - -#include -#include - -#include - -KPropertyUrlEditor::KPropertyUrlEditor(KProperty *property, QWidget *parent) - : Widget(property, parent) -{ - QHBoxLayout *l = new QHBoxLayout(this); - l->setMargin(0); - l->setSpacing(0); - - m_edit = new KUrlRequester(this); - m_edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - m_edit->setMinimumHeight(5); - l->addWidget(m_edit); - - setProperty(property); - - connect(m_edit, SIGNAL(textChanged(const QString&)), this, SLOT(slotValueChanged(const QString&))); - m_edit->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); -} - -KPropertyUrlEditor::~KPropertyUrlEditor() -{} - -QVariant -KPropertyUrlEditor::value() const -{ - return m_edit->url(); -} - -void -KPropertyUrlEditor::setValue(const QVariant &value, bool emitChange) -{ - m_edit->blockSignals(true); - m_edit->setUrl(value.toString()); - m_edit->blockSignals(false); - if (emitChange) - emit valueChanged(this); -} - -void -KPropertyUrlEditor::slotValueChanged(const QString&) -{ - emit valueChanged(this); -} - -void -KPropertyUrlEditor::setProperty(KProperty *property) -{ - if (property) { - KFile::Modes mode; - switch (property->type()) { - case DirectoryURL: mode = KFile::Directory|KFile::ExistingOnly; break; - case FileURL: case PictureFileURL: default: mode = KFile::File|KFile::ExistingOnly; - } - m_edit->setMode(mode); - } - - Widget::setProperty(property); -} - -void -KPropertyUrlEditor::setReadOnlyInternal(bool readOnly) -{ - m_edit->lineEdit()->setReadOnly(readOnly); - m_edit->button()->setEnabled(!readOnly); -}