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>
28 class PlasmaViewHost::Private
: public QObject
{
31 Private(GadgetInfo
*i
, Type type
, bool popout
)
40 feedback_handler_(NULL
) {}
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
) {
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_
) {
72 if (info
->widget
== NULL
) {
73 widget_
= new QtViewWidget(view_
, 0);
74 embedWidget(info
->applet
, widget_
);
75 info
->widget
= widget_
;
77 widget_
= info
->widget
;
78 widget_
->SetView(view_
);
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)));
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());
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();
103 static_cast<QWidget
*>(info
->expanded_main_view_host
->GetNativeWidget());
104 QPoint p
= ggadget::qt::GetPopupPosition(expanded
->geometry(), QSize(w
, h
));
107 widget_
->move(info
->applet
->popupPosition(widget_
->sizeHint()));
115 kDebug() << "CloseView";
116 if (parent_widget_
) {
117 delete parent_widget_
;
118 parent_widget_
= NULL
;
121 if (info
->applet
&& widget_
) {
122 // widget_ is owned by applet, so if applet is null, widget_ is
124 widget_
->SetView(NULL
);
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;
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
);
156 if (isHorizontal(info
->applet
->location()))
157 info
->applet
->setMaximumWidth(w
);
159 info
->applet
->setMaximumHeight(h
);
161 kDebug() << "applet new size:" << info
->applet
->size();
164 kDebug() << "widget old size:" << widget_
->size();
165 widget_
->resize(w
, h
);
166 kDebug() << "widget new size:" << widget_
->size();
171 if (type_
== ViewHostInterface::VIEW_HOST_MAIN
&& !is_popout_
) {
173 } else if (widget_
) {
174 widget_
->AdjustToViewSize();
178 bool showContextMenu(int 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());
192 ViewInterface
*view_
;
193 QWidget
*parent_widget_
;
194 QtViewWidget
*widget_
;
195 ViewHostInterface::Type type_
;
201 Slot1
<bool, int> *feedback_handler_
;
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