In the previous article, “showing rating levels in Ubuntu Scope with reviews PreviewWidget,” we showed how to use the review PreviewWidget to display rating data. In this article, we will show you how to use the rating- Input PreviewWidget to rate and score things.

\

Refer to our API introduction, there are two types of previewWidgets:

  • A star-based rating (rating)\

  • an input field for the user to enter his/her review (review)\

That is, we can give things a rating, a star rating. In addition, we can also input text to express our opinions.

\

In both cases, 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);
Copy the code

\

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);
Copy the code


\

Run our Scope and it will look like this:

\

   \

\

\

We can see a review and rating of the rating-input above.

\

Above we can see that when we click “Send” and enter our rating, how do we get their values and use them? For this purpose, we create 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(a) override;

private:
    std::string const action_id_;
};

#endif // SCOPE_ACTION_H_
Copy the code


\

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(a)
{
    QString review = QString("% 1").arg(qstr_(action_metadata().scope_data().
                                              get_dict(to)"review"].get_string()));

    double rating = action_metadata().scope_data().
                          get_dict(to)"rating"].get_double(a);qDebug() < <"review: " << review;
    qDebug() < <"rating: " << rating;

    sc::ActivationResponse done(sc::ActivationResponse::ShowDash);
    cerr << "activate called" << endl;
    return done;
}
Copy the code


\

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));
}
Copy the code


\

Using the Action class, after pressing the “Send” button and changing the star rating input, we can see the following output:

\

\

\

From the output above, we can see that all output can be intercepted. We can take advantage of them and use the corresponding API to update our online data, such as reviewing Chinese restaurant reviews. For an example, see my routine “How to intercept button events in Ubuntu Scope and do what we want”.

\

The entire project source at: github.com/liu-xiao-gu…

\

\