File indexing completed on 2024-05-12 16:39:58
0001 /* This file is part of the KDE project 0002 Copyright (C) 2003-2017 Jarosław Staniek <staniek@kde.org> 0003 0004 Contains code from kglobalsettings.h: 0005 Copyright (C) 2000, 2006 David Faure <faure@kde.org> 0006 Copyright (C) 2008 Friedrich W. H. Kossebau <kossebau@kde.org> 0007 0008 Contains code from kdialog.h: 0009 Copyright (C) 1998 Thomas Tanghus (tanghus@earthling.net) 0010 Additions 1999-2000 by Espen Sand (espen@kde.org) 0011 and Holger Freyther <freyther@kde.org> 0012 2005-2009 Olivier Goffart <ogoffart @ kde.org> 0013 2006 Tobias Koenig <tokoe@kde.org> 0014 0015 This program is free software; you can redistribute it and/or 0016 modify it under the terms of the GNU Library General Public 0017 License as published by the Free Software Foundation; either 0018 version 2 of the License, or (at your option) any later version. 0019 0020 This program is distributed in the hope that it will be useful, 0021 but WITHOUT ANY WARRANTY; without even the implied warranty of 0022 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0023 Library General Public License for more details. 0024 0025 You should have received a copy of the GNU Library General Public License 0026 along with this program; see the file COPYING. If not, write to 0027 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0028 * Boston, MA 02110-1301, USA. 0029 */ 0030 0031 #ifndef KEXIUTILS_UTILS_H 0032 #define KEXIUTILS_UTILS_H 0033 0034 #include "kexiutils_export.h" 0035 #include "kexi_global.h" 0036 0037 #include <QObject> 0038 #include <QMetaMethod> 0039 #include <QFont> 0040 #include <QFrame> 0041 #include <QFontDatabase> 0042 #include <QMimeType> 0043 #include <QUrl> 0044 0045 #include <KIconLoader> 0046 #include <KLocalizedString> 0047 0048 #include <KDbTristate> 0049 0050 class QColor; 0051 class QDomNode; 0052 class QMetaProperty; 0053 class QLayout; 0054 class QLineEdit; 0055 0056 //! @short General Utils 0057 namespace KexiUtils 0058 { 0059 0060 //! \return true if parent of \a o that is of type \a className or false otherwise 0061 inline bool parentIs(QObject* o, const char* className = 0) 0062 { 0063 if (!o) 0064 return false; 0065 while ((o = o->parent())) { 0066 if (o->inherits(className)) 0067 return true; 0068 } 0069 return false; 0070 } 0071 0072 //! \return parent object of \a o that is of type \a type or 0 if no such parent 0073 template<class type> 0074 inline type findParentByType(QObject* o) 0075 { 0076 if (!o) 0077 return 0; 0078 while ((o = o->parent())) { 0079 if (dynamic_cast< type >(o)) 0080 return dynamic_cast< type >(o); 0081 } 0082 return 0; 0083 } 0084 0085 /*! \return first found child of \a o, inheriting \a className. 0086 If objName is 0 (the default), all object names match. 0087 Returned pointer type is casted. */ 0088 KEXIUTILS_EXPORT QObject* findFirstQObjectChild(QObject *o, const char* className, const char* objName); 0089 0090 /*! \return first found child of \a o, that inherit \a className. 0091 If \a objName is 0 (the default), all object names match. 0092 Returned pointer type is casted. */ 0093 template<class type> 0094 inline type findFirstChild(QObject *o, const char* className, const char* objName = 0) 0095 { 0096 return ::qobject_cast< type >(findFirstQObjectChild(o, className, objName)); 0097 } 0098 0099 //! Finds property for name \a name and object \a object returns it index; 0100 //! otherwise returns a null QMetaProperty. 0101 KEXIUTILS_EXPORT QMetaProperty findPropertyWithSuperclasses(const QObject* object, 0102 const char* name); 0103 0104 //! \return true if \a object object is of class name \a className 0105 inline bool objectIsA(QObject* object, const char* className) 0106 { 0107 return 0 == qstrcmp(object->metaObject()->className(), className); 0108 } 0109 0110 //! \return true if \a object object is of the class names inside \a classNames 0111 KEXIUTILS_EXPORT bool objectIsA(QObject* object, const QList<QByteArray>& classNames); 0112 0113 //! \return a list of methods for \a metaObject meta object. 0114 //! The methods are of type declared in \a types and have access declared 0115 //! in \a access. 0116 KEXIUTILS_EXPORT QList<QMetaMethod> methodsForMetaObject( 0117 const QMetaObject *metaObject, QFlags<QMetaMethod::MethodType> types 0118 = QFlags<QMetaMethod::MethodType>(QMetaMethod::Method | QMetaMethod::Signal | QMetaMethod::Slot), 0119 QFlags<QMetaMethod::Access> access 0120 = QFlags<QMetaMethod::Access>(QMetaMethod::Private | QMetaMethod::Protected | QMetaMethod::Public)); 0121 0122 //! Like \ref KexiUtils::methodsForMetaObject() but includes methods from all 0123 //! parent meta objects of the \a metaObject. 0124 KEXIUTILS_EXPORT QList<QMetaMethod> methodsForMetaObjectWithParents( 0125 const QMetaObject *metaObject, QFlags<QMetaMethod::MethodType> types 0126 = QFlags<QMetaMethod::MethodType>(QMetaMethod::Method | QMetaMethod::Signal | QMetaMethod::Slot), 0127 QFlags<QMetaMethod::Access> access 0128 = QFlags<QMetaMethod::Access>(QMetaMethod::Private | QMetaMethod::Protected | QMetaMethod::Public)); 0129 0130 //! \return a list with all this class's properties. 0131 KEXIUTILS_EXPORT QList<QMetaProperty> propertiesForMetaObject( 0132 const QMetaObject *metaObject); 0133 0134 //! \return a list with all this class's properties including thise inherited. 0135 KEXIUTILS_EXPORT QList<QMetaProperty> propertiesForMetaObjectWithInherited( 0136 const QMetaObject *metaObject); 0137 0138 //! \return a list of enum keys for meta property \a metaProperty. 0139 //! If @a filter is not INT_MIN, the method only returns enum keys that overlap with filter 0140 //! and are not combination of other keys. 0141 KEXIUTILS_EXPORT QStringList enumKeysForProperty(const QMetaProperty &metaProperty, 0142 int filter = INT_MIN); 0143 0144 //! Convert a list @a list of @a SourceType type to another list of @a DestinationType 0145 //! type using @a convertMethod function 0146 /*! 0147 Example: 0148 @code 0149 QList<QByteArray> list = ....; 0150 QStringList result = KexiUtils::convertTypesUsingFunction<QByteArray, QString, &QString::fromLatin1>(list); 0151 @endcode */ 0152 template <typename SourceType, typename DestinationType, 0153 DestinationType (*ConvertFunction)(const SourceType&)> 0154 QList<DestinationType> convertTypesUsingFunction(const QList<SourceType> &list) 0155 { 0156 QList<DestinationType> result; 0157 foreach(const SourceType &element, list) { 0158 result.append(ConvertFunction(element)); 0159 } 0160 return result; 0161 } 0162 0163 //! Convert a list @a list of @a SourceType type to another list of @a DestinationType 0164 //! type using @a convertMethod 0165 /*! 0166 Example: 0167 @code 0168 QVariantList list = ....; 0169 QStringList result = KexiUtils::convertTypesUsingMethod<QVariant, QString, &QVariant::toString>(list); 0170 @endcode */ 0171 template <typename SourceType, typename DestinationType, 0172 DestinationType (SourceType::*ConvertMethod)() const> 0173 QList<DestinationType> convertTypesUsingMethod(const QList<SourceType> &list) 0174 { 0175 QList<DestinationType> result; 0176 foreach(const SourceType &element, list) { 0177 result.append((element.*ConvertMethod)()); 0178 } 0179 return result; 0180 } 0181 0182 /*! Sets "wait" cursor with 1 second delay (or 0 seconds if noDelay is true). 0183 Does nothing if the application has no GUI enabled. (see KApplication::guiEnabled()) */ 0184 KEXIUTILS_EXPORT void setWaitCursor(bool noDelay = false); 0185 0186 /*! Remove "wait" cursor previously set with \a setWaitCursor(), 0187 even if it's not yet visible. 0188 Does nothing if the application has no GUI enabled. (see KApplication::guiEnabled()) */ 0189 KEXIUTILS_EXPORT void removeWaitCursor(); 0190 0191 /*! Helper class. Allocate it in your code block as follows: 0192 <code> 0193 KexiUtils::WaitCursor wait; 0194 </code> 0195 .. and wait cursor will be visible (with one second delay) until you're in this block, without 0196 a need to call removeWaitCursor() before exiting the block. 0197 Does nothing if the application has no GUI enabled. (see KApplication::guiEnabled()) */ 0198 class KEXIUTILS_EXPORT WaitCursor 0199 { 0200 public: 0201 //! Wait cursor handler for application 0202 explicit WaitCursor(bool noDelay = false); 0203 0204 //! @overload 0205 //! Wait cursor handler for widget @a widget 0206 explicit WaitCursor(QWidget *widget, bool noDelay = false); 0207 0208 ~WaitCursor(); 0209 private: 0210 QObject *m_handler; 0211 }; 0212 0213 /*! Helper class. Allocate it in your code block as follows: 0214 <code> 0215 KexiUtils::WaitCursorRemover remover; 0216 </code> 0217 .. and the wait cursor will be hidden unless you leave this block, without 0218 a need to call setWaitCursor() before exiting the block. After leaving the codee block, 0219 the cursor will be visible again, if it was visible before creating the WaitCursorRemover object. 0220 Does nothing if the application has no GUI enabled. (see KApplication::guiEnabled()) */ 0221 class KEXIUTILS_EXPORT WaitCursorRemover 0222 { 0223 public: 0224 WaitCursorRemover(); 0225 ~WaitCursorRemover(); 0226 private: 0227 bool m_reactivateCursor; 0228 }; 0229 0230 /*! Creates a modal file dialog which returns the selected url of image filr to open 0231 or an empty string if none was chosen. 0232 Like KFileDialog::getImageOpenUrl(). */ 0233 //! @todo KEXI3 add equivalent of kfiledialog:/// 0234 KEXIUTILS_EXPORT QUrl getOpenImageUrl(QWidget *parent = 0, const QString &caption = QString(), 0235 const QUrl &directory = QUrl()); 0236 0237 /*! Creates a modal file dialog with returns the selected url of image file to save 0238 or an empty string if none was chosen. 0239 Like KFileDialog::getSaveUrl(). */ 0240 //! @todo KEXI3 add equivalent of kfiledialog:/// 0241 KEXIUTILS_EXPORT QUrl getSaveImageUrl(QWidget *parent = 0, const QString &caption = QString(), 0242 const QUrl &directory = QUrl()); 0243 0244 #ifndef KEXI_MOBILE 0245 /** 0246 * This method implements the logic to determine the user's default directory 0247 * to be listed. E.g. the documents directory, home directory or a recently 0248 * used directory. 0249 * 0250 * Use instead of KFileWidget::getStartUrl(const QUrl &startDir, QString &recentDirClass). 0251 * 0252 * @param startDirOrVariable A URL specifying the initial directory, or using the 0253 * @c kfiledialog:/// syntax to specify a last used directory. 0254 * If the string includes a file name, it is ignored. 0255 * Refer to the KFileWidget::KFileWidget() documentation for the @c kfiledialog:/// 0256 * URL syntax. 0257 * @param recentDirClass If the @c kfiledialog:/// syntax is used, this 0258 * will return the string to be passed to KRecentDirs::dir() and 0259 * KRecentDirs::add(). Optional file name is supported. 0260 * @param fileName Optional file name that is added to the resulting URL. 0261 * @return The URL that should be listed by default (e.g. by KFileDialog or 0262 * KDirSelectDialog). 0263 * 0264 * @see KFileWidget::KFileWidget() 0265 * @since 3.1.0 0266 * @todo Make it independent of KIOFileWidgets 0267 */ 0268 KEXIUTILS_EXPORT QUrl getStartUrl(const QUrl &startDirOrVariable, QString *recentDirClass, 0269 const QString &fileName = QString()); 0270 0271 /** 0272 * Associates @p directory with @p fileClass 0273 * 0274 * Use instead of KRecentDirs::add() 0275 * 0276 * @since 3.1.0 0277 * @todo Make it independent of KIOFileWidgets 0278 */ 0279 KEXIUTILS_EXPORT void addRecentDir(const QString &fileClass, const QString &directory); 0280 #endif // !KEXI_MOBILE 0281 0282 /*! Displays a "The file %1 already exists. Do you want to overwrite it?" Yes/No message box. 0283 @a parent is used as a parent of the message box. 0284 @return @c true if @a filePath file does not exist or user has agreed to overwrite it; 0285 returns @c false if user does not agree to overwrite it. */ 0286 KEXIUTILS_EXPORT bool askForFileOverwriting(const QString& filePath, QWidget *parent = nullptr); 0287 0288 /*! A global setting for minimal readable font. 0289 This can be used in dockers, rulers and other places where space is at a premium. 0290 @see QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont). 0291 @todo Add integration with KDE platform theme (how to detect it?); for now we don't assume it's installed 0292 */ 0293 KEXIUTILS_EXPORT QFont smallestReadableFont(); 0294 0295 /*! \return a color being a result of blending \a c1 with \a c2 with \a factor1 0296 and \a factor1 factors: (c1*factor1+c2*factor2)/(factor1+factor2). */ 0297 KEXIUTILS_EXPORT QColor blendedColors(const QColor& c1, const QColor& c2, int factor1 = 1, int factor2 = 1); 0298 0299 /*! \return a contrast color for a color \a c: 0300 If \a c is light color, darker color created using c.dark(200) is returned; 0301 otherwise lighter color created using c.light(200) is returned. */ 0302 KEXIUTILS_EXPORT QColor contrastColor(const QColor& c); 0303 0304 /*! \return a lighter color for a color \a c and a factor \a factor. 0305 For colors like Qt::red or Qt::green where hue and saturation are near to 255, 0306 hue is decreased so the result will be more bleached. 0307 For black color the result is dark gray rather than black. */ 0308 KEXIUTILS_EXPORT QColor bleachedColor(const QColor& c, int factor); 0309 0310 /*! \return icon set computed as a result of colorizing \a icon pixmap with \a role 0311 color of \a palette palette. This function is useful for displaying monochromed icons 0312 on the list view or table view header, to avoid bloat, but still have the color compatible 0313 with accessibility settings. */ 0314 KEXIUTILS_EXPORT QIcon colorizeIconToTextColor(const QPixmap& icon, const QPalette& palette, 0315 QPalette::ColorRole role = QPalette::ButtonText); 0316 0317 /*! Replaces colors in pixmap @a original using @a color color. Used for coloring bitmaps 0318 that have to reflect the foreground color. */ 0319 KEXIUTILS_EXPORT void replaceColors(QPixmap* original, const QColor& color); 0320 0321 /*! Replaces colors in image @a original using @a color color. Used for coloring bitmaps 0322 that have to reflect the foreground color. */ 0323 KEXIUTILS_EXPORT void replaceColors(QImage* original, const QColor& color); 0324 0325 /*! @return true if curent color scheme is light. 0326 Lightness of window background is checked to measure this. */ 0327 KEXIUTILS_EXPORT bool isLightColorScheme(); 0328 0329 /*! @return alpha value for dimmed color (150). */ 0330 KEXIUTILS_EXPORT int dimmedAlpha(); 0331 0332 /*! @return palette @a pal with dimmed color @a role. @see dimmedAlpha() */ 0333 KEXIUTILS_EXPORT QPalette paletteWithDimmedColor(const QPalette &pal, 0334 QPalette::ColorGroup group, 0335 QPalette::ColorRole role); 0336 0337 /*! @overload paletteWithDimmedColor(const QPalette &, QPalette::ColorGroup, QPalette::ColorRole) */ 0338 KEXIUTILS_EXPORT QPalette paletteWithDimmedColor(const QPalette &pal, 0339 QPalette::ColorRole role); 0340 0341 /*! @return palette altered for indicating "read only" flag. */ 0342 KEXIUTILS_EXPORT QPalette paletteForReadOnly(const QPalette &palette); 0343 0344 /*! Convenience function that sets background color @a color for widget @a widget. 0345 "autoFillBackground" property is set to true for the widget. 0346 Background color role is obtained from QWidget::backgroundRole(). */ 0347 KEXIUTILS_EXPORT void setBackgroundColor(QWidget *widget, const QColor &color); 0348 0349 /*! \return empty (fully transparent) pixmap that can be used as a place for icon of size \a iconGroup */ 0350 KEXIUTILS_EXPORT QPixmap emptyIcon(KIconLoader::Group iconGroup); 0351 0352 #ifdef KEXI_DEBUG_GUI 0353 //! Creates debug window for convenient debugging output 0354 KEXIUTILS_EXPORT QWidget *createDebugWindow(QWidget *parent); 0355 0356 //! Connects push button action to \a receiver and its \a slot. This allows to execute debug-related actions 0357 //! using buttons displayed in the debug window. 0358 KEXIUTILS_EXPORT void connectPushButtonActionForDebugWindow(const char* actionName, 0359 const QObject *receiver, const char* slot); 0360 #endif 0361 0362 //! Sets focus for widget \a widget with reason \a reason. 0363 KEXIUTILS_EXPORT void setFocusWithReason(QWidget* widget, Qt::FocusReason reason); 0364 0365 //! Unsets focus for widget \a widget with reason \a reason. 0366 KEXIUTILS_EXPORT void unsetFocusWithReason(QWidget* widget, Qt::FocusReason reason); 0367 0368 //! If the application's layout direction, swaps left and right margins. 0369 //! @see QGuiApplication::isRightToLeft() 0370 void adjustIfRtl(QMargins *margins); 0371 0372 //! Draws pixmap @a pixmap on painter @a p using predefined parameters. 0373 //! Used in KexiDBImageBox and KexiBlobTableEdit. 0374 KEXIUTILS_EXPORT void drawPixmap(QPainter* p, const QMargins& margins, const QRect& rect, 0375 const QPixmap& pixmap, Qt::Alignment alignment, 0376 bool scaledContents, bool keepAspectRatio, 0377 Qt::TransformationMode transformMode = Qt::FastTransformation); 0378 0379 //! Scales pixmap @a pixmap on painter @a p using predefined parameters. 0380 //! Used in KexiDBImageBox and KexiBlobTableEdit. 0381 KEXIUTILS_EXPORT QPixmap scaledPixmap(const QMargins& margins, const QRect& rect, 0382 const QPixmap& pixmap, QPoint* pos, Qt::Alignment alignment, 0383 bool scaledContents, bool keepAspectRatio, 0384 Qt::TransformationMode transformMode = Qt::FastTransformation); 0385 0386 //! This function should be used instead of QPixmap::loadFromData(). 0387 /** Loads a pixmap from @a data into @a pixmap. First tries to detect format, on failure 0388 * tries to load most common formats: png, jpg, bmp, tif. Then tries to load any of format 0389 * returned by QImageReader::supportedImageFormats(). 0390 * If @a format is provided, only this format is tried. 0391 * @return on success. 0392 * @note This function exists because QPixmap::loadFromData() not always works when there 0393 * are broken image format plugins installed (as it was the case with KRA plugin). 0394 * @todo Idea: Support while/black list of supported image formats. It would be useful 0395 * for security reasons because files can be loaded from remote locations. 0396 * @todo For the black/white list an enum describing local/remote data source is needed. */ 0397 KEXIUTILS_EXPORT bool loadPixmapFromData(QPixmap *pixmap, const QByteArray &data, const char *format = nullptr); 0398 0399 //! A helper for automatic deleting of contents of containers. 0400 template <typename Container> 0401 class ContainerDeleter 0402 { 0403 public: 0404 explicit ContainerDeleter(Container& container) : m_container(container) {} 0405 ~ContainerDeleter() { 0406 clear(); 0407 } 0408 void clear() { 0409 qDeleteAll(m_container); m_container.clear(); 0410 } 0411 private: 0412 Container& m_container; 0413 }; 0414 0415 /*! A modified QFrame which sets up sunken styled panel frame style depending 0416 on the current widget style. The widget also reacts on style changes. */ 0417 class KEXIUTILS_EXPORT KTextEditorFrame : public QFrame 0418 { 0419 Q_OBJECT 0420 public: 0421 explicit KTextEditorFrame(QWidget * parent = 0, Qt::WindowFlags f = 0); 0422 protected: 0423 virtual void changeEvent(QEvent *event) override; 0424 }; 0425 0426 /** 0427 * Returns the number of pixels that should be used between a 0428 * dialog edge and the outermost widget(s) according to the KDE standard. 0429 * 0430 * Copied from QDialog. 0431 * 0432 * @deprecated Use the style's pixelMetric() function to query individual margins. 0433 * Different platforms may use different values for the four margins. 0434 */ 0435 KEXIUTILS_EXPORT int marginHint(); 0436 0437 /** 0438 * Returns the number of pixels that should be used between 0439 * widgets inside a dialog according to the KDE standard. 0440 * 0441 * Copied from QDialog. 0442 * 0443 * @deprecated Use the style's layoutSpacing() function to query individual spacings. 0444 * Different platforms may use different values depending on widget types and pairs. 0445 */ 0446 KEXIUTILS_EXPORT int spacingHint(); 0447 0448 /*! Sets KexiUtils::marginHint() margins and KexiUtils::spacingHint() spacing 0449 for the layout @a layout. */ 0450 KEXIUTILS_EXPORT void setStandardMarginsAndSpacing(QLayout *layout); 0451 0452 /*! Sets the same @a value for layout @a layout margins. */ 0453 KEXIUTILS_EXPORT void setMargins(QLayout *layout, int value); 0454 0455 //! sometimes we leave a space in the form of empty QFrame and want to insert here 0456 //! a widget that must be instantiated by hand. 0457 //! This macro inserts a widget \a what into a frame \a where. 0458 #define GLUE_WIDGET(what, where) \ 0459 { QVBoxLayout *lyr = new QVBoxLayout(where); \ 0460 lyr->addWidget(what); } 0461 0462 //! A tool for setting temporary value for boolean variable. 0463 /*! After destruction of the instance, the variable is set back 0464 to the original value. This class is useful in recursion guards. 0465 To use it, declare class atrribute of type bool and block it, e.g.: 0466 @code 0467 bool m_myNonRecursiveFunctionEnabled; 0468 // ... set m_myNonRecursiveFunctionEnabled initially to true 0469 void myNonRecursiveFunctionEnabled() { 0470 if (!m_myNonRecursiveFunctionEnabled) 0471 return; 0472 KexiUtils::BoolBlocker guard(&m_myNonRecursiveFunctionEnabled, false); 0473 // function's body guarded against recursion... 0474 } 0475 @endcode 0476 */ 0477 class KEXIUTILS_EXPORT BoolBlocker 0478 { 0479 public: 0480 inline BoolBlocker(bool *var, bool tempValue) 0481 : v(var), origValue(*var) { *var = tempValue; } 0482 inline ~BoolBlocker() { *v = origValue; } 0483 private: 0484 bool *v; 0485 bool origValue; 0486 }; 0487 0488 /*! This helper function install an event filter on @a object and all of its 0489 children, directed to @a filter. */ 0490 KEXIUTILS_EXPORT void installRecursiveEventFilter(QObject *object, QObject *filter); 0491 0492 /*! This helper function removes an event filter installed before 0493 on @a object and all of its children. */ 0494 KEXIUTILS_EXPORT void removeRecursiveEventFilter(QObject *object, QObject *filter); 0495 0496 //! Blocks paint events on specified widget. 0497 /*! Works recursively. Useful when widget should be hidden without changing 0498 geometry it takes. */ 0499 class KEXIUTILS_EXPORT PaintBlocker : public QObject 0500 { 0501 Q_OBJECT 0502 public: 0503 explicit PaintBlocker(QWidget* parent); 0504 void setEnabled(bool set); 0505 bool enabled() const; 0506 protected: 0507 virtual bool eventFilter(QObject* watched, QEvent* event) override; 0508 private: 0509 bool m_enabled; 0510 }; 0511 0512 /*! 0513 * \short Options for opening of hyperlinks 0514 * \sa openHyperLink() 0515 */ 0516 class KEXIUTILS_EXPORT OpenHyperlinkOptions : public QObject 0517 { 0518 Q_OBJECT 0519 0520 public: 0521 0522 /*! 0523 * A tool used for opening hyperlinks 0524 */ 0525 enum HyperlinkTool{ 0526 DefaultHyperlinkTool, /*!< Default tool for a given type of the hyperlink */ 0527 BrowserHyperlinkTool, /*!< Opens hyperlink in a browser */ 0528 MailerHyperlinkTool /*!< Opens hyperlink in a default mailer */ 0529 }; 0530 Q_ENUM(HyperlinkTool) 0531 0532 OpenHyperlinkOptions() : 0533 tool(DefaultHyperlinkTool) 0534 , allowExecutable(false) 0535 , allowRemote(false) 0536 {} 0537 0538 HyperlinkTool tool; 0539 bool allowExecutable; 0540 bool allowRemote; 0541 }; 0542 0543 /*! 0544 * Opens the given \a url using \a options 0545 * It can fail if opening a given link is not possible or allowed. 0546 * @return true on success, cancelled if user has cancelled the opening and false on failure 0547 */ 0548 KEXIUTILS_EXPORT tristate openHyperLink(const QUrl &url, QWidget *parent, 0549 const OpenHyperlinkOptions &options); 0550 0551 //! \return size of combo box arrow according to \a style 0552 /*! Application's style is the default. \see QStyle::SC_ComboBoxArrow */ 0553 KEXIUTILS_EXPORT QSize comboBoxArrowSize(QStyle *style = 0); 0554 0555 //! Adds a dirty ("document modified") flag to @a text according to current locale. 0556 //! It is usually "*" character appended. 0557 KEXIUTILS_EXPORT void addDirtyFlag(QString *text); 0558 0559 //! @return The name of the user's preferred encoding 0560 //! Based on KLocale::encoding() 0561 KEXIUTILS_EXPORT QByteArray encoding(); 0562 0563 //! The desired level of effects on the GUI 0564 enum GraphicEffect { 0565 NoEffects = 0x0000, ///< GUI with no effects at all. 0566 GradientEffects = 0x0001, ///< GUI with only gradients enabled. 0567 SimpleAnimationEffects = 0x0002, ///< GUI with simple animations enabled. 0568 ComplexAnimationEffects = 0x0006 ///< GUI with complex animations enabled. 0569 ///< Note that ComplexAnimationsEffects implies SimpleAnimationEffects. 0570 }; 0571 Q_DECLARE_FLAGS(GraphicEffects, GraphicEffect) 0572 0573 //! @return the desired level of effects on the GUI. 0574 //! @note A copy of KGlobalSettings::graphicEffectsLevel() needed for porting from kdelibs4. 0575 KEXIUTILS_EXPORT GraphicEffects graphicEffectsLevel(); 0576 0577 //! @return the inactive titlebar background color 0578 //! @note A copy of KGlobalSettings::inactiveTitleColor() needed for porting from kdelibs4. 0579 KEXIUTILS_EXPORT QColor inactiveTitleColor(); 0580 0581 //! @return the inactive titlebar text (foreground) color 0582 //! @note A copy of KGlobalSettings::inactiveTextColor() needed for porting from kdelibs4. 0583 KEXIUTILS_EXPORT QColor inactiveTextColor(); 0584 0585 //! @return the active titlebar background color 0586 //! @note A copy of KGlobalSettings::activeTitleColor() needed for porting from kdelibs4. 0587 KEXIUTILS_EXPORT QColor activeTitleColor(); 0588 0589 //! @return the active titlebar text (foreground) color 0590 //! @note A copy of KGlobalSettings::activeTextColor() needed for porting from kdelibs4. 0591 KEXIUTILS_EXPORT QColor activeTextColor(); 0592 0593 //! @return Paper White color, see https://techbase.kde.org/Projects/Usability/HIG/Color 0594 inline QColor paperWhite() { return QColor(0xfcfcfc); } 0595 0596 //! @return Charcoal Grey color, see https://techbase.kde.org/Projects/Usability/HIG/Color 0597 inline QColor charcoalGrey() { return QColor(0x31363b); } 0598 0599 //! @return Shade Black color, see https://techbase.kde.org/Projects/Usability/HIG/Color 0600 inline QColor shadeBlack() { return QColor(0x232629); } 0601 0602 /*! @return @c true if whether the app runs in a single click mode (the default). 0603 @c false if returned if the app runs in double click mode. 0604 The flag is checked in two stages. 0605 0606 Stage 1. Application config file's "SingleClickOpensItem" value in "MainWindow" group is checked. 0607 If it exists, the value is returned. 0608 On Windows if it does not exist, @c true is returned, on other systems Stage 2 checking is performed. 0609 0610 Stage 2. For Qt < 5.5 this information is taken from @a widget widget's style. If there is no widget 0611 specified, QApplication::style() is used. 0612 For Qt >= 5.5 the result is equal to QApplication::styleHints()->singleClickActivation() 0613 and @a widget is ignored. 0614 @note This is a replacement for bool KGlobalSettings::singleClick(). 0615 */ 0616 KEXIUTILS_EXPORT bool activateItemsOnSingleClick(QWidget *widget = 0); 0617 0618 /** 0619 * Detects name of desktop session based on environment variables. 0620 * 0621 * Possible value are like GNOME, XFCE, KDE. They are always uppercase. Following environment 0622 * variables are used: XDG_SESSION_DESKTOP. XDG_CURRENT_DESKTOP, DESKTOP_SESSION, KDE_FULL_SESSION, 0623 * GNOME_DESKTOP_SESSION_ID. 0624 * 0625 * @return empty string if no desktop session was detected or sessions are not supported for the 0626 * running OS (Windows, macOS, non-desktop OS). 0627 * 0628 * @note use QApplication::styleHints() if possible. 0629 */ 0630 KEXIUTILS_EXPORT QByteArray detectedDesktopSession(); 0631 0632 /** 0633 * @return true is this is a KDE / Plasma desktop session 0634 * 0635 * Detection is based on detectedDesktopSession(). 0636 */ 0637 KEXIUTILS_EXPORT bool isKDEDesktopSession(); 0638 0639 /** 0640 * @brief Returns @c true if native operating system's dialogs should be used 0641 * 0642 * Returns @c false if Qt's standard dialogs should be used instead of the operating system native 0643 * dialogs. Can be used with QColorDialog, QFileDialog and QFontDialog. 0644 * 0645 * Depends on the curent desktop in use: 0646 * - on Unix (other than macOS) returns @c true if isKDEDesktopSession() is @c true or if desktop 0647 * session can't be detected, @c false for other desktops 0648 * - @c true for all other operating systems, i.e. for MS Windows, macOS, etc. 0649 * 0650 * @todo Share this code with KReport and Kexi 0651 */ 0652 KEXIUTILS_EXPORT bool shouldUseNativeDialogs(); 0653 0654 /** 0655 * @enum CaptionFlag 0656 * Used to specify how to construct a window caption 0657 * 0658 * @var AppNameCaption 0659 * Indicates that the method shall include 0660 * the application name when making the caption string. 0661 * @var ModifiedCaption 0662 * Causes a 'modified' sign will be included in the 0663 * returned string. This is useful when indicating that a file is 0664 * modified, i.e., it contains data that has not been saved. 0665 */ 0666 enum CaptionFlag { 0667 NoCaptionFlags = 0, 0668 AppNameCaption = 1, 0669 ModifiedCaption = 2 0670 }; 0671 Q_DECLARE_FLAGS(CaptionFlags, CaptionFlag) 0672 0673 /** 0674 * Builds a caption that contains the application name along with the 0675 * userCaption using a standard layout. 0676 * 0677 * To make a compliant caption for your window, simply do: 0678 * @p setWindowTitle(makeStandardCaption(yourCaption)); 0679 * 0680 * To ensure that the caption is appropriate to the desktop in which the 0681 * application is running, pass in a pointer to the window the caption will 0682 * be applied to. 0683 * 0684 * @param userCaption The caption string you want to display in the 0685 * window caption area. Do not include the application name! 0686 * @param flags 0687 * @return the created caption 0688 * Based on KDialog::makeStandardCaption() 0689 */ 0690 KEXIUTILS_EXPORT QString makeStandardCaption(const QString &userCaption, 0691 CaptionFlags flags = AppNameCaption); 0692 0693 /** 0694 * Return rich text for @a string. Equivalent of KLocalizedString::toString(Kuit::RichText) 0695 * but \<html\> and \</html\> is removed so the result can be used as %* argument in other string. 0696 */ 0697 KEXIUTILS_EXPORT QString localizedStringToHtmlSubstring(const KLocalizedString& string); 0698 0699 /** 0700 * Return rich text for concatenated sentences or paragraphs. 0701 * 0702 * Any of the parameters can be empty but at least first should be non-empty. 0703 */ 0704 KEXIUTILS_EXPORT QString localizedSentencesToHtml( 0705 const KLocalizedString &part1, const KLocalizedString &part2 = KLocalizedString(), 0706 const KLocalizedString &part3 = KLocalizedString(), 0707 const KLocalizedString &part4 = KLocalizedString(), 0708 const KLocalizedString &part5 = KLocalizedString(), 0709 const KLocalizedString &part6 = KLocalizedString()); 0710 0711 /** 0712 * @return @c true if text cursor is at the end of the line edit @a lineEdit. 0713 * If the @a lineEdit edit has input mask, cursor is at the end if it's at position 0714 * lineEdit->displayText().length() - 1 or further. If the @a lineEdit has no input mask, cursor 0715 * is at the end if it's at position lineEdit->text().length() or further. 0716 */ 0717 KEXIUTILS_EXPORT bool cursorAtEnd(const QLineEdit *lineEdit); 0718 0719 } //namespace KexiUtils 0720 0721 /** 0722 * Writes the DOM tree to the stream and returns a reference to the stream. 0723 */ 0724 KEXIUTILS_EXPORT QDebug operator<<(QDebug dbg, const QDomNode &node); 0725 0726 #endif //KEXIUTILS_UTILS_H