1 /********* 2 * 3 * In the name of the Father, and of the Son, and of the Holy Spirit. 4 * 5 * This file is part of BibleTime's source code, http://www.bibletime.info/. 6 * 7 * Copyright 1999-2016 by the BibleTime developers. 8 * The BibleTime source code is licensed under the GNU General Public License version 2.0. 9 * 10 **********/ 11 12 #ifndef BT_MODULECHOOSERHEADERWIDGET 13 #define BT_MODULECHOOSERHEADERWIDGET 14 15 #include <QString> 16 #include <QWidget> 17 #include <QList> 18 19 #include "backend/btmoduletreeitem.h" 20 #include "backend/drivers/cswordmoduleinfo.h" 21 22 class BtTextWindowHeader; 23 class QMenu; 24 class QAction; 25 class QLabel; 26 class QToolButton; 27 class QFrame; 28 29 /** 30 * A widget for choosing a module in a window. Consists of a label and a button. 31 * When user selects a module, 32 * button sends a signal. This widget needs to get a message back after a window 33 * module list has been changed. Only then it will be updated. 34 * See BtTextWindowHeader. 35 */ 36 class BtTextWindowHeaderWidget : public QWidget { 37 Q_OBJECT 38 39 public: 40 /** For internal use to mark the menu items */ 41 enum TypeOfAction {RemoveAction, AddAction, ReplaceAction}; 42 43 /** Filter out modules of wrong type from buttons module list. 44 * See populateMenu() and BTModuleTreeItem. */ 45 struct TypeFilter : public BTModuleTreeItem::Filter { TypeFilterTypeFilter46 TypeFilter(CSwordModuleInfo::ModuleType t) { 47 m_mType = t; 48 } filterTypeFilter49 bool filter(CSwordModuleInfo const & mi) const override 50 { return ((mi.type() == m_mType) && !mi.isLocked()); } 51 CSwordModuleInfo::ModuleType m_mType; 52 }; 53 54 /** 55 * A new empty widget. updateMenu() is needed to update the label, menu items etc. 56 */ 57 BtTextWindowHeaderWidget(BtTextWindowHeader *parent, CSwordModuleInfo::ModuleType mtype); 58 59 public: 60 /** 61 * Called after the window module list has changed. Updates the module name and 62 * the existing menu items but doesn't add or remove them if the menu exists. 63 * If the menu doesn't exist, creates it first and then updates it. 64 */ 65 void updateWidget(QStringList newModulesToUse, QString thisModule, int newIndex, int leftLikeModules); 66 67 /** Creates the menu from scratch and updates the items using updateMenu().*/ 68 void recreateWidget(QStringList newModulesToUse, QString thisModule, int newIndex, int leftLikeModules); 69 70 signals: 71 /** User selected a module from menu to replace an existing module.*/ 72 void sigModuleReplace ( int index, QString newModule ); 73 /** User selected a module from menu to add. */ 74 void sigModuleAdd ( int index, QString module ); 75 /** User selected a module from menu to be removed. */ 76 void sigModuleRemove ( int index ); 77 78 private slots: 79 /** Handle the action signal from the menu.*/ 80 void moduleChosen(QAction* action ); 81 82 private: 83 84 /** 85 * Populates the menu with language submenus and module items without setting 86 * their states. 87 */ 88 void populateMenu(); 89 /** Adds items to the menu recursively. */ 90 void addItemToMenu(BTModuleTreeItem* item, QMenu* menu, TypeOfAction actionType); 91 92 private: 93 94 int m_id; 95 QAction* m_removeAction; 96 CSwordModuleInfo::ModuleType m_moduleType; 97 QString m_module; 98 QLabel* m_label; 99 QToolButton* m_button; 100 QFrame* m_separator; 101 QMenu* m_popup; 102 QList<QMenu*> m_submenus; 103 }; 104 105 #endif 106