Changed repo structure
[mdictionary] / src / desktopWidget / MainWidget.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 //Created by Mateusz Półrola
23
24 #include "MainWidget.h"
25
26 #include <X11/Xlib.h>
27 #include <X11/Xatom.h>
28 #include <QtGui/QX11Info>
29 #include <QtDBus/QDBusMessage>
30 #include <QtDBus/QDBusPendingCall>
31 #include <QList>
32 #include <QDebug>
33
34
35 HomeWidget::HomeWidget(QWidget *parent):
36     QWidget(parent)
37 {
38     //setting application name, it will use it later to set home applet id
39     QCoreApplication::instance()->setApplicationName(
40             "mDictionary desktop widget");
41
42     //setting this attribute to get transparent backgorund
43     setAttribute(Qt::WA_TranslucentBackground);
44
45     initializeX11();
46
47     initializeUI();
48 }
49
50
51 void HomeWidget::initializeUI() {
52
53
54     horizontalLayout = new QHBoxLayout;
55     setLayout(horizontalLayout);
56
57
58     searchStringLineEdit = new QLineEdit;
59
60     searchButton = new QPushButton(tr("Search"));
61     searchButton->setMaximumHeight(60);
62
63     logo = new QToolButton();
64     logo->setIcon(QIcon(":/icons/64x64/mdictionary.png"));
65
66     horizontalLayout->addWidget(logo);
67     horizontalLayout->addWidget(searchStringLineEdit);
68     horizontalLayout->addWidget(searchButton);
69     horizontalLayout->setContentsMargins(10,10,10,10);
70
71     connect(searchStringLineEdit, SIGNAL(returnPressed()),
72             this, SLOT(search()));
73
74     connect(searchButton, SIGNAL(clicked()),
75             this, SLOT(search()));
76
77     connect(logo, SIGNAL(clicked()),
78             this, SLOT(logoClicked()));
79
80
81     setMinimumHeight(100);
82     setMaximumHeight(100);
83 }
84
85 void HomeWidget::initializeX11() {
86
87     Atom winTypeAtom = XInternAtom(QX11Info::display(),
88                                    "_NET_WM_WINDOW_TYPE",
89                                    false);
90
91     Atom homeAppletAtom = XInternAtom(QX11Info::display(),
92                                      "_HILDON_WM_WINDOW_TYPE_HOME_APPLET",
93                                      false);
94
95     Atom appletIDAtom = XInternAtom(QX11Info::display(),
96                                     "_HILDON_APPLET_ID",
97                                     false);
98
99     Atom utf8Atom = XInternAtom(QX11Info::display(),
100                                 "UTF8_STRING",
101                                 false);
102
103
104     // Set window type to home applet
105     XChangeProperty(QX11Info::display(), winId(), winTypeAtom,
106                     XA_ATOM, 32, PropModeReplace,
107                     (unsigned char *) &homeAppletAtom, 1);
108
109     // Applet id will be the same as application name
110     QByteArray id (
111             QCoreApplication::instance()->applicationName().remove(' ').toUtf8());
112
113
114     XChangeProperty(QX11Info::display(), winId(), appletIDAtom, utf8Atom, 8,
115                     PropModeReplace, (unsigned char *)id.constData(),
116                     id.length());
117 }
118
119 void HomeWidget::search() {
120     if(searchStringLineEdit->text().isEmpty()) return;
121
122     QDBusMessage message =
123             QDBusMessage::createMethodCall("com.comarch.mdictionary",
124                                            "/mainWindow",
125                                            "com.comarch.mdictionary",
126                                            "search");
127
128     QList<QVariant> args;
129     args.append(searchStringLineEdit->text());
130     message.setArguments(args);
131
132     QDBusConnection::sessionBus().send(message);
133 }
134
135 void HomeWidget::logoClicked() {
136     QDBusMessage message =
137             QDBusMessage::createMethodCall("com.comarch.mdictionary",
138                                            "/mainWindow",
139                                            "com.comarch.mdictionary",
140                                            "showApplication");
141     QDBusConnection::sessionBus().send(message);
142 }
143
144
145
146 void HomeWidget::paintEvent(QPaintEvent *event) {
147     //custom painting of rounded corners
148     QPainter p(this);
149     p.setBrush(QColor(0, 0, 0, 255));
150
151     p.setPen(Qt::NoPen);
152
153     p.drawRoundedRect(rect(), 25, 25);
154
155     p.end();
156
157     QWidget::paintEvent(event);
158 }