File indexing completed on 2024-05-19 05:01:24
0001 /* 0002 This file is part of the KDE project. 0003 0004 SPDX-FileCopyrightText: 2020 Stefano Crocco <posta@stefanocrocco.it> 0005 0006 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0007 */ 0008 0009 #ifndef WEBFIELDSDATAVIEW_H 0010 #define WEBFIELDSDATAVIEW_H 0011 0012 #include <QTableView> 0013 #include <QStandardItemModel> 0014 #include <QStyledItemDelegate> 0015 0016 #include "webenginewallet.h" 0017 0018 /** 0019 * @brief Helper class to display passwords in WebFieldsDataWidget 0020 */ 0021 class WebFieldsDataViewPasswordDelegate : public QStyledItemDelegate { 0022 Q_OBJECT 0023 0024 public: 0025 0026 /** 0027 * @brief Default constructor 0028 * 0029 * @param parent the parent object 0030 */ 0031 explicit WebFieldsDataViewPasswordDelegate(QObject *parent = nullptr); 0032 0033 ///@brief Destructor 0034 ~WebFieldsDataViewPasswordDelegate() override{} 0035 0036 /** 0037 * @brief Override of <a href="https://doc.qt.io/qt-5/qstyleditemdelegate.html#paint">QStyledItemDelegate::paint()</a> 0038 * 0039 * It displays the text replacing each character with a special character according to QStyle::StyleHint::SH_LineEdit_PasswordCharacter, 0040 * but only if the data contained in the index represents a password, according to WebEngineCustomizeCacheableFieldsDlg::PasswordRole 0041 * 0042 * @param painter the painter 0043 * @param option the option 0044 * @param index the index to paint 0045 */ 0046 void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const override; 0047 0048 /** 0049 * @brief Override of <a href="https://doc.qt.io/qt-5/qstyleditemdelegate.html#sizeHint">QStyledItemDelegate::sizeHint()</a> 0050 * 0051 * @param option the option 0052 * @param index the index whose size hint should be returned 0053 * @return the size hint for @p index 0054 */ 0055 QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const override; 0056 0057 private: 0058 0059 /** 0060 * @brief Whether the given index represents a password 0061 * 0062 * This function uses the value of the WebEngineCustomizeCacheableFieldsDlg::PasswordRole to determine if @p idx represents or not a password 0063 * @param idx the index 0064 * @return @b true if @p idx represents and index and @b false otherwise 0065 */ 0066 static bool isPassword(const QModelIndex &idx); 0067 0068 /** 0069 * @brief The string to display in place of a password 0070 * 0071 * It returns a string with the same length as the Qt::DisplayRole of @p index all made by the character returned by <tt>QStyle::styleHint</tt> 0072 * called with argument QStyle::StyleHint::SH_LineEdit_PasswordCharacter. 0073 * 0074 * @param option the option to pass to QStyle::styleHint 0075 * @param index the index containing the password 0076 * @return a string suitable to be displayed instead of the display role of @p index 0077 */ 0078 static QString passwordReplacement(const QStyleOptionViewItem &option, const QModelIndex &index); 0079 }; 0080 0081 /** 0082 * @brief Model which contains informations about web forms 0083 * 0084 * This model has several columns, which correspond to the values of the #Columns enum. 0085 * 0086 * The items in this model can be checkable or not, depending on how it's created see (WebFieldsDataModel()). This behaviour can't be 0087 * changed after creation. 0088 * 0089 * To fill the model, call setForms() with the forms to display 0090 */ 0091 class WebFieldsDataModel : public QStandardItemModel 0092 { 0093 Q_OBJECT 0094 0095 public: 0096 /** 0097 * @brief Constructor 0098 * 0099 * @param checkableItems whether the items in the model should be checkable or not 0100 * @param parent the parent object 0101 */ 0102 WebFieldsDataModel(bool checkableItems, QObject *parent = nullptr); 0103 0104 /** 0105 * @brief Destructor 0106 */ 0107 ~WebFieldsDataModel() override; 0108 0109 ///@brief enum to reference the various columns of the table 0110 enum Columns { 0111 ChosenCol = 0, ///< The column where the user can select the fields 0112 LabelCol, ///< The field name column 0113 ValueCol, ///< The field value column 0114 InternalNameCol, ///< The internal name column 0115 TypeCol, ///< The field type column 0116 IdCol, ///< The ID field type column 0117 DetailsCol ///< The field details column 0118 }; 0119 0120 ///@brief Special roles used by the model 0121 enum Roles { 0122 PasswordRole = Qt::UserRole+1, ///< Role which tells whether an index corresponds to a password or not 0123 FormRole, ///< Role which contains the index of the form a field belongs to inside m_fields 0124 FieldRole ///< Role which contains the index of the field in the form it belongs to 0125 }; 0126 0127 /** 0128 * @brief Fills the model with form data 0129 * 0130 * @note This will remove all previous content 0131 * 0132 * @param forms the forms to display in the model 0133 */ 0134 void setForms(const WebEngineWallet::WebFormList &forms); 0135 0136 /** 0137 * @brief The list of fields checked by the user 0138 * 0139 * @return a list of the fields the user has checked 0140 */ 0141 WebEngineWallet::WebFormList checkedFields() const; 0142 0143 /** 0144 * @brief Whether the items in the model can be checked or not 0145 * 0146 * @return @b true if the items in the model can be checked and @b false otherwise 0147 */ 0148 bool areItemsCheckable() const {return m_checkableItems;} 0149 0150 /** 0151 * @brief Remove the contents from the model 0152 */ 0153 void clearForms(); 0154 0155 private: 0156 0157 /** 0158 * @brief Creates a row for the given field 0159 * 0160 * @param field the field to create the row for 0161 * @param formIndex the index of the form the field belongs to in #m_forms 0162 * @param fieldIndex the index of the field in the parent form's list of fields 0163 * @return The row corresponding to the field 0164 */ 0165 QList<QStandardItem*> createRowForField(const WebEngineWallet::WebForm::WebField &field, int formIndex, int fieldIndex); 0166 0167 /** 0168 * @brief A string to use as tool tip for the given field 0169 * 0170 * The tool tip contains information about all the field characteristics which aren't shown by default (internal name, id, type, read-only, enabled, autocomplete enabled). 0171 * @param field the field 0172 * @return a string with the tool tip 0173 */ 0174 static QString toolTipForField(const WebEngineWallet::WebForm::WebField &field); 0175 0176 private: 0177 ///@brief Whether the items in the model should have a check box 0178 bool m_checkableItems; 0179 0180 ///@brief the forms displayed in the model 0181 WebEngineWallet::WebFormList m_forms; 0182 }; 0183 0184 /** 0185 * @brief View to display informations about web fields 0186 * 0187 * This view is meant to be used with a WebFieldsDataModel, but this isn't a requirement. See setModel() if you use another model. 0188 * 0189 * This widget can be configured to: 0190 * * display only the label and value column or all columns provided by WebFieldsDataModel 0191 * * display or not tool tips for items 0192 * * display the password in clear text or replace each character with a dot 0193 */ 0194 class WebFieldsDataView : public QTableView 0195 { 0196 Q_OBJECT 0197 0198 public: 0199 /** 0200 * @brief Constructor 0201 * 0202 * @param parent the parent widget 0203 */ 0204 WebFieldsDataView( QWidget *parent = nullptr); 0205 0206 ///@brief Destructor 0207 ~WebFieldsDataView() override; 0208 0209 /** 0210 * @brief Override of `QTableView::setModel()` 0211 * 0212 * In most cases, @p model should be an instance of WebFieldsDataModel. If you want to use another model, you should know that this class assumes that the model 0213 * provides a column for each value of the WebFieldsDataModel::Columns enum and in that order. Besides, if you don't want the items to be checkable, you'll need 0214 * to hide the first column by hand (if @p model is a WebFieldsDataModel, this is done automatically based on WebFieldsDataModel::areItemsCheckable()). 0215 * 0216 * @param model the model 0217 */ 0218 void setModel(QAbstractItemModel *model) override; 0219 0220 /** 0221 * @brief Override of `QTableView::sizeHint()` 0222 * 0223 * @return a size hint whose height is exactly that needed to display all the rows and the header view (unless that's hidden) 0224 */ 0225 QSize sizeHint() const override; 0226 0227 public slots: 0228 /** 0229 * @brief Slot which toggles displaying of passwords on or off 0230 * 0231 * @param show whether passwords should be displayed (@b true) or obfuscated (@b false) using WebEnginePartPasswordDelegate 0232 * 0233 * @see WebEnginePartPasswordDelegate 0234 */ 0235 void togglePasswords(bool show); 0236 0237 /** 0238 * @brief Shows or hides the columns displaying details about fields 0239 * 0240 * When details are hidden, the only visible information about fields are name and value; when details are visible, instead, also the type and 0241 * characteristics such as being read-only, enabled and having autocomplete on or off are shown 0242 * 0243 * @note Unlike setDetailsVisible(), this function does nothing if @p show is @b true and details are already visible or @p show is @b false 0244 * and details are already hidden 0245 * 0246 * @param show whether to show or hide details about fields 0247 */ 0248 void toggleDetails(bool show); 0249 0250 /** 0251 * @brief Enables or disables tool tips for items 0252 * 0253 * @param show @b true if the tool tips should be shown and @b false otherwise 0254 */ 0255 void toggleToolTips(bool show); 0256 0257 /** 0258 * @brief Shows or hides the columns displaying details about fields 0259 * 0260 * When details are hidden, the only visible information about fields are name and value; when details are visible, instead, also the type and 0261 * characteristics such as being read-only, enabled and having autocomplete on or off are shown 0262 * 0263 * @note Unlike toggleDetails(), this function doesn't check the current details visibility state 0264 * 0265 * @param visible whether to show or hide details about fields 0266 */ 0267 void setDetailsVisible(bool visible); 0268 protected: 0269 0270 /** 0271 * @brief Override of `QTableView::viewportEvent()` 0272 * 0273 * It prevents displaying the tool tips if #m_showToolTips is false 0274 * 0275 * @param e the event 0276 * @return As `QTableView::viewportEvent()` 0277 */ 0278 bool viewportEvent(QEvent * e) override; 0279 0280 private: 0281 0282 ///@brief the delegate to draw passwords 0283 WebFieldsDataViewPasswordDelegate *m_passwordDelegate; 0284 0285 ///@brief Whether passwords should be displayed in clear text or not 0286 bool m_showPasswords; 0287 0288 ///@brief Whether all columns should be shown or only the name and value columns (and the column with the check box if the items are checkable) should be shown 0289 bool m_showDetails; 0290 0291 ///@brief Whether tool tips should be shown 0292 bool m_showToolTips; 0293 }; 0294 0295 #endif // WEBFIELDSDATAVIEW_H 0296 0297