1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-24 12:46:35 +01:00

[spellcheck] Added dialog with spell check settings.

This commit is contained in:
nowrep 2013-02-05 22:31:16 +01:00
parent c3a0e1f13a
commit 0a7773bec1
9 changed files with 526 additions and 21 deletions

View File

@ -30,7 +30,7 @@ SpellCheck::SpellCheck()
bool SpellCheck::isContinousSpellCheckingEnabled() const
{
return m_speller->isEnabled();
return mApp->speller()->isEnabled();
}
void SpellCheck::toggleContinousSpellChecking()

View File

@ -0,0 +1,136 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "spellcheckdialog.h"
#include "ui_spellcheckdialog.h"
#include "settings.h"
#include "speller.h"
#include "mainapplication.h"
#include <QFile>
#include <QTextStream>
#include <QFileDialog>
#include <QInputDialog>
#include <QDebug>
SpellCheckDialog::SpellCheckDialog(QWidget* parent)
: QDialog(parent)
, ui(new Ui::SpellCheckDialog)
, m_listChanged(false)
{
ui->setupUi(this);
ui->dictPath->setText(mApp->speller()->dictionaryPath());
QFile file(mApp->currentProfilePath() + "userdictionary.txt");
if (!file.open(QFile::ReadOnly)) {
qWarning() << "SpellCheckDialog: Cannot open file" << file.fileName() << "for reading!";
}
else {
QString word;
QTextStream stream(&file);
while (!stream.atEnd()) {
stream >> word;
word = word.trimmed();
if (!word.isEmpty()) {
ui->userDictList->insertItem(0, word);
}
}
file.close();
}
connect(ui->changeDictPath, SIGNAL(clicked()), this, SLOT(changeDictionaryPath()));
connect(ui->add, SIGNAL(clicked()), this, SLOT(addUserWord()));
connect(ui->remove, SIGNAL(clicked()), this, SLOT(removeUserWord()));
connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(saveSettings()));
ui->userDictList->setFocus();
}
void SpellCheckDialog::changeDictionaryPath()
{
const QString &path = QFileDialog::getExistingDirectory(this, tr("Choose dictionary path..."),
ui->dictPath->text());
if (!path.isEmpty()) {
ui->dictPath->setText(path);
}
}
void SpellCheckDialog::addUserWord()
{
const QString &word = QInputDialog::getText(0, tr("Add new word..."), tr("Add new word:"));
if (!word.isEmpty()) {
ui->userDictList->addItem(word);
ui->userDictList->setCurrentRow(ui->userDictList->count() - 1);
m_listChanged = true;
}
}
void SpellCheckDialog::removeUserWord()
{
QListWidgetItem* item = ui->userDictList->currentItem();
if (!item) {
return;
}
m_listChanged = true;
delete item;
}
void SpellCheckDialog::saveSettings()
{
// Save only when changed
if (ui->dictPath->text() != mApp->speller()->dictionaryPath()) {
Settings settings;
settings.beginGroup("SpellCheck");
settings.setValue("dictionaryPath", ui->dictPath->text());
settings.endGroup();
}
if (!m_listChanged) {
return;
}
QFile file(mApp->currentProfilePath() + "userdictionary.txt");
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
qWarning() << "SpellCheckDialog: Cannot open file" << file.fileName() << "for reading!";
return;
}
QTextStream stream(&file);
int count = ui->userDictList->count();
for (int i = 0; i < count; ++i) {
const QString &word = ui->userDictList->item(i)->text();
stream << word << endl;
}
file.close();
}
SpellCheckDialog::~SpellCheckDialog()
{
delete ui;
}

View File

@ -0,0 +1,49 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#ifndef SPELLCHECKDIALOG_H
#define SPELLCHECKDIALOG_H
#include <QDialog>
namespace Ui
{
class SpellCheckDialog;
}
class SpellCheckDialog : public QDialog
{
Q_OBJECT
public:
explicit SpellCheckDialog(QWidget* parent = 0);
~SpellCheckDialog();
private slots:
void changeDictionaryPath();
void addUserWord();
void removeUserWord();
void saveSettings();
private:
Ui::SpellCheckDialog* ui;
bool m_listChanged;
};
#endif // SPELLCHECKDIALOG_H

View File

@ -0,0 +1,161 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SpellCheckDialog</class>
<widget class="QDialog" name="SpellCheckDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>441</width>
<height>305</height>
</rect>
</property>
<property name="windowTitle">
<string>SpellCheck</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;Dictionary path&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="dictPath">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="changeDictPath">
<property name="text">
<string>Change...</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>&lt;b&gt;User dictionary&lt;/b&gt;</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QListWidget" name="userDictList"/>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="add">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="remove">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="textLabel">
<property name="text">
<string>Using Hunspell library</string>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>SpellCheckDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>SpellCheckDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -61,7 +61,10 @@ void Speller::loadSettings()
settings.endGroup();
m_userDictionary.setFileName(mApp->currentProfilePath() + "userdictionary.txt");
initialize();
if (m_enabled) {
initialize();
}
}
void Speller::initialize()
@ -112,9 +115,11 @@ Speller::Language Speller::language() const
return m_language;
}
QList<Speller::Language> Speller::availableLanguages() const
QList<Speller::Language> Speller::availableLanguages()
{
QList<Language> languages;
if (!m_availableLanguages.isEmpty()) {
return m_availableLanguages;
}
QDirIterator it(m_dictionaryPath, QStringList("*.dic"), QDir::Files);
@ -129,12 +134,12 @@ QList<Speller::Language> Speller::availableLanguages() const
lang.code = it.fileInfo().baseName();
lang.name = nameForLanguage(lang.code);
if (!languages.contains(lang)) {
languages.append(lang);
if (!m_availableLanguages.contains(lang)) {
m_availableLanguages.append(lang);
}
}
return languages;
return m_availableLanguages;
}
QString Speller::dictionaryPath() const
@ -142,20 +147,12 @@ QString Speller::dictionaryPath() const
return m_dictionaryPath;
}
void Speller::showSettings(QWidget* parent)
{
SpellCheckDialog dialog(parent);
if (dialog.exec() == QDialog::Accepted) {
loadSettings();
}
}
void Speller::populateContextMenu(QMenu* menu, const QWebHitTestResult &hitTest)
{
m_element = hitTest.element();
if (m_element.isNull()) {
if (!m_enabled || m_element.isNull() ||
m_element.attribute(QLatin1String("type")) == QLatin1String("password")) {
return;
}
@ -205,8 +202,8 @@ void Speller::addToDictionary()
return;
}
m_userDictionary.write(word.toUtf8());
m_userDictionary.write("\n");
QTextStream stream(&m_userDictionary);
stream << word << endl;
m_userDictionary.close();
}
}
@ -234,6 +231,33 @@ void Speller::replaceWord()
}
}
void Speller::showSettings()
{
SpellCheckDialog dialog;
if (dialog.exec() == QDialog::Accepted) {
loadSettings();
}
}
void Speller::changeLanguage()
{
QAction* act = qobject_cast<QAction*>(sender());
if (!act) {
return;
}
Language lang = act->data().value<Language>();
Settings settings;
settings.beginGroup("SpellCheck");
settings.setValue("language", lang.code);
settings.endGroup();
loadSettings();
}
void Speller::putWord(const QString &word)
{
if (!m_hunspell || !m_textCodec) {
@ -290,6 +314,43 @@ bool Speller::isValidWord(const QString &str)
return false;
}
void Speller::populateLanguagesMenu()
{
QMenu* menu = qobject_cast<QMenu*>(sender());
if (!menu || !menu->isEmpty()) {
return;
}
const QList<Language> langs = availableLanguages();
foreach(const Language & lang, langs) {
QAction* act = menu->addAction(lang.name, this, SLOT(changeLanguage()));
act->setCheckable(true);
act->setChecked(m_language == lang);
act->setData(QVariant::fromValue(lang));
}
if (menu->isEmpty()) {
QAction* act = menu->addAction(tr("Empty"));
act->setEnabled(false);
}
menu->addSeparator();
menu->addAction(tr("Settings"), this, SLOT(showSettings()));
}
void Speller::toggleEnableSpellChecking()
{
m_enabled = !m_enabled;
Settings settings;
settings.beginGroup("SpellCheck");
settings.setValue("enabled", m_enabled);
settings.endGroup();
loadSettings();
}
bool Speller::dictionaryExists(const QString &path) const
{
return QFile(path + ".dic").exists() &&

View File

@ -52,10 +52,9 @@ public:
void loadSettings();
Language language() const;
QList<Language> availableLanguages() const;
QList<Language> availableLanguages();
QString dictionaryPath() const;
void showSettings(QWidget* parent);
void populateContextMenu(QMenu* menu, const QWebHitTestResult &hitTest);
bool isMisspelled(const QString &string);
@ -63,10 +62,17 @@ public:
static bool isValidWord(const QString &str);
public slots:
void populateLanguagesMenu();
void toggleEnableSpellChecking();
private slots:
void addToDictionary();
void replaceWord();
void showSettings();
void changeLanguage();
private:
void initialize();
void putWord(const QString &word);
@ -81,6 +87,7 @@ private:
QFile m_userDictionary;
Language m_language;
QList<Language> m_availableLanguages;
bool m_enabled;
// Replacing word
@ -89,4 +96,6 @@ private:
int m_endPos;
};
Q_DECLARE_METATYPE(Speller::Language)
#endif // SPELLER_H

View File

@ -814,6 +814,8 @@ void WebView::createContextMenu(QMenu* menu, const QWebHitTestResult &hitTest, c
if (hitTest.element().tagName().toLower() == QLatin1String("input")) {
checkForForm(menu, hitTest.element());
}
createSpellCheckContextMenu(menu);
}
if (!selectedText().isEmpty()) {
@ -1030,6 +1032,22 @@ void WebView::createMediaContextMenu(QMenu* menu, const QWebHitTestResult &hitTe
menu->addAction(QIcon::fromTheme("document-save"), tr("Save Media To &Disk"), this, SLOT(downloadUrlToDisk()))->setData(videoUrl);
}
void WebView::createSpellCheckContextMenu(QMenu* menu)
{
menu->addSeparator();
QAction* act = menu->addAction(tr("Check &Spelling"), mApp->speller(), SLOT(toggleEnableSpellChecking()));
act->setCheckable(true);
act->setChecked(mApp->speller()->isEnabled());
if (mApp->speller()->isEnabled()) {
QMenu* men = menu->addMenu(tr("Languages"));
connect(men, SIGNAL(aboutToShow()), mApp->speller(), SLOT(populateLanguagesMenu()));
}
menu->addSeparator();
}
void WebView::pauseMedia()
{
bool paused = m_clickedElement.evaluateJavaScript("this.paused").toBool();

View File

@ -142,6 +142,7 @@ protected:
void createImageContextMenu(QMenu* menu, const QWebHitTestResult &hitTest);
void createSelectedTextContextMenu(QMenu* menu, const QWebHitTestResult &hitTest);
void createMediaContextMenu(QMenu* menu, const QWebHitTestResult &hitTest);
void createSpellCheckContextMenu(QMenu* menu);
private slots:
void pauseMedia();

View File

@ -4241,6 +4241,68 @@ After adding or removing certificate paths, it is neccessary to restart QupZilla
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>SpellCheckDialog</name>
<message>
<source>SpellCheck</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;Dictionary path&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Change...</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>&lt;b&gt;User dictionary&lt;/b&gt;</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Add</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Remove</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Using Hunspell library</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Choose dictionary path...</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Add new word...</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Add new word:</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>Speller</name>
<message>
<source>No suggestions</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Add to dictionary</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Empty</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Settings</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>SqueezeLabelV2</name>
<message>
@ -4817,6 +4879,14 @@ After adding or removing certificate paths, it is neccessary to restart QupZilla
<source>Create Search Engine</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Check &amp;Spelling</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Languages</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>jsAlert</name>