VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/globals/QIWithRetranslateUI.h@ 103977

Last change on this file since 103977 was 99949, checked in by vboxsync, 19 months ago

FE/Qt: bugref:10451: Build fix for r157603.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
1/* $Id: QIWithRetranslateUI.h 99949 2023-05-24 09:15:37Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - Qt extensions: QIWithRetranslateUI class declaration.
4 */
5
6/*
7 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#ifndef FEQT_INCLUDED_SRC_globals_QIWithRetranslateUI_h
29#define FEQT_INCLUDED_SRC_globals_QIWithRetranslateUI_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34/* Qt includes: */
35#include <QApplication>
36#include <QDialog>
37#include <QEvent>
38#include <QGraphicsWidget>
39#include <QMainWindow>
40#include <QObject>
41#include <QWidget>
42
43/* GUI includes: */
44#include "QIWithRestorableGeometry.h"
45#include "UILibraryDefs.h"
46#include "UITranslator.h"
47
48
49/** Template for automatic language translations of underlying QWidget. */
50template <class Base>
51class QIWithRetranslateUI : public Base
52{
53public:
54
55 /** Constructs translatable widget passing @a pParent to the base-class. */
56 QIWithRetranslateUI(QWidget *pParent = 0) : Base(pParent)
57 {
58 qApp->installEventFilter(this);
59 }
60
61protected:
62
63 /** Pre-handles standard Qt @a pEvent for passed @a pObject. */
64 virtual bool eventFilter(QObject *pObject, QEvent *pEvent)
65 {
66 /* If translation is NOT currently in progress handle
67 * LanguageChange events for qApp or this object: */
68 if ( !UITranslator::isTranslationInProgress()
69 && pEvent->type() == QEvent::LanguageChange
70 && (pObject == qApp || pObject == this))
71 retranslateUi();
72
73 /* Call to base-class: */
74 return Base::eventFilter(pObject, pEvent);
75 }
76
77 /** Handles translation event. */
78 virtual void retranslateUi() = 0;
79};
80
81/** Explicit QIWithRetranslateUI instantiation for QWidget & QDialog classes.
82 * @note On Windows it's important that all template cases are instantiated just once across
83 * the linking space. In case we have particular template case instantiated from both
84 * library and executable sides, - we have multiple definition case and need to strictly
85 * ask compiler to do it just once and link such cases against library only.
86 * I would also note that it would be incorrect to just make whole the template exported
87 * to library because latter can have lack of required instantiations (current case). */
88template class SHARED_LIBRARY_STUFF QIWithRetranslateUI<QWidget>;
89template class SHARED_LIBRARY_STUFF QIWithRetranslateUI<QDialog>;
90template class SHARED_LIBRARY_STUFF QIWithRetranslateUI<QIWithRestorableGeometry<QMainWindow> >;
91
92
93/** Template for automatic language translations of underlying QWidget with certain flags. */
94template <class Base>
95class QIWithRetranslateUI2 : public Base
96{
97public:
98
99 /** Constructs translatable widget passing @a pParent and @a enmFlags to the base-class. */
100 QIWithRetranslateUI2(QWidget *pParent = 0, Qt::WindowFlags enmFlags = Qt::WindowFlags()) : Base(pParent, enmFlags)
101 {
102 qApp->installEventFilter(this);
103 }
104
105protected:
106
107 /** Pre-handles standard Qt @a pEvent for passed @a pObject. */
108 virtual bool eventFilter(QObject *pObject, QEvent *pEvent)
109 {
110 /* If translation is NOT currently in progress handle
111 * LanguageChange events for qApp or this object: */
112 if ( !UITranslator::isTranslationInProgress()
113 && pEvent->type() == QEvent::LanguageChange
114 && (pObject == qApp || pObject == this))
115 retranslateUi();
116
117 /* Call to base-class: */
118 return Base::eventFilter(pObject, pEvent);
119 }
120
121 /** Handles translation event. */
122 virtual void retranslateUi() = 0;
123};
124
125
126/** Template for automatic language translations of underlying QObject. */
127template <class Base>
128class QIWithRetranslateUI3 : public Base
129{
130public:
131
132 /** Constructs translatable widget passing @a pParent to the base-class. */
133 QIWithRetranslateUI3(QObject *pParent = 0)
134 : Base(pParent)
135 {
136 qApp->installEventFilter(this);
137 }
138
139protected:
140
141 /** Pre-handles standard Qt @a pEvent for passed @a pObject. */
142 virtual bool eventFilter(QObject *pObject, QEvent *pEvent)
143 {
144 /* If translation is NOT currently in progress handle
145 * LanguageChange events for qApp or this object: */
146 if ( !UITranslator::isTranslationInProgress()
147 && pEvent->type() == QEvent::LanguageChange
148 && (pObject == qApp || pObject == this))
149 retranslateUi();
150
151 /* Call to base-class: */
152 return Base::eventFilter(pObject, pEvent);
153 }
154
155 /** Handles translation event. */
156 virtual void retranslateUi() = 0;
157};
158
159/** Explicit QIWithRetranslateUI3 instantiation for QObject class.
160 * @note On Windows it's important that all template cases are instantiated just once across
161 * the linking space. In case we have particular template case instantiated from both
162 * library and executable sides, - we have multiple definition case and need to strictly
163 * ask compiler to do it just once and link such cases against library only.
164 * I would also note that it would be incorrect to just make whole the template exported
165 * to library because latter can have lack of required instantiations (current case). */
166template class SHARED_LIBRARY_STUFF QIWithRetranslateUI3<QObject>;
167
168
169/** Template for automatic language translations of underlying QGraphicsWidget. */
170template <class Base>
171class QIWithRetranslateUI4 : public Base
172{
173public:
174
175 /** Constructs translatable widget passing @a pParent to the base-class. */
176 QIWithRetranslateUI4(QGraphicsWidget *pParent = 0)
177 : Base(pParent)
178 {
179 qApp->installEventFilter(this);
180 }
181
182protected:
183
184 /** Pre-handles standard Qt @a pEvent for passed @a pObject. */
185 virtual bool eventFilter(QObject *pObject, QEvent *pEvent)
186 {
187 /* If translation is NOT currently in progress handle
188 * LanguageChange events for qApp or this object: */
189 if ( !UITranslator::isTranslationInProgress()
190 && pEvent->type() == QEvent::LanguageChange
191 && (pObject == qApp || pObject == this))
192 retranslateUi();
193
194 /* Call to base-class: */
195 return Base::eventFilter(pObject, pEvent);
196 }
197
198 /** Handles translation event. */
199 virtual void retranslateUi() = 0;
200};
201
202
203#endif /* !FEQT_INCLUDED_SRC_globals_QIWithRetranslateUI_h */
204
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette