File indexing completed on 2025-02-16 13:11:37
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2005 Joseph Wenninger <jowenn@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef KCHARSELECT_P_H 0009 #define KCHARSELECT_P_H 0010 0011 #include <QAbstractTableModel> 0012 #include <QFont> 0013 #include <QMimeData> 0014 #include <QTableView> 0015 #include <memory> 0016 0017 #include "kcharselectdata_p.h" 0018 0019 class KCharSelectTablePrivate; 0020 0021 /** 0022 * @short Character selection table 0023 * 0024 * A table widget which displays the characters of a font. Internally 0025 * used by KCharSelect. See the KCharSelect documentation for further 0026 * details. 0027 * 0028 * @author Reginald Stadlbauer <reggie@kde.org> 0029 * @author Daniel Laidig <d.laidig@gmx.de> 0030 */ 0031 0032 class KCharSelectTable : public QTableView 0033 { 0034 Q_OBJECT 0035 0036 public: 0037 /** 0038 * Constructor. Using @p _font, draw a table of chars. 0039 * @sa setContents 0040 */ 0041 KCharSelectTable(QWidget *parent, const QFont &_font); 0042 0043 ~KCharSelectTable() override; 0044 0045 void resizeEvent(QResizeEvent *) override; 0046 0047 /** Set the font to be displayed to @p _font . */ 0048 void setFont(const QFont &_font); 0049 0050 /** Set the highlighted character to @p c . */ 0051 void setChar(uint c); 0052 /** Set the contents of the table to @p chars . */ 0053 void setContents(const QVector<uint> &chars); 0054 0055 /** @return Currently highlighted character. */ 0056 uint chr(); 0057 0058 /** 0059 * Returns the currently displayed font. 0060 */ 0061 QFont font() const; 0062 0063 /** 0064 * Returns a list of currently displayed characters. 0065 */ 0066 QVector<uint> displayedChars() const; 0067 0068 /** 0069 * Reimplemented. 0070 */ 0071 void scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible) override; 0072 0073 protected: 0074 /** 0075 * Reimplemented. 0076 */ 0077 void keyPressEvent(QKeyEvent *e) override; 0078 0079 Q_SIGNALS: 0080 /** Emitted to indicate that character @p c is activated (such as by double-clicking it). */ 0081 void activated(uint c); 0082 void focusItemChanged(uint c); 0083 void showCharRequested(uint c); 0084 0085 private: 0086 friend class KCharSelectTablePrivate; 0087 std::unique_ptr<KCharSelectTablePrivate> const d; 0088 0089 Q_DISABLE_COPY(KCharSelectTable) 0090 }; 0091 0092 // NO D-Pointer needed, private internal class, no public API 0093 0094 class KCharSelectItemModel : public QAbstractTableModel 0095 { 0096 Q_OBJECT 0097 public: 0098 KCharSelectItemModel(const QVector<uint> &chars, const QFont &font, QObject *parent) 0099 : QAbstractTableModel(parent) 0100 , m_chars(chars) 0101 , m_font(font) 0102 { 0103 if (!chars.isEmpty()) { 0104 m_columns = chars.count(); 0105 } else { 0106 m_columns = 1; 0107 } 0108 } 0109 0110 enum internalRoles { CharacterRole = Qt::UserRole }; 0111 int rowCount(const QModelIndex &parent = QModelIndex()) const override 0112 { 0113 if (parent.isValid()) { 0114 return 0; 0115 } 0116 if (m_chars.count() % m_columns == 0) { 0117 return m_chars.count() / m_columns; 0118 } else { 0119 return m_chars.count() / m_columns + 1; 0120 } 0121 } 0122 int columnCount(const QModelIndex & = QModelIndex()) const override 0123 { 0124 return m_columns; 0125 } 0126 0127 void setFont(const QFont &font) 0128 { 0129 beginResetModel(); 0130 m_font = font; 0131 endResetModel(); 0132 } 0133 Qt::ItemFlags flags(const QModelIndex &index) const override 0134 { 0135 int pos = m_columns * (index.row()) + index.column(); 0136 if (pos >= m_chars.size() || index.row() < 0 || index.column() < 0) { 0137 return Qt::ItemIsDropEnabled; 0138 } 0139 return (Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsSelectable | Qt::ItemIsEnabled); 0140 } 0141 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; 0142 QMimeData *mimeData(const QModelIndexList &indexes) const override 0143 { 0144 if (indexes.size() != 1) { 0145 return nullptr; 0146 } 0147 QMimeData *mimeData = new QMimeData(); 0148 uint character = data(indexes[0], CharacterRole).toUInt(); 0149 mimeData->setText(QString::fromUcs4(&character, 1)); 0150 return mimeData; 0151 } 0152 Qt::DropActions supportedDropActions() const override 0153 { 0154 return Qt::CopyAction; 0155 } 0156 QStringList mimeTypes() const override 0157 { 0158 return {QStringLiteral("text/plain")}; 0159 } 0160 bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; 0161 0162 void setColumnCount(int columns); 0163 0164 QVector<uint> chars() const 0165 { 0166 return m_chars; 0167 } 0168 0169 private: 0170 QVector<uint> m_chars; 0171 QFont m_font; 0172 int m_columns; 0173 0174 Q_SIGNALS: 0175 void showCharRequested(uint c); 0176 }; 0177 #endif // KCHARSELECT_P_H