2
Copyright 2008 Google Inc.
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
8
http://www.apache.org/licenses/LICENSE-2.0
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
16
#include "plasma_host.h"
19
#include <QGraphicsWidget>
20
#include <QGraphicsProxyWidget>
21
#include <QtGui/QFontDatabase>
22
#include <ggadget/common.h>
23
#include <ggadget/logger.h>
24
#include <ggadget/qt/qt_view_host.h>
25
#include <ggadget/script_runtime_manager.h>
26
#include <ggadget/gadget_consts.h>
27
#include <ggadget/decorated_view_host.h>
28
#include <ggadget/docked_main_view_decorator.h>
29
#include <ggadget/details_view_decorator.h>
30
#include <ggadget/permissions.h>
31
#include <ggadget/qt/utilities.h>
32
#include <ggadget/qt/qt_view_host.h>
33
#include <ggadget/gadget.h>
37
#include <Plasma/Applet>
38
#include <Plasma/ToolTipManager>
39
#include "plasma_view_host.h"
40
#include "panel_decorator.h"
41
#include "popout_decorator.h"
42
#include "floating_decorator.h"
46
class PlasmaHost::Private {
48
Private(GadgetInfo *i)
52
global_permissions_.SetGranted(Permissions::ALL_ACCESS, true);
55
void onCloseMainViewHandler() {
56
if (info->expanded_main_view_host)
58
info->gadget->RemoveMe(true);
61
void onCloseDetailsViewHandler() {
62
info->gadget->CloseDetailsView();
65
void onClosePopOutViewHandler() {
69
void onPopOutHandler() {
70
if (info->expanded_main_view_host != NULL) {
74
ViewInterface *child = info->main_view_host->GetView();
77
PlasmaViewHost *vh = new PlasmaViewHost(
78
info, ViewHostInterface::VIEW_HOST_MAIN, true);
79
PopOutDecorator *view_decorator = new PopOutDecorator(vh);
80
DecoratedViewHost *dvh = new DecoratedViewHost(view_decorator);
81
view_decorator->ConnectOnClose(
82
NewSlot(this, &Private::onClosePopOutViewHandler));
84
// Send popout event to decorator first.
85
SimpleEvent event(Event::EVENT_POPOUT);
86
info->main_view_host->GetViewDecorator()->OnOtherEvent(event);
88
child->SwitchViewHost(dvh);
89
dvh->ShowView(false, 0, NULL);
91
info->expanded_main_view_host = dvh;
95
void onPopInHandler() {
96
if (!info->expanded_main_view_host) return;
97
ViewInterface *child = info->expanded_main_view_host->GetView();
100
// Close details view
101
child->GetGadget()->CloseDetailsView();
103
child->SwitchViewHost(info->main_view_host);
104
SimpleEvent event(Event::EVENT_POPIN);
105
info->main_view_host->GetViewDecorator()->OnOtherEvent(event);
106
info->expanded_main_view_host->Destroy();
107
info->expanded_main_view_host = NULL;
111
DecoratedViewHost *newFloatingViewHost() {
112
PlasmaViewHost* vh = new PlasmaViewHost(
113
info, ViewHostInterface::VIEW_HOST_MAIN);
115
FloatingDecorator *decorator = new FloatingDecorator(vh);
116
decorator->ConnectOnClose(NewSlot(this, &Private::onCloseMainViewHandler));
117
decorator->ConnectOnPopOut(NewSlot(this, &Private::onPopOutHandler));
118
decorator->ConnectOnPopIn(NewSlot(this, &Private::onPopInHandler));
119
DecoratedViewHost *dvh = new DecoratedViewHost(decorator);
121
DLOG("NewViewHost: dvh(%p), pvh(%p), vd(%p)",
126
DecoratedViewHost *newPanelViewHost(bool vertical) {
127
PlasmaViewHost* vh = new PlasmaViewHost(
128
info, ViewHostInterface::VIEW_HOST_MAIN);
130
PanelDecorator *decorator = new PanelDecorator(vh, vertical);
131
decorator->ConnectOnPopOut(NewSlot(this, &Private::onPopOutHandler));
132
decorator->ConnectOnPopIn(NewSlot(this, &Private::onPopInHandler));
133
DecoratedViewHost *dvh = new DecoratedViewHost(decorator);
135
DLOG("NewViewHost: dvh(%p), pvh(%p), vd(%p)",
141
Permissions global_permissions_;
142
double gadget_w_, gadget_h_;
145
PlasmaHost::PlasmaHost(GadgetInfo *info)
146
: d(new Private(info)) {
149
PlasmaHost::~PlasmaHost() {
153
ViewHostInterface *PlasmaHost::NewViewHost(Gadget *,
154
ViewHostInterface::Type type) {
155
if (type == ViewHostInterface::VIEW_HOST_MAIN) {
156
Plasma::Location loc = d->info->applet->location();
157
if (loc == Plasma::Floating) {
158
d->info->main_view_host = d->newFloatingViewHost();
160
d->info->main_view_host = d->newPanelViewHost(isVertical(loc));
162
return d->info->main_view_host;
163
} else if (type == ViewHostInterface::VIEW_HOST_OPTIONS) {
164
ViewHostInterface* vh = new QtViewHost(type, 1.0, 0, 0, NULL);
165
d->info->options_view_host = vh;
168
ViewHostInterface* vh = new PlasmaViewHost(d->info, type);
169
DetailsViewDecorator *view_decorator = new DetailsViewDecorator(vh);
170
DecoratedViewHost *dvh = new DecoratedViewHost(view_decorator);
171
view_decorator->ConnectOnClose(
172
NewSlot(d, &Private::onCloseDetailsViewHandler));
173
d->info->details_view_host = dvh;
178
void PlasmaHost::RemoveGadget(Gadget *gadget, bool save_data) {
179
// Please close me through plasma's button
184
bool PlasmaHost::LoadFont(const char *filename) {
185
if (QFontDatabase::addApplicationFont(filename) != -1)
191
int PlasmaHost::GetDefaultFontSize() {
192
return kDefaultFontSize;
195
bool PlasmaHost::OpenURL(const ggadget::Gadget *gadget, const char *url) {
196
return ggadget::qt::OpenURL(gadget, url);
199
Gadget* PlasmaHost::LoadGadget(const char *path, const char *options_name,
200
int instance_id, bool show_debug_console) {
201
Q_UNUSED(instance_id);
202
Q_UNUSED(show_debug_console);
204
Gadget *gadget = new Gadget(this, path, options_name, 0,
205
d->global_permissions_,
206
Gadget::DEBUG_CONSOLE_DISABLED);
208
if (!gadget->IsValid()) {
209
LOG("Failed to load gadget %s", path);
214
if (!gadget->ShowMainView()) {
215
LOG("Failed to show main view of gadget %s", path);
217
d->info->main_view_host = NULL;
221
if (gadget->HasOptionsDialog()) {
222
d->info->script->setHasConfigurationInterface(true);
228
void PlasmaHost::onConstraintsEvent(Plasma::Constraints constraints) {
229
if (!d->info->main_view_host) return;
231
if (constraints & Plasma::FormFactorConstraint) {
232
// TODO: Do something to handle it right
233
kDebug() << "FormFactorConstraint changed:" << d->info->applet->formFactor();
236
if ((constraints & Plasma::LocationConstraint) &&
237
d->info->applet->location() != d->info->location) {
239
// It will be enabled when PanelDecorator is on horizontal panel.
240
Plasma::ToolTipManager::self()->unregisterWidget(d->info->applet);
243
d->onCloseDetailsViewHandler();
244
Plasma::Location loc = d->info->applet->location();
246
kDebug() << "LocationConstraint changed from " << d->info->location
249
DecoratedViewHost *vh;
250
if (loc == Plasma::Floating) {
251
vh = d->newFloatingViewHost();
253
vh = d->newPanelViewHost(isVertical(loc));
256
// Send popout event here so elements like browser_element will know
257
// about it and they will hide themselves.
258
SimpleEvent event(Event::EVENT_POPOUT);
259
d->info->main_view_host->GetViewDecorator()->OnOtherEvent(event);
261
ViewInterface *child = d->info->main_view_host->GetView();
262
ViewHostInterface *old = child->SwitchViewHost(vh);
265
d->info->main_view_host = vh;
266
SimpleEvent event1(Event::EVENT_POPIN);
267
vh->GetViewDecorator()->OnOtherEvent(event1);
269
// Must call it to get the aspectRatioMode of applet right.
270
// Maybe we can do it nicely in GGL.
271
vh->GetViewDecorator()->GetViewHost()->SetResizable(
272
vh->GetViewDecorator()->GetResizable());
274
vh->ShowView(false, 0, NULL);
275
d->info->location = loc;
279
if (constraints & Plasma::SizeConstraint) {
280
ViewInterface *view = d->info->main_view_host->GetViewDecorator();
281
if (!view || !d->info->main_view_widget || !d->info->proxy) return;
283
QSizeF s = d->info->applet->size();
284
kDebug() << "size requested:" << s;
285
double w = s.width();
286
double h = s.height();
287
double old_w = view->GetWidth();
288
double old_h = view->GetHeight();
289
if (w == old_w && h == old_h) {
290
d->info->main_view_widget->resize(w, h);
291
d->info->proxy->resize(s);
295
if (view->OnSizing(&w, &h)) {
301
} // namespace ggadget