not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / scriptengines / google_gadgets / plasma_view_host.cpp
blob10e4d6295fefe21a38c240e618b789c0d625d463
1 /*
2 Copyright 2007 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.
17 #include <sys/time.h>
19 #include <KMessageBox>
20 #include <QtGui/QGraphicsProxyWidget>
21 #include <QtGui/QGraphicsLinearLayout>
22 #include <QtGui/QToolTip>
24 #include <KInputDialog>
25 #include <ggadget/file_manager_interface.h>
26 #include <ggadget/gadget_consts.h>
27 #include <ggadget/logger.h>
28 #include <ggadget/decorated_view_host.h>
29 #include <ggadget/options_interface.h>
30 #include <ggadget/script_context_interface.h>
31 #include <ggadget/script_runtime_interface.h>
32 #include <ggadget/script_runtime_manager.h>
33 #include <ggadget/qt/qt_graphics.h>
34 #include <ggadget/qt/utilities.h>
35 #include "plasma_view_host.h"
36 #include "plasma_view_host_internal.h"
38 #include <Plasma/Applet>
40 using namespace ggadget::qt;
41 namespace ggadget {
43 PlasmaViewHost::PlasmaViewHost(GadgetInfo *info, ViewHostInterface::Type type,
44 bool popout)
45 : d(new Private(info, type, popout)) {
48 PlasmaViewHost::~PlasmaViewHost() {
49 delete d;
52 void PlasmaViewHost::Destroy() {
53 delete this;
56 void PlasmaViewHost::SetView(ViewInterface *view) {
57 kDebug() << "PlasmaViewHost::SetView:" << this << "," << view;
58 if (d->view_ == view) return;
59 d->detach();
60 d->view_ = view;
63 void *PlasmaViewHost::GetNativeWidget() const {
64 return d->widget_;
67 void PlasmaViewHost::ViewCoordToNativeWidgetCoord(
68 double x, double y, double *widget_x, double *widget_y) const {
69 double zoom = d->view_->GetGraphics()->GetZoom();
70 if (widget_x)
71 *widget_x = x * zoom;
72 if (widget_y)
73 *widget_y = y * zoom;
76 void PlasmaViewHost::NativeWidgetCoordToViewCoord(
77 double x, double y, double *view_x, double *view_y) const {
78 double zoom = d->view_->GetGraphics()->GetZoom();
79 if (zoom == 0) return;
80 if (view_x) *view_x = x / zoom;
81 if (view_y) *view_y = y / zoom;
84 void PlasmaViewHost::QueueDraw() {
85 d->queueDraw();
88 void PlasmaViewHost::QueueResize() {
89 d->queueResize();
92 void PlasmaViewHost::EnableInputShapeMask(bool enable) {
95 void PlasmaViewHost::SetResizable(ViewInterface::ResizableMode mode) {
96 if (d->type_ != ViewHostInterface::VIEW_HOST_MAIN || d->is_popout_ ||
97 !d->info->applet)
98 return;
99 if (mode == ViewInterface::RESIZABLE_TRUE)
100 d->info->applet->setAspectRatioMode(Plasma::IgnoreAspectRatio);
101 else
102 d->info->applet->setAspectRatioMode(Plasma::KeepAspectRatio);
103 kDebug() << "SetResizable:" << mode << d->info->applet->aspectRatioMode();
106 void PlasmaViewHost::SetCaption(const std::string &caption) {
107 d->caption_ = QString::fromUtf8(caption.c_str());
108 if (d->parent_widget_)
109 d->parent_widget_->setWindowTitle(d->caption_);
112 void PlasmaViewHost::SetShowCaptionAlways(bool always) {
113 // TODO:
116 void PlasmaViewHost::SetCursor(ggadget::ViewInterface::CursorType type) {
117 Qt::CursorShape shape = ggadget::qt::GetQtCursorShape(type);
118 // Up to Qt4.4.3, There is a bug in handling cursor when
119 // QGraphicsProxyWidget is involved.
120 d->info->applet->setCursor(shape);
121 if (d->widget_)
122 d->widget_->setCursor(shape);
125 void PlasmaViewHost::ShowTooltip(const std::string &tooltip) {
126 QToolTip::showText(QCursor::pos(), QString::fromUtf8(tooltip.c_str()));
129 void PlasmaViewHost::ShowTooltipAtPosition(const std::string &tooltip,
130 double x, double y) {
131 // TODO:
134 bool PlasmaViewHost::ShowView(bool modal, int flags,
135 Slot1<bool, int> *feedback_handler) {
136 if (d->showView(modal, flags, feedback_handler)) {
137 if (d->parent_widget_)
138 d->parent_widget_->setWindowTitle(d->caption_);
139 return true;
141 return false;
144 void PlasmaViewHost::CloseView() {
145 d->closeView();
148 bool PlasmaViewHost::ShowContextMenu(int button) {
149 return d->showContextMenu(button);
152 void PlasmaViewHost::Alert(const ViewInterface *view, const char *message) {
153 KMessageBox::information(NULL,message,
154 view->GetCaption().c_str());
157 ViewHostInterface::ConfirmResponse PlasmaViewHost::Confirm(
158 const ViewInterface *view, const char *message, bool) {
159 int ret = KMessageBox::questionYesNo(NULL,
160 message,
161 view->GetCaption().c_str() );
162 if (ret == KMessageBox::Yes)
163 return CONFIRM_YES;
164 else
165 return CONFIRM_NO;
168 std::string PlasmaViewHost::Prompt(const ViewInterface *view,
169 const char *message,
170 const char *default_value) {
171 QString s = KInputDialog::getText(view->GetCaption().c_str(),
172 message);
173 return s.toUtf8().data();
176 ViewHostInterface::Type PlasmaViewHost::GetType() const {
177 return d->type_;
180 ViewInterface *PlasmaViewHost::GetView() const {
181 return d->view_;
184 int PlasmaViewHost::GetDebugMode() const {
185 return d->info->view_debug_mode;
188 GadgetInfo *PlasmaViewHost::getInfo() {
189 return d->info;
192 } // namespace ggadget
193 #include "plasma_view_host_internal.moc"