在Ubuntu Scope的模版中利用attributes來顯示額外的資訊

來源:互聯網
上載者:User

我在昨天的文章中介紹了我設計的優酷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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.