mainwindow.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*****************************************************************************
  2. * mainwindow.cpp - QStarDict, a StarDict clone written with using Qt *
  3. * Copyright (C) 2007-2009 Alexander *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License along *
  16. * with this program; if not, write to the Free Software Foundation, Inc., *
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  18. *****************************************************************************/
  19. #include "mainwindow.h"
  20. #include <QApplication>
  21. #include <QCloseEvent>
  22. #include <QDesktopServices>
  23. #include <QDir>
  24. #include <QFile>
  25. #include <QFileDialog>
  26. #include <QListWidgetItem>
  27. #include <QKeyEvent>
  28. #include <QMenu>
  29. #include <QMessageBox>
  30. #include <QSettings>
  31. #include <QTextStream>
  32. #include <QTimerEvent>
  33. #include <QToolBar>
  34. #include "dictcore.h"
  35. #include "application.h"
  36. #include "popupwindow.h"
  37. #include "settingsdialog.h"
  38. #include "trayicon.h"
  39. namespace QStarDict
  40. {
  41. MainWindow::MainWindow(QWidget *parent)
  42. : QMainWindow(parent)
  43. {
  44. setupUi(this);
  45. m_dict = 0;
  46. translationView->setDict(m_dict);
  47. menu_File->insertActions(actionQuit, translationView->toolBar()->actions());
  48. menu_Options->insertAction(menu_Options->actions().first(), wordsListDock->toggleViewAction());
  49. createConnections();
  50. loadSettings();
  51. }
  52. MainWindow::~MainWindow()
  53. {
  54. saveSettings();
  55. }
  56. void MainWindow::showTranslation(const QString &text)
  57. {
  58. searchBox->setText(text);
  59. on_queryButton_clicked();
  60. }
  61. void MainWindow::createConnections()
  62. {
  63. connect(actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  64. connect(actionQuit, SIGNAL(triggered()), qApp, SLOT(quit()));
  65. actionScan->setChecked(Application::instance()->popupWindow()->isScan());
  66. connect(actionScan, SIGNAL(toggled(bool)),
  67. Application::instance()->popupWindow(), SLOT(setScan(bool)));
  68. connect(Application::instance()->popupWindow(), SIGNAL(scanChanged(bool)),
  69. actionScan, SLOT(setChecked(bool)));
  70. connect(wordsList, SIGNAL(itemActivated(QListWidgetItem*)),
  71. SLOT(wordsListItemActivated(QListWidgetItem*)));
  72. connect(wordsList, SIGNAL(itemClicked(QListWidgetItem*)),
  73. SLOT(wordsListItemActivated(QListWidgetItem*)));
  74. connect(translationView, SIGNAL(wordTranslated(const QString&)),
  75. SLOT(wordTranslated(const QString&)));
  76. }
  77. void MainWindow::loadSettings()
  78. {
  79. QSettings config;
  80. restoreGeometry(config.value("MainWindow/geometry", QByteArray()).toByteArray());
  81. restoreState(config.value("MainWindow/state", QByteArray()).toByteArray());
  82. setVisible(config.value("MainWindow/visible", true).toBool());
  83. if (isHidden() && ! Application::instance()->trayIcon()->isVisible())
  84. show();
  85. wordsListDock->setFloating(config.value("MainWindow/wordsListDock/floating", wordsListDock->isFloating()).toBool());
  86. wordsListDock->setGeometry(config.value("MainWindow/wordsListDock/geometry", wordsListDock->geometry()).toRect());
  87. setInstantSearch(config.value("MainWindow/instantSearch", true).toBool());
  88. setDefaultStyleSheet(config.value("MainWindow/defaultStyleSheet", defaultStyleSheet()).toString());
  89. }
  90. void MainWindow::saveSettings()
  91. {
  92. QSettings config;
  93. config.setValue("MainWindow/geometry", saveGeometry());
  94. config.setValue("MainWindow/state", saveState());
  95. config.setValue("MainWindow/visible", isVisible());
  96. config.setValue("MainWindow/wordsListDock/floating", wordsListDock->isFloating());
  97. config.setValue("MainWindow/wordsListDock/geometry", wordsListDock->geometry());
  98. config.setValue("MainWindow/instantSearch", m_instantSearch);
  99. config.setValue("MainWindow/defaultStyleSheet", defaultStyleSheet());
  100. }
  101. void MainWindow::on_actionAbout_triggered()
  102. {
  103. QMessageBox::about(this,
  104. tr("About QStarDict"),
  105. tr("<b>QStarDict %1 </b> - Qt version of StarDict<br>").arg(QSTARDICT_VERSION) +
  106. tr("Copyright (C) 2007-2009 Alexander Rodin "
  107. "<a href=\"http://qstardict.ylsoftware.com\">http://qstardict.ylsoftware.com</a>"));
  108. }
  109. void MainWindow::on_actionSettings_triggered()
  110. {
  111. SettingsDialog dialog(this);
  112. dialog.exec();
  113. }
  114. void MainWindow::on_actionHelp_triggered()
  115. {
  116. QDesktopServices::openUrl(QUrl("http://wiki.qstardict.ylsoftware.com"));
  117. }
  118. void MainWindow::on_queryButton_clicked()
  119. {
  120. if (searchBox->text().simplified().isEmpty())
  121. {
  122. setWindowTitle(tr("QStarDict"));
  123. translationView->clear();
  124. wordsList->clear();
  125. return;
  126. }
  127. wordsList->clear();
  128. wordsList->addItems(m_dict->findSimilarWords(searchBox->text()));
  129. translationView->translate(searchBox->text());
  130. }
  131. void MainWindow::reload()
  132. {
  133. wordsList->clear();
  134. wordsList->addItems(m_dict->findSimilarWords(translationView->translatedWord()));
  135. translationView->reload();
  136. }
  137. void MainWindow::queryEdited(const QString &)
  138. {
  139. if (m_queryTimer)
  140. killTimer(m_queryTimer);
  141. m_queryTimer = startTimer(200);
  142. }
  143. void MainWindow::timerEvent(QTimerEvent *event)
  144. {
  145. if (event->timerId() == m_queryTimer)
  146. {
  147. killTimer(m_queryTimer);
  148. m_queryTimer = 0;
  149. on_queryButton_clicked();
  150. }
  151. else
  152. QMainWindow::timerEvent(event);
  153. }
  154. void MainWindow::wordTranslated(const QString &word)
  155. {
  156. if (word.simplified().isEmpty())
  157. setWindowTitle(tr("QStarDict"));
  158. else
  159. setWindowTitle(tr("%1 - QStarDict").arg(word));
  160. if (m_queryTimer)
  161. {
  162. killTimer(m_queryTimer);
  163. m_queryTimer = 0;
  164. }
  165. }
  166. void MainWindow::wordsListItemActivated(QListWidgetItem *item)
  167. {
  168. searchBox->setText(item->text());
  169. translationView->translate(item->text());
  170. setWindowTitle(tr("%1 - QStarDict").arg(translationView->translatedWord()));
  171. }
  172. void MainWindow::setInstantSearch(bool instantSearch)
  173. {
  174. if (instantSearch == m_instantSearch)
  175. return;
  176. m_instantSearch = instantSearch;
  177. if (m_instantSearch)
  178. connect(searchBox, SIGNAL(textEdited(const QString&)), SLOT(queryEdited(const QString&)));
  179. else
  180. disconnect(searchBox, SIGNAL(textEdited(const QString&)), this, SLOT(queryEdited(const QString&)));
  181. }
  182. void MainWindow::setDict(DictCore *dict)
  183. {
  184. m_dict = dict;
  185. translationView->setDict(dict);
  186. }
  187. void MainWindow::keyPressEvent(QKeyEvent *event)
  188. {
  189. if (event->text().size() || event->key() == Qt::Key_Escape)
  190. {
  191. if (! searchBox->hasFocus())
  192. {
  193. searchBox->setText(event->text());
  194. searchBox->setFocus(Qt::OtherFocusReason);
  195. }
  196. if (event->key() == Qt::Key_Escape)
  197. {
  198. if (searchBox->text().isEmpty())
  199. close();
  200. else
  201. searchBox->clear();
  202. }
  203. }
  204. QMainWindow::keyPressEvent(event);
  205. }
  206. void MainWindow::closeEvent(QCloseEvent *event)
  207. {
  208. if (! Application::instance()->trayIcon()->isVisible())
  209. Application::instance()->quit();
  210. QMainWindow::closeEvent(event);
  211. }
  212. }
  213. // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc