File indexing completed on 2024-06-09 04:25:57

0001 /*
0002  * klangbutton.h - Button with language selection drop down menu.
0003  *                 Derived from the KLangCombo class by Hans Petter Bieker.
0004  *
0005  * SPDX-FileCopyrightText: 1999-2003 Hans Petter Bieker <bieker@kde.org>
0006  * SPDX-FileCopyrightText: 2001 Martijn Klingens <klingens@kde.org>
0007  * SPDX-FileCopyrightText: 2007 David Jarvie <software@astrojar.org.uk>
0008  *
0009  *  SPDX-License-Identifier: LGPL-2.0-or-later
0010  */
0011 
0012 #ifndef KLANGUAGEBUTTON_H
0013 #define KLANGUAGEBUTTON_H
0014 
0015 #include "kritawidgetutils_export.h"
0016 #include <QWidget>
0017 
0018 class QAction;
0019 class KLanguageButtonPrivate;
0020 
0021 /**
0022  * KLanguageButton is a pushbutton which allows a language to be selected from
0023  * a popup list.
0024  *
0025  * Languages are identified by their ISO 639-1 codes, e.g. en, pt_BR.
0026  *
0027  * \image html klanguagebutton.png "KDE Language Selection Widget"
0028  *
0029  * @author Hans Petter Bieker <bieker@kde.org>, Martijn Klingens <klingens@kde.org>,
0030  *         David Jarvie <software@astrojar.org.uk>
0031  */
0032 class KRITAWIDGETUTILS_EXPORT KLanguageButton : public QWidget
0033 {
0034     Q_OBJECT
0035 
0036 public:
0037     /**
0038      * Constructs a button whose text is determined by the current language
0039      * in the popup list.
0040      *
0041      * @param parent the parent of the button
0042      */
0043     explicit KLanguageButton(QWidget *parent = 0);
0044 
0045     /**
0046      * Constructs a button with static text.
0047      *
0048      * @param text the text of the button
0049      * @param parent the parent of the button
0050      */
0051     explicit KLanguageButton(const QString &text, QWidget *parent = 0);
0052 
0053     /**
0054      * Deconstructor
0055      */
0056     ~KLanguageButton() override;
0057 
0058     /**
0059      * Sets the locale to display language names. By default, QLocale::system().name() is used.
0060      *
0061      * @param locale locale to use
0062      */
0063     void setLocale(const QString &locale);
0064 
0065     /**
0066      * Sets a static button text.
0067      *
0068      * @param text button text
0069      */
0070     void setText(const QString &text);
0071 
0072     /**
0073      * Specifies whether language codes should be shown alongside language names
0074      * in the popup. Calling this method does not affect any previously
0075      * inserted language texts, so it should normally be called before
0076      * populating the list.
0077      *
0078      * @param show true to show codes, false to hide codes
0079      */
0080     void showLanguageCodes(bool show);
0081 
0082     /**
0083      * Load all known languages into the popup list.
0084      * The current language in the list is set to the default language for the
0085      * current locale (as modified by setLocale()).
0086      */
0087     void loadAllLanguages();
0088 
0089     /**
0090      * Inserts a language into the combo box.
0091      * Normally the display name of the language is obtained automatically, but
0092      * if either the language code does not exist, or there are special display
0093      * requirements, the name of the language can be specified in @p name.
0094      *
0095      * @param languageCode the code for the language
0096      * @param name language name. If empty, the name is obtained automatically.
0097      * @param index the insertion position, or -1 to insert in alphabetical order
0098      */
0099     void insertLanguage(const QString &languageCode, const QString &name = QString(), int index = -1);
0100 
0101     /**
0102      * Inserts a separator item into the combo box. A negative index will append the item.
0103      *
0104      * @param index the insertion position
0105      */
0106     void insertSeparator(int index = -1);
0107 
0108     /**
0109      * Returns the number of items in the combo box.
0110      */
0111     int count() const;
0112 
0113     /**
0114      * Removes all combobox items.
0115      */
0116     void clear();
0117 
0118     /**
0119      * Returns the language code of the combobox's current item.
0120      *
0121      * @return the current item's language code
0122      */
0123     QString current() const;
0124 
0125     /**
0126      * Checks whether the specified language is in the popup list.
0127      *
0128      * @param languageCode the language's code
0129      * @return true if in the list
0130      */
0131     bool contains(const QString &languageCode) const;
0132 
0133     /**
0134      * Sets a given language to be the current item.
0135      *
0136      * @param languageCode the language's code
0137      */
0138     void setCurrentItem(const QString &languageCode);
0139 
0140 Q_SIGNALS:
0141     /**
0142      * This signal is emitted when a new item is activated.
0143      *
0144      * @param languageCode code of the activated language
0145      */
0146     void activated(const QString &languageCode);
0147     /**
0148      * This signal is emitted when a new item is highlighted.
0149      *
0150      * @param languageCode code of the highlighted language
0151      */
0152     void highlighted(const QString &languageCode);
0153 
0154 private Q_SLOTS:
0155     void slotTriggered(QAction *);
0156     void slotHovered(QAction *);
0157 
0158 private:
0159     KLanguageButtonPrivate *const d;
0160 };
0161 
0162 #endif