c9833d445a900bff67f56afa289b2bbf82787c27
[mdictionary] / trunk / 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 MainWidget::MainWidget(QWidget *parent):
36     QWidget(parent)
37 {
38     setAttribute(Qt::WA_TranslucentBackground);
39
40
41     QCoreApplication::instance()->setApplicationName(
42             "mDictionary desktop widget");
43
44     // Get required atoms
45     Atom winTypeAtom = XInternAtom(
46             QX11Info::display(), "_NET_WM_WINDOW_TYPE", false);
47
48     Atom homeAppletAto = XInternAtom(
49             QX11Info::display(), "_HILDON_WM_WINDOW_TYPE_HOME_APPLET", false);
50
51     Atom appletIDAtom = XInternAtom(
52             QX11Info::display(), "_HILDON_APPLET_ID", false);
53
54     Atom utf8Atom = XInternAtom(
55             QX11Info::display(), "UTF8_STRING", false);
56
57
58     // Set correct window type
59     XChangeProperty(QX11Info::display(), winId(), winTypeAtom, XA_ATOM, 32,
60                     PropModeReplace, (unsigned char *) &homeAppletAto, 1);
61
62
63
64     // Use application name to fill AppletID
65     QByteArray id (QCoreApplication::instance()->applicationName().remove(' ').toUtf8());
66
67     XChangeProperty(QX11Info::display(), winId(), appletIDAtom, utf8Atom, 8,
68                     PropModeReplace, (unsigned char *)id.constData(),
69                     id.length());
70
71
72     horizontalLayout = new QHBoxLayout;
73     setLayout(horizontalLayout);
74
75     searchStringLineEdit = new QLineEdit;
76     searchButton = new QPushButton(tr("Search"));
77
78     searchButton->setMaximumHeight(60);
79
80     logo = new QToolButton();
81     logo->setIcon(QIcon(":/icons/64x64/mdictionary.png"));
82
83
84     horizontalLayout->addWidget(logo);
85     horizontalLayout->addWidget(searchStringLineEdit);
86     horizontalLayout->addWidget(searchButton);
87     horizontalLayout->setContentsMargins(10,10,10,10);
88
89
90     connect(searchStringLineEdit, SIGNAL(returnPressed()),
91             this, SLOT(search()));
92
93     connect(searchButton, SIGNAL(clicked()),
94             this, SLOT(search()));
95
96     connect(logo, SIGNAL(clicked()),
97             this, SLOT(logoClicked()));
98
99
100     setMinimumHeight(100);
101     setMaximumHeight(100);
102 }
103
104 void MainWidget::search() {
105     if(searchStringLineEdit->text().isEmpty()) return;
106
107     QDBusMessage message =
108             QDBusMessage::createMethodCall("com.comarch.mdictionary",
109                                            "/mainWindow",
110                                            "com.comarch.mdictionary",
111                                            "search");
112
113     QList<QVariant> args;
114     args.append(searchStringLineEdit->text());
115     message.setArguments(args);
116
117     QDBusConnection::sessionBus().send(message);
118 }
119
120 void MainWidget::logoClicked() {
121     QDBusMessage message =
122             QDBusMessage::createMethodCall("com.comarch.mdictionary",
123                                            "/mainWindow",
124                                            "com.comarch.mdictionary",
125                                            "showApplication");
126     QDBusConnection::sessionBus().send(message);
127 }
128
129
130
131 void MainWidget::paintEvent(QPaintEvent *event) {
132
133     QPainter p(this);
134     p.setBrush(QColor(0, 0, 0, 255));
135
136     p.setPen(Qt::NoPen);
137
138     p.drawRoundedRect(rect(), 25, 25);
139
140     p.end();
141 }