summaryrefslogtreecommitdiffstats
blob: c6fcc2c3b159631b79de674022eee75588a4eec3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (c) 2011 Vlad Voicu
 * Licensed under the Simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include "Swift/QtUI/QtSpellCheckerWindow.h"

#include <Swift/Controllers/Settings/SettingsProvider.h>
#include <Swift/Controllers/SettingConstants.h>
#include <Swift/QtUI/QtUISettingConstants.h>
#include <Swift/QtUI/QtSwiftUtil.h>

#include <QCoreApplication>
#include <QFileDialog>
#include <QDir>
#include <QStringList>

namespace Swift {

QtSpellCheckerWindow::QtSpellCheckerWindow(SettingsProvider* settings, QWidget* parent) : QDialog(parent) {
	settings_ = settings;
	ui_.setupUi(this);
	connect(ui_.spellChecker, SIGNAL(toggled(bool)), this, SLOT(handleChecker(bool)));
	connect(ui_.cancel, SIGNAL(clicked()), this, SLOT(handleCancel()));
	connect(ui_.apply, SIGNAL(clicked()), this, SLOT(handleApply()));
	connect(ui_.pathButton, SIGNAL(clicked()), this, SLOT(handlePathButton()));
	connect(ui_.personalPathButton, SIGNAL(clicked()), this, SLOT(handlePersonalPathButton()));
	setFromSettings();
	setEnabled(settings_->getSetting(SettingConstants::SPELL_CHECKER));
}

void QtSpellCheckerWindow::setFromSettings() {
	ui_.spellChecker->setChecked(settings_->getSetting(SettingConstants::SPELL_CHECKER));
	ui_.personalPathContent->insert(P2QSTRING(settings_->getSetting(SettingConstants::PERSONAL_DICT_PATH)));
	ui_.pathContent->insert(P2QSTRING(settings_->getSetting(SettingConstants::DICT_PATH)));
}

void QtSpellCheckerWindow::handleChecker(bool state) {
	setEnabled(state);
}

void QtSpellCheckerWindow::setEnabled(bool state) {
	ui_.pathContent->setEnabled(state);
	ui_.personalPathContent->setEnabled(state);
	ui_.languageView->setEnabled(state);
	ui_.pathButton->setEnabled(state);
	ui_.personalPathButton->setEnabled(state);
	ui_.pathLabel->setEnabled(state);
	ui_.personalDictionaryLabel->setEnabled(state);
	ui_.language->setEnabled(state);
}

void QtSpellCheckerWindow::handleApply() {
	settings_->storeSetting(SettingConstants::SPELL_CHECKER, ui_.spellChecker->isChecked());
	QList<QListWidgetItem* > selectedLanguage = ui_.languageView->selectedItems();
	if (!selectedLanguage.empty()) {
		settings_->storeSetting(SettingConstants::DICT_FILE, Q2PSTRING(selectedLanguage[0]->text()));
	}
	this->done(0);
}

void QtSpellCheckerWindow::handleCancel() {
	this->done(0);
}

void QtSpellCheckerWindow::handlePathButton() {
	std::string currentPath = settings_->getSetting(SettingConstants::DICT_PATH);
	QString dirpath = QFileDialog::getExistingDirectory(this, tr("Dictionary Path"), P2QSTRING(currentPath));
	if (dirpath != NULL) {
		QDir dictDirectory = QDir(dirpath);
		settings_->storeSetting(SettingConstants::DICT_PATH, Q2PSTRING(dirpath));
		ui_.personalPathContent->insert(dirpath);
		QString filename = "*.dic";
		QStringList files = dictDirectory.entryList(QStringList(filename), QDir::Files);
		showFiles(files);
	}
}

void QtSpellCheckerWindow::handlePersonalPathButton() {
	std::string currentPath = settings_->getSetting(SettingConstants::PERSONAL_DICT_PATH);
	QString filename = QFileDialog::getOpenFileName(this, tr("Select Personal Dictionary"), P2QSTRING(currentPath), tr("(*.dic"));
}

void QtSpellCheckerWindow::showFiles(const QStringList& files) {
	for (int i = 0; i < files.size(); ++i) {
		ui_.languageView->insertItem(i, files[i]);
	}
}

}