add more spacing
[personal-kdebase.git] / workspace / plasma / scriptengines / google_gadgets / plasma_view_host_internal.h
blob11e84e3d58e4c15df28540baee7a53f4eba7f395
1 /*
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.
17 #ifndef HOSTS_PLASMA_VIEW_HOST_INTERNAL_H__
18 #define HOSTS_PLASMA_VIEW_HOST_INTERNAL_H__
19 #include <Plasma/Dialog>
20 #include <Plasma/Applet>
21 #include <ggadget/qt/qt_menu.h>
22 #include <ggadget/view_interface.h>
23 #include <ggadget/qt/utilities.h>
24 #include <QDialogButtonBox>
25 #include <QVBoxLayout>
26 namespace ggadget{
28 class PlasmaViewHost::Private : public QObject {
29 Q_OBJECT
30 public:
31 Private(GadgetInfo *i, Type type, bool popout)
32 : view_(NULL),
33 parent_widget_(NULL),
34 widget_(NULL),
35 type_(type),
36 info(i),
37 is_popout_(popout),
38 gadget_w_(0),
39 gadget_h_(0),
40 feedback_handler_(NULL) {}
42 ~Private() {
43 closeView();
46 static void embedWidget(QGraphicsWidget *parent, QWidget *widget) {
47 widget->setAttribute(Qt::WA_NoSystemBackground);
48 QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(parent);
49 layout->setSpacing(0);
50 QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget(parent);
51 proxy->setWidget(widget);
52 layout->addItem(proxy);
53 parent->setLayout(layout);
54 DLOG("EmbededWidget: widget:%p, applet:%p, layout:%p, proxy:%p",
55 widget, parent, layout, proxy);
58 /* Show the view in right place
59 * - floating main view: Shown within the applet
60 * - popouted main view and details view: Shown in QtViewWidget
62 bool showView(bool modal, int flags, Slot1<bool, int> *feedback_handler) {
63 ASSERT(view_);
64 if (feedback_handler_ && feedback_handler_ != feedback_handler)
65 delete feedback_handler_;
66 feedback_handler_ = feedback_handler;
68 if (widget_) return true;
70 if (type_ == ViewHostInterface::VIEW_HOST_MAIN && !is_popout_) {
71 // normal main view
72 if (info->widget == NULL) {
73 widget_ = new QtViewWidget(view_, 0);
74 embedWidget(info->applet, widget_);
75 info->widget = widget_;
76 } else {
77 widget_ = info->widget;
78 widget_->SetView(view_);
79 adjustAppletSize();
81 info->applet->setBackgroundHints(Plasma::Applet::NoBackground);
82 if (info->applet->location() == Plasma::Floating) {
83 connect(widget_, SIGNAL(moved(int, int)),
84 this, SLOT(onViewMoved(int, int)));
85 } else {
86 disconnect();
89 if (info->applet->formFactor() == Plasma::Vertical)
90 view_->SetWidth(info->applet->size().width());
91 if (info->applet->formFactor() == Plasma::Horizontal)
92 view_->SetHeight(info->applet->size().height());
93 } else {
94 // Popouted main view and details view
95 widget_ = new QtViewWidget(view_, QtViewWidget::FLAG_MOVABLE);
96 parent_widget_ = widget_;
97 SetGadgetWindowIcon(widget_, view_->GetGadget());
98 if (info->expanded_main_view_host
99 && type_ == ViewHostInterface::VIEW_HOST_DETAILS) {
100 int w = view_->GetWidth();
101 int h = view_->GetHeight();
102 QWidget *expanded =
103 static_cast<QWidget*>(info->expanded_main_view_host->GetNativeWidget());
104 QPoint p = ggadget::qt::GetPopupPosition(expanded->geometry(), QSize(w, h));
105 widget_->move(p);
106 } else {
107 widget_->move(info->applet->popupPosition(widget_->sizeHint()));
109 widget_->show();
111 return true;
114 void closeView() {
115 kDebug() << "CloseView";
116 if (parent_widget_) {
117 delete parent_widget_;
118 parent_widget_ = NULL;
119 widget_ = NULL;
120 } else {
121 if (info->applet && widget_) {
122 // widget_ is owned by applet, so if applet is null, widget_ is
123 // destroyed already
124 widget_->SetView(NULL);
126 widget_ = NULL;
130 void queueDraw() {
131 if (parent_widget_)
132 parent_widget_->update();
133 else if (info->applet)
134 info->applet->update();
137 // This is called when view size has changed, caused by constraintsEvent or
138 // user manually resizes gadget. Applet and widget size will be adjusted
139 // according to view size.
140 void adjustAppletSize() {
141 if (!info->main_view_host || !info->applet) return;
142 ViewInterface *view = info->main_view_host->GetViewDecorator();
143 double w = view->GetWidth();
144 double h = view->GetHeight();
145 if (w <= 0 || h <= 0) return;
146 if (gadget_w_ == w && gadget_h_ == h) return;
148 gadget_w_ = w;
149 gadget_h_ = h;
150 kDebug() << "view size:" << w << " " << h;
152 kDebug() << "applet old size:" << info->applet->size();
153 if (info->applet->location() == Plasma::Floating) {
154 info->applet->resize(w, h);
155 } else {
156 if (isHorizontal(info->applet->location()))
157 info->applet->setMaximumWidth(w);
158 else
159 info->applet->setMaximumHeight(h);
161 kDebug() << "applet new size:" << info->applet->size();
163 if (widget_) {
164 kDebug() << "widget old size:" << widget_->size();
165 widget_->resize(w, h);
166 kDebug() << "widget new size:" << widget_->size();
170 void queueResize() {
171 if (type_ == ViewHostInterface::VIEW_HOST_MAIN && !is_popout_) {
172 adjustAppletSize();
173 } else if (widget_) {
174 widget_->AdjustToViewSize();
178 bool showContextMenu(int button) {
179 ASSERT(view_);
180 Q_UNUSED(button);
181 context_menu_.clear();
182 QtMenu qt_menu(&context_menu_);
183 view_->OnAddContextMenuItems(&qt_menu);
184 if (!context_menu_.isEmpty()) {
185 context_menu_.popup(QCursor::pos());
186 return true;
187 } else {
188 return false;
192 ViewInterface *view_;
193 QWidget *parent_widget_;
194 QtViewWidget *widget_;
195 ViewHostInterface::Type type_;
196 GadgetInfo *info;
197 bool is_popout_;
198 double gadget_w_;
199 double gadget_h_;
201 Slot1<bool, int> *feedback_handler_;
202 QString caption_;
203 QMenu context_menu_;
205 void detach() {
206 view_ = NULL;
209 public slots:
210 void onViewMoved(int x, int y) {
211 if (type_ == ViewHostInterface::VIEW_HOST_MAIN && !is_popout_ &&
212 info->applet->immutability() == Plasma::Mutable)
213 info->applet->moveBy(x, y);
217 } // namespace ggadget
219 #endif