123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- /*****************************************************************************
- * settingsdialog.cpp - QStarDict, a StarDict clone written with using Qt *
- * Copyright (C) 2007 Alexander Rodin *
- * *
- * 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 2 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, write to the Free Software Foundation, Inc., *
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
- *****************************************************************************/
- #include "settingsdialog.h"
- #include <QFileDialog>
- #include <QMessageBox>
- #include <QStandardItemModel>
- #include <QHeaderView>
- #include <QInputDialog>
- #include <QSettings>
- #include <math.h>
- #include "dictcore.h"
- #include "mainwindow.h"
- #include "popupwindow.h"
- #include "application.h"
- #include "speaker.h"
- #include "trayicon.h"
- namespace
- {
- int toPercents(double value)
- {
- int integralValue = static_cast<int>(value * 100.0);
- if (value * 100.0 - 0.5 > integralValue)
- ++integralValue;
- return integralValue;
- }
- }
- namespace QStarDict {
- SettingsDialog::SettingsDialog(QWidget *parent)
- : QDialog(parent)
- {
- setupUi(this);
- DictCore *dict = Application::instance()->dictCore();
- m_oldPlugins = dict->loadedPlugins();
- m_oldDicts = dict->loadedDicts();
- m_pluginsModel = new QStandardItemModel(this);
- m_pluginsModel->setHorizontalHeaderLabels(
- QStringList() << tr("Enabled") << tr("Name"));
- loadPluginsList();
- pluginsTableView->setModel(m_pluginsModel);
- pluginsTableView->verticalHeader()->hide();
- pluginsTableView->setColumnWidth(0, 60);
- pluginsTableView->setColumnWidth(1, 320);
- m_dictsModel = new QStandardItemModel(this);
- m_dictsModel->setHorizontalHeaderLabels(QStringList() << tr("Enabled") << tr("Name") << tr("Plugin"));
- loadDictsList();
- dictsTableView->setModel(m_dictsModel);
- dictsTableView->verticalHeader()->hide();
- dictsTableView->setColumnWidth(0, 60);
- dictsTableView->setColumnWidth(1, 200);
- dictsTableView->setColumnWidth(2, 120);
- // Load global settings
- systemTrayBox->setChecked(Application::instance()->trayIcon()->isVisible());
- instantSearchBox->setChecked(Application::instance()->mainWindow()->isInstantSearch());
- speechCmdEdit->setText(Application::instance()->speaker()->speechCmd());
- #ifdef Q_OS_LINUX
- QFile desktop(QDir::homePath() + "/.config/autostart/qstardict.desktop");
- if (desktop.open(QIODevice::ReadOnly) && QString(desktop.readAll())
- .contains(QRegExp("\\bhidden\\s*=\\s*false", Qt::CaseInsensitive))) {
- autostartBox->setChecked(true);
- }
- #elif defined(Q_OS_WIN)
- QSettings reg("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\"
- "CurrentVersion\\Run", QSettings::NativeFormat);
- autostartBox->setChecked(
- reg.contains(QCoreApplication::applicationName()));
- #else
- autostartBox->setVisible(false);
- #endif
- // Load popup window settings
- PopupWindow *popup = Application::instance()->popupWindow();
- useScanBox->setChecked(popup->isScan());
- if (popup->modifierKey())
- {
- useScanModifierBox->setChecked(true);
- QString modifierName;
- switch (popup->modifierKey())
- {
- case Qt::ShiftModifier:
- modifierName = "Shift";
- break;
- case Qt::ControlModifier:
- modifierName = "Control";
- break;
- case Qt::AltModifier:
- modifierName = "Alt";
- break;
- case Qt::MetaModifier:
- modifierName = "Win";
- break;
- }
- modifierKeyBox->setCurrentIndex(modifierKeyBox->findText(modifierName));
- }
- showIfNotFoundBox->setChecked(popup->showIfNotFound());
- popupOpacitySpin->setValue(toPercents(popup->windowOpacity()));
- timeoutBeforeHideSpin->setValue(popup->timeoutBeforeHide() / 1000.0);
- popupDefaultWidthSpin->setValue(popup->defaultSize().width());
- popupDefaultHeightSpin->setValue(popup->defaultSize().height());
- pronounceWordBox->setChecked(popup->pronounceWord());
- // Load translations CSS
- QHash<QString, QString> cssAliases;
- cssAliases["body"] = tr("All translation");
- cssAliases["font.dict_name"] = tr("Dictionary name");
- cssAliases["font.title"] = tr("Title");
- cssAliases["font.explanation"] = tr("Explanation");
- cssAliases["font.abbreviature"] = tr("Abbreviation");
- cssAliases["font.example"] = tr("Example");
- cssAliases["font.transcription"] = tr("Transcription");
- appearanceCSSEdit->setElementsAliases(cssAliases);
- appearanceCSSEdit->setCSS(Application::instance()->mainWindow()->defaultStyleSheet());
- connect(m_pluginsModel, SIGNAL(itemChanged(QStandardItem*)),
- SLOT(pluginsItemChanged(QStandardItem*)));
- }
- void SettingsDialog::accept()
- {
- // Save dicts and plugins settings
- DictCore *dict = Application::instance()->dictCore();
- QStringList loadedPlugins;
- int rowCount = m_pluginsModel->rowCount();
- for (int i = 0; i < rowCount; ++i)
- if (m_pluginsModel->item(i, 0)->checkState() == Qt::Checked)
- loadedPlugins << m_pluginsModel->item(i, 1)->text();
- dict->setLoadedPlugins(loadedPlugins);
- QList<DictCore::Dictionary> loadedDicts;
- rowCount = m_dictsModel->rowCount();
- for (int i = 0; i < rowCount; ++i)
- if (m_dictsModel->item(i, 0)->checkState() == Qt::Checked)
- loadedDicts << DictCore::Dictionary(m_dictsModel->item(i, 2)->text(), m_dictsModel->item(i, 1)->text());
- dict->setLoadedDicts(loadedDicts);
- // Save global settings
- Application::instance()->trayIcon()->setVisible(systemTrayBox->isChecked());
- Application::instance()->mainWindow()->setInstantSearch(instantSearchBox->isChecked());
- Application::instance()->speaker()->setSpeechCmd(speechCmdEdit->text());
- #ifdef Q_OS_LINUX
- QDir home = QDir::home();
- if (!home.exists(".config/autostart")) {
- home.mkpath(".config/autostart");
- }
- QFile desktopFile(QSTARDICT_INSTALL_PREFIX "/share/applications/qstardict.desktop");
- if (desktopFile.open(QIODevice::ReadOnly)) {
- QByteArray contents = desktopFile.readAll();
- QFile f(home.absolutePath() +
- "/.config/autostart/qstardict.desktop");
- if (f.open(QIODevice::WriteOnly | QIODevice::Text)) {
- f.write(contents.trimmed());
- f.write(QString("\nHidden=%1").arg(autostartBox->isChecked()?
- "false\n":"true\n").toUtf8());
- }
- }
- #elif defined(Q_OS_WIN)
- QSettings reg("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\"
- "CurrentVersion\\Run", QSettings::NativeFormat);
- if(autostartBox->isChecked())
- reg.setValue(QCoreApplication::applicationName(), '"' +
- QDir::toNativeSeparators(QCoreApplication::
- applicationFilePath()) + '"');
- else
- reg.remove(QCoreApplication::applicationName());
- #endif
- // Save popup window settings
- PopupWindow *popup = Application::instance()->popupWindow();
- popup->setScan(useScanBox->isChecked());
- int modifierKey = 0;
- if (useScanModifierBox->isChecked())
- {
- if (modifierKeyBox->currentText() == "Shift")
- modifierKey = Qt::ShiftModifier;
- else if (modifierKeyBox->currentText() == "Control")
- modifierKey = Qt::ControlModifier;
- else if (modifierKeyBox->currentText() == "Alt")
- modifierKey = Qt::AltModifier;
- else if (modifierKeyBox->currentText() == "Win")
- modifierKey = Qt::MetaModifier;
- }
- popup->setShowIfNotFound(showIfNotFoundBox->isChecked());
- popup->setModifierKey(modifierKey);
- popup->setWindowOpacity(popupOpacitySpin->value() / 100.0);
- popup->setTimeoutBeforeHide(static_cast<int>(timeoutBeforeHideSpin->value() * 1000.0));
- popup->setDefaultSize(QSize(popupDefaultWidthSpin->value(), popupDefaultHeightSpin->value()));
- popup->setPronounceWord(pronounceWordBox->isChecked());
- // Save translations CSS
- Application::instance()->mainWindow()->setDefaultStyleSheet(appearanceCSSEdit->css());
- Application::instance()->popupWindow()->setDefaultStyleSheet(appearanceCSSEdit->css());
- if (! Application::instance()->trayIcon()->isVisible())
- Application::instance()->mainWindow()->show();
- Application::instance()->mainWindow()->reload();
- Application::instance()->dictCore()->saveSettings();
- Application::instance()->mainWindow()->saveSettings();
- Application::instance()->trayIcon()->saveSettings();
- Application::instance()->popupWindow()->saveSettings();
- QDialog::accept();
- }
- void SettingsDialog::reject()
- {
- DictCore *dict = Application::instance()->dictCore();
- dict->setLoadedPlugins(m_oldPlugins);
- dict->setLoadedDicts(m_oldDicts);
- QDialog::reject();
- }
- void SettingsDialog::loadDictsList()
- {
- int i;
- QList<DictCore::Dictionary> loadedDicts = Application::instance()->dictCore()->loadedDicts();
- m_dictsModel->setRowCount(0);
- for (i = 0; i < loadedDicts.size(); ++i)
- {
- QStandardItem *item = new QStandardItem();
- item->setCheckable(true);
- item->setCheckState(Qt::Checked);
- m_dictsModel->setItem(i, 0, item);
- m_dictsModel->setItem(i, 1, new QStandardItem(loadedDicts[i].name()));
- m_dictsModel->setItem(i, 2, new QStandardItem(loadedDicts[i].plugin()));
- }
- QList<DictCore::Dictionary> dicts = Application::instance()->dictCore()->availableDicts();
- for (QList<DictCore::Dictionary>::const_iterator iter = dicts.begin(); iter != dicts.end(); ++iter)
- {
- if (! loadedDicts.contains(*iter))
- {
- QStandardItem *item = new QStandardItem();
- item->setCheckable(true);
- item->setCheckState(Qt::Unchecked);
- m_dictsModel->setItem(i, 0, item);
- m_dictsModel->setItem(i, 1, new QStandardItem(iter->name()));
- m_dictsModel->setItem(i, 2, new QStandardItem(iter->plugin()));
- ++i;
- }
- }
- }
- void SettingsDialog::loadPluginsList()
- {
- m_pluginsModel->setRowCount(0);
- DictCore *dict = Application::instance()->dictCore();
- QStringList plugins = dict->availablePlugins();
- QStringList loaded = dict->loadedPlugins();
- for (int i = 0; i < plugins.size(); ++i)
- {
- QStandardItem *item = new QStandardItem();
- item->setCheckable(true);
- item->setCheckState(loaded.contains(plugins[i]) ? Qt::Checked : Qt::Unchecked);
- m_pluginsModel->setItem(i, 0, item);
- m_pluginsModel->setItem(i, 1, new QStandardItem(plugins[i]));
- }
- }
- void SettingsDialog::on_dictsMoveUpButton_clicked()
- {
- int currentRow = dictsTableView->currentIndex().row();
- if (currentRow > 0)
- {
- m_dictsModel->insertRow(currentRow - 1, m_dictsModel->takeRow(currentRow));
- dictsTableView->selectRow(currentRow - 1);
- }
- }
- void SettingsDialog::on_dictsMoveDownButton_clicked()
- {
- int currentRow = dictsTableView->currentIndex().row();
- if (currentRow < m_dictsModel->rowCount() - 1)
- {
- m_dictsModel->insertRow(currentRow + 1, m_dictsModel->takeRow(currentRow));
- dictsTableView->selectRow(currentRow + 1);
- }
- }
- void SettingsDialog::on_dictsShowInfoButton_clicked()
- {
- int currentRow = dictsTableView->currentIndex().row();
- if (currentRow == -1)
- return;
- QString dict = m_dictsModel->item(currentRow, 1)->text();
- QString plugin = m_dictsModel->item(currentRow, 2)->text();
- DictPlugin::DictInfo info = Application::instance()->dictCore()->plugin(plugin)->dictInfo(dict);
- QMessageBox::information(this,
- tr("Information about dictionary \"%1\"").arg(dict),
- tr("<b>Name:</b> %1<br>").arg(dict) +
- tr("<b>Plugin:</b> %1<br>").arg(plugin) +
- tr("<b>Author:</b> %1<br>").arg(info.author()) +
- tr("<b>Words count:</b> %1<br>").arg((info.wordsCount() == -1) ? tr("unknown") : QString::number(info.wordsCount())) +
- tr("<b>Description:</b> %1").arg(info.description()));
- }
- void SettingsDialog::on_pluginsShowInfoButton_clicked()
- {
- int currentRow = pluginsTableView->currentIndex().row();
- if (currentRow == -1)
- return;
- DictPlugin *plugin = Application::instance()->dictCore()->plugin(m_pluginsModel->item(currentRow, 1)->text());
- if (! plugin)
- return;
- QMessageBox::information(this,
- tr("Information about %1 plugin").arg(plugin->name()),
- tr("<b>Name:</b> %1<br>").arg(plugin->name()) +
- tr("<b>Version:</b> %1<br>").arg(plugin->version()) +
- tr("<b>Authors:</b> %1<br>").arg(plugin->authors().replaceInStrings("<", "<").replaceInStrings(">", ">").join(tr("<br>"))) +
- tr("<b>Can search similar words:</b> %1<br>").arg(plugin->features().testFlag(DictPlugin::SearchSimilar) ? tr("yes") : tr("no")) +
- tr("<b>Description:</b> %1").arg(plugin->description()));
- }
- void SettingsDialog::on_pluginsConfigureButton_clicked()
- {
- int currentRow = pluginsTableView->currentIndex().row();
- if (currentRow == -1)
- return;
- DictCore *dict = Application::instance()->dictCore();
- DictPlugin *plugin = dict->plugin(m_pluginsModel->item(currentRow, 1)->text());
- if (plugin && plugin->execSettingsDialog(this) == QDialog::Accepted)
- {
- dict->reloadDicts();
- loadDictsList();
- }
- }
- void SettingsDialog::pluginsItemChanged(QStandardItem *item)
- {
- if (item->isCheckable())
- {
- DictCore *dict = Application::instance()->dictCore();
- QStringList loadedPlugins;
- int rowCount = m_pluginsModel->rowCount();
- for (int i = 0; i < rowCount; ++i)
- if (m_pluginsModel->item(i, 0)->checkState() == Qt::Checked)
- loadedPlugins << m_pluginsModel->item(i, 1)->text();
- dict->setLoadedPlugins(loadedPlugins);
- dict->reloadDicts();
- loadDictsList();
- }
- }
- }
- // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
|