~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/scriptengines/google_gadgets/plasma_view_host.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright 2007 Google Inc.
 
3
 
 
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
 
7
 
 
8
       http://www.apache.org/licenses/LICENSE-2.0
 
9
 
 
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.
 
15
*/
 
16
 
 
17
#include "plasma_view_host.h"
 
18
 
 
19
#include <sys/time.h>
 
20
 
 
21
#include <QtGui/QGraphicsProxyWidget>
 
22
#include <QtGui/QGraphicsLinearLayout>
 
23
#include <QtGui/QToolTip>
 
24
 
 
25
#include <KDebug>
 
26
#include <KInputDialog>
 
27
#include <KMessageBox>
 
28
#include <Plasma/Applet>
 
29
#include <Plasma/ToolTipContent>
 
30
#include <Plasma/ToolTipManager>
 
31
 
 
32
#include <ggadget/file_manager_interface.h>
 
33
#include <ggadget/gadget_consts.h>
 
34
#include <ggadget/logger.h>
 
35
#include <ggadget/decorated_view_host.h>
 
36
#include <ggadget/options_interface.h>
 
37
#include <ggadget/script_context_interface.h>
 
38
#include <ggadget/script_runtime_interface.h>
 
39
#include <ggadget/script_runtime_manager.h>
 
40
#include <ggadget/qt/qt_graphics.h>
 
41
#include <ggadget/qt/utilities.h>
 
42
 
 
43
#include "plasma_view_host_internal.h"
 
44
 
 
45
 
 
46
using namespace ggadget::qt;
 
47
namespace ggadget {
 
48
 
 
49
PlasmaViewHost::PlasmaViewHost(GadgetInfo *info, ViewHostInterface::Type type,
 
50
                               bool popout)
 
51
  : d(new Private(info, type, popout)) {
 
52
}
 
53
 
 
54
PlasmaViewHost::~PlasmaViewHost() {
 
55
  delete d;
 
56
}
 
57
 
 
58
void PlasmaViewHost::Destroy() {
 
59
  delete this;
 
60
}
 
61
 
 
62
void PlasmaViewHost::SetView(ViewInterface *view) {
 
63
  kDebug() << "PlasmaViewHost::SetView:" << this << "," << view;
 
64
  d->view_ = view;
 
65
}
 
66
 
 
67
void *PlasmaViewHost::GetNativeWidget() const {
 
68
  return d->widget_;
 
69
}
 
70
 
 
71
void PlasmaViewHost::ViewCoordToNativeWidgetCoord(
 
72
    double x, double y, double *widget_x, double *widget_y) const {
 
73
  double zoom = d->view_->GetGraphics()->GetZoom();
 
74
  if (widget_x) *widget_x = x * zoom;
 
75
  if (widget_y) *widget_y = y * zoom;
 
76
}
 
77
 
 
78
void PlasmaViewHost::NativeWidgetCoordToViewCoord(
 
79
    double x, double y, double *view_x, double *view_y) const {
 
80
  double zoom = d->view_->GetGraphics()->GetZoom();
 
81
  if (zoom == 0) return;
 
82
  if (view_x) *view_x = x / zoom;
 
83
  if (view_y) *view_y = y / zoom;
 
84
}
 
85
 
 
86
void PlasmaViewHost::QueueDraw() {
 
87
  d->queueDraw();
 
88
}
 
89
 
 
90
void PlasmaViewHost::QueueResize() {
 
91
  d->queueResize();
 
92
}
 
93
 
 
94
void PlasmaViewHost::SetResizable(ViewInterface::ResizableMode mode) {
 
95
  if (d->type_ != ViewHostInterface::VIEW_HOST_MAIN ||
 
96
      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();
 
104
}
 
105
 
 
106
static void UpdateTooltip(Plasma::Applet *applet, const QString &text) {
 
107
  if (isHorizontal(applet->location())) {
 
108
    Plasma::ToolTipContent data;
 
109
    data.setMainText(text);
 
110
    Plasma::ToolTipManager::self()->setContent(applet, data);
 
111
  }
 
112
}
 
113
 
 
114
void PlasmaViewHost::SetCaption(const std::string &caption) {
 
115
  d->caption_ = QString::fromUtf8(caption.c_str());
 
116
  if (d->parent_widget_) {
 
117
    d->parent_widget_->setWindowTitle(d->caption_);
 
118
  } else {
 
119
    UpdateTooltip(d->info->applet, d->caption_);
 
120
  }
 
121
}
 
122
 
 
123
void PlasmaViewHost::SetCursor(ggadget::ViewInterface::CursorType type) {
 
124
  Qt::CursorShape shape = ggadget::qt::GetQtCursorShape(type);
 
125
  // Up to Qt4.4.3, There is a bug in handling cursor when
 
126
  // QGraphicsProxyWidget is involved.
 
127
  d->info->applet->setCursor(shape);
 
128
  if (d->widget_)
 
129
    d->widget_->setCursor(shape);
 
130
}
 
131
 
 
132
void PlasmaViewHost::ShowTooltip(const std::string &tooltip) {
 
133
  QToolTip::showText(QCursor::pos(), QString::fromUtf8(tooltip.c_str()));
 
134
}
 
135
 
 
136
void PlasmaViewHost::ShowTooltipAtPosition(const std::string &tooltip,
 
137
                                           double x, double y) {
 
138
  double widget_x = 0, widget_y = 0;
 
139
  ViewCoordToNativeWidgetCoord(x, y, &widget_x, &widget_y);
 
140
  QToolTip::showText(QPoint(widget_x, widget_y),
 
141
                     QString::fromUtf8(tooltip.c_str()));
 
142
}
 
143
 
 
144
bool PlasmaViewHost::ShowView(bool modal, int flags,
 
145
                              Slot1<bool, int> *feedback_handler) {
 
146
  if (d->showView(modal, flags, feedback_handler)) {
 
147
    if (d->parent_widget_) {
 
148
      d->parent_widget_->setWindowTitle(d->caption_);
 
149
    } else {
 
150
      UpdateTooltip(d->info->applet, d->caption_);
 
151
    }
 
152
    return true;
 
153
  }
 
154
  return false;
 
155
}
 
156
 
 
157
void PlasmaViewHost::CloseView() {
 
158
  d->closeView();
 
159
}
 
160
 
 
161
bool PlasmaViewHost::ShowContextMenu(int button) {
 
162
  return d->showContextMenu(button);
 
163
}
 
164
 
 
165
void PlasmaViewHost::Alert(const ViewInterface *view, const char *message) {
 
166
  KMessageBox::information(NULL,message,
 
167
                           view->GetCaption().c_str());
 
168
}
 
169
 
 
170
ViewHostInterface::ConfirmResponse PlasmaViewHost::Confirm(
 
171
    const ViewInterface *view, const char *message, bool) {
 
172
  int ret = KMessageBox::questionYesNo(NULL,
 
173
                                       message,
 
174
                                       view->GetCaption().c_str() );
 
175
  return ret == KMessageBox::Yes ? CONFIRM_YES : CONFIRM_NO;
 
176
}
 
177
 
 
178
std::string PlasmaViewHost::Prompt(const ViewInterface *view,
 
179
                                   const char *message,
 
180
                                   const char *default_value) {
 
181
  QString s = KInputDialog::getText(view->GetCaption().c_str(),
 
182
                                    message,
 
183
                                    default_value);
 
184
  return s.toUtf8().data();
 
185
}
 
186
 
 
187
ViewHostInterface::Type PlasmaViewHost::GetType() const {
 
188
  return d->type_;
 
189
}
 
190
 
 
191
ViewInterface *PlasmaViewHost::GetView() const {
 
192
  return d->view_;
 
193
}
 
194
 
 
195
int PlasmaViewHost::GetDebugMode() const {
 
196
  return d->info->view_debug_mode;
 
197
}
 
198
 
 
199
GadgetInfo *PlasmaViewHost::getInfo() {
 
200
  return d->info;
 
201
}
 
202
 
 
203
} // namespace ggadget
 
204
#include "plasma_view_host_internal.moc"