Skip to content

Commit

Permalink
Repair the mechanism of modify phrase in UserphraseView
Browse files Browse the repository at this point in the history
The phrase can be preserved when the "add" step in "modify" is failed.

After a new phrase is added, the selection of the original phrase in
chewing-editor seems to be reset, then the remove() in UserphaseView
will lost the remove target.
So I check the modified phrase add to the database successfully or not,
If the adding is fail, then re-add the original phrase.

Resolves: chewing#75
See also: chewing#109
  • Loading branch information
hchung authored and hchung committed Mar 26, 2016
1 parent e342e21 commit fbe5896
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/model/UserphraseModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ void UserphraseModel::add(const QString &phrase, const QString &bopomofo)
phrase.toUtf8().constData(),
bopomofo.toUtf8().constData());

addResult = ret;

if (ret > 0) {
emit beginResetModel();
userphrase_.insert(Userphrase{
Expand Down
2 changes: 2 additions & 0 deletions src/model/UserphraseModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class UserphraseModel final: public QAbstractListModel {

const Userphrase *getUserphrase(const QModelIndex& idx);

int getAddResult(){return addResult;}
signals:
void importCompleted(
bool result,
Expand All @@ -71,4 +72,5 @@ public slots:

std::unique_ptr<ChewingContext, void (*)(ChewingContext*)> ctx_;
UserphraseSet userphrase_;
int addResult;
};
5 changes: 5 additions & 0 deletions src/model/UserphraseSortFilterProxyModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ const Userphrase *UserphraseSortFilterProxyModel::getUserphrase(const QModelInde
{
return sourceModel()->getUserphrase(mapToSource(idx));
}

int UserphraseSortFilterProxyModel::getAddResult()
{
return sourceModel()->getAddResult();
}
1 change: 1 addition & 0 deletions src/model/UserphraseSortFilterProxyModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class UserphraseSortFilterProxyModel final : public QSortFilterProxyModel {

const Userphrase *getUserphrase(const QModelIndex& idx);

int getAddResult();
public slots:
void add(std::shared_ptr<QString> phrase, std::shared_ptr<QString> bopomofo);
void remove(QModelIndexList indexList);
Expand Down
14 changes: 12 additions & 2 deletions src/view/UserphraseView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "UserphraseModel.h"
#include "UserphraseView.h"

#include <QDebug>
Expand All @@ -26,6 +25,8 @@ UserphraseView::UserphraseView(QWidget *parent)
:QListView{parent}
,UserphraseDialog_{new UserphraseDialog{this}}
,menu_(new UserphraseViewMenu{this})
,originalPhrase{new QString()}
,originalBopomofo{new QString()}
{
setupContextMenu();
setupAddUserphraseDialog();
Expand Down Expand Up @@ -53,9 +54,12 @@ void UserphraseView::showModifyUserphraseDialog()
dialogType_ = DIALOG_MODIFY;

auto userphrase = model()->getUserphrase(selectionModel()->selectedIndexes().first());

UserphraseDialog_->setText(userphrase->phrase_, userphrase->bopomofo_);
UserphraseDialog_->setWindowTitle(tr("Modify phrase"));

*originalPhrase = UserphraseDialog_->getPhrase();
*originalBopomofo = UserphraseDialog_->getBopomofo();

emit UserphraseDialog_->exec();
}

Expand All @@ -75,6 +79,12 @@ void UserphraseView::addPhrase(int result)
}

emit model()->add(phrase, bopomofo);

if (dialogType_ == DIALOG_MODIFY) {
if(model()->getAddResult() <= 0) {
emit model()->add(originalPhrase, originalBopomofo);
}
}
}

void UserphraseView::remove()
Expand Down
3 changes: 3 additions & 0 deletions src/view/UserphraseView.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@ public slots:
UserphraseViewMenu *menu_;

bool dialogType_;

std::shared_ptr<QString> originalPhrase;
std::shared_ptr<QString> originalBopomofo;
};

0 comments on commit fbe5896

Please sign in to comment.