| 1 | /* |
| 2 | klangbutton.h - Button with language selection drop down menu. |
| 3 | Derived from the KLangCombo class by Hans Petter Bieker. |
| 4 | |
| 5 | SPDX-FileCopyrightText: 1999-2003 Hans Petter Bieker <bieker@kde.org> |
| 6 | SPDX-FileCopyrightText: 2001 Martijn Klingens <klingens@kde.org> |
| 7 | SPDX-FileCopyrightText: 2007 David Jarvie <djarvie@kde.org> |
| 8 | |
| 9 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 10 | */ |
| 11 | |
| 12 | #ifndef KLANGUAGEBUTTON_H |
| 13 | #define KLANGUAGEBUTTON_H |
| 14 | |
| 15 | #include "kconfigwidgets_export.h" |
| 16 | #include <QWidget> |
| 17 | #include <memory> |
| 18 | |
| 19 | class QAction; |
| 20 | class KLanguageButtonPrivate; |
| 21 | |
| 22 | /*! |
| 23 | * \class KLanguageButton |
| 24 | * \inmodule KConfigWidgets |
| 25 | * |
| 26 | * \brief KLanguageButton is a pushbutton which allows a language to be selected from |
| 27 | * a popup list. |
| 28 | * |
| 29 | * Languages are identified by their ISO 639-1 codes, e.g. en, pt_BR. |
| 30 | * |
| 31 | * \image klanguagebutton.png "KDE Language Selection Widget" |
| 32 | */ |
| 33 | class KCONFIGWIDGETS_EXPORT KLanguageButton : public QWidget |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | |
| 37 | public: |
| 38 | /*! |
| 39 | * Constructs a button whose text is determined by the current language |
| 40 | * in the popup list. |
| 41 | * |
| 42 | * \a parent the parent of the button |
| 43 | */ |
| 44 | explicit KLanguageButton(QWidget *parent = nullptr); |
| 45 | |
| 46 | /*! |
| 47 | * Constructs a button with static text. |
| 48 | * |
| 49 | * \a text the text of the button |
| 50 | * |
| 51 | * \a parent the parent of the button |
| 52 | */ |
| 53 | explicit KLanguageButton(const QString &text, QWidget *parent = nullptr); |
| 54 | |
| 55 | ~KLanguageButton() override; |
| 56 | |
| 57 | /*! |
| 58 | * Sets the locale to display language names. By default, QLocale::system().name() is used. |
| 59 | * |
| 60 | * \a locale locale to use |
| 61 | */ |
| 62 | void setLocale(const QString &locale); |
| 63 | |
| 64 | /*! |
| 65 | * Sets a static button text. |
| 66 | * |
| 67 | * \a text button text |
| 68 | */ |
| 69 | void setText(const QString &text); |
| 70 | |
| 71 | /*! |
| 72 | * Specifies whether language codes should be shown alongside language names |
| 73 | * in the popup. Calling this method does not affect any previously |
| 74 | * inserted language texts, so it should normally be called before |
| 75 | * populating the list. |
| 76 | * |
| 77 | * \a show \c true to show codes, \c false to hide codes |
| 78 | */ |
| 79 | void showLanguageCodes(bool show); |
| 80 | |
| 81 | /*! |
| 82 | * Load all known languages into the popup list. |
| 83 | * |
| 84 | * The current language in the list is set to the default language for the |
| 85 | * current locale (as modified by setLocale()). |
| 86 | */ |
| 87 | void loadAllLanguages(); |
| 88 | |
| 89 | /*! |
| 90 | * Inserts a language into the combo box. |
| 91 | * |
| 92 | * Normally the display name of the language is obtained automatically, but |
| 93 | * if either the language code does not exist, or there are special display |
| 94 | * requirements, the name of the language can be specified in \a name. |
| 95 | * |
| 96 | * \a languageCode the code for the language |
| 97 | * |
| 98 | * \a name language name. If empty, the name is obtained automatically |
| 99 | * |
| 100 | * \a index the insertion position, or -1 to insert in alphabetical order |
| 101 | */ |
| 102 | void insertLanguage(const QString &languageCode, const QString &name = QString(), int index = -1); |
| 103 | |
| 104 | /*! |
| 105 | * Inserts a separator item into the combo box. A negative index will append the item. |
| 106 | * |
| 107 | * \a index the insertion position |
| 108 | */ |
| 109 | void insertSeparator(int index = -1); |
| 110 | |
| 111 | /*! |
| 112 | * Returns the number of items in the combo box. |
| 113 | */ |
| 114 | int count() const; |
| 115 | |
| 116 | /*! |
| 117 | * Removes all combobox items. |
| 118 | */ |
| 119 | void clear(); |
| 120 | |
| 121 | /*! |
| 122 | * Returns the language code of the combobox's current item. |
| 123 | */ |
| 124 | QString current() const; |
| 125 | |
| 126 | /*! |
| 127 | * Checks whether the specified language is in the popup list. |
| 128 | * |
| 129 | * \a languageCode the language's code |
| 130 | */ |
| 131 | bool contains(const QString &languageCode) const; |
| 132 | |
| 133 | /*! |
| 134 | * Sets a given language to be the current item. |
| 135 | * |
| 136 | * \a languageCode the language's code |
| 137 | */ |
| 138 | void setCurrentItem(const QString &languageCode); |
| 139 | |
| 140 | Q_SIGNALS: |
| 141 | /*! |
| 142 | * This signal is emitted when a new item is activated. |
| 143 | * |
| 144 | * \a languageCode code of the activated language |
| 145 | */ |
| 146 | void activated(const QString &languageCode); |
| 147 | /*! |
| 148 | * This signal is emitted when a new item is highlighted. |
| 149 | * |
| 150 | * \a languageCode code of the highlighted language |
| 151 | */ |
| 152 | void highlighted(const QString &languageCode); |
| 153 | |
| 154 | protected: |
| 155 | bool event(QEvent *event) override; |
| 156 | |
| 157 | private Q_SLOTS: |
| 158 | KCONFIGWIDGETS_NO_EXPORT void slotTriggered(QAction *action); |
| 159 | KCONFIGWIDGETS_NO_EXPORT void slotHovered(QAction *action); |
| 160 | |
| 161 | private: |
| 162 | friend class KLanguageButtonPrivate; |
| 163 | std::unique_ptr<KLanguageButtonPrivate> const d; |
| 164 | }; |
| 165 | |
| 166 | #endif |
| 167 | |