summaryrefslogtreecommitdiff
path: root/navit/gui/qml/gui_qml.cpp
blob: c8d20450ed10ce6ca403dc4c1222767896a9f020 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include <glib.h>
#include <QtCore>
#include <QtGui>
#include <QtDeclarative>
#include <QtXml>
#include "config.h"
#ifdef HAVE_API_WIN32_BASE
#include <windows.h>
#endif
#include "plugin.h"
#include "item.h"
#include "attr.h"
#include "xmlconfig.h"
#include "color.h"
#include "gui.h"
#include "callback.h"
#include "debug.h"
#include "navit.h"
#include "point.h"
#include "graphics.h"
#include "event.h"
#include "map.h"
#include "coord.h"
#include "vehicle.h"
#include "coord.h"
#include "transform.h"
#include "mapset.h"
#include "route.h"
#include "country.h"
#include "track.h"
#include "search.h"
#include "bookmarks.h"
#include "command.h"
#include "keys.h"

#include "layout.h"

struct gui_priv {
    struct navit *nav;
    struct gui *gui;
    struct attr self;
    struct vehicle* currVehicle;

    //configuration items
    int fullscreen;
    int menu_on_map_click;
    int signal_on_map_click;
    int w;
    int h;
    char *source;
    char *skin;
    char* icon_src;
    int radius;
    int pitch;
    int lazy; //When TRUE - menu state will not be changed during map/menu switches, FALSE - menu will be always reset to main.qml

    //Interface stuff
    struct callback_list *cbl;
    QCoreApplication *app;
    struct window *win;
    struct graphics *gra;
    QWidget *mainWindow;
    QWidget *graphicsWidget;
    QDeclarativeView *guiWidget;
    QDeclarativeView *prevGuiWidget;
    QStackedLayout *switcherWidget;
    struct callback *button_cb;
    struct callback *motion_cb;
    struct callback *resize_cb;
    struct callback *keypress_cb;
    struct callback *window_closed_cb;

    //Proxy objects
    class NGQProxyGui* guiProxy;
    class NGQProxyNavit* navitProxy;
    class NGQProxyVehicle* vehicleProxy;
    class NGQProxySearch* searchProxy;
    class NGQProxyBookmarks* bookmarksProxy;
    class NGQProxyRoute* routeProxy;
    class NGQPoint* currentPoint;
};

#include "proxy.h"
#include "ngqpoint.h"
#include "searchProxy.h"
#include "routeProxy.h"
#include "bookmarksProxy.h"
#include "vehicleProxy.h"
#include "navitProxy.h"
#include "guiProxy.h"

//Main window class for resizeEvent handling
#ifdef Q_WS_X11
#include <QX11EmbedWidget>
class NGQMainWindow : public QX11EmbedWidget {
#else
class NGQMainWindow : public QWidget {
#endif /* Q_WS_X11 */
public:
#ifdef Q_WS_X11
    NGQMainWindow(struct gui_priv* this_,QWidget *parent) : QX11EmbedWidget(parent) {
#else
    NGQMainWindow(struct gui_priv* this_,QWidget *parent) : QWidget(parent) {
#endif /* Q_WS_X11  */
        this->object=this_;
    }
protected:
    void resizeEvent(QResizeEvent *) {
        this->object->w=this->width();
        this->object->h=this->height();
        //YES, i KNOW about signal/slot thing
        this->object->guiProxy->setWidth(this->width());
        this->object->guiProxy->setHeight(this->height());
    }
    void closeEvent(QCloseEvent* event) {
        this->object->graphicsWidget->close();
    }
private:
    struct gui_priv* object;
};

//Meta object
#include "gui_qml.moc"

static void gui_qml_dbus_signal(struct gui_priv *this_, struct point *p) {
    struct displaylist_handle *dlh;
    struct displaylist *display;
    struct displayitem *di;

    display=navit_get_displaylist(this_->nav);
    dlh=graphics_displaylist_open(display);
    while ((di=graphics_displaylist_next(dlh))) {
        struct item *item=graphics_displayitem_get_item(di);
        if (item_is_point(*item) && graphics_displayitem_get_displayed(di) &&
                graphics_displayitem_within_dist(display, di, p, 10)) {
            struct map_rect *mr=map_rect_new(item->map, NULL);
            struct item *itemo=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
            struct attr attr;
            if (item_attr_get(itemo, attr_data, &attr)) {
                struct attr cb,*attr_list[2];
                int valid=0;
                attr.type=attr_data;
                attr_list[0]=&attr;
                attr_list[1]=NULL;
                if (navit_get_attr(this_->nav, attr_callback_list, &cb, NULL))
                    callback_list_call_attr_4(cb.u.callback_list, attr_command, "dbus_send_signal", attr_list, NULL, &valid);
            }
            map_rect_destroy(mr);
        }
    }
    graphics_displaylist_close(dlh);
}

static void gui_qml_button(void *data, int pressed, int button, struct point *p) {
    struct gui_priv *this_=(struct gui_priv*)data;

    // check whether the position of the mouse changed during press/release OR if it is the scrollwheel
    if (!navit_handle_button(this_->nav, pressed, button, p, NULL)) {
        dbg(lvl_debug,"navit has handled button");
        return;
    }

    dbg(lvl_debug,"enter %d %d", pressed, button);
    if (this_->signal_on_map_click) {
        gui_qml_dbus_signal(this_, p);
        return;
    }

    if ( button == 1 && this_->menu_on_map_click ) {
        this_->guiProxy->switchToMenu(p);
    }
}

static void gui_qml_motion(void *data, struct point *p) {
    struct gui_priv *this_=(struct gui_priv*)data;
    navit_handle_motion(this_->nav, p);
    return;
}
static void gui_qml_resize(void *data, int w, int h) {
    struct gui_priv *this_=(struct gui_priv*)data;
    navit_handle_resize(this_->nav, w, h);
}

static void gui_qml_keypress(void *data, char *key) {
    struct gui_priv *this_=(struct gui_priv*) data;
    int w,h;
    struct point p;
    transform_get_size(navit_get_trans(this_->nav), &w, &h);
    switch (*key) {
    case NAVIT_KEY_UP:
        p.x=w/2;
        p.y=0;
        navit_set_center_screen(this_->nav, &p, 1);
        break;
    case NAVIT_KEY_DOWN:
        p.x=w/2;
        p.y=h;
        navit_set_center_screen(this_->nav, &p, 1);
        break;
    case NAVIT_KEY_LEFT:
        p.x=0;
        p.y=h/2;
        navit_set_center_screen(this_->nav, &p, 1);
        break;
    case NAVIT_KEY_RIGHT:
        p.x=w;
        p.y=h/2;
        navit_set_center_screen(this_->nav, &p, 1);
        break;
    case NAVIT_KEY_ZOOM_IN:
        navit_zoom_in(this_->nav, 2, NULL);
        break;
    case NAVIT_KEY_ZOOM_OUT:
        navit_zoom_out(this_->nav, 2, NULL);
        break;
    case NAVIT_KEY_RETURN:
    case NAVIT_KEY_MENU:
        p.x=w/2;
        p.y=h/2;
        this_->guiProxy->switchToMenu(&p);
        break;
    }
    return;
}

static void gui_qml_window_closed(struct gui_priv *data) {
    struct gui_priv *this_=(struct gui_priv*) data;
    this_->navitProxy->quit();
}
//GUI interface calls
static int argc=1;
static char *argv[]= {(char *)"navit",NULL};

static int gui_qml_set_graphics(struct gui_priv *this_, struct graphics *gra) {
    QString xid;
    NGQMainWindow* _mainWindow;
    bool ok;

    this_->gra=gra;

    //Check if we are already in Qt environment
    if (QApplication::instance()==NULL) {
        //Not yet
        this_->app=new QApplication(argc,argv);
    } else {
        this_->app=QApplication::instance();
    }

    //Link graphics events
    this_->button_cb=callback_new_attr_1(callback_cast(gui_qml_button), attr_button, this_);
    graphics_add_callback(gra, this_->button_cb);
    this_->motion_cb=callback_new_attr_1(callback_cast(gui_qml_motion), attr_motion, this_);
    graphics_add_callback(gra, this_->motion_cb);
    this_->resize_cb=callback_new_attr_1(callback_cast(gui_qml_resize), attr_resize, this_);
    graphics_add_callback(gra, this_->resize_cb);
    this_->keypress_cb=callback_new_attr_1(callback_cast(gui_qml_keypress), attr_keypress, this_);
    graphics_add_callback(gra, this_->keypress_cb);
    this_->window_closed_cb=callback_new_attr_1(callback_cast(gui_qml_window_closed), attr_window_closed, this_);
    graphics_add_callback(gra, this_->window_closed_cb);


    //Create main window
    this_->switcherWidget = new QStackedLayout();
    _mainWindow = new NGQMainWindow(this_, NULL);
#ifdef Q_WS_X11
    xid=getenv("NAVIT_XID");
    if (xid.length()>0) {
        _mainWindow->embedInto(xid.toULong(&ok,0));
    } else {
        dbg(lvl_error, "FATAL: Environment variable NAVIT_XID not set."
            "       Please set NAVIT_XID to the window ID of the window to embed into.\n");
        exit(1);
    }
#endif /* Q_WS_X11  */
    this_->mainWindow=_mainWindow;
    if ( this_->w && this_->h ) {
        this_->mainWindow->resize(this_->w,this_->h);
    }
    if ( this_->fullscreen ) {
        this_->mainWindow->showFullScreen();
    }

    this_->mainWindow->setLayout(this_->switcherWidget);

    //Create proxy object and bind them to gui widget
    this_->guiProxy = new NGQProxyGui(this_,this_->mainWindow);
    this_->navitProxy = new NGQProxyNavit(this_,this_->mainWindow);
    this_->vehicleProxy = new NGQProxyVehicle(this_,this_->mainWindow);
    this_->searchProxy = new NGQProxySearch(this_,this_->mainWindow);
    this_->bookmarksProxy = new NGQProxyBookmarks(this_,this_->mainWindow);
    this_->routeProxy = new NGQProxyRoute(this_,this_->mainWindow);

    //Check, if we have compatible graphics
    this_->graphicsWidget = (QWidget*)graphics_get_data(gra,"qt_widget");
    if (this_->graphicsWidget == NULL ) {
        this_->graphicsWidget = new QLabel(QString("Sorry, current graphics type is incompatible with this gui."));
    }
    this_->switcherWidget->addWidget(this_->graphicsWidget);

    //Instantiate qml components
    this_->guiWidget = new QDeclarativeView(NULL);
    this_->guiWidget->setResizeMode(QDeclarativeView::SizeRootObjectToView);

    this_->guiWidget->rootContext()->setContextProperty("gui",this_->guiProxy);
    this_->guiWidget->rootContext()->setContextProperty("navit",this_->navitProxy);
    this_->guiWidget->rootContext()->setContextProperty("vehicle",this_->vehicleProxy);
    this_->guiWidget->rootContext()->setContextProperty("search",this_->searchProxy);
    this_->guiWidget->rootContext()->setContextProperty("bookmarks",this_->bookmarksProxy);
    this_->guiWidget->rootContext()->setContextProperty("route",this_->routeProxy);
    this_->guiWidget->rootContext()->setContextProperty("point",this_->currentPoint);

    QString mainQml = QString(this_->source)+"/"+this_->skin+"/main.qml";
    if (!QFile(mainQml).exists()) {
        dbg(lvl_error, "FATAL: QML file %s not found. Navit is not installed correctly.", mainQml.toAscii().constData());
        exit(1);
    }
    this_->guiWidget->setSource(QUrl::fromLocalFile(mainQml));
    this_->switcherWidget->addWidget(this_->guiWidget);

    //Switch to graphics
    navit_draw(this_->nav);
    this_->switcherWidget->setCurrentWidget(this_->graphicsWidget);

    this_->mainWindow->show();

    return 0;
}

static int gui_qml_get_attr(struct gui_priv *this_, enum attr_type type, struct attr *attr) {
    switch (type) {
    case attr_fullscreen:
        attr->u.num=this_->fullscreen;
        break;
    case attr_skin:
        attr->u.str=this_->skin;
        break;
    case attr_pitch:
        attr->u.num=this_->pitch;
        break;
    case attr_radius:
        attr->u.num=this_->radius;
        break;
    default:
        return 0;
    }
    attr->type=type;
    return 1;
}

static int gui_qml_set_attr(struct gui_priv *this_, struct attr *attr) {
    switch (attr->type) {
    case attr_fullscreen:
        if (!(this_->fullscreen) && (attr->u.num)) {
            this_->mainWindow->showFullScreen();
        }
        if ((this_->fullscreen) && !(attr->u.num)) {
            this_->mainWindow->showNormal();
        }
        this_->fullscreen=attr->u.num;
        return 1;
    case attr_pitch:
        this_->pitch=attr->u.num;
        return 1;
    case attr_radius:
        this_->radius=attr->u.num;
        return 1;
    default:
        dbg(lvl_error,"unknown attr: %s",attr_to_name(attr->type));
        return 1;
    }
}

struct gui_methods gui_qml_methods = {
    NULL,
    NULL,
    gui_qml_set_graphics,
    NULL,
    NULL,
    NULL,
    NULL,
    gui_qml_get_attr,
    NULL,
    gui_qml_set_attr,
};

static void gui_qml_command(struct gui_priv *this_, char *function, struct attr **in, struct attr ***out, int *valid) {
    this_->guiProxy->processCommand(function);
}

static struct command_table commands[] = {
    {"*",command_cast(gui_qml_command)},
};

static struct gui_priv * gui_qml_new(struct navit *nav, struct gui_methods *meth, struct attr **attrs,
                                     struct gui *gui) {
    struct gui_priv *this_;
    struct attr *attr;
    *meth=gui_qml_methods;
    this_=g_new0(struct gui_priv, 1);

    this_->nav=nav;
    this_->gui=gui;

    this_->self.type=attr_gui;
    this_->self.u.gui=gui;

    navit_ignore_graphics_events(this_->nav, 1);

    this_->fullscreen = 0; //NO by default
    if( (attr=attr_search(attrs,attr_fullscreen)))
        this_->fullscreen=attr->u.num;
    this_->menu_on_map_click = 1; //YES by default;
    if( (attr=attr_search(attrs,attr_menu_on_map_click)))
        this_->menu_on_map_click=attr->u.num;
    this_->signal_on_map_click = 0; //YES by default;
    if( (attr=attr_search(attrs,attr_signal_on_map_click)))
        this_->signal_on_map_click=attr->u.num;
    this_->radius = 10; //Default value
    if( (attr=attr_search(attrs,attr_radius)))
        this_->radius=attr->u.num;
    this_->pitch = 20; //Default value
    if( (attr=attr_search(attrs,attr_pitch)))
        this_->pitch=attr->u.num;
    this_->lazy = 1; //YES by default
    if( (attr=attr_search(attrs,attr_lazy)))
        this_->lazy=attr->u.num;
    this_->w=800; //Default value
    if( (attr=attr_search(attrs,attr_width)))
        this_->w=attr->u.num;
    this_->h=600; //Default value
    if( (attr=attr_search(attrs,attr_height)))
        this_->h=attr->u.num;
    if( (attr=attr_search(attrs,attr_source)))
        this_->source=attr->u.str;
    if( (attr=attr_search(attrs,attr_skin)))
        this_->skin=attr->u.str;
    if( (attr=attr_search(attrs,attr_icon_src)))
        this_->icon_src=attr->u.str;

    if ( this_->source==NULL ) {
        this_->source=g_strjoin(NULL,getenv("NAVIT_SHAREDIR"),"/gui/qml/skins",NULL);
    }
    if ( this_->skin==NULL ) {
        this_->skin=g_strdup("navit");
    }
    if ( this_->icon_src==NULL ) {
        this_->icon_src=g_strjoin(NULL,getenv("NAVIT_SHAREDIR"),"/icons/",NULL);
    }

    if ((attr=attr_search(attrs, attr_callback_list))) {
        command_add_table(attr->u.callback_list, commands, sizeof(commands)/sizeof(struct command_table), this_);
    }

    this_->cbl=callback_list_new();

    return this_;
}

void plugin_init(void) {
    plugin_register_category_gui("qml",gui_qml_new);
}