1 /* 2 SPDX-FileCopyrightText: 2009-2011 Graeme Gott <graeme@gottcode.org> 3 4 SPDX-License-Identifier: GPL-3.0-or-later 5 */ 6 7 #ifndef TANGLET_WORD_TREE_H 8 #define TANGLET_WORD_TREE_H 9 10 class Trie; 11 12 #include <QTreeWidget> 13 14 /** 15 * @brief The WordTree class contains a list of words to display to the player. 16 */ 17 class WordTree : public QTreeWidget 18 { 19 Q_OBJECT 20 21 public: 22 /** 23 * Constructs a word tree instance. 24 * @param parent the widget that owns the word counts 25 */ 26 explicit WordTree(QWidget* parent = nullptr); 27 28 /** 29 * Adds a word to the list. 30 * @param word word to add 31 * @return item in list representing @p word 32 */ 33 QTreeWidgetItem* addWord(const QString& word); 34 35 /** 36 * Removes all words from list. 37 */ 38 void removeAll(); 39 40 /** 41 * Set the location to look up word definitions. 42 * @param url location to look up words 43 */ 44 void setDictionary(const QString& url); 45 46 /** 47 * Sets if the word list is in Hebrew. This is important because vowels at the end of a word need 48 * to be replaced to make it correct for displaying to the player or looking up in the dictionary. 49 * @param hebrew whether the word list is in Hebrew 50 */ 51 void setHebrew(bool hebrew); 52 53 /** 54 * Set the optimized word list to look up spellings of the words. 55 * @param trie optimized word list 56 */ 57 void setTrie(const Trie* trie); 58 59 protected: 60 /** 61 * Override to detect player mousing off of word tree. This allows the word tree to clear the 62 * icon on the active word. 63 * @param event details of leave event 64 */ 65 void leaveEvent(QEvent* event) override; 66 67 /** 68 * Override to detect player mousing over word tree. This is to detectwhen the mouse has entered 69 * an item. 70 * @param event details of mouse move event 71 */ 72 void mouseMoveEvent(QMouseEvent* event) override; 73 74 /** 75 * Override to detect player scrolling word tree. This is to detect if a new item is now active. 76 * @param event details of wheel event 77 */ 78 void wheelEvent(QWheelEvent* event) override; 79 80 private slots: 81 /** 82 * Handles player clicking on an item. If they click on the second column, it opens a web 83 * browser at the dictionary definition of the clicked item. 84 * @param item which item was clicked on 85 * @param column which column was clicked on 86 */ 87 void onItemClicked(QTreeWidgetItem* item, int column); 88 89 private: 90 /** 91 * Shows an icon on the active item to inform player they can look up a word. 92 * @param item which item is now active, or @c nullptr if none are 93 */ 94 void enterItem(QTreeWidgetItem* item); 95 96 private: 97 QTreeWidgetItem* m_active_item; /**< currently hovered item */ 98 QByteArray m_url; /**< location to look up word definitions */ 99 bool m_hebrew; /**< is this a Hebrew word list */ 100 const Trie* m_trie; /**< word list to find all spellings of a word */ 101 }; 102 103 #endif // TANGLET_WORD_TREE_H 104