Rating-input PreviewWidget is used to evaluate and grade things.
In the previous article "using reviews PreviewWidget to display rating in Ubuntu Scope", we showed how to use review PreviewWidget to display rating data. In this article, we will introduce how to use rating-input PreviewWidget to evaluate and grade things.
According to our API introduction, rating-input PreviewWidget can be divided into two types:
A star-based rating (rating)
An input field for the user to enter his/her review (review)
That is, we can rate things and mark them with stars. In addition, you can enter text to express your opinions.
Based on the two situations, I made the following code:
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);
Run our Scope, as shown below:
We can see a review and rating-input from the above.
As we can see above, when we click "Send" and input our rating, how can we get their values and use them? For this purpose, we have created a new class:
Action. h
#ifndef SCOPE_ACTION_H_#define SCOPE_ACTION_H_#include
#include
#include
#include
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
#include
#include
#include
#include
#include
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));}
Using the action class, we can see the following output after pressing the "Send" button and changing the star rating input:
From the above output, we can see that all outputs can be intercepted. We can use them and use corresponding APIs to update our online data, such as comments on Chinese restaurants. For specific examples, refer to my routine "How to intercept button events in Ubuntu Scope and do what we want to do ".
Source code for the entire project in: git clone https://gitcafe.com/ubuntu/scopetemplates_rating_input.git