在先前的文章“利用reviews PreviewWidget在Ubuntu Scope中显示评价等级”中,我们展示了如何使用review PreviewWidget来显示评价的数据。在这篇文章中,我们将介绍如何使用rating-input
PreviewWidget来对事物进行评价及打分。
参考我们的API介绍,rating-input PreviewWidget分为两种:
- A star-based rating (rating)
- an input field for the user to enter his/her review (review)
也就是,我们可以对事物进行打分,用星级来标示。另外我们也可以输入文字来发表我们的意见。
根据这两种情况,我分别做了如下的代码:
review
// The following shows a review rating-input PreviewWidget w_review("review_input", "rating-input"); w_review.add_attribute_value("submit-label", Variant("Send")); w_review.add_attribute_value("visible", Variant("review")); w_review.add_attribute_value("required", Variant("review")); std::string reply_label = "Reply"; std::string max_chars_label = "140 characters max"; w_review.add_attribute_value("review-label", Variant(reply_label + ": " + max_chars_label)); widgets.emplace_back(w_review);
rating
// The follwing shows a rating rating-input PreviewWidget w_rating("rating_input", "rating-input"); w_rating.add_attribute_value("visible", Variant("rating")); w_rating.add_attribute_value("required", Variant("rating")); w_rating.add_attribute_value("rating-label", Variant("Please rate this")); widgets.emplace_back(w_rating);
运行我们的Scope,显示如下:
我们可以从上面看到一个review及rating的rating-input。
在上面我们可以看到当我们点击“Send”及输入我们的rating时,我们怎么获取他们的值,并使用它们呢?为了这个目的,我们创建了一个新的类:
action.h
#ifndef SCOPE_ACTION_H_ #define SCOPE_ACTION_H_ #include <unity/scopes/ActionMetadata.h> #include <unity/scopes/ActivationQueryBase.h> #include <unity/scopes/ActivationResponse.h> #include <unity/scopes/Result.h> class Action : public unity::scopes::ActivationQueryBase { public: Action(unity::scopes::Result const& result, unity::scopes::ActionMetadata const& metadata, std::string const& action_id); ~Action() = default; virtual unity::scopes::ActivationResponse activate() override; private: std::string const action_id_; }; #endif // SCOPE_ACTION_H_
action.cpp
#include <scope/action.h> #include <unity/scopes/ActivationResponse.h> #include <unity/UnityExceptions.h> #include <QString> #include <QDebug> #include <iostream> namespace sc = unity::scopes; using namespace std; QString qstr_(std::string str) { return QString::fromStdString(str); } Action::Action(const unity::scopes::Result &result, const unity::scopes::ActionMetadata &metadata, std::string const& action_id) : sc::ActivationQueryBase(result, metadata), action_id_(action_id) { qDebug() << "action id: " << qstr_(action_id_); } sc::ActivationResponse Action::activate() { QString review = QString("%1").arg(qstr_(action_metadata().scope_data(). get_dict()["review"].get_string())); double rating = action_metadata().scope_data(). get_dict()["rating"].get_double(); qDebug() << "review: " << review; qDebug() << "rating: " << rating; sc::ActivationResponse done(sc::ActivationResponse::ShowDash); cerr << "activate called" << endl; return done; }
scope.cpp
sc::ActivationQueryBase::UPtr Scope::perform_action( sc::Result const& result, sc::ActionMetadata const& metadata, string const& widget_id, string const& action_id) { cerr << "perform_action called" << endl; return sc::ActivationQueryBase::UPtr(new Action(result, metadata, action_id)); }
使用action类,我们可以在按下按钮“Send”及改变星级rating输入后,我们可以看到如下的输出:
从上面的输出,我们可以看到所有的输出都可以被截获。我们可以利用它们并使用相应的API来更新我们网上的数据,比如点评中餐馆的评价。具体的使用实例请参考我的例程“在Ubuntu Scope中如何截获按钮事件并做我们想做的事情”。
整个项目的源码在:git clone https://gitcafe.com/ubuntu/scopetemplates_rating_input.git
时间: 2024-11-09 01:12:09