summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swift/QtUI')
-rw-r--r--Swift/QtUI/QtSpellCheckerWindow.cpp77
-rw-r--r--Swift/QtUI/QtSpellCheckerWindow.h18
-rw-r--r--Swift/QtUI/QtSpellCheckerWindow.ui45
-rw-r--r--Swift/QtUI/QtTextEdit.cpp22
4 files changed, 62 insertions, 100 deletions
diff --git a/Swift/QtUI/QtSpellCheckerWindow.cpp b/Swift/QtUI/QtSpellCheckerWindow.cpp
index 9232b19..ed7e7e8 100644
--- a/Swift/QtUI/QtSpellCheckerWindow.cpp
+++ b/Swift/QtUI/QtSpellCheckerWindow.cpp
@@ -18,6 +18,8 @@
#include <QStringList>
#include <QTimer>
+#include <Swiften/Base/Log.h>
+
#include <Swift/Controllers/SettingConstants.h>
#include <Swift/Controllers/Settings/SettingsProvider.h>
@@ -38,7 +40,6 @@ QtSpellCheckerWindow::QtSpellCheckerWindow(SettingsProvider* settings, QWidget*
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()));
setFromSettings();
}
@@ -48,27 +49,39 @@ void QtSpellCheckerWindow::shrinkWindow() {
void QtSpellCheckerWindow::setFromSettings() {
ui_.spellChecker->setChecked(settings_->getSetting(SettingConstants::SPELL_CHECKER));
- ui_.pathContent->setText(P2QSTRING(settings_->getSetting(SettingConstants::DICT_PATH)));
- ui_.currentLanguageValue->setText(P2QSTRING(settings_->getSetting(SettingConstants::DICT_FILE)));
- std::string currentPath = settings_->getSetting(SettingConstants::DICT_PATH);
- QString filename = "*.dic";
- QDir dictDirectory = QDir(P2QSTRING(currentPath));
- QStringList files = dictDirectory.entryList(QStringList(filename), QDir::Files);
- showFiles(files);
setEnabled(settings_->getSetting(SettingConstants::SPELL_CHECKER));
}
+void QtSpellCheckerWindow::setSupportedLanguages(const std::vector<std::string>& languages) {
+ languageItems_.clear();
+ ui_.languageView->clear();
+ for (const auto& shortLang : languages) {
+ auto locale = QLocale(P2QSTRING(shortLang));
+ auto label = QString("%1 ( %2 )").arg(locale.nativeLanguageName(), locale.nativeCountryName());
+
+ QListWidgetItem* item = new QListWidgetItem(label);
+ item->setData(Qt::UserRole, P2QSTRING(shortLang));
+ languageItems_[shortLang] = item;
+ ui_.languageView->addItem(item);
+ }
+}
+
+void QtSpellCheckerWindow::setActiveLanguage(const std::string& language) {
+ SWIFT_LOG_ASSERT(languageItems_.find(language) != languageItems_.end(), warning) << "Language '" << language << "' is not available." << std::endl;
+ if (languageItems_.find(language) != languageItems_.end()) {
+ languageItems_[language]->setSelected(true);
+ }
+}
+
+void QtSpellCheckerWindow::setAutomaticallyIdentifiesLanguage(bool isAutomaticallyIdentifying) {
+ ui_.languageView->setHidden(isAutomaticallyIdentifying);
+}
+
void QtSpellCheckerWindow::handleChecker(bool state) {
setEnabled(state);
}
void QtSpellCheckerWindow::setEnabled(bool state) {
- ui_.pathContent->setEnabled(state);
- ui_.languageView->setEnabled(state);
- ui_.pathButton->setEnabled(state);
- ui_.pathLabel->setEnabled(state);
- ui_.currentLanguage->setEnabled(state);
- ui_.currentLanguageValue->setEnabled(state);
ui_.language->setEnabled(state);
}
@@ -76,7 +89,7 @@ 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.first())->text()));
+ settings_->storeSetting(SettingConstants::SPELL_CHECKER_LANGUAGE, Q2PSTRING(selectedLanguage.first()->data(Qt::UserRole).toString()));
}
this->done(0);
}
@@ -85,38 +98,4 @@ 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 != P2QSTRING(currentPath)) {
- ui_.languageView->clear();
- settings_->storeSetting(SettingConstants::DICT_FILE, "");
- ui_.currentLanguageValue->setText(" ");
- }
- if (!dirpath.isEmpty()) {
- if (!dirpath.endsWith("/")) {
- dirpath.append("/");
- }
- settings_->storeSetting(SettingConstants::DICT_PATH, Q2PSTRING(dirpath));
- QDir dictDirectory = QDir(dirpath);
- ui_.pathContent->setText(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"));
- settings_->storeSetting(SettingConstants::PERSONAL_DICT_PATH, Q2PSTRING(filename));
-}
-
-void QtSpellCheckerWindow::showFiles(const QStringList& files) {
- ui_.languageView->clear();
- for (int i = 0; i < files.size(); ++i) {
- ui_.languageView->insertItem(i, files[i]);
- }
-}
-
}
diff --git a/Swift/QtUI/QtSpellCheckerWindow.h b/Swift/QtUI/QtSpellCheckerWindow.h
index 846dcbb..86ebd40 100644
--- a/Swift/QtUI/QtSpellCheckerWindow.h
+++ b/Swift/QtUI/QtSpellCheckerWindow.h
@@ -12,29 +12,41 @@
#pragma once
+#include <string>
+#include <unordered_map>
+#include <vector>
+
#include <QDialog>
#include <Swift/QtUI/ui_QtSpellCheckerWindow.h>
+class QListWidgetItem;
+
namespace Swift {
class SettingsProvider;
class QtSpellCheckerWindow : public QDialog, protected Ui::QtSpellCheckerWindow {
Q_OBJECT
public:
QtSpellCheckerWindow(SettingsProvider* settings, QWidget* parent = nullptr);
+
+ void setSupportedLanguages(const std::vector<std::string>& languages);
+ void setActiveLanguage(const std::string& language);
+ void setAutomaticallyIdentifiesLanguage(bool isAutomaticallyIdentifying);
+
public slots:
void handleChecker(bool state);
void handleCancel();
- void handlePathButton();
- void handlePersonalPathButton();
void handleApply();
+
private slots:
void shrinkWindow();
+
private:
void setEnabled(bool state);
void setFromSettings();
- void showFiles(const QStringList& files);
+
SettingsProvider* settings_;
Ui::QtSpellCheckerWindow ui_;
+ std::unordered_map<std::string, QListWidgetItem*> languageItems_;
};
}
diff --git a/Swift/QtUI/QtSpellCheckerWindow.ui b/Swift/QtUI/QtSpellCheckerWindow.ui
index b7f5161..dcb70fa 100644
--- a/Swift/QtUI/QtSpellCheckerWindow.ui
+++ b/Swift/QtUI/QtSpellCheckerWindow.ui
@@ -25,45 +25,6 @@
<widget class="QWidget" name="hunspellOptions" native="true">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
- <layout class="QHBoxLayout" name="horizontalLayout_2">
- <item>
- <widget class="QLabel" name="pathLabel">
- <property name="text">
- <string>Dictionary Path:</string>
- </property>
- </widget>
- </item>
- <item>
- <widget class="QLineEdit" name="pathContent"/>
- </item>
- <item>
- <widget class="QPushButton" name="pathButton">
- <property name="text">
- <string>Change</string>
- </property>
- </widget>
- </item>
- </layout>
- </item>
- <item>
- <layout class="QHBoxLayout" name="horizontalLayout_3">
- <item>
- <widget class="QLabel" name="currentLanguage">
- <property name="text">
- <string>Current Language:</string>
- </property>
- </widget>
- </item>
- <item>
- <widget class="QLabel" name="currentLanguageValue">
- <property name="text">
- <string/>
- </property>
- </widget>
- </item>
- </layout>
- </item>
- <item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="language">
@@ -73,7 +34,11 @@
</widget>
</item>
<item>
- <widget class="QListWidget" name="languageView"/>
+ <widget class="QListWidget" name="languageView">
+ <property name="alternatingRowColors">
+ <bool>true</bool>
+ </property>
+ </widget>
</item>
</layout>
</item>
diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp
index 846dcbc..3143192 100644
--- a/Swift/QtUI/QtTextEdit.cpp
+++ b/Swift/QtUI/QtTextEdit.cpp
@@ -179,17 +179,17 @@ void QtTextEdit::addSuggestions(QMenu* menu, QContextMenuEvent* event)
#ifdef HAVE_SPELLCHECKER
-void QtTextEdit::setUpSpellChecker()
-{
+void QtTextEdit::setUpSpellChecker() {
delete highlighter_;
highlighter_ = nullptr;
delete checker_;
checker_ = nullptr;
if (settings_->getSetting(SettingConstants::SPELL_CHECKER)) {
- std::string dictPath = settings_->getSetting(SettingConstants::DICT_PATH);
- std::string dictFile = settings_->getSetting(SettingConstants::DICT_FILE);
- checker_ = SpellCheckerFactory().createSpellChecker(dictPath + dictFile);
+ checker_ = SpellCheckerFactory().createSpellChecker();
if (checker_) {
+ if (!checker_->isAutomaticallyDetectingLanguage()) {
+ checker_->setActiveLanguage(settings_->getSetting(SettingConstants::SPELL_CHECKER_LANGUAGE));
+ }
highlighter_ = new QtSpellCheckHighlighter(document(), checker_);
}
else {
@@ -213,12 +213,18 @@ void QtTextEdit::spellCheckerSettingsWindow() {
spellCheckerWindow_->raise();
spellCheckerWindow_->activateWindow();
}
+ if (checker_) {
+ spellCheckerWindow_->setAutomaticallyIdentifiesLanguage(checker_->isAutomaticallyDetectingLanguage());
+ if (!checker_->isAutomaticallyDetectingLanguage()) {
+ spellCheckerWindow_->setSupportedLanguages(checker_->supportedLanguages());
+ spellCheckerWindow_->setActiveLanguage(checker_->activeLanguage());
+ }
+ }
}
void QtTextEdit::handleSettingChanged(const std::string& settings) {
- if (settings == SettingConstants::SPELL_CHECKER.getKey()
- || settings == SettingConstants::DICT_PATH.getKey()
- || settings == SettingConstants::DICT_FILE.getKey()) {
+ if (settings == SettingConstants::SPELL_CHECKER.getKey() ||
+ settings == SettingConstants::SPELL_CHECKER_LANGUAGE.getKey()) {
#ifdef HAVE_SPELLCHECKER
setUpSpellChecker();
if (highlighter_) {