1 /*
2 	Actiona
3 	Copyright (C) 2005 Jonathan Mercier-Ganady
4 
5 	Actiona is free software: you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation, either version 3 of the License, or
8 	(at your option) any later version.
9 
10 	Actiona is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 	Contact : jmgr@jmgr.info
19 */
20 
21 #include "localeparameterdefinition.h"
22 #include "subparameter.h"
23 #include "codecombobox.h"
24 #include "actioninstance.h"
25 
26 namespace ActionTools
27 {
LocaleParameterDefinition(const Name & name,QObject * parent)28     LocaleParameterDefinition::LocaleParameterDefinition(const Name &name, QObject *parent)
29         : ParameterDefinition(name, parent)
30 	{
31 	}
32 
buildEditors(Script * script,QWidget * parent)33     void LocaleParameterDefinition::buildEditors(Script *script, QWidget *parent)
34 	{
35 		ParameterDefinition::buildEditors(script, parent);
36 
37 		mComboBox = new CodeComboBox(parent);
38 
39         for(const QLocale &locale: mLocales)
40         {
41             if(locale == QLocale::C)
42                 continue;
43 
44             QString countryName = locale.name().split(QLatin1Char('_')).at(1).toLower();
45             QIcon icon{QStringLiteral(":/images/flags/%1.png").arg(countryName)};
46             QString label = QStringLiteral("%1 (%2)")
47                                     .arg(QLocale::languageToString(locale.language()))
48                                     .arg(QLocale::countryToString(locale.country()));
49 
50             mComboBox->addItem(icon, label, locale.name());
51         }
52 
53         mComboBox->model()->sort(0);
54 
55         mComboBox->insertItem(0, tr("System language"), QStringLiteral(""));
56 
57 		addEditor(mComboBox);
58 
59 		emit editorBuilt();
60 	}
61 
load(const ActionInstance * actionInstance)62     void LocaleParameterDefinition::load(const ActionInstance *actionInstance)
63 	{
64 		const SubParameter &subParameter = actionInstance->subParameter(name().original(), QStringLiteral("value"));
65 
66         int index = mComboBox->findData(subParameter.value());
67 
68         if(index != -1)
69             mComboBox->setCurrentIndex(index);
70         else
71         {
72             QString localeName = (index != -1) ? mComboBox->itemText(index) : subParameter.value();
73             mComboBox->setEditText(localeName);
74         }
75 
76 		mComboBox->setCode(subParameter.isCode());
77 	}
78 
save(ActionInstance * actionInstance)79     void LocaleParameterDefinition::save(ActionInstance *actionInstance)
80 	{
81         int index = mComboBox->findText(mComboBox->currentText());
82         QString localeName = (index != -1) ? mComboBox->currentData().toString() : mComboBox->currentText();
83 
84         actionInstance->setSubParameter(name().original(), QStringLiteral("value"), mComboBox->isCode(), localeName);
85     }
86 }
87