-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AppSettings and TempAPI classes.
- Loading branch information
Ugnius
committed
Jun 14, 2018
1 parent
b33a60d
commit 19d9bc5
Showing
10 changed files
with
331 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "tempapi.h" | ||
|
||
TempAPI::TempAPI(QObject *parent) | ||
: QObject(parent) { | ||
} | ||
|
||
TempAPI::~TempAPI() { | ||
} | ||
|
||
QString TempAPI::translate(const QString &input, | ||
const QString &sourceLanguage, | ||
const QString &targetLanguage) const { | ||
if (input.isEmpty()) { | ||
return QString(); | ||
} | ||
|
||
QSettings settings; | ||
QString key = settings.value("temp/key").toString(); | ||
QString format = settings.value("temp/format").toString(); | ||
|
||
QString translation = "Some translation."; | ||
|
||
return translation; | ||
} | ||
|
||
QStringList TempAPI::getSupportedLanguages() const { | ||
QSettings settings; | ||
QString key = settings.value("temp/key").toString(); | ||
|
||
QStringList languagesList; | ||
|
||
languagesList.append("en"); | ||
languagesList.append("fr"); | ||
languagesList.append("de"); | ||
languagesList.append("lt"); | ||
|
||
return languagesList; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#ifndef INSTANT_TRANSLATOR_TEMPAPI_H | ||
#define INSTANT_TRANSLATOR_TEMPAPI_H | ||
|
||
#include <QObject> | ||
#include <QSettings> | ||
#include "api.h" | ||
|
||
/** | ||
* Temporary API implementation class. | ||
*/ | ||
class TempAPI : public QObject, public API { | ||
Q_OBJECT | ||
Q_INTERFACES(API) | ||
public: | ||
/** | ||
* Constructs an object with parent object parent. | ||
* @param parent Parent of an object may be viewed as the object's owner. | ||
*/ | ||
explicit TempAPI(QObject *parent = nullptr); | ||
|
||
/** | ||
* Destructor. | ||
*/ | ||
~TempAPI() override; | ||
|
||
/** | ||
* Translate given input. | ||
* @param input Input string. | ||
* @param sourceLanguage Source language code. | ||
* @param targetLanguage Target language code. | ||
* @return Translated string. | ||
*/ | ||
QString translate(const QString &input, | ||
const QString &sourceLanguage, | ||
const QString &targetLanguage) const override; | ||
|
||
/** | ||
* Get supported languages. | ||
* @return Supported languages string list. | ||
*/ | ||
QStringList getSupportedLanguages() const override; | ||
}; | ||
|
||
#endif // INSTANT_TRANSLATOR_TEMPAPI_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,76 @@ | ||
#include "settingsdialog.h" | ||
#include "ui_settingsdialog.h" | ||
|
||
SettingsDialog::SettingsDialog(QWidget *parent) : | ||
SettingsDialog::SettingsDialog(QWidget *parent, AppSettings *appSettings) : | ||
QDialog(parent), | ||
ui(new Ui::SettingsDialog) { | ||
ui(new Ui::SettingsDialog), | ||
appSettings(appSettings) { | ||
ui->setupUi(this); | ||
|
||
APISettings *googleAPISettings = new APISettings(this, "google", "Google Translate API"); | ||
googleAPISettings->readSettings(); | ||
apiSettingsList.append(googleAPISettings); | ||
|
||
APISettings *tempAPISettings = new APISettings(this, "temp", "Temp"); | ||
tempAPISettings->readSettings(); | ||
apiSettingsList.append(tempAPISettings); | ||
|
||
for (int i = 0; i < apiSettingsList.count(); ++i) { | ||
QListWidgetItem *listWidgetItem = new QListWidgetItem(apiSettingsList.at(i)->getFullName()); | ||
apiSettingsListWidgetItemList.append(listWidgetItem); | ||
ui->apisListWidget->addItem(listWidgetItem); | ||
} | ||
loadSettings(); | ||
} | ||
|
||
SettingsDialog::~SettingsDialog() { | ||
delete ui; | ||
} | ||
|
||
void SettingsDialog::setAppSettings(AppSettings *appSettings) { | ||
this->appSettings = appSettings; | ||
loadSettings(); | ||
} | ||
|
||
void SettingsDialog::on_apisListWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) { | ||
int currentIndex = apiSettingsListWidgetItemList.indexOf(current); | ||
|
||
if (currentIndex >= 0) { | ||
APISettings *apiSettings = apiSettingsList.at(currentIndex); | ||
APISettings *apiSettings = appSettings->getApiSettingsList().at(currentIndex); | ||
ui->apiKeyLineEdit->setText(apiSettings->getApiKey()); | ||
ui->defaultSourceLanguageLineEdit->setText(apiSettings->getDefaultSourceLanguage()); | ||
ui->defaultTargetLanguageLineEdit->setText(apiSettings->getDefaultTargetLanguage()); | ||
} | ||
} | ||
|
||
void SettingsDialog::on_settingsButtonBox_accepted() { | ||
save(); | ||
saveSettings(); | ||
} | ||
|
||
void SettingsDialog::on_apiKeyLineEdit_textChanged() { | ||
int currentIndex = apiSettingsListWidgetItemList.indexOf(ui->apisListWidget->currentItem()); | ||
|
||
if (currentIndex >= 0) { | ||
APISettings *apiSettings = apiSettingsList.at(currentIndex); | ||
apiSettings->setApiKey(ui->apiKeyLineEdit->text()); | ||
void SettingsDialog::on_settingsButtonBox_clicked(QAbstractButton *button) { | ||
if ((QPushButton *) button == ui->settingsButtonBox->button(QDialogButtonBox::Apply)) { | ||
saveSettings(); | ||
} | ||
} | ||
|
||
void SettingsDialog::on_defaultSourceLanguageLineEdit_textChanged() { | ||
void SettingsDialog::saveSettings() { | ||
int currentIndex = apiSettingsListWidgetItemList.indexOf(ui->apisListWidget->currentItem()); | ||
|
||
if (currentIndex >= 0) { | ||
APISettings *apiSettings = apiSettingsList.at(currentIndex); | ||
APISettings *apiSettings = appSettings->getApiSettingsList().at(currentIndex); | ||
apiSettings->setApiKey(ui->apiKeyLineEdit->text()); | ||
apiSettings->setDefaultSourceLanguage(ui->defaultSourceLanguageLineEdit->text()); | ||
apiSettings->setDefaultTargetLanguage(ui->defaultTargetLanguageLineEdit->text()); | ||
} | ||
} | ||
|
||
void SettingsDialog::on_defaultTargetLanguageLineEdit_textChanged() { | ||
int currentIndex = apiSettingsListWidgetItemList.indexOf(ui->apisListWidget->currentItem()); | ||
int defaultApiIndex = ui->defaultApiComboBox->currentIndex(); | ||
QString defaultApiName = appSettings->getApiSettingsList().at(defaultApiIndex)->getName(); | ||
appSettings->setDefaultApi(defaultApiName); | ||
|
||
appSettings->save(); | ||
|
||
if (currentIndex >= 0) { | ||
APISettings *apiSettings = apiSettingsList.at(currentIndex); | ||
apiSettings->setDefaultTargetLanguage(ui->defaultTargetLanguageLineEdit->text()); | ||
} | ||
} | ||
|
||
void SettingsDialog::save() { | ||
for (int i = 0; i < apiSettingsList.count(); ++i) { | ||
APISettings *apiSettings = apiSettingsList.at(i); | ||
apiSettings->writeSettings(); | ||
void SettingsDialog::loadSettings() { | ||
for (int i = 0; i < appSettings->getApiSettingsList().count(); ++i) { | ||
APISettings *apiSettings = appSettings->getApiSettingsList().at(i); | ||
|
||
QListWidgetItem *listWidgetItem = new QListWidgetItem(apiSettings->getFullName()); | ||
apiSettingsListWidgetItemList.append(listWidgetItem); | ||
ui->apisListWidget->addItem(listWidgetItem); | ||
|
||
ui->defaultApiComboBox->addItem(apiSettings->getFullName()); | ||
QSettings settings; | ||
QString defaultApiName = settings.value("default_api").toString(); | ||
if (apiSettings->getName() == defaultApiName) { | ||
ui->defaultApiComboBox->setCurrentIndex(i); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.