~unity-2d-team/unity-2d/window-focus-fixes

« back to all changes in this revision

Viewing changes to libunity-2d-private/src/hud.cpp

  • Committer: Tarmac
  • Author(s): Gerry Boland, Michał Sawicz
  • Date: 2012-02-28 11:30:52 UTC
  • mfrom: (915.1.61 hud-qml)
  • Revision ID: tarmac-20120228113052-ko3v4z076cq2aanr
Add HUD to Unity 2D

Known issue:
- on switching between Dash & Hud (tap Alt, then Super, then Alt...) blurred background image will contain Hud/Dash.. Fixes: https://bugs.launchpad.net/bugs/942045. Approved by Albert Astals Cid.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 Canonical, Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Gerry Boland <gerry.boland@canonical.com>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; version 3.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
// Self
 
21
#include "hud.h"
 
22
 
 
23
// std
 
24
#include <stdexcept>
 
25
 
 
26
// Local
 
27
#include <debug_p.h>
 
28
 
 
29
// Qt
 
30
#include <QDateTime>
 
31
 
 
32
static const char* HUD_DBUS_SERVICE = "com.canonical.hud";
 
33
static const char* HUD_DBUS_PATH = "/com/canonical/hud";
 
34
 
 
35
Hud::Hud(QObject *parent) :
 
36
    QAbstractListModel(parent),
 
37
    m_connected(false)
 
38
{
 
39
    QHash<int, QByteArray> names;
 
40
    names[ResultIdRole] = "resultId";
 
41
    names[FormattedTextRole] = "formattedText";
 
42
    names[IconNameRole] = "iconName";
 
43
    names[ItemIconRole] = "itemIcon";
 
44
    names[CompletionTextRole] = "completionText";
 
45
    names[ShortcutRole] = "shortcut";
 
46
    setRoleNames(names);
 
47
 
 
48
    m_unityHud = new unity::hud::Hud(HUD_DBUS_SERVICE, HUD_DBUS_PATH);
 
49
 
 
50
    m_unityHud->target.changed.connect(sigc::mem_fun(this, &Hud::onTargetChanged));
 
51
    m_unityHud->connected.changed.connect(sigc::mem_fun(this, &Hud::onConnectedChanged));
 
52
    m_unityHud->queries_updated.connect(sigc::mem_fun(this, &Hud::onResultsUpdated));
 
53
}
 
54
 
 
55
Hud::~Hud()
 
56
{
 
57
    delete m_unityHud;
 
58
}
 
59
 
 
60
int Hud::rowCount(const QModelIndex& parent) const
 
61
{
 
62
    Q_UNUSED(parent)
 
63
    if (m_searchQuery.isEmpty()) {
 
64
        return 0;
 
65
    }
 
66
    return m_unityHudResults.size();
 
67
}
 
68
 
 
69
QVariant Hud::data(const QModelIndex& index, int role) const
 
70
{
 
71
    if (!index.isValid() || m_searchQuery.isEmpty()) {
 
72
        return QVariant();
 
73
    }
 
74
 
 
75
    std::shared_ptr<unity::hud::Query> result;
 
76
 
 
77
    try {
 
78
        result = m_unityHudResults.at(index.row());
 
79
    } catch (std::out_of_range) {
 
80
        UQ_DEBUG << "HUD query id invalid";
 
81
        return QVariant();
 
82
    }
 
83
 
 
84
    switch(role) {
 
85
    case Hud::ResultIdRole:
 
86
        return index.row();
 
87
    case Hud::FormattedTextRole:
 
88
        return QString::fromStdString(result->formatted_text);
 
89
    case Hud::IconNameRole:
 
90
        return QString::fromStdString(result->icon_name);
 
91
    case Hud::ItemIconRole:
 
92
        return QString::fromStdString(result->item_icon);
 
93
    case Hud::CompletionTextRole:
 
94
        return QString::fromStdString(result->completion_text);
 
95
    case Hud::ShortcutRole:
 
96
        return QString::fromStdString(result->shortcut);
 
97
    /* TODO (?) Hud returns more information in a "key" struct containing
 
98
     * lower-level information than we seem not to need right now. */
 
99
    default:
 
100
        return QVariant();
 
101
    }
 
102
}
 
103
 
 
104
QString Hud::searchQuery() const
 
105
{
 
106
    return m_searchQuery;
 
107
}
 
108
 
 
109
void Hud::setSearchQuery(const QString& searchQuery)
 
110
{
 
111
    if (searchQuery != m_searchQuery) {
 
112
        m_searchQuery = searchQuery;
 
113
        m_unityHud->RequestQuery(m_searchQuery.toStdString());
 
114
        beginResetModel();
 
115
        Q_EMIT searchQueryChanged();
 
116
    }
 
117
}
 
118
 
 
119
void Hud::executeResult(const int id) const
 
120
{
 
121
    std::shared_ptr<unity::hud::Query> result;
 
122
 
 
123
    try {
 
124
        result = m_unityHudResults.at(id);
 
125
    } catch (std::out_of_range) {
 
126
        UQ_DEBUG << "HUD query id invalid";
 
127
        return;
 
128
    }
 
129
 
 
130
    m_unityHud->ExecuteQuery(result,
 
131
                             QDateTime::currentDateTime().toTime_t());
 
132
}
 
133
 
 
134
void Hud::executeResultBySearch(const QString& searchQuery) const
 
135
{
 
136
    m_unityHud->ExecuteQueryBySearch(searchQuery.toStdString(),
 
137
                                     QDateTime::currentDateTime().toTime_t());
 
138
}
 
139
 
 
140
void Hud::endSearch()
 
141
{
 
142
    m_unityHud->CloseQuery();
 
143
    m_searchQuery.clear();
 
144
}
 
145
 
 
146
void Hud::onResultsUpdated(const unity::hud::Hud::Queries results)
 
147
{
 
148
    m_unityHudResults = results;
 
149
    endResetModel();
 
150
}
 
151
 
 
152
QString Hud::target() const {
 
153
    return QString::fromStdString(m_unityHud->target);
 
154
}
 
155
 
 
156
bool Hud::connected() const{
 
157
    return m_connected;
 
158
}
 
159
 
 
160
void Hud::onConnectedChanged(const bool connected) {
 
161
    m_connected = connected;
 
162
    Q_EMIT connectedChanged();
 
163
}
 
164
 
 
165
void Hud::onTargetChanged(const std::string target) {
 
166
    Q_EMIT targetChanged();
 
167
}
 
168
 
 
169
#include "hud.moc"