VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/settings/global/VBoxGLSettingsLanguage.cpp@ 25177

Last change on this file since 25177 was 25177, checked in by vboxsync, 15 years ago

fix OSE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 10.9 KB
Line 
1/** @file
2 *
3 * VBox frontends: Qt4 GUI ("VirtualBox"):
4 * VBoxGLSettingsLanguage class implementation
5 */
6
7/*
8 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23#include "VBoxGLSettingsLanguage.h"
24#include "VBoxGlobalSettings.h"
25#include "VBoxGlobal.h"
26
27#include <QHeaderView>
28#include <QTranslator>
29#include <QDir>
30#include <QPainter>
31
32#include <iprt/err.h>
33#include <iprt/param.h>
34#include <iprt/path.h>
35
36extern const char *gVBoxLangSubDir;
37extern const char *gVBoxLangFileBase;
38extern const char *gVBoxLangFileExt;
39extern const char *gVBoxLangIDRegExp;
40extern const char *gVBoxBuiltInLangName;
41
42class LanguageItem : public QTreeWidgetItem
43{
44public:
45
46 enum { LanguageItemType = QTreeWidgetItem::UserType + 1 };
47
48 LanguageItem (QTreeWidget *aParent, const QTranslator &aTranslator,
49 const QString &aId, bool aBuiltIn = false)
50 : QTreeWidgetItem (aParent, LanguageItemType), mBuiltIn (aBuiltIn)
51 {
52 Assert (!aId.isEmpty());
53
54 /* Note: context/source/comment arguments below must match strings
55 * used in VBoxGlobal::languageName() and friends (the latter are the
56 * source of information for the lupdate tool that generates
57 * translation files) */
58
59 QString nativeLanguage = tratra (aTranslator,
60 "@@@", "English", "Native language name");
61 QString nativeCountry = tratra (aTranslator,
62 "@@@", "--", "Native language country name "
63 "(empty if this language is for all countries)");
64
65 QString englishLanguage = tratra (aTranslator,
66 "@@@", "English", "Language name, in English");
67 QString englishCountry = tratra (aTranslator,
68 "@@@", "--", "Language country name, in English "
69 "(empty if native country name is empty)");
70
71 QString translatorsName = tratra (aTranslator,
72 "@@@", "Sun Microsystems, Inc.", "Comma-separated list of translators");
73
74 QString itemName = nativeLanguage;
75 QString langName = englishLanguage;
76
77 if (!aBuiltIn)
78 {
79 if (nativeCountry != "--")
80 itemName += " (" + nativeCountry + ")";
81
82 if (englishCountry != "--")
83 langName += " (" + englishCountry + ")";
84
85 if (itemName != langName)
86 langName = itemName + " / " + langName;
87 }
88 else
89 {
90 itemName += VBoxGLSettingsLanguage::tr (" (built-in)", "Language");
91 langName += VBoxGLSettingsLanguage::tr (" (built-in)", "Language");
92 }
93
94 setText (0, itemName);
95 setText (1, aId);
96 setText (2, langName);
97 setText (3, translatorsName);
98
99 /* Current language appears in bold */
100 if (text (1) == VBoxGlobal::languageId())
101 {
102 QFont fnt = font (0);
103 fnt.setBold (true);
104 setFont (0, fnt);
105 }
106 }
107
108 /* Constructs an item for an invalid language ID (i.e. when a language
109 * file is missing or corrupt). */
110 LanguageItem (QTreeWidget *aParent, const QString &aId)
111 : QTreeWidgetItem (aParent, LanguageItemType), mBuiltIn (false)
112 {
113 Assert (!aId.isEmpty());
114
115 setText (0, QString ("<%1>").arg (aId));
116 setText (1, aId);
117 setText (2, VBoxGLSettingsLanguage::tr ("<unavailable>", "Language"));
118 setText (3, VBoxGLSettingsLanguage::tr ("<unknown>", "Author(s)"));
119
120 /* Invalid language appears in italic */
121 QFont fnt = font (0);
122 fnt.setItalic (true);
123 setFont (0, fnt);
124 }
125
126 /* Constructs an item for the default language ID (column 1 will be set
127 * to QString::null) */
128 LanguageItem (QTreeWidget *aParent)
129 : QTreeWidgetItem (aParent, LanguageItemType), mBuiltIn (false)
130 {
131 setText (0, VBoxGLSettingsLanguage::tr ("Default", "Language"));
132 setText (1, QString::null);
133 /* Empty strings of some reasonable length to prevent the info part
134 * from being shrinked too much when the list wants to be wider */
135 setText (2, " ");
136 setText (3, " ");
137 }
138
139 bool isBuiltIn() const { return mBuiltIn; }
140
141 bool operator< (const QTreeWidgetItem &aOther) const
142 {
143 QString thisId = text (1);
144 QString thatId = aOther.text (1);
145 if (thisId.isNull())
146 return true;
147 if (thatId.isNull())
148 return false;
149 if (mBuiltIn)
150 return true;
151 if (aOther.type() == LanguageItemType && ((LanguageItem*) &aOther)->mBuiltIn)
152 return false;
153 return QTreeWidgetItem::operator< (aOther);
154 }
155
156private:
157
158 QString tratra (const QTranslator &aTranslator, const char *aCtxt,
159 const char *aSrc, const char *aCmnt)
160 {
161 QString msg = aTranslator.translate (aCtxt, aSrc, aCmnt);
162 /* return the source text if no translation is found */
163 if (msg.isEmpty())
164 msg = QString (aSrc);
165 return msg;
166 }
167
168 bool mBuiltIn : 1;
169};
170
171
172VBoxGLSettingsLanguage::VBoxGLSettingsLanguage()
173{
174 /* Apply UI decorations */
175 Ui::VBoxGLSettingsLanguage::setupUi (this);
176
177 /* Setup dialog */
178 mTwLanguage->header()->hide();
179 mTwLanguage->hideColumn (1);
180 mTwLanguage->hideColumn (2);
181 mTwLanguage->hideColumn (3);
182
183 /* Setup Connections */
184 connect (mTwLanguage, SIGNAL (painted (QTreeWidgetItem *, QPainter *)),
185 this, SLOT (mTwItemPainted (QTreeWidgetItem *, QPainter *)));
186 connect (mTwLanguage, SIGNAL (currentItemChanged (QTreeWidgetItem *, QTreeWidgetItem *)),
187 this, SLOT (mTwLanguageChanged (QTreeWidgetItem *)));
188
189 /* Applying language settings */
190 retranslateUi();
191}
192
193void VBoxGLSettingsLanguage::getFrom (const CSystemProperties & /* aProps */, const VBoxGlobalSettings &aGs)
194{
195 reload (aGs.languageId());
196 mTxName->setFixedHeight (fontMetrics().height() * 4);
197}
198
199void VBoxGLSettingsLanguage::putBackTo (CSystemProperties & /* aProps */, VBoxGlobalSettings &aGs)
200{
201 QTreeWidgetItem *curItem = mTwLanguage->currentItem();
202 Assert (curItem);
203 if (mLanguageChanged && curItem)
204 {
205 aGs.setLanguageId (curItem->text (1));
206 VBoxGlobal::loadLanguage (curItem->text (1));
207 }
208}
209
210void VBoxGLSettingsLanguage::setOrderAfter (QWidget *aWidget)
211{
212 setTabOrder (aWidget, mTwLanguage);
213}
214
215void VBoxGLSettingsLanguage::reload (const QString &aLangId)
216{
217 /* Clear languages list */
218 mTwLanguage->clear();
219
220 /* Load languages list */
221 char szNlsPath [RTPATH_MAX];
222 int rc = RTPathAppPrivateNoArch (szNlsPath, sizeof(szNlsPath));
223 AssertRC (rc);
224 QString nlsPath = QString (szNlsPath) + gVBoxLangSubDir;
225 QDir nlsDir (nlsPath);
226 QStringList files = nlsDir.entryList (QStringList (QString ("%1*%2")
227 .arg (gVBoxLangFileBase, gVBoxLangFileExt)), QDir::Files);
228
229 QTranslator translator;
230 /* Add the default language */
231 new LanguageItem (mTwLanguage);
232 /* Add the built-in language */
233 new LanguageItem (mTwLanguage, translator, gVBoxBuiltInLangName, true /* built-in */);
234 /* Add all existing languages */
235 for (QStringList::Iterator it = files.begin(); it != files.end(); ++ it)
236 {
237 QString fileName = *it;
238 QRegExp regExp (QString (gVBoxLangFileBase) + gVBoxLangIDRegExp);
239 int pos = regExp.indexIn (fileName);
240 if (pos == -1)
241 continue;
242
243 /* Skip any English version, cause this is extra handled. */
244 QString lang = regExp.cap (2);
245 if (lang.toLower() == "en")
246 continue;
247
248 bool loadOk = translator.load (fileName, nlsPath);
249 if (!loadOk)
250 continue;
251
252 new LanguageItem (mTwLanguage, translator, regExp.cap (1));
253 }
254
255 /* Adjust selector list */
256#ifdef Q_WS_MAC
257 int width = qMax (static_cast<QAbstractItemView*> (mTwLanguage)
258 ->sizeHintForColumn (0) + 2 * mTwLanguage->frameWidth() +
259 QApplication::style()->pixelMetric (QStyle::PM_ScrollBarExtent),
260 220);
261 mTwLanguage->setFixedWidth (width);
262#else /* Q_WS_MAC */
263 mTwLanguage->setMinimumWidth (static_cast<QAbstractItemView*> (mTwLanguage)
264 ->sizeHintForColumn (0) + 2 * mTwLanguage->frameWidth() +
265 QApplication::style()->pixelMetric (QStyle::PM_ScrollBarExtent));
266#endif /* Q_WS_MAC */
267 mTwLanguage->resizeColumnToContents (0);
268
269 /* Search for necessary language */
270 QList<QTreeWidgetItem*> itemsList =
271 mTwLanguage->findItems (aLangId, Qt::MatchExactly, 1);
272 QTreeWidgetItem *item = itemsList.isEmpty() ? 0 : itemsList [0];
273 if (!item)
274 {
275 /* Add an item for an invalid language to represent it in the list */
276 item = new LanguageItem (mTwLanguage, aLangId);
277 mTwLanguage->resizeColumnToContents (0);
278 }
279 Assert (item);
280 if (item)
281 mTwLanguage->setCurrentItem (item);
282
283 mTwLanguage->sortItems (0, Qt::AscendingOrder);
284 mTwLanguage->scrollToItem (item);
285 mLanguageChanged = false;
286}
287
288void VBoxGLSettingsLanguage::retranslateUi()
289{
290 /* Translate uic generated strings */
291 Ui::VBoxGLSettingsLanguage::retranslateUi (this);
292 reload (VBoxGlobal::languageId());
293}
294
295void VBoxGLSettingsLanguage::mTwItemPainted (QTreeWidgetItem *aItem, QPainter *aPainter)
296{
297 if (aItem && aItem->type() == LanguageItem::LanguageItemType)
298 {
299 LanguageItem *item = static_cast <LanguageItem*> (aItem);
300 if (item->isBuiltIn())
301 {
302 QRect rect = mTwLanguage->visualItemRect (item);
303 aPainter->setPen (mTwLanguage->palette().color (QPalette::Mid));
304 aPainter->drawLine (rect.x(), rect.y() + rect.height() - 1,
305 rect.x() + rect.width(), rect.y() + rect.height() - 1);
306 }
307 }
308}
309
310void VBoxGLSettingsLanguage::mTwLanguageChanged (QTreeWidgetItem *aItem)
311{
312 if (!aItem) return;
313
314 /* Disable labels for the Default language item */
315 bool enabled = !aItem->text (1).isNull();
316
317 mTxName->setEnabled (enabled);
318 mTxName->setText (QString ("<table>"
319 "<tr><td>%1&nbsp;</td><td>%2</td></tr>"
320 "<tr><td>%3&nbsp;</td><td>%4</td></tr>"
321 "</table>")
322 .arg (tr ("Language:"))
323 .arg (aItem->text (2))
324 .arg (tr ("Author(s):"))
325 .arg (aItem->text (3)));
326
327 mLanguageChanged = true;
328}
329
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