Plasma
plasma_view_host_internal.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef HOSTS_PLASMA_VIEW_HOST_INTERNAL_H__
00018 #define HOSTS_PLASMA_VIEW_HOST_INTERNAL_H__
00019 #include <Plasma/Dialog>
00020 #include <Plasma/Applet>
00021 #include <ggadget/qt/qt_menu.h>
00022 #include <ggadget/view_interface.h>
00023 #include <ggadget/qt/utilities.h>
00024 #include <QDialogButtonBox>
00025 #include <QVBoxLayout>
00026 namespace ggadget{
00027
00028 class PlasmaViewHost::Private : public QObject {
00029 Q_OBJECT
00030 public:
00031 Private(GadgetInfo *i, Type type, bool popout)
00032 : view_(NULL),
00033 parent_widget_(NULL),
00034 widget_(NULL),
00035 type_(type),
00036 info(i),
00037 is_popout_(popout),
00038 gadget_w_(0),
00039 gadget_h_(0),
00040 feedback_handler_(NULL) {}
00041
00042 ~Private() {
00043 closeView();
00044 }
00045
00046 static void embedWidget(QGraphicsWidget *parent, QWidget *widget) {
00047 widget->setAttribute(Qt::WA_NoSystemBackground);
00048 QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(parent);
00049 layout->setSpacing(0);
00050 layout->setContentsMargins(0, 0, 0, 0);
00051 QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget(parent);
00052 proxy->setWidget(widget);
00053 layout->addItem(proxy);
00054 parent->setLayout(layout);
00055 DLOG("EmbededWidget: widget:%p, applet:%p, layout:%p, proxy:%p",
00056 widget, parent, layout, proxy);
00057 }
00058
00059
00060
00061
00062
00063 bool showView(bool modal, int flags, Slot1<bool, int> *feedback_handler) {
00064 ASSERT(view_);
00065 if (feedback_handler_ && feedback_handler_ != feedback_handler)
00066 delete feedback_handler_;
00067 feedback_handler_ = feedback_handler;
00068
00069 if (widget_) return true;
00070
00071 if (type_ == ViewHostInterface::VIEW_HOST_MAIN && !is_popout_) {
00072
00073 if (info->widget == NULL) {
00074 widget_ = new QtViewWidget(view_, 0);
00075 embedWidget(info->applet, widget_);
00076 info->widget = widget_;
00077 } else {
00078 widget_ = info->widget;
00079 widget_->SetView(view_);
00080 adjustAppletSize();
00081 }
00082 info->applet->setBackgroundHints(Plasma::Applet::NoBackground);
00083 if (info->applet->location() == Plasma::Floating) {
00084 connect(widget_, SIGNAL(moved(int, int)),
00085 this, SLOT(onViewMoved(int, int)));
00086 } else {
00087 disconnect();
00088 }
00089
00090 if (info->applet->formFactor() == Plasma::Vertical)
00091 view_->SetWidth(info->applet->size().width());
00092 if (info->applet->formFactor() == Plasma::Horizontal)
00093 view_->SetHeight(info->applet->size().height());
00094 } else {
00095
00096 widget_ = new QtViewWidget(view_, QtViewWidget::FLAG_MOVABLE);
00097 parent_widget_ = widget_;
00098 SetGadgetWindowIcon(widget_, view_->GetGadget());
00099 if (info->expanded_main_view_host
00100 && type_ == ViewHostInterface::VIEW_HOST_DETAILS) {
00101 int w = view_->GetWidth();
00102 int h = view_->GetHeight();
00103 QWidget *expanded =
00104 static_cast<QWidget*>(info->expanded_main_view_host->GetNativeWidget());
00105 QPoint p = ggadget::qt::GetPopupPosition(expanded->geometry(), QSize(w, h));
00106 widget_->move(p);
00107 } else {
00108 widget_->move(info->applet->popupPosition(widget_->sizeHint()));
00109 }
00110 widget_->show();
00111 }
00112 return true;
00113 }
00114
00115 void closeView() {
00116 kDebug() << "CloseView";
00117 if (parent_widget_) {
00118 delete parent_widget_;
00119 parent_widget_ = NULL;
00120 widget_ = NULL;
00121 } else {
00122 if (info->applet && widget_) {
00123
00124
00125 widget_->SetView(NULL);
00126 }
00127 widget_ = NULL;
00128 }
00129 }
00130
00131 void queueDraw() {
00132 if (parent_widget_)
00133 parent_widget_->update();
00134 else if (info->applet)
00135 info->applet->update();
00136 }
00137
00138
00139
00140
00141 void adjustAppletSize() {
00142 if (!info->main_view_host || !info->applet) return;
00143 ViewInterface *view = info->main_view_host->GetViewDecorator();
00144 double w = view->GetWidth();
00145 double h = view->GetHeight();
00146 if (w <= 0 || h <= 0) return;
00147 if (gadget_w_ == w && gadget_h_ == h) return;
00148
00149 gadget_w_ = w;
00150 gadget_h_ = h;
00151 #if 0
00152 kDebug() << "view size:" << w << " " << h;
00153 kDebug() << "applet old size:" << info->applet->size();
00154
00155 if (info->applet->location() == Plasma::Floating) {
00156 info->applet->resize(w, h);
00157 } else {
00158 if (isHorizontal(info->applet->location()))
00159 info->applet->setMaximumWidth(w);
00160 else
00161 info->applet->setMaximumHeight(h);
00162 }
00163 kDebug() << "applet new size:" << info->applet->size();
00164 #endif
00165
00166 if (widget_) {
00167 kDebug() << "widget old size:" << widget_->size();
00168 widget_->resize(w, h);
00169 kDebug() << "widget new size:" << widget_->size();
00170 }
00171 }
00172
00173 void queueResize() {
00174 if (type_ == ViewHostInterface::VIEW_HOST_MAIN && !is_popout_) {
00175 adjustAppletSize();
00176 } else if (widget_) {
00177 widget_->AdjustToViewSize();
00178 }
00179 }
00180
00181 bool showContextMenu(int button) {
00182 ASSERT(view_);
00183 Q_UNUSED(button);
00184 context_menu_.clear();
00185 QtMenu qt_menu(&context_menu_);
00186 view_->OnAddContextMenuItems(&qt_menu);
00187 if (!context_menu_.isEmpty()) {
00188 context_menu_.popup(QCursor::pos());
00189 return true;
00190 } else {
00191 return false;
00192 }
00193 }
00194
00195 ViewInterface *view_;
00196 QWidget *parent_widget_;
00197 QtViewWidget *widget_;
00198 ViewHostInterface::Type type_;
00199 GadgetInfo *info;
00200 bool is_popout_;
00201 double gadget_w_;
00202 double gadget_h_;
00203
00204 Slot1<bool, int> *feedback_handler_;
00205 QString caption_;
00206 QMenu context_menu_;
00207
00208 void detach() {
00209 view_ = NULL;
00210 }
00211
00212 public slots:
00213 void onViewMoved(int x, int y) {
00214 if (type_ == ViewHostInterface::VIEW_HOST_MAIN && !is_popout_ &&
00215 info->applet->immutability() == Plasma::Mutable)
00216 info->applet->moveBy(x, y);
00217 }
00218 };
00219
00220 }
00221
00222 #endif