TLocalizableWrap.h
Go to the documentation of this file.
1 #ifndef ibase_TLocalizableWrap_included
2 #define ibase_TLocalizableWrap_included
3 
4 
5 // Qt includes
6 #include <QtCore/QObject>
7 #include <QtCore/QCoreApplication>
8 
9 
10 namespace ibase
11 {
12 
13 
14 template <class Base>
15 class TLocalizableWrap: public Base
16 {
17 public:
18  typedef Base BaseClass;
19 
21 
25  virtual void EnableLocalization(bool enableLocalization = true);
26 
27 protected:
28  // abstract methods
29  virtual void OnLanguageChanged() = 0;
30 
31 private:
32  class LanguageChangeEventFilter: public QObject
33  {
34  public:
35  typedef QObject BaseClass;
36 
37  explicit LanguageChangeEventFilter(TLocalizableWrap& parent);
38 
39  protected:
40  // reimplemented (QObject)
41  bool eventFilter(QObject* sourcePtr, QEvent* eventPtr);
42 
43  private:
44  TLocalizableWrap& m_parent;
45  };
46 
47  LanguageChangeEventFilter m_languageChangeEventFilter;
48 };
49 
50 
51 // public methods of class TLocalizableWrap
52 
53 template <class Base>
55  :m_languageChangeEventFilter(*this)
56 {
57 }
58 
59 
60 template <class Base>
61 void TLocalizableWrap<Base>::EnableLocalization(bool enableLocalization)
62 {
63  QCoreApplication* applicationPtr = QCoreApplication::instance();
64  if (applicationPtr != NULL){
65  if (enableLocalization){
66  applicationPtr->installEventFilter(&m_languageChangeEventFilter);
67  }
68  else{
69  applicationPtr->removeEventFilter(&m_languageChangeEventFilter);
70  }
71  }
72 }
73 
74 
75 // public methods of the embedded class LanguageChangeEventFilter
76 
77 template <class Base>
79  :m_parent(parent)
80 {
81 }
82 
83 
84 // protected methods of the embedded class LanguageChangeEventFilter
85 
86 // reimplemented (QObject)
87 
88 template <class Base>
89 bool TLocalizableWrap<Base>::LanguageChangeEventFilter::eventFilter(QObject* sourcePtr, QEvent* eventPtr)
90 {
91  if ((eventPtr->type() == QEvent::LanguageChange) && (sourcePtr == QCoreApplication::instance())){
92  m_parent.OnLanguageChanged();
93  }
94 
95  return BaseClass::eventFilter(sourcePtr, eventPtr);
96 }
97 
98 
99 } // namespace ibase
100 
101 
102 #endif // ibase_TLocalizableWrap_included
103 
virtual void EnableLocalization(bool enableLocalization=true)
If enabled, listen to the language change event from the application context.
#define NULL
Definition: istd.h:64
virtual void OnLanguageChanged()=0

© 2007-2017 Witold Gantzke and Kirill Lepskiy