| 1 | /* |
| 2 | * chewing-editor: Chewing userphrase editor |
| 3 | * Copyright (C) 2014 Chewing Development Team |
| 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 | |
| 20 | #include "ChewingEditor.h" |
| 21 | #include "ui_ChewingEditor.h" |
| 22 | #include "config.h" |
| 23 | |
| 24 | #include <QtGui> |
| 25 | #include <QDebug> |
| 26 | #include <QMessageBox> |
| 27 | |
| 28 | #include "ChewingImporter.h" |
| 29 | #include "ChewingExporter.h" |
| 30 | |
| 31 | static int import_count; |
| 32 | |
| 33 | ChewingEditor::ChewingEditor(QWidget *parent) |
| 34 | :QMainWindow{parent} |
| 35 | ,ui_{new Ui::ChewingEditor} |
| 36 | ,model_{new UserphraseModel{this}} |
| 37 | ,proxyModel_{new UserphraseSortFilterProxyModel{this}} |
| 38 | ,fileDialog_{new QFileDialog{this}} |
| 39 | { |
| 40 | ui_.get()->setupUi(this); |
| 41 | |
| 42 | proxyModel_->setSourceModel(model_); |
| 43 | ui_.get()->userphraseView->setModel(proxyModel_); |
| 44 | |
| 45 | setupFileSelection(); |
| 46 | setupImport(); |
| 47 | setupExport(); |
| 48 | setupAdd(); |
| 49 | setupRemove(); |
| 50 | setupRefresh(); |
| 51 | setupUndo(); |
| 52 | setupFilter(); |
| 53 | setupAboutWidget(); |
| 54 | |
| 55 | // exit action |
| 56 | shortcut_exit_ = new QShortcut(QKeySequence::Quit, this); |
| 57 | connect( |
| 58 | shortcut_exit_, SIGNAL(activated()), |
| 59 | SLOT(close()) |
| 60 | ); |
| 61 | connect( |
| 62 | ui_.get()->actionExit, SIGNAL(triggered()), |
| 63 | SLOT(close()) |
| 64 | ); |
| 65 | |
| 66 | // modify action |
| 67 | shortcut_modify_ = new QShortcut(Qt::Key_Return, this); |
| 68 | connect( |
| 69 | shortcut_modify_, SIGNAL(activated()), |
| 70 | ui_.get()->userphraseView, SLOT(showModifyUserphraseDialog()) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | ChewingEditor::~ChewingEditor() |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | void ChewingEditor::execFileDialog(DialogType type) |
| 79 | { |
| 80 | dialogType_ = type; |
| 81 | |
| 82 | qDebug() << "dialogType_ = " << dialogType_; |
| 83 | |
| 84 | switch (dialogType_) { |
| 85 | case DIALOG_IMPORT: |
| 86 | fileDialog_->setWindowTitle(tr("Import")); |
| 87 | fileDialog_->setAcceptMode(QFileDialog::AcceptOpen); |
| 88 | fileDialog_->setFileMode(QFileDialog::ExistingFile); |
| 89 | fileDialog_->setOption(QFileDialog::DontConfirmOverwrite, true); |
| 90 | fileDialog_->selectFile(""); |
| 91 | break; |
| 92 | |
| 93 | case DIALOG_EXPORT: |
| 94 | fileDialog_->setWindowTitle(tr("Export")); |
| 95 | fileDialog_->setAcceptMode(QFileDialog::AcceptSave); |
| 96 | fileDialog_->setFileMode(QFileDialog::AnyFile); |
| 97 | fileDialog_->setOption(QFileDialog::DontConfirmOverwrite, false); |
| 98 | fileDialog_->selectFile("chewing.json"); |
| 99 | break; |
| 100 | |
| 101 | default: |
| 102 | Q_ASSERT(!"Unknown DialogType"); |
| 103 | } |
| 104 | |
| 105 | emit fileDialog_->exec(); |
| 106 | } |
| 107 | |
| 108 | void ChewingEditor::selectImportFile() |
| 109 | { |
| 110 | // reset import_count |
| 111 | import_count = 0; |
| 112 | qDebug() << "import_count = " << import_count; |
| 113 | execFileDialog(DIALOG_IMPORT); |
| 114 | } |
| 115 | |
| 116 | void ChewingEditor::selectExportFile() |
| 117 | { |
| 118 | execFileDialog(DIALOG_EXPORT); |
| 119 | } |
| 120 | |
| 121 | void ChewingEditor::finishFileSelection(const QString& file) |
| 122 | { |
| 123 | qDebug() << "dialogType_ = " << dialogType_; |
| 124 | |
| 125 | switch (dialogType_) { |
| 126 | case DIALOG_IMPORT: |
| 127 | // Do nothing after the first trigger |
| 128 | if (import_count == 0) { |
| 129 | importUserphrase(file); |
| 130 | import_count++; |
| 131 | } |
| 132 | |
| 133 | qDebug() << "import_count = " << import_count; |
| 134 | |
| 135 | break; |
| 136 | |
| 137 | case DIALOG_EXPORT: |
| 138 | exportUserphrase(file); |
| 139 | break; |
| 140 | |
| 141 | default: |
| 142 | Q_ASSERT(!"Unknown DialogType"); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void ChewingEditor::importUserphrase(const QString& file) |
| 147 | { |
| 148 | emit model_->importUserphrase(createUserphraseImporter(file)); |
| 149 | } |
| 150 | |
| 151 | void ChewingEditor::exportUserphrase(const QString& file) |
| 152 | { |
| 153 | // TODO: Find a suitable exporter |
| 154 | std::shared_ptr<ChewingExporter> exporter(new ChewingExporter{file}); |
| 155 | emit model_->exportUserphrase(exporter); |
| 156 | } |
| 157 | |
| 158 | void ChewingEditor::showAbout() |
| 159 | { |
| 160 | QString caption = QString("<h3>%1</h3><p>%2</p>") |
| 161 | .arg(tr("About Chewing Editor")) |
| 162 | #ifdef PROJECT_GIT_VERSION |
| 163 | .arg(tr("Version %1 (%2).").arg(PROJECT_VERSION).arg(PROJECT_GIT_VERSION)); |
| 164 | #else |
| 165 | .arg(tr("Version %1.").arg(PROJECT_VERSION)); |
| 166 | #endif |
| 167 | QString text = QString("<p style=\"white-space: pre-wrap;\">%1\n\n%2\n\n%3\n\n%4\n\n%5\n\n%6</p>") |
| 168 | .arg("Chewing editor is a cross platform Chewing user phrase editor. " |
| 169 | "It provides a easy way to manage user phrase. With it, user can " |
| 170 | "customize their user phrase to increase input performance.") |
| 171 | .arg("Chewing editor is licensed under the GNU GPL version 2.0 or later version.") |
| 172 | .arg("Please see <a href=\"https://github.com/chewing/chewing-editor/blob/master/COPYING\">COPYING</a> " |
| 173 | "for detail of Chewing editor licensing.") |
| 174 | .arg("Copyright© 2004-2016 by Chewing Core Team and contributors on " |
| 175 | "<a href=\"https://github.com/chewing/chewing-editor/graphs/contributors\">GitHub</a>.") |
| 176 | .arg("Chewing editor is developed as an open source project on " |
| 177 | "<a href=\"https://github.com/chewing/chewing-editor\">https://github.com/chewing/chewing-editor</a>.") |
| 178 | .arg("Any suggestions are welcomed on " |
| 179 | "<a href=\"https://github.com/chewing/chewing-editor/issues\">https://github.com/chewing/chewing-editor/issues</a>"); |
| 180 | QMessageBox aboutBox(this); |
| 181 | aboutBox.setWindowTitle(tr("About Chewing Editor")); |
| 182 | aboutBox.setText(caption); |
| 183 | aboutBox.setInformativeText(text); |
| 184 | aboutBox.setStandardButtons(QMessageBox::Ok); |
| 185 | aboutBox.setDefaultButton(QMessageBox::Ok); |
| 186 | |
| 187 | QPixmap pm(QLatin1String(":/chewing-editor.png")); |
| 188 | if (!pm.isNull()) |
| 189 | aboutBox.setIconPixmap(pm); |
| 190 | |
| 191 | aboutBox.exec(); |
| 192 | } |
| 193 | |
| 194 | void ChewingEditor::showDeleteConfirmWindow() |
| 195 | { |
| 196 | if(ui_.get()->userphraseView->selectionModel()->selectedIndexes().size() == 0) return; |
| 197 | |
| 198 | QString text = tr("Do you want to delete this phrase?"); |
| 199 | |
| 200 | QMessageBox deleteBox(this); |
| 201 | deleteBox.setWindowTitle(tr("Delete phrase")); |
| 202 | deleteBox.setText(text); |
| 203 | deleteBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); |
| 204 | deleteBox.setDefaultButton(QMessageBox::No); |
| 205 | |
| 206 | connect( |
| 207 | deleteBox.button(QMessageBox::Yes), SIGNAL(clicked()), |
| 208 | ui_.get()->userphraseView, SLOT(remove()) |
| 209 | ); |
| 210 | |
| 211 | deleteBox.exec(); |
| 212 | } |
| 213 | |
| 214 | void ChewingEditor::setupFileSelection() |
| 215 | { |
| 216 | fileDialog_->setNameFilter("*.json"); |
| 217 | |
| 218 | connect( |
| 219 | fileDialog_, SIGNAL(fileSelected(const QString&)), |
| 220 | this, SLOT(finishFileSelection(const QString &)) |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | void ChewingEditor::setupImport() |
| 225 | { |
| 226 | shortcut_import_ = new QShortcut(Qt::CTRL + Qt::Key_I, this); |
| 227 | connect( |
| 228 | shortcut_import_, SIGNAL(activated()), |
| 229 | this, SLOT(selectImportFile()) |
| 230 | ); |
| 231 | |
| 232 | connect( |
| 233 | ui_.get()->actionImport, SIGNAL(triggered()), |
| 234 | this, SLOT(selectImportFile()) |
| 235 | ); |
| 236 | |
| 237 | connect( |
| 238 | model_, SIGNAL(importCompleted(bool, const QString&, size_t, size_t)), |
| 239 | ui_.get()->notification, SLOT(notifyImportCompleted(bool, const QString&, size_t, size_t)) |
| 240 | ); |
| 241 | |
| 242 | // initialize import_count |
| 243 | import_count = 0; |
| 244 | qDebug() << "import_count = " << import_count; |
| 245 | } |
| 246 | |
| 247 | void ChewingEditor::setupExport() |
| 248 | { |
| 249 | shortcut_export_ = new QShortcut(Qt::CTRL + Qt::Key_E, this); |
| 250 | connect( |
| 251 | shortcut_export_, SIGNAL(activated()), |
| 252 | this, SLOT(selectExportFile()) |
| 253 | ); |
| 254 | |
| 255 | connect( |
| 256 | ui_.get()->actionExport, SIGNAL(triggered()), |
| 257 | this, SLOT(selectExportFile()) |
| 258 | ); |
| 259 | |
| 260 | connect( |
| 261 | model_, SIGNAL(exportCompleted(bool, const QString&, size_t)), |
| 262 | ui_.get()->notification, SLOT(notifyExportCompleted(bool, const QString&, size_t)) |
| 263 | ); |
| 264 | } |
| 265 | |
| 266 | void ChewingEditor::setupAdd() |
| 267 | { |
| 268 | shortcut_add_ = new QShortcut(QKeySequence::New, this); |
| 269 | connect( |
| 270 | shortcut_add_, SIGNAL(activated()), |
| 271 | ui_.get()->userphraseView, SLOT(showAddUserphraseDialog()) |
| 272 | ); |
| 273 | |
| 274 | connect( |
| 275 | ui_.get()->addButton, SIGNAL(pressed()), |
| 276 | ui_.get()->userphraseView, SLOT(showAddUserphraseDialog()) |
| 277 | ); |
| 278 | |
| 279 | connect( |
| 280 | model_, SIGNAL(addNewPhraseCompleted(const Userphrase&)), |
| 281 | ui_.get()->notification, SLOT(notifyAddNewPhraseCompleted(const Userphrase&)) |
| 282 | ); |
| 283 | |
| 284 | connect( |
| 285 | model_, SIGNAL(addNewPhraseFailed()), |
| 286 | ui_.get()->notification, SLOT(notifyAddNewPhraseFailed()) |
| 287 | ); |
| 288 | } |
| 289 | |
| 290 | void ChewingEditor::setupRemove() |
| 291 | { |
| 292 | shortcut_remove_ = new QShortcut(QKeySequence::Delete, this); |
| 293 | connect( |
| 294 | shortcut_remove_, SIGNAL(activated()), |
| 295 | this, SLOT(showDeleteConfirmWindow()) |
| 296 | ); |
| 297 | |
| 298 | connect( |
| 299 | ui_.get()->removeButton, SIGNAL(pressed()), |
| 300 | this, SLOT(showDeleteConfirmWindow()) |
| 301 | ); |
| 302 | |
| 303 | connect( |
| 304 | model_, SIGNAL(removePhraseCompleted(size_t)), |
| 305 | ui_.get()->notification, SLOT(notifyRemovePhraseCompleted(size_t)) |
| 306 | ); |
| 307 | } |
| 308 | |
| 309 | void ChewingEditor::setupRefresh() |
| 310 | { |
| 311 | shortcut_refresh_ = new QShortcut(Qt::CTRL + Qt::Key_R, this); |
| 312 | connect( |
| 313 | shortcut_refresh_, SIGNAL(activated()), |
| 314 | model_, SLOT(refresh()) |
| 315 | ); |
| 316 | |
| 317 | connect( |
| 318 | ui_.get()->refreshButton, SIGNAL(pressed()), |
| 319 | model_, SLOT(refresh()) |
| 320 | ); |
| 321 | |
| 322 | connect( |
| 323 | model_, SIGNAL(refreshCompleted(size_t)), |
| 324 | ui_.get()->notification, SLOT(notifyRefreshCompleted(size_t)) |
| 325 | ); |
| 326 | |
| 327 | emit model_->refresh(); |
| 328 | } |
| 329 | |
| 330 | void ChewingEditor::setupUndo() |
| 331 | { |
| 332 | connect( |
| 333 | ui_.get()->undoButton, SIGNAL(pressed()), |
| 334 | model_, SLOT(undo()) |
| 335 | ); |
| 336 | |
| 337 | connect( |
| 338 | model_, SIGNAL(undoCompleted(const QString&)), |
| 339 | ui_.get()->notification, SLOT(notifyUndoCompleted(const QString&)) |
| 340 | ); |
| 341 | } |
| 342 | |
| 343 | void ChewingEditor::setupFilter() |
| 344 | { |
| 345 | connect( |
| 346 | ui_.get()->userphraseFilter, SIGNAL(textEdited(const QString&)), |
| 347 | ui_.get()->userphraseView, SLOT(setFilterString(const QString&)) |
| 348 | ); |
| 349 | } |
| 350 | |
| 351 | void ChewingEditor::setupAboutWidget() |
| 352 | { |
| 353 | connect( |
| 354 | ui_.get()->actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()) |
| 355 | ); |
| 356 | |
| 357 | connect( |
| 358 | ui_.get()->actionAbout, SIGNAL(triggered()), this, SLOT(showAbout()) |
| 359 | ); |
| 360 | } |