In the previous article, "Using reviews Previewwidget to display rating levels in Ubuntu scope," We showed how to use the review previewwidget to display the data of the review. In this article, we'll show you how to use rating-input previewwidget to evaluate things and score them.
Referring to our API introduction, Rating-input Previewwidget is 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 use star ratings to mark them. In addition, we can also enter text to express our views.
In both cases, I made the following code, respectively:
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 = "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"); Widgets.emplace_back (w_rating);
To run our scope, the following is displayed:
We can see from the above a review and rating rating-input.
In the above we can see when we click "Send" and enter our rating, how do 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 <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::p erform_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));}
With the action class, we can see the following output after we press the button "Send" and change the star rating input:
From the above output, we can see that all the outputs can be intercepted. We can use them and use the appropriate APIs to update our online data, such as reviews of restaurants in the review. For specific usage examples, refer to my routines "how to intercept button events in Ubuntu scope and do what we want to do".
The source code for the entire project is: Git clone https://gitcafe.com/ubuntu/scopetemplates_rating_input.git
Use Rating-input Previewwidget to evaluate and score things