Clean and order documentation in source files. Source ready to beta 2 release
[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 /*! \file MainWidget.cpp
23     \brief Implements widget for maemo that allows user to search quickly right from home screen using mdictionary.
24
25     \author Mateusz Półrola <mateusz.polrola@comarch.pl>
26 */
27
28 #include "MainWidget.h"
29
30 #include <X11/Xlib.h>
31 #include <X11/Xatom.h>
32 #include <QtGui/QX11Info>
33 #include <QtDBus/QDBusMessage>
34 #include <QtDBus/QDBusPendingCall>
35 #include <QList>
36 #include <QDebug>
37
38
39 HomeWidget::HomeWidget(QWidget *parent):
40     QWidget(parent)
41 {
42     //setting application name, it will use it later to set home applet id
43     QCoreApplication::instance()->setApplicationName(
44             "mDictionary desktop widget");
45
46     //setting this attribute to get transparent backgorund
47     setAttribute(Qt::WA_TranslucentBackground);
48
49     initializeX11();
50
51     initializeUI();
52 }
53
54
55 void HomeWidget::initializeUI() {
56
57
58     horizontalLayout = new QHBoxLayout;
59     setLayout(horizontalLayout);
60
61
62     searchStringLineEdit = new QLineEdit;
63
64     searchButton = new QPushButton(tr("Search"));
65     searchButton->setMaximumHeight(60);
66
67     logo = new QToolButton();
68     logo->setIcon(QIcon(":/icons/64x64/mdictionary.png"));
69
70     horizontalLayout->addWidget(logo);
71     horizontalLayout->addWidget(searchStringLineEdit);
72     horizontalLayout->addWidget(searchButton);
73     horizontalLayout->setContentsMargins(10,10,10,10);
74
75     connect(searchStringLineEdit, SIGNAL(returnPressed()),
76             this, SLOT(search()));
77
78     connect(searchButton, SIGNAL(clicked()),
79             this, SLOT(search()));
80
81     connect(logo, SIGNAL(clicked()),
82             this, SLOT(logoClicked()));
83
84
85     setMinimumHeight(100);
86     setMaximumHeight(100);
87 }
88
89 void HomeWidget::initializeX11() {
90
91     Atom winTypeAtom = XInternAtom(QX11Info::display(),
92                                    "_NET_WM_WINDOW_TYPE",
93                                    false);
94
95     Atom homeAppletAtom = XInternAtom(QX11Info::display(),
96                                      "_HILDON_WM_WINDOW_TYPE_HOME_APPLET",
97                                      false);
98
99     Atom appletIDAtom = XInternAtom(QX11Info::display(),
100                                     "_HILDON_APPLET_ID",
101                                     false);
102
103     Atom utf8Atom = XInternAtom(QX11Info::display(),
104                                 "UTF8_STRING",
105                                 false);
106
107
108     // Set window type to home applet
109     XChangeProperty(QX11Info::display(), winId(), winTypeAtom,
110                     XA_ATOM, 32, PropModeReplace,
111                     (unsigned char *) &homeAppletAtom, 1);
112
113     // Applet id will be the same as application name
114     QByteArray id (
115             QCoreApplication::instance()->applicationName().remove(' ').toUtf8());
116
117
118     XChangeProperty(QX11Info::display(), winId(), appletIDAtom, utf8Atom, 8,
119                     PropModeReplace, (unsigned char *)id.constData(),
120                     id.length());
121 }
122
123 void HomeWidget::search() {
124     if(searchStringLineEdit->text().isEmpty()) return;
125
126     QDBusMessage message =
127             QDBusMessage::createMethodCall("com.comarch.mdictionary",
128                                            "/mainWindow",
129                                            "com.comarch.mdictionary",
130                                            "search");
131
132     QList<QVariant> args;
133     args.append(searchStringLineEdit->text());
134     message.setArguments(args);
135
136     QDBusConnection::sessionBus().send(message);
137
138     searchStringLineEdit->clear();
139 }
140
141 void HomeWidget::logoClicked() {
142     QDBusMessage message =
143             QDBusMessage::createMethodCall("com.comarch.mdictionary",
144                                            "/mainWindow",
145                                            "com.comarch.mdictionary",
146                                            "showApplication");
147     QDBusConnection::sessionBus().send(message);
148 }
149
150
151
152 void HomeWidget::paintEvent(QPaintEvent *event) {
153     //custom painting of rounded corners
154     QPainter p(this);
155
156     p.setBrush(palette().background());
157
158     p.setPen(Qt::NoPen);
159
160     p.drawRoundedRect(rect(), 25, 25);
161
162     p.end();
163
164     QWidget::paintEvent(event);
165 }