1 | /* Copyright (C) 2013 Olivier Goffart <ogoffart@woboq.com> |
2 | http://woboq.com/blog/property-bindings-in-cpp.html |
3 | |
4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
5 | associated documentation files (the "Software"), to deal in the Software without restriction, |
6 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
7 | and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
8 | subject to the following conditions: |
9 | |
10 | The above copyright notice and this permission notice shall be included in all copies or substantial |
11 | portions of the Software. |
12 | |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT |
14 | NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
15 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES |
16 | OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
17 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
18 | */ |
19 | |
20 | #include "property.h" |
21 | #include "qobject_wrappers.h" |
22 | |
23 | #include <QtWebKitWidgets/QtWebKitWidgets> |
24 | |
25 | |
26 | typedef QtWrapper::Widget<QWidget> Widget; |
27 | typedef QtWrapper::Label<QLabel> Label; |
28 | typedef QtWrapper::LineEdit<QLineEdit> LineEdit; |
29 | typedef QtWrapper::Label<QPushButton> PushButton; |
30 | |
31 | struct WebView : QtWrapper::Widget<QWebView> { |
32 | template<typename... Args> WebView(Args &&...args) : QtWrapper::Widget<QWebView>(std::forward<Args>(args)...) { |
33 | |
34 | //Because the property don't have a NOTIFY signals :-( |
35 | title.bindToSignal(metaObject()->indexOfSignal("titleChanged(QString)" )); |
36 | icon.bindToSignal(metaObject()->indexOfSignal("iconChanged()" )); |
37 | url.bindToSignal(metaObject()->indexOfSignal("urlChanged()" )); |
38 | } |
39 | property_qobject<QString> title{ this, "title" }; |
40 | property_qobject<QIcon> icon{ this, "icon" }; |
41 | property_qobject<QString> url { this, "url" }; |
42 | }; |
43 | |
44 | |
45 | struct BrowserWindow : Widget { |
46 | LineEdit urlBar { this }; |
47 | PushButton nextButton { tr("Next" ) , this }; |
48 | PushButton prevButton { tr("Previous" ) , this }; |
49 | WebView webview { this }; |
50 | property_qobject<QString> windowTitle { this, "windowTitle" }; |
51 | property_qobject<QIcon> windowIcon { this, "windowIcon" }; |
52 | |
53 | ::property<int> margin {10}; |
54 | |
55 | BrowserWindow() { |
56 | urlBar.geometry = [&](){ return QRect(margin, margin, |
57 | prevButton.geometry().left() - 2*margin , |
58 | urlBar.sizeHint().height()); }; |
59 | nextButton.geometry = [&](){ return QRect(QPoint(geometry().width() - 2*margin - nextButton.sizeHint().width(), margin), |
60 | nextButton.sizeHint()); }; |
61 | prevButton.geometry = [&](){ return QRect(QPoint(nextButton.geometry().x() - prevButton.sizeHint().width() - margin, margin), |
62 | prevButton.sizeHint()); }; |
63 | |
64 | webview.geometry = [&]() { |
65 | int x = urlBar.geometry().bottom() + margin; |
66 | return QRect(margin, x, width() - 2*margin, geometry().height() - x - margin); }; |
67 | |
68 | webview.url =[&](){ return urlBar.text(); }; |
69 | //urlBar.text =[&](){ return webview.url(); }; //Well , that's a loop |
70 | windowTitle = [&]() { return webview.title(); }; |
71 | windowIcon = [&]() { return webview.icon(); }; } |
72 | }; |
73 | |
74 | int main(int argc, char **argv) |
75 | { |
76 | QApplication app(argc,argv); |
77 | |
78 | BrowserWindow window; |
79 | window.show(); |
80 | |
81 | return app.exec(); |
82 | } |
83 | |
84 | // c++ -std=c++11 -I../../src -lQt5Core -lQt5Gui -lQt5Widgets -lQt5WebKitWidgets -fPIC -I/usr/include/qt ./browser.cc |
85 | |