Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let undo can restore multiple pharses at one time #233

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/model/UserphraseModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ void UserphraseModel::remove(QModelIndexList indexList)
auto last = indexList.first().row();

emit beginRemoveRows(indexList.first().parent(), first, last);


jserv marked this conversation as resolved.
Show resolved Hide resolved
int remove_cnt = 0;
foreach(auto item, indexList) {
auto index = item.row();

Expand All @@ -106,14 +107,15 @@ void UserphraseModel::remove(QModelIndexList indexList)
userphrase_[index].bopomofo_.toUtf8().constData());
if (ret > 0) {
removerecord_.push_back(Userphrase{userphrase_[index]});
// FIXME: std::vector::erase is an inefficient operation.
remove_cnt++;
// FIXME: std::vector::erase is an inefficient operation.
userphrase_.erase(userphrase_.begin() + index);
} else {
qWarning() << "chewing_userphrase_remove() returns" << ret;
}
// FIXME: Handle chewing_userphrase_remove fails.
}

maxundocnt = remove_cnt;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you rely on maxunocnt?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to store how many pharses can be restored at one time.
How can I do that better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply use STL iterator to traverse.

Copy link
Author

@kuoyliu kuoyliu May 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean I should add a new STL like vector and don't use a simple global variable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about create a dummy Userpharse as divider between each operation?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use vector to store in new commit
Is there any question?

emit endRemoveRows();

emit removePhraseCompleted(indexList.size());
Expand Down Expand Up @@ -232,11 +234,14 @@ const Userphrase *UserphraseModel::getUserphrase(const QModelIndex& idx)

void UserphraseModel::undo()
{
if (!removerecord_.empty()) {
auto last = removerecord_.end() - 1;
const QString phrase = last->display_;
add(last->phrase_, last->bopomofo_);
removerecord_.erase(last);
emit undoCompleted(phrase);
if (!removerecord_.empty() && maxundocnt>0) {
while(maxundocnt--){
jserv marked this conversation as resolved.
Show resolved Hide resolved
auto last = removerecord_.end() - 1;
const QString phrase = last->display_;
add(last->phrase_, last->bopomofo_);
removerecord_.erase(last);
emit undoCompleted(phrase);
}
maxundocnt = 1;
}
}
1 change: 1 addition & 0 deletions src/model/UserphraseModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@ public slots:
UserphraseSet userphrase_;
std::vector<Userphrase> removerecord_;
int addresult_;
int maxundocnt;
jserv marked this conversation as resolved.
Show resolved Hide resolved
};