在Ubuntu Scope的模版中利用attributes来显示额外的信息

我在昨天的文章中介绍了我设计的优酷Scope。在今天的练习中,我将对它的模版做一些小的改动,利用模版中的attributes项使得它的显示更加生动。


如果感兴趣的朋友,可以在如下的地址下载最新的youku scope:

https://github.com/liu-xiao-guo/youku_keywords


首先,我们在query.cpp中对它的模版做如下的改动:


query.cpp


const std::string NORMAL_TEMPLATE = R"(
{
  "schema-version": 1,
  "template": {
    "category-layout": "grid",
    "card-size": "medium",
    "overlay": false
  },
  "components": {
    "title": "title",
    "subtitle": "subtitle",
    "art" : {
      "field": "art",
      "aspect-ratio": 2.0
    },
    "attributes": {
        "field": "attributes",
        "max-count": 2
    }
  }
}
)";


在这个模版中,我们添加了如下的项:


    "attributes": {
        "field": "attributes",
        "max-count": 2
    }


我们可以利用这个项来显示一些我们所感兴趣的东西。

query.cpp


void Query::do_normal_search(sc::SearchReplyProxy const& reply) {
    try {
        // Start by getting information about the query
        const sc::CannedQuery &query(sc::SearchQueryBase::query());

        // Get the query string
        string query_string = query.query_string();

        // Populate current weather category

        // the Client is the helper class that provides the results
        // without mixing APIs and scopes code.
        // Add your code to retreive xml, json, or any other kind of result
        // in the client.

        Client::DataList datalist;
        datalist = client_.getData(query_string);

        CategoryRenderer rdrGrid(NORMAL_TEMPLATE);
        auto grid = reply->register_category("youku", "Normal", "", rdrGrid);

        for (const Client::Data &data : datalist) {
            CategorisedResult catres(grid);

            catres.set_uri(data.link);
            catres.set_title(data.title);
            catres.set_art(data.image);

            QString likes = QString("%1 %2").arg(qstr(u8"\u261d "),qstr(data.up_count));
            QString views = QString("%1 %2").arg(qstr(u8"   \u261f "),qstr(data.down_count));
            std::string both = qstr("%1 %2").arg(likes,views).toStdString();
            sc::VariantBuilder builder;
            builder.add_tuple({
                {"value", Variant(both)}
            });
            builder.add_tuple({
                {"value", Variant("")}
            });
            catres["attributes"] = builder.end();

            // Push the result
            if (!reply->push(catres)) {
                // If we fail to push, it means the query has been cancelled.
                // So don't continue;
                return;
            }
        }

    } catch (domain_error &e) {
        // Handle exceptions being thrown by the client API
        cerr << e.what() << endl;
        reply->error(current_exception());
    }


在上面,我们添加了如下的句子:


        QString likes = QString("%1 %2").arg(qstr(u8"\u261d "),qstr(data.up_count));
        QString views = QString("%1 %2").arg(qstr(u8"   \u261f "),qstr(data.down_count));
        std::string both = qstr("%1 %2").arg(likes,views).toStdString();
        sc::VariantBuilder builder;
        builder.add_tuple({
           {"value", Variant(both)}
        });
        builder.add_tuple({
           {"value", Variant("")}
        });
        catres["attributes"] = builder.end();

这里的2个字符表示的是特定的字符(向上和向下的手势)。通过这样的改动,我们可以得到如下的显示:




从上面的图片中可以看出来,对于每部视频的下面,多了一项显示有多少人喜欢,有多少人讨厌这部视频。


整个项目的源码在: https://github.com/liu-xiao-guo/youku_attributes