| 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 "UserphraseModel.h" |
| 21 | #include "UserphraseView.h" |
| 22 | |
| 23 | #include <QDebug> |
| 24 | |
| 25 | UserphraseView::UserphraseView(QWidget *parent) |
| 26 | :QListView{parent} |
| 27 | ,UserphraseDialog_{new UserphraseDialog{this}} |
| 28 | ,menu_(new UserphraseViewMenu{this}) |
| 29 | { |
| 30 | setupContextMenu(); |
| 31 | setupAddUserphraseDialog(); |
| 32 | // setup double click |
| 33 | connect( |
| 34 | this, SIGNAL(doubleClicked(const QModelIndex &)), |
| 35 | this, SLOT(showModifyUserphraseDialog()) |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | void UserphraseView::showAddUserphraseDialog() |
| 40 | { |
| 41 | dialogType_ = DIALOG_ADD; |
| 42 | UserphraseDialog_->setText("", ""); |
| 43 | UserphraseDialog_->setWindowTitle(tr("Add new phrase")); |
| 44 | emit UserphraseDialog_->exec(); |
| 45 | } |
| 46 | |
| 47 | void UserphraseView::showModifyUserphraseDialog() |
| 48 | { |
| 49 | if (selectionModel()->selectedIndexes().size() != 1) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | dialogType_ = DIALOG_MODIFY; |
| 54 | |
| 55 | auto userphrase = model()->getUserphrase(selectionModel()->selectedIndexes().first()); |
| 56 | |
| 57 | UserphraseDialog_->setText(userphrase->phrase_, userphrase->bopomofo_); |
| 58 | UserphraseDialog_->setWindowTitle(tr("Modify phrase")); |
| 59 | |
| 60 | originalphrase_ = userphrase->phrase_; |
| 61 | originalbopomofo_ = userphrase->bopomofo_; |
| 62 | |
| 63 | emit UserphraseDialog_->exec(); |
| 64 | } |
| 65 | |
| 66 | void UserphraseView::addPhrase(int result) |
| 67 | { |
| 68 | if (result != QDialog::Accepted) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | QString phrase = UserphraseDialog_->getPhrase(); |
| 73 | QString bopomofo = UserphraseDialog_->getBopomofo(); |
| 74 | |
| 75 | qDebug() << "Add" << phrase << "(" << bopomofo << ")"; |
| 76 | |
| 77 | if (dialogType_ == DIALOG_MODIFY) { |
| 78 | remove(); |
| 79 | } |
| 80 | |
| 81 | emit model()->add(phrase, bopomofo); |
| 82 | |
| 83 | if (dialogType_ == DIALOG_MODIFY) { |
| 84 | if (model()->sourceModel()->getAddResult() <= 0) { |
| 85 | emit model()->add(originalphrase_, originalbopomofo_); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void UserphraseView::remove() |
| 91 | { |
| 92 | auto selection = selectionModel(); |
| 93 | emit model()->remove(selection->selectedIndexes()); |
| 94 | selection->reset(); |
| 95 | } |
| 96 | |
| 97 | void UserphraseView::setFilterString(const QString& text) |
| 98 | { |
| 99 | model()->setFilterWildcard(text); |
| 100 | } |
| 101 | |
| 102 | void UserphraseView::setupContextMenu() |
| 103 | { |
| 104 | setContextMenuPolicy(Qt::CustomContextMenu); |
| 105 | |
| 106 | connect( |
| 107 | this, SIGNAL(customContextMenuRequested(const QPoint&)), |
| 108 | this, SLOT(showContextMenu(const QPoint&)) |
| 109 | ); |
| 110 | |
| 111 | connect( |
| 112 | menu_->getActionRemovePhrase(), SIGNAL(triggered()), |
| 113 | this, SLOT(remove()) |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | void UserphraseView::showContextMenu(const QPoint& point) |
| 118 | { |
| 119 | emit menu_->execWithSelectInfo(mapToGlobal(point), selectionModel()->selectedIndexes().size()); |
| 120 | } |
| 121 | |
| 122 | void UserphraseView::setupAddUserphraseDialog() |
| 123 | { |
| 124 | connect( |
| 125 | menu_->getActionAddPhrase(), SIGNAL(triggered()), |
| 126 | this, SLOT(showAddUserphraseDialog()) |
| 127 | ); |
| 128 | |
| 129 | connect( |
| 130 | menu_->getActionModifyPhrase(), SIGNAL(triggered()), |
| 131 | this, SLOT(showModifyUserphraseDialog()) |
| 132 | ); |
| 133 | |
| 134 | connect( |
| 135 | UserphraseDialog_, SIGNAL(finished(int)), |
| 136 | this, SLOT(addPhrase(int)) |
| 137 | ); |
| 138 | } |